karmaio 0.1.0

A modern, fast, share-nothing asynchronous runtime for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use std::net::SocketAddr;
use std::{io::Result, os::raw::c_int};

#[cfg(unix)]
use std::os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, RawFd};

#[cfg(windows)]
use std::os::windows::io::{AsRawSocket, AsSocket, BorrowedSocket, FromRawSocket, RawSocket};

use crate::buf::{BufResult, IoBuf, IoBufMut, IoVectoredBuf, IoVectoredBufMut};
use crate::driver::helpers::attached_handle::AttachedHandle;
#[cfg(target_os = "linux")]
use crate::driver::ops::MultiOp;
use crate::driver::ops::Op;
#[cfg(target_os = "linux")]
use crate::driver::ops::accept_multi::AcceptMulti;
#[cfg(target_os = "linux")]
use crate::io::Stream;

// This is an internal wrapper around socket operations for the runtime.
// This wrapper abstracts and handles all the driver operations and os compatiblity,
// presenting a clean, reusable api for the top level socket modules.
//
// The owned resource is a `socket2::Socket` so control-plane APIs (listen, nodelay,
// etc.) go through `AttachedHandle`/`Deref` without reconverting through raw FDs.
#[derive(Clone, Debug)]
pub(crate) struct Socket {
    pub(crate) handle: AttachedHandle<socket2::Socket>,
}

/// Configure a socket for async use on the current platform.
///
/// Sets non-blocking mode, and configures close-on-exec on kqueue Unix targets.
/// Apple targets additionally enable `NOSIGPIPE`.
/// Used by create, bind, and accept paths so flags stay consistent.
fn configure_async_socket(socket: &socket2::Socket) -> Result<()> {
    socket.set_nonblocking(true)?;
    #[cfg(any(
        target_os = "macos",
        target_os = "freebsd",
        target_os = "netbsd",
        target_os = "openbsd",
        target_os = "dragonfly"
    ))]
    {
        socket.set_cloexec(true)?;
    }
    #[cfg(target_vendor = "apple")]
    {
        // Avoid SIGPIPE killing the process when writing to a closed socket.
        // macOS can reject SO_NOSIGPIPE on Unix domain sockets (and accepted
        // sockets inherit it from the listener anyway), so skip unix sockets.
        let is_unix = socket
            .local_addr()
            .map(|a| i32::from(a.family()) == libc::AF_UNIX)
            .unwrap_or(false);
        if !is_unix {
            socket.set_nosigpipe(true)?;
        }
    }
    Ok(())
}

impl Socket {
    fn attach(socket: socket2::Socket) -> Result<AttachedHandle<socket2::Socket>> {
        #[cfg(windows)]
        {
            AttachedHandle::new_socket(socket)
        }

        #[cfg(unix)]
        {
            AttachedHandle::new(socket)
        }
    }

    pub(crate) fn set_async_flags(&self) -> Result<()> {
        configure_async_socket(&self.handle)
    }

    /// Creates a new network socket (TCP/UDP)
    pub(crate) fn new(socket_addr: SocketAddr, socket_type: socket2::Type) -> Result<Self> {
        let socket = socket2::Socket::new(socket2::Domain::for_address(socket_addr), socket_type, None)?;
        configure_async_socket(&socket)?;

        Ok(Self {
            handle: Self::attach(socket)?,
        })
    }

    /// Creates a new UNIX socket
    #[cfg(unix)]
    pub(crate) fn new_unix(socket_type: c_int) -> Result<Self> {
        let socket = socket2::Socket::new(socket2::Domain::UNIX, socket_type.into(), None)?;
        configure_async_socket(&socket)?;

        Ok(Self {
            handle: Self::attach(socket)?,
        })
    }

    /// Binds a socket to the specified address.
    pub(crate) fn bind(socket_addr: SocketAddr, socket_type: socket2::Type) -> Result<Self> {
        Self::bind_internal(
            socket_addr.into(),
            socket2::Domain::for_address(socket_addr),
            socket_type,
        )
    }

