package com.anyun.exam.lib.net
|
|
import android.util.Log
|
import io.netty.channel.ChannelHandler
|
import io.netty.channel.ChannelHandlerContext
|
import io.netty.channel.ChannelInboundHandlerAdapter
|
import io.netty.handler.timeout.IdleState
|
import io.netty.handler.timeout.IdleStateEvent
|
import io.netty.util.AttributeKey
|
|
@ChannelHandler.Sharable
|
class ConnectorIdleStateTrigger : ChannelInboundHandlerAdapter() {
|
internal var KEY = AttributeKey.valueOf<NtripTcpClient>("TcpClient")
|
|
override fun userEventTriggered(ctx: ChannelHandlerContext, evt: Any) {
|
if (evt is IdleStateEvent) {
|
val tcpClient = ctx.channel().attr(KEY).get()
|
val state = evt.state()
|
// Log.d(Constant.LOGTAG, "tcp idle =$state")
|
if(state == IdleState.WRITER_IDLE) {
|
// Log.d(Constant.LOGTAG,"超时自动关闭连接 ${tcpClient.session.ip}:${tcpClient.session.port}")
|
}
|
if(state == IdleState.READER_IDLE) {
|
// Log.d(Constant.LOGTAG,"超时自动关闭连接 ${tcpClient.session.ip}:${tcpClient.session.port}")
|
tcpClient.close(true)
|
}
|
} else {
|
super.userEventTriggered(ctx, evt)
|
}
|
}
|
}
|