logos.std.io.net

Module std · package logos-std

Types

structAsyncTcpStream

struct AsyncTcpStream

Fiber-aware adapter around TcpStream. Its Read/Write impls route through async_recv/async_send so calls suspend the current fiber instead of blocking the whole thread. Use this variant when feeding a TcpStream into a generic reader/writer (BufReader<AsyncTcpStream>, HTTP servers, ...) inside a reactor.

Implements: Read Write

Fields

inner: TcpStream

The wrapped stream; owns the socket fd.

Methods

fn close(self: &mut AsyncTcpStream) -> void

Closes the underlying stream; idempotent.

fn flush(self: &mut AsyncTcpStream) -> i64
fn is_valid(self: &AsyncTcpStream) -> bool

Returns true if the underlying stream is valid.

fn read(self: &mut AsyncTcpStream, buf: *mut u8, len: i64) -> i64
fn write(self: &mut AsyncTcpStream, buf: *const u8, len: i64) -> i64

structSockAddr4

struct SockAddr4

IPv4 socket address matching struct sockaddr_in (16 bytes); port/addr in network byte order.

Fields

addr: u32
family: u16
port: u16
zero0: u32
zero1: u32

structTcpListener

struct TcpListener

A TCP socket listening for incoming IPv4 connections; check is_valid after construction.

Fields

fd: i32

Methods

fn accept(self: &TcpListener) -> TcpStream

Blocks until a client connects. Returns a TcpStream that is invalid on error.

fn async_accept(self: &TcpListener) -> TcpStream

Awaits an incoming connection via io_uring, suspending the calling fiber. Returns an invalid TcpStream on error (negative errno in the fd; -1 if no current Reactor or the SQ ring is full).

fn close(self: &mut TcpListener) -> void

Closes the listening socket and marks the listener invalid; idempotent.

fn is_valid(self: &TcpListener) -> bool

Returns true if the listener holds a valid (non-negative) socket fd.

structTcpStream

struct TcpStream

A connected TCP socket; its Read/Write impls use blocking recv/send syscalls.

Implements: Read Write

Fields

fd: i32

Methods

fn async_recv(self: &TcpStream, buf: *mut u8, len: i64) -> i64

Awaits up to len bytes from the socket; returns bytes read (>= 0) or negative errno. Suspends the current fiber until the CQE arrives. len is clamped to i32 max.

fn async_send(self: &TcpStream, buf: *const u8, len: i64) -> i64

Awaits send of len bytes, suspending the calling fiber; returns bytes written (>= 0) or negative errno. len is clamped to i32 max.

fn async_write_vectored(self: &TcpStream, iovs: *const IoSlice, nvecs: i32) -> i64

Awaits a vectored write; returns total bytes written (>= 0) or negative errno. The iovs array must remain valid for the lifetime of the parked fiber — the caller’s stack is the natural holder.

fn close(self: &mut TcpStream) -> void

Closes the socket and marks the stream invalid; idempotent.

fn flush(self: &mut TcpStream) -> i64
fn is_valid(self: &TcpStream) -> bool

Returns true if the stream holds a valid (non-negative) socket fd.

fn raw_fd(self: &TcpStream) -> i32

Returns the underlying fd for cross-thread handoff (serve_mt); ownership stays here.

fn read(self: &mut TcpStream, buf: *mut u8, len: i64) -> i64
fn write(self: &mut TcpStream, buf: *const u8, len: i64) -> i64
fn write_vectored(self: &mut TcpStream, iovs: *const IoSlice, nvecs: i32) -> i64

Performs a vectored synchronous write (writev). Returns total bytes written (>= 0) or negative on error. IoSlice layout matches struct iovec.

structUdpSocket

struct UdpSocket

An unconnected IPv4 UDP socket; check is_valid after construction.

Fields

fd: i32

Methods

fn close(self: &mut UdpSocket) -> void

Closes the socket and marks it invalid; idempotent.

fn is_valid(self: &UdpSocket) -> bool

Returns true if the socket holds a valid (non-negative) fd.

fn recv_from(self: &UdpSocket, buf: *mut u8, len: i64, from: &mut SockAddr4) -> i64

Blocks until a datagram arrives; fills buf and stores the sender address in from. Returns bytes received (>= 0) or negative on error.

fn send_to(self: &UdpSocket, buf: *const u8, len: i64, to: &SockAddr4) -> i64

Sends len bytes from buf as one datagram to to. Returns bytes sent (>= 0) or negative on error.

Functions

fnAF_INET

fn AF_INET() -> i32

Returns the AF_INET address-family constant (2).

fnasync_tcp_connect

fn async_tcp_connect(ip: u32, port: u16) -> TcpStream

Connects a fresh TCP socket to ip:port via io_uring, suspending the calling fiber. Returns an invalid TcpStream on error (negative errno in the fd; -1 if no current Reactor).

fnasync_tcp_stream

fn async_tcp_stream(s: TcpStream) -> AsyncTcpStream

Wraps s in an AsyncTcpStream, taking ownership.

fninaddr_any

fn inaddr_any() -> u32

Returns INADDR_ANY (0.0.0.0), for binding to all interfaces.

fnip4

fn ip4(a: u32, b: u32, c: u32, d: u32) -> u32

Builds a host-order IPv4 address from four octets, e.g. ip4(127,0,0,1).

fnSOCK_DGRAM

fn SOCK_DGRAM() -> i32

Returns the SOCK_DGRAM socket-type constant (2).

fnSOCK_STREAM

fn SOCK_STREAM() -> i32

Returns the SOCK_STREAM socket-type constant (1).

fnsockaddr4

fn sockaddr4(ip: u32, port: u16) -> SockAddr4

Creates a SockAddr4 from host-order ip and port, converting both to network byte order.

fntcp_connect

fn tcp_connect(ip: u32, port: u16) -> TcpStream

Connects a blocking TCP socket to ip:port. Returns an invalid stream on error.

fntcp_listen

fn tcp_listen(ip: u32, port: u16, backlog: i32) -> TcpListener

Binds a TCP socket to ip:port (with SO_REUSEADDR) and starts listening. Returns an invalid listener (is_valid() false) on error.

fntcp_listen_reuseport

fn tcp_listen_reuseport(ip: u32, port: u16, backlog: i32) -> TcpListener

Like tcp_listen but additionally sets SO_REUSEPORT, so multiple sockets bound to the same (ip, port) can be used concurrently — the kernel load-balances accept across them. Intended for per-worker listeners in multi-threaded servers (one listener per thread eliminates the cross-thread fd handoff cost).

fntcp_stream_from_fd

fn tcp_stream_from_fd(fd: i32) -> TcpStream

Builds a TcpStream from a raw fd (e.g. received via serve_mt handoff). The caller is responsible for fd being a valid connected socket; the stream assumes ownership and the fd is released via close().

fnudp_bind

fn udp_bind(ip: u32, port: u16) -> UdpSocket

Creates a UDP socket bound to ip:port. Returns an invalid socket (is_valid() false) on error.