pub trait SessionFactory:
Send
+ Sync
+ 'static {
type Session: Session;
// Required method
fn open(&self, info: &ConnectionInfo) -> Result<Self::Session, RefusalCause>;
}Expand description
Creates one session per admitted connection. Any Fn(&ConnectionInfo) -> S is one.
§Examples
use net_io::{ConnectionInfo, Input, RefusalCause, Session, SessionContext, SessionFactory, Step};
struct Discard;
impl Session for Discard {
fn on_data(&mut self, _cx: &mut SessionContext<'_>, input: &mut Input) -> Step {
let n = input.bytes().len();
input.consume(n);
Step::Read
}
}
struct OnlyLoopback;
impl SessionFactory for OnlyLoopback {
type Session = Discard;
fn open(&self, info: &ConnectionInfo) -> Result<Discard, RefusalCause> {
match info.client_addr() {
Some(addr) if addr.ip().is_loopback() => Ok(Discard),
_ => Err(RefusalCause::Session),
}
}
}
let info = ConnectionInfo::for_testing("10.0.0.1:1".parse().unwrap());
assert!(OnlyLoopback.open(&info).is_err());Required Associated Types§
Required Methods§
Sourcefn open(&self, info: &ConnectionInfo) -> Result<Self::Session, RefusalCause>
fn open(&self, info: &ConnectionInfo) -> Result<Self::Session, RefusalCause>
A session for this connection.
§Errors
Refusing closes the connection with CloseReason::Refused(cause).
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".