pub trait Connection: 'static {
// Required method
fn on_data(&mut self, cx: &mut Ctx<'_>, data: &[u8]) -> usize;
// Provided methods
fn on_open(&mut self, cx: &mut Ctx<'_>) { ... }
fn on_eof(&mut self, cx: &mut Ctx<'_>) { ... }
fn on_drain(&mut self, cx: &mut Ctx<'_>) { ... }
fn on_timer(&mut self, cx: &mut Ctx<'_>, token: u64) { ... }
fn on_message(&mut self, cx: &mut Ctx<'_>, msg: Message) { ... }
fn on_stop(&mut self, cx: &mut Ctx<'_>) { ... }
fn on_close(&mut self, reason: CloseReason) { ... }
}Expand description
A connection’s protocol state machine. It lives on one loop thread and is called only from
there, so it need not be Send. A sans-io protocol crate sits behind it: received bytes go
in through Connection::on_data, bytes to send go out through Ctx::send.
Callbacks never run concurrently and never re-enter: a call made through the Ctx (send,
close, timer) takes effect after the callback returns or at the end of the loop turn.
Required Methods§
Sourcefn on_data(&mut self, cx: &mut Ctx<'_>, data: &[u8]) -> usize
fn on_data(&mut self, cx: &mut Ctx<'_>, data: &[u8]) -> usize
Bytes arrived. data holds everything received and not yet consumed, in order. Return
how many bytes from the front were consumed; the rest is offered again, with whatever
arrives next, on the next call. Consume every complete message you can in one call.
While reading is paused (Ctx::pause_reading) this is not called.
Provided Methods§
Sourcefn on_open(&mut self, cx: &mut Ctx<'_>)
fn on_open(&mut self, cx: &mut Ctx<'_>)
The connection was registered with the loop; called once, before any other callback. Arm timers here (a request deadline must run even if the peer never sends a byte).
Sourcefn on_eof(&mut self, cx: &mut Ctx<'_>)
fn on_eof(&mut self, cx: &mut Ctx<'_>)
The peer closed its sending side. The default closes the connection gracefully.
Sourcefn on_drain(&mut self, cx: &mut Ctx<'_>)
fn on_drain(&mut self, cx: &mut Ctx<'_>)
Queued output drained: below the low-water mark after being above the high-water mark
(reading had been paused for it and is resumed), or after Ctx::notify_drain.
Sourcefn on_timer(&mut self, cx: &mut Ctx<'_>, token: u64)
fn on_timer(&mut self, cx: &mut Ctx<'_>, token: u64)
A timer set with Ctx::set_timer expired.
Sourcefn on_message(&mut self, cx: &mut Ctx<'_>, msg: Message)
fn on_message(&mut self, cx: &mut Ctx<'_>, msg: Message)
A message from another thread arrived (crate::ConnHandle::send or an offloaded job).
Sourcefn on_stop(&mut self, cx: &mut Ctx<'_>)
fn on_stop(&mut self, cx: &mut Ctx<'_>)
The runtime is stopping gracefully. Finish what is in flight and close; the default closes at once (after flushing). Connections still open when the grace period ends are aborted.
Sourcefn on_close(&mut self, reason: CloseReason)
fn on_close(&mut self, reason: CloseReason)
The connection is gone; this is the last call.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".