robotcode-jsonrpc2 2.9.0.dev214__tar.gz → 2.9.0.dev264__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -340,7 +340,7 @@ bundled/libs
340
340
  .kilocode/
341
341
 
342
342
  # .agents
343
- .agents/
343
+ #.agents/
344
344
  skills-lock.json
345
345
  #.claude
346
346
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: robotcode-jsonrpc2
3
- Version: 2.9.0-dev.214
3
+ Version: 2.9.0-dev.264
4
4
  Summary: JSONRPC Server for RobotCode
5
5
  Project-URL: Homepage, https://robotcode.io
6
6
  Project-URL: Donate, https://opencollective.com/robotcode
@@ -26,7 +26,7 @@ Classifier: Programming Language :: Python :: Implementation :: PyPy
26
26
  Classifier: Topic :: Utilities
27
27
  Classifier: Typing :: Typed
28
28
  Requires-Python: >=3.10
29
- Requires-Dist: robotcode-core==2.9.0-dev.214
29
+ Requires-Dist: robotcode-core==2.9.0-dev.264
30
30
  Description-Content-Type: text/markdown
31
31
 
32
32
  # robotcode-jsonrpc2
@@ -27,7 +27,7 @@ classifiers = [
27
27
  "Framework :: Robot Framework",
28
28
  "Framework :: Robot Framework :: Tool",
29
29
  ]
30
- dependencies = ["robotcode-core==2.9.0-dev.214"]
30
+ dependencies = ["robotcode-core==2.9.0-dev.264"]
31
31
  dynamic = ["version"]
32
32
 
33
33
  [project.urls]
@@ -0,0 +1 @@
1
+ __version__ = "2.9.0-dev.264"
@@ -26,7 +26,7 @@ from typing_extensions import Self
26
26
  from robotcode.core.types import ServerMode, TcpParams
27
27
  from robotcode.core.utils.logging import LoggingDescriptor
28
28
 
29
- from .protocol import JsonRPCException
29
+ from .protocol import JsonRPCException, JsonRPCProtocolBase
30
30
 
31
31
  __all__ = ["JsonRPCServer"]
32
32
 
@@ -37,6 +37,13 @@ class NotSupportedError(Exception):
37
37
  pass
38
38
 
39
39
 
40
+ class _RejectingProtocol(asyncio.Protocol):
41
+ """Closes a connection of a server that serves one client only and already has it."""
42
+
43
+ def connection_made(self, transport: asyncio.BaseTransport) -> None:
44
+ transport.close()
45
+
46
+
40
47
  class StdOutTransportAdapter(asyncio.Transport):
41
48
  def __init__(self, rfile: BinaryIO, wfile: BinaryIO) -> None:
42
49
  super().__init__()
@@ -67,15 +74,22 @@ class _JsonRPCServerState:
67
74
  class JsonRPCServer(Generic[TProtocol], abc.ABC):
68
75
  _logger = LoggingDescriptor()
69
76
 
77
+ # whether a server on standard input and output ends when its input ends, also without being told to end
78
+ _ends_with_input = False
79
+
70
80
  def __init__(
71
81
  self,
72
82
  mode: ServerMode = ServerMode.STDIO,
73
83
  tcp_params: TcpParams = TcpParams(None, 0),
74
84
  pipe_name: Optional[str] = None,
85
+ single_client: bool = False,
75
86
  ):
76
87
  self.mode = mode
77
88
  self.tcp_params = tcp_params
78
89
  self.pipe_name = pipe_name
90
+ # in TCP mode, the server stops listening at the first connection and ends when that connection is lost
91
+ self.single_client = single_client
92
+ self._has_client = False
79
93
 
80
94
  self._run_func: Optional[Callable[[], None]] = None
81
95
  self._serve_func: Optional[Callable[[], Coroutine[None, None, None]]] = None
@@ -281,7 +295,14 @@ class JsonRPCServer(Generic[TProtocol], abc.ABC):
281
295
  def start_stdio(self) -> None:
282
296
  self.mode = ServerMode.STDIO
283
297
 
284
- transport = StdOutTransportAdapter(sys.__stdin__.buffer, sys.__stdout__.buffer) # type: ignore
298
+ # The protocol gets a duplicate of file descriptor 1, and descriptor 1 goes to stderr, so that `print`, native
299
+ # writes and child processes cannot write into the protocol stream.
300
+ stdout = cast(Any, sys.__stdout__)
301
+ stdout.flush()
302
+ protocol_fd = os.dup(stdout.fileno())
303
+ os.dup2(cast(Any, sys.__stderr__).fileno(), stdout.fileno())
304
+
305
+ transport = StdOutTransportAdapter(cast(Any, sys.__stdin__).buffer, os.fdopen(protocol_fd, "wb"))
285
306
 
286
307
  protocol = self.create_protocol()
287
308
 
@@ -302,6 +323,9 @@ class JsonRPCServer(Generic[TProtocol], abc.ABC):
302
323
  data = os.read(fd, 10000)
303
324
  if data:
304
325
  self.loop.call_soon_threadsafe(protocol.data_received, data)
326
+ elif self._ends_with_input:
327
+ # the client is gone, also when it did not tell the server to end
328
+ break
305
329
  else:
306
330
  # end of input, wait a bit if we are not closing
307
331
  stop_event.wait(0.01)
@@ -323,11 +347,33 @@ class JsonRPCServer(Generic[TProtocol], abc.ABC):
323
347
  async def start_tcp(self, host: Union[str, Sequence[str], None] = None, port: int = 0) -> None:
324
348
  self.mode = ServerMode.TCP
325
349
 
326
- self._server = await self.loop.create_server(self.create_protocol, host, port, reuse_address=True)
350
+ factory: Callable[[], asyncio.BaseProtocol] = (
351
+ self._create_single_client_protocol if self.single_client else self.create_protocol
352
+ )
353
+ self._server = await self.loop.create_server(factory, host, port, reuse_address=True)
327
354
 
328
355
  self._serve_func = self._server.serve_forever
329
356
  self._run_func = self.loop.run_forever
330
357
 
358
+ def _create_single_client_protocol(self) -> asyncio.BaseProtocol:
359
+ if self._has_client:
360
+ return _RejectingProtocol()
361
+ self._has_client = True
362
+
363
+ protocol = self.create_protocol()
364
+ if isinstance(protocol, JsonRPCProtocolBase):
365
+ protocol.on_connection_made.add(self._single_client_made)
366
+ protocol.on_connection_lost.add(self._single_client_lost)
367
+ return protocol
368
+
369
+ # closing the server before the connection is made would keep asyncio from attaching it to the server
370
+ def _single_client_made(self, sender: Any, transport: asyncio.BaseTransport) -> None:
371
+ if self._server is not None:
372
+ self._server.close()
373
+
374
+ def _single_client_lost(self, sender: Any, exc: Optional[BaseException]) -> None:
375
+ self.loop.stop()
376
+
331
377
  @_logger.call
332
378
  async def start_pipe(self, pipe_name: Optional[str]) -> None:
333
379
  from typing import TYPE_CHECKING
@@ -1 +0,0 @@
1
- __version__ = "2.9.0-dev.214"