pub struct Context<Rin, Rout, Win, Wout> { /* private fields */ }Expand description
Handler and context types for building protocol pipelines
Enables a Handler to interact with the pipeline.
The Context object is passed to each handler method and provides the API
for forwarding messages, polling data, and propagating events through the
pipeline.
§Type Parameters
Rin: Read input message type (what the current handler receives)Rout: Read output message type (what the current handler produces)Win: Write input message type (what the current handler receives for writing)Wout: Write output message type (what the current handler produces for writing)
§Responsibilities
The context provides two primary functions:
-
Forward Operations: Push data/events down the pipeline
fire_handle_read(): Forward processed inbound messagesfire_transport_active(): Propagate connection established eventfire_handle_timeout(): Propagate timeout events
-
Pull Operations: Pull data from the next handler
fire_poll_write(): Poll outbound messages from next handlerfire_poll_timeout(): Poll timeout requirements from next handler
§Example
use sansio::{Handler, Context};
struct MyHandler;
impl Handler for MyHandler {
type Rin = String;
type Rout = String;
type Win = String;
type Wout = String;
fn name(&self) -> &str {
"MyHandler"
}
fn handle_read(&mut self, ctx: &Context<Self::Rin, Self::Rout, Self::Win, Self::Wout>, msg: Self::Rin) {
// Use context to forward message
ctx.fire_handle_read(msg);
}
fn poll_write(&mut self, ctx: &Context<Self::Rin, Self::Rout, Self::Win, Self::Wout>) -> Option<Self::Wout> {
// Use context to poll next handler
ctx.fire_poll_write()
}
}Implementations§
Source§impl<Rin: 'static, Rout: 'static, Win: 'static, Wout: 'static> Context<Rin, Rout, Win, Wout>
impl<Rin: 'static, Rout: 'static, Win: 'static, Wout: 'static> Context<Rin, Rout, Win, Wout>
Sourcepub fn new(name: &str) -> Self
pub fn new(name: &str) -> Self
Creates a new Context.
Typically called internally by the pipeline when adding a handler. You rarely need to create a Context manually.
§Parameters
name: The name of the handler this context belongs to
Sourcepub fn fire_transport_active(&self)
pub fn fire_transport_active(&self)
Propagates transport active event to the next handler.
Call this to forward the “connection established” event through the pipeline.
This should be called from your Handler::transport_active implementation
unless you explicitly want to stop event propagation.
Sourcepub fn fire_transport_inactive(&self)
pub fn fire_transport_inactive(&self)
Propagates transport inactive event to the next handler.
Call this to forward the “connection closed” event through the pipeline.
This should be called from your Handler::transport_inactive implementation
unless you explicitly want to stop event propagation.
Sourcepub fn fire_handle_read(&self, msg: Rout)
pub fn fire_handle_read(&self, msg: Rout)
Forwards an inbound message to the next handler.
After processing a message in Handler::handle_read, call this method
to pass the transformed message (Rout) to the next handler in the pipeline.
§Parameters
msg: The processed message to forward
§Example
fn handle_read(&mut self, ctx: &Context<Self::Rin, Self::Rout, Self::Win, Self::Wout>, msg: Self::Rin) {
// Decode bytes to string
if let Ok(decoded) = String::from_utf8(msg) {
// Forward decoded message
ctx.fire_handle_read(decoded);
}
}Sourcepub fn fire_poll_write(&self) -> Option<Win>
pub fn fire_poll_write(&self) -> Option<Win>
Polls the next handler for an outbound message.
Call this from Handler::poll_write to retrieve messages from the next
handler in the pipeline. Returns Some(Win) if the next handler has data
to send, or None otherwise.
§Returns
Some(Win): A message from the next handlerNone: No message available
§Example
fn poll_write(&mut self, ctx: &Context<Self::Rin, Self::Rout, Self::Win, Self::Wout>) -> Option<Self::Wout> {
// Poll next handler first
if let Some(msg) = ctx.fire_poll_write() {
// Encode string to bytes
return Some(msg.into_bytes());
}
// Then return our own queued data
self.queue.pop_front()
}Sourcepub fn fire_handle_timeout(&self, now: Instant)
pub fn fire_handle_timeout(&self, now: Instant)
Propagates timeout event to the next handler.
Call this from Handler::handle_timeout to forward the timeout event
through the pipeline.
§Parameters
now: The current timestamp
Sourcepub fn fire_poll_timeout(&self, eto: &mut Instant)
pub fn fire_poll_timeout(&self, eto: &mut Instant)
Polls the next handler for its timeout deadline.
Call this from Handler::poll_timeout to allow the next handler to
update the earliest timeout deadline.
§Parameters
eto: Mutable reference to the earliest timeout. The next handler may update this.
Sourcepub fn fire_handle_eof(&self)
pub fn fire_handle_eof(&self)
Propagates EOF event to the next handler.
Call this from Handler::handle_eof to forward the end-of-file event
through the pipeline.
Sourcepub fn fire_handle_error(&self, err: Box<dyn Error>)
pub fn fire_handle_error(&self, err: Box<dyn Error>)
Propagates error event to the next handler.
Call this from Handler::handle_error to forward the error through
the pipeline.
§Parameters
err: The error to propagate
Sourcepub fn fire_handle_close(&self)
pub fn fire_handle_close(&self)
Propagates close event to the next handler.
Call this from Handler::handle_close to forward the close request
through the pipeline.