pub struct Token(pub usize);
Associates readiness notifications with Evented
handles.
Token
is a wrapper around usize
and is used as an argument to
Poll::register
and Poll::reregister
.
See Poll
for more documentation on polling.
Using Token
to track which socket generated the notification. In this
example, HashMap
is used, but usually something like slab
is better.
use mio::{Events, Ready, Poll, PollOpt, Token};
use mio::net::TcpListener;
use std::thread;
use std::io::{self, Read};
use std::collections::HashMap;
const MAX_SOCKETS: usize = 32;
const LISTENER: Token = Token(1024);
let mut sockets = HashMap::new();
let mut next_socket_index = 0;
let poll = Poll::new()?;
let listener = TcpListener::bind(&"127.0.0.1:0".parse()?)?;
poll.register(&listener,
LISTENER,
Ready::readable(),
PollOpt::edge())?;
let addr = listener.local_addr()?;
thread::spawn(move || {
use std::net::TcpStream;
for _ in 0..(MAX_SOCKETS+1) {
let _ = TcpStream::connect(&addr).unwrap();
}
});
let mut events = Events::with_capacity(1024);
let mut buf = [0; 256];
loop {
poll.poll(&mut events, None)?;
for event in &events {
match event.token() {
LISTENER => {
loop {
match listener.accept() {
Ok((socket, _)) => {
if next_socket_index == MAX_SOCKETS {
return Ok(());
}
let token = Token(next_socket_index);
next_socket_index += 1;
poll.register(&socket,
token,
Ready::readable(),
PollOpt::edge())?;
sockets.insert(token, socket);
}
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
break;
}
e => panic!("err={:?}", e),
}
}
}
token => {
loop {
match sockets.get_mut(&token).unwrap().read(&mut buf) {
Ok(0) => {
sockets.remove(&token);
break;
}
Ok(_) => unreachable!(),
Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {
break;
}
e => panic!("err={:?}", e),
}
}
}
}
}
}
This method returns an ordering between self
and other
values if one exists. Read more
This method tests less than (for self
and other
) and is used by the <
operator. Read more
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
This method tests greater than (for self
and other
) and is used by the >
operator. Read more
This method tests greater than or equal to (for self
and other
) and is used by the >=
operator. Read more
This method returns an Ordering
between self
and other
. Read more
fn max(self, other: Self) -> Self | 1.21.0 [src] |
[−]
Compares and returns the maximum of two values. Read more
fn min(self, other: Self) -> Self | 1.21.0 [src] |
[−]
Compares and returns the minimum of two values. Read more
This method tests for self
and other
values to be equal, and is used by ==
. Read more
This method tests for !=
.
Performs copy-assignment from source
. Read more
Feeds this value into the given [Hasher
]. Read more
Feeds a slice of this type into the given [Hasher
]. Read more
Formats the value using the given formatter. Read more
Creates owned data from borrowed data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
[−]
[−]
type Error = <U as TryFrom<T>>::Error
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
[−]
🔬 This is a nightly-only experimental API. (try_from
)
[−]
[−]
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static