From: snickersun Date: Fri, 28 Aug 2020 11:19:38 +0000 (+0800) Subject: 发现网络即使通过心跳重发机制,也无法解决长时间 socket假链接的情况。因此添加15分钟(3次心跳)链接服务器无反馈后 自主断开网络的机制。 X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=82aa1c4c07f1547961421c6c590d6a0e679ac7d5;p=protocol_srv.git 发现网络即使通过心跳重发机制,也无法解决长时间 socket假链接的情况。因此添加15分钟(3次心跳)链接服务器无反馈后 自主断开网络的机制。 --- diff --git a/app/src/main/java/com/enqualcomm/support/service/KeepAlive.java b/app/src/main/java/com/enqualcomm/support/service/KeepAlive.java index c123011d..445603d8 100644 --- a/app/src/main/java/com/enqualcomm/support/service/KeepAlive.java +++ b/app/src/main/java/com/enqualcomm/support/service/KeepAlive.java @@ -110,18 +110,26 @@ public class KeepAlive extends AbstractConnectionService { //更新系统的IMEI boolean ret=DeviceInfoUtil.updateImei(mRuntime); ICLogger.i("Update IMEI "+ret); - if (is_login_send_success) { + + //只有曾经login成功过,且上一次成功时间超过15分钟,才会自主断开网络。 + long currenttime=System.currentTimeMillis(); + ICLogger.i("只有曾经login或者心跳发送成功过,且上一次成功时间超过15分钟,才会自主断开网络! last longin time "+ + mRuntime.lastLoginSuccessTime+" ms from last login time "+(currenttime-mRuntime.lastLoginSuccessTime)); + if ((mRuntime.lastLoginSuccessTime>0) && ((currenttime- mRuntime.lastLoginSuccessTime) > 15 * 60 * 1000)) { + ICLogger.i("超时自主断开网络!"); + CoreService.getInstance().disconnect_server(); + }else { + if (is_login_send_success) { connection.send(protocolAPIHelper.getHeartbeatMSG(mRuntime)); Observable.timer(1000L, TimeUnit.MILLISECONDS).subscribe(new Action1() { - public void call(Long param1Long) { - connection.send(protocolAPIHelper.getBindingMSG(mRuntime)); - - } + public void call(Long param1Long) { + connection.send(protocolAPIHelper.getBindingMSG(mRuntime)); + } }); + } else { + connection.send(protocolAPIHelper.getLoginMSG(mRuntime)); + } } - else - connection.send(protocolAPIHelper.getLoginMSG(mRuntime)); - } } @@ -313,7 +321,7 @@ public class KeepAlive extends AbstractConnectionService { paramRuntime.setServerProtocolConnected(true); // isConnected = true; is_heartBeat_send_success=true; - + paramRuntime.lastLoginSuccessTime=System.currentTimeMillis(); // LauncherCtrl.getInstance(this.mContext).setStatusBarIcon("server_connected", STATUS_BAR_STATE_SERVER_CONNECTED_ON); // Deliver.triggerSendThread(); //链接上就尝试清除 buffer中的数据 @@ -325,7 +333,7 @@ public class KeepAlive extends AbstractConnectionService { } }else if (paramStringMessage.cmd().equals("login")) { //广播服务器连接成功的消息 - // paramRuntime.getContextProxy().sendBroadcastServerConnectionChanged(true); + paramRuntime.getContextProxy().sendBroadcastServerConnectionChanged(true); //登录包收到就停止重链接服务器 CoreService.getInstance().stopConnectionRetryTimer(); is_login_send_success=true; @@ -333,6 +341,8 @@ public class KeepAlive extends AbstractConnectionService { //心跳开始发送的时候,先重置response检测标志 is_heartBeat_send_success=false; + + paramRuntime.lastLoginSuccessTime=System.currentTimeMillis(); //发送心跳包 Observable.timer(3500L, TimeUnit.MILLISECONDS).subscribe(new Action1() { public void call(Long param1Long) {