diff -r 7c2eb284f9f1 -r 038e3415100a project_files/frontlib/socket.c --- a/project_files/frontlib/socket.c Mon Jun 04 21:12:20 2012 +0200 +++ b/project_files/frontlib/socket.c Thu Jun 07 02:45:18 2012 +0200 @@ -23,10 +23,30 @@ return get_peer_ip(sock) == (uint32_t)((127UL<<24)+1); // 127.0.0.1 } +static flib_tcpsocket flib_socket_create(TCPsocket sdlsock) { + flib_tcpsocket result = malloc(sizeof(_flib_tcpsocket)); + if(!result) { + flib_log_e("Can't allocate socket: Out of memory!"); + return NULL; + } + result->sock = sdlsock; + result->sockset = SDLNet_AllocSocketSet(1); + + if(!result->sockset) { + flib_log_e("Can't allocate socket: Out of memory!"); + SDLNet_FreeSocketSet(result->sockset); + free(result); + return NULL; + } + + SDLNet_AddSocket(result->sockset, (SDLNet_GenericSocket)result->sock); + return result; +} + flib_acceptor flib_acceptor_create(uint16_t port) { flib_acceptor result = malloc(sizeof(_flib_acceptor)); if(!result) { - flib_log_e("Out of memory!"); + flib_log_e("Can't allocate acceptor: Out of memory!"); return NULL; } @@ -47,7 +67,6 @@ } else { /* SDL_net does not seem to have a way to listen on a random unused port and find out which port that is, so let's try to find one ourselves. */ - // TODO: Is socket binding fail-fast on all platforms? srand(time(NULL)); rand(); for(int i=0; i<1000; i++) { @@ -58,7 +77,7 @@ if(result->sock) { return result; } else { - flib_log_i("Unable to listen on port %u: %s", result->port, SDLNet_GetError()); + flib_log_w("Unable to listen on port %u: %s", result->port, SDLNet_GetError()); } } flib_log_e("Unable to listen on a random unused port."); @@ -68,68 +87,60 @@ } uint16_t flib_acceptor_listenport(flib_acceptor acceptor) { + if(!acceptor) { + flib_log_e("Call to flib_acceptor_listenport with acceptor==null"); + return 0; + } return acceptor->port; } void flib_acceptor_close(flib_acceptor *acceptorptr) { - if(acceptorptr == NULL || *acceptorptr == NULL) { - return; + if(!acceptorptr) { + flib_log_e("Call to flib_acceptor_close with acceptorptr==null"); + } else if(*acceptorptr) { + SDLNet_TCP_Close((*acceptorptr)->sock); + free(*acceptorptr); + *acceptorptr = NULL; } - SDLNet_TCP_Close((*acceptorptr)->sock); - free(*acceptorptr); - *acceptorptr = NULL; } flib_tcpsocket flib_socket_accept(flib_acceptor acceptor, bool localOnly) { - flib_tcpsocket result = NULL; if(!acceptor) { + flib_log_e("Call to flib_socket_accept with acceptor==null"); return NULL; } - while(result==NULL) { - TCPsocket sock = SDLNet_TCP_Accept(acceptor->sock); - if(!sock) { - // No incoming connections - return NULL; - } + flib_tcpsocket result = NULL; + TCPsocket sock = NULL; + while(!result && (sock = SDLNet_TCP_Accept(acceptor->sock))) { if(localOnly && !connection_is_local(sock)) { flib_log_i("Rejected nonlocal connection attempt from %s", flib_format_ip(get_peer_ip(sock))); SDLNet_TCP_Close(sock); } else { - result = malloc(sizeof(_flib_tcpsocket)); - if(result==NULL) { - flib_log_e("Out of memory!"); + result = flib_socket_create(sock); + if(!result) { SDLNet_TCP_Close(sock); - return NULL; } - result->sock = sock; - result->sockset = SDLNet_AllocSocketSet(1); - if(result->sockset==NULL) { - flib_log_e("Out of memory!"); - SDLNet_TCP_Close(sock); - free(result); - return NULL; - } - SDLNet_AddSocket(result->sockset, (SDLNet_GenericSocket)result->sock); } } return result; } void flib_socket_close(flib_tcpsocket *sockptr) { - if(sockptr==NULL || *sockptr == NULL) { - return; + if(!sockptr) { + flib_log_e("Call to flib_socket_close with sockptr==null"); + } else if(*sockptr) { + flib_tcpsocket sock = *sockptr; + SDLNet_DelSocket(sock->sockset, (SDLNet_GenericSocket)sock->sock); + SDLNet_TCP_Close(sock->sock); + SDLNet_FreeSocketSet(sock->sockset); + free(sock); + *sockptr = NULL; } - flib_tcpsocket sock = *sockptr; - SDLNet_DelSocket(sock->sockset, (SDLNet_GenericSocket)sock->sock); - SDLNet_TCP_Close(sock->sock); - SDLNet_FreeSocketSet(sock->sockset); - free(sock); - *sockptr = NULL; } int flib_socket_nbrecv(flib_tcpsocket sock, void *data, int maxlen) { - if(!sock) { - flib_log_e("Attempt to receive on a NULL socket."); + if(!sock || (maxlen>0 && !data)) { + flib_log_e("Call to flib_socket_nbrecv with sock==null or data==null"); return -1; } int readySockets = SDLNet_CheckSockets(sock->sockset, 0); @@ -145,5 +156,9 @@ } int flib_socket_send(flib_tcpsocket sock, void *data, int len) { + if(!sock || (len>0 && !data)) { + flib_log_e("Call to flib_socket_send with sock==null or data==null"); + return -1; + } return SDLNet_TCP_Send(sock->sock, data, len); }