From ac787a2fdebb0bf94b5f4a844ccde2cd14bf8778 Mon Sep 17 00:00:00 2001 From: murmur Date: Mon, 13 Mar 2023 15:27:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=97=A0=E7=94=A8=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server.py | 45 ++++++++++----------------------------------- 1 file changed, 10 insertions(+), 35 deletions(-) diff --git a/server.py b/server.py index 88f972c..3f9e3d5 100755 --- a/server.py +++ b/server.py @@ -3,18 +3,17 @@ import socketserver import subprocess import pickle - - -offs = 0 # 偏移量 -p = {} -# 保存终端信息,ccid与地址对 -# p={ccid=addr,} -# 缓存未能正常发送消息的客户端 -pp = {} -# cache={} from collections import defaultdict cache = defaultdict(list) +offs = 0 # 偏移量 + +# 保存终端信息,ccid=地址 +p = {} +# 地址 ==ccid +pp = {} + + def save_cache(dic): @@ -92,15 +91,6 @@ def get_ccid(addr): +--------+--------------+----------+----------+--------------+------------------------+--------+ """ - -def send(fd, data): - try: - fd.send(data) - return 0 - except: - return -1 - - def tt_hh(addr, data): """处理心跳""" @@ -119,15 +109,11 @@ def tt_hh(addr, data): try: # 按缓存先后顺序发送 addr.send(msgs[i]) + # 成功则清空已发送成功的缓存数据 cache[ccid].pop(0) print("第",i+1,"包缓存数据发送完成。") except: # 异常时退出循环,不用再尝试发送后续msgs - # 退出前清空已发送成功的缓存数据 - # del方法会改变list大小 - # while i: - # cache[ccid].pop(0) - # i = i - 1 print("第",i+1,"包发送失败,停止尝试") break if cache[ccid] == []: @@ -187,14 +173,6 @@ def tt_trans(addr, data): return 0 -def err_handle(flag, addr): - """错误处理\n - 默认打印错误信息 - 1-非法连接,关闭连接 - """ - - return 0 - def data_split(data): # 按帧头分割长数据 @@ -252,15 +230,12 @@ class MyServer(socketserver.BaseRequestHandler): # print(type(conn),conn.fd) while True: - data = conn.recv(1000000) - # update_pairs(conn,data[7+offs:11+offs]) + data = conn.recv(1024000) if not data: break print( "接收到新数据", self.client_address, ",长度", len(data), ":\r\n", data.hex(" ") ) - # ack_msg = "got from "+ str(ip) + " to " + str(self.client_address) + data - # conn.send(data[:6]) if tt_decode(conn, data): conn.close() print(self.client_address, "疑是非法连接,已切断。")