riku
2019-09-05 197d6766d056fda4fdf9e1f9ee26de9f9a480ef5
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.obd.socket
 
import com.flightfeather.obd.domain.entity.ObdData
import com.google.gson.Gson
import io.netty.channel.ChannelHandler
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("------收到的原始数据:$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()
    }
}