riku
2019-09-05 28b10b7255483c8e2eb5e3e828f8cda658e94c44
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
package com.flightfeather.obd.socket
 
import io.netty.channel.ChannelHandlerContext
import io.netty.channel.ChannelInboundHandlerAdapter
import io.netty.util.AttributeKey
 
 
class ServerHandler : ChannelInboundHandlerAdapter() {
 
    val attributeKey = AttributeKey.valueOf<String>("deviceCode")
 
    override fun channelRegistered(ctx: ChannelHandlerContext?) {
        super.channelRegistered(ctx)
    }
 
    override fun channelActive(ctx: ChannelHandlerContext?) {
        super.channelActive(ctx)
    }
 
    override fun channelRead(ctx: ChannelHandlerContext?, msg: Any?) {
        super.channelRead(ctx, msg)
        println("------收到的原始数据:[ip:${ctx?.channel()?.remoteAddress()}]\r\n$msg")
        if (msg is String) {
            MessageManager().dealMsg(msg, ctx)
        }
 
//        val attribute = ctx?.channel()?.attr(attributeKey)?.apply {
//            if (get() == null) {
//                set(data.obdVin)
//            }
//        }
 
    }
 
    override fun channelReadComplete(ctx: ChannelHandlerContext?) {
        super.channelReadComplete(ctx)
    }
 
    override fun channelInactive(ctx: ChannelHandlerContext?) {
        super.channelInactive(ctx)
    }
 
    override fun exceptionCaught(ctx: ChannelHandlerContext?, cause: Throwable?) {
        cause?.printStackTrace()
        ctx?.close()
    }
}