@@ -243,17 +243,24 @@ static void mainloop(void)
243
243
SDL_RenderPresent (renderer );
244
244
}
245
245
}
246
+ static void print_usage (const char * prog ) {
247
+ SDL_Log ("USAGE: %s <hostname|ip> [--help] [--server] [--port X] [--simulate-failure Y]" , prog );
248
+ }
246
249
247
250
static void run_voipchat (int argc , char * * argv )
248
251
{
249
252
const char * hostname = NULL ;
250
253
bool is_server = false;
251
254
int simulate_failure = 0 ;
252
255
int i ;
256
+ SDLNet_Address * socket_address = NULL ;
253
257
254
258
for (i = 1 ; i < argc ; i ++ ) {
255
259
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 ) {
257
264
is_server = true;
258
265
} else if ((SDL_strcmp (arg , "--port" ) == 0 ) && (i < (argc - 1 ))) {
259
266
server_port = (Uint16 ) SDL_atoi (argv [++ i ]);
@@ -270,15 +277,37 @@ static void run_voipchat(int argc, char **argv)
270
277
SDL_Log ("Simulating failure at %d percent" , simulate_failure );
271
278
}
272
279
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 ]);
277
282
return ;
278
283
}
279
284
280
285
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
+ }
282
311
} else {
283
312
SDL_Log ("CLIENT: Resolving server hostname '%s' ..." , hostname );
284
313
server_addr = SDLNet_ResolveHostname (hostname );
@@ -299,26 +328,27 @@ static void run_voipchat(int argc, char **argv)
299
328
300
329
audio_device = SDL_OpenAudioDevice (SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK , & audio_spec );
301
330
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 ());
303
332
}
304
333
305
334
capture_device = SDL_OpenAudioDevice (SDL_AUDIO_DEVICE_DEFAULT_RECORDING , & audio_spec );
306
335
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 ());
308
337
} else {
309
338
capture_stream = SDL_CreateAudioStream (& audio_spec , & audio_spec );
310
339
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 ());
312
341
SDL_CloseAudioDevice (capture_device );
313
342
capture_device = 0 ;
314
343
}
315
344
}
316
345
}
317
346
318
347
/* 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 );
320
350
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 ());
322
352
} else {
323
353
if (simulate_failure ) {
324
354
SDLNet_SimulateDatagramPacketLoss (sock , simulate_failure );
@@ -345,17 +375,17 @@ static void run_voipchat(int argc, char **argv)
345
375
int main (int argc , char * * argv )
346
376
{
347
377
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 ());
349
379
return 1 ;
350
380
}
351
381
352
382
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 ());
354
384
SDL_Quit ();
355
385
return 1 ;
356
386
}
357
387
358
- window = SDL_CreateWindow ("SDL_Net3 voipchat example" , 640 , 480 , 0 );
388
+ window = SDL_CreateWindow ("SDL3_Net voipchat example" , 640 , 480 , 0 );
359
389
renderer = SDL_CreateRenderer (window , NULL );
360
390
SDL_SetRenderDrawColor (renderer , 0 , 0 , 0 , 255 );
361
391
0 commit comments