Skip to content

Commit de5d805

Browse files
committed
voipchat: allow configuring server bind address from cli arguments
1 parent e4a9733 commit de5d805

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
@@ -245,17 +245,24 @@ static void mainloop(void)
245245
SDL_RenderPresent(renderer);
246246
}
247247
}
248+
static void print_usage(const char *prog) {
249+
SDL_Log("USAGE: %s <hostname|ip> [--help] [--server] [--port X] [--simulate-failure Y]", prog);
250+
}
248251

249252
static void run_voipchat(int argc, char **argv)
250253
{
251254
const char *hostname = NULL;
252255
bool is_server = false;
253256
int simulate_failure = 0;
254257
int i;
258+
SDLNet_Address *socket_address = NULL;
255259

256260
for (i = 1; i < argc; i++) {
257261
const char *arg = argv[i];
258-
if (SDL_strcmp(arg, "--server") == 0) {
262+
if (SDL_strcmp(arg, "--help") == 0) {
263+
print_usage(argv[0]);
264+
return;
265+
} else if (SDL_strcmp(arg, "--server") == 0) {
259266
is_server = true;
260267
} else if ((SDL_strcmp(arg, "--port") == 0) && (i < (argc-1))) {
261268
server_port = (Uint16) SDL_atoi(argv[++i]);
@@ -272,15 +279,37 @@ static void run_voipchat(int argc, char **argv)
272279
SDL_Log("Simulating failure at %d percent", simulate_failure);
273280
}
274281

275-
if (is_server && hostname) {
276-
SDL_Log("WARNING: Specified --server and a hostname, ignoring the hostname");
277-
} else if (!is_server && !hostname) {
278-
SDL_Log("USAGE: %s <--server|hostname> [--port X] [--simulate-failure Y]", argv[0]);
282+
if (!is_server && !hostname) {
283+
print_usage(argv[0]);
279284
return;
280285
}
281286

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

302331
audio_device = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, &audio_spec);
303332
if (!audio_device) {
304-
SDL_Log("CLIENT: Failed to open output audio device (%s), going on without sound playback!", SDL_GetError());
333+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "CLIENT: Failed to open output audio device (%s), going on without sound playback!", SDL_GetError());
305334
}
306335

307336
capture_device = SDL_OpenAudioDevice(SDL_AUDIO_DEVICE_DEFAULT_RECORDING, &audio_spec);
308337
if (!capture_device) {
309-
SDL_Log("CLIENT: Failed to open capture audio device (%s), going on without sound recording!", SDL_GetError());
338+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "CLIENT: Failed to open capture audio device (%s), going on without sound recording!", SDL_GetError());
310339
} else {
311340
capture_stream = SDL_CreateAudioStream(&audio_spec, &audio_spec);
312341
if (!capture_stream) {
313-
SDL_Log("CLIENT: Failed to create capture audio stream (%s), going on without sound recording!", SDL_GetError());
342+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "CLIENT: Failed to create capture audio stream (%s), going on without sound recording!", SDL_GetError());
314343
SDL_CloseAudioDevice(capture_device);
315344
capture_device = 0;
316345
}
317346
}
318347
}
319348

320349
/* server _must_ be on the requested port. Clients can take anything available, server will respond to where it sees it come from. */
321-
sock = SDLNet_CreateDatagramSocket(NULL, is_server ? server_port : 0);
350+
sock = SDLNet_CreateDatagramSocket(socket_address, is_server ? server_port : 0);
351+
SDLNet_UnrefAddress(socket_address);
322352
if (!sock) {
323-
SDL_Log("Failed to create datagram socket: %s", SDL_GetError());
353+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create datagram socket: %s", SDL_GetError());
324354
} else {
325355
if (simulate_failure) {
326356
SDLNet_SimulateDatagramPacketLoss(sock, simulate_failure);
@@ -347,17 +377,17 @@ static void run_voipchat(int argc, char **argv)
347377
int main(int argc, char **argv)
348378
{
349379
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
350-
SDL_Log("SDL_Init failed: %s\n", SDL_GetError());
380+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Init failed: %s\n", SDL_GetError());
351381
return 1;
352382
}
353383

354384
if (!SDLNet_Init()) {
355-
SDL_Log("SDLNet_Init failed: %s\n", SDL_GetError());
385+
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDLNet_Init failed: %s\n", SDL_GetError());
356386
SDL_Quit();
357387
return 1;
358388
}
359389

360-
window = SDL_CreateWindow("SDL_Net3 voipchat example", 640, 480, 0);
390+
window = SDL_CreateWindow("SDL3_Net voipchat example", 640, 480, 0);
361391
renderer = SDL_CreateRenderer(window, NULL);
362392
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
363393

0 commit comments

Comments
 (0)