Pārlūkot izejas kodu

修复由于意外断网导致的MB服务器停止工作且无法接入新的连接 (#18)

* 修复由于意外断网导致的MB服务器停止工作且无法接入新的连接

当前版本的MB服务只能接入一个连接,第二个连接接入时会打印提示并关闭新的连接。但在某种特定情形,比如网络意外断开,MB服务器没有收到Disconnect信号,这时新的连接无法接入。本次更新引入了通信超时处理方式来处理这种情况,当Client没有请求超过30秒,MB服务器就断开当前的Client,这样就可以解决上述问题。

* Apply suggestions from code review

Co-authored-by: Man, Jianting (Meco) <920369182@qq.com>
lg28870983 3 gadi atpakaļ
vecāks
revīzija
8268e2ae53
1 mainītis faili ar 13 papildinājumiem un 2 dzēšanām
  1. 13 2
      port/porttcp.c

+ 13 - 2
port/porttcp.c

@@ -38,6 +38,7 @@ static USHORT   prvvTCPLength;
 
 static void tcpserver_event_notify(tcpclient_t client, rt_uint8_t event)
 {
+    static rt_tick_t recv_tick = 0;
     switch (event)
     {
     case TCPSERVER_EVENT_CONNECT:
@@ -47,13 +48,23 @@ static void tcpserver_event_notify(tcpclient_t client, rt_uint8_t event)
         }
         else
         {
-            tcpserver_close(client);
-            rt_kprintf("Multi-host is not supported, please disconnect the current host first!");
+            if(rt_tick_get() - recv_tick > 30 * RT_TICK_PER_SECOND) /* set timeout as 30s */
+            {
+                tcpserver_close(mb_client);
+                mb_client = client;
+                recv_tick = rt_tick_get();
+            }
+            else
+            {
+                tcpserver_close(client);
+                rt_kprintf("Multi-host is not supported, please disconnect the current host first!\n");
+            }
         }
         break;
     case TCPSERVER_EVENT_RECV:
         if( mb_client == client)
         {
+            recv_tick = rt_tick_get();
             prvvTCPLength = tcpserver_recv(mb_client, &prvvTCPBuf, MB_TCP_BUF_SIZE, 100);
             if (prvvTCPLength)
             {