Skip to content

Commit f35fb58

Browse files
committed
voipchat: allow configuring server bind address from cli arguments
1 parent 92e05ac commit f35fb58

File tree

1 file changed

+44
-14
lines changed

1 file changed

+44
-14
lines changed

examples/voipchat.c

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -243,17 +243,24 @@ static void mainloop(void)
243243
SDL_RenderPresent(renderer);
244244
}
245245
}
246+
static void print_usage(const char *prog) {
247+
SDL_Log("USAGE: %s <hostname|ip> [--help] [--server] [--port X] [--simulate-failure Y]", prog);
248+
}
246249

247250
static void run_voipchat(int argc, char **argv)
248251
{
249252
const char *hostname = NULL;
250253
bool is_server = false;
251254
int simulate_failure = 0;
252255
int i;
256+
SDLNet_Address *socket_address = NULL;
253257

254258
for (i = 1; i < argc; i++) {
255259
const char *arg = argv[i];
256-
if (SDL_strcmp(arg, "--server") == 0) {
260+
if (SDL_strcmp(arg, "--help") == 0) {
261+
print_usage(argv[0]);
262+
return;
263+
} else if (SDL_strcmp(arg, "--server") == 0) {
257264
is_server = true;
258265
} else if ((SDL_strcmp(arg, "--port") == 0) && (i < (argc-1))) {
259266
server_port = (Uint16) SDL_atoi(argv[++i]);
@@ -270,15 +277,37 @@ static void run_voipchat(int argc, char **argv)
270277
SDL_Log("Simulating failure at %d percent", simulate_failure);
271278
}
272279

273-
if (is_server && hostname) {
274-
SDL_Log("WARNING: Specified --server and a hostname, ignoring the hostname");
275-
} else if (!is_server && !hostname) {
276-
SDL_Log("USAGE: %s <--server|hostname> [--port X] [--simulate-failure Y]", argv[0]);
280+
if (!is_server && !hostname) {
281+
print_usage(argv[0]);
277282
return;
278283
}
279284

280285
if (is_server) {
281-
SDL_Log("SERVER: Listening on port %d", server_port);
286+
if (hostname) {
287+
SDL_Log("SERVER: Resolving binding hostname '%s' ...", hostname);
288+
socket_address = SDLNet_ResolveHostname(hostname);
289+
if (socket_address) {
290+
if (SDLNet_WaitUntilResolved(socket_address, -1) < 0) {
291+
SDLNet_UnrefAddress(socket_address);
292+
socket_address = NULL;
293+
}
294+
}
295+
} else {
296+
int num_addresses;
297+
SDLNet_Address **addresses;
298+
addresses = SDLNet_GetLocalAddresses(&num_addresses);
299+
if (addresses == NULL || num_addresses <= 0) {
300+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to to get local addresses: %s", SDL_GetError());
301+
} else {
302+
socket_address = addresses[0];
303+
SDLNet_RefAddress(socket_address);
304+
}
305+
}
306+
if (socket_address) {
307+
SDL_Log("SERVER: Listening on %s:%d.", SDLNet_GetAddressString(socket_address), server_port);
308+
} else {
309+
SDL_Log("SERVER: Listening on port %d", server_port);
310+
}
282311
} else {
283312
SDL_Log("CLIENT: Resolving server hostname '%s' ...", hostname);
284313
server_addr = SDLNet_ResolveHostname(hostname);
@@ -299,26 +328,27 @@ static void run_voipchat(int argc, char **argv)
299328

300329
audio_device = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &audio_spec);
301330
if (!audio_device) {
302-
SDL_Log("CLIENT: Failed to open output audio device (%s), going on without sound playback!", SDL_GetError());
331+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "CLIENT: Failed to open output audio device (%s), going on without sound playback!", SDL_GetError());
303332
}
304333

305334
capture_device = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_RECORDING, &audio_spec);
306335
if (!capture_device) {
307-
SDL_Log("CLIENT: Failed to open capture audio device (%s), going on without sound recording!", SDL_GetError());
336+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "CLIENT: Failed to open capture audio device (%s), going on without sound recording!", SDL_GetError());
308337
} else {
309338
capture_stream = SDL_CreateAudioStream(&audio_spec, &audio_spec);
310339
if (!capture_stream) {
311-
SDL_Log("CLIENT: Failed to create capture audio stream (%s), going on without sound recording!", SDL_GetError());
340+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "CLIENT: Failed to create capture audio stream (%s), going on without sound recording!", SDL_GetError());
312341
SDL_CloseAudioDevice(capture_device);
313342
capture_device = 0;
314343
}
315344
}
316345
}
317346

318347
/* server _must_ be on the requested port. Clients can take anything available, server will respond to where it sees it come from. */
319-
sock = SDLNet_CreateDatagramSocket(NULL, is_server ? server_port : 0);
348+
sock = SDLNet_CreateDatagramSocket(socket_address, is_server ? server_port : 0);
349+
SDLNet_UnrefAddress(socket_address);
320350
if (!sock) {
321-
SDL_Log("Failed to create datagram socket: %s", SDL_GetError());
351+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create datagram socket: %s", SDL_GetError());
322352
} else {
323353
if (simulate_failure) {
324354
SDLNet_SimulateDatagramPacketLoss(sock, simulate_failure);
@@ -345,17 +375,17 @@ static void run_voipchat(int argc, char **argv)
345375
int main(int argc, char **argv)
346376
{
347377
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
348-
SDL_Log("SDL_Init failed: %s\n", SDL_GetError());
378+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init failed: %s\n", SDL_GetError());
349379
return 1;
350380
}
351381

352382
if (!SDLNet_Init()) {
353-
SDL_Log("SDLNet_Init failed: %s\n", SDL_GetError());
383+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLNet_Init failed: %s\n", SDL_GetError());
354384
SDL_Quit();
355385
return 1;
356386
}
357387

358-
window = SDL_CreateWindow("SDL_Net3 voipchat example", 640, 480, 0);
388+
window = SDL_CreateWindow("SDL3_Net voipchat example", 640, 480, 0);
359389
renderer = SDL_CreateRenderer(window, NULL);
360390
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
361391

0 commit comments

Comments
 (0)