diff --git a/app/common/IPC_URL/csharp_ipc_handler.py b/app/common/IPC_URL/csharp_ipc_handler.py index ead1c6ce..9aad8f33 100644 --- a/app/common/IPC_URL/csharp_ipc_handler.py +++ b/app/common/IPC_URL/csharp_ipc_handler.py @@ -1,3 +1,4 @@ +import sys import asyncio import threading from typing import Optional @@ -8,6 +9,9 @@ CSHARP_AVAILABLE = False try: + # 添加 dlls path + sys.path.append(str(get_data_path("dlls"))) + # 导入 Python.NET from pythonnet import load @@ -29,8 +33,9 @@ from SecRandom4Ci.Interface.Models import CallResult, Student CSHARP_AVAILABLE = True -except: +except Exception as e: logger.warning("无法加载 Python.NET,将会回滚!") + logger.warning(e) if CSHARP_AVAILABLE: @@ -60,6 +65,7 @@ def __init__(self): self.ipc_client: Optional[IpcClient] = None self.client_thread: Optional[threading.Thread] = None self.is_running = False + self.is_connected = False def start_ipc_client(self) -> bool: """ @@ -97,6 +103,11 @@ def send_notification( settings_group=None, ) -> bool: """发送提醒""" + if not self.is_running: + return False + + if not self.is_connected: + return False if settings: display_duration = settings.get("notification_display_duration", 5) @@ -167,17 +178,40 @@ async def client(): task = self.ipc_client.Connect() await loop.run_in_executor(None, lambda: task.Wait()) + self.is_connected = True while self.is_running: await asyncio.sleep(1) + if not self._check_alive(): + logger.warning("C# IPC 断连!重连...") + self.is_connected = False + + task = self.ipc_client.Connect() + await loop.run_in_executor(None, lambda: task.Wait()) + self.is_connected = True + + logger.info("C# IPC 重连成功!") + self.ipc_client = None + self.is_connected = False # 启动新的 asyncio 事件循环 loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) loop.run_until_complete(client()) loop.close() + + def _check_alive(self) -> bool: + """客户端是否正常连接""" + try: + randomService = GeneratedIpcFactory.CreateIpcProxy[ISecRandomService]( + self.ipc_client.Provider, self.ipc_client.PeerProxy + ) + return randomService.IsAlive() == "Yes" + except Exception as e: + logger.warning(e) + return False else: class CSharpIPCHandler: @@ -205,6 +239,7 @@ def __init__(self): self.ipc_client = None self.client_thread = None self.is_running = False + self.is_connected = False def start_ipc_client(self) -> bool: """ diff --git a/data/dlls/SecRandom4Ci.Interface.dll b/data/dlls/SecRandom4Ci.Interface.dll index cff93791..89d9768c 100644 Binary files a/data/dlls/SecRandom4Ci.Interface.dll and b/data/dlls/SecRandom4Ci.Interface.dll differ diff --git a/data/dlls/SecRandom4Ci.Interface.pdb b/data/dlls/SecRandom4Ci.Interface.pdb index ac8f09f6..b0ad7eef 100644 Binary files a/data/dlls/SecRandom4Ci.Interface.pdb and b/data/dlls/SecRandom4Ci.Interface.pdb differ