project_files/frontlib/ipc/ipcbase.c
changeset 7576 65d29988fd3d
parent 7316 f7b49b2c5d84
child 8071 7d6aaba59505
--- a/project_files/frontlib/ipc/ipcbase.c	Sat Aug 18 21:05:30 2012 +0200
+++ b/project_files/frontlib/ipc/ipcbase.c	Sun Aug 19 15:05:23 2012 +0200
@@ -74,6 +74,9 @@
 	if(ipc) {
 		flib_acceptor_close(ipc->acceptor);
 		flib_socket_close(ipc->sock);
+		if(ipc->sock) {
+			flib_log_d("IPC connection closed.");
+		}
 		free(ipc);
 	}
 }
@@ -96,6 +99,7 @@
 		if(size>=0) {
 			ipc->readBufferSize += size;
 		} else {
+			flib_log_d("IPC connection lost.");
 			flib_socket_close(ipc->sock);
 			ipc->sock = NULL;
 		}
@@ -106,6 +110,35 @@
 	return ipc->readBufferSize >= ipc->readBuffer[0]+1;
 }
 
+static void logSentMsg(const uint8_t *data, size_t len) {
+	if(flib_log_isActive(FLIB_LOGLEVEL_DEBUG)) {
+		size_t msgStart = 0;
+		while(msgStart < len) {
+			uint8_t msglen = data[msgStart];
+			if(msgStart+msglen < len) {
+				flib_log_d("[IPC OUT][%03u]%*.*s",(unsigned)msglen, (unsigned)msglen, (unsigned)msglen, data+msgStart+1);
+			} else {
+				uint8_t msglen2 = len-msgStart-1;
+				flib_log_d("[IPC OUT][%03u/%03u]%*.*s",(unsigned)msglen2, (unsigned)msglen, (unsigned)msglen2, (unsigned)msglen2, data+msgStart+1);
+			}
+			msgStart += (uint8_t)data[msgStart]+1;
+		}
+	}
+}
+
+static void logRecvMsg(const uint8_t *data) {
+	if(flib_log_isActive(FLIB_LOGLEVEL_DEBUG)) {
+		uint8_t msglen = data[0];
+		flib_log_d("[IPC IN][%03u]%*.*s",(unsigned)msglen, (unsigned)msglen, (unsigned)msglen, data+1);
+	}
+}
+
+static void popFromReadBuffer(flib_ipcbase *ipc, uint8_t *outbuf, size_t size) {
+	memcpy(outbuf, ipc->readBuffer, size);
+	memmove(ipc->readBuffer, ipc->readBuffer+size, ipc->readBufferSize-size);
+	ipc->readBufferSize -= size;
+}
+
 int flib_ipcbase_recv_message(flib_ipcbase *ipc, void *data) {
 	if(log_badargs_if2(ipc==NULL, data==NULL)) {
 		return -1;
@@ -117,9 +150,8 @@
 
 	if(isMessageReady(ipc)) {
 		int msgsize = ipc->readBuffer[0]+1;
-		memcpy(data, ipc->readBuffer, msgsize);
-		memmove(ipc->readBuffer, ipc->readBuffer+msgsize, ipc->readBufferSize-msgsize);
-		ipc->readBufferSize -= msgsize;
+		popFromReadBuffer(ipc, data, msgsize);
+		logRecvMsg(data);
 		return msgsize;
 	} else if(!ipc->sock && ipc->readBufferSize>0) {
 		flib_log_w("Last message from engine data stream is incomplete (received %u of %u bytes)", (unsigned)ipc->readBufferSize, (unsigned)(ipc->readBuffer[0])+1);
@@ -138,30 +170,13 @@
 	receiveToBuffer(ipc);
 
 	if(ipc->readBufferSize >= IPCBASE_MAPMSG_BYTES) {
-		memcpy(data, ipc->readBuffer, IPCBASE_MAPMSG_BYTES);
-		memmove(ipc->readBuffer, ipc->readBuffer+IPCBASE_MAPMSG_BYTES, ipc->readBufferSize-IPCBASE_MAPMSG_BYTES);
+		popFromReadBuffer(ipc, data, IPCBASE_MAPMSG_BYTES);
 		return IPCBASE_MAPMSG_BYTES;
 	} else {
 		return -1;
 	}
 }
 
-static void logSentMsg(const uint8_t *data, size_t len) {
-	if(flib_log_isActive(FLIB_LOGLEVEL_DEBUG)) {
-		size_t msgStart = 0;
-		while(msgStart < len) {
-			uint8_t msglen = data[msgStart];
-			if(msgStart+msglen < len) {
-				flib_log_d("[IPC OUT][%03u]%*.*s",(unsigned)msglen, (unsigned)msglen, (unsigned)msglen, data+msgStart+1);
-			} else {
-				uint8_t msglen2 = len-msgStart-1;
-				flib_log_d("[IPC OUT][%03u/%03u]%*.*s",(unsigned)msglen2, (unsigned)msglen, (unsigned)msglen2, (unsigned)msglen2, data+msgStart+1);
-			}
-			msgStart += (uint8_t)data[msgStart]+1;
-		}
-	}
-}
-
 int flib_ipcbase_send_raw(flib_ipcbase *ipc, const void *data, size_t len) {
 	if(log_badargs_if2(ipc==NULL, data==NULL && len>0)
 			|| log_w_if(!ipc->sock, "flib_ipcbase_send_raw: Not connected.")) {
@@ -193,6 +208,7 @@
 	if(!log_badargs_if(ipc==NULL) && !ipc->sock && ipc->acceptor) {
 		ipc->sock = flib_socket_accept(ipc->acceptor, true);
 		if(ipc->sock) {
+			flib_log_d("IPC connection accepted.");
 			flib_acceptor_close(ipc->acceptor);
 			ipc->acceptor = NULL;
 		}