@@ -46,7 +46,8 @@ def __init__(self, endpoint: str, log_handler: LogHandler | None = None) -> None
4646 self ._pub_endpoint , self ._ctrl_endpoint = _derive_endpoints (endpoint )
4747 self ._serializer = Serializer ()
4848 self ._serializer .register_command_types (SyncCommand )
49- self ._handlers : dict [str , Callable [[BaseCommand ], CommandResponse | None ]] = {}
49+ self ._handlers : dict [str , Callable [[
50+ BaseCommand ], CommandResponse | None ]] = {}
5051 self ._snapshot_providers : dict [str , Callable [[], BaseEvent ]] = {}
5152 self ._log : LogHandler = log_handler or NullHandler ()
5253
@@ -89,7 +90,8 @@ def start(self) -> None:
8990 self ._ctx = zmq .Context ()
9091 self ._pub_socket = self ._ctx .socket (zmq .PUB )
9192 self ._router_socket = self ._ctx .socket (zmq .ROUTER )
92-
93+ if self ._pub_socket is None or self ._router_socket is None :
94+ raise RuntimeError ("Failed to create socket" )
9395 # 启用 ZMQ 原生心跳,与客户端匹配
9496 # HEARTBEAT_IVL: 每 5 秒发送心跳
9597 # HEARTBEAT_TIMEOUT: 5 秒内没收到回复视为断连
@@ -108,7 +110,8 @@ def start(self) -> None:
108110 self ._thread = threading .Thread (target = self ._poll_loop , daemon = True )
109111 self ._thread .start ()
110112
111- self ._log .info ("Server started: pub=%s, ctrl=%s" , self ._pub_endpoint , self ._ctrl_endpoint )
113+ self ._log .info ("Server started: pub=%s, ctrl=%s" ,
114+ self ._pub_endpoint , self ._ctrl_endpoint )
112115
113116 def stop (self ) -> None :
114117 self ._stopped .set ()
@@ -141,14 +144,15 @@ def publish(self, topic: type[BaseEvent], event: BaseEvent) -> None:
141144 def _poll_loop (self ) -> None :
142145 while not self ._stopped .is_set ():
143146 try :
144- events = self ._poller .poll (timeout = 100 )
147+ events = self ._poller .poll (timeout = 100 ) # type: ignore
145148 except zmq .ZMQError :
146149 break
147150
148151 for socket , _ in events :
149152 if socket is self ._router_socket :
150153 try :
151- msg = self ._router_socket .recv_multipart (zmq .NOBLOCK )
154+ msg = self ._router_socket .recv_multipart ( # type: ignore
155+ zmq .NOBLOCK )
152156 except zmq .Again :
153157 continue
154158 if len (msg ) < 2 :
@@ -158,7 +162,8 @@ def _poll_loop(self) -> None:
158162 try :
159163 cmd = self ._serializer .decode_command (payload )
160164 except Exception :
161- self ._log .warning ("Failed to decode command" , exc_info = True )
165+ self ._log .warning (
166+ "Failed to decode command" , exc_info = True )
162167 continue
163168 self ._dispatch (client_id , cmd )
164169
@@ -167,11 +172,12 @@ def _dispatch(self, client_id: bytes, cmd: BaseCommand) -> None:
167172 if isinstance (tag , type ):
168173 tag = tag .__name__
169174 tag = str (tag )
170-
171- self ._log .info ("[Server] 收到命令: tag=%s, request_id=%s" , tag , cmd .request_id )
175+
176+ self ._log .info ("[Server] 收到命令: tag=%s, request_id=%s" ,
177+ tag , cmd .request_id )
172178
173179 if tag == "__sync__" :
174- self ._handle_sync (client_id , cmd )
180+ self ._handle_sync (client_id , cmd ) # type: ignore
175181 return
176182
177183 handler = self ._handlers .get (tag )
@@ -181,7 +187,8 @@ def _dispatch(self, client_id: bytes, cmd: BaseCommand) -> None:
181187
182188 try :
183189 result = handler (cmd )
184- self ._log .info ("[Server] handler 执行完成: tag=%s, result=%s" , tag , result )
190+ self ._log .info (
191+ "[Server] handler 执行完成: tag=%s, result=%s" , tag , result )
185192 except Exception as e :
186193 self ._log .error ("Handler error for %s: %s" , tag , e , exc_info = True )
187194 if cmd .request_id :
@@ -211,7 +218,8 @@ def _handle_sync(self, client_id: bytes, cmd: SyncCommand) -> None:
211218 request_id = cmd .request_id , success = True , data = payload
212219 )
213220 except Exception as e :
214- self ._log .error ("Snapshot provider error for %s: %s" , topic , e , exc_info = True )
221+ self ._log .error (
222+ "Snapshot provider error for %s: %s" , topic , e , exc_info = True )
215223 resp = CommandResponse (
216224 request_id = cmd .request_id , success = False , error = str (e )
217225 )
@@ -225,4 +233,5 @@ def _send_to_client(self, client_id: bytes, resp: CommandResponse) -> None:
225233 [client_id , b"" , self ._serializer .encode_response (resp )]
226234 )
227235 except zmq .ZMQError :
228- self ._log .warning ("Failed to send response to client" , exc_info = True )
236+ self ._log .warning (
237+ "Failed to send response to client" , exc_info = True )
0 commit comments