comparison nnchat.c @ 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 7b98da167a0b
comparison
equal deleted inserted replaced
50:d08bf3b6561d 51:cf7789d88350
1032 }; 1032 };
1033 1033
1034 const int nprotoCmds = (sizeof(protoCmds) / sizeof(protoCmds[0])); 1034 const int nprotoCmds = (sizeof(protoCmds) / sizeof(protoCmds[0]));
1035 1035
1036 1036
1037 int handleProtocol(int sock, char *buf, size_t bufLen) 1037 int handleProtocol(const int sock, char *buf, size_t bufLen)
1038 { 1038 {
1039 int i; 1039 int i;
1040 1040
1041 for (i = 0; i < nprotoCmds; i++) { 1041 for (i = 0; i < nprotoCmds; i++) {
1042 size_t cmdLen = strlen(protoCmds[i].cmd); 1042 size_t cmdLen = strlen(protoCmds[i].cmd);
1047 1047
1048 return 1; 1048 return 1;
1049 } 1049 }
1050 1050
1051 1051
1052 int handleUserInput(int sock, char *buf, size_t bufLen) 1052 int handleUserInput(const int sock, char *buf, size_t bufLen)
1053 { 1053 {
1054 char *tmpStr, *tmpStr2, tmpBuf[4096]; 1054 char *tmpStr, *tmpStr2, tmpBuf[4096];
1055 BOOL result; 1055 BOOL result;
1056 1056
1057 /* Trim right */ 1057 /* Trim right */
1309 strerror(errno)); 1309 strerror(errno));
1310 isError = TRUE; 1310 isError = TRUE;
1311 } else if (FD_ISSET(tmpSocket, &tmpfds)) { 1311 } else if (FD_ISSET(tmpSocket, &tmpfds)) {
1312 ssize_t gotBuf; 1312 ssize_t gotBuf;
1313 char tmpBuf[8192]; 1313 char tmpBuf[8192];
1314 char *bufPtr = tmpBuf;
1314 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0); 1315 gotBuf = recv(tmpSocket, tmpBuf, sizeof(tmpBuf), 0);
1315 1316
1316 if (gotBuf < 0) { 1317 if (gotBuf < 0) {
1317 printMsg("Error in recv: %s\n", strerror(errno)); 1318 printMsg("Error in recv: %s\n", strerror(errno));
1318 isError = TRUE; 1319 isError = TRUE;
1320 printMsg("Server closed connection.\n"); 1321 printMsg("Server closed connection.\n");
1321 isError = TRUE; 1322 isError = TRUE;
1322 } else { 1323 } else {
1323 /* Handle protocol data */ 1324 /* Handle protocol data */
1324 tmpBuf[gotBuf] = 0; 1325 tmpBuf[gotBuf] = 0;
1325 result = handleProtocol(tmpSocket, tmpBuf, gotBuf); 1326 do {
1327 size_t bufLen = strlen(bufPtr) + 1;
1328 result = handleProtocol(tmpSocket, bufPtr, bufLen);
1326 1329
1327 if (result > 0) { 1330 if (result > 0) {
1328 /* Couldn't handle the message for some reason */ 1331 /* Couldn't handle the message for some reason */
1329 printMsg("Could not handle: %s\n", tmpBuf); 1332 printMsg("Could not handle: %s\n", tmpBuf);
1330 } else if (result < 0) { 1333 } else if (result < 0) {
1331 /* Fatal error, quit */ 1334 /* Fatal error, quit */
1332 printMsg("Fatal error with message: %s\n", tmpBuf); 1335 printMsg("Fatal error with message: %s\n", tmpBuf);
1333 isError = TRUE; 1336 isError = TRUE;
1334 } 1337 }
1338
1339 gotBuf -= bufLen;
1340 bufPtr += bufLen;
1341 } while (gotBuf > 0 && !isError);
1335 updateStatus(insertMode); 1342 updateStatus(insertMode);
1336 } 1343 }
1337 } 1344 }
1338 1345
1339 /* Handle user input */ 1346 /* Handle user input */