mio-poll-wrapper

Crates.iomio-poll-wrapper
lib.rsmio-poll-wrapper
version0.1.0
sourcesrc
created_at2018-05-26 10:05:06.946369
updated_at2018-05-26 10:05:06.946369
descriptionWrapper that slightly simplifies handling tokens and evented in mio
homepage
repositoryhttps://github.com/trangar/mio-poll-wrapper
max_upload_size
id67113
size6,110
Trangar (VictorKoenders)

documentation

README

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);
    }
}
Commit count: 11

cargo fmt