void handleClientRequest(int i) { ssize_t r; char * p; size_t remain; if (stream[i].len >= sizeof(stream[i].buffer) - 2) { logAbort("Input line on stream %d too long: %s\n", i, stream[i].buffer); } remain = sizeof(stream[i].buffer) - stream[i].len - 2; logDebug("handleClientRequest: read i=%d\n", i); logDebug("read %s i=%d fd=%d len=%u remain=%u\n", streamTypeName[stream[i].type], i, stream[i].fd, stream[i].len, remain); r = read(stream[i].fd, stream[i].buffer + stream[i].len, remain); if (r <= 0) { logDebug("read %s i=%d fd=%d r=%d\n", streamTypeName[stream[i].type], i, stream[i].fd, r); if (stream[i].type == DATA_INPUT_STREAM) { logAbort("EOF on reading stdin\n"); } closeStream(i); return; } stream[i].len += r; stream[i].buffer[stream[i].len] = 0; while (stream[i].len > 0) { size_t len; p = strchr(stream[i].buffer, '\n'); if (!p) { break; } len = p - stream[i].buffer; sbAppendData(&tcpMessage, stream[i].buffer, len + 1); if (stream[i].type != DATA_INPUT_STREAM || stream[outputIdx].type == DATA_OUTPUT_COPY) { /* Send all TCP client input and the main stdin stream if the mode is -o */ /* directly to stdout */ sbAppendData(&outMessage, stream[i].buffer, len + 1); } *p = 0; if (storeMessage(stream[i].buffer, len)) { convertJSONToNMEA0183(&nmeaMessage, stream[i].buffer); } p++, len++; stream[i].len -= len; /* Now remove [buffer..p> == the entire line */ memmove(stream[i].buffer, p, stream[i].len + 1); } }
void handleClientRequest(int i) { ssize_t r; char * p; r = read(stream[i].fd, stream[i].buffer + stream[i].len, sizeof(stream[i].buffer) - 1 - stream[i].len); logDebug("read i=%d fd=%d r=%d\n", i, stream[i].fd, r); if (r < 0) { if (stream[i].fd == stdinfd) { logAbort("Read %d on reading stdin\n", r); } closeStream(i); return; } if (r == 0) { if (stream[i].type == DATA_INPUT_STREAM) { logAbort("EOF on reading stdin\n", r); } else { closeStream(i); } return; } stream[i].len += r; stream[i].buffer[stream[i].len] = 0; while (r > 0) { p = strchr(stream[i].buffer, '\n'); if (p) { size_t len = ++p - stream[i].buffer; /* Feed it into the NMEA2000 message handler */ char stash = *p; logDebug("Got msg='%1.*s'\n", len, stream[i].buffer); *p = 0; sbAppendData(&tcpMessage, stream[i].buffer, len); if (stream[i].type != DATA_INPUT_STREAM || stream[outputIdx].type == DATA_OUTPUT_COPY) { /* Send all TCP client input and the main stdin stream if the mode is -o */ /* directly to stdout */ sbAppendData(&outMessage, stream[i].buffer, len); } convertJSONToNMEA0183(&nmeaMessage, stream[i].buffer); storeMessage(stream[i].buffer, len); *p = stash; /* Now remove [buffer..p> */ memcpy(stream[i].buffer, p, strlen(p)); stream[i].len -= len; r -= len; } else { r = 0; } } }