2017-12-12 22:12:07 +08:00
|
|
|
#include "netutil.h"
|
|
|
|
|
|
|
|
#include <SDL2/SDL_net.h>
|
|
|
|
|
|
|
|
// contrary to SDLNet_TCP_Send and SDLNet_TCP_Recv, SDLNet_TCP_Accept is non-blocking
|
|
|
|
// so we need to block before calling it
|
2018-02-09 22:18:09 +08:00
|
|
|
TCPsocket server_socket_accept(TCPsocket server_socket, Uint32 timeout_ms) {
|
2017-12-12 22:12:07 +08:00
|
|
|
SDLNet_SocketSet set = SDLNet_AllocSocketSet(1);
|
|
|
|
if (!set) {
|
2018-02-12 23:06:53 +08:00
|
|
|
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "Could not allocate socket set");
|
2017-12-12 22:12:07 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SDLNet_TCP_AddSocket(set, server_socket) == -1) {
|
2018-02-12 23:06:53 +08:00
|
|
|
SDL_LogCritical(SDL_LOG_CATEGORY_APPLICATION, "Could not add socket to set");
|
2017-12-12 22:12:07 +08:00
|
|
|
SDLNet_FreeSocketSet(set);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-02-09 22:18:09 +08:00
|
|
|
if (SDLNet_CheckSockets(set, timeout_ms) != 1) {
|
2018-02-12 23:06:53 +08:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "No connection to accept");
|
2017-12-12 22:12:07 +08:00
|
|
|
SDLNet_FreeSocketSet(set);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDLNet_FreeSocketSet(set);
|
|
|
|
|
|
|
|
return SDLNet_TCP_Accept(server_socket);
|
|
|
|
}
|