Dana
2025-12-01 faf9ab5bc6f172819dd5c0cd6dcc0ebb82391c1e
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package com.anyun.h264;
 
 
import com.anyun.h264.util.BytesUtils;
 
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import timber.log.Timber;
 
import java.util.concurrent.TimeUnit;
 
/**
 * JT/T 1076-2016 TCP客户端工具类
 * 使用Netty实现TCP连接和RTP包发送
 */
public class JT1076TcpClient {
    private static final String TAG = "JT1076TcpClient";
    
    private String serverIp;
    private int serverPort;
    private EventLoopGroup workerGroup;
    private Channel channel;
    private boolean isConnected = false;
    
    // 连接状态回调接口
    public interface ConnectionListener {
        void onConnected();
        void onDisconnected();
        void onError(Throwable cause);
    }
    
    private ConnectionListener connectionListener;
    
    /**
     * 设置服务器地址
     */
    public void setServerAddress(String ip, int port) {
        this.serverIp = ip;
        this.serverPort = port;
    }
    
    /**
     * 设置连接状态监听器
     */
    public void setConnectionListener(ConnectionListener listener) {
        this.connectionListener = listener;
    }
    
    /**
     * 初始化TCP连接(异步)
     */
    public void connect() {
        if (serverIp == null || serverIp.isEmpty()) {
            Timber.e( "Server IP not set");
            if (connectionListener != null) {
                connectionListener.onError(new IllegalArgumentException("Server IP not set"));
            }
            return;
        }
        
        if (workerGroup != null) {
            Timber.w("TCP client already initialized, disconnecting first");
            disconnect();
        }
        
        // 创建EventLoopGroup(使用单线程组即可)
        workerGroup = new NioEventLoopGroup(1);
        
        try {
            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(workerGroup)
                    .channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY, true) // 禁用Nagle算法,降低延迟
                    .option(ChannelOption.SO_KEEPALIVE, true) // 启用TCP keepalive
                    .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000) // 连接超时5秒
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        protected void initChannel(SocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new TcpClientHandler());
                        }
                    });
            
            Timber.d("Connecting to TCP server: " + serverIp + ":" + serverPort);
            
            // 异步连接
            ChannelFuture future = bootstrap.connect(serverIp, serverPort);
            future.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (future.isSuccess()) {
                        channel = future.channel();
                        isConnected = true;
                        Timber.d("TCP connection established: " + serverIp + ":" + serverPort);
                        if (connectionListener != null) {
                            connectionListener.onConnected();
                        }
                    } else {
                        isConnected = false;
                        Throwable cause = future.cause();
                        Timber.e(cause, "TCP connection failed: " + serverIp + ":" + serverPort);
                        if (connectionListener != null) {
                            connectionListener.onError(cause);
                        }
                        // 连接失败时清理资源
                        shutdown();
                    }
                }
            });
            
        } catch (Exception e) {
            Timber.e(e, "Initialize TCP client failed");
            isConnected = false;
            if (connectionListener != null) {
                connectionListener.onError(e);
            }
            shutdown();
        }
    }
    
    /**
     * 发送RTP包(TCP方式)
     */
    public void sendPacket(byte[] packet) {
        if (!isConnected || channel == null || !channel.isActive()) {
            Timber.w( "TCP channel not connected, packet dropped");
            return;
        }
        
        try {
            // 将字节数组包装为ByteBuf
            ByteBuf buffer = Unpooled.wrappedBuffer(packet);
            String str = BytesUtils.bytesToHexString(  BytesUtils.subArray(buffer.array(),0,30));
            Timber.i( "Send TCP packet:"+ str);
            // 异步写入
            ChannelFuture future = channel.writeAndFlush(buffer);
            
            // 可选:监听发送结果
            future.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    if (!future.isSuccess()) {
                        Timber.e(future.cause(), "Send TCP packet failed");
                    }
                }
            });
            
        } catch (Exception e) {
            Timber.e(e, "Send TCP packet error");
        }
    }
    
    /**
     * 断开TCP连接
     */
    public void disconnect() {
        isConnected = false;
        
        if (channel != null) {
            try {
                ChannelFuture future = channel.close();
                future.await(2, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                Timber.w(e,"Close channel interrupted");
                Thread.currentThread().interrupt();
            } catch (Exception e) {
                Timber.e(e, "Close channel error");
            }
            channel = null;
        }
        
        shutdown();
        
        if (connectionListener != null) {
            connectionListener.onDisconnected();
        }
        
        Timber.d("TCP connection closed");
    }
    
    /**
     * 关闭EventLoopGroup(清理资源)
     */
    private void shutdown() {
        if (workerGroup != null) {
            try {
                workerGroup.shutdownGracefully().await(3, TimeUnit.SECONDS);
            } catch (InterruptedException e) {
                Timber.w(e, "Shutdown worker group interrupted");
                Thread.currentThread().interrupt();
            } catch (Exception e) {
                Timber.e(e, "Shutdown worker group error");
            }
            workerGroup = null;
        }
    }
    
    /**
     * 检查是否已连接
     */
    public boolean isConnected() {
        return isConnected && channel != null && channel.isActive();
    }
    
    /**
     * TCP客户端通道处理器
     */
    private class TcpClientHandler extends ChannelInboundHandlerAdapter {
        
        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            super.channelActive(ctx);
            Timber.d("TCP channel active: " + ctx.channel().remoteAddress());
            isConnected = true;
        }
        
        @Override
        public void channelInactive(ChannelHandlerContext ctx) throws Exception {
            super.channelInactive(ctx);
            Timber.d("TCP channel inactive: " + ctx.channel().remoteAddress());
            isConnected = false;
            channel = null;
            if (connectionListener != null) {
                connectionListener.onDisconnected();
            }
        }
        
        @Override
        public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
            Timber.e(cause, "TCP channel exception");
            isConnected = false;
            if (connectionListener != null) {
                connectionListener.onError(cause);
            }
            ctx.close();
        }
        
        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            // 如果服务器有响应,可以在这里处理
            // 对于JT/T 1076-2016 RTP发送,通常不需要处理响应
            ByteBuf buf = (ByteBuf) msg;
            Timber.d("Received data from server: " + buf.readableBytes() + " bytes");
            buf.release(); // 释放ByteBuf
        }
    }
}