Crates.io | polling |
lib.rs | polling |
version | 3.7.4 |
source | src |
created_at | 2020-08-06 13:17:41.554742 |
updated_at | 2024-11-07 15:28:40.971957 |
description | Portable interface to epoll, kqueue, event ports, and IOCP |
homepage | |
repository | https://github.com/smol-rs/polling |
max_upload_size | |
id | 273658 |
size | 258,226 |
Portable interface to epoll, kqueue, event ports, and IOCP.
Supported platforms:
Polling is done in oneshot mode, which means interest in I/O events needs to be reset after an event is delivered if we're interested in the next event of the same kind.
Only one thread can be waiting for I/O events at a time.
use polling::{Event, Poller};
use std::net::TcpListener;
// Create a TCP listener.
let socket = TcpListener::bind("127.0.0.1:8000")?;
socket.set_nonblocking(true)?;
let key = 7; // Arbitrary key identifying the socket.
// Create a poller and register interest in readability on the socket.
let poller = Poller::new()?;
poller.add(&socket, Event::readable(key))?;
// The event loop.
let mut events = Vec::new();
loop {
// Wait for at least one I/O event.
events.clear();
poller.wait(&mut events, None)?;
for ev in &events {
if ev.key == key {
// Perform a non-blocking accept operation.
socket.accept()?;
// Set interest in the next readability event.
poller.modify(&socket, Event::readable(key))?;
}
}
}
Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.