Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
net_tcp_timeout.c
Go to the documentation of this file.
1
9
10#include <banjo/log.h>
11#include <banjo/main.h>
12#include <banjo/net.h>
13#include <banjo/system.h>
14#include <banjo/error.h>
15
16#define PORT 8085
17#define ACCEPT_TIMEOUT_MS 5000
18
19int main(int argc, char* argv[]) {
20 (void)argc;
21 (void)argv;
22
23 if (!bj_begin(BJ_NETWORK_SYSTEM, 0)) {
24 return 1;
25 }
26
27 struct bj_error* error = 0;
28
29 struct bj_tcp_listener* listener = bj_listen_tcp("0.0.0.0", PORT, BJ_RESOLVE_IPV4, 5, &error);
30 if (!listener) {
31 bj_err("server: failed to listen (%s)", bj_error_message(error));
32 bj_clear_error(&error);
33 bj_end();
34 return 1;
35 }
36
37 bj_info("server: listening on port %d", bj_tcp_listener_port(listener));
38 bj_info("server: accept timeout is %d ms", ACCEPT_TIMEOUT_MS);
39
40 int timeout_count = 0;
41 int connection_count = 0;
42
43 while (1) {
44 struct bj_tcp_stream* client = bj_accept_tcp(listener, ACCEPT_TIMEOUT_MS, &error);
45
46 if (!client) {
47 if (error && bj_error_code(error) == BJ_ERROR_NETWORK_TIMEOUT) {
48 timeout_count++;
49 bj_info("server: accept timed out (%d timeouts, %d connections so far)",
50 timeout_count, connection_count);
51 bj_clear_error(&error);
52 // Could do periodic maintenance here
53 continue;
54 }
55
56 bj_err("server: accept failed (%s)", bj_error_message(error));
57 bj_clear_error(&error);
58 continue;
59 }
60
61 connection_count++;
62
63 char addr[BJ_ADDR_STRLEN];
64 bj_tcp_peer_address(client, addr, sizeof(addr));
65 bj_info("server: client %d connected from %s", connection_count, addr);
66
67 char buf[256];
68 int n;
69 while ((n = bj_tcp_recv(client, buf, sizeof(buf) - 1)) > 0) {
70 buf[n] = '\0';
71 bj_info("server: client %d: \"%s\"", connection_count, buf);
72 bj_tcp_send(client, buf, n);
73 }
74
75 bj_info("server: client %d disconnected", connection_count);
76 bj_close_tcp_stream(client);
77 }
78
79 bj_close_tcp_listener(listener);
80 bj_end();
81 return 0;
82}
int main(int argc, char *argv[])
Definition audio_pcm.c:177
Recoverable error handling.
struct bj_error bj_error
Definition api.h:333
struct bj_tcp_listener bj_tcp_listener
Definition api.h:356
struct bj_tcp_stream bj_tcp_stream
Definition api.h:357
const char * bj_error_message(const struct bj_error *error)
Gets the error message from an error object.
void bj_clear_error(struct bj_error **error)
Frees an error and sets the pointer to NULL.
bj_error_code
A numeric representation of an error in Banjo.
Definition error.h:249
@ BJ_ERROR_NETWORK_TIMEOUT
Operation timed out.
Definition error.h:303
#define bj_info(...)
Log a message using the BJ_LOG_INFO level.
Definition log.h:141
#define bj_err(...)
Log a message using the BJ_LOG_ERROR level.
Definition log.h:169
uint16_t bj_tcp_listener_port(const struct bj_tcp_listener *listener)
Get the listening port of the given server.
int bj_tcp_recv(struct bj_tcp_stream *stream, void *buf, size_t len)
Receives data from a TCP stream.
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.
void bj_close_tcp_stream(struct bj_tcp_stream *stream)
Closes a TCP stream and releases associated resources.
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.
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.
#define BJ_ADDR_STRLEN
Maximum length of an address string (including null terminator).
Definition net.h:245
@ BJ_RESOLVE_IPV4
Restrict address resolution to IPv4.
Definition net.h:118
bj_bool bj_begin(int systems, struct bj_error **error)
Initialises the system.
void bj_end(void)
De-initialises the system.
@ BJ_NETWORK_SYSTEM
Definition system.h:82
Logging utility functions.
Portable main() replacement with platform-aware entry shim.
Header file for network API.
#define PORT
#define ACCEPT_TIMEOUT_MS
Header file for system interactions.