    /// Binds a Unix domain socket to the specified path.
    #[cfg(unix)]
    pub(crate) fn bind_unix<P: AsRef<std::path::Path>>(path: P, socket_type: c_int) -> Result<Self> {
        let addr = socket2::SockAddr::unix(path.as_ref())?;
        Self::bind_internal(addr, socket2::Domain::UNIX, socket_type.into())
    }

    fn bind_internal(
        socket_addr: socket2::SockAddr,
        domain: socket2::Domain,
        socket_type: socket2::Type,
    ) -> Result<Socket> {
        let socket = socket2::Socket::new(domain, socket_type, None)?;

        socket.set_reuse_address(true)?;
        #[cfg(unix)]
        socket.set_reuse_port(true)?;

        configure_async_socket(&socket)?;

        socket.bind(&socket_addr)?;

        Ok(Self {
            handle: Self::attach(socket)?,
        })
    }

    /// Wrap an already-created socket and attach it to the current runtime.
    pub(crate) fn from_socket(socket: socket2::Socket) -> Result<Self> {
        Ok(Self {
            handle: Self::attach(socket)?,
        })
    }

    // ================================
    //  Connection Operations
    // ================================

    /// Initiates a connection to the specified address.
    pub(crate) async fn connect(&self, socket_addr: socket2::SockAddr) -> Result<()> {
        let op = Op::connect(&self.handle, socket_addr)?;
        op.await
    }

    /// Accepts a new incoming connection.
    pub(crate) async fn accept(&self) -> Result<(Self, Option<SocketAddr>)> {
        let op = Op::accept(&self.handle)?;
        op.await
    }

    /// Stream of incoming connections (Linux io_uring multishot accept).
    ///
    /// Yields the same item shape as [`accept`](Self::accept): an owned
    /// [`Socket`] and optional peer IP address. Peer addresses are resolved
    /// with `getpeername` after each successful accept.
    ///
    /// Uses multishot accept under the hood (Linux 6.12+). The request is
    /// submitted when the stream is first polled, which lets a cancellation
    /// token wrap it first. Dropping the stream cancels a submitted request.
    /// The multishot SQE is **not** re-armed after it terminates; call
    /// [`incoming`](Self::incoming) again to start a new one.
    #[cfg(target_os = "linux")]
    pub(crate) fn incoming(&self) -> Result<Incoming> {
        Ok(Incoming {
            inner: MultiOp::accept_multi(&self.handle)?,
        })
    }

    // ================================
    //  Connection Control
    // ================================

    /// Starts listening for incoming connections.
    pub(crate) fn listen(&self, backlog: c_int) -> Result<()> {
        self.handle.listen(backlog)
    }

    /// Shuts down the read, write, or both halves of this connection.
    ///
    /// This function will cause all pending and future I/O on the specified portions to return immediately with an appropriate value.
    pub fn shutdown(&self, how: std::net::Shutdown) -> Result<()> {
        self.handle.shutdown(how)
    }

    /// Closes the socket, waiting for in-flight operations to complete.
    ///
    /// Prefer this over dropping when close errors must be observed. Drop still
    /// closes the handle synchronously when the last reference is dropped.
    pub(crate) async fn close(self) -> Result<()> {
        self.handle.into_inner().close().await
    }

    /// Set the value of the `TCP_NODELAY` option on this socket.
    ///
    /// If set, this option disables the Nagle algorithm.
    /// This means that segments are always sent as soon as possible, even if there is only a small amount of data.
    /// When not set, data is buffered until there is a sufficient amount to send out, thereby avoiding the frequent sending of small packets.
    pub fn set_nodelay(&self, nodelay: bool) -> Result<()> {
        self.handle.set_tcp_nodelay(nodelay)
    }

