From 22f9daf0d8d03c5ce74f92903abecdc7a0a7a890 Mon Sep 17 00:00:00 2001 From: dean11 Date: Tue, 18 Feb 2014 16:38:08 +0100 Subject: [PATCH] Connection between server and client is stable --- Code/Game/GameLogic/Game_LevelData.cpp | 1 + Code/Game/GameServer/GameClient.h | 13 +++- Code/Game/GameServer/GameLobby.h | 3 + .../GameServer/Implementation/GameClient.cpp | 11 ++- .../GameServer/Implementation/GameLobby.cpp | 14 ++-- .../GameLobby_ProtocolParser.cpp | 71 +++++++++++++------ .../Implementation/GameSession_Gameplay.cpp | 4 +- .../Implementation/GameSession_General.cpp | 12 ++-- .../StandAloneLauncher/Form1.Designer.cs | 63 +++++++++------- .../LanServer/StandAloneLauncher/Form1.cs | 10 +-- .../Utilities/Resource/OResourceHandler.cpp | 2 + Code/Network/NetworkAPI/NetworkClient.cpp | 4 ++ 12 files changed, 141 insertions(+), 67 deletions(-) diff --git a/Code/Game/GameLogic/Game_LevelData.cpp b/Code/Game/GameLogic/Game_LevelData.cpp index 80220d5f..62fb3cc5 100644 --- a/Code/Game/GameLogic/Game_LevelData.cpp +++ b/Code/Game/GameLogic/Game_LevelData.cpp @@ -54,6 +54,7 @@ IObjectData* Game::LevelData::GetObjectAt(int ID) const void Game::LevelData::GetAllDynamicObjects(Utility::DynamicMemory::DynamicArray& mem) const { + mem.Resize(level->dynamicObjects.Size()); for(int i = 0; i < level->dynamicObjects.Size(); i++) { mem[i] = level->dynamicObjects[i]; diff --git a/Code/Game/GameServer/GameClient.h b/Code/Game/GameServer/GameClient.h index 422df933..6fcf6b05 100644 --- a/Code/Game/GameServer/GameClient.h +++ b/Code/Game/GameServer/GameClient.h @@ -17,6 +17,13 @@ namespace DanBias */ class GameClient { + public: + enum ClientState + { + ClientState_CreatingGame, + ClientState_Ready, + }; + public: GameClient(Utility::DynamicMemory::SmartPointer nwClient); virtual~GameClient(); @@ -33,6 +40,7 @@ namespace DanBias inline bool IsReady() const { return this->isReady; } inline GameLogic::IPlayerData* GetPlayer() const { return this->player; } Oyster::Network::NetClient GetClient() const { return this->client; } + ClientState GetState() const { return this->state; } void SetPlayer(GameLogic::IPlayerData* player); @@ -40,6 +48,7 @@ namespace DanBias void SetAlias(std::wstring alias); void SetCharacter(std::wstring character); void SetSinceLastResponse(float seconds); + void SetState(ClientState state); GameLogic::IPlayerData* ReleasePlayer(); Oyster::Network::NetClient ReleaseClient(); @@ -50,13 +59,15 @@ namespace DanBias private: GameLogic::IPlayerData* player; - Utility::DynamicMemory::SmartPointer client; + Oyster::Network::NetClient client; bool isReady; float secondsSinceLastResponse; std::wstring alias; std::wstring character; + + ClientState state; }; }//End namespace DanBias diff --git a/Code/Game/GameServer/GameLobby.h b/Code/Game/GameServer/GameLobby.h index 7b9a46d7..f62baa63 100644 --- a/Code/Game/GameServer/GameLobby.h +++ b/Code/Game/GameServer/GameLobby.h @@ -58,6 +58,9 @@ namespace DanBias void LobbyReady(GameLogic::Protocol_LobbyClientReadyState& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_ClientReadyState: void LobbyQuerryGameData(GameLogic::Protocol_QuerryGameType& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_QuerryGameType: + private: + int FindClient(Oyster::Network::NetworkClient* c); + private: void ClientEventCallback(Oyster::Network::NetEvent e) override; void ClientConnectedEvent(Utility::DynamicMemory::SmartPointer client) override; diff --git a/Code/Game/GameServer/Implementation/GameClient.cpp b/Code/Game/GameServer/Implementation/GameClient.cpp index 7ef4a338..81066450 100644 --- a/Code/Game/GameServer/Implementation/GameClient.cpp +++ b/Code/Game/GameServer/Implementation/GameClient.cpp @@ -50,6 +50,10 @@ void GameClient::SetCharacter(std::wstring character) { this->character = character; } +void GameClient::SetState(ClientState state) +{ + this->state = state; +} void GameClient::SetOwner(Oyster::Network::NetworkSession* owner) @@ -58,7 +62,12 @@ void GameClient::SetOwner(Oyster::Network::NetworkSession* owner) } void GameClient::UpdateClient() { - this->client->Update(); + switch (this->state) + { + case ClientState_Ready: + this->client->Update(); + break; + } } diff --git a/Code/Game/GameServer/Implementation/GameLobby.cpp b/Code/Game/GameServer/Implementation/GameLobby.cpp index b95402e2..cadfa53a 100644 --- a/Code/Game/GameServer/Implementation/GameLobby.cpp +++ b/Code/Game/GameServer/Implementation/GameLobby.cpp @@ -17,7 +17,7 @@ GameLobby::GameLobby() { } GameLobby::~GameLobby() { - this->clients.Clear(); + this->gClients.Clear(); } void GameLobby::Release() { @@ -26,11 +26,11 @@ void GameLobby::Release() } void GameLobby::Update() { - for (unsigned int i = 0; i < this->clients.Size(); i++) + for (unsigned int i = 0; i < this->gClients.Size(); i++) { - if(this->clients[i]) + if(this->gClients[i]) { - this->clients[i]->Update(); + this->gClients[i]->GetClient()->Update(); } } } @@ -119,7 +119,7 @@ void GameLobby::ClientEventCallback(NetEventGetID(), e.sender->GetIpAddress().c_str()); - e.sender->Disconnect(); + //e.sender->Disconnect(); //this->readyList.Remove(e.sender); //this->gClients.Remove(e.sender); break; @@ -184,7 +184,9 @@ void GameLobby::ProcessClients() } bool GameLobby::Attach(Utility::DynamicMemory::SmartPointer client) { - if(this->clientCount = this->description.maxClients) return false; + if(this->clientCount == this->description.maxClients) return false; + + client->SetOwner(this); bool added = false; for (unsigned int i = 0; i < this->gClients.Size(); i++) diff --git a/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp b/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp index ebcfb8bf..2c4f148a 100644 --- a/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp +++ b/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp @@ -40,26 +40,46 @@ void GameLobby::GeneralStatus(GameLogic::Protocol_General_Status& p, Oyster::Net { case Protocol_General_Status::States_ready: { - + int temp = FindClient(c); + if(temp != -1 ) + { + switch (this->gClients[temp]->GetState()) + { + case GameClient::ClientState_CreatingGame: + { + this->gameSession.Join(this->gClients[temp]); + this->gClients[temp] = 0; + } + break; + } + } + else + { + c->Disconnect(); + } } + break; case Protocol_General_Status::States_idle: { } + break; case Protocol_General_Status::States_leave: + break; case Protocol_General_Status::States_disconected: { Detach(c)->Disconnect(); } + break; } } void GameLobby::GeneralText(GameLogic::Protocol_General_Text& p, Oyster::Network::NetworkClient* c) { - for (unsigned int i = 0; i < this->clients.Size(); i++) + for (unsigned int i = 0; i < this->gClients.Size(); i++) { - if(this->clients[i]) + if(this->gClients[i]) { - this->clients[i]->Send(p); + this->gClients[i]->GetClient()->Send(p); } } printf(p.text.c_str()); @@ -69,9 +89,9 @@ void GameLobby::LobbyStartGame(GameLogic::Protocol_LobbyStartGame& p, Oyster::Ne if(this->sessionOwner->GetClient()->GetID() == c->GetID()) { //Send countdown timer before lobby shuts down - for (unsigned int i = 0; i < this->clients.Size(); i++) + for (unsigned int i = 0; i < this->gClients.Size(); i++) { - this->clients[i]->Send(Protocol_LobbyStartGame(3.0f)); + this->gClients[i]->GetClient()->Send(Protocol_LobbyStartGame(3.0f)); } } else @@ -118,32 +138,41 @@ void GameLobby::LobbyQuerryGameData(GameLogic::Protocol_QuerryGameType& p, Oyste { if(this->gameSession) { - gClient temp; - bool found = false; - - //find client in waiting list - for (unsigned int i = 0; !found && i < this->clients.Size(); i++) - { - if(this->gClients[i]->GetClient()->GetID() == c->GetID()) - { - temp = this->gClients[i]; - found = true; - } - } + int temp = FindClient(c); //Something is wrong - if(!found) + if(temp == -1) { c->Disconnect(); } else { //Send game data - this->gameSession.Join(temp); + Protocol_LobbyCreateGame lcg((char)1, (char)0, Utility::String::WStringToString(this->description.mapName, std::string())); + c->Send(lcg); + this->gClients[temp]->SetState(GameClient::ClientState_CreatingGame); } } else { - + // Nothing.- } } + + +int GameLobby::FindClient(Oyster::Network::NetworkClient* c) +{ + int temp = -1; + + //find client in waiting list + for (unsigned int i = 0; i < this->gClients.Size(); i++) + { + if(this->gClients[i]->GetClient()->GetID() == c->GetID()) + { + temp = i; + break; + } + } + + return temp; +} \ No newline at end of file diff --git a/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp b/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp index 61ccf9c8..6a9c09c2 100644 --- a/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp +++ b/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp @@ -45,7 +45,7 @@ using namespace DanBias; //Find the idiot for (unsigned int i = 0; i < this->gClients.Size(); i++) { - if(this->gClients[i]->Equals(e.sender)) + if(this->gClients[i] && this->gClients[i]->Equals(e.sender)) { temp = i; } @@ -78,7 +78,7 @@ using namespace DanBias; { for (unsigned int i = 0; i < this->gClients.Size(); i++) { - if(this->gClients[i]) + if(this->gClients[i] ) { this->gClients[i]->UpdateClient(); } diff --git a/Code/Game/GameServer/Implementation/GameSession_General.cpp b/Code/Game/GameServer/Implementation/GameSession_General.cpp index 07d2dcd4..8b5bd8cb 100644 --- a/Code/Game/GameServer/Implementation/GameSession_General.cpp +++ b/Code/Game/GameServer/Implementation/GameSession_General.cpp @@ -199,18 +199,12 @@ bool GameSession::Join(gClient gameClient) gameClient->SetPlayer(playerData); NetworkClient* nwClient = gameClient->GetClient(); - -// Send the level information - { - Protocol_LobbyCreateGame lcg((char)1, (char)0, Utility::String::WStringToString(this->description.mapName, std::string())); - nwClient->Send(lcg); - } // Send the player data only { Protocol_ObjectCreatePlayer oc( playerData->GetPosition(), playerData->GetRotation(), playerData->GetScale(), playerData->GetID(), true, playerData->GetTeamID(), - /*nwClient->GetAlias()*/"", /*playerData->GetMesh()*/"char_white.dan"); + /*nwClient->GetAlias()*/"Unknown", /*playerData->GetMesh()*/"char_white.dan"); nwClient->Send(oc); } @@ -223,7 +217,7 @@ bool GameSession::Join(gClient gameClient) IPlayerData* temp = this->gClients[i]->GetPlayer(); Protocol_ObjectCreatePlayer oc( temp->GetPosition(), temp->GetRotation(), temp->GetScale(), temp->GetID(), false, temp->GetTeamID(), - /*nwClient->GetAlias()*/"", /*playerData->GetMesh()*/"char_white.dan"); + /*nwClient->GetAlias()*/"Unknown", /*playerData->GetMesh()*/"char_white.dan"); nwClient->Send(oc); } } @@ -259,6 +253,8 @@ bool GameSession::Join(gClient gameClient) } } + gameClient->SetState(GameClient::ClientState_Ready); + return added; } diff --git a/Code/Game/LanServer/StandAloneLauncher/Form1.Designer.cs b/Code/Game/LanServer/StandAloneLauncher/Form1.Designer.cs index 103a047f..bd168c3c 100644 --- a/Code/Game/LanServer/StandAloneLauncher/Form1.Designer.cs +++ b/Code/Game/LanServer/StandAloneLauncher/Form1.Designer.cs @@ -43,6 +43,7 @@ this.forceStart = new System.Windows.Forms.CheckBox(); this.label2 = new System.Windows.Forms.Label(); this.label4 = new System.Windows.Forms.Label(); + this.labelClientsConnected = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.nrOfClients = new System.Windows.Forms.NumericUpDown(); this.buttonStartGame = new System.Windows.Forms.Button(); @@ -50,13 +51,14 @@ this.ServerInfoTextArea = new System.Windows.Forms.RichTextBox(); this.splitter1 = new System.Windows.Forms.Splitter(); this.clientInfoBox = new System.Windows.Forms.ListBox(); - this.labelClientsConnected = new System.Windows.Forms.Label(); + this.panel_CommanArea = new System.Windows.Forms.Panel(); ((System.ComponentModel.ISupportInitialize)(this.listenPort)).BeginInit(); this.panel_serverOptions.SuspendLayout(); this.panel_commands.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.timeLimit)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.nrOfClients)).BeginInit(); this.panel_clientArea.SuspendLayout(); + this.panel_CommanArea.SuspendLayout(); this.SuspendLayout(); // // serverToggle @@ -135,9 +137,10 @@ this.panel_serverOptions.Controls.Add(this.label_listenPort); this.panel_serverOptions.Controls.Add(this.lanBroadcast); this.panel_serverOptions.Controls.Add(this.label_serverName); - this.panel_serverOptions.Location = new System.Drawing.Point(12, 12); + this.panel_serverOptions.Dock = System.Windows.Forms.DockStyle.Top; + this.panel_serverOptions.Location = new System.Drawing.Point(0, 0); this.panel_serverOptions.Name = "panel_serverOptions"; - this.panel_serverOptions.Size = new System.Drawing.Size(183, 141); + this.panel_serverOptions.Size = new System.Drawing.Size(200, 141); this.panel_serverOptions.TabIndex = 6; // // panel_commands @@ -153,19 +156,19 @@ this.panel_commands.Controls.Add(this.label1); this.panel_commands.Controls.Add(this.nrOfClients); this.panel_commands.Controls.Add(this.buttonStartGame); - this.panel_commands.Location = new System.Drawing.Point(12, 159); + this.panel_commands.Location = new System.Drawing.Point(3, 150); this.panel_commands.Name = "panel_commands"; - this.panel_commands.Size = new System.Drawing.Size(183, 202); + this.panel_commands.Size = new System.Drawing.Size(191, 202); this.panel_commands.TabIndex = 7; this.panel_commands.Visible = false; // // mapName // - this.mapName.Location = new System.Drawing.Point(78, 7); + this.mapName.Location = new System.Drawing.Point(75, 10); this.mapName.Name = "mapName"; - this.mapName.Size = new System.Drawing.Size(98, 20); + this.mapName.Size = new System.Drawing.Size(113, 20); this.mapName.TabIndex = 12; - this.mapName.Text = "Unknown"; + this.mapName.Text = "2ofAll_updated.bias"; // // timeLimit // @@ -194,7 +197,7 @@ "Team death-match"}); this.gameModes.Location = new System.Drawing.Point(78, 66); this.gameModes.Name = "gameModes"; - this.gameModes.Size = new System.Drawing.Size(99, 21); + this.gameModes.Size = new System.Drawing.Size(110, 21); this.gameModes.TabIndex = 10; // // label3 @@ -236,6 +239,15 @@ this.label4.TabIndex = 8; this.label4.Text = "Map name"; // + // labelClientsConnected + // + this.labelClientsConnected.AutoSize = true; + this.labelClientsConnected.Location = new System.Drawing.Point(9, 149); + this.labelClientsConnected.Name = "labelClientsConnected"; + this.labelClientsConnected.Size = new System.Drawing.Size(104, 13); + this.labelClientsConnected.TabIndex = 8; + this.labelClientsConnected.Text = "Clients connected: 0"; + // // label1 // this.label1.AutoSize = true; @@ -282,9 +294,10 @@ this.panel_clientArea.Controls.Add(this.ServerInfoTextArea); this.panel_clientArea.Controls.Add(this.splitter1); this.panel_clientArea.Controls.Add(this.clientInfoBox); - this.panel_clientArea.Location = new System.Drawing.Point(202, 12); + this.panel_clientArea.Dock = System.Windows.Forms.DockStyle.Fill; + this.panel_clientArea.Location = new System.Drawing.Point(200, 0); this.panel_clientArea.Name = "panel_clientArea"; - this.panel_clientArea.Size = new System.Drawing.Size(303, 349); + this.panel_clientArea.Size = new System.Drawing.Size(535, 616); this.panel_clientArea.TabIndex = 8; // // ServerInfoTextArea @@ -297,7 +310,7 @@ this.ServerInfoTextArea.Location = new System.Drawing.Point(0, 152); this.ServerInfoTextArea.Name = "ServerInfoTextArea"; this.ServerInfoTextArea.ReadOnly = true; - this.ServerInfoTextArea.Size = new System.Drawing.Size(303, 197); + this.ServerInfoTextArea.Size = new System.Drawing.Size(535, 464); this.ServerInfoTextArea.TabIndex = 1; this.ServerInfoTextArea.Text = ""; // @@ -306,7 +319,7 @@ this.splitter1.Dock = System.Windows.Forms.DockStyle.Top; this.splitter1.Location = new System.Drawing.Point(0, 147); this.splitter1.Name = "splitter1"; - this.splitter1.Size = new System.Drawing.Size(303, 5); + this.splitter1.Size = new System.Drawing.Size(535, 5); this.splitter1.TabIndex = 2; this.splitter1.TabStop = false; // @@ -316,26 +329,26 @@ this.clientInfoBox.FormattingEnabled = true; this.clientInfoBox.Location = new System.Drawing.Point(0, 0); this.clientInfoBox.Name = "clientInfoBox"; - this.clientInfoBox.Size = new System.Drawing.Size(303, 147); + this.clientInfoBox.Size = new System.Drawing.Size(535, 147); this.clientInfoBox.TabIndex = 0; // - // labelClientsConnected + // panel_CommanArea // - this.labelClientsConnected.AutoSize = true; - this.labelClientsConnected.Location = new System.Drawing.Point(9, 149); - this.labelClientsConnected.Name = "labelClientsConnected"; - this.labelClientsConnected.Size = new System.Drawing.Size(104, 13); - this.labelClientsConnected.TabIndex = 8; - this.labelClientsConnected.Text = "Clients connected: 0"; + this.panel_CommanArea.Controls.Add(this.panel_commands); + this.panel_CommanArea.Controls.Add(this.panel_serverOptions); + this.panel_CommanArea.Dock = System.Windows.Forms.DockStyle.Left; + this.panel_CommanArea.Location = new System.Drawing.Point(0, 0); + this.panel_CommanArea.Name = "panel_CommanArea"; + this.panel_CommanArea.Size = new System.Drawing.Size(200, 616); + this.panel_CommanArea.TabIndex = 9; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(517, 373); + this.ClientSize = new System.Drawing.Size(735, 616); this.Controls.Add(this.panel_clientArea); - this.Controls.Add(this.panel_commands); - this.Controls.Add(this.panel_serverOptions); + this.Controls.Add(this.panel_CommanArea); this.Name = "Form1"; this.Text = "Form1"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FormClosingEvent); @@ -347,6 +360,7 @@ ((System.ComponentModel.ISupportInitialize)(this.timeLimit)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.nrOfClients)).EndInit(); this.panel_clientArea.ResumeLayout(false); + this.panel_CommanArea.ResumeLayout(false); this.ResumeLayout(false); } @@ -376,6 +390,7 @@ private System.Windows.Forms.TextBox mapName; private System.Windows.Forms.CheckBox forceStart; private System.Windows.Forms.Label labelClientsConnected; + private System.Windows.Forms.Panel panel_CommanArea; } } diff --git a/Code/Game/LanServer/StandAloneLauncher/Form1.cs b/Code/Game/LanServer/StandAloneLauncher/Form1.cs index 28a83c72..58deb2a9 100644 --- a/Code/Game/LanServer/StandAloneLauncher/Form1.cs +++ b/Code/Game/LanServer/StandAloneLauncher/Form1.cs @@ -9,6 +9,8 @@ using System.Threading.Tasks; using System.Windows.Forms; using System.Windows.Interop; using System.Runtime.InteropServices; +using System.Threading; +using System.Timers; namespace StandAloneLauncher { @@ -30,18 +32,17 @@ namespace StandAloneLauncher return true; } + public void Run() { while (this.Created) { Application.DoEvents(); - - //Do some stuff this.gameServer.ServerUpdate(); this.labelClientsConnected.Text = "Clients connected: " + this.gameServer.GetClientsConnectedCount().ToString(); } } - + private void button1_serverToggle_Click(object sender, EventArgs e) { if (this.serverIsRunning) @@ -88,8 +89,9 @@ namespace StandAloneLauncher { //this.gameServer.GameSetGameMode(this.gameModes.SelectedText); this.gameServer.GameSetGameTime((int)this.timeLimit.Value); - //this.gameServer.GameSetMapId(0); + this.gameServer.GameSetMapName(this.mapName.Text); this.gameServer.GameSetMaxClients((int)this.nrOfClients.Value); + if (!this.gameServer.GameStart( this.forceStart.Checked )) { this.ServerInfoTextArea.AppendText(DateTime.Now.ToUniversalTime() + "\n\t" + "Failed to start the game session!\n"); diff --git a/Code/Misc/Utilities/Resource/OResourceHandler.cpp b/Code/Misc/Utilities/Resource/OResourceHandler.cpp index 52d93af0..cc297f67 100644 --- a/Code/Misc/Utilities/Resource/OResourceHandler.cpp +++ b/Code/Misc/Utilities/Resource/OResourceHandler.cpp @@ -52,6 +52,8 @@ OHRESOURCE OysterResource::LoadResource(const wchar_t* filename, ResourceType ty } } + if(!resourceData) return 0; + return resourceData->GetResourceHandle(); } OHRESOURCE OysterResource::LoadResource(const wchar_t filename[], CustomLoadFunction loadFnc, int customId, bool force) diff --git a/Code/Network/NetworkAPI/NetworkClient.cpp b/Code/Network/NetworkAPI/NetworkClient.cpp index 96d149f3..69872fc1 100644 --- a/Code/Network/NetworkAPI/NetworkClient.cpp +++ b/Code/Network/NetworkAPI/NetworkClient.cpp @@ -96,6 +96,10 @@ struct NetworkClient::PrivateData : public IThreadObject //printf("\t(%i)\n", this->sendQueue.Size()); OysterByte temp; CustomNetProtocol p = this->sendQueue.Pop(); + + if(p[0].value.netShort == 304) + int i = 0; + this->translator.Pack(temp, p); errorCode = this->connection.Send(temp);