pub struct JsonRpcConnection<R, W> { /* private fields */ }Expand description
A framed JSON-RPC 2.0 connection over one async read half and one async write half.
Generic over any AsyncRead + AsyncWrite pair — a child’s stdout/stdin, an in-memory
duplex in tests, or a socket. The read and write halves are independent, so a caller may read
inbound frames on one task while another task writes outbound frames concurrently: writes are
serialised internally, and the id allocator is shared, so both are safe to use behind a shared
reference (e.g. an std::sync::Arc).
Implementations§
Source§impl<R, W> JsonRpcConnection<R, W>
impl<R, W> JsonRpcConnection<R, W>
Sourcepub fn new(read_half: R, write_half: W) -> Self
pub fn new(read_half: R, write_half: W) -> Self
Wraps a read half and a write half into a framed connection.
Sourcepub fn next_request_id(&self) -> JsonRpcId
pub fn next_request_id(&self) -> JsonRpcId
Allocates the next monotonic request id.
Ids are process-local to this connection and used only to correlate a response to the request that produced it, so a plain monotonic counter is correct here.
Sourcepub async fn send_request(
&self,
request: &JsonRpcRequest,
) -> Result<(), HarnessError>
pub async fn send_request( &self, request: &JsonRpcRequest, ) -> Result<(), HarnessError>
Writes one JSON-RPC request as a newline-delimited frame.
§Errors
Returns HarnessError::Protocol if the request cannot be serialised, or
HarnessError::Transport if the underlying write fails.
Sourcepub async fn send_notification(
&self,
notification: &JsonRpcNotification,
) -> Result<(), HarnessError>
pub async fn send_notification( &self, notification: &JsonRpcNotification, ) -> Result<(), HarnessError>
Writes one JSON-RPC notification as a newline-delimited frame.
§Errors
Returns HarnessError::Protocol if the notification cannot be serialised, or
HarnessError::Transport if the underlying write fails.
Sourcepub async fn send_response(
&self,
response: &JsonRpcResponse,
) -> Result<(), HarnessError>
pub async fn send_response( &self, response: &JsonRpcResponse, ) -> Result<(), HarnessError>
Writes one JSON-RPC response as a newline-delimited frame.
§Errors
Returns HarnessError::Protocol if the response cannot be serialised, or
HarnessError::Transport if the underlying write fails.
Sourcepub async fn recv(&self) -> Result<Option<IncomingMessage>, HarnessError>
pub async fn recv(&self) -> Result<Option<IncomingMessage>, HarnessError>
Reads the next inbound frame, classifying it into an IncomingMessage.
Returns Ok(None) at end of stream (the peer closed its write half). Blank lines are
skipped, matching the newline-delimited framing convention.
§Errors
Returns HarnessError::Transport on an I/O failure and HarnessError::Protocol on a
frame that is not a valid JSON-RPC message.