/*! Get data from GDB * * @param buff - buffer for data * @param size - maximum number of bytes to read * * @return >0 - Number of bytes read \n * GDB_OK(0) - No data \n * -GDB_FATAL_ERROR - Unexpected error */ int GdbInOutSocket::getData(unsigned char *buffer, int size) { memset(buffer, 0, size); if (activeSocket < 0) { connectionActive = false; return -GDB_FATAL_ERROR; } int rc = waitForActivity(activeSocket, 0); if (rc == -GDB_NON_FATAL_ERROR) { // No data available return 0; } if (rc != GDB_OK) { return rc; } rc = recv(activeSocket, (char*)buffer, D_INFO, 0); if ((rc < 0) && (NET_ERROR_NUM != EWOULDBLOCK)) { fprintf(stderr, " recv() failed. Error = %d\n", NET_ERROR_NUM); return -GDB_FATAL_ERROR; } if (rc > 0) { return rc; } return -GDB_NON_FATAL_ERROR; }
// main loop int main(int argc, char *argv[]) { char buffer[MAXBUF]; // character buffer used to receive messages int i; // iterator printf("\n"); printf(" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n"); printf(" | |\n"); printf(" * Warhammer On The Road *\n"); printf(" | |\n"); printf(" * Server *\n"); printf(" | |\n"); printf(" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*\n"); printf("\n"); int sock_serv = init(argc, argv); /* Enter an infinite loop to respond to client requests and echo input */ while(1) { printf("\r "); printf("\r> "); fflush(stdout); fd_set rfds; waitForActivity(&rfds, sock_serv); // Something happened in stdin if(FD_ISSET(fileno(stdin), &rfds)) { fgets(buffer, sizeof(buffer), stdin); if(parseStdin(buffer) < 0) break; } // If something happened on the listening socket if(FD_ISSET(sock_serv, &rfds)) acceptNewSocket(sock_serv); // if something happened on a client socket for(i = 0 ; i < nb_client ; i ++) if(FD_ISSET(players[i]->fd, &rfds)) parseClientRequest(players[i], buffer); // if a child process tries to communicate for(i=0; i<nb_file_processes; i++) if(FD_ISSET(fileprocesses[i]->in, &rfds)) parseFileProcessesRequest(i, buffer); } printf("\r"); clean(sock_serv); return 0; }
bool SocketServer::serve() { while (!stopped && !errored) { waitForActivity(); checkForNewConnections(); handleMessages(); } for (auto& clientList : clientLists) { for (auto& client : clientList->clients) { client.close(); } } return !errored; }
int doSession(int listenSocket) { int serverSocket = INVALID_SOCKET; // Connection to the server (gdbserver) int clientSocket = INVALID_SOCKET; // Connection to the client (GDB) int rc = GDB_OK; // Wait for connection from GDB do { rc = waitForConnection(listenSocket, &clientSocket); } while (rc == GDB_NON_FATAL_ERROR); if (rc == GDB_FATAL_ERROR) { return GDB_FATAL_ERROR; } // Connect to gdbserver if (createSocket(&serverSocket, -1, FALSE) != GDB_OK) { return GDB_FATAL_ERROR; } if (connectToServer(serverSocket) != GDB_OK) { return GDB_FATAL_ERROR; } // Transfer data (GDB Session) do { struct timeval sendTime; gettimeofday(&sendTime, NULL); char buffer[4000]; int size = 0; int activeSocket = waitForActivity(clientSocket, serverSocket); if (activeSocket == clientSocket) { gettimeofday(&sendTime, NULL); size = getData(TRUE, 0, clientSocket, buffer, sizeof(buffer)); if (size > 0) { sendData(serverSocket, buffer, size); } else if (size == -GDB_FATAL_ERROR) { rc = GDB_FATAL_ERROR; } } else if (activeSocket == serverSocket) { struct timeval currentTime; gettimeofday(¤tTime, NULL); double elapsedTime = timeval_subtract(¤tTime, &sendTime); size = getData(FALSE, elapsedTime, serverSocket, buffer, sizeof(buffer)); if (size > 0) { sendData(clientSocket, buffer, size); } else if (size == -GDB_FATAL_ERROR) { rc = GDB_FATAL_ERROR; } } else if (activeSocket == -GDB_FATAL_ERROR) { rc = GDB_FATAL_ERROR; } } while ((rc != GDB_FATAL_ERROR)); if (clientSocket >= 0) { closesocket(clientSocket); } if (serverSocket >= 0) { closesocket(serverSocket); } return GDB_NON_FATAL_ERROR; }