From 7420f84fd0abce2a0b86e12104233a71c7081d74 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Mon, 16 Mar 2026 08:03:51 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85=20named=20pipe=20=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E7=9A=84=E7=B1=BB=E5=9E=8B=E6=B3=A8=E8=A7=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/plugin_runtime/transport/named_pipe.py | 32 +++++++++++++++------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/plugin_runtime/transport/named_pipe.py b/src/plugin_runtime/transport/named_pipe.py index cc8534ff..a759507d 100644 --- a/src/plugin_runtime/transport/named_pipe.py +++ b/src/plugin_runtime/transport/named_pipe.py @@ -3,7 +3,7 @@ 适用于 Windows 平台,使用 asyncio ProactorEventLoop 的 named pipe 支持。 """ -from typing import Any, Optional, Protocol, cast +from typing import Any, Callable, Dict, List, Optional, Protocol, Tuple, cast import asyncio import os @@ -17,12 +17,24 @@ _PIPE_PREFIX = "\\\\.\\pipe\\" _DEFAULT_PIPE_PREFIX = "maibot-plugin" +class _NamedPipeServerHandle(Protocol): + def close(self) -> None: ... + + class _NamedPipeEventLoop(Protocol): - async def start_serving_pipe(self, protocol_factory: Any, address: str) -> list[Any]: ... + async def start_serving_pipe( + self, + protocol_factory: Callable[[], asyncio.BaseProtocol], + address: str, + ) -> List[_NamedPipeServerHandle]: ... - async def create_pipe_connection(self, protocol_factory: Any, address: str) -> tuple[Any, Any]: ... + async def create_pipe_connection( + self, + protocol_factory: Callable[[], asyncio.BaseProtocol], + address: str, + ) -> Tuple[asyncio.BaseTransport, asyncio.BaseProtocol]: ... - def call_exception_handler(self, context: dict[str, Any]) -> None: ... + def call_exception_handler(self, context: Dict[str, Any]) -> None: ... def create_task(self, coro: Any) -> asyncio.Task[None]: ... @@ -50,10 +62,10 @@ class NamedPipeConnection(Connection): class _NamedPipeServerProtocol(asyncio.StreamReaderProtocol): def __init__(self, handler: ConnectionHandler, loop: asyncio.AbstractEventLoop) -> None: - self._reader = asyncio.StreamReader() + self._reader: asyncio.StreamReader = asyncio.StreamReader() super().__init__(self._reader) - self._handler = handler - self._loop = loop + self._handler: ConnectionHandler = handler + self._loop: asyncio.AbstractEventLoop = loop self._handler_task: Optional[asyncio.Task[None]] = None def connection_made(self, transport: asyncio.BaseTransport) -> None: @@ -86,8 +98,8 @@ class NamedPipeTransportServer(TransportServer): """Windows Named Pipe 传输服务端。""" def __init__(self, pipe_name: Optional[str] = None) -> None: - self._address = _normalize_pipe_address(pipe_name) - self._servers: list[Any] = [] + self._address: str = _normalize_pipe_address(pipe_name) + self._servers: List[_NamedPipeServerHandle] = [] async def start(self, handler: ConnectionHandler) -> None: if sys.platform != "win32": @@ -117,7 +129,7 @@ class NamedPipeTransportClient(TransportClient): """Windows Named Pipe 传输客户端。""" def __init__(self, address: str) -> None: - self._address = _normalize_pipe_address(address) + self._address: str = _normalize_pipe_address(address) async def connect(self) -> Connection: if sys.platform != "win32":