Crates.io | mio-poll-handler |
lib.rs | mio-poll-handler |
version | 0.1.0 |
source | src |
created_at | 2018-05-26 10:02:54.018143 |
updated_at | 2018-05-26 10:02:54.018143 |
description | Wrapper that slightly simplifies handling tokens and evented in mio |
homepage | |
repository | https://github.com/trangar/mio-poll-handler |
max_upload_size | |
id | 67112 |
size | 6,110 |
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);
}
}