更新缓存重发函数

This commit is contained in:
murmur 2023-03-11 16:04:43 +08:00
parent 211b879a75
commit 2c5d8933ba

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
#coding:utf-8
import socketserver
import subprocess
@ -18,7 +18,8 @@ cache=defaultdict(list)
def save_cache(dic):
'''缓存未发送成功的消息'''
'''缓存未发送成功的消息
仅在未发送成功时缓存'''
# f=open("./cache.txt",mode='w+')
# f.write(str(dic))
@ -26,6 +27,7 @@ def save_cache(dic):
pickle.dump(dic,open("./cache.txt",'wb'))
def load_cache():
'''程序启动时加载缓存'''
try:
# f=open("./cache.txt")
# c = eval(f.read())
@ -99,9 +101,17 @@ def tt_hh(addr,data):
# 组帧再返回
# 从缓存中匹配数据并重发
# 仅有新终端上线时才重发
if ccid in cache:
for i in cache[ccid]:
addr.send(i)
msgs = cache[ccid]
for i in range(len(msgs)):
try:
addr.send(msgs[i])
# 发送成功则清除对应缓存数据
del cache[ccid][i] # not msgs[i]
except:
# 异常时退出循环
break
return 0
@ -122,26 +132,30 @@ def tt_trans(addr,data):
msg[-1]=crc
if not taddr:
# 未报到匹配的在线终端
#更新缓存
#{tccid1=[msg1,msg2,...],...}
cache[tccid].append(msg)
save_cache(cache)
print("未找到在线终端,可能终端未在线或ccid错。")
print("终端未在线或ccid错。")
return 0
print("找到匹配终端:",tccid,"<--->",taddr)
print("匹配终端:",tccid,"<--->",taddr)
# 发送
try:
taddr.send(msg)
except Exception:
# 发送失败
# 更新pairs清空对应终端
update_pairs(taddr,tccid,1)
#更新缓存
#{tccid1=[msg1,msg2,...],...}
cache[tccid].append(msg)
save_cache(cache)
print("终端已掉线。",taddr)
print("发送失败,终端可能已掉线。",taddr)
return 0
def err_handle(flag,addr):