@@ -245,17 +245,24 @@ static void mainloop(void)
245
245
SDL_RenderPresent (renderer );
246
246
}
247
247
}
248
+ static void print_usage (const char * prog ) {
249
+ SDL_Log ("USAGE: %s <hostname|ip> [--help] [--server] [--port X] [--simulate-failure Y]" , prog );
250
+ }
248
251
249
252
static void run_voipchat (int argc , char * * argv )
250
253
{
251
254
const char * hostname = NULL ;
252
255
bool is_server = false;
253
256
int simulate_failure = 0 ;
254
257
int i ;
258
+ SDLNet_Address * socket_address = NULL ;
255
259
256
260
for (i = 1 ; i < argc ; i ++ ) {
257
261
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 ) {
259
266
is_server = true;
260
267
} else if ((SDL_strcmp (arg , "--port" ) == 0 ) && (i < (argc - 1 ))) {
261
268
server_port = (Uint16 ) SDL_atoi (argv [++ i ]);
@@ -272,15 +279,37 @@ static void run_voipchat(int argc, char **argv)
272
279
SDL_Log ("Simulating failure at %d percent" , simulate_failure );
273
280
}
274
281
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 ]);
279
284
return ;
280
285
}
281
286
282
287
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
+ }
284
313
} else {
285
314
SDL_Log ("CLIENT: Resolving server hostname '%s' ..." , hostname );
286
315
server_addr = SDLNet_ResolveHostname (hostname );
@@ -301,26 +330,27 @@ static void run_voipchat(int argc, char **argv)
301
330
302
331
audio_device = SDL_OpenAudioDevice (SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK , & audio_spec );
303
332
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 ());
305
334
}
306
335
307
336
capture_device = SDL_OpenAudioDevice (SDL_AUDIO_DEVICE_DEFAULT_RECORDING , & audio_spec );
308
337
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 ());
310
339
} else {
311
340
capture_stream = SDL_CreateAudioStream (& audio_spec , & audio_spec );
312
341
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 ());
314
343
SDL_CloseAudioDevice (capture_device );
315
344
capture_device = 0 ;
316
345
}
317
346
}
318
347
}
319
348
320
349
/* 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 );
322
352
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 ());
324
354
} else {
325
355
if (simulate_failure ) {
326
356
SDLNet_SimulateDatagramPacketLoss (sock , simulate_failure );
@@ -347,17 +377,17 @@ static void run_voipchat(int argc, char **argv)
347
377
int main (int argc , char * * argv )
348
378
{
349
379
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 ());
351
381
return 1 ;
352
382
}
353
383
354
384
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 ());
356
386
SDL_Quit ();
357
387
return 1 ;
358
388
}
359
389
360
- window = SDL_CreateWindow ("SDL_Net3 voipchat example" , 640 , 480 , 0 );
390
+ window = SDL_CreateWindow ("SDL3_Net voipchat example" , 640 , 480 , 0 );
361
391
renderer = SDL_CreateRenderer (window , NULL );
362
392
SDL_SetRenderDrawColor (renderer , 0 , 0 , 0 , 255 );
363
393
0 commit comments