project_files/frontlib/socket.c
changeset 7271 5608ac657362
parent 7234 613998625a3c
child 7314 6171f0bad318
--- a/project_files/frontlib/socket.c	Thu Jun 21 21:32:12 2012 +0200
+++ b/project_files/frontlib/socket.c	Mon Jun 25 00:42:07 2012 +0200
@@ -5,84 +5,77 @@
 #include <SDL_net.h>
 #include <time.h>
 
-typedef struct _flib_tcpsocket {
+struct _flib_tcpsocket {
 	TCPsocket sock;
 	SDLNet_SocketSet sockset;
-} _flib_tcpsocket;
+};
 
-typedef struct _flib_acceptor {
+struct _flib_acceptor {
 	TCPsocket sock;
 	uint16_t port;
-} _flib_acceptor;
+};
 
-static uint32_t get_peer_ip(TCPsocket sock) {
+static uint32_t getPeerIp(TCPsocket sock) {
 	IPaddress *addr = SDLNet_TCP_GetPeerAddress(sock);
 	return SDLNet_Read32(&addr->host);
 }
 
-static bool connection_is_local(TCPsocket sock) {
-	return get_peer_ip(sock) == (uint32_t)((127UL<<24)+1); // 127.0.0.1
+static bool connectionIsLocal(TCPsocket sock) {
+	return getPeerIp(sock) == (uint32_t)((127UL<<24)+1); // 127.0.0.1
 }
 
-static flib_tcpsocket *flib_socket_create(TCPsocket sdlsock) {
-	flib_tcpsocket *result = flib_calloc(1, sizeof(_flib_tcpsocket));
-	if(!result) {
-		return NULL;
-	}
-	result->sock = sdlsock;
-	result->sockset = SDLNet_AllocSocketSet(1);
+static flib_tcpsocket *createSocket(TCPsocket sdlsock) {
+	flib_tcpsocket *result = flib_calloc(1, sizeof(flib_tcpsocket));
+	if(result) {
+		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;
+		if(!result->sockset) {
+			flib_log_e("Can't allocate socket: Out of memory!");
+			SDLNet_FreeSocketSet(result->sockset);
+			free(result);
+			result = NULL;
+		} else {
+			SDLNet_AddSocket(result->sockset, (SDLNet_GenericSocket)result->sock);
+		}
 	}
+	return result;
+}
 
-	SDLNet_AddSocket(result->sockset, (SDLNet_GenericSocket)result->sock);
-	return result;
+TCPsocket listen(uint16_t port) {
+	IPaddress addr;
+	addr.host = INADDR_ANY;
+	SDLNet_Write16(port, &addr.port);
+	TCPsocket sock = SDLNet_TCP_Open(&addr);
+	if(!sock) {
+		flib_log_w("Unable to listen on port %u: %s", (unsigned)port, SDLNet_GetError());
+	}
+	return sock;
 }
 
 flib_acceptor *flib_acceptor_create(uint16_t port) {
-	flib_acceptor *result = flib_calloc(1, sizeof(_flib_acceptor));
-	if(!result) {
-		return NULL;
-	}
-
-	IPaddress addr;
-	addr.host = INADDR_ANY;
-
-	if(port > 0) {
-		result->port = port;
-		SDLNet_Write16(port, &addr.port);
-		result->sock = SDLNet_TCP_Open(&addr);
-		if(result->sock) {
-			return result;
+	flib_acceptor *result = flib_calloc(1, sizeof(flib_acceptor));
+	if(result) {
+		if(port > 0) {
+			result->port = port;
+			result->sock = listen(result->port);
 		} else {
-			flib_log_e("Unable to listen on port %u: %s", (unsigned)port, SDLNet_GetError());
-			free(result);
-			return NULL;
-		}
-	} 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. */
-		srand(time(NULL));
-		rand();
-		for(int i=0; i<1000; i++) {
-			// IANA suggests using ports in the range 49152-65535 for things like this
-			result->port = 49152+(rand()%(65535-49152));
-			SDLNet_Write16(result->port, &addr.port);
-			result->sock = SDLNet_TCP_Open(&addr);
-			if(result->sock) {
-				return result;
-			} else {
-				flib_log_w("Unable to listen on port %u: %s", (unsigned)result->port, SDLNet_GetError());
+			/* 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. */
+			srand(time(NULL));
+			for(int i=0; !result->sock && i<1000; i++) {
+				// IANA suggests using ports in the range 49152-65535 for things like this
+				result->port = 49152+(rand()%(65535-49152));
+				result->sock = listen(result->port);
 			}
 		}
-		flib_log_e("Unable to listen on a random unused port.");
-		free(result);
-		return NULL;
+		if(!result->sock) {
+			flib_log_e("Failed to create acceptor.");
+			free(result);
+			result = NULL;
+		}
 	}
+	return result;
 }
 
 uint16_t flib_acceptor_listenport(flib_acceptor *acceptor) {
@@ -101,18 +94,17 @@
 }
 
 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;
-	}
-	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 = flib_socket_create(sock);
+	} else {
+		TCPsocket sock = NULL;
+		while(!result && (sock = SDLNet_TCP_Accept(acceptor->sock))) {
+			if(localOnly && !connectionIsLocal(sock)) {
+				flib_log_i("Rejected nonlocal connection attempt from %s", flib_format_ip(getPeerIp(sock)));
+			} else {
+				result = createSocket(sock);
+			}
 			if(!result) {
 				SDLNet_TCP_Close(sock);
 			}
@@ -134,7 +126,7 @@
 			if(!sock) {
 				flib_log_e("SDLNet_TCP_Open: %s\n", SDLNet_GetError());
 			} else {
-				result = flib_socket_create(sock);
+				result = createSocket(sock);
 				if(result) {
 					sock = NULL;
 				}