yy1717
2024-02-28 27fc91fbe8f88b6885356e68828cfe1ce1db7601
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
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)
        }
    }
}