Expand description
kevy-sys — kevy’s network-boundary layer.
One of kevy’s three OS-boundary crates (alongside the publishable
kevy-uring and
kevy-madvise). This is the
server-internal piece — hand-curated to the exact subset of sockets and the
readiness poller (kqueue on macOS, epoll on Linux) that kevy’s server
needs. Every binding is declared by hand with unsafe extern "C"
(no libc crate, no third-party dep). On Linux these symbols resolve
through glibc, on macOS through libSystem — both already linked by
std, so we add zero dependencies.
The poller here is readiness-based. The completion-based io_uring
engine has moved to its own crate, kevy-uring; either can back
the reactor on top (kevy-net), which exposes only a byte-level
service contract.
Part of the kevy key–value server; not a generic OS-binding library.
§Safety
unsafe is confined to the private ffi module’s extern "C" declarations
and the thin wrappers that call them. The bindings match the platform libc
ABI (socklen_t = u32; struct sockaddr_in, kevent, and epoll_event
laid out per-OS/arch). All raw fds are owned by RAII types (Socket,
Poller, Waker) that close on drop, and errors are read via
std::io::Error::last_os_error(). The public API is safe.
§Example
use kevy_sys::{Poller, tcp_listen};
let listener = tcp_listen([127, 0, 0, 1], 6379, 1024)?;
listener.set_nonblocking()?;
let poller = Poller::new()?;
poller.add(listener.raw(), /* read */ true, /* write */ false)?;
let mut events = Vec::new();
poller.wait(&mut events, Some(1000))?; // block up to 1s
for ev in &events {
if ev.fd == listener.raw() && ev.readable {
let conn = listener.accept()?;
conn.set_nodelay()?;
// ... read/write `conn` ...
}
}Structs§
- Event
- A readiness notification for one file descriptor.
- Poller
- Socket
- An owned socket file descriptor. Closes itself on drop via our own
close. - Waker
- A self-pipe used to wake a
Pollerblocked inwaitfrom another thread. Registerread_fd()in the poller for read-readiness; callwake()from any thread to make the poll return; calldrain()when the read end fires.
Functions§
- tcp_
listen - Create a blocking IPv4 TCP listener bound to
ip:portwithSO_REUSEADDR. Passport == 0to let the OS assign an ephemeral port. - tcp_
listen_ reuseport - Like
tcp_listenbut also setsSO_REUSEPORT, so multiple listeners can share one port (one per thread-per-core shard). - waker
- Create a non-blocking self-pipe waker.