Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
net.h
Go to the documentation of this file.
1
65#ifndef BJ_NET_H
66#define BJ_NET_H
67
68#include <banjo/api.h>
69#include <banjo/error.h>
70
78struct bj_address;
79
86struct bj_tcp_listener;
87
94struct bj_tcp_stream;
95
101struct bj_udp;
102
109struct bj_datagram;
110
125
130
148 const char* host,
149 uint16_t port,
150 uint16_t hints,
151 struct bj_error** error
152);
153
164 const struct bj_address* addr,
165 char* buf,
166 size_t size
167);
168
176BANJO_EXPORT uint16_t bj_address_port(const struct bj_address* addr);
177
187 const struct bj_address* a,
188 const struct bj_address* b
189);
190
200BANJO_EXPORT size_t bj_address_count(const struct bj_address* addr);
201
213
220
227
229
238
245#define BJ_ADDR_STRLEN 46
246
252#define BJ_NET_BACKLOG_DEFAULT 0
253
259#define BJ_NET_BACKLOG_MAX (-1)
260
282 const char* host,
283 uint16_t port,
284 uint16_t hint,
285 int backlog,
286 struct bj_error** error
287);
288
309 const char* host,
310 uint16_t port,
311 uint16_t hint,
312 uint32_t ms_timeout,
313 struct bj_error** error
314);
315
333 struct bj_address* addr,
334 uint16_t hints,
335 uint32_t ms_timeout,
336 struct bj_error** error
337);
338
354 struct bj_tcp_listener* listener,
355 uint32_t ms_timeout,
356 struct bj_error** error
357);
358
374 struct bj_tcp_stream* stream,
375 void* buf,
376 size_t len
377);
378
394 struct bj_tcp_stream* stream,
395 void* buf,
396 size_t len
397);
398
411 struct bj_tcp_stream* stream,
412 const void* buf,
413 size_t len
414);
415
429 struct bj_tcp_stream* stream,
430 int mode
431);
432
447
456
468 struct bj_tcp_stream* stream,
469 uint32_t ms_timeout
470);
471
483 struct bj_tcp_stream* stream,
484 uint32_t ms_timeout
485);
486
495
507 struct bj_tcp_stream* stream,
508 uint32_t ms_timeout
509);
510
519
528 struct bj_tcp_stream* stream
529);
530
539 struct bj_tcp_listener* listener
540);
541
560 const struct bj_tcp_stream* stream,
561 char* buf,
562 size_t size
563);
564
583 const struct bj_tcp_stream* stream,
584 char* buf,
585 size_t size
586);
587
598 const struct bj_tcp_listener* listener
599);
600
608 struct bj_tcp_stream* stream
609);
610
615
626 const char* host,
627 uint16_t port,
628 uint16_t hints,
629 struct bj_error** error
630);
631
638 struct bj_udp* udp
639);
640
655 struct bj_udp* udp,
656 const char* host,
657 uint16_t port,
658 const void* data,
659 size_t len,
660 struct bj_error** error
661);
662
681 struct bj_udp* udp,
682 const struct bj_address* addr,
683 const void* data,
684 size_t len,
685 struct bj_error** error
686);
687
700 struct bj_udp* udp,
701 struct bj_error** error
702);
703
711 struct bj_udp* udp
712);
713
724 struct bj_udp* udp,
725 uint32_t ms_timeout
726);
727
735 const struct bj_udp* udp
736);
737
747 const struct bj_udp* udp,
748 char* buf,
749 size_t size
750);
751
753
758
768 const struct bj_datagram* dgram,
769 char* buf,
770 size_t size
771);
772
780 const struct bj_datagram* dgram
781);
782
790 const struct bj_datagram* dgram
791);
792
800 const struct bj_datagram* dgram
801);
802
809 struct bj_datagram* dgram
810);
811
813
818
819#if defined(BJ_BUILD_DEBUG) || defined(BJ_CONFIG_NET_SIMULATION)
820
828 struct bj_tcp_stream* stream,
829 int percent
830);
831
839 struct bj_udp* udp,
840 int percent
841);
842
843#endif
844
846
847#endif
General-purpose definitions for Banjo API.
bj_audio_play_note_data data
Definition audio_pcm.c:104
Recoverable error handling.
struct bj_datagram bj_datagram
Definition api.h:359
struct bj_address bj_address
Definition api.h:355
struct bj_error bj_error
Definition api.h:333
#define BANJO_EXPORT
Definition api.h:163
struct bj_udp bj_udp
Definition api.h:358
uint32_t bj_bool
Boolean type used throughout the Banjo API.
Definition api.h:257
struct bj_tcp_listener bj_tcp_listener
Definition api.h:356
struct bj_tcp_stream bj_tcp_stream
Definition api.h:357
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.
size_t bj_datagram_addr(const struct bj_datagram *dgram, char *buf, size_t size)
Gets the sender's address from a datagram.
bj_bool bj_address_next(struct bj_address *addr)
Advances to the next resolved address.
bj_bool bj_address_equals(const struct bj_address *a, const struct bj_address *b)
Compares two addresses for equality.
uint16_t bj_tcp_listener_port(const struct bj_tcp_listener *listener)
Get the listening port of the given server.
void bj_close_datagram(struct bj_datagram *dgram)
Closes a datagram and frees resources.
int bj_tcp_recv(struct bj_tcp_stream *stream, void *buf, size_t len)
Receives data from a TCP stream.
uint32_t bj_get_tcp_recv_timeout(struct bj_tcp_stream *stream)
Gets the current receive timeout.
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.
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.
uint16_t bj_datagram_port(const struct bj_datagram *dgram)
Gets the sender's port from a datagram.
size_t bj_datagram_len(const struct bj_datagram *dgram)
Gets the data length from a datagram.
bj_bool bj_set_tcp_send_timeout(struct bj_tcp_stream *stream, uint32_t ms_timeout)
Sets the send timeout for a TCP stream.
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.
bj_bool bj_set_tcp_keepalive(struct bj_tcp_stream *stream, bj_bool enable)
Enables or disables TCP keep-alive probes.
bj_bool bj_get_tcp_keepalive(struct bj_tcp_stream *stream)
Gets the current TCP keep-alive setting.
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.
bj_bool bj_set_tcp_recv_timeout(struct bj_tcp_stream *stream, uint32_t ms_timeout)
Sets the receive timeout for a TCP stream.
void bj_free_address(struct bj_address *addr)
Frees a resolved address.
void bj_close_tcp_stream(struct bj_tcp_stream *stream)
Closes a TCP stream and releases associated resources.
int bj_tcp_available(struct bj_tcp_stream *stream)
Checks bytes available to read without blocking.
bj_net_hints
Hint flags for network operations.
Definition net.h:116
size_t bj_address_count(const struct bj_address *addr)
Returns number of resolved addresses.
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.
uint16_t bj_udp_local_port(const struct bj_udp *udp)
Gets the local port of a UDP socket.
size_t bj_address_string(const struct bj_address *addr, char *buf, size_t size)
Gets the IP address string from a resolved address.
void bj_udp_simulate_loss(struct bj_udp *udp, int percent)
Simulates packet loss on a UDP socket.
int bj_tcp_peek(struct bj_tcp_stream *stream, void *buf, size_t len)
Peek at incoming data without consuming it.
void bj_address_reset(struct bj_address *addr)
Resets address iteration to the first resolved address.
uint32_t bj_get_tcp_send_timeout(struct bj_tcp_stream *stream)
Gets the current send timeout.
void bj_tcp_shutdown(struct bj_tcp_stream *stream, int mode)
Shuts down part or all of a TCP connection.
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.
void bj_close_tcp_listener(struct bj_tcp_listener *listener)
Closes a TCP listener and releases associated resources.
uint16_t bj_address_port(const struct bj_address *addr)
Gets the port from a resolved address.
int bj_tcp_send(struct bj_tcp_stream *stream, const void *buf, size_t len)
Sends data over a 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.
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.
void bj_tcp_simulate_loss(struct bj_tcp_stream *stream, int percent)
Simulates packet loss on a TCP stream.
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).
bj_bool bj_udp_available(struct bj_udp *udp)
Checks if a datagram is available without blocking.
bj_net_shutdown
Shutdown mode for bj_tcp_shutdown.
Definition net.h:233
struct bj_datagram * bj_udp_recv(struct bj_udp *udp, struct bj_error **error)
Receives a datagram from the UDP socket.
bj_bool bj_tcp_wait(struct bj_tcp_stream *stream, uint32_t ms_timeout)
Waits for data to become available on a TCP stream.
void bj_close_udp(struct bj_udp *udp)
Closes a UDP socket.
const void * bj_datagram_data(const struct bj_datagram *dgram)
Gets the data pointer from a datagram.
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).
bj_bool bj_udp_wait(struct bj_udp *udp, uint32_t ms_timeout)
Waits for data to become available on a UDP socket.
@ BJ_NET_NOHINT
No hint provided.
Definition net.h:117
@ BJ_NONBLOCKING
Non-blocking mode.
Definition net.h:122
@ BJ_RESOLVE_IPV4
Restrict address resolution to IPv4.
Definition net.h:118
@ BJ_EXCLUSIVE_BIND
Disable address reuse.
Definition net.h:120
@ BJ_BROADCAST
Enable UDP broadcast (SO_BROADCAST).
Definition net.h:123
@ BJ_TCP_DELAY_ACK
Enable Nagle's algorithm.
Definition net.h:121
@ BJ_RESOLVE_IPV6
Restrict address resolution to IPv6.
Definition net.h:119
@ BJ_SHUTDOWN_SEND
Shutdown send operations.
Definition net.h:235
@ BJ_SHUTDOWN_RECEIVE
Shutdown receive operations.
Definition net.h:234
@ BJ_SHUTDOWN_BOTH
Shutdown both send and receive.
Definition net.h:236