GitHub

RpcServer

API reference for RpcServer.

A running server: write hands it a client's message, disconnect drops a client and interrupts its requests.

Functions

__init__

__init__(group, *, on_from_server, options, services, track, semaphore) -> None
ParameterTypeDescription
grouptype[RpcGroup]
on_from_serverOnFromServer
optionsServerOptions
servicesContext[Any]
trackCloseableScope
semaphoreSemaphore | None
def __init__(
    self,
    group: type[RpcGroup],
    *,
    on_from_server: OnFromServer,
    options: ServerOptions,
    services: Context[Any],
    track: CloseableScope,
    semaphore: Semaphore | None,
) -> None:
    self._group = group
    self._on_from_server = on_from_server
    self._options = options
    self._services = services
    self._track = track
    self._semaphore = semaphore
    self._clients: dict[int, _Client] = {}
    self._is_shutdown = False
    self._shutdown = Latch(False)

disconnect

disconnect(client_id) -> Effect[None]
ParameterTypeDescription
client_idint

Returns Effect[None]

def disconnect(self, client_id: int) -> Effect[None]:
    def body(parent: Fiber[Any, Any]) -> Effect[None]:
        client = self._clients.pop(client_id, None)
        if client is not None:
            for fiber in list(client.fibers.values()):
                fiber.interrupt_as_unsafe(parent.id)
        return _VOID

    return _WithFiber(body)

write

write(client_id, message, on_request=None) -> Effect[None]

Hand the server a message of the client client_id. Fails (interrupted) when the server is shut down, or the client ended and the message is not for one of its running requests.

ParameterTypeDescription
client_idint
messageFromClient
on_requestOnRequest | None = None

Returns Effect[None]

def write(
    self, client_id: int, message: FromClient, on_request: OnRequest | None = None
) -> Effect[None]:
    """Hand the server a message of the client ``client_id``. Fails
    (interrupted) when the server is shut down, or the client ended and
    the message is not for one of its running requests."""

    def body(request_fiber: Fiber[Any, Any]) -> Effect[None]:
        if self._is_shutdown:
            return interrupt_closed("RPC server")
        client = self._clients.get(client_id)
        if client is None:
            client = _Client(client_id, ServerClient(client_id))
            self._clients[client_id] = client
        elif client.ended and not (
            isinstance(message, (Ack, Interrupt)) and message.request_id in client.fibers
        ):
            return interrupt()
        return self._dispatch(request_fiber, client, message, on_request)

    return _WithFiber(body).catch_defect(lambda defect: self._defect_of(client_id, defect))