JrConnection

Struct JrConnection 

Source
pub struct JrConnection<H: JrMessageHandler, R: JrResponder<H::Link> = NullResponder> { /* private fields */ }
Expand description

A JSON-RPC connection with an active transport.

This type represents a JrConnectionBuilder that has been connected to a transport via connect_to(). It can be driven in two modes:

  • serve() - Run as a server, handling incoming messages until the connection closes
  • run_until() - Run until a closure completes, allowing you to send requests/notifications

Most users won’t construct this directly - instead use JrConnectionBuilder::connect_to() or JrConnectionBuilder::serve() for convenience.

Implementations§

Source§

impl<H: JrMessageHandler, R: JrResponder<H::Link>> JrConnection<H, R>

Source

pub async fn serve(self) -> Result<(), Error>

Run the connection in server mode with the provided transport.

This drives the connection by continuously processing messages from the transport and dispatching them to your registered handlers. The connection will run until:

  • The transport closes (e.g., EOF on byte streams)
  • An error occurs
  • One of your handlers returns an error

The transport is responsible for serializing and deserializing jsonrpcmsg::Message values to/from the underlying I/O mechanism (byte streams, channels, etc.).

Use this mode when you only need to respond to incoming messages and don’t need to initiate your own requests. If you need to send requests to the other side, use run_until instead.

§Example: Byte Stream Transport
let transport = ByteStreams::new(
    tokio::io::stdout().compat_write(),
    tokio::io::stdin().compat(),
);

UntypedLink::builder()
    .on_receive_request(async |req: MyRequest, request_cx, cx| {
        request_cx.respond(MyResponse { status: "ok".into() })
    }, sacp::on_receive_request!())
    .serve(transport)
    .await?;
Source

pub async fn run_until<T>( self, main_fn: impl AsyncFnOnce(JrConnectionCx<H::Link>) -> Result<T, Error>, ) -> Result<T, Error>

Run the connection until the provided closure completes.

This drives the connection by:

  1. Running your registered handlers in the background to process incoming messages
  2. Executing your main_fn closure with a JrConnectionCx<R> for sending requests/notifications

The connection stays active until your main_fn returns, then shuts down gracefully. If the connection closes unexpectedly before main_fn completes, this returns an error.

Use this mode when you need to initiate communication (send requests/notifications) in addition to responding to incoming messages. For server-only mode where you just respond to messages, use serve instead.

§Example
let transport = ByteStreams::new(
    tokio::io::stdout().compat_write(),
    tokio::io::stdin().compat(),
);

UntypedLink::builder()
    .on_receive_request(async |req: MyRequest, request_cx, cx| {
        // Handle incoming requests in the background
        request_cx.respond(MyResponse { status: "ok".into() })
    }, sacp::on_receive_request!())
    .connect_to(transport)?
    .run_until(async |cx| {
        // Initialize the protocol
        let init_response = cx.send_request(InitializeRequest::make())
            .block_task()
            .await?;

        // Send more requests...
        let result = cx.send_request(MyRequest {})
            .block_task()
            .await?;

        // When this closure returns, the connection shuts down
        Ok(())
    })
    .await?;
§Parameters
§Errors

Returns an error if the connection closes before main_fn completes.

Auto Trait Implementations§

§

impl<H, R> Freeze for JrConnection<H, R>
where H: Freeze, R: Freeze, <H as JrMessageHandler>::Link: Freeze,

§

impl<H, R = NullResponder> !RefUnwindSafe for JrConnection<H, R>

§

impl<H, R> Send for JrConnection<H, R>

§

impl<H, R> Sync for JrConnection<H, R>
where H: Sync, R: Sync,

§

impl<H, R> Unpin for JrConnection<H, R>
where H: Unpin, R: Unpin, <H as JrMessageHandler>::Link: Unpin,

§

impl<H, R = NullResponder> !UnwindSafe for JrConnection<H, R>

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoMaybeUndefined<T> for T

Source§

impl<T> IntoOption<T> for T

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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more