changeset 51:cf7789d88350

Fix the input packet handling to properly handle packets with multiple messages .. finally.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 03 Nov 2008 19:01:32 +0200
parents d08bf3b6561d
children 8d9c065241b7
files nnchat.c
diffstat 1 files changed, 18 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/nnchat.c	Sun Nov 02 18:43:38 2008 +0200
+++ b/nnchat.c	Mon Nov 03 19:01:32 2008 +0200
@@ -1034,7 +1034,7 @@
 const int nprotoCmds = (sizeof(protoCmds) / sizeof(protoCmds[0]));
 
 
-int handleProtocol(int sock, char *buf, size_t bufLen)
+int handleProtocol(const int sock, char *buf, size_t bufLen)
 {
 	int i;
 	
@@ -1049,7 +1049,7 @@
 }
 
 
-int handleUserInput(int sock, char *buf, size_t bufLen)
+int handleUserInput(const int sock, char *buf, size_t bufLen)
 {
 	char *tmpStr, *tmpStr2, tmpBuf[4096];
 	BOOL result;
@@ -1311,6 +1311,7 @@
 		} else if (FD_ISSET(tmpSocket, &tmpfds)) {
 			ssize_t gotBuf;
 			char tmpBuf[8192];
+			char *bufPtr = tmpBuf;
 			gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0);
 			
 			if (gotBuf < 0) {
@@ -1322,16 +1323,22 @@
 			} else {
 				/* Handle protocol data */
 				tmpBuf[gotBuf] = 0;
-				result = handleProtocol(tmpSocket, tmpBuf, gotBuf);
+				do {
+					size_t bufLen = strlen(bufPtr) + 1;
+					result = handleProtocol(tmpSocket, bufPtr, bufLen);
 				
-				if (result > 0) {
-					/* Couldn't handle the message for some reason */
-					printMsg("Could not handle: %s\n", tmpBuf);
-				} else if (result < 0) {
-					/* Fatal error, quit */
-					printMsg("Fatal error with message: %s\n", tmpBuf);
-					isError = TRUE;
-				}
+					if (result > 0) {
+						/* Couldn't handle the message for some reason */
+						printMsg("Could not handle: %s\n", tmpBuf);
+					} else if (result < 0) {
+						/* Fatal error, quit */
+						printMsg("Fatal error with message: %s\n", tmpBuf);
+						isError = TRUE;
+					}
+					
+					gotBuf -= bufLen;
+					bufPtr += bufLen;
+				} while (gotBuf > 0 && !isError);
 				updateStatus(insertMode);
 			}
 		}