Skip to content

std.net

Blocking TCP over the C library. A Socket is linear, so a descriptor cannot be dropped by forgetting it; the failure paths below close the raw descriptor before a Socket exists, which is why they still read errno first - close() would overwrite it. Cost: one syscall per call except send and send_all, which loop until the whole view is gone. Everything here blocks; an I/O ring (IoRing) keeps accepts and receives in flight without a thread.

pub linear struct Socket { fd:i32; }

// effects: alloc, ffi:__errno_location, ffi:bind, ffi:close, ffi:listen, ffi:setsockopt, ffi:socket, ffi_precondition,
// free, io, local_read, local_write, mmio, read:ip, trap, zero_init
pub fn listen_on(ip:ro<u8>[4]@host, port:u16, backlog:i32) -> std.core.Result[std.net.Socket, std.io.IoError]

// effects: alloc, ffi:__errno_location, ffi:accept, ffi_precondition, free, io, local_write, mmio, read:s, trap,
// zero_init
pub fn accept(s:ro<std.net.Socket>) -> std.core.Result[std.net.Socket, std.io.IoError]

// effects: alloc, ffi:__errno_location, ffi:close, ffi:connect, ffi:socket, ffi_precondition, free, io, local_read,
// local_write, mmio, read:ip, trap, zero_init
pub fn connect_to(ip:ro<u8>[4]@host, port:u16) -> std.core.Result[std.net.Socket, std.io.IoError]

// effects: diverge, ffi:__errno_location, ffi:send, ffi_precondition, io, mmio, read:data, read:s, trap
pub fn send(s:ro<std.net.Socket>, n:usize, data:ro<u8>[n]@host) -> std.core.Result[usize, std.io.IoError]

// Loops until the whole view is gone. On a raw descriptor, for one an I/O ring's accept answered.
// effects: diverge, ffi:__errno_location, ffi:send, ffi_precondition, io, mmio, read:data, trap
pub fn send_all(fd:i32, n:usize, data:ro<u8>[n]@host) -> std.core.Result[usize, std.io.IoError]

// One recv: a count of 0 means the peer closed its end, never an error.
// effects: ffi:__errno_location, ffi:recv, ffi_precondition, io, mmio, read:s, trap, write:into
pub fn recv(s:ro<std.net.Socket>, n:usize, into:rw<u8>[n]@host) -> std.core.Result[usize, std.io.IoError]

pub fn close(s:std.net.Socket)  // effects: ffi:close, io

A generic function's effects are what it may do for any arguments within its bounds, besides what their own trait members do.