Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

GitHub

This documentation is part of the "Projects with Books" initiative at zenOSmosis.

The source code for this project is available on GitHub.

RPC Layer: Dispatcher, Sessions & Stream Lifecycle

Loading…

RPC Layer: Dispatcher, Sessions & Stream Lifecycle

Relevant source files

The RPC layer provides a high-level multiplexing engine on top of the binary frame protocol. It handles request correlation, stream-based payload delivery, and session management. It is designed to be runtime-agnostic, supporting both standard multithreaded environments and single-threaded WASM targets through a callback-driven, non-async model.

1. RPC Message Fundamentals

At the core of the RPC layer are structured messages that wrap raw byte streams with intent and identity.

Sources: core/src/rpc/rpc_internals/rpc_header.rs:5-24 core/src/rpc/rpc_request_response.rs:10-33 core/src/rpc/rpc_request_response.rs:90-104 core/src/rpc/rpc_internals/rpc_message_type.rs:1-7


2. Stream Lifecycle and Multiplexing

The RpcSession manages the low-level mechanics of mapping RPC messages to multiplexed streams.

Stream ID Allocation

Every RPC call initiates a new stream. RpcSession maintains a next_stream_id counter, initialized via increment_u32_id() core/src/rpc/rpc_internals/rpc_session.rs:21-33 This ensures that multiple concurrent RPC calls can coexist on a single transport without collision.

Data Flow: Encoding and Decoding

The session coordinates between the Frame layer and RPC events:

  1. Outbound : init_request creates an RpcStreamEncoder, which wraps a stream_id and an on_emit callback core/src/rpc/rpc_internals/rpc_session.rs:35-50
  2. Inbound : read_bytes feeds raw data into a FrameMuxStreamDecoder core/src/rpc/rpc_internals/rpc_session.rs61 Decoded frames are then routed to a specific RpcStreamDecoder based on their stream_id core/src/rpc/rpc_internals/rpc_session.rs:65-70
  3. Events : The RpcStreamDecoder transitions through states (AwaitHeader -> AwaitPayload -> Done) core/src/rpc/rpc_internals/rpc_stream_decoder.rs:20-24 and emits RpcStreamEvent variants: Header, PayloadChunk, End, or Error core/src/rpc/rpc_internals/rpc_stream_event.rs:6-29

Stream Termination

Streams are cleaned up from the rpc_stream_decoders map when an End or Cancel frame is received, or if a decoding error occurs core/src/rpc/rpc_internals/rpc_session.rs:73-101

Stream Lifecycle Diagram

Sources: core/src/rpc/rpc_internals/rpc_session.rs:20-117 core/src/rpc/rpc_internals/rpc_stream_decoder.rs:59-182 core/src/rpc/rpc_internals/rpc_stream_event.rs:6-29 core/src/rpc/rpc_internals/rpc_trait.rs:32-33


3. RpcRespondableSession: Handler Management

RpcRespondableSession is a wrapper that adds stateful response tracking to the base session. It maintains a map of response_handlers keyed by rpc_request_id core/src/rpc/rpc_internals/rpc_respondable_session.rs:21-28

Sources: core/src/rpc/rpc_internals/rpc_respondable_session.rs:21-45 core/src/rpc/rpc_internals/rpc_respondable_session.rs:97-102 core/src/rpc/rpc_internals/rpc_respondable_session.rs:113-180


4. RpcDispatcher: Request Correlation

The RpcDispatcher is the primary interface for application code. It manages a RpcRespondableSession and provides a synchronized queue for tracking inbound requests.

Internal Request Queue

The dispatcher installs a “catch-all” handler that populates an internal rpc_request_queue (a VecDeque of (u32, RpcRequest)) core/src/rpc/rpc_dispatcher.rs:50-51 This handler listens for incoming Header events to create new RpcRequest entries and appends PayloadChunk data to them as it arrives core/src/rpc/rpc_dispatcher.rs:118-162

Critical Safety

The rpc_request_queue is protected by a Mutex. If the mutex is poisoned, the dispatcher will panic! to prevent inconsistent state transitions or data loss core/src/rpc/rpc_dispatcher.rs:119-133

Entity Mapping: Code to Logic

Sources: core/src/rpc/rpc_dispatcher.rs:36-71 core/src/rpc/rpc_dispatcher.rs:114-162 core/src/rpc/rpc_internals/rpc_respondable_session.rs:21-33 core/src/rpc/rpc_internals/rpc_session.rs:20-24


5. Implementation Summary Table

ComponentResponsibilityFile Reference
RpcHeaderBinary layout of RPC metadata and IDscore/src/rpc/rpc_internals/rpc_header.rs:5-24
RpcStreamEncoderFragments payloads into frames with headerscore/src/rpc/rpc_internals/rpc_stream_encoder.rs:6-12
RpcStreamDecoderReassembles frames into RpcStreamEventcore/src/rpc/rpc_internals/rpc_stream_decoder.rs:11-18
RpcSessionManages stream_id and decoder registrycore/src/rpc/rpc_internals/rpc_session.rs:20-24
RpcRespondableSessionMaps rpc_request_id to user callbacks and handles pre-bufferingcore/src/rpc/rpc_internals/rpc_respondable_session.rs:21-33
RpcDispatcherHigh-level correlation API and request queuecore/src/rpc/rpc_dispatcher.rs:36-51

Sources: core/src/rpc/rpc_internals/rpc_header.rs:5-24 core/src/rpc/rpc_internals/rpc_session.rs:20-33 core/src/rpc/rpc_dispatcher.rs:36-51 core/src/rpc/rpc_internals/rpc_respondable_session.rs:21-33