# HG changeset patch # User Matti Hamalainen # Date 1225731692 -7200 # Node ID cf7789d88350c997b63120ca975fd9b0263d79ab # Parent d08bf3b6561d050001ea625ab0a39f0c43ddcb1a Fix the input packet handling to properly handle packets with multiple messages .. finally. diff -r d08bf3b6561d -r cf7789d88350 nnchat.c --- 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); } }