void CheckForNetworkEvents(int server_socket, int client_socket, std::vector<FifoFrameData>& frames, std::vector<AnalyzedFrameInfo>& analyzed_frames) { #if 0 fd_set readset; FD_ZERO(&readset); // FD_SET(server_socket, &readset); // if (client_socket != -1) FD_SET(client_socket, &readset); // int maxfd = std::max(client_socket, server_socket); int maxfd = client_socket; struct timeval timeout; timeout.tv_sec = 1; timeout.tv_usec = 0; char data[12]; int ret = net_select(maxfd+1, &readset, NULL, NULL, &timeout); // TODO: Is this compatible with winsocks? if (ret <= 0) { if (ret < 0) printf("select returned %d\n", ret); return; } /* if (FD_ISSET(server_socket, &readset)) { int new_socket = net_accept(server_socket, NULL, NULL); if (new_socket < 0) { qDebug() << "accept failed"; } else client_socket = new_socket; }*/ #endif struct pollsd fds[2]; memset(fds, 0, sizeof(fds)); // fds[0].socket = server_socket; fds[0].socket = client_socket; fds[0].events = POLLIN; int nfds = 1; int timeout = 1; // TODO: Set to zero int ret; do { ret = net_poll(fds, nfds, timeout); if (ret < 0) { printf("poll returned error %d\n", ret); return; } if (ret == 0) { printf("timeout :(\n"); // timeout return; } char cmd; ssize_t numread = net_recv(client_socket, &cmd, 1, 0); printf("Peeked command %d\n", cmd); switch (cmd) { case CMD_HANDSHAKE: if (RET_SUCCESS == ReadHandshake(client_socket)) printf("Successfully exchanged handshake token!\n"); else printf("Failed to exchange handshake token!\n"); // TODO: should probably write a handshake in return, but ... I'm lazy break; case CMD_STREAM_DFF: //ReadStreamedDff(client_socket); break; case CMD_ENABLE_COMMAND: case CMD_DISABLE_COMMAND: ReadCommandEnable(client_socket, analyzed_frames, (cmd == CMD_ENABLE_COMMAND) ? true : false); break; case CMD_PATCH_COMMAND: ReadCommandPatch(client_socket, frames); break; default: printf("Received unknown command: %d\n", cmd); break; } printf("Looping again\n"); timeout = 100; } while (ret > 0); }
//============================================================================ // NMessageServer::ServerThread : Server thread. //---------------------------------------------------------------------------- void NMessageServer::ServerThread(NSocket *theSocket) { NNetworkMessage msgServerInfo, msgConnectRequest, msgConnectResponse; NStatus theErr, acceptErr; NMessageHandshake clientHandshake; NString thePassword; NDictionary serverInfo; bool addClient; NEntityID clientID; // Validate our state NN_ASSERT(mStatus == kNServerStarted); // Do the handshake theErr = ReadHandshake(theSocket, clientHandshake); if (theErr == kNoErr) theErr = WriteHandshake(theSocket, CreateHandshake()); if (theErr == kNoErr) theErr = ValidateHandshake(clientHandshake); if (theErr != kNoErr) { theSocket->Close(theErr); return; } // Send the server info mLock.Lock(); serverInfo = GetConnectionInfo(); msgServerInfo = CreateMessage(kNMessageServerInfoMsg, kNEntityEveryone); msgServerInfo.SetProperties(serverInfo); mLock.Unlock(); theErr = WriteMessage(theSocket, msgServerInfo); if (theErr != kNoErr) { theSocket->Close(theErr); return; } // Read the connection request // // If the client does not wish to continue connecting, they will disconnect. theErr = ReadMessage(theSocket, msgConnectRequest); if (theErr != kNoErr) { theSocket->Close(); return; } NN_ASSERT(msgConnectRequest.GetType() == kNMessageConnectRequestMsg); NN_ASSERT(msgConnectRequest.GetSource() == kNEntityInvalid); // Accept the connection mLock.Lock(); clientID = GetNextClientID(); acceptErr = AcceptConnection(serverInfo, msgConnectRequest.GetProperties(), clientID); msgConnectResponse = CreateMessage(kNMessageConnectResponseMsg, clientID); msgConnectResponse.SetValue(kNMessageStatusKey, acceptErr); theErr = WriteMessage(theSocket, msgConnectResponse); addClient = (theErr == kNoErr && acceptErr == kNoErr); if (addClient) AddClient(clientID, theSocket); mLock.Unlock(); if (!addClient) { theSocket->Close(theErr); return; } // Process messages ClientConnected(clientID); ProcessMessages(theSocket); }