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()
|
}
|
}
|