|
@@ -8,7 +8,11 @@ import java.util.Scanner;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
import io.netty.bootstrap.Bootstrap;
|
|
|
-import io.netty.channel.*;
|
|
|
+import io.netty.channel.Channel;
|
|
|
+import io.netty.channel.ChannelFuture;
|
|
|
+import io.netty.channel.ChannelFutureListener;
|
|
|
+import io.netty.channel.ChannelInitializer;
|
|
|
+import io.netty.channel.ChannelOption;
|
|
|
import io.netty.channel.nio.NioEventLoopGroup;
|
|
|
import io.netty.channel.socket.SocketChannel;
|
|
|
import io.netty.channel.socket.nio.NioSocketChannel;
|
|
@@ -23,14 +27,16 @@ import io.netty.util.CharsetUtil;
|
|
|
public class TcpClient {
|
|
|
private String TAG = TcpClient.class.getSimpleName();
|
|
|
|
|
|
- private NioEventLoopGroup workGroup = new NioEventLoopGroup();
|
|
|
+ private NioEventLoopGroup workGroup = new NioEventLoopGroup(2);
|
|
|
public Channel channel;
|
|
|
private Bootstrap bootstrap;
|
|
|
|
|
|
//数据处理
|
|
|
- TcpClientHandler tcpClientHandler = new TcpClientHandler();
|
|
|
+ private TcpClientHandler tcpClientHandler = new TcpClientHandler();
|
|
|
+ //是否运行中
|
|
|
+ public boolean isRunning = false;
|
|
|
//重试间隔
|
|
|
- private Integer retrySeconds = 2;
|
|
|
+ private Integer retrySeconds = 5;
|
|
|
//重试计数
|
|
|
private Integer retryTimes = 1;
|
|
|
|
|
@@ -61,14 +67,14 @@ public class TcpClient {
|
|
|
// LengthFieldPrepender是一个编码器,主要是在响应字节数据前面添加字节长度字段
|
|
|
socketChannel.pipeline().addLast(new LengthFieldPrepender(2));
|
|
|
//心跳包应当小于服务器间隔
|
|
|
-// socketChannel.pipeline().addLast(new IdleStateHandler(0, hbSeconds, 0, TimeUnit.SECONDS));
|
|
|
- socketChannel.pipeline().addLast(new IdleStateHandler(hbSeconds+1, hbSeconds, 0, TimeUnit.SECONDS));
|
|
|
+ socketChannel.pipeline().addLast(new IdleStateHandler(0, 0,hbSeconds, TimeUnit.SECONDS));
|
|
|
socketChannel.pipeline().addLast(new StringDecoder(CharsetUtil.UTF_8));
|
|
|
socketChannel.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8));
|
|
|
socketChannel.pipeline().addLast(tcpClientHandler);
|
|
|
}
|
|
|
}).remoteAddress(serverIP, serverPort);
|
|
|
doConnect();
|
|
|
+ System.out.println("connect server host: " + serverIP + ", port: " + serverPort);
|
|
|
}
|
|
|
|
|
|
//独立连接方法,用于重新连接
|
|
@@ -78,23 +84,24 @@ public class TcpClient {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- System.out.println("TcpClient connect start");
|
|
|
+ System.out.println("connect start");
|
|
|
ChannelFuture future = bootstrap.connect().addListener(new ChannelFutureListener() {
|
|
|
@Override
|
|
|
public void operationComplete(ChannelFuture channelFuture) throws Exception {
|
|
|
- if (channelFuture.isSuccess()) {
|
|
|
- channel = channelFuture.channel();
|
|
|
+ if (channelFuture.isSuccess()){
|
|
|
+ isRunning = true;
|
|
|
retryTimes = 0;
|
|
|
- System.out.println("TcpClient connect success");
|
|
|
+ System.out.println("connect success");
|
|
|
} else {
|
|
|
//连接失败时的处理
|
|
|
+ isRunning = false;
|
|
|
System.out.println("TcpClient connect retry : " + retryTimes);
|
|
|
channelFuture.channel().eventLoop().schedule(new Runnable() {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
retryTimes++;
|
|
|
- if (retryTimes > 30) {
|
|
|
- System.out.println("TcpClient 重试" + (retryTimes - 1) + "次,结束");
|
|
|
+ if (retryTimes>30){
|
|
|
+ System.out.println("重试"+(retryTimes-1)+"次,结束");
|
|
|
workGroup.shutdownGracefully();
|
|
|
//todo: 从API获取新的serverIP和serverPort,全新连接
|
|
|
TcpClient.getInstance().init(Constants.Companion.getTcp_ip(), Integer.parseInt(Constants.Companion.getTcp_port()), Integer.parseInt(Constants.Companion.getReader_idle_time()));
|