Merge remote-tracking branch 'origin/main'
# Conflicts: # .idea/workspace.xml
This commit is contained in:
@@ -62,27 +62,66 @@ def start_socket_listener():
|
||||
print(f"[INFO] Socket listener started on port {port}, waiting for connections...")
|
||||
while True:
|
||||
try:
|
||||
LogManager.info(f"[INFO] Waiting for a new connection on port {port}...")
|
||||
print(f"[INFO] Waiting for a new connection on port {port}...")
|
||||
conn, addr = s.accept()
|
||||
LogManager.info(f"[INFO] Connection accepted from: {addr}")
|
||||
print(f"[INFO] Connection accepted from: {addr}")
|
||||
LogManager.info(f"[INFO] Connection from {addr}")
|
||||
except Exception as e:
|
||||
LogManager.error(f"[ERROR] accept 失败: {e}")
|
||||
continue
|
||||
|
||||
raw_data = conn.recv(1024).decode('utf-8').strip()
|
||||
LogManager.info(f"[INFO] Raw data received: {raw_data}")
|
||||
print(f"[INFO] Raw data received: {raw_data}")
|
||||
|
||||
data = json.loads(raw_data)
|
||||
LogManager.info(f"[INFO] Parsed data: {data}")
|
||||
print(f"[INFO] Parsed data: {data}")
|
||||
dataQueue.put(data)
|
||||
except Exception as conn_error:
|
||||
LogManager.error(f"[ERROR]连接处理失败: {conn_error}")
|
||||
print(f"[ERROR]连接处理失败: {conn_error}")
|
||||
# 独立线程处理单条连接,避免单客户端异常拖垮监听线程
|
||||
threading.Thread(target=_handle_conn, args=(conn, addr), daemon=True).start()
|
||||
# while True:
|
||||
# try:
|
||||
# LogManager.info(f"[INFO] Waiting for a new connection on port {port}...")
|
||||
# print(f"[INFO] Waiting for a new connection on port {port}...")
|
||||
# conn, addr = s.accept()
|
||||
# LogManager.info(f"[INFO] Connection accepted from: {addr}")
|
||||
# print(f"[INFO] Connection accepted from: {addr}")
|
||||
#
|
||||
# raw_data = conn.recv(1024).decode('utf-8').strip()
|
||||
# LogManager.info(f"[INFO] Raw data received: {raw_data}")
|
||||
# print(f"[INFO] Raw data received: {raw_data}")
|
||||
#
|
||||
# data = json.loads(raw_data)
|
||||
# LogManager.info(f"[INFO] Parsed data: {data}")
|
||||
# print(f"[INFO] Parsed data: {data}")
|
||||
# dataQueue.put(data)
|
||||
# except Exception as conn_error:
|
||||
# LogManager.error(f"[ERROR]连接处理失败: {conn_error}")
|
||||
# print(f"[ERROR]连接处理失败: {conn_error}")
|
||||
except Exception as e:
|
||||
LogManager.error(f"[ERROR]Socket服务启动失败: {e}")
|
||||
print(f"[ERROR]Socket服务启动失败: {e}")
|
||||
|
||||
def _handle_conn(conn: socket.socket, addr):
|
||||
try:
|
||||
with conn:
|
||||
# 1. 循环收包直到拿到完整 JSON
|
||||
buffer = ""
|
||||
while True:
|
||||
data = conn.recv(1024)
|
||||
if not data: # 对端关闭
|
||||
break
|
||||
buffer += data.decode('utf-8', errors='ignore')
|
||||
# 2. 尝试切出完整 JSON(简单按行,也可按长度头、分隔符)
|
||||
while True:
|
||||
line, sep, buffer = buffer.partition('\n')
|
||||
if not sep: # 没找到完整行
|
||||
break
|
||||
line = line.strip()
|
||||
if not line: # 空行跳过
|
||||
continue
|
||||
try:
|
||||
obj = json.loads(line)
|
||||
except json.JSONDecodeError as e:
|
||||
LogManager.warning(f"[WARN] 非法 JSON 丢弃: {line[:100]} {e}")
|
||||
continue
|
||||
# 3. 收到合法数据,塞进队列
|
||||
dataQueue.put(obj)
|
||||
LogManager.info(f"[INFO] 收到合法消息: {obj}")
|
||||
except Exception as e:
|
||||
LogManager.error(f"[ERROR] 连接处理异常: {e}")
|
||||
|
||||
|
||||
# 在独立线程中启动Socket服务
|
||||
listener_thread = threading.Thread(target=start_socket_listener, daemon=True)
|
||||
|
||||
Reference in New Issue
Block a user