void CheckForCommands() { int result = strlen(recv_buffer); if (result == 0) // No pending commands { // Keep going till the commands have been received completely (the last character is an endline; recv_buffer better be big enough for at least one entire command) do { memset(temp_buffer, 0, RECV_BUFFER_SIZE); result = NetRecvTCP(interfaceSocket, temp_buffer, RECV_BUFFER_SIZE - strlen(recv_buffer) - 1); if (result > 0) strcat(recv_buffer, temp_buffer); } while (result > 0 && (strlen(recv_buffer) > 0 && strlen(recv_buffer) < RECV_BUFFER_SIZE - 1 && recv_buffer[strlen(recv_buffer) - 1] != '\n')); } if (result > 0) HandleRequest(); else if (NetCheckForDisconnect()) { DebugPrint("Broken connection."); connected = false; NetCloseTCP(interfaceSocket); } }
/** ** Receive reply from Meta Server ** ** @param reply Text of the reply ** ** @return error or number of bytes */ int RecvMetaReply(char **reply) { if (NetSocketReady(MetaServerFildes, 5000) == -1) { return -1; } // FIXME: Allow for large packets char buf[1024]; int n = NetRecvTCP(MetaServerFildes, &buf, 1024); char *p = new char[n + 1]; if (!p) { return -1; } // We know we now have the whole command. // Convert to standard notation buf[n - 1] = '\0'; buf[n - 2] = '\n'; strcpy(p, buf); *reply = p; return n; }
int Recv(void *buf, int len) { int res = NetRecvTCP(socket, buf, len); return res; }