Module: net.udp

Source: ./net/udp.reef


Overview

net.udp - UDP datagram socket operations

Provides UDP datagram functionality with IPv4/IPv6 support, broadcast, and connectionless send/receive operations.

The 7 fallible ops (create/create_ipv6/bind, sendto/sendto_bytes, recv/recv_bytes) return result.Result[T, error.Error]. Failures are classified via net.socket's net_err() (errno -> ErrorKind, with a strerror() message) -- same pattern as net.tcp.

udp_recv treats a zero-length datagram as Ok(""), NOT an error: the runtime returns 0 for that case, which is a normal (if unusual) UDP condition, not a failure. Only a negative return from the runtime becomes Err(net_err()).

udp_recv_bytes keeps its out-param buffer (byte arrays aren't returned by value): Ok(n) for n >= 0, Err(net_err()) for n < 0.

Socket management (close/set_broadcast) and peer/local accessors are unchanged -- they don't fail in ways worth modeling as Result.


Functions

fn udp_create(): result.Result[int, error.Error]

Create IPv4 UDP socket Ok(socket fd) on success, Err(socket.net_err()) on failure.

fn udp_create_ipv6(): result.Result[int, error.Error]

Create IPv6 UDP socket (also accepts IPv4) Ok(socket fd) on success, Err(socket.net_err()) on failure.

fn udp_bind(sockfd: int, port: int): result.Result[int, error.Error]

Bind socket to a port Ok(0) on success, Err(socket.net_err()) on failure.

fn udp_sendto(sockfd: int, host: string, port: int, data: string): result.Result[int, error.Error]

Send string datagram Ok(number of bytes sent) on success, Err(socket.net_err()) on failure.

fn udp_sendto_bytes(sockfd: int, host: string, port: int, data: [uint8], len: int): result.Result[int, error.Error]

Send byte array datagram Ok(number of bytes sent) on success, Err(socket.net_err()) on failure.

fn udp_recv(sockfd: int, max_len: int): result.Result[string, error.Error]

Receive datagram as string Call udp_peer_addr() and udp_peer_port() to get sender info. Ok(received string, GC-copied; may be shorter than max_len) on success; Ok("") on a zero-length datagram (runtime returns 0 -- NOT an error); Err(socket.net_err()) on failure (runtime returns < 0).

fn udp_recv_bytes(sockfd: int, buffer: [uint8], max_len: int): result.Result[int, error.Error]

Receive datagram into byte array (out-param buffer kept -- byte arrays aren't returned by value) Ok(number of bytes received) on success (0 = zero-length datagram, still Ok); Err(socket.net_err()) on failure (runtime returns < 0).

fn udp_close(sockfd: int): int

Close socket

fn udp_set_broadcast(sockfd: int, enable: bool): int

Enable/disable broadcast

fn udp_peer_addr(): string

Get sender address (call after recv)

fn udp_peer_port(): int

Get sender port (call after recv)

fn udp_local_port(sockfd: int): int

Get local port of bound socket


Generated by reefc doc