整理业务逻辑,添加一些注释
This commit is contained in:
@@ -20,7 +20,9 @@ import prot_codec as pc
|
||||
# 另一个连接本地tcp服务器
|
||||
|
||||
|
||||
# 保存连接代理服务器的端口
|
||||
_remote_client=None
|
||||
# 保存本地连接的tcp客户端端口列表
|
||||
_local_client=[]
|
||||
|
||||
|
||||
@@ -44,17 +46,22 @@ def close(ip,port):
|
||||
if(item[1]==ip and item[2]==port):
|
||||
print(f"remote close:{ip},{port}")
|
||||
item[0].close()
|
||||
# 删除已被关闭的条目
|
||||
_local_client.remove(item)
|
||||
break
|
||||
|
||||
# 关闭所有
|
||||
def close_all():
|
||||
for item in _local_client:
|
||||
item[0].close()
|
||||
# 关闭端口之后把列表置空
|
||||
_local_client=[]
|
||||
print('remote close all')
|
||||
|
||||
|
||||
|
||||
# 本地数据处理,解包,把负载数据发送到本地服务器
|
||||
# 每一个connect都会创建一个线程
|
||||
def local_client_handler(tcp_server:socket,ip,port):
|
||||
global _remote_client
|
||||
global _local_client
|
||||
@@ -76,11 +83,12 @@ def local_client_handler(tcp_server:socket,ip,port):
|
||||
else:
|
||||
break
|
||||
tcp_server.close()
|
||||
_local_client.remove(self_info)
|
||||
# 发送连接断开的消息
|
||||
cmd={'device':'server','option':'disconnect','ip':ip,'port':port}
|
||||
data=pc.encode(json.dumps(cmd).encode('utf-8'),b'default')
|
||||
if _remote_client is not None:
|
||||
_remote_client.send(data)
|
||||
_local_client.remove(self_info)
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user