///-----------MAIN LOOP-------------- void Server::MainLoop() { while(serverIsRunning) { if (SDLNet_UDP_Recv(sock, input)) //Check if the server is receiving data from the clients { if(RECEIVED_KEY == LOG_IN_KEY) { string username = GetQueryArgument(input->data, 2); char logInStatus[1]; logInStatus[0] = profile.LogIn(username, GetQueryArgument(input->data, 3)); if(logInStatus[0] == LOG_IN_SUCCESS) AddNewClient(input->address.host, input->address.port, username, input->channel); SendData((Uint8*)logInStatus, sizeof(logInStatus)); } else if(RECEIVED_KEY == REGISTER_KEY) { char registerStatus[1]; registerStatus[0] = profile.Register(GetQueryArgument(input->data, 2), GetQueryArgument(input->data, 3)); SendData((Uint8*)registerStatus, sizeof(registerStatus)); cout << "New profile registered: " << GetQueryArgument(input->data, 2) << endl; } else if(RECEIVED_KEY == KICK_KEY) { KickClient(input->address.host); } Server::SendBack(); receivedData = ""; } } }
/**************************************************************************** * Description: Process the VCM_CLIENTINFO message from a client. * Input: LPCTSTR lpData: The data string from client. * SOCKET sock: client connected socket. * Output: None. * Return: None. * Revision History: * 08/03/09 Author: DiepTH *****************************************************************************/ void CListener::OnMsgClientInfo(LPCTSTR lpData, SOCKET sock) { SOCKADDR_IN ClientAddr = {0}; int iNameLen; CToken token(lpData); CString strIP, strHostName, strVersion, strReleaseDate; BOOL bExistIP = FALSE; DWORD dwThreadId; LPCLIENT_INFO pClientInfo; iNameLen = sizeof(ClientAddr); if (getpeername(sock, (LPSOCKADDR)&ClientAddr, &iNameLen) == SOCKET_ERROR) { CloseConnection(sock); return; } strIP.Format(_T("%d.%d.%d.%d"), ClientAddr.sin_addr.S_un.S_un_b.s_b1, ClientAddr.sin_addr.S_un.S_un_b.s_b2, ClientAddr.sin_addr.S_un.S_un_b.s_b3, ClientAddr.sin_addr.S_un.S_un_b.s_b4); // parse the received data // lpData: <Host_Name>;<Client_Version>;<Release_Date>; token.SetToken(_T(";")); strHostName = token.GetNextToken(); strHostName.MakeUpper(); strVersion = token.GetNextToken(); strReleaseDate = token.GetNextToken(); // check existence or add a client (IP and Name) in the connection list pClientInfo = AddNewClient(sock, strIP, strHostName, strVersion, strReleaseDate); if (!pClientInfo) return; }
//---------------------------------------------------------------- bool SelectServer::WaitForClient(ClientVector& waitClientVec) throw(ConnectionException&) { int nReady; bool ret = false; waitClientVec.clear(); mCurSet = mInitSet; for(ClientVectorIter iter = mClientConnVec.begin(); iter!=mClientConnVec.end(); ++iter){ FD_SET(iter->GetSockfd(), &mCurSet); } /*only care read event*/ nReady = select(mMaxSockfd+1, &mCurSet, NULL, NULL, NULL); if(nReady == 0) return false;/*no new client*/ else if(nReady < 0){ throw ConnectionException("Error : select error\n"); } //check for listen socket if(FD_ISSET(mListenConn.GetSockfd(), &mCurSet)){/*new client in*/ ConnectionIPV4 cliConn ; if(mListenConn.AcceptClient(cliConn) == true){/*ignore error in accept*/ AddNewClient(cliConn); waitClientVec.push_back(cliConn); mMaxSockfd = mClientConnVec[mClientConnVec.size() - 1].GetSockfd(); ret = true; } --nReady; } //check for other socket for(ClientVectorIter iter = mClientConnVec.begin(); iter!=mClientConnVec.end() && nReady > 0; ++iter){ if(FD_ISSET(iter->GetSockfd(), &mCurSet)){ waitClientVec.push_back(*iter); --nReady; } } return ret; }