Adapt new dll

This commit is contained in:
Changhua 2023-04-09 11:29:12 +08:00
parent 86d71f9f7b
commit 8d6da3d43b

View File

@ -26,7 +26,7 @@ def _retry():
def decorator(func): def decorator(func):
""" Retry the function """ """ Retry the function """
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
def logerror(): def logerror(e):
func_name = re.findall(r"func: (.*?)\n", str(args[1]))[-1] func_name = re.findall(r"func: (.*?)\n", str(args[1]))[-1]
logging.getLogger("WCF").error(f"Call {func_name} failed: {e}") logging.getLogger("WCF").error(f"Call {func_name} failed: {e}")
@ -36,10 +36,10 @@ def _retry():
try: try:
ret = func(*args, **kwargs) ret = func(*args, **kwargs)
except Exception as e: except Exception as e:
logerror() logerror(e)
ret = wcf_pb2.Response() ret = wcf_pb2.Response()
else: # 其他异常,退出 except Exception as e: # 其他异常,退出
logerror() logerror(e)
sys.exit(-1) sys.exit(-1)
return ret return ret
@ -85,37 +85,40 @@ class Wcf():
"""是否文本消息""" """是否文本消息"""
return self.type == 1 return self.type == 1
def __init__(self, host_port: str = None, debug: bool = False) -> None: def __init__(self, host: str = None, port: int = 10086, debug: bool = True) -> None:
self._local_host = False self._local_host = False
self._is_running = False self._is_running = False
self._is_receiving_msg = False self._is_receiving_msg = False
self.LOG = logging.getLogger("WCF") self.LOG = logging.getLogger("WCF")
self.LOG.info(f"wcferry version: {__version__}") self.LOG.info(f"wcferry version: {__version__}")
if host_port is None: self.port = port
self.host = host
if host is None:
self._local_host = True self._local_host = True
host_port = "tcp://127.0.0.1:10086" self.host = "127.0.0.1"
self.host, self.port = host_port.rsplit(":", 1)
self.port = int(self.port)
cmd = f"{WCF_ROOT}/wcf.exe start {self.port} {'debug' if debug else ''}" cmd = f"{WCF_ROOT}/wcf.exe start {self.port} {'debug' if debug else ''}"
if os.system(cmd) != 0: if os.system(cmd) != 0:
self.LOG.error("初始化失败!") self.LOG.error("初始化失败!")
os._exit(-1) os._exit(-1)
else:
self.host, self.port = host_port.rsplit(":", 1) self.cmd_url = f"tcp://{self.host}:{self.port}"
self.port = int(self.port)
# 连接 RPC # 连接 RPC
self.cmd_socket = pynng.Pair1() # Client --> Server发送消息 self.cmd_socket = pynng.Pair1() # Client --> Server发送消息
self.cmd_socket.send_timeout = 2000 # 发送 2 秒超时 self.cmd_socket.send_timeout = 2000 # 发送 2 秒超时
self.cmd_socket.recv_timeout = 2000 # 接收 2 秒超时 self.cmd_socket.recv_timeout = 2000 # 接收 2 秒超时
self.cmd_socket.dial(host_port, block=False) try:
self.cmd_socket.dial(self.cmd_url, block=True)
except Exception as e:
self.LOG.error(f"连接失败: {e}")
os._exit(-2)
self.msg_socket = pynng.Pair1() # Server --> Client接收消息 self.msg_socket = pynng.Pair1() # Server --> Client接收消息
self.msg_socket.send_timeout = 2000 # 发送 2 秒超时 self.msg_socket.send_timeout = 2000 # 发送 2 秒超时
self.msg_socket.recv_timeout = 2000 # 接收 2 秒超时 self.msg_socket.recv_timeout = 2000 # 接收 2 秒超时
self.msg_url = host_port.replace(str(self.port), str(self.port + 1)) self.msg_url = self.cmd_url.replace(str(self.port), str(self.port + 1))
atexit.register(self.cleanup) # 退出的时候停止消息接收,防止内存泄露 atexit.register(self.cleanup) # 退出的时候停止消息接收,防止资源占用
while not self.is_login(): # 等待微信登录成功 while not self.is_login(): # 等待微信登录成功
sleep(1) sleep(1)