#mio #poll #wrapper #tokens #evented #simplifies

mio-poll-wrapper

Wrapper that slightly simplifies handling tokens and evented in mio

1 unstable release

Uses old Rust 2015

0.1.0 May 26, 2018

#3 in #evented

MIT license

7KB
60 lines

Simple wrapper around mio's Poll method.

extern crate mio;
extern crate mio_poll_wrapper;

use mio_poll_wrapper::PollWrapper;
use mio::net::TcpListener;
use std::collections::HashMap;

fn main() {
    let mut handle = PollWrapper::new().unwrap();

    let listener = TcpListener::bind(&"0.0.0.0:8000".parse().unwrap()).unwrap();

    let process_token = handle.register(&listener).unwrap();
    let mut clients = HashMap::new();

    let result: ::std::io::Result<()> = handle.handle(|event, handle| {
        if event.token() == process_token {
            let (stream, addr) = listener.accept()?;
            println!("Accepted socket from {:?}", addr);
            let token = handle.register(&stream)?;
            clients.insert(token, stream);
        } else if let Some(client) = clients.get_mut(&event.token()) {
            println!("Received data from client {:?}", client.peer_addr());
        }
        Ok(())
    });

    if let Err(e) = result {
        println!("Could not execute: {:?}", e);
    }
}

Dependencies

~0.6–1MB
~14K SLoC