pub trait Session: Send + 'static {
// Required method
fn on_data(
&mut self,
cx: &mut SessionContext<'_>,
input: &mut Input,
) -> Step;
// Provided methods
fn on_open(&mut self, cx: &mut SessionContext<'_>) -> Step { ... }
fn on_writable(&mut self, cx: &mut SessionContext<'_>) -> Step { ... }
fn on_wake(
&mut self,
cx: &mut SessionContext<'_>,
token: u32,
value: u64,
) -> Step { ... }
fn on_timeout(
&mut self,
cx: &mut SessionContext<'_>,
which: TimeoutKind,
) -> Step { ... }
fn on_drain(&mut self, cx: &mut SessionContext<'_>) -> Step { ... }
fn on_peer_closed(&mut self, cx: &mut SessionContext<'_>) -> Step { ... }
fn on_denied(&mut self, code: u16, output: &mut Output) { ... }
fn on_close(&mut self, info: &ConnectionInfo, reason: CloseReason) { ... }
}Expand description
A sans-IO protocol engine bound to one connection.
net-io reads, calls the session with the bytes, and does what the returned
Step says. Sessions are called only on the worker that owns the
connection, never concurrently.
§Examples
use net_io::{Input, Session, SessionContext, Step};
struct Echo;
impl Session for Echo {
fn on_data(&mut self, cx: &mut SessionContext<'_>, input: &mut Input) -> Step {
// Copy as much as the output budget takes; the rest stays in `input`,
// and net-io calls on_data again after the flush because the input changed.
let taken = cx.output().push_partial(input.bytes());
input.consume(taken);
Step::Write
}
}
let server = net_io::serve_sessions("127.0.0.1:7002", |_info: &net_io::ConnectionInfo| Echo)?;
server.wait_for_stop();Required Methods§
Sourcefn on_data(&mut self, cx: &mut SessionContext<'_>, input: &mut Input) -> Step
fn on_data(&mut self, cx: &mut SessionContext<'_>, input: &mut Input) -> Step
Bytes arrived; input holds everything not yet consumed.
Provided Methods§
Sourcefn on_open(&mut self, cx: &mut SessionContext<'_>) -> Step
fn on_open(&mut self, cx: &mut SessionContext<'_>) -> Step
Once, after admission and the handshake.
Sourcefn on_writable(&mut self, cx: &mut SessionContext<'_>) -> Step
fn on_writable(&mut self, cx: &mut SessionContext<'_>) -> Step
After a Step::Write flushed all pending output.
Sourcefn on_wake(
&mut self,
cx: &mut SessionContext<'_>,
token: u32,
value: u64,
) -> Step
fn on_wake( &mut self, cx: &mut SessionContext<'_>, token: u32, value: u64, ) -> Step
Another thread posted a completion through this connection’s Completer.
Sourcefn on_timeout(
&mut self,
cx: &mut SessionContext<'_>,
which: TimeoutKind,
) -> Step
fn on_timeout( &mut self, cx: &mut SessionContext<'_>, which: TimeoutKind, ) -> Step
A deadline passed. Close or Abort closes with the timeout’s own reason
and close style; anything else re-arms with the same limits.
Sourcefn on_drain(&mut self, cx: &mut SessionContext<'_>) -> Step
fn on_drain(&mut self, cx: &mut SessionContext<'_>) -> Step
A drain reached the connection between exchanges (idle, nothing unread), once.
Close (the default) closes it with Drained after flushing what this queued,
as before 0.8.0. Any other step keeps it: the drain no longer closes it
between exchanges, and the session closes it itself (a WebSocket sends its
1001 Close and reads the peer’s), or the drain deadline does.
Sourcefn on_peer_closed(&mut self, cx: &mut SessionContext<'_>) -> Step
fn on_peer_closed(&mut self, cx: &mut SessionContext<'_>) -> Step
The peer sent FIN; the session may keep writing (half-close).
Sourcefn on_denied(&mut self, code: u16, output: &mut Output)
fn on_denied(&mut self, code: u16, output: &mut Output)
The gate denied a write and no byte of the current response has left:
write the protocol’s refusal for code into output.
Sourcefn on_close(&mut self, info: &ConnectionInfo, reason: CloseReason)
fn on_close(&mut self, info: &ConnectionInfo, reason: CloseReason)
Exactly once, with the one close reason.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".