project_files/frontlib/ipc/ipcconn.c
changeset 7224 5143861c83bd
parent 7182 076aba32abd3
child 7227 1c859f572d72
--- a/project_files/frontlib/ipc/ipcconn.c	Mon Jun 11 00:06:22 2012 +0200
+++ b/project_files/frontlib/ipc/ipcconn.c	Tue Jun 12 11:25:05 2012 +0200
@@ -1,6 +1,6 @@
 #include "ipcconn.h"
-#include "demo.h"
 #include "../util/logging.h"
+#include "../util/util.h"
 #include "../socket.h"
 
 #include <string.h>
@@ -17,25 +17,21 @@
  */
 typedef struct _flib_ipcconn {
 	uint8_t readBuffer[8192];
-	char playerName[256];
-
 	int readBufferSize;
 
-	flib_acceptor acceptor;
+	flib_acceptor *acceptor;
 	uint16_t port;
 
-	flib_tcpsocket sock;
-	flib_vector demoBuffer;
+	flib_tcpsocket *sock;
 } _flib_ipcconn;
 
-flib_ipcconn flib_ipcconn_create(bool recordDemo, const char *localPlayerName) {
-	flib_ipcconn result = malloc(sizeof(_flib_ipcconn));
-	flib_acceptor acceptor = flib_acceptor_create(0);
+flib_ipcconn *flib_ipcconn_create() {
+	flib_ipcconn *result = flib_malloc(sizeof(_flib_ipcconn));
+	flib_acceptor *acceptor = flib_acceptor_create(0);
 
 	if(!result || !acceptor) {
-		flib_log_e("Can't create ipcconn.");
 		free(result);
-		flib_acceptor_close(&acceptor);
+		flib_acceptor_close(acceptor);
 		return NULL;
 	}
 
@@ -44,44 +40,29 @@
 	result->readBufferSize = 0;
 	result->port = flib_acceptor_listenport(acceptor);
 
-	if(localPlayerName) {
-		strncpy(result->playerName, localPlayerName, 255);
-	} else {
-		strncpy(result->playerName, "Player", 255);
-	}
-
-	if(recordDemo) {
-		result->demoBuffer = flib_vector_create();
-	}
-
 	flib_log_i("Started listening for IPC connections on port %u", (unsigned)result->port);
 	return result;
 }
 
-uint16_t flib_ipcconn_port(flib_ipcconn ipc) {
+uint16_t flib_ipcconn_port(flib_ipcconn *ipc) {
 	if(!ipc) {
-		flib_log_e("Call to flib_ipcconn_port with ipc==null");
+		flib_log_e("null parameter in flib_ipcconn_port");
 		return 0;
 	}
 	return ipc->port;
 }
 
-void flib_ipcconn_destroy(flib_ipcconn *ipcptr) {
-	if(!ipcptr) {
-		flib_log_e("Call to flib_ipcconn_destroy with ipcptr==null");
-	} else if(*ipcptr) {
-		flib_ipcconn ipc = *ipcptr;
-		flib_acceptor_close(&ipc->acceptor);
-		flib_socket_close(&ipc->sock);
-		flib_vector_destroy(&ipc->demoBuffer);
+void flib_ipcconn_destroy(flib_ipcconn *ipc) {
+	if(ipc) {
+		flib_acceptor_close(ipc->acceptor);
+		flib_socket_close(ipc->sock);
 		free(ipc);
-		*ipcptr = NULL;
 	}
 }
 
-IpcConnState flib_ipcconn_state(flib_ipcconn ipc) {
+IpcConnState flib_ipcconn_state(flib_ipcconn *ipc) {
 	if(!ipc) {
-		flib_log_e("Call to flib_ipcconn_state with ipc==null");
+		flib_log_e("null parameter in flib_ipcconn_state");
 		return IPC_NOT_CONNECTED;
 	} else if(ipc->sock) {
 		return IPC_CONNECTED;
@@ -92,24 +73,25 @@
 	}
 }
 
-static bool isMessageReady(flib_ipcconn ipc) {
+static bool isMessageReady(flib_ipcconn *ipc) {
 	return ipc->readBufferSize >= ipc->readBuffer[0]+1;
 }
 
-static void receiveToBuffer(flib_ipcconn ipc) {
+static void receiveToBuffer(flib_ipcconn *ipc) {
 	if(ipc->sock) {
 		int size = flib_socket_nbrecv(ipc->sock, ipc->readBuffer+ipc->readBufferSize, sizeof(ipc->readBuffer)-ipc->readBufferSize);
 		if(size>=0) {
 			ipc->readBufferSize += size;
 		} else {
-			flib_socket_close(&ipc->sock);
+			flib_socket_close(ipc->sock);
+			ipc->sock = NULL;
 		}
 	}
 }
 
-int flib_ipcconn_recv_message(flib_ipcconn ipc, void *data) {
+int flib_ipcconn_recv_message(flib_ipcconn *ipc, void *data) {
 	if(!ipc || !data) {
-		flib_log_e("Call to flib_ipcconn_recv_message with ipc==null or data==null");
+		flib_log_e("null parameter in flib_ipcconn_recv_message");
 		return -1;
 	}
 
@@ -118,12 +100,6 @@
 	}
 
 	if(isMessageReady(ipc)) {
-		if(ipc->demoBuffer) {
-			if(flib_demo_record_from_engine(ipc->demoBuffer, ipc->readBuffer, ipc->playerName) < 0) {
-				flib_log_w("Stopping demo recording due to an error.");
-				flib_vector_destroy(&ipc->demoBuffer);
-			}
-		}
 		int msgsize = ipc->readBuffer[0]+1;
 		memcpy(data, ipc->readBuffer, msgsize);
 		memmove(ipc->readBuffer, ipc->readBuffer+msgsize, ipc->readBufferSize-msgsize);
@@ -138,9 +114,9 @@
 	}
 }
 
-int flib_ipcconn_recv_map(flib_ipcconn ipc, void *data) {
+int flib_ipcconn_recv_map(flib_ipcconn *ipc, void *data) {
 	if(!ipc || !data) {
-		flib_log_e("Call to flib_ipcconn_recv_map with ipc==null or data==null");
+		flib_log_e("null parameter in flib_ipcconn_recv_map");
 		return -1;
 	}
 
@@ -156,7 +132,7 @@
 }
 
 static void logSentMsg(const uint8_t *data, size_t len) {
-	if(flib_log_getLevel() > FLIB_LOGLEVEL_DEBUG) {
+	if(flib_log_isActive(FLIB_LOGLEVEL_DEBUG)) {
 		size_t msgStart = 0;
 		while(msgStart < len) {
 			uint8_t msglen = data[msgStart];
@@ -171,9 +147,9 @@
 	}
 }
 
-int flib_ipcconn_send_raw(flib_ipcconn ipc, const void *data, size_t len) {
+int flib_ipcconn_send_raw(flib_ipcconn *ipc, const void *data, size_t len) {
 	if(!ipc || (!data && len>0)) {
-		flib_log_e("Call to flib_ipcconn_send_raw with ipc==null or data==null");
+		flib_log_e("null parameter in flib_ipcconn_send_raw");
 		return -1;
 	}
 	if(!ipc->sock) {
@@ -183,23 +159,21 @@
 
 	if(flib_socket_send(ipc->sock, data, len) == len) {
 		logSentMsg(data, len);
-		if(ipc->demoBuffer) {
-			if(flib_demo_record_to_engine(ipc->demoBuffer, data, len) < 0) {
-				flib_log_w("Stopping demo recording due to an error.");
-				flib_vector_destroy(&ipc->demoBuffer);
-			}
-		}
 		return 0;
 	} else {
 		flib_log_w("Failed or incomplete ICP write: engine connection lost.");
-		flib_socket_close(&ipc->sock);
+		flib_socket_close(ipc->sock);
+		ipc->sock = NULL;
 		return -1;
 	}
 }
 
-int flib_ipcconn_send_message(flib_ipcconn ipc, void *data, size_t len) {
-	if(!ipc || (!data && len>0) || len>255) {
-		flib_log_e("Call to flib_ipcconn_send_message with ipc==null or data==null or len>255");
+int flib_ipcconn_send_message(flib_ipcconn *ipc, void *data, size_t len) {
+	if(!ipc || (!data && len>0)) {
+		flib_log_e("null parameter in flib_ipcconn_send_message");
+		return -1;
+	} else if(len>255) {
+		flib_log_e("Overlong message (%zu bytes) in flib_ipcconn_send_message", len);
 		return -1;
 	}
 
@@ -209,29 +183,18 @@
 	return flib_ipcconn_send_raw(ipc, sendbuf, len+1);
 }
 
-int flib_ipcconn_send_messagestr(flib_ipcconn ipc, char *data) {
+int flib_ipcconn_send_messagestr(flib_ipcconn *ipc, char *data) {
 	return flib_ipcconn_send_message(ipc, data, strlen(data));
 }
 
-void flib_ipcconn_accept(flib_ipcconn ipc) {
+void flib_ipcconn_accept(flib_ipcconn *ipc) {
 	if(!ipc) {
-		flib_log_e("Call to flib_ipcconn_accept with ipc==null");
+		flib_log_e("null parameter in flib_ipcconn_accept");
 	} else if(!ipc->sock && ipc->acceptor) {
 		ipc->sock = flib_socket_accept(ipc->acceptor, true);
 		if(ipc->sock) {
-			flib_acceptor_close(&ipc->acceptor);
+			flib_acceptor_close(ipc->acceptor);
+			ipc->acceptor = NULL;
 		}
 	}
 }
-
-flib_constbuffer flib_ipcconn_getrecord(flib_ipcconn ipc, bool save) {
-	if(!ipc) {
-		flib_log_e("Call to flib_ipcconn_getrecord with ipc==null");
-	}
-	if(!ipc || !ipc->demoBuffer) {
-		flib_constbuffer result = {NULL, 0};
-		return result;
-	}
-	flib_demo_replace_gamemode(flib_vector_as_buffer(ipc->demoBuffer), save ? 'S' : 'D');
-	return flib_vector_as_constbuffer(ipc->demoBuffer);
-}