    // ================================
    //  Read Operations
    // ================================

    /// Reads a message from the socket from the connected address
    pub(crate) async fn recv<B: IoBufMut>(&self, buf: B) -> BufResult<usize, B> {
        let op = match Op::recv(&self.handle, buf) {
            Ok(op) => op,
            Err((error, buf)) => return BufResult(Err(error), buf),
        };
        op.await
    }

    /// Receive into a runtime pool buffer (Linux only).
    ///
    /// See [`crate::buf::PooledBuf`] for lease ownership and pool starvation
    /// risks. `len == 0` uses the full pool buffer size.
    #[cfg(target_os = "linux")]
    pub(crate) async fn recv_managed(&self, len: usize) -> Result<Option<crate::buf::PooledBuf>> {
        let op = Op::recv_managed(&self.handle, len)?;
        op.await
    }

    /// Multishot receive stream into runtime pool buffers (Linux only).
    ///
    /// See [`crate::buf::PooledBuf`] for lease ownership and pool starvation
    /// risks. No auto-rearm: when the stream ends (including on `ENOBUFS`),
    /// call again after recycling leases.
    #[cfg(target_os = "linux")]
    pub(crate) fn recv_multi(&self) -> Result<MultiOp<crate::driver::ops::recv_multi::RecvMulti>> {
        MultiOp::recv_multi(&self.handle)
    }

    /// Managed UDP receive with truncation and message metadata (Linux only).
    #[cfg(target_os = "linux")]
    pub(crate) async fn recv_datagram_managed(
        &self,
        len: usize,
        capture_peer: bool,
    ) -> Result<crate::net::udp::RecvDatagram> {
        let op = Op::recv_datagram_managed(&self.handle, len, capture_peer)?;
        op.await
    }

    /// Multishot UDP receive with truncation and message metadata (Linux only).
    #[cfg(target_os = "linux")]
    pub(crate) fn recv_datagram_multi(
        &self,
        capture_peer: bool,
    ) -> Result<MultiOp<crate::driver::ops::recv_from_multi::RecvFromMulti>> {
        MultiOp::recv_datagram_multi(&self.handle, capture_peer)
    }

    /// Managed oneshot recv_from into a pool buffer (Linux only).
    #[cfg(target_os = "linux")]
    pub(crate) async fn recv_from_managed(&self, len: usize) -> Result<crate::net::udp::RecvDatagram> {
        self.recv_datagram_managed(len, true).await
    }

    /// Multishot recv_from stream into pool buffers (Linux only).
    #[cfg(target_os = "linux")]
    pub(crate) fn recv_from_multi(&self) -> Result<MultiOp<crate::driver::ops::recv_from_multi::RecvFromMulti>> {
        self.recv_datagram_multi(true)
    }

    /// Reads a message from the socket along with the receiver address
    pub(crate) async fn recv_from<B: IoBufMut>(&self, buf: B) -> BufResult<(usize, SocketAddr), B> {
        let op = match Op::recv_from(&self.handle, buf) {
            Ok(op) => op,
            Err((error, buf)) => return BufResult(Err(error), buf),
        };
        op.await
    }

    /// Performs a scattered read into the supplied buffers along with the receiver address
    pub(crate) async fn recvmsg<V: IoVectoredBufMut>(&self, buf: V) -> BufResult<(usize, SocketAddr), V> {
        let op = match Op::recvmsg(&self.handle, buf) {
            Ok(op) => op,
            Err((error, buf)) => return BufResult(Err(error), buf),
        };
        op.await
    }

    // ================================
    //  Write Operations
    // ================================

    /// Writes the buffer on the connected socket
    pub(crate) async fn send<B: IoBuf>(&self, buf: B) -> BufResult<usize, B> {
        let op = match Op::send(&self.handle, buf) {
            Ok(op) => op,
            Err((error, buf)) => return BufResult(Err(error), buf),
        };
        op.await
    }

    /// Writes the buffer to the specified address on the socket
    pub(crate) async fn send_to<B: IoBuf>(&self, buf: B, socket_addr: SocketAddr) -> BufResult<usize, B> {
        let op = match Op::send_to(&self.handle, buf, socket_addr) {
            Ok(op) => op,
            Err((error, buf)) => return BufResult(Err(error), buf),
        };
        op.await
    }

    /// Performes a gather write on the socket with data from the specified buffers
    /// Needs an address if the socket is not connected to an address
    pub(crate) async fn sendmsg<V: IoVectoredBuf, C: IoBuf>(
        &self,
        io_slices: V,
        socket_addr: Option<SocketAddr>,
        control: Option<C>,
    ) -> BufResult<(usize, Option<C>), V> {
        let op = match Op::sendmsg(&self.handle, io_slices, control, socket_addr) {
            Ok(op) => op,
            Err((error, io_slices)) => return BufResult(Err(error), io_slices),
        };
        op.await
    }
}

#[cfg(unix)]
impl AsRawFd for Socket {
    fn as_raw_fd(&self) -> RawFd {
        self.handle.raw_fd()
    }
}

#[cfg(unix)]
impl AsFd for Socket {
    fn as_fd(&self) -> BorrowedFd<'_> {
        self.handle.as_fd()
    }
}

#[cfg(windows)]
impl AsSocket for Socket {
    fn as_socket(&self) -> BorrowedSocket<'_> {
        // Safety: `self.handle.raw_socket()` returns a valid, open socket
        // that is owned by this `Socket` and will remain valid for the lifetime of `&self`.
        unsafe { BorrowedSocket::borrow_raw(self.handle.raw_socket()) }
    }
}

#[cfg(windows)]
impl AsRawSocket for Socket {
    fn as_raw_socket(&self) -> RawSocket {
        self.handle.raw_socket()
    }
}

impl From<AttachedHandle<socket2::Socket>> for Socket {
    fn from(value: AttachedHandle<socket2::Socket>) -> Self {
        Self { handle: value }
    }
}

/// Stream of incoming connections owned by the shared [`Socket`] helper.
///
/// Each item matches oneshot [`Socket::accept`]: `(Socket, Option<SocketAddr>)`.
/// Public listeners map this into their domain types (`TcpStream`, `UnixStream`).
///
/// Backed by io_uring multishot accept on Linux 6.12+. Dropping the stream
/// cancels the in-flight request. The multishot SQE is not re-armed after it
/// ends; construct a new stream via [`Socket::incoming`].
#[cfg(target_os = "linux")]
pub(crate) struct Incoming {
    inner: MultiOp<AcceptMulti>,
}

#[cfg(target_os = "linux")]
impl Stream for Incoming {
    type Item = Result<(Socket, Option<SocketAddr>)>;

    async fn next(&mut self) -> Option<Self::Item> {
        let accepted = self.inner.next().await?;
        Some(match accepted {
            Ok(socket) => match socket.handle.peer_addr() {
                Ok(addr) => Ok((socket, addr.as_socket())),
                Err(err) => Err(err),
            },
            Err(err) => Err(err),
        })
    }
}

#[cfg(unix)]
impl FromRawFd for Socket {
    unsafe fn from_raw_fd(fd: RawFd) -> Self {
        // Safety: caller guarantees `fd` is an open socket; ownership transfers here.
        Self {
            handle: unsafe { AttachedHandle::new_unchecked(socket2::Socket::from_raw_fd(fd)) },
        }
    }
}

#[cfg(windows)]
impl FromRawSocket for Socket {
    unsafe fn from_raw_socket(socket: RawSocket) -> Self {
        // Safety: caller guarantees `socket` is open; ownership transfers here.
        Self {
            handle: unsafe { AttachedHandle::new_unchecked(socket2::Socket::from_raw_socket(socket)) },
        }
    }
}