Banjo API 0.0.1
C99 game development API
Loading...
Searching...
No Matches
net.h
Go to the documentation of this file.
1
33#ifndef BJ_NET_H
34#define BJ_NET_H
35
36#include <banjo/api.h>
37#include <banjo/error.h>
38
46struct bj_address;
47
54struct bj_tcp_listener;
55
62struct bj_tcp_stream;
63
69struct bj_udp;
70
77struct bj_datagram;
78
93
98
116 const char* host,
117 uint16_t port,
118 uint16_t hints,
119 struct bj_error** error
120);
121
132 const struct bj_address* addr,
133 char* buf,
134 size_t size
135);
136
144BANJO_EXPORT uint16_t bj_address_port(const struct bj_address* addr);
145
155 const struct bj_address* a,
156 const struct bj_address* b
157);
158
168BANJO_EXPORT size_t bj_address_count(const struct bj_address* addr);
169
181
188
195
197
206
213#define BJ_ADDR_STRLEN 46
214
220#define BJ_NET_BACKLOG_DEFAULT 0
221
227#define BJ_NET_BACKLOG_MAX (-1)
228
250 const char* host,
251 uint16_t port,
252 uint16_t hint,
253 int backlog,
254 struct bj_error** error
255);
256
277 const char* host,
278 uint16_t port,
279 uint16_t hint,
280 uint32_t ms_timeout,
281 struct bj_error** error
282);
283
301 struct bj_address* addr,
302 uint16_t hints,
303 uint32_t ms_timeout,
304 struct bj_error** error
305);
306
322 struct bj_tcp_listener* listener,
323 uint32_t ms_timeout,
324 struct bj_error** error
325);
326
342 struct bj_tcp_stream* stream,
343 void* buf,
344 size_t len
345);
346
362 struct bj_tcp_stream* stream,
363 void* buf,
364 size_t len
365);
366
379 struct bj_tcp_stream* stream,
380 const void* buf,
381 size_t len
382);
383
397 struct bj_tcp_stream* stream,
398 int mode
399);
400
415
424
436 struct bj_tcp_stream* stream,
437 uint32_t ms_timeout
438);
439
451 struct bj_tcp_stream* stream,
452 uint32_t ms_timeout
453);
454
463
475 struct bj_tcp_stream* stream,
476 uint32_t ms_timeout
477);
478
487
496 struct bj_tcp_stream* stream
497);
498
507 struct bj_tcp_listener* listener
508);
509
528 const struct bj_tcp_stream* stream,
529 char* buf,
530 size_t size
531);
532
551 const struct bj_tcp_stream* stream,
552 char* buf,
553 size_t size
554);
555
566 const struct bj_tcp_listener* listener
567);
568
576 struct bj_tcp_stream* stream
577);
578
583
594 const char* host,
595 uint16_t port,
596 uint16_t hints,
597 struct bj_error** error
598);
599
606 struct bj_udp* udp
607);
608
623 struct bj_udp* udp,
624 const char* host,
625 uint16_t port,
626 const void* data,
627 size_t len,
628 struct bj_error** error
629);
630
649 struct bj_udp* udp,
650 const struct bj_address* addr,
651 const void* data,
652 size_t len,
653 struct bj_error** error
654);
655
668 struct bj_udp* udp,
669 struct bj_error** error
670);
671
679 struct bj_udp* udp
680);
681
692 struct bj_udp* udp,
693 uint32_t ms_timeout
694);
695
703 const struct bj_udp* udp
704);
705
715 const struct bj_udp* udp,
716 char* buf,
717 size_t size
718);
719
721
726
736 const struct bj_datagram* dgram,
737 char* buf,
738 size_t size
739);
740
748 const struct bj_datagram* dgram
749);
750
758 const struct bj_datagram* dgram
759);
760
768 const struct bj_datagram* dgram
769);
770
777 struct bj_datagram* dgram
778);
779
781
786
787#if defined(BJ_BUILD_DEBUG) || defined(BJ_CONFIG_NET_SIMULATION)
788
796 struct bj_tcp_stream* stream,
797 int percent
798);
799
807 struct bj_udp* udp,
808 int percent
809);
810
811#endif
812
814
815#endif
General-purpose definitions for Banjo API.
bj_audio_play_note_data data
Definition audio_pcm.c:22
Recoverable error handling.
struct bj_datagram bj_datagram
Definition api.h:308
struct bj_address bj_address
Definition api.h:304
struct bj_error bj_error
Definition api.h:282
#define BANJO_EXPORT
Definition api.h:113
struct bj_udp bj_udp
Definition api.h:307
uint32_t bj_bool
Boolean type used throughout the Banjo API.
Definition api.h:207
struct bj_tcp_listener bj_tcp_listener
Definition api.h:305
struct bj_tcp_stream bj_tcp_stream
Definition api.h:306
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:84
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:201
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:85
@ BJ_NONBLOCKING
Non-blocking mode.
Definition net.h:90
@ BJ_RESOLVE_IPV4
Restrict address resolution to IPv4.
Definition net.h:86
@ BJ_EXCLUSIVE_BIND
Disable address reuse.
Definition net.h:88
@ BJ_BROADCAST
Enable UDP broadcast (SO_BROADCAST).
Definition net.h:91
@ BJ_TCP_DELAY_ACK
Enable Nagle's algorithm.
Definition net.h:89
@ BJ_RESOLVE_IPV6
Restrict address resolution to IPv6.
Definition net.h:87
@ BJ_SHUTDOWN_SEND
Shutdown send operations.
Definition net.h:203
@ BJ_SHUTDOWN_RECEIVE
Shutdown receive operations.
Definition net.h:202
@ BJ_SHUTDOWN_BOTH
Shutdown both send and receive.
Definition net.h:204