// Update loop - accepts new connections // Collects and stores data void ServerNetworkManager::update() { // get new clients do { unsigned int temp_c_id = client_id; if(acceptNewClient(temp_c_id)) { if(debugFlag) DC::get()->print("client %d has been connected to the server\n",client_id); PlayerSObj * o; if(temp_c_id == client_id) { // Create a Test Server Object for them (for now) SOM *som = SOM::get(); // Ok, since we should only have one object on both sides, the id's will match // but how do we get them matching later? maybe the server should send // the client the id back or something? o = new PlayerSObj(som->genId()); som->add(o); sessionsobjid.insert( pair<unsigned int, unsigned int>(temp_c_id, o->getId()) ); } else { o = reinterpret_cast<PlayerSObj *>(SOM::get()->find(sessionsobjid.find(temp_c_id)->second)); } this->getSendBuffer(); // Need to call this before each send, regardless of whether or not you have a message. this->sendToClient(sessions[temp_c_id], INIT_CONNECTION, o->getId(), 0); if(debugFlag) DC::get()->print("client %d has been assigned client_id... Moving onto the rest of the loop.\n",client_id); if(temp_c_id == client_id) { client_id++; } EventManager::get()->fireEvent(EVENT_CONNECTION, o); } } while (sessions.empty()); // Collect data from clients receiveFromClients(); iteration++; }
void TCPServer::start() { struct timeval tv; fd_set read_fds; m_running = true; while (m_running) { tv.tv_sec = 0; tv.tv_usec = 250000; read_fds = m_socks; int select_return = select(m_maxfd + 1, &read_fds, NULL, NULL, &tv); if (select_return == -1) { perror("Select failed!"); switch(errno) { case EBADF: // An invalid file descriptor was given in one of the sets. (Perhaps a file descriptor that was already closed, or one on which an error has occurred.) std::cout << "An invalid file descriptor was given in one of the sets. (Perhaps a file descriptor that was already closed, or one on which an error has occurred.)\n"; break; case EINTR: // A signal was caught; see signal(7). break; case EINVAL: // nfds is negative or the value contained within timeout is invalid. break; case ENOMEM: // unable to allocate memory for internal tables. throw Common::DisCODeException("TCPServe: Unable to allocate memory for internal tables"); } } if (select_return == 0) { //std::cout << "Select timed out." << std::endl; continue; } // run through the existing connections looking for data to read for (int i = 0; i <= m_maxfd; i++) { if (FD_ISSET(i, &read_fds)) { // we got one!! // check, if server listening socket is ready if (i == m_sock) { acceptNewClient(); } else { handleClient(i); } // END handle data from client } // END got new incoming connection } } }
int main(int argc, char const *argv[]) { int serverSocket = -1; in_port_t serverPort = 0; if (argc != 2) { fprintf(stderr, "Usage: %s <error>\n", argv[0]); exit(1); } serverSocket = startServer(&serverPort); if (serverSocket < 0) { exit(serverSocket); } double errorRate; sscanf(argv[1], "%lf", &errorRate); sendErr_init(errorRate, DROP_ON, FLIP_ON, DEBUG_OFF, RSEED_OFF); clientList->maxSocket = DEFAULT_MAX_SOCKET; printf("Server is using port %d\n", ntohs(serverPort)); for(;;) { fd_set fdSet; // static struct timeval timeout; // timeout.tv_sec = 1; // timeout.tv_usec = 0; FD_ZERO(&fdSet); FD_SET(serverSocket, &fdSet); setActiveClientsForSelect(&fdSet); if (select(clientList->maxSocket+1, &fdSet, NULL, NULL, NULL) < 0){ perror("main:select"); exit(-5); } else { if (FD_ISSET(serverSocket, &fdSet)) { acceptNewClient(serverSocket); } checkActiveClientsAfterSelect(&fdSet); } } close(serverSocket); return 0; }
void runloop(server_t* server) { struct pollfd pollDescriptors[kMaxClients + 1]; int socksToHandle; memset(pollDescriptors, 0, sizeof(struct pollfd)*kMaxClients + 1); int pollNumDescriptors = 1; pollDescriptors[0].fd = server->socket; pollDescriptors[0].events = POLLIN | POLLHUP; { int i = 1; for (int j = 0; j < kMaxClients; j++) { if (server->clients[j] != NULL) { pollDescriptors[i].fd = server->clients[j]->socket; pollDescriptors[i].events = POLLIN | POLLHUP; i++; pollNumDescriptors = i; } } } socksToHandle = poll(pollDescriptors, pollNumDescriptors, kPollTimeout); if (socksToHandle < 0) { server->running = false; perror("poll"); return; } else if (socksToHandle > 0) { for (int i = 0; i < pollNumDescriptors; i++) { if ((pollDescriptors[i].revents & POLLIN) > 0) { if (pollDescriptors[i].fd == server->socket) { if (!acceptNewClient(server)) { server->running = false; return; } } else { for (int j = 0; j < kMaxClients; j++) { if (server->clients[j] && server->clients[j]->socket == pollDescriptors[i].fd) { if (!processClient(server->clients[j])) { CloseClient(server->clients[j]); } } } } } if ((pollDescriptors[i].revents & POLLHUP) > 0) { if (pollDescriptors[i].fd == server->socket) { server->running = false; return; } else { for (int j = 0; j < kMaxClients; j++) { if (server->clients[j] && server->clients[j]->socket == pollDescriptors[i].fd) { CloseClient(server->clients[j]); } } } } } } }