Common networking utilities for powpow. This module provides platform-agnostic socket types, error handling, and helper functions for setting socket options and resolving addresses. It abstracts away differences between Windows and POSIX APIs, allowing powpow to use a consistent interface for network operations across platforms.
Consts
DefaultBufSize = 4096
- Default read buffer size per connection.
O_RDONLY = 0
SEEK_CUR = 1
SEEK_END = 2
SEEK_SET = 0
SendFileChunkSize = 65536
UNIX_PATH_MAX = 107
Procs
proc closeFile(fd: int) {.inline, ...raises: [], tags: [], forbids: [].}
- Close a file descriptor.
proc getFileSize(fd: int): int64 {....raises: [], tags: [], forbids: [].}
- Get file size from an open fd. Returns -1 on error.
proc getSockLen(addrBuf: ptr Sockaddr_storage): SockLen {....raises: [], tags: [], forbids: [].}
- Return the correct socklen for the address family.
proc initNet() {....raises: [], tags: [], forbids: [].}
- Initialize networking. Safe to call multiple times.
proc lastSocketError(): cint {.inline, ...raises: [], tags: [], forbids: [].}
- Get the last socket error (platform-agnostic).
proc openFileRead(path: string): int {....raises: [], tags: [], forbids: [].}
- Open a file for reading. Returns fd or -1 on error.
proc readFile(fd: int; buf: ptr UncheckedArray[byte]; len: int): int64 {. ...raises: [], tags: [], forbids: [].}
- Read up to len bytes from a file. Returns bytes read, 0 on EOF, -1 on error.
proc resolveAddr(address: string; port: int; sockType = SOCK_STREAM; protocol = 0): Sockaddr_storage {....raises: [NetError], tags: [], forbids: [].}
- Resolve address:port into a Sockaddr_storage ready for bind/connect. Works for both IPv4 and IPv6.
proc seekFile(fd: int; offset: int64): int64 {....raises: [], tags: [], forbids: [].}
- Seek to an absolute position in a file. Returns new position or -1 on error.
proc sendFileChunk(sockFd: SocketHandle; fileFd: int; fileOff: var int64; remaining: var int64): int64 {....raises: [], tags: [], forbids: [].}
-
Send file data to a socket using zero-copy when available. Updates fileOff and remaining. Returns bytes sent, 0 on EAGAIN (caller should retry when socket is writable),-1 on hard error.
proc setNonBlocking(fd: SocketHandle) {....raises: [NetError], tags: [], forbids: [].}
- Put a socket into non-blocking mode using a single ioctl syscall.
proc setReuseAddr(fd: SocketHandle) {....raises: [NetError], tags: [], forbids: [].}
- Enable SO_REUSEADDR on a socket.
proc setReusePort(fd: SocketHandle) {....raises: [NetError], tags: [], forbids: [].}
- Enable SO_REUSEPORT on a socket (macOS/Linux). No-op on Windows.
proc setTcpCork(fd: SocketHandle; enable: bool) {....raises: [], tags: [], forbids: [].}
- Enable or disable TCP corking (TCP_CORK on Linux, TCP_NOPUSH on macOS/BSD). No-op on Windows and other unsupported platforms.
proc setTcpNoDelay(fd: SocketHandle) {....raises: [], tags: [], forbids: [].}
- Disable Nagle's algorithm for lower latency. Silently ignores errors (e.g. on AF_UNIX sockets where TCP_NODELAY is not applicable).
proc sockClose(fd: SocketHandle) {.inline, ...raises: [], tags: [], forbids: [].}
- Close a socket.
proc sockInProgress(): bool {.inline, ...raises: [], tags: [], forbids: [].}
proc sockInterrupted(): bool {.inline, ...raises: [], tags: [], forbids: [].}
proc sockRecv(fd: SocketHandle; buf: pointer; bufLen: int): int {.inline, ...raises: [], tags: [], forbids: [].}
- Read from a socket. Returns bytes read, 0 on EOF, negative on error.
proc sockSend(fd: SocketHandle; buf: pointer; len: int): int {.inline, ...raises: [], tags: [], forbids: [].}
- Write to a socket. Returns bytes written, negative on error.
proc sockShutdown(fd: SocketHandle; how: cint) {.inline, ...raises: [], tags: [], forbids: [].}
- Shut down part of a full-duplex connection.
proc sockWouldBlock(): bool {.inline, ...raises: [], tags: [], forbids: [].}
proc sockWritev(fd: SocketHandle; iov: ptr IOVec; iovcnt: int): int {.inline, ...raises: [], tags: [], forbids: [].}
- Scatter-gather write. On Windows, concatenates buffers and calls send.