33"""
44
55import json
6+ import socket
67import threading
78from multiprocessing .connection import Client , Listener
89from pathlib import Path
@@ -103,7 +104,7 @@ def start_ipc_server(self, port: int = 0) -> bool:
103104
104105 try :
105106 address , family = self ._get_ipc_address_for_name (self .ipc_name )
106- authkey = self . _get_authkey ( self . ipc_name )
107+ authkey = None
107108
108109 if family == "AF_UNIX" :
109110 try :
@@ -171,13 +172,22 @@ def _run_server(self):
171172
172173 def _handle_connection (self , conn ):
173174 try :
174- data = conn .recv_bytes ()
175+ if hasattr (conn , "_recv" ) and hasattr (conn , "_send" ):
176+ data = self ._recv_line_from_conn (conn )
177+ else :
178+ data = conn .recv_bytes ()
175179 if not data :
176180 return
177181
178- message = json .loads (data .decode ("utf-8" ))
182+ message = json .loads (data .decode ("utf-8" ). strip () )
179183 response = self ._process_message (message )
180- conn .send_bytes (json .dumps (response , ensure_ascii = False ).encode ("utf-8" ))
184+ response_bytes = (
185+ json .dumps (response , ensure_ascii = False ).encode ("utf-8" ) + b"\n "
186+ )
187+ if hasattr (conn , "_send" ):
188+ self ._send_line_to_conn (conn , response_bytes )
189+ else :
190+ conn .send_bytes (response_bytes )
181191 except EOFError :
182192 return
183193 except Exception as e :
@@ -188,6 +198,34 @@ def _handle_connection(self, conn):
188198 except Exception :
189199 pass
190200
201+ def _recv_line_from_conn (self , conn , max_bytes : int = 262144 ) -> bytes :
202+ buf = bytearray ()
203+ try :
204+ recv = getattr (conn , "_recv" , None )
205+ if recv is None :
206+ return b""
207+
208+ while len (buf ) < max_bytes :
209+ try :
210+ chunk = recv (1 )
211+ except EOFError :
212+ break
213+ if not chunk :
214+ break
215+ buf += chunk
216+ if chunk == b"\n " :
217+ break
218+ except Exception :
219+ return b""
220+
221+ return bytes (buf )
222+
223+ def _send_line_to_conn (self , conn , data : bytes ) -> None :
224+ send = getattr (conn , "_send" , None )
225+ if send is None :
226+ raise RuntimeError ("IPC连接不支持原始发送" )
227+ send (data )
228+
191229 def _process_message (self , message : Dict [str , Any ]) -> Dict [str , Any ]:
192230 """处理接收到的消息"""
193231 message_type = message .get ("type" , "" )
@@ -288,20 +326,58 @@ def send_ipc_message_by_name(
288326 try :
289327 target_name = self ._normalize_ipc_name (target_ipc_name or self .ipc_name )
290328 address , family = self ._get_ipc_address_for_name (target_name )
291- authkey = self ._get_authkey (target_name )
292-
293- conn = Client (address = address , family = family , authkey = authkey )
294- conn .send_bytes (json .dumps (message , ensure_ascii = False ).encode ("utf-8" ))
295- response_data = conn .recv_bytes ()
296- conn .close ()
329+ request_bytes = (
330+ json .dumps (message , ensure_ascii = False ).encode ("utf-8" ) + b"\n "
331+ )
332+ response_data = self ._send_stream_request (
333+ address = address ,
334+ family = family ,
335+ request_bytes = request_bytes ,
336+ timeout = timeout ,
337+ )
297338
298339 if not response_data :
299340 return None
300- return json .loads (response_data .decode ("utf-8" ))
341+ return json .loads (response_data .decode ("utf-8" ). strip () )
301342 except Exception as e :
302343 logger .exception (f"发送IPC消息失败: { e } " )
303344 return None
304345
346+ def _send_stream_request (
347+ self , address : str , family : str , request_bytes : bytes , timeout : float
348+ ) -> Optional [bytes ]:
349+ if os .name == "nt" :
350+ conn = Client (address = address , family = family , authkey = None )
351+ try :
352+ conn .send_bytes (request_bytes )
353+ response = conn .recv_bytes ()
354+ return response or None
355+ finally :
356+ try :
357+ conn .close ()
358+ except Exception :
359+ pass
360+
361+ if family != "AF_UNIX" :
362+ raise RuntimeError (f"不支持的IPC family: { family } " )
363+
364+ sock = socket .socket (socket .AF_UNIX , socket .SOCK_STREAM )
365+ try :
366+ sock .settimeout (max (0.1 , float (timeout )))
367+ sock .connect (address )
368+ sock .sendall (request_bytes )
369+ sock_file = sock .makefile ("rb" )
370+ try :
371+ response = sock_file .readline ()
372+ finally :
373+ sock_file .close ()
374+ return response or None
375+ finally :
376+ try :
377+ sock .close ()
378+ except Exception :
379+ pass
380+
305381 def send_ipc_message_to_app (
306382 self ,
307383 target_app_name : str ,
0 commit comments