pub struct Connection { /* private fields */ }Expand description
One open connection, as a Stream of its frames.
Yields Ok frames as Mode selects, and ends after the server’s close,
after the end of the stream, or after the one Err a failed read yields.
Frames kept while a request awaited its answer come out
first, in the order they arrived. The server’s pings are answered as the
stream is read.
The server’s close is answered: the RFC 6455 close reply is flushed before
the stream yields its last item, so it goes out even when the caller never
reads again (DRIFT R3). Once the stream has ended on the close,
closed says with what code and reason, as a
Kind::Closed error.
The basis is one socket crate’s bare-tier poll_next (src/client.rs)
and its frame classifier, with the queue two other socket crates’ former
bare tiers drained first and the former close capture of one of those two
(record_close and closed_error); all five socket crates read through
this type since Stories 4.6 to 4.10. Two divergence rows stay with each
venue: what counts as liveness (D5), which Mode::All and
last_inbound let a venue decide, and what a Binary
frame means (D12): one venue decodes it as text, and the others skip it.
Implementations§
Source§impl Connection
impl Connection
Sourcepub fn new(socket: BoxSocket, mode: Mode) -> Self
pub fn new(socket: BoxSocket, mode: Mode) -> Self
Read socket in mode, on the target’s default runtime.
Sourcepub fn with_runtime(
socket: BoxSocket,
mode: Mode,
runtime: Arc<dyn Runtime>,
) -> Self
pub fn with_runtime( socket: BoxSocket, mode: Mode, runtime: Arc<dyn Runtime>, ) -> Self
Read socket in mode, on runtime: its clock stamps the frames, and
it paces and bounds the connection’s requests.
Sourcepub fn runtime(&self) -> &Arc<dyn Runtime> ⓘ
pub fn runtime(&self) -> &Arc<dyn Runtime> ⓘ
The runtime this connection runs on, for a protocol’s own timers.
Sourcepub fn last_inbound(&self) -> Instant
pub fn last_inbound(&self) -> Instant
When the last frame of any kind arrived, pings, pongs and frames read while awaiting an answer included; when the connection was made, if none has.
Sourcepub fn closed(&self) -> Option<WsError>
pub fn closed(&self) -> Option<WsError>
The server’s close, once the stream has ended on it: a
Kind::Closed with the close frame’s code and
reason, or with no code and an empty reason when the stream ended
without a close frame. A close frame without a code reads the same.
None while the connection is open, and after a failed read, whose
error the stream yielded.
Sourcepub async fn send(&mut self, message: Message) -> Result<(), WsError>
pub async fn send(&mut self, message: Message) -> Result<(), WsError>
Send one frame and flush it.
Sourcepub async fn close(&mut self) -> Result<(), WsError>
pub async fn close(&mut self) -> Result<(), WsError>
Close the connection: send a close frame and flush it. On a connection the server already closed, flush the reply if it has not gone out yet; there is nothing else to send. A connection whose read failed is already gone, so closing it does nothing.
Source§impl Connection
Requests and their answers.
impl Connection
Requests and their answers.
Ids count from 1. A request is sent no sooner than its minimum interval
after the previous one, when one is set, and then reads until the caller’s
matcher recognises its answer. Every other frame read meanwhile is kept and
yielded by the stream afterwards, in the order it arrived; an answer to
another request is dropped. With an answer timeout set, a request whose
send and wait together outlast it fails as
Kind::NoAnswer, so a peer that stops reading
cannot hang it; without one, it waits as long as the connection stays
open. A server close while waiting fails the request with the close, as
does any request after it, and a request after a failed read fails as
Kind::AlreadyClosed without sending.
The envelope is the caller’s: a builder writes the request for its id, and
a matcher reads each frame. Neither request nor its wait is cancel-safe,
and after a NoAnswer the server’s state is
unknown: replace the connection rather than reuse it.
The basis is one socket crate’s former request and await_answer
(its bare tier’s, on this correlation since Story 4.7), with its 10 s
answer timeout and 200 ms pacing, and another socket crate’s former
take_id and await_response (ws/client.rs, Story 4.6), which set
neither.
Sourcepub fn min_request_interval(self, interval: Duration) -> Self
pub fn min_request_interval(self, interval: Duration) -> Self
Send requests no sooner than interval apart. Only
request and take_paced_id
are paced: a frame sent with send, under an id from
take_id or not, neither waits nor counts.
Sourcepub fn answer_timeout(self, timeout: Duration) -> Self
pub fn answer_timeout(self, timeout: Duration) -> Self
Give up on a request after timeout, its send and its wait for the
answer together, and on await_answer’s wait.
Sourcepub fn take_id(&mut self) -> u64
pub fn take_id(&mut self) -> u64
Take the next id, for a request sent another way. A request sent that
way is not paced by min_request_interval,
and does not delay the next paced one.
Sourcepub fn take_paced_id(&mut self) -> Option<u64>
pub fn take_paced_id(&mut self) -> Option<u64>
Take the next id for a request that is sent now and paced like
request, without waiting: for a synchronous hook
such as a protocol’s ping, which cannot await.
None, taking no id, while min_request_interval
has not passed since the last paced send on the connection’s runtime
clock. Otherwise the next id, and now is recorded as the last paced
send, so the next request waits from it. The caller
must send the frame it builds for the id; an id taken and not sent
still delays the next paced request. With no interval set, or one too
long to add (never reached, as for request), it always takes the id.
Sourcepub async fn request<A>(
&mut self,
build: impl FnOnce(u64) -> Message,
answer: impl FnMut(u64, &Message) -> Correlated<A>,
) -> Result<A, WsError>
pub async fn request<A>( &mut self, build: impl FnOnce(u64) -> Message, answer: impl FnMut(u64, &Message) -> Correlated<A>, ) -> Result<A, WsError>
Send the request build writes for the next id, and read until
answer recognises its answer.
Sourcepub async fn await_answer<A>(
&mut self,
answer: impl FnMut(&Message) -> Correlated<A>,
) -> Result<A, WsError>
pub async fn await_answer<A>( &mut self, answer: impl FnMut(&Message) -> Correlated<A>, ) -> Result<A, WsError>
Read until answer recognises a frame as the answer awaited, keeping
pushes for the stream, within the answer timeout when one is set.
For an answer to something request did not send,
such as a protocol ping’s pong.
Trait Implementations§
Source§impl Debug for Connection
impl Debug for Connection
Source§impl Stream for Connection
impl Stream for Connection
Auto Trait Implementations§
impl !RefUnwindSafe for Connection
impl !Sync for Connection
impl !UnwindSafe for Connection
impl Freeze for Connection
impl Send for Connection
impl Unpin for Connection
impl UnsafeUnpin for Connection
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> StreamExt for T
impl<T> StreamExt for T
Source§fn next(&mut self) -> Next<'_, Self> ⓘwhere
Self: Unpin,
fn next(&mut self) -> Next<'_, Self> ⓘwhere
Self: Unpin,
Source§fn into_future(self) -> StreamFuture<Self> ⓘ
fn into_future(self) -> StreamFuture<Self> ⓘ
Source§fn map<T, F>(self, f: F) -> Map<Self, F>
fn map<T, F>(self, f: F) -> Map<Self, F>
Source§fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
Source§fn filter<Fut, F>(self, f: F) -> Filter<Self, Fut, F>
fn filter<Fut, F>(self, f: F) -> Filter<Self, Fut, F>
Source§fn filter_map<Fut, T, F>(self, f: F) -> FilterMap<Self, Fut, F>
fn filter_map<Fut, T, F>(self, f: F) -> FilterMap<Self, Fut, F>
Source§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
Source§fn collect<C>(self) -> Collect<Self, C> ⓘ
fn collect<C>(self) -> Collect<Self, C> ⓘ
Source§fn unzip<A, B, FromA, FromB>(self) -> Unzip<Self, FromA, FromB> ⓘ
fn unzip<A, B, FromA, FromB>(self) -> Unzip<Self, FromA, FromB> ⓘ
Source§fn concat(self) -> Concat<Self> ⓘ
fn concat(self) -> Concat<Self> ⓘ
Source§fn count(self) -> Count<Self> ⓘwhere
Self: Sized,
fn count(self) -> Count<Self> ⓘwhere
Self: Sized,
Source§fn fold<T, Fut, F>(self, init: T, f: F) -> Fold<Self, Fut, T, F> ⓘ
fn fold<T, Fut, F>(self, init: T, f: F) -> Fold<Self, Fut, T, F> ⓘ
Source§fn any<Fut, F>(self, f: F) -> Any<Self, Fut, F> ⓘ
fn any<Fut, F>(self, f: F) -> Any<Self, Fut, F> ⓘ
true if any element in stream satisfied a predicate. Read moreSource§fn all<Fut, F>(self, f: F) -> All<Self, Fut, F> ⓘ
fn all<Fut, F>(self, f: F) -> All<Self, Fut, F> ⓘ
true if all element in stream satisfied a predicate. Read moreSource§fn flatten(self) -> Flatten<Self>
fn flatten(self) -> Flatten<Self>
Source§fn flatten_unordered(
self,
limit: impl Into<Option<usize>>,
) -> FlattenUnorderedWithFlowController<Self, ()>
fn flatten_unordered( self, limit: impl Into<Option<usize>>, ) -> FlattenUnorderedWithFlowController<Self, ()>
Source§fn flat_map_unordered<U, F>(
self,
limit: impl Into<Option<usize>>,
f: F,
) -> FlatMapUnordered<Self, U, F>
fn flat_map_unordered<U, F>( self, limit: impl Into<Option<usize>>, f: F, ) -> FlatMapUnordered<Self, U, F>
StreamExt::map but flattens nested Streams
and polls them concurrently, yielding items in any order, as they made
available. Read moreSource§fn scan<S, B, Fut, F>(self, initial_state: S, f: F) -> Scan<Self, S, Fut, F>
fn scan<S, B, Fut, F>(self, initial_state: S, f: F) -> Scan<Self, S, Fut, F>
StreamExt::fold that holds internal state
and produces a new stream. Read moreSource§fn skip_while<Fut, F>(self, f: F) -> SkipWhile<Self, Fut, F>
fn skip_while<Fut, F>(self, f: F) -> SkipWhile<Self, Fut, F>
true. Read moreSource§fn take_while<Fut, F>(self, f: F) -> TakeWhile<Self, Fut, F>
fn take_while<Fut, F>(self, f: F) -> TakeWhile<Self, Fut, F>
true. Read moreSource§fn take_until<Fut>(self, fut: Fut) -> TakeUntil<Self, Fut>
fn take_until<Fut>(self, fut: Fut) -> TakeUntil<Self, Fut>
Source§fn for_each<Fut, F>(self, f: F) -> ForEach<Self, Fut, F> ⓘ
fn for_each<Fut, F>(self, f: F) -> ForEach<Self, Fut, F> ⓘ
Source§fn for_each_concurrent<Fut, F>(
self,
limit: impl Into<Option<usize>>,
f: F,
) -> ForEachConcurrent<Self, Fut, F> ⓘ
fn for_each_concurrent<Fut, F>( self, limit: impl Into<Option<usize>>, f: F, ) -> ForEachConcurrent<Self, Fut, F> ⓘ
Source§fn take(self, n: usize) -> Take<Self>where
Self: Sized,
fn take(self, n: usize) -> Take<Self>where
Self: Sized,
n items of the underlying stream. Read moreSource§fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
n items of the underlying stream. Read moreSource§fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
Source§fn boxed<'a>(self) -> Pin<Box<dyn Stream<Item = Self::Item> + Send + 'a>>
fn boxed<'a>(self) -> Pin<Box<dyn Stream<Item = Self::Item> + Send + 'a>>
Source§fn boxed_local<'a>(self) -> Pin<Box<dyn Stream<Item = Self::Item> + 'a>>where
Self: Sized + 'a,
fn boxed_local<'a>(self) -> Pin<Box<dyn Stream<Item = Self::Item> + 'a>>where
Self: Sized + 'a,
Source§fn buffered(self, n: usize) -> Buffered<Self>
fn buffered(self, n: usize) -> Buffered<Self>
Source§fn buffer_unordered(self, n: usize) -> BufferUnordered<Self>
fn buffer_unordered(self, n: usize) -> BufferUnordered<Self>
Source§fn zip<St>(self, other: St) -> Zip<Self, St>
fn zip<St>(self, other: St) -> Zip<Self, St>
Source§fn peekable(self) -> Peekable<Self>where
Self: Sized,
fn peekable(self) -> Peekable<Self>where
Self: Sized,
peek method. Read moreSource§fn chunks(self, capacity: usize) -> Chunks<Self>where
Self: Sized,
fn chunks(self, capacity: usize) -> Chunks<Self>where
Self: Sized,
Source§fn ready_chunks(self, capacity: usize) -> ReadyChunks<Self>where
Self: Sized,
fn ready_chunks(self, capacity: usize) -> ReadyChunks<Self>where
Self: Sized,
Source§fn forward<S>(self, sink: S) -> Forward<Self, S> ⓘ
fn forward<S>(self, sink: S) -> Forward<Self, S> ⓘ
Source§fn split<Item>(self) -> (SplitSink<Self, Item>, SplitStream<Self>)
fn split<Item>(self) -> (SplitSink<Self, Item>, SplitStream<Self>)
Source§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
Source§fn left_stream<B>(self) -> Either<Self, B> ⓘ
fn left_stream<B>(self) -> Either<Self, B> ⓘ
Source§fn right_stream<B>(self) -> Either<B, Self> ⓘ
fn right_stream<B>(self) -> Either<B, Self> ⓘ
Source§fn poll_next_unpin(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>where
Self: Unpin,
fn poll_next_unpin(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>where
Self: Unpin,
Stream::poll_next on Unpin
stream types.Source§fn select_next_some(&mut self) -> SelectNextSome<'_, Self> ⓘwhere
Self: Unpin + FusedStream,
fn select_next_some(&mut self) -> SelectNextSome<'_, Self> ⓘwhere
Self: Unpin + FusedStream,
Source§impl<S> TryStreamExt for S
impl<S> TryStreamExt for S
Source§fn err_into<E>(self) -> ErrInto<Self, E>
fn err_into<E>(self) -> ErrInto<Self, E>
Source§fn map_ok<T, F>(self, f: F) -> MapOk<Self, F>
fn map_ok<T, F>(self, f: F) -> MapOk<Self, F>
Source§fn map_err<E, F>(self, f: F) -> MapErr<Self, F>
fn map_err<E, F>(self, f: F) -> MapErr<Self, F>
Source§fn and_then<Fut, F>(self, f: F) -> AndThen<Self, Fut, F>
fn and_then<Fut, F>(self, f: F) -> AndThen<Self, Fut, F>
f. Read moreSource§fn or_else<Fut, F>(self, f: F) -> OrElse<Self, Fut, F>
fn or_else<Fut, F>(self, f: F) -> OrElse<Self, Fut, F>
f. Read moreSource§fn inspect_ok<F>(self, f: F) -> InspectOk<Self, F>
fn inspect_ok<F>(self, f: F) -> InspectOk<Self, F>
Source§fn inspect_err<F>(self, f: F) -> InspectErr<Self, F>
fn inspect_err<F>(self, f: F) -> InspectErr<Self, F>
Source§fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
Source§fn try_next(&mut self) -> TryNext<'_, Self> ⓘwhere
Self: Unpin,
fn try_next(&mut self) -> TryNext<'_, Self> ⓘwhere
Self: Unpin,
Source§fn try_for_each<Fut, F>(self, f: F) -> TryForEach<Self, Fut, F> ⓘ
fn try_for_each<Fut, F>(self, f: F) -> TryForEach<Self, Fut, F> ⓘ
Source§fn try_skip_while<Fut, F>(self, f: F) -> TrySkipWhile<Self, Fut, F>
fn try_skip_while<Fut, F>(self, f: F) -> TrySkipWhile<Self, Fut, F>
true. Read moreSource§fn try_take_while<Fut, F>(self, f: F) -> TryTakeWhile<Self, Fut, F>
fn try_take_while<Fut, F>(self, f: F) -> TryTakeWhile<Self, Fut, F>
true. Read moreSource§fn try_for_each_concurrent<Fut, F>(
self,
limit: impl Into<Option<usize>>,
f: F,
) -> TryForEachConcurrent<Self, Fut, F> ⓘ
fn try_for_each_concurrent<Fut, F>( self, limit: impl Into<Option<usize>>, f: F, ) -> TryForEachConcurrent<Self, Fut, F> ⓘ
Source§fn try_collect<C>(self) -> TryCollect<Self, C> ⓘ
fn try_collect<C>(self) -> TryCollect<Self, C> ⓘ
Source§fn try_chunks(self, capacity: usize) -> TryChunks<Self>where
Self: Sized,
fn try_chunks(self, capacity: usize) -> TryChunks<Self>where
Self: Sized,
Source§fn try_ready_chunks(self, capacity: usize) -> TryReadyChunks<Self>where
Self: Sized,
fn try_ready_chunks(self, capacity: usize) -> TryReadyChunks<Self>where
Self: Sized,
Source§fn try_filter<Fut, F>(self, f: F) -> TryFilter<Self, Fut, F>
fn try_filter<Fut, F>(self, f: F) -> TryFilter<Self, Fut, F>
Source§fn try_filter_map<Fut, F, T>(self, f: F) -> TryFilterMap<Self, Fut, F>
fn try_filter_map<Fut, F, T>(self, f: F) -> TryFilterMap<Self, Fut, F>
Source§fn try_flatten_unordered(
self,
limit: impl Into<Option<usize>>,
) -> TryFlattenUnordered<Self>
fn try_flatten_unordered( self, limit: impl Into<Option<usize>>, ) -> TryFlattenUnordered<Self>
Source§fn try_flatten(self) -> TryFlatten<Self>
fn try_flatten(self) -> TryFlatten<Self>
Source§fn try_fold<T, Fut, F>(self, init: T, f: F) -> TryFold<Self, Fut, T, F> ⓘ
fn try_fold<T, Fut, F>(self, init: T, f: F) -> TryFold<Self, Fut, T, F> ⓘ
Source§fn try_concat(self) -> TryConcat<Self> ⓘ
fn try_concat(self) -> TryConcat<Self> ⓘ
Source§fn try_buffer_unordered(self, n: usize) -> TryBufferUnordered<Self>
fn try_buffer_unordered(self, n: usize) -> TryBufferUnordered<Self>
Source§fn try_buffered(self, n: usize) -> TryBuffered<Self>
fn try_buffered(self, n: usize) -> TryBuffered<Self>
Source§fn try_poll_next_unpin(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Self::Ok, Self::Error>>>where
Self: Unpin,
fn try_poll_next_unpin(
&mut self,
cx: &mut Context<'_>,
) -> Poll<Option<Result<Self::Ok, Self::Error>>>where
Self: Unpin,
TryStream::try_poll_next on Unpin
stream types.Source§fn try_all<Fut, F>(self, f: F) -> TryAll<Self, Fut, F> ⓘ
fn try_all<Fut, F>(self, f: F) -> TryAll<Self, Fut, F> ⓘ
Err is encountered or if an Ok item is found
that does not satisfy the predicate. Read more