Skip to main content

JsonRpcConnection

Struct JsonRpcConnection 

Source
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>
where R: AsyncRead + Unpin + Send, W: AsyncWrite + Unpin + Send,

Source

pub fn new(read_half: R, write_half: W) -> Self

Wraps a read half and a write half into a framed connection.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Auto Trait Implementations§

§

impl<R, W> !Freeze for JsonRpcConnection<R, W>

§

impl<R, W> !RefUnwindSafe for JsonRpcConnection<R, W>

§

impl<R, W> Send for JsonRpcConnection<R, W>
where W: Send, R: Send,

§

impl<R, W> Sync for JsonRpcConnection<R, W>
where W: Send, R: Send,

§

impl<R, W> Unpin for JsonRpcConnection<R, W>
where W: Unpin, R: Unpin,

§

impl<R, W> UnsafeUnpin for JsonRpcConnection<R, W>
where W: UnsafeUnpin, R: UnsafeUnpin,

§

impl<R, W> UnwindSafe for JsonRpcConnection<R, W>
where W: UnwindSafe, R: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.