Trait Listen

Source
pub trait Listen: Send + Sync {
    // Required method
    fn accept(&self) -> impl Future<Output = Result<Stream>> + Send;
}
Expand description

trait for defining how socket listener would accept remote connection and omit connection stream asynchronously

listener must be thread safe type for parallel accessing by multiple worker threads.

§Examples

use std::io;

use xitca_io::net::Stream;
use xitca_server::net::{IntoListener, Listen};
use xitca_service::fn_service;

// arbitrary socket type
struct MySocket;

impl Listen for MySocket {
    async fn accept(&self) -> io::Result<Stream> {
        todo!("defining how my socket would accept remote connection in the type of Stream")
    }
}

// arbitrary builder type for socket. allow for additional logic when constructing socket type
struct MySocketBuilder;

impl IntoListener for MySocketBuilder {
    type Listener = MySocket;

    fn into_listener(self) -> io::Result<Self::Listener> {
        // transform socket builder to the socket runner type.
        // this function is called from inside xitca-server and it's possible to tap into it's internal from here.
        // e.g: accessing the thread local storage or the async runtime(tokio)'s context.
        Ok(MySocket)
    }
}

// service function receive connection stream from MySocket's Listen::accept method
let service = fn_service(async |stream: Stream| {
    Ok::<_, io::Error>(())
});

// start a server with socket builder where My socket would be instantiated and it's accepting logic would start and
// run the service function when successfully accepted remote connection.
let _ = xitca_server::Builder::new().listen("my_socket_service", MySocketBuilder, service);

Required Methods§

Source

fn accept(&self) -> impl Future<Output = Result<Stream>> + Send

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Listen for TcpListener

Source§

async fn accept(&self) -> Result<Stream>

Source§

impl Listen for UnixListener

Source§

async fn accept(&self) -> Result<Stream>

Implementors§