feiyu02
2025-05-28 19c90b219bd80f82bfcf799a7adfd03fd469c0b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.flightfeather.uav.socket.handler
 
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.ChannelInboundHandlerAdapter
import java.text.SimpleDateFormat
import java.util.*
 
/**
 * socket消息处理积基类
 * @date 2025/5/13
 * @author feiyu02
 */
abstract class BaseHandler : ChannelInboundHandlerAdapter() {
 
    abstract var tag: String
 
    override fun channelRegistered(ctx: ChannelHandlerContext?) {
        println("------【${tag}】IP连接:[ip:${ctx?.channel()?.remoteAddress()}] ${
            SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(
                Date()
            )}")
    }
 
    override fun channelActive(ctx: ChannelHandlerContext?) {
        println("------【${tag}】IP激活:[ip:${ctx?.channel()?.remoteAddress()}] ${
            SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(
                Date()
            )}")
    }
 
    override fun channelRead(ctx: ChannelHandlerContext?, msg: Any?) {
        println("------【${tag}】收到的原始数据:[ip:${ctx?.channel()?.remoteAddress()}] ${
            SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(
                Date()
            )}")
    }
 
    override fun channelInactive(ctx: ChannelHandlerContext?) {
        println("------【${tag}】端口有IP不活动:[ip:${ctx?.channel()?.remoteAddress()}] ${
            SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(
                Date()
            )}")
    }
 
    @Deprecated("Deprecated in Java")
    override fun exceptionCaught(ctx: ChannelHandlerContext?, cause: Throwable?) {
        cause?.printStackTrace()
        ctx?.close()
    }
}