4.4. Network socket library

The NETWORK module implements networking facilities including HTTP client/server and low-level socket operations. It provides Server and Client classes with event-driven callbacks for handling connections, requests, and responses.

All functions and symbols are in “network” module, use require to get access to it.

require daslib/network

4.4.1. Handled structures

NetworkClient

The native TCP client behind the Client class: one outgoing connection, driven by client_connect, client_tick, client_send and client_close.

NetworkServer

Base implementation of the server.

4.4.2. Low level NetworkServer IO

make_server(class: void?; info: StructInfo const? ): bool

Creates a new Server instance.

Arguments:
  • class : void? implicit

  • info : StructInfo? implicit

server_init(server: NetworkServer?; port: int ): bool

Initializes the server to listen on the specified port.

Arguments:
server_is_connected(server: NetworkServer? ): bool

Returns true if the server has an active client connection.

Arguments:
server_is_open(server: NetworkServer? ): bool

Returns true if the server is listening on its bound port.

Arguments:
server_restore(server: NetworkServer?; class: void?; info: StructInfo const? )

Restores a server from an orphaned or interrupted state.

Arguments:
server_send(server: NetworkServer?; data: uint8?; size: int ): bool

Sends data from the server to the connected client.

Arguments:
  • server : NetworkServer? implicit

  • data : uint8? implicit

  • size : int

server_tick(server: NetworkServer? )

Processes pending network I/O; must be called periodically for the server to function.

Arguments:

4.4.3. Low level NetworkClient IO

client_close(client: NetworkClient? )

Closes the client’s socket. A later client_tick does nothing; client_connect opens a new one.

Arguments:
client_connect(client: NetworkClient?; host: string; port: int; timeout_ms: int ): bool

Opens a TCP connection to host (a host name, an IPv4 or IPv6 literal, or localhost) on port, trying each address the name resolves to in turn. Blocks until the peer accepts, refuses, or timeout_ms milliseconds pass across every address; on success the socket is non-blocking and onConnect has run. Returns false, with onError called, when the host does not resolve, every address refuses or times out, or the client is already connected.

Arguments:
  • client : NetworkClient? implicit

  • host : string implicit

  • port : int

  • timeout_ms : int

client_is_connected(client: NetworkClient? ): bool

Returns true while the client holds an open socket: after a successful client_connect and before client_close or the peer’s close was seen by client_tick.

Arguments:
client_send(client: NetworkClient?; data: uint8?; size: int ): bool

Sends size bytes from data to the peer, looping until every byte is out. Returns false, with the socket closed and onDisconnect called, when the peer went away.

Arguments:
  • client : NetworkClient? implicit

  • data : uint8? implicit

  • size : int

client_tick(client: NetworkClient? )

Reads everything the socket holds without blocking, handing each chunk to onData. A peer that closed the connection is reported through onDisconnect once, after which the client is no longer connected.

Arguments:
make_client(class: void?; info: StructInfo const? ): bool

Creates the native client behind a Client class instance: stores it in the instance’s _client field and wires the instance’s onConnect, onDisconnect, onData, onError and onLog methods as the socket’s callbacks. Called by Client.make_client_adapter; returns false when the class carries no _client field.

Arguments:
  • class : void? implicit

  • info : StructInfo? implicit

probe_local_port(host: string; port: int ): int

Binds a throwaway TCP socket to port on every interface, as a listener would, and returns the port it got: the same port when it is free, the one the system picked when port is 0, and -1 when the port is taken or host (an IPv4 address, or localhost) does not resolve. The socket is closed before returning, so the port is a report, not a reservation.

Arguments:
  • host : string implicit

  • port : int