From 0212f884c62656a289a2f121441e3c58d4cd9407 Mon Sep 17 00:00:00 2001 From: murmur Date: Sat, 11 Mar 2023 17:06:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E7=BC=93=E5=AD=98=E5=87=BD?= =?UTF-8?q?=E6=95=B0bug=EF=BC=8C=E6=9B=BF=E6=8D=A2del=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server.py | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/server.py b/server.py index 79b3f69..cebabe9 100644 --- a/server.py +++ b/server.py @@ -106,13 +106,18 @@ def tt_hh(addr,data): msgs = cache[ccid] for i in range(len(msgs)): try: + # 按缓存先后顺序发送 addr.send(msgs[i]) - # 发送成功则清除对应缓存数据 - del cache[ccid][i] # not msgs[i] except: - # 异常时退出循环 + # 异常时退出循环,不用再尝试发送后续msgs + # 退出前清空已发送成功的缓存数据 + # del方法会改变list大小 + while(i): + cache[ccid].pop(0) + i = i-1 break + return 0 def tt_trans(addr,data): @@ -132,12 +137,13 @@ def tt_trans(addr,data): msg[-1]=crc if not taddr: - # 未报到匹配的在线终端 + # 未找到匹配的在线终端 #更新缓存 #{tccid1=[msg1,msg2,...],...} cache[tccid].append(msg) save_cache(cache) + print("终端未在线或ccid错。") return 0 print("匹配终端:",tccid,"<--->",taddr) @@ -148,13 +154,14 @@ def tt_trans(addr,data): taddr.send(msg) except Exception: # 发送失败 - + taddr.close() # 更新pairs,清空对应终端 update_pairs(taddr,tccid,1) #更新缓存 #{tccid1=[msg1,msg2,...],...} cache[tccid].append(msg) save_cache(cache) + print("发送失败,终端可能已掉线。",taddr) return 0 @@ -171,10 +178,12 @@ def err_handle(flag,addr): def tt_decode(addr,data): - '''处理收到的数据''' + '''处理收到的数据 + 数据正常返回0 + 数据异常返回-1''' - if data[:5] != b'$TTMS' or len(data) <12: + if data[:5] != b'$TTMS' or len(data) <12 or len(data)>200: # 数据长度不可能小于12,考虑非法接入的可能,主动断开连接 return -1