add keep-alive

This commit is contained in:
“zj”
2026-04-04 21:26:16 +08:00
parent 2169847926
commit 1aa8f7b608
4 changed files with 79 additions and 51 deletions

View File

@@ -70,7 +70,8 @@ cd UPC-Resent
"upc_ip": "192.168.8.73",
"upc_port": 502,
"listen_port": 10079,
"enabled": true
"enabled": true,
"keep_alive": true // TCP长连接模式可选默认false
},
{
"id": "dev2",
@@ -141,6 +142,7 @@ crontab crontab.txt
| `upc_port` | 控制设备的端口Modbus默认502 | `502` |
| `listen_port` | 本机监听端口(每个设备需不同) | `10079` |
| `enabled` | 是否启用该设备 | `true` / `false` |
| `keep_alive` | **TCP 专用** 是否保持连接(长连接模式) | `true` / `false` (默认)
**协议组合说明**
- `listen_protocol`: 主机监听客户端连接的协议(客户端 -> 主机)

View File

@@ -61,7 +61,8 @@
"upc_ip": "192.168.8.73",
"upc_port": 502,
"listen_port": 10079,
"enabled": true
"enabled": true,
"keep_alive": true
},
{
"id": "dev2",

View File

@@ -103,6 +103,9 @@ def generate_listener(device, global_config, mappings, command_sets, global_comm
# 确定使用哪个 sender 脚本(每个设备独立的 sender
sender_file = f"{global_config['base_dir']}/bin/sender_{device['id']}.py"
# 获取 keep_alive 配置仅TCP有效默认False
keep_alive = device.get('keep_alive', False)
# 替换模板变量
content = template.format(
device_id=device['id'],
@@ -115,7 +118,8 @@ def generate_listener(device, global_config, mappings, command_sets, global_comm
python_path=global_config['python_path'],
command_mappings=mappings_str,
sender_file=sender_file,
device_protocol=device_protocol.upper()
device_protocol=device_protocol.upper(),
keep_alive='True' if keep_alive else 'False'
)
output_file = os.path.join(global_config['base_dir'], 'bin', f"{device['id']}.py")

View File

@@ -18,6 +18,7 @@ SENDER_FILE = '{sender_file}'
LOG_FILE = '{base_dir}/log/{device_id}.log'
PYTHON_PATH = '{python_path}'
DEVICE_ID = '{device_id}'
KEEP_ALIVE = {keep_alive} # 是否保持TCP连接
# =========================
BUFSIZE = 1024
@@ -60,15 +61,25 @@ def main():
logging.info("服务启动,监听 %s:%s" % (TMS_SERVER_IP, TMS_PORT))
print(f"[{{DEVICE_ID}}] 服务启动,监听 {{TMS_SERVER_IP}}:{{TMS_PORT}}")
while True:
logging.debug("等待客户端连接...")
connection, address = sock.accept()
logging.info("客户端连接建立: %s:%s" % (address[0], address[1]))
def handle_connection(connection, address):
"""处理单个连接,支持 keep_alive 模式"""
logging.info("客户端连接建立: %s:%s, keep_alive=%s" % (address[0], address[1], KEEP_ALIVE))
try:
connection.settimeout(5)
logging.debug("设置超时 5 秒,等待接收数据...")
# 设置超时keep_alive 模式下超时时间更长
timeout = 30 if KEEP_ALIVE else 5
connection.settimeout(timeout)
logging.debug("设置超时 %d 秒" % timeout)
while True:
logging.debug("等待接收数据...")
buf_bytes = connection.recv(BUFSIZE)
# 客户端断开连接
if not buf_bytes:
logging.info("客户端断开连接: %s:%s" % (address[0], address[1]))
break
logging.debug("收到原始数据: %s (长度=%d)" % (repr(buf_bytes), len(buf_bytes)))
buf = buf_bytes.decode('utf-8').strip()
@@ -115,6 +126,11 @@ def main():
connection.send(response.encode('utf-8'))
logging.debug("响应发送完成")
# 非 keep_alive 模式,处理完一个请求后断开连接
if not KEEP_ALIVE:
logging.debug("非 keep_alive 模式,断开连接")
break
except socket.timeout:
datetime_str = time.strftime(ISOTIMEFORMAT, time.localtime(time.time()))
logstr = "===" + datetime_str + " 客户端连接超时: " + address[0] + ":" + str(address[1]) + "==="
@@ -136,6 +152,11 @@ def main():
logging.info("关闭客户端连接: %s:%s" % (address[0], address[1]))
connection.close()
while True:
logging.debug("等待客户端连接...")
connection, address = sock.accept()
handle_connection(connection, address)
if __name__ == '__main__':
main()