Antra

Standardbibliothek

Antra::Socket

import Antra::Socket

Small shared socket operations for Antra.

Antra::Socket is the transport-agnostic companion to packages such as Antra::Socket::TCP. It operates on opaque #Socket<...> runtime values that were created by a transport-specific entrypoint.

In the current early phase the surface is intentionally small:

  • send(...) / recv(...) for text-oriented exchange
  • close(...) for explicit lifecycle control
  • local_addr(...) for reading the bound local endpoint
  • bang variants for scripts that prefer direct throws over explicit Result handling

Current phase-1 constraints:

  • recv(..., [mode: :line]) is the main convenience read form
  • mode: :binary is available as the raw fallback for send(...) / recv(...)
  • recv(..., [mode: :binary, length: n]) reads exactly n bytes or returns an error
  • local addresses are currently exposed as %SocketAddr{family: :inet, host, port} for the implemented TCP path
  • broader address families, resolver helpers and peer-address helpers are still a later phase

Example:

import Antra::Socket as Socket
import Antra::Socket::TCP as TCP

val %Ok[sock] = TCP.connect("127.0.0.1", 4040)
val %Ok[_] = Socket.send(sock, "ping", [mode: :line])
val %Ok[%Some[line]] = Socket.recv(sock, [mode: :line])
println(line)
val %Ok[_] = Socket.close(sock)

Funktionen#

fn close(socket)

Closes an open socket handle.

Returns %Ok[undef] on success or %Err[%Error{...}] on failure.

fn local_addr(socket)

Reads the local bound endpoint of a socket as %SocketAddr{...}.

Returns %Ok[%SocketAddr{family: :inet, host: string, port: int}] for the current TCP phase-1 implementation, or %Err[%Error{...}] on failure.

fn recv(socket)
fn recv(socket, opts)

Receives data from a connected socket.

Returns %Ok[%Some[text]] for received data, %Ok[%None[]] for clean EOF, or %Err[%Error{...}] on failure.

fn send(socket, data)
fn send(socket, data, opts)

Sends text data over a connected socket.

Returns %Ok[Number] where the integer is the number of bytes handed to the host socket layer.

Quelle dieser Seite