|
@@ -44,7 +44,7 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<String> {
|
|
retryTimes = 0;
|
|
retryTimes = 0;
|
|
Constant.TCP_CONNECTED = true;
|
|
Constant.TCP_CONNECTED = true;
|
|
EventBus.getDefault().post(new MessageEvent(1, Constant.EVENT_TCP_STATE));
|
|
EventBus.getDefault().post(new MessageEvent(1, Constant.EVENT_TCP_STATE));
|
|
- TcpModel tcpModel = DeviceUtil.deviceConnect(Constant.LOCAL_MAC);
|
|
|
|
|
|
+ TcpModel tcpModel = DeviceUtil.deviceConnect(Constant.LOCAL_MAC, Constant.LOCAL_IP);
|
|
TcpClient.getInstance().sendMsg(tcpModel.toJson());
|
|
TcpClient.getInstance().sendMsg(tcpModel.toJson());
|
|
}
|
|
}
|
|
|
|
|
|
@@ -57,7 +57,6 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<String> {
|
|
Constant.TCP_CONNECTED = false;
|
|
Constant.TCP_CONNECTED = false;
|
|
Log.i(TAG, "失去连接");
|
|
Log.i(TAG, "失去连接");
|
|
TcpClient.getInstance().doConnect();
|
|
TcpClient.getInstance().doConnect();
|
|
- //reConnect(ctx);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
//读取String消息
|
|
//读取String消息
|
|
@@ -87,10 +86,8 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<String> {
|
|
if (event.state() == IdleState.WRITER_IDLE) { //超时未执行写操作,在指定的超时时间内未有写操作,要发送心跳包,告诉服务器连接还存活。服务器收到心跳立马回应,正常客户端收到后执行读操作,
|
|
if (event.state() == IdleState.WRITER_IDLE) { //超时未执行写操作,在指定的超时时间内未有写操作,要发送心跳包,告诉服务器连接还存活。服务器收到心跳立马回应,正常客户端收到后执行读操作,
|
|
ctx.writeAndFlush("0"); //这种情况下不会引发READER_IDLE事件。如果服务器因为网络或其他原因导致回应的心跳,客户端没有收到,在超过写超时时间2个周期后依然没有收到,
|
|
ctx.writeAndFlush("0"); //这种情况下不会引发READER_IDLE事件。如果服务器因为网络或其他原因导致回应的心跳,客户端没有收到,在超过写超时时间2个周期后依然没有收到,
|
|
} else if (event.state() == IdleState.READER_IDLE) { //认为服务不可用,主动断开连接
|
|
} else if (event.state() == IdleState.READER_IDLE) { //认为服务不可用,主动断开连接
|
|
-// ctx.channel().close();
|
|
|
|
Log.i(TAG, "TcpClientHandler ===> READER_IDLE");
|
|
Log.i(TAG, "TcpClientHandler ===> READER_IDLE");
|
|
ctx.close();
|
|
ctx.close();
|
|
-// ctx.close();
|
|
|
|
} else if (event.state() == IdleState.ALL_IDLE) {
|
|
} else if (event.state() == IdleState.ALL_IDLE) {
|
|
Log.i(TAG, "TcpClientHandler ===> ALL_IDLE");
|
|
Log.i(TAG, "TcpClientHandler ===> ALL_IDLE");
|
|
|
|
|
|
@@ -112,50 +109,9 @@ public class TcpClientHandler extends SimpleChannelInboundHandler<String> {
|
|
public void sendMsg(String msg){
|
|
public void sendMsg(String msg){
|
|
if (ctx==null){
|
|
if (ctx==null){
|
|
System.out.println("ctx is null");
|
|
System.out.println("ctx is null");
|
|
- /*try {
|
|
|
|
- Thread.sleep(1000);
|
|
|
|
- sendMsg(msg);
|
|
|
|
- } catch (InterruptedException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }*/
|
|
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
System.out.println("wzlll: send tcp msg====" + msg);
|
|
System.out.println("wzlll: send tcp msg====" + msg);
|
|
ctx.writeAndFlush(Unpooled.copiedBuffer(msg, CharsetUtil.UTF_8));
|
|
ctx.writeAndFlush(Unpooled.copiedBuffer(msg, CharsetUtil.UTF_8));
|
|
}
|
|
}
|
|
-
|
|
|
|
- //已经连接上,中途失去连接时的处理
|
|
|
|
- private void reConnect(final ChannelHandlerContext ctx){
|
|
|
|
- if (totalRetryTimes>100){
|
|
|
|
- //todo: 存储数据库,并告警
|
|
|
|
- }
|
|
|
|
- if (connected && (TcpClient.getInstance().channel != null && TcpClient.getInstance().channel.isActive())){
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- totalRetryTimes++;
|
|
|
|
- System.out.println("TcpClientHandler 总计连接次数:"+totalRetryTimes);
|
|
|
|
- retryTimes++;
|
|
|
|
- if (retryTimes > 30) { //超时30次,10分钟后再试
|
|
|
|
- retryTimes = 0;
|
|
|
|
- ctx.channel().eventLoop().schedule(new Runnable() {
|
|
|
|
- @Override
|
|
|
|
- public void run() {
|
|
|
|
- System.out.println("TcpClientHandler 重新连接,第" + retryTimes + "次");
|
|
|
|
- TcpClient.getInstance().doConnect();
|
|
|
|
- reConnect(ctx);
|
|
|
|
- }
|
|
|
|
- }, 10*60, TimeUnit.SECONDS);
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- }else {
|
|
|
|
- ctx.channel().eventLoop().schedule(new Runnable() {
|
|
|
|
- @Override
|
|
|
|
- public void run() {
|
|
|
|
- System.out.println("TcpClientHandler 重新连接,第" + retryTimes + "次");
|
|
|
|
- TcpClient.getInstance().doConnect();
|
|
|
|
- reConnect(ctx);
|
|
|
|
- }
|
|
|
|
- }, retrySeconds * retryTimes, TimeUnit.SECONDS);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
}
|
|
}
|