Skip to main content

Connection

Struct Connection 

Source
pub struct Connection { /* private fields */ }
Expand description

Cloneable handle of a connection task.

The task ends when Connection::shutdown is called or the last handle is dropped.

Implementations§

Source§

impl Connection

Source

pub async fn open<C: Connect>( connector: C, host: &str, port: u16, options: ConnectionOptions, ) -> Result<Self, ConnectorError>

Connects to host on port and starts the connection task.

Must be called from within a Tokio runtime.

§Errors

Returns ConnectorError::Transport when the connection cannot be established.

Source

pub async fn request<R: Request>( &self, request: &R, ) -> Result<R::Response, ConnectorError>

Sends a request and waits for its response.

§Errors

Returns ConnectorError::Server when the server answers with an error, ConnectorError::Timeout when it does not answer in time, ConnectorError::Transport when the connection fails while the request is pending, ConnectorError::UnexpectedResponse when the answer has another type, and ConnectorError::Closed when the connection is shut down.

Source

pub async fn request_message( &self, payload_type: u32, payload: Vec<u8>, ) -> Result<ProtoMessage, ConnectorError>

Sends an already encoded request and waits for the envelope of its response.

§Errors

Fails like Connection::request, except that the type of the answer is not checked.

Source

pub async fn request_once( &self, payload_type: u32, payload: Vec<u8>, ) -> Result<ProtoMessage, ConnectorError>

Sends an already encoded request that the server must not receive twice, such as an order, and waits for the envelope of the first message that answers it.

The request is written to the socket at most once. It is not sent again when the server answers that a limit was exceeded, which is handed to the caller as the refusal it is, and it is never replayed after a reconnection. The answer is also reported to the subscribers of Connection::events as an Event::Message, ahead of the messages the server sends after it, so that a reader of the events sees all the server said about the request in the order it was said.

§Errors

Returns ConnectorError::OutcomeUnknown when the request was written and the connection failed or the time allowed ran out before an answer arrived, in which case the server may or may not have acted on it. ConnectorError::Transport means the request was not written: the connection was down when it was made or was lost while it waited its turn. ConnectorError::Closed is returned when the connection is shut down from this side, also while the answer is awaited. Fails like Connection::request_message otherwise.

Source

pub async fn register_replay<R: Request>( &self, key: ReplayKey, request: &R, ) -> Result<(), ConnectorError>

Registers a request to be sent again, under key, after every reconnection.

The request is not sent now. Requests are replayed in the order of their keys. A request registered or replaced while the connection is being restored is replayed before Event::Reconnected is reported.

§Errors

Returns ConnectorError::Closed when the connection is shut down.

Source

pub async fn remove_replay(&self, key: ReplayKey) -> Result<(), ConnectorError>

Forgets the request registered under key.

While the connection is being restored the registered requests are looked up one at a time, each just before it is sent, so a request removed before its turn is not replayed. A request that was replayed already stays in force on the server: the task knows no request that would withdraw it, which is left to the caller once Event::Reconnected is reported.

§Errors

Returns ConnectorError::Closed when the connection is shut down.

Source

pub async fn force_reconnect(&self) -> Result<(), ConnectorError>

Drops the socket on this side, which starts a reconnection as a loss would.

§Errors

Returns ConnectorError::Closed when the connection is shut down.

Source

pub fn events(&self) -> Receiver<Event>

Subscribes to the events of the connection, starting with the next one.

A subscriber that falls more than the event capacity behind skips the oldest events and is told how many were skipped.

Source

pub async fn closed(&self)

Waits until the connection task has ended, after which every request fails with ConnectorError::Closed.

Source

pub fn stats(&self) -> Stats

Returns the counters of the connection.

Source

pub async fn shutdown(&self)

Stops the connection task and waits until the socket is closed.

Requests still pending fail with ConnectorError::Closed. Calling it again is harmless.

Trait Implementations§

Source§

impl Clone for Connection

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Connection

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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