Banjo API 1.0.0-rc.2
Low-level C99 game development API
Loading...
Searching...
No Matches
net_udp_broadcast_recv.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
15#include <string.h>
16
17#define PORT 9999
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_udp* udp = bj_open_udp("0.0.0.0", PORT, BJ_RESOLVE_IPV4, &error);
30 if (!udp) {
31 bj_err("discover: failed to open socket (%s)", bj_error_message(error));
32 bj_clear_error(&error);
33 bj_end();
34 return 1;
35 }
36
37 bj_info("discover: listening for broadcasts on port %d", PORT);
38
39 while (1) {
40 if (!bj_udp_wait(udp, 5000)) {
41 bj_info("discover: no broadcasts received in last 5 seconds...");
42 continue;
43 }
44
45 struct bj_datagram* dgram = bj_udp_recv(udp, &error);
46 if (!dgram) {
47 if (error) {
48 bj_err("discover: recv error (%s)", bj_error_message(error));
49 bj_clear_error(&error);
50 }
51 continue;
52 }
53
54 char addr[BJ_ADDR_STRLEN];
55 bj_datagram_addr(dgram, addr, sizeof(addr));
56
57 char message[256];
58 size_t len = bj_datagram_len(dgram);
59 if (len >= sizeof(message)) {
60 len = sizeof(message) - 1;
61 }
62 memcpy(message, bj_datagram_data(dgram), len);
63 message[len] = '\0';
64
65 bj_info("discover: found service at %s:%d - \"%s\"",
66 addr, bj_datagram_port(dgram), message);
67
68 bj_close_datagram(dgram);
69 }
70
71 bj_close_udp(udp);
72 bj_end();
73 return 0;
74}
int main(int argc, char *argv[])
Definition audio_pcm.c:177
struct bj_datagram bj_datagram
Definition api.h:359
struct bj_error bj_error
Definition api.h:333
struct bj_udp bj_udp
Definition api.h:358
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.
#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
size_t bj_datagram_addr(const struct bj_datagram *dgram, char *buf, size_t size)
Gets the sender's address from a datagram.
void bj_close_datagram(struct bj_datagram *dgram)
Closes a datagram and frees resources.
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.
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.
#define BJ_ADDR_STRLEN
Maximum length of an address string (including null terminator).
Definition net.h:245
struct bj_datagram * bj_udp_recv(struct bj_udp *udp, struct bj_error **error)
Receives a datagram from the UDP socket.
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.
bj_bool bj_udp_wait(struct bj_udp *udp, uint32_t ms_timeout)
Waits for data to become available on a UDP socket.
@ 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
String utility functions.
Header file for system interactions.