发现网络即使通过心跳重发机制,也无法解决长时间 socket假链接的情况。因此添加15分钟(3次心跳)链接服务器无反馈后 自主断开网络的机制。
authorsnickersun <scy@incartech.cn>
Fri, 28 Aug 2020 11:19:38 +0000 (19:19 +0800)
committersnickersun <scy@incartech.cn>
Fri, 28 Aug 2020 11:19:38 +0000 (19:19 +0800)
app/src/main/java/com/enqualcomm/support/service/KeepAlive.java

index c123011..445603d 100644 (file)
@@ -110,18 +110,26 @@ public class KeepAlive extends AbstractConnectionService {
           //更新系统的IMEI\r
           boolean ret=DeviceInfoUtil.updateImei(mRuntime);\r
           ICLogger.i("Update IMEI "+ret);\r
-          if (is_login_send_success) {\r
+\r
+          //只有曾经login成功过,且上一次成功时间超过15分钟,才会自主断开网络。\r
+          long currenttime=System.currentTimeMillis();\r
+          ICLogger.i("只有曾经login或者心跳发送成功过,且上一次成功时间超过15分钟,才会自主断开网络! last longin time "+\r
+                  mRuntime.lastLoginSuccessTime+" ms from last login time "+(currenttime-mRuntime.lastLoginSuccessTime));\r
+          if ((mRuntime.lastLoginSuccessTime>0) && ((currenttime- mRuntime.lastLoginSuccessTime) > 15 * 60 * 1000)) {\r
+            ICLogger.i("超时自主断开网络!");\r
+            CoreService.getInstance().disconnect_server();\r
+          }else {\r
+            if (is_login_send_success) {\r
               connection.send(protocolAPIHelper.getHeartbeatMSG(mRuntime));\r
               Observable.timer(1000L, TimeUnit.MILLISECONDS).subscribe(new Action1<Long>() {\r
-                  public void call(Long param1Long) {\r
-                      connection.send(protocolAPIHelper.getBindingMSG(mRuntime));\r
-\r
-                  }\r
+                public void call(Long param1Long) {\r
+                  connection.send(protocolAPIHelper.getBindingMSG(mRuntime));\r
+                }\r
               });\r
+            } else {\r
+              connection.send(protocolAPIHelper.getLoginMSG(mRuntime));\r
+            }\r
           }\r
-          else\r
-            connection.send(protocolAPIHelper.getLoginMSG(mRuntime));\r
-\r
         }\r
       }\r
 \r
@@ -313,7 +321,7 @@ public class KeepAlive extends AbstractConnectionService {
       paramRuntime.setServerProtocolConnected(true);\r
 //      isConnected = true;\r
       is_heartBeat_send_success=true;\r
-\r
+      paramRuntime.lastLoginSuccessTime=System.currentTimeMillis();\r
 //      LauncherCtrl.getInstance(this.mContext).setStatusBarIcon("server_connected", STATUS_BAR_STATE_SERVER_CONNECTED_ON);\r
 //      Deliver.triggerSendThread(); //链接上就尝试清除 buffer中的数据\r
 \r
@@ -325,7 +333,7 @@ public class KeepAlive extends AbstractConnectionService {
       }\r
     }else   if (paramStringMessage.cmd().equals("login")) {\r
       //广播服务器连接成功的消息\r
-     // paramRuntime.getContextProxy().sendBroadcastServerConnectionChanged(true);\r
+      paramRuntime.getContextProxy().sendBroadcastServerConnectionChanged(true);\r
       //登录包收到就停止重链接服务器\r
       CoreService.getInstance().stopConnectionRetryTimer();\r
       is_login_send_success=true;\r
@@ -333,6 +341,8 @@ public class KeepAlive extends AbstractConnectionService {
 \r
       //心跳开始发送的时候,先重置response检测标志\r
       is_heartBeat_send_success=false;\r
+\r
+      paramRuntime.lastLoginSuccessTime=System.currentTimeMillis();\r
       //发送心跳包\r
       Observable.timer(3500L, TimeUnit.MILLISECONDS).subscribe(new Action1<Long>() {\r
         public void call(Long param1Long) {\r