|
@@ -39,6 +39,8 @@ public class TcpClient {
|
|
private Integer retrySeconds = 5;
|
|
private Integer retrySeconds = 5;
|
|
//重试计数
|
|
//重试计数
|
|
private Integer retryTimes = 1;
|
|
private Integer retryTimes = 1;
|
|
|
|
+ //tcp是否完成初始化
|
|
|
|
+ private boolean inited = false;
|
|
|
|
|
|
|
|
|
|
//单例
|
|
//单例
|
|
@@ -73,12 +75,18 @@ public class TcpClient {
|
|
socketChannel.pipeline().addLast(tcpClientHandler);
|
|
socketChannel.pipeline().addLast(tcpClientHandler);
|
|
}
|
|
}
|
|
}).remoteAddress(serverIP, serverPort);
|
|
}).remoteAddress(serverIP, serverPort);
|
|
|
|
+ inited = true;
|
|
doConnect();
|
|
doConnect();
|
|
System.out.println("connect server host: " + serverIP + ", port: " + serverPort);
|
|
System.out.println("connect server host: " + serverIP + ", port: " + serverPort);
|
|
}
|
|
}
|
|
|
|
|
|
//独立连接方法,用于重新连接
|
|
//独立连接方法,用于重新连接
|
|
public synchronized void doConnect() {
|
|
public synchronized void doConnect() {
|
|
|
|
+ if (!inited) {
|
|
|
|
+ System.out.println("tcp is not initialized");
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
if (channel != null && channel.isActive()) {
|
|
if (channel != null && channel.isActive()) {
|
|
System.out.println("TcpClient connecting");
|
|
System.out.println("TcpClient connecting");
|
|
return;
|
|
return;
|
|
@@ -103,6 +111,7 @@ public class TcpClient {
|
|
if (retryTimes>30){
|
|
if (retryTimes>30){
|
|
System.out.println("重试"+(retryTimes-1)+"次,结束");
|
|
System.out.println("重试"+(retryTimes-1)+"次,结束");
|
|
workGroup.shutdownGracefully();
|
|
workGroup.shutdownGracefully();
|
|
|
|
+ inited = false;
|
|
//todo: 从API获取新的serverIP和serverPort,全新连接
|
|
//todo: 从API获取新的serverIP和serverPort,全新连接
|
|
TcpClient.getInstance().init(Constants.Companion.getTcp_ip(), Integer.parseInt(Constants.Companion.getTcp_port()), Integer.parseInt(Constants.Companion.getReader_idle_time()));
|
|
TcpClient.getInstance().init(Constants.Companion.getTcp_ip(), Integer.parseInt(Constants.Companion.getTcp_port()), Integer.parseInt(Constants.Companion.getReader_idle_time()));
|
|
return;
|
|
return;
|