Skip to main content

SessionFactory

Trait SessionFactory 

Source
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§

Source

type Session: Session

The session type.

Required Methods§

Source

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".

Implementors§

Source§

impl<F, S> SessionFactory for F
where F: Fn(&ConnectionInfo) -> S + Send + Sync + 'static, S: Session,