https://github.com/tisonkun/polling
Portable interface to epoll, kqueue, event ports, and wepoll
https://github.com/tisonkun/polling
Last synced: 6 months ago
JSON representation
Portable interface to epoll, kqueue, event ports, and wepoll
- Host: GitHub
- URL: https://github.com/tisonkun/polling
- Owner: tisonkun
- License: apache-2.0
- Fork: true (smol-rs/polling)
- Created: 2023-08-21T15:06:16.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2023-10-06T02:57:27.000Z (almost 2 years ago)
- Last Synced: 2023-10-06T03:30:40.508Z (almost 2 years ago)
- Homepage:
- Size: 241 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# polling
[](
https://github.com/smol-rs/polling/actions)
[](
https://github.com/smol-rs/polling)
[](
https://crates.io/crates/polling)
[](
https://docs.rs/polling)Portable interface to epoll, kqueue, event ports, and IOCP.
Supported platforms:
- [epoll](https://en.wikipedia.org/wiki/Epoll): Linux, Android
- [kqueue](https://en.wikipedia.org/wiki/Kqueue): macOS, iOS, tvOS, watchOS, FreeBSD, NetBSD, OpenBSD,
DragonFly BSD
- [event ports](https://illumos.org/man/port_create): illumos, Solaris
- [poll](https://en.wikipedia.org/wiki/Poll_(Unix)): VxWorks, Fuchsia, other Unix systems
- [IOCP](https://learn.microsoft.com/en-us/windows/win32/fileio/i-o-completion-ports): Windows, Wine (version 7.13+)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.
## Examples
```rust,no_run
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))?;
}
}
}
```## License
Licensed under either of
* Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)at your option.
#### Contribution
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.