void GameState::ReceiveChatMessage(Packet *p) { ChatMessageStruct s = *((ChatMessageStruct *) p->data); assert(p->length >= sizeof(ChatMessageStruct)); directSound->PlaySound(soundContainer->sounds[RECEIVE_CHAT_MESSAGE_SOUND], false); if (rakClient->IsConnected()) AddChatMessage(((EnumerationDataStruct*)(rakClient->GetStaticServerData()->GetData()))->serverName, p->data+sizeof(ChatMessageStruct)); else AddChatMessage(((StaticClientDataStruct*)(rakServer->GetStaticClientData(p->playerId)->GetData()))->name, p->data+sizeof(ChatMessageStruct)); }
void ChatMessageCallback (const TTV_ChatMessageList* messageList, void* /*userdata*/) { assert (messageList); for (uint i = 0; i < messageList->messageCount; ++i) { AddChatMessage(&messageList->messageList[i]); } }
void GameState::KeyJustPressed(int key) { if (key==DIK_ESCAPE) manager->SetState(mainMenuState); else if (userChatMessageInput.GetUserInput()==false && key==DIK_SPACE && (currentState==GAME_LOST || currentState==GAME_WON || currentState==RESTART_WAITING_ON_LOCAL_SYSTEM)) { if (rakClient->IsConnected()==false && rakServer->IsActive()==false) RestartGame(); // Single player else { char c=ID_RESTART_GAME; if (rakClient->IsConnected()) rakClient->Send(&c, 1, HIGH_PRIORITY, RELIABLE, 0); else if (rakServer->IsActive()) rakServer->Send(&c, 1, HIGH_PRIORITY, RELIABLE, 0, 255, true, false); if (currentState==RESTART_WAITING_ON_LOCAL_SYSTEM) // They already indicated they wanted to start RestartGame(); else { currentState=RESTART_WAITING_ON_REMOTE_SYSTEM; // We are now waiting for them (game restarts from network code) strcpy(gameMessage, "Waiting for opponent to restart game."); } } } else if (CanChoosePerk()) { for (int i=0; i < numberOfPerksToChooseFrom; i++) if (key==DIK_1 + i) { GivePerkToPlayer(perks[i], user->GetTeam(), false); break; } } else if (rakServer->IsActive() || rakClient->IsConnected()) { userChatMessageInput.KeyJustPressed(key); if (key==DIK_RETURN) { TransmitChatMessage((char*)userChatMessageInput.GetUserInput()); AddChatMessage((char*)user->userName.GetUserInput(), (char*)userChatMessageInput.GetUserInput()); userChatMessageInput.SetText(0); userChatMessageInput.StartTextInput(); } } /* #ifdef _DEBUG if (key==DIK_L) { players[0].IncreaseExperience(10); players[1].IncreaseExperience(10); } #endif */ }
void NetPlayDialog::OnChat(wxCommandEvent&) { std::string text = WxStrToStr(m_chat_msg_text->GetValue()); if (!text.empty()) { netplay_client->SendChatMessage(text); m_chat_msg_text->Clear(); AddChatMessage(ChatMessageType::UserOut, text); } }
// update gui void NetPlayDialog::OnThread(wxThreadEvent& event) { if (m_is_hosting && m_host_label && g_TraversalClient) { UpdateHostLabel(); } // player list m_playerids.clear(); std::string tmps; netplay_client->GetPlayerList(tmps, m_playerids); wxString selection; if (m_player_lbox->GetSelection() != wxNOT_FOUND) selection = m_player_lbox->GetString(m_player_lbox->GetSelection()); m_player_lbox->Clear(); std::istringstream ss(tmps); while (std::getline(ss, tmps)) m_player_lbox->Append(StrToWxStr(tmps)); // remove ping from selection string, in case it has changed selection.erase(selection.rfind('|') + 1); if (!selection.empty()) { for (unsigned int i = 0; i < m_player_lbox->GetCount(); ++i) { if (selection == m_player_lbox->GetString(i).substr(0, selection.length())) { m_player_lbox->SetSelection(i); break; } } } // flash window in taskbar when someone joins if window isn't active static u8 numPlayers = 1; if (netplay_server != nullptr && numPlayers < m_playerids.size() && !HasFocus()) { RequestUserAttention(); } numPlayers = m_playerids.size(); switch (event.GetId()) { case NP_GUI_EVT_CHANGE_GAME: // update selected game :/ { m_selected_game = WxStrToStr(event.GetString()); wxString button_label = event.GetString(); m_game_btn->SetLabel(button_label.Prepend(_(" Game : "))); } break; case NP_GUI_EVT_START_GAME: // client start game :/ { netplay_client->StartGame(FindCurrentGame()); std::string msg = "Starting game"; AddChatMessage(ChatMessageType::Info, msg); } break; case NP_GUI_EVT_STOP_GAME: // client stop game { std::string msg = "Stopping game"; AddChatMessage(ChatMessageType::Info, msg); } break; case NP_GUI_EVT_DISPLAY_MD5_DIALOG: { m_MD5_dialog = new MD5Dialog(this, netplay_server, netplay_client->GetPlayers(), event.GetString().ToStdString()); m_MD5_dialog->Show(); } break; case NP_GUI_EVT_MD5_PROGRESS: { if (m_MD5_dialog == nullptr || m_MD5_dialog->IsBeingDeleted()) break; std::pair<int, int> payload = event.GetPayload<std::pair<int, int>>(); m_MD5_dialog->SetProgress(payload.first, payload.second); } break; case NP_GUI_EVT_MD5_RESULT: { if (m_MD5_dialog == nullptr || m_MD5_dialog->IsBeingDeleted()) break; std::pair<int, std::string> payload = event.GetPayload<std::pair<int, std::string>>(); m_MD5_dialog->SetResult(payload.first, payload.second); } break; case NP_GUI_EVT_PAD_BUFFER_CHANGE: { std::string msg = StringFromFormat("Buffer size: %d", m_pad_buffer); if (g_ActiveConfig.bShowNetPlayMessages) { OSD::AddTypedMessage(OSD::MessageType::NetPlayBuffer, msg, OSD::Duration::NORMAL); } AddChatMessage(ChatMessageType::Info, msg); } break; case NP_GUI_EVT_DESYNC: { std::string msg = "Possible desync detected from player " + m_desync_player + " on frame " + std::to_string(m_desync_frame); AddChatMessage(ChatMessageType::Error, msg); if (g_ActiveConfig.bShowNetPlayMessages) { OSD::AddMessage(msg, OSD::Duration::VERY_LONG, OSD::Color::RED); } } break; case NP_GUI_EVT_CONNECTION_LOST: { std::string msg = "Lost connection to server"; AddChatMessage(ChatMessageType::Error, msg); } break; case NP_GUI_EVT_TRAVERSAL_CONNECTION_ERROR: { std::string msg = "Traversal server connection error"; AddChatMessage(ChatMessageType::Error, msg); } } // chat messages while (m_chat_msgs.Size()) { std::string s; m_chat_msgs.Pop(s); AddChatMessage(ChatMessageType::UserIn, s); if (g_ActiveConfig.bShowNetPlayMessages) { OSD::AddMessage(s, OSD::Duration::NORMAL, OSD::Color::GREEN); } } }
int main( int pArgc, const char** pArgs ) { const char* lQueryPtr; int lLock; int lMemId; char lQuery[4096]; char lOp[12]; BOOL lPrintTitle; #ifndef _NO_IPC_ union semun lNULL; #endif #ifdef _NO_IPC_ gGlobalState.Clear(); if( InitLogFile() ) { fprintf( gLogFile, "IMR Init version 0.13.13b %s\n", GetTime( time(NULL)) ); } #endif #ifdef _FAST_CGI_ while( FCGI_Accept() >= 0 ) { #endif lPrintTitle = TRUE; /* Send the header required by the server */ printf("Content-type: text/plain%c%c", 10, 10); lQueryPtr = getenv( "QUERY_STRING" ); if( lQueryPtr == NULL ) { printf( "No parameters\n" ); } else { StrMaxCopy( lQuery, lQueryPtr, 4096 ); UnpadQuery( lQuery ); if( sscanf( lQuery, "=%11s", lOp ) == 1 ) { #ifndef _FAST_CGI_ /* Change the local directory */ char* lPathEnd = strrchr( pArgs[0], '/' ); if( lPathEnd != NULL ) { *lPathEnd = 0; chdir( pArgs[0] ); } #endif if( !strcmp( lOp, "RESET" ) ) { printf( "RESET OK\n\n" ); #ifdef _NO_IPC_ /* // gGlobalState.Clear(); // sleep( 2 ); // return 0; // break; */ #else lLock = semget( IR_SEM_KEY, 1, 0777 ); lMemId = shmget( IR_SHMEM_KEY, sizeof( IRState ), 0666 ); if( lLock != -1 ) { semctl( lLock, 0, IPC_RMID, lNULL ); } if( lMemId != -1 ) { shmctl( lMemId, IPC_RMID, NULL ); } #endif if( InitLogFile() ) { fprintf( gLogFile, "IMR Reset %s\n", GetTime( time(NULL)) ); } #ifdef _FAST_CGI_ break; #endif } else if( !strcmp( lOp, "SET_MESSAGE" ) ) { } /* else if( !strcmp( lOp, "STICK" ) ) { // Fork and leave a child running for 1 hour int lCode = fork(); if( lCode == 0 ) { if( InitLogFile() ) { fprintf( gLogFile, "STICK Start %s\n", GetTime( time(NULL)) ); CloseLogFile(); } close( 0 ); close( 1 ); close( 2 ); sleep( 3600 ); if( InitLogFile() ) { fprintf( gLogFile, "STICK End %s\n", GetTime( time(NULL)) ); CloseLogFile(); } } else if( lCode == -1 ) { printf( "ERROR\n" ); } else { printf( "SUCCESS\n" ); } } */ else { IRState* lState = NULL; #ifdef _NO_IPC_ lState = &gGlobalState; #else int lLock; /* Semaphore */ int lMemId; /* Shared memory */ struct sembuf lSemOp; lSemOp.sem_flg = 0; /*Avoid corruption but must not core-dump SEM_UNDO; // Risky but prevents dead-lock */ lSemOp.sem_num = 0; /* First try to create the structure for the first time */ /* Lock the data struct */ lLock = semget( IR_SEM_KEY, 1, 0777|IPC_CREAT|IPC_EXCL ); if( lLock != -1 ) { union semun lArg; /* Initialize the newly created semaphore */ lArg.val = 1; semctl( lLock, 0, SETVAL, lArg ); } else { lLock = semget( IR_SEM_KEY, 1, 0777 ); } if( lLock == -1 ) { printf( "Unable to get semaphore\n" ); } else { lSemOp.sem_op = -1; if( semop( lLock, &lSemOp, 1 ) == -1 ) { printf( "Unable to decrement semaphore\n" ); lLock = -1; } else { /* From here we can work safely */ lMemId = shmget( IR_SHMEM_KEY, sizeof( IRState ), 0666|IPC_CREAT|IPC_EXCL ); if( lMemId != -1 ) { lState = (IRState*)shmat( lMemId, NULL, 0 ); if( (int)lState == -1 ) { lState = NULL; } if( lState == NULL ) { printf( "Unable to attach shmem\n" ); } else { Clear( lState ); if( InitLogFile() ) { fprintf( gLogFile, "IMR Init %s\n", GetTime( time(NULL)) ); } } } else { lMemId = shmget( IR_SHMEM_KEY, sizeof( IRState ), 0666 ); if( lMemId == -1 ) { printf( "Unable to get shmem\n" ); } else { lState = (IRState*)shmat( lMemId, NULL, 0 ); if( (int)lState == -1 ) { lState = NULL; } if( lState == NULL ) { printf( "Unable to attach shmem\n" ); } } } } } #endif if( lState != NULL ) { lPrintTitle = FALSE; VerifyExpirations( lState ); if( !strcmp( lOp, "REFRESH" ) ) { int lUserIndex; int lUserId; int lTimeStamp; if( sscanf( lQuery, "%*s %d-%u %d", &lUserIndex, &lUserId, &lTimeStamp )==3 ) { PrintStateChange( lState, lUserIndex, lUserId, lTimeStamp ); } } else if( !strcmp( lOp, "ADD_CHAT" ) ) { int lUserIndex; int lUserId; char lChatMessage[200]; if( sscanf( lQuery, "%*s %d-%u %200s", &lUserIndex, &lUserId, lChatMessage )==3 ) { Unpad( lChatMessage ); AddChatMessage( lState, lUserIndex, lUserId, lChatMessage ); } } /* URL?=ADD_USER MAJOR-MINORID VERSION KEY2 KEY3 ALIAS */ else if( !strcmp( lOp, "ADD_USER" ) ) { int lMajorID; int lMinorID; int lVersion; unsigned int lKey2; unsigned int lKey3; char lUserName[40]; #ifdef _EXPIRED_ AddUser( lState, "User", 1,-1, -1, 0, 0 ); #else if( sscanf( lQuery, "%*s %d-%d %d %d %d %40s", &lMajorID, &lMinorID, &lVersion, &lKey2, &lKey3, lUserName )==6 ) { Unpad( lUserName ); AddUser( lState, lUserName, lVersion,lMajorID, lMinorID, lKey2, lKey3 ); } #endif } /* URL?=ADD_GAME USER_ID GAME_NAME TRACK_NAME NBLAP WEAPON PORT */ else if( !strcmp( lOp, "ADD_GAME" ) ) { int lUserIndex; int lUserId; int lNbLap; char lGameName[40]; char lTrackName[40]; int lWeapon; unsigned lPort; if( sscanf( lQuery, "%*s %d-%u %40s %40s %d %d %u", &lUserIndex, &lUserId, lGameName, lTrackName, &lNbLap, &lWeapon, &lPort )==7 ) { const char* lRemoteAddr = getenv( "REMOTE_ADDR" ); if( (lRemoteAddr != NULL)&&(strlen(lRemoteAddr) != 0) ) { Unpad( lTrackName ); Unpad( lGameName ); AddGame( lState, lGameName, lTrackName, lNbLap, lUserIndex, lUserId, lRemoteAddr, lPort, lWeapon ); } } } else if( !strcmp( lOp, "JOIN_GAME" ) ) { int lUserIndex; int lUserId; int lGameIndex; int lGameId; if( sscanf( lQuery, "%*s %d-%u %d-%u", &lGameIndex, &lGameId, &lUserIndex, &lUserId )==4 ) { JoinGame( lState, lGameIndex, lGameId, lUserIndex, lUserId ); } } else if( !strcmp( lOp, "DEL_GAME" ) ) { int lUserIndex; int lUserId; int lGameIndex; int lGameId; if( sscanf( lQuery, "%*s %d-%u %d-%u", &lGameIndex, &lGameId, &lUserIndex, &lUserId )==4 ) { DeleteGame( lState, lGameIndex, lGameId, lUserIndex, lUserId ); } } else if( !strcmp( lOp, "LEAVE_GAME" ) ) { int lUserIndex; int lUserId; int lGameIndex; int lGameId; if( sscanf( lQuery, "%*s %d-%u %d-%u", &lGameIndex, &lGameId, &lUserIndex, &lUserId )==4 ) { LeaveGame( lState, lGameIndex, lGameId, lUserIndex, lUserId ); } } else if( !strcmp( lOp, "DEL_USER" ) ) { int lUserIndex; int lUserId; if( sscanf( lQuery, "%*s %d-%u", &lUserIndex, &lUserId )==2 ) { DeleteUser( lState, lUserIndex, lUserId ); } } else if( !strcmp( lOp, "START_GAME" ) ) { int lUserIndex; int lUserId; int lGameIndex; int lGameId; if( sscanf( lQuery, "%*s %d-%u %d-%u", &lGameIndex, &lGameId, &lUserIndex, &lUserId )==4 ) { StartGame( lState, lGameIndex, lGameId, lUserIndex, lUserId ); } } else { lPrintTitle = TRUE; } } #ifdef _NO_IPC_ lState = NULL; #else /* Release lock */ if( lLock != -1 ) { lSemOp.sem_op = 1; semop( lLock, &lSemOp, 1 ); /* Release memory */ if( lState != NULL ) { shmdt( (char*)lState ); } } #endif } } } CloseLogFile(); if( lPrintTitle ) { printf( "Internet Meeting Room (c)1996,97 GrokkSoft inc.\n" ); } #ifdef _FAST_CGI_ } #endif return 0; }
void CChat::ProcessInput( void ) { // Are we not connected? if( !pCore->GetNetworkModule() || !pCore->GetNetworkModule()->IsConnected() ) return; // Was anything entered? if( m_strInput.GetLength() > 0 ) { // Is the input a command? bool bIsCommand = (m_strInput.GetChar( 0 ) == CHAT_CMD_CHAR); // Internal cmds bool bHasUsedCmd = false; // Process internal commands if( bIsCommand ) { // std::string sInput = m_strInput.Get(); // Get the end of the command size_t sCommandEnd = sInput.find( " " ); // If we don't have a valid end use the end of the string if ( sCommandEnd == std::string::npos ) sCommandEnd = sInput.length(); // Get the command name std::string strCommand = sInput.substr( 1, (sCommandEnd - 1) ); // Get the command parameters std::string strParams; // Do we have any parameters? if( sCommandEnd < sInput.length() ) strParams = sInput.substr( (sCommandEnd + 1), sInput.length() ); if( strCommand == "q" || strCommand == "quit" || strCommand == "exit" ) { // Shutdown pCore->Shutdown(); return; } else if( strCommand == "disconnect" ) { // Are we connected? if( pCore->GetNetworkModule() && pCore->GetNetworkModule()->IsConnected() ) { // Disconnect from the network pCore->GetNetworkModule()->Disconnect(); // Stop multiplayer pCore->StopMultiplayer(); // Go back to main menu pCore->GetGUI()->GetMainMenu()->SetVisible( true ); } bHasUsedCmd = true; } else if( strCommand == "savepos" ) { bHasUsedCmd = true; // Are we spawned? if( pCore->GetPlayerManager()->GetLocalPlayer()->IsSpawned() ) { // CVector3 vecPosition; CVector3 vecRotation; bool bOnFoot = true; // Is the player on-foot? if( pCore->GetPlayerManager()->GetLocalPlayer()->GetState() == ePlayerState::PLAYERSTATE_ONFOOT ) { // Get the localplayer position pCore->GetPlayerManager()->GetLocalPlayer()->GetPosition( &vecPosition ); // Get the localplayer rotation pCore->GetPlayerManager()->GetLocalPlayer()->GetRotation( &vecRotation ); } else if( pCore->GetPlayerManager()->GetLocalPlayer()->GetState() == ePlayerState::PLAYERSTATE_DRIVER || pCore->GetPlayerManager()->GetLocalPlayer()->GetState() == ePlayerState::PLAYERSTATE_PASSENGER ) { // Get the vehicle position pCore->GetPlayerManager()->GetLocalPlayer()->GetVehicle()->GetPosition( &vecPosition ); // Get the vehicle rotation pCore->GetPlayerManager()->GetLocalPlayer()->GetVehicle()->GetRotation( &vecRotation ); // bOnFoot = false; } // Open the saved positions file FILE * pFile = fopen( SharedUtility::GetAbsolutePath( "data\\savedpositions.txt" ).Get(), "a" ); // Did the file open? if( pFile ) { // Get the localplayer pointer CLocalPlayer * pLocalPlayer = pCore->GetPlayerManager()->GetLocalPlayer(); // Save the player position fprintf( pFile, "%d, %f, %f, %f, %f, %f, %f // %s\n", (bOnFoot ? pLocalPlayer->GetModel () : pLocalPlayer->GetVehicle()->GetModel ()), vecPosition.fX, vecPosition.fY, vecPosition.fZ, vecRotation.fX, vecRotation.fY, vecRotation.fZ, strParams.c_str() ); // Close the saved positions file fclose( pFile ); // AddInfoMessage( (bOnFoot ? " -> Onfoot position saved!" : " -> Invehicle position saved!") ); } else { // AddInfoMessage( CColor( 255, 0, 0, 255 ), "Failed to open savedpositions.txt" ); } } } #ifdef DEBUG else if( strCommand == "lua" ) { bHasUsedCmd = true; if( CLua::Execute( strParams.c_str() ) ) AddInfoMessage( CColor( 50, 177, 94, 255 ), strParams.c_str() ); else AddInfoMessage( CColor( 178, 40, 86, 255 ), strParams.c_str() ); } #endif } // Have we used a command? if( bHasUsedCmd ) { // Add this command to the history AddToHistory(); // This is an internal command, don't pass it to the server! return; } // Is the network module instance valid? if( pCore->GetNetworkModule() ) { // Are we connected? if( pCore->GetNetworkModule()->IsConnected() ) { RakNet::BitStream bitStream; RakNet::RakString strInput; // Is this a command? if( bIsCommand ) { // Write 1 bitStream.Write1(); // Set the input minus the command character strInput = (GetInputText() + 1); } else { // Write 0 bitStream.Write0(); // Set the input strInput = GetInputText(); } // Write the input bitStream.Write( strInput ); // Call the client event CSquirrelArguments pArguments; pArguments.push( strInput.C_String () ); // Should we send this message? if ( pCore->GetClientScriptingManager()->GetEvents()->Call( "onClientChat", &pArguments ).GetInteger() == 1 ) { // Send it to the server pCore->GetNetworkModule()->Call( RPC_PLAYER_CHAT, &bitStream, HIGH_PRIORITY, RELIABLE_ORDERED, true ); // Add this message to the history AddToHistory(); // Add the chat message for the localplayer if it's not a command if ( !bIsCommand ) AddChatMessage( pCore->GetPlayerManager()->GetLocalPlayer(), GetInputText() ); } } } } }