|
Banjo API 1.0.0-rc.2
Low-level C99 game development API
|
Macros | |
| #define | BJ_ADDR_STRLEN 46 |
| #define | BJ_NET_BACKLOG_DEFAULT 0 |
| #define | BJ_NET_BACKLOG_MAX (-1) |
Enumerations | |
| enum | bj_net_hints { BJ_NET_NOHINT = 0x0 , BJ_RESOLVE_IPV4 = 0x01 , BJ_RESOLVE_IPV6 = 0x02 , BJ_EXCLUSIVE_BIND = 0x04 , BJ_TCP_DELAY_ACK = 0x08 , BJ_NONBLOCKING = 0x10 , BJ_BROADCAST = 0x20 } |
| enum | bj_net_shutdown { BJ_SHUTDOWN_RECEIVE = 0x01 , BJ_SHUTDOWN_SEND = 0x02 , BJ_SHUTDOWN_BOTH = 0x03 } |
Functions | |
| struct bj_tcp_listener * | bj_listen_tcp (const char *host, uint16_t port, uint16_t hint, int backlog, struct bj_error **error) |
| struct bj_tcp_stream * | bj_connect_tcp (const char *host, uint16_t port, uint16_t hint, uint32_t ms_timeout, struct bj_error **error) |
| struct bj_tcp_stream * | bj_connect_tcp_to (struct bj_address *addr, uint16_t hints, uint32_t ms_timeout, struct bj_error **error) |
| struct bj_tcp_stream * | bj_accept_tcp (struct bj_tcp_listener *listener, uint32_t ms_timeout, struct bj_error **error) |
| int | bj_tcp_recv (struct bj_tcp_stream *stream, void *buf, size_t len) |
| int | bj_tcp_peek (struct bj_tcp_stream *stream, void *buf, size_t len) |
| int | bj_tcp_send (struct bj_tcp_stream *stream, const void *buf, size_t len) |
| void | bj_tcp_shutdown (struct bj_tcp_stream *stream, int mode) |
| bj_bool | bj_set_tcp_keepalive (struct bj_tcp_stream *stream, bj_bool enable) |
| bj_bool | bj_get_tcp_keepalive (struct bj_tcp_stream *stream) |
| bj_bool | bj_tcp_wait (struct bj_tcp_stream *stream, uint32_t ms_timeout) |
| bj_bool | bj_set_tcp_recv_timeout (struct bj_tcp_stream *stream, uint32_t ms_timeout) |
| uint32_t | bj_get_tcp_recv_timeout (struct bj_tcp_stream *stream) |
| bj_bool | bj_set_tcp_send_timeout (struct bj_tcp_stream *stream, uint32_t ms_timeout) |
| uint32_t | bj_get_tcp_send_timeout (struct bj_tcp_stream *stream) |
| void | bj_close_tcp_stream (struct bj_tcp_stream *stream) |
| void | bj_close_tcp_listener (struct bj_tcp_listener *listener) |
| size_t | bj_tcp_peer_address (const struct bj_tcp_stream *stream, char *buf, size_t size) |
| size_t | bj_tcp_local_address (const struct bj_tcp_stream *stream, char *buf, size_t size) |
| uint16_t | bj_tcp_listener_port (const struct bj_tcp_listener *listener) |
| int | bj_tcp_available (struct bj_tcp_stream *stream) |
Address Resolution Functions | |
| struct bj_address * | bj_resolve_address (const char *host, uint16_t port, uint16_t hints, struct bj_error **error) |
| size_t | bj_address_string (const struct bj_address *addr, char *buf, size_t size) |
| uint16_t | bj_address_port (const struct bj_address *addr) |
| bj_bool | bj_address_equals (const struct bj_address *a, const struct bj_address *b) |
| size_t | bj_address_count (const struct bj_address *addr) |
| bj_bool | bj_address_next (struct bj_address *addr) |
| void | bj_address_reset (struct bj_address *addr) |
| void | bj_free_address (struct bj_address *addr) |
UDP Functions | |
| struct bj_udp * | bj_open_udp (const char *host, uint16_t port, uint16_t hints, struct bj_error **error) |
| void | bj_close_udp (struct bj_udp *udp) |
| int | bj_udp_send (struct bj_udp *udp, const char *host, uint16_t port, const void *data, size_t len, struct bj_error **error) |
| int | bj_udp_send_to (struct bj_udp *udp, const struct bj_address *addr, const void *data, size_t len, struct bj_error **error) |
| struct bj_datagram * | bj_udp_recv (struct bj_udp *udp, struct bj_error **error) |
| bj_bool | bj_udp_available (struct bj_udp *udp) |
| bj_bool | bj_udp_wait (struct bj_udp *udp, uint32_t ms_timeout) |
| uint16_t | bj_udp_local_port (const struct bj_udp *udp) |
| size_t | bj_udp_local_address (const struct bj_udp *udp, char *buf, size_t size) |
Datagram Accessors | |
| size_t | bj_datagram_addr (const struct bj_datagram *dgram, char *buf, size_t size) |
| uint16_t | bj_datagram_port (const struct bj_datagram *dgram) |
| const void * | bj_datagram_data (const struct bj_datagram *dgram) |
| size_t | bj_datagram_len (const struct bj_datagram *dgram) |
| void | bj_close_datagram (struct bj_datagram *dgram) |
Network Simulation | |
| void | bj_tcp_simulate_loss (struct bj_tcp_stream *stream, int percent) |
| void | bj_udp_simulate_loss (struct bj_udp *udp, int percent) |
Talk to other programs over a network: TCP for streams, UDP for datagrams.
This subsystem lets your program send and receive data over a network. The network plumbing is the same on every platform Banjo supports (the same code runs on Linux, macOS, and Windows), but you should know what kind of communication you want, because the choice changes the API quite a lot.
TCP is what most programs mean by "a network connection." One side listens on a port; the other side connects to that port. Once the handshake completes, both sides exchange bytes through a stream: the OS guarantees that whatever you send arrives in order, with no duplicates, no losses, and nothing extra inserted. Use TCP for anything that's fundamentally "send a message, wait for a reply" or "stream a chunk of data": HTTP, chat protocols, file transfer.
UDP is the opposite trade-off. There's no connection: each send puts one packet on the wire and each recv returns one packet. Packets can arrive out of order, can be duplicated, or not arrive at all. The OS doesn't try to fix any of that; it's faster and lower-latency, and you build whatever reliability you need on top yourself. Use UDP for real-time games, voice and video, or LAN broadcast / discovery (one message to "anyone on the network who's listening").
See the net_tcp_*.c and net_udp_*.c examples for runnable versions of both styles, plus broadcast and non-blocking patterns.
Before you can connect to "example.com", the name has to be turned into an actual IP address. The OS handles that for you, but you can hint which kind of address you want with the resolve flags:
| #define BJ_ADDR_STRLEN 46 |
| #define BJ_NET_BACKLOG_DEFAULT 0 |
Default backlog value for bj_listen_tcp.
Uses a reasonable implementation-defined default.
| #define BJ_NET_BACKLOG_MAX (-1) |
Maximum backlog value for bj_listen_tcp.
Uses the system's maximum allowed backlog (SOMAXCONN).
| enum bj_net_hints |
Hint flags for network operations.
These flags can be combined with bitwise OR to configure socket behaviour.
| enum bj_net_shutdown |
Shutdown mode for bj_tcp_shutdown.
| Enumerator | |
|---|---|
| BJ_SHUTDOWN_RECEIVE | Shutdown receive operations. |
| BJ_SHUTDOWN_SEND | Shutdown send operations. |
| BJ_SHUTDOWN_BOTH | Shutdown both send and receive. |
| struct bj_tcp_stream * bj_accept_tcp | ( | struct bj_tcp_listener * | listener, |
| uint32_t | ms_timeout, | ||
| struct bj_error ** | error ) |
Accepts an incoming connection on a TCP listener with timeout.
Waits for a client connection up to the specified timeout.
| listener | The listener to accept connections on. |
| ms_timeout | Timeout in milliseconds. 0 = non-blocking, UINT32_MAX = infinite. |
| error | Optional error output. Set to BJ_ERROR_NETWORK_TIMEOUT on timeout. |
Referenced by main().
| size_t bj_address_count | ( | const struct bj_address * | addr | ) |
Returns number of resolved addresses.
DNS resolution may return multiple IPs for a single hostname.
| addr | The address to query. |
| bj_bool bj_address_equals | ( | const struct bj_address * | a, |
| const struct bj_address * | b ) |
Compares two addresses for equality.
| a | First address. |
| b | Second address. |
| bj_bool bj_address_next | ( | struct bj_address * | addr | ) |
Advances to the next resolved address.
Use this to manually iterate through multiple resolved IPs for fallback on connection failure.
| addr | The address to advance. |
| uint16_t bj_address_port | ( | const struct bj_address * | addr | ) |
Gets the port from a resolved address.
| addr | The address to query. |
| void bj_address_reset | ( | struct bj_address * | addr | ) |
Resets address iteration to the first resolved address.
| addr | The address to reset. |
| size_t bj_address_string | ( | const struct bj_address * | addr, |
| char * | buf, | ||
| size_t | size ) |
Gets the IP address string from a resolved address.
| addr | The address to query. |
| buf | Buffer to store the string. May be NULL to query length. |
| size | Buffer size. |
| void bj_close_datagram | ( | struct bj_datagram * | dgram | ) |
Closes a datagram and frees resources.
NULL-safe.
| dgram | The datagram to close. May be NULL. |
Referenced by main().
| void bj_close_tcp_listener | ( | struct bj_tcp_listener * | listener | ) |
Closes a TCP listener and releases associated resources.
Safe to call with NULL.
| listener | The TCP listener to close. May be NULL. |
Referenced by main().
| void bj_close_tcp_stream | ( | struct bj_tcp_stream * | stream | ) |
Closes a TCP stream and releases associated resources.
Safe to call with NULL.
| stream | The TCP stream to close. May be NULL. |
Referenced by main().
| void bj_close_udp | ( | struct bj_udp * | udp | ) |
Closes a UDP socket.
NULL-safe.
| udp | The UDP socket to close. May be NULL. |
Referenced by main().
| struct bj_tcp_stream * bj_connect_tcp | ( | const char * | host, |
| uint16_t | port, | ||
| uint16_t | hint, | ||
| uint32_t | ms_timeout, | ||
| struct bj_error ** | error ) |
Connects to a TCP server at the specified host and port.
Resolves the hostname and attempts to connect. If multiple addresses are returned by DNS, each is tried until one succeeds.
| host | Hostname or IP address to connect to. |
| port | Port number to connect to. |
| hint | Address family hint. Use 0 for any, BJ_RESOLVE_IPV4 for IPv4 only, or BJ_RESOLVE_IPV6 for IPv6 only. |
| ms_timeout | Attempt timeout in milliseconds (0 = infinite) |
| error | Optional error output. May be NULL. |
Referenced by main().
| struct bj_tcp_stream * bj_connect_tcp_to | ( | struct bj_address * | addr, |
| uint16_t | hints, | ||
| uint32_t | ms_timeout, | ||
| struct bj_error ** | error ) |
Connects to a pre-resolved address, trying all resolved IPs.
Automatically iterates through all addresses returned by DNS resolution until one succeeds.
| addr | Destination address (from bj_resolve_address). |
| hints | Socket hints (BJ_TCP_DELAY_ACK, etc.). |
| ms_timeout | Connection timeout per address in milliseconds. |
| error | Optional error output. |
| size_t bj_datagram_addr | ( | const struct bj_datagram * | dgram, |
| char * | buf, | ||
| size_t | size ) |
Gets the sender's address from a datagram.
| dgram | Datagram. |
| buf | Output buffer (may be NULL to query length). |
| size | Buffer size. |
Referenced by main().
| const void * bj_datagram_data | ( | const struct bj_datagram * | dgram | ) |
Gets the data pointer from a datagram.
| dgram | Datagram. |
Referenced by main().
| size_t bj_datagram_len | ( | const struct bj_datagram * | dgram | ) |
Gets the data length from a datagram.
| dgram | Datagram. |
Referenced by main().
| uint16_t bj_datagram_port | ( | const struct bj_datagram * | dgram | ) |
Gets the sender's port from a datagram.
| dgram | Datagram. |
Referenced by main().
| void bj_free_address | ( | struct bj_address * | addr | ) |
Frees a resolved address.
NULL-safe.
| addr | The address to free. May be NULL. |
| bj_bool bj_get_tcp_keepalive | ( | struct bj_tcp_stream * | stream | ) |
Gets the current TCP keep-alive setting.
| stream | TCP stream. |
| uint32_t bj_get_tcp_recv_timeout | ( | struct bj_tcp_stream * | stream | ) |
Gets the current receive timeout.
| stream | TCP stream. |
| uint32_t bj_get_tcp_send_timeout | ( | struct bj_tcp_stream * | stream | ) |
Gets the current send timeout.
| stream | TCP stream. |
| struct bj_tcp_listener * bj_listen_tcp | ( | const char * | host, |
| uint16_t | port, | ||
| uint16_t | hint, | ||
| int | backlog, | ||
| struct bj_error ** | error ) |
Creates a TCP listener bound to the specified host and port.
The listener accepts incoming TCP connections via bj_accept_tcp.
| host | Hostname or IP address to bind to. Pass NULL to bind to all interfaces. |
| port | Port number to listen on. |
| hint | Address family hint. Use 0 for any, BJ_RESOLVE_IPV4 for IPv4 only, or BJ_RESOLVE_IPV6 for IPv6 only. |
| backlog | Maximum pending connections queue size. Use BJ_NET_BACKLOG_DEFAULT for a reasonable default, or BJ_NET_BACKLOG_MAX for the system maximum. |
| error | Optional error output. May be NULL. |
Referenced by main().
| struct bj_udp * bj_open_udp | ( | const char * | host, |
| uint16_t | port, | ||
| uint16_t | hints, | ||
| struct bj_error ** | error ) |
Opens a UDP socket bound to the specified port.
| host | Hostname/IP to bind to. NULL for any interface. |
| port | Port to bind. 0 for ephemeral (system-assigned). |
| hints | Flags: BJ_RESOLVE_IPV4, BJ_RESOLVE_IPV6, BJ_NONBLOCKING, BJ_BROADCAST |
| error | Optional error output. |
Referenced by main().
| struct bj_address * bj_resolve_address | ( | const char * | host, |
| uint16_t | port, | ||
| uint16_t | hints, | ||
| struct bj_error ** | error ) |
Resolves a hostname to a network address.
The returned address can be used with bj_connect_tcp_to and bj_udp_send_to to avoid repeated DNS lookups.
| host | Hostname or IP string to resolve. |
| port | Port number. |
| hints | BJ_RESOLVE_IPV4, BJ_RESOLVE_IPV6, or 0 for any. |
| error | Optional error output. |
| bj_bool bj_set_tcp_keepalive | ( | struct bj_tcp_stream * | stream, |
| bj_bool | enable ) |
Enables or disables TCP keep-alive probes.
When enabled, the OS sends periodic probes on idle connections to detect dead peers. If no response is received, the connection is terminated.
| stream | TCP stream. |
| enable | BJ_TRUE to enable, BJ_FALSE to disable. |
| bj_bool bj_set_tcp_recv_timeout | ( | struct bj_tcp_stream * | stream, |
| uint32_t | ms_timeout ) |
Sets the receive timeout for a TCP stream.
When set, recv operations will timeout after the specified duration.
| stream | TCP stream. |
| ms_timeout | Timeout in milliseconds. 0 = no timeout (blocking). |
| bj_bool bj_set_tcp_send_timeout | ( | struct bj_tcp_stream * | stream, |
| uint32_t | ms_timeout ) |
Sets the send timeout for a TCP stream.
When set, send operations will timeout after the specified duration.
| stream | TCP stream. |
| ms_timeout | Timeout in milliseconds. 0 = no timeout (blocking). |
| int bj_tcp_available | ( | struct bj_tcp_stream * | stream | ) |
Checks bytes available to read without blocking.
| stream | TCP stream. |
Referenced by main().
| uint16_t bj_tcp_listener_port | ( | const struct bj_tcp_listener * | listener | ) |
Get the listening port of the given server.
| listener | The listener to get the local port from. |
Will return 0 if listener is 0 or closed.
Referenced by main().
| size_t bj_tcp_local_address | ( | const struct bj_tcp_stream * | stream, |
| char * | buf, | ||
| size_t | size ) |
Gets the local address of a TCP stream.
Writes the local IP address as a string into buf. Works like snprintf: returns the length of the address string regardless of buffer size.
| stream | The TCP stream to query. |
| buf | Buffer to store the address string. May be NULL to query length only. |
| size | Size of the buffer in bytes. |
| int bj_tcp_peek | ( | struct bj_tcp_stream * | stream, |
| void * | buf, | ||
| size_t | len ) |
Peek at incoming data without consuming it.
Like bj_tcp_recv but data remains in the buffer for subsequent reads. Useful for protocol parsing where you need to inspect headers.
| stream | The TCP stream to peek from. |
| buf | Buffer to store peeked data. |
| len | Maximum number of bytes to peek. |
| size_t bj_tcp_peer_address | ( | const struct bj_tcp_stream * | stream, |
| char * | buf, | ||
| size_t | size ) |
Gets the remote address of a TCP stream.
Writes the peer's IP address as a string into buf. Works like snprintf: returns the length of the address string regardless of buffer size.
| stream | The TCP stream to query. |
| buf | Buffer to store the address string. May be NULL to query length only. |
| size | Size of the buffer in bytes. |
Referenced by main().
| int bj_tcp_recv | ( | struct bj_tcp_stream * | stream, |
| void * | buf, | ||
| size_t | len ) |
Receives data from a TCP stream.
Reads up to len bytes from the stream into buf. Blocks until data is available or the connection is closed.
| stream | The TCP stream to receive from. |
| buf | Buffer to store received data. |
| len | Maximum number of bytes to receive. |
Referenced by main().
| int bj_tcp_send | ( | struct bj_tcp_stream * | stream, |
| const void * | buf, | ||
| size_t | len ) |
Sends data over a TCP stream.
Sends up to len bytes from buf over the stream.
| stream | The TCP stream to send on. |
| buf | Buffer containing data to send. |
| len | Number of bytes to send. |
Referenced by main().
| void bj_tcp_shutdown | ( | struct bj_tcp_stream * | stream, |
| int | mode ) |
Shuts down part or all of a TCP connection.
Gracefully shuts down the send and/or receive side of the connection without closing the socket.
| stream | The TCP stream to shutdown. |
| mode | Shutdown mode: BJ_SHUTDOWN_RECEIVE, BJ_SHUTDOWN_SEND, or BJ_SHUTDOWN_BOTH. |
| void bj_tcp_simulate_loss | ( | struct bj_tcp_stream * | stream, |
| int | percent ) |
Simulates packet loss on a TCP stream.
| stream | TCP stream. |
| percent | Loss percentage (0-100). 0 disables simulation. |
| bj_bool bj_tcp_wait | ( | struct bj_tcp_stream * | stream, |
| uint32_t | ms_timeout ) |
Waits for data to become available on a TCP stream.
Uses select() internally to wait for readability with a timeout.
| stream | TCP stream to wait on. |
| ms_timeout | Timeout in milliseconds. 0 = non-blocking poll. |
Checks if a datagram is available without blocking.
| udp | UDP socket. |
| size_t bj_udp_local_address | ( | const struct bj_udp * | udp, |
| char * | buf, | ||
| size_t | size ) |
Gets the local address of a UDP socket (snprintf-style).
| udp | UDP socket. |
| buf | Output buffer (may be NULL to query length). |
| size | Buffer size. |
| uint16_t bj_udp_local_port | ( | const struct bj_udp * | udp | ) |
Gets the local port of a UDP socket.
| udp | UDP socket. |
Referenced by main().
| struct bj_datagram * bj_udp_recv | ( | struct bj_udp * | udp, |
| struct bj_error ** | error ) |
Receives a datagram from the UDP socket.
If BJ_NONBLOCKING was set and no data available, returns NULL without error. If blocking, waits until a datagram arrives.
| udp | UDP socket. |
| error | Optional error output. |
Referenced by main().
| int bj_udp_send | ( | struct bj_udp * | udp, |
| const char * | host, | ||
| uint16_t | port, | ||
| const void * | data, | ||
| size_t | len, | ||
| struct bj_error ** | error ) |
Sends a datagram to the specified destination.
Resolves hostname internally on each call.
| udp | UDP socket. |
| host | Destination hostname or IP. |
| port | Destination port. |
| data | Data to send. |
| len | Data length. |
| error | Optional error output. |
Referenced by main().
| int bj_udp_send_to | ( | struct bj_udp * | udp, |
| const struct bj_address * | addr, | ||
| const void * | data, | ||
| size_t | len, | ||
| struct bj_error ** | error ) |
Sends a datagram to a pre-resolved address (no DNS lookup).
More efficient than bj_udp_send when sending many packets to the same destination, as it avoids repeated DNS lookups.
| udp | UDP socket. |
| addr | Destination address (from bj_resolve_address). |
| data | Data to send. |
| len | Data length. |
| error | Optional error output. |
| void bj_udp_simulate_loss | ( | struct bj_udp * | udp, |
| int | percent ) |
Simulates packet loss on a UDP socket.
| udp | UDP socket. |
| percent | Loss percentage (0-100). 0 disables simulation. |
Waits for data to become available on a UDP socket.
Uses select() internally to wait for readability with a timeout.
| udp | UDP socket. |
| ms_timeout | Timeout in milliseconds. 0 = non-blocking poll. |
Referenced by main().