Skip to main content

CloseHandle

Struct CloseHandle 

Source
pub struct CloseHandle { /* private fields */ }
Available on crate feature server only.
Expand description

Closes a running Connection from outside it, for example when the credentials of the opening handshake expire (RFC 8887 §4.2). Get one with Connection::close_handle before calling run; clone it freely and send it to another task.

close makes run send a Close frame with the given code and reason, run the same shutdown as any other end (push subscription dropped, extension queue closed, ExtensionHandler::close called), and return Ok(()). run does not wait for the client’s answering Close; the transport (for example tungstenite::serve) drops the socket.

Nothing queued is sent ahead of the Close: Responses of Requests still running, queued extension messages and push notifications not yet written are dropped. A message run is writing when close is called is finished first. After credentials expire this is what the drafts ask: “A server that does not close the connection when credentials expire SHOULD NOT deliver further ephemeral events on that connection” (draft-atwood-jmap-chat-wss-00).

use std::sync::Arc;
use std::time::Duration;
use jmap_websocket::jmap_server::Dispatcher;
use jmap_websocket::jmap_types::State;
use jmap_websocket::server::{CloseCode, Connection, DispatcherHandler};

let handler = Arc::new(DispatcherHandler::new(dispatcher, (), || State::from("s0")));
let connection = Connection::new(handler);
let close = connection.close_handle();
// The credentials of the opening handshake expire after `expires_in`.
tokio::spawn(async move {
    tokio::time::sleep(expires_in).await;
    close.close(CloseCode::POLICY_VIOLATION, "credentials expired");
});
jmap_websocket::tungstenite::serve(connection, ws).await

Implementations§

Source§

impl CloseHandle

Source

pub fn close(&self, code: CloseCode, reason: impl Into<String>) -> bool

Ask the connection to close with code and reason.

reason longer than MAX_CLOSE_REASON_LEN bytes is cut to the longest prefix within it that ends on a character boundary, so the Close frame stays a valid control frame (RFC 6455 §5.5).

Returns true when this call is the request that run acts on. Returns false, and does nothing, when an earlier close (through any clone) asked already (the first request wins), or run has returned (the client closed, the stream ended, or it failed). Called before run starts, the Close is the first thing run sends. true is not a promise of delivery: the client may close, or the socket fail, at the same moment.

Trait Implementations§

Source§

impl Clone for CloseHandle

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 CloseHandle

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> ErasedDestructor for T
where T: 'static,

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

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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