From 05740665dac9cc7a7630d57a04ef8446f843017f Mon Sep 17 00:00:00 2001 From: Dennis Andersen Date: Thu, 30 Jan 2014 11:11:04 +0100 Subject: [PATCH] GameServer - Fixed a small error regarding not reaching some data --- Code/Game/DanBiasGame/DanBiasGame.vcxproj | 11 +- Code/Game/DanBiasGame/DanBiasMaincpp.cpp | 2 +- .../Game/DanBiasGame/GameClientRecieverFunc.h | 7 -- .../DanBiasGame/GameClientState/GameState.cpp | 4 +- .../DanBiasServerLauncher/ServerLauncher.cpp | 4 +- Code/Game/GameProtocols/LobbyProtocols.h | 106 +++++++++--------- Code/Game/GameServer/GameLobby.h | 20 +++- Code/Game/GameServer/GameSession.h | 2 - .../GameServer/Implementation/GameLobby.cpp | 32 +++++- .../GameLobby_ProtocolParser.cpp | 42 +++---- .../GameServer/Implementation/GameServer.cpp | 21 ++-- Code/Network/NetworkAPI/NetworkSession.cpp | 53 +++++---- Code/Network/NetworkAPI/NetworkSession.h | 11 +- 13 files changed, 179 insertions(+), 136 deletions(-) diff --git a/Code/Game/DanBiasGame/DanBiasGame.vcxproj b/Code/Game/DanBiasGame/DanBiasGame.vcxproj index 25696efd..955107ef 100644 --- a/Code/Game/DanBiasGame/DanBiasGame.vcxproj +++ b/Code/Game/DanBiasGame/DanBiasGame.vcxproj @@ -72,7 +72,7 @@ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName)D $(SolutionDir)..\External\Lib\;$(SolutionDir)..\Bin\DLL;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) - $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Network/NetworkAPI;$(IncludePath);C:\Program Files %28x86%29\Visual Leak Detector\include + $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Network/NetworkAPI;$(IncludePath);C:\Program Files %28x86%29\Visual Leak Detector\include;$(SolutionDir)Game\GameServer\ true @@ -80,7 +80,7 @@ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName)D $(SolutionDir)..\External\Lib\;$(SolutionDir)..\Bin\DLL;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) - $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Network/NetworkAPI;$(IncludePath);C:\Program Files %28x86%29\Visual Leak Detector\include + $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Network/NetworkAPI;$(IncludePath);C:\Program Files %28x86%29\Visual Leak Detector\include;$(SolutionDir)Game\GameServer\ false @@ -88,7 +88,7 @@ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName) $(SolutionDir)..\External\Lib\;$(SolutionDir)..\Bin\DLL;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) - $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Network/NetworkAPI;$(IncludePath);C:\Program Files %28x86%29\Visual Leak Detector\include + $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Network/NetworkAPI;$(IncludePath);C:\Program Files %28x86%29\Visual Leak Detector\include;$(SolutionDir)Game\GameServer\ false @@ -96,7 +96,7 @@ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName) $(SolutionDir)..\External\Lib\;$(SolutionDir)..\Bin\DLL;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) - $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Network/NetworkAPI;$(IncludePath);C:\Program Files %28x86%29\Visual Leak Detector\include + $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Network/NetworkAPI;$(IncludePath);C:\Program Files %28x86%29\Visual Leak Detector\include;$(SolutionDir)Game\GameServer\ @@ -190,6 +190,9 @@ {f10cbc03-9809-4cba-95d8-327c287b18ee} + + {143bd516-20a1-4890-a3e4-f8bfd02220e7} + diff --git a/Code/Game/DanBiasGame/DanBiasMaincpp.cpp b/Code/Game/DanBiasGame/DanBiasMaincpp.cpp index 0b7ea666..b62dbe11 100644 --- a/Code/Game/DanBiasGame/DanBiasMaincpp.cpp +++ b/Code/Game/DanBiasGame/DanBiasMaincpp.cpp @@ -7,7 +7,7 @@ //-------------------------------------------------------------------------------------- #define NOMINMAX #include - +#include < #include "DllInterfaces/GFXAPI.h" //#include "IGame.h" diff --git a/Code/Game/DanBiasGame/GameClientRecieverFunc.h b/Code/Game/DanBiasGame/GameClientRecieverFunc.h index f7214b32..e7710b67 100644 --- a/Code/Game/DanBiasGame/GameClientRecieverFunc.h +++ b/Code/Game/DanBiasGame/GameClientRecieverFunc.h @@ -13,13 +13,6 @@ struct GameRecieverObject :public Oyster::Network::NetworkClient // parsing protocols and sending it to the gameState void NetworkCallback(Oyster::Network::CustomNetProtocol& p) override { - - //if( IsGameplayProtocol(p[protocol_INDEX_ID].value.netShort) ) - //ParseGameplayEvent(e.protocol, e.gameClient); - - //if( IsGeneralProtocol(p[protocol_INDEX_ID].value.netShort) ) - //ParseGeneralEvent(e.protocol, e.gameClient); - int pType = p[0].value.netInt; switch (pType) { diff --git a/Code/Game/DanBiasGame/GameClientState/GameState.cpp b/Code/Game/DanBiasGame/GameClientState/GameState.cpp index 871ff558..ba3508a7 100644 --- a/Code/Game/DanBiasGame/GameClientState/GameState.cpp +++ b/Code/Game/DanBiasGame/GameClientState/GameState.cpp @@ -335,8 +335,8 @@ void GameState::Protocol( ObjPos* pos ) //camera->setLook((Oyster::Math::Float3(world[8], world[9], world[10]))); if(i == 2) // playerobj { - //camera->SetPosition(Oyster::Math::Float3(world[12], world[13], world[14])); - //camera->UpdateViewMatrix(); + camera->SetPosition(Oyster::Math::Float3(world[12], world[13], world[14])); + camera->UpdateViewMatrix(); } } } diff --git a/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp b/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp index 62e2b828..a27ab406 100644 --- a/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp +++ b/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp @@ -2,7 +2,7 @@ // Launcher to launch Danbias server // // Created by [Dennis Andersen] [2013] // ////////////////////////////////////////////////// -#define NOMINMAX //Blame it on windows +#define NOMINMAX //Blame it on microsoft #include #include #include @@ -23,7 +23,9 @@ int WINAPI WinMain( HINSTANCE hinst, HINSTANCE prevInst, PSTR cmdLine, int cmdSh desc.listenPort = 15151; if(DanBias::GameServerAPI::ServerInitiate(desc) == DanBias::DanBiasServerReturn_Sucess) { + DanBias::GameServerAPI::ServerStart(); + //DanBias::GameServerAPI::GameStart(); while (!(GetAsyncKeyState(0x51))) //Q for exit { DanBias::GameServerAPI::ServerUpdate(); diff --git a/Code/Game/GameProtocols/LobbyProtocols.h b/Code/Game/GameProtocols/LobbyProtocols.h index 87aad62e..8b333a54 100644 --- a/Code/Game/GameProtocols/LobbyProtocols.h +++ b/Code/Game/GameProtocols/LobbyProtocols.h @@ -92,32 +92,32 @@ namespace GameLogic Oyster::Network::CustomNetProtocol protocol; }; - struct Protocol_LobbyJoin :public Oyster::Network::CustomProtocolObject - { - short value; - - Protocol_LobbyJoin() - { - this->protocol[protocol_INDEX_ID].value = protocol_Lobby_Join; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; - this->protocol[1].type = Oyster::Network::NetAttributeType_Short; - } - Protocol_LobbyJoin(Oyster::Network::CustomNetProtocol& p) - { - this->protocol[protocol_INDEX_ID].value = protocol_Lobby_Join; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; - this->protocol[1].type = Oyster::Network::NetAttributeType_Short; - value = p[1].value.netShort; - } - Oyster::Network::CustomNetProtocol* GetProtocol() override - { - protocol[1].value = value; - return &protocol; - } - - private: - Oyster::Network::CustomNetProtocol protocol; - }; + //struct Protocol_LobbyJoin :public Oyster::Network::CustomProtocolObject + //{ + // short value; + // + // Protocol_LobbyJoin() + // { + // this->protocol[protocol_INDEX_ID].value = protocol_Lobby_Join; + // this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + // this->protocol[1].type = Oyster::Network::NetAttributeType_Short; + // } + // Protocol_LobbyJoin(Oyster::Network::CustomNetProtocol& p) + // { + // this->protocol[protocol_INDEX_ID].value = protocol_Lobby_Join; + // this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + // this->protocol[1].type = Oyster::Network::NetAttributeType_Short; + // value = p[1].value.netShort; + // } + // Oyster::Network::CustomNetProtocol* GetProtocol() override + // { + // protocol[1].value = value; + // return &protocol; + // } + // + // private: + // Oyster::Network::CustomNetProtocol protocol; + //}; struct Protocol_LobbyRefresh :public Oyster::Network::CustomProtocolObject { @@ -140,7 +140,8 @@ namespace GameLogic /** * A protocol that contains all data to send to client when update game lobby */ - struct Protocol_LobbyGameData :public Oyster::Network::CustomProtocolObject + + struct Protocol_LobbyClientData :public Oyster::Network::CustomProtocolObject { // Player list struct PlayerData @@ -150,14 +151,14 @@ namespace GameLogic }; Utility::DynamicMemory::DynamicArray list; - Protocol_LobbyGameData() + Protocol_LobbyClientData() { this->protocol[protocol_INDEX_ID].value = protocol_Lobby_GameData; this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; list.Reserve(10); } - Protocol_LobbyGameData(Oyster::Network::CustomNetProtocol& p) + Protocol_LobbyClientData(Oyster::Network::CustomNetProtocol& p) { this->protocol[protocol_INDEX_ID].value = protocol_Lobby_GameData; this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; @@ -181,32 +182,33 @@ namespace GameLogic private: Oyster::Network::CustomNetProtocol protocol; }; + /** * A protocol that contains all data to send to client when update main lobby */ - struct Protocol_LobbyMainData :public Oyster::Network::CustomProtocolObject - { - // Game instance list - - Protocol_LobbyMainData() - { - this->protocol[protocol_INDEX_ID].value = protocol_Lobby_MainData; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; - - this->protocol[1].type = Oyster::Network::NetAttributeType_Short; - } - Protocol_LobbyMainData(Oyster::Network::CustomNetProtocol& p) - { - - } - Oyster::Network::CustomNetProtocol* GetProtocol() override - { - return &protocol; - } - - private: - Oyster::Network::CustomNetProtocol protocol; - }; + //struct Protocol_LobbyMainData :public Oyster::Network::CustomProtocolObject + //{ + // // Game instance list + // + // Protocol_LobbyMainData() + // { + // this->protocol[protocol_INDEX_ID].value = protocol_Lobby_MainData; + // this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + // + // this->protocol[1].type = Oyster::Network::NetAttributeType_Short; + // } + // Protocol_LobbyMainData(Oyster::Network::CustomNetProtocol& p) + // { + // + // } + // Oyster::Network::CustomNetProtocol* GetProtocol() override + // { + // return &protocol; + // } + // + // private: + // Oyster::Network::CustomNetProtocol protocol; + //}; } #endif // !GAMELOGIC_PLAYER_PROTOCOLS_H diff --git a/Code/Game/GameServer/GameLobby.h b/Code/Game/GameServer/GameLobby.h index 2b9f4bc4..8463b9d9 100644 --- a/Code/Game/GameServer/GameLobby.h +++ b/Code/Game/GameServer/GameLobby.h @@ -12,6 +12,14 @@ namespace DanBias { + struct LobbyLevelData + { + int mapNumber; + int maxClients; + int gameMode; + int gameTime; + std::string gameName; + }; class GameLobby :public Oyster::Network::NetworkSession { public: @@ -20,8 +28,8 @@ namespace DanBias void Release(); void Update(); - void SetGameDesc(const GameSession::GameDescription& desc); - void GetGameDesc(GameSession::GameDescription& desc); + void SetGameDesc(const LobbyLevelData& desc); + void GetGameDesc(LobbyLevelData& desc); bool StartGameSession(); private: @@ -31,11 +39,11 @@ namespace DanBias void GeneralText(GameLogic::Protocol_General_Text& p, Oyster::Network::NetworkClient* c); //id = protocol_General_Text: //void LobbyCreateGame(GameLogic::Protocol_LobbyCreateGame& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Create: void LobbyStartGame(GameLogic::Protocol_LobbyStartGame& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Start: - void LobbyJoin(GameLogic::Protocol_LobbyJoin& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Join: + //void LobbyJoin(GameLogic::Protocol_LobbyJoin& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Join: void LobbyLogin(GameLogic::Protocol_LobbyLogin& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Login: void LobbyRefresh(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Refresh: - void LobbyMainData(GameLogic::Protocol_LobbyMainData& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_MainData: - void LobbyGameData(GameLogic::Protocol_LobbyGameData& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_GameData: + void LobbyMainData(GameLogic::Protocol_LobbyClientData& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_MainData: + //void LobbyGameData(GameLogic::Protocol_LobbyGameData& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_GameData: private: void ClientEventCallback(Oyster::Network::NetEvent e) override; @@ -45,7 +53,7 @@ namespace DanBias Utility::WinTimer timer; float refreshFrequency; GameSession gameSession; - GameSession::GameDescription description; + LobbyLevelData description; }; }//End namespace DanBias #endif // !DANBIASGAME_GAMELOBBY_H diff --git a/Code/Game/GameServer/GameSession.h b/Code/Game/GameServer/GameSession.h index 4685440a..cdae58e5 100644 --- a/Code/Game/GameServer/GameSession.h +++ b/Code/Game/GameServer/GameSession.h @@ -32,10 +32,8 @@ namespace DanBias struct GameDescription { int mapNumber; - int maxClients; int gameMode; int gameTime; - std::string gameName; Oyster::Network::NetworkSession* owner; Utility::DynamicMemory::DynamicArray clients; }; diff --git a/Code/Game/GameServer/Implementation/GameLobby.cpp b/Code/Game/GameServer/Implementation/GameLobby.cpp index 24ca7887..308e4ae6 100644 --- a/Code/Game/GameServer/Implementation/GameLobby.cpp +++ b/Code/Game/GameServer/Implementation/GameLobby.cpp @@ -9,6 +9,7 @@ using namespace Utility::DynamicMemory; using namespace Oyster::Network; using namespace Oyster; +using namespace GameLogic; namespace DanBias { @@ -30,17 +31,34 @@ namespace DanBias this->ProcessClients(); } - void GameLobby::SetGameDesc(const GameSession::GameDescription& desc) + void GameLobby::SetGameDesc(const LobbyLevelData& desc) { - this->description = desc; + this->description.gameMode = desc.gameMode; + this->description.gameTime = desc.gameTime; + this->description.mapNumber = desc.mapNumber; + this->description.maxClients = desc.maxClients; } - void GameLobby::GetGameDesc(GameSession::GameDescription& desc) + void GameLobby::GetGameDesc(LobbyLevelData& desc) { - desc = this->description; + desc.gameMode = this->description.gameMode; + desc.gameTime = this->description.gameTime; + desc.mapNumber = this->description.mapNumber; + desc.maxClients = this->description.maxClients; } bool GameLobby::StartGameSession() { - if(this->gameSession.Create(this->description)) + GameSession::GameDescription desc; + desc.gameMode = this->description.gameMode; + desc.gameTime = this->description.gameTime; + desc.mapNumber = this->description.mapNumber; + desc.owner = this; + while (this->GetClientCount()) + { + NetClient c; + if((c = this->Detach())) + desc.clients.Push(c); + } + if(this->gameSession.Create(desc)) { this->gameSession.Run(); return true; @@ -70,6 +88,10 @@ namespace DanBias { printf("New client(%i) connected - %s \n", client->GetID(), client->GetIpAddress().c_str()); Attach(client); + + Protocol_LobbyClientData p; + + client->Send(p.GetProtocol()); } }//End namespace DanBias \ No newline at end of file diff --git a/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp b/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp index 0de7b062..1c841519 100644 --- a/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp +++ b/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp @@ -17,16 +17,16 @@ void GameLobby::ParseProtocol(Oyster::Network::CustomNetProtocol& p, NetworkClie //break; case protocol_Lobby_Start: this->LobbyStartGame (Protocol_LobbyStartGame (p), c); break; - case protocol_Lobby_Join: this->LobbyJoin (Protocol_LobbyJoin (p), c); - break; + //case protocol_Lobby_Join: this->LobbyJoin (Protocol_LobbyJoin (p), c); + //break; case protocol_Lobby_Login: this->LobbyLogin (Protocol_LobbyLogin (p), c); break; case protocol_Lobby_Refresh: this->LobbyRefresh (Protocol_LobbyRefresh (p), c); break; - case protocol_Lobby_MainData: this->LobbyMainData (Protocol_LobbyMainData (p), c); - break; - case protocol_Lobby_GameData: this->LobbyGameData (Protocol_LobbyGameData (p), c); + case protocol_Lobby_MainData: this->LobbyMainData (Protocol_LobbyClientData (p), c); break; + //case protocol_Lobby_GameData: this->LobbyGameData (Protocol_LobbyGameData (p), c); + //break; } } @@ -62,17 +62,17 @@ void GameLobby::LobbyStartGame(GameLogic::Protocol_LobbyStartGame& p, Oyster::Ne { } -void GameLobby::LobbyJoin(GameLogic::Protocol_LobbyJoin& p, Oyster::Network::NetworkClient* c) -{ - //for (unsigned int i = 0; i < this->gameLobby.Size(); i++) - //{ - // if (this->gameLobby[i]->GetID() == p.value) - // { - // this->gameLobby[i]->Attach(Detach(c)); - // return; - // } - //} -} +//void GameLobby::LobbyJoin(GameLogic::Protocol_LobbyJoin& p, Oyster::Network::NetworkClient* c) +//{ +// //for (unsigned int i = 0; i < this->gameLobby.Size(); i++) +// //{ +// // if (this->gameLobby[i]->GetID() == p.value) +// // { +// // this->gameLobby[i]->Attach(Detach(c)); +// // return; +// // } +// //} +//} void GameLobby::LobbyLogin(GameLogic::Protocol_LobbyLogin& p, Oyster::Network::NetworkClient* c) { @@ -81,13 +81,13 @@ void GameLobby::LobbyRefresh(GameLogic::Protocol_LobbyRefresh& p, Oyster::Networ { //Dont need to handle this on the server... } -void GameLobby::LobbyMainData(GameLogic::Protocol_LobbyMainData& p, Oyster::Network::NetworkClient* c) -{ - -} -void GameLobby::LobbyGameData(GameLogic::Protocol_LobbyGameData& p, Oyster::Network::NetworkClient* c) +void GameLobby::LobbyMainData(GameLogic::Protocol_LobbyClientData& p, Oyster::Network::NetworkClient* c) { } +//void GameLobby::LobbyGameData(GameLogic::Protocol_LobbyGameData& p, Oyster::Network::NetworkClient* c) +//{ +// +//} diff --git a/Code/Game/GameServer/Implementation/GameServer.cpp b/Code/Game/GameServer/Implementation/GameServer.cpp index 0489a12d..416503e5 100644 --- a/Code/Game/GameServer/Implementation/GameServer.cpp +++ b/Code/Game/GameServer/Implementation/GameServer.cpp @@ -39,9 +39,6 @@ DanBiasServerReturn GameServerAPI::ServerInitiate(const ServerInitDesc& desc) return DanBiasServerReturn_Error; } GameSession::GameDescription d; - lobby.GetGameDesc(d); - d.gameName.assign(desc.serverName); - lobby.SetGameDesc(d); std::printf("Server created!\t-\t%s: [%i]\n\n", server.GetLanAddress().c_str(), desc.listenPort); @@ -95,59 +92,59 @@ GameServerAPI::GameServerInfo GameServerAPI::ServerGetInfo() } void GameServerAPI::GameSetMapId(const int& val) { - GameSession::GameDescription d; + LobbyLevelData d; lobby.GetGameDesc(d); d.mapNumber = val; lobby.SetGameDesc(d); } void GameServerAPI::GameSetMaxClients(const int& val) { - GameSession::GameDescription d; + LobbyLevelData d; lobby.GetGameDesc(d); d.maxClients = val; lobby.SetGameDesc(d); } void GameServerAPI::GameSetGameMode(const int& val) { - GameSession::GameDescription d; + LobbyLevelData d; lobby.GetGameDesc(d); d.gameMode = val; lobby.SetGameDesc(d); } void GameServerAPI::GameSetGameTime(const int& val) { - GameSession::GameDescription d; + LobbyLevelData d; lobby.GetGameDesc(d); d.gameTime = val; lobby.SetGameDesc(d); } int GameServerAPI::GameGetMapId() { - GameSession::GameDescription d; + LobbyLevelData d; lobby.GetGameDesc(d); return d.mapNumber; } int GameServerAPI::GameGetMaxClients() { - GameSession::GameDescription d; + LobbyLevelData d; lobby.GetGameDesc(d); return d.maxClients; } int GameServerAPI::GameGetGameMode() { - GameSession::GameDescription d; + LobbyLevelData d; lobby.GetGameDesc(d); return d.gameMode; } int GameServerAPI::GameGetGameTime() { - GameSession::GameDescription d; + LobbyLevelData d; lobby.GetGameDesc(d); return d.gameTime; } const char* GameServerAPI::GameGetGameName() { - GameSession::GameDescription d; + LobbyLevelData d; lobby.GetGameDesc(d); return d.gameName.c_str(); } diff --git a/Code/Network/NetworkAPI/NetworkSession.cpp b/Code/Network/NetworkAPI/NetworkSession.cpp index d1fc7fec..35352243 100644 --- a/Code/Network/NetworkAPI/NetworkSession.cpp +++ b/Code/Network/NetworkAPI/NetworkSession.cpp @@ -20,11 +20,9 @@ struct NetworkSession::PrivateSessionData NetworkClient::ClientEventFunction messageCallback; std::mutex clientListLock; NetworkSession* owner; //Where clients end up when session is closed. - int clientCount; int id; NetworkSession::PrivateSessionData() - : clientCount(0) - , owner(0) + : owner(0) , id(GID()) {} }; @@ -38,7 +36,7 @@ NetworkSession::NetworkSession(const NetworkSession& orig) { this->data->clients = orig.data->clients; this->data->owner = orig.data->owner; - this->data->clientCount = orig.data->clientCount; + this->clientCount = orig.clientCount; this->data->id = orig.data->id; this->data->messageCallback = orig.data->messageCallback; } @@ -47,7 +45,7 @@ const NetworkSession& NetworkSession::operator=(const NetworkSession& orig) { this->data->clients = orig.data->clients; this->data->owner = orig.data->owner; - this->data->clientCount = orig.data->clientCount; + this->clientCount = orig.clientCount; this->data->id = orig.data->id; this->data->messageCallback = orig.data->messageCallback; @@ -57,7 +55,7 @@ const NetworkSession& NetworkSession::operator=(const NetworkSession& orig) NetworkSession::~NetworkSession() { this->data->clients.Clear(); - this->data->clientCount = 0; + this->clientCount = 0; this->data->messageCallback = 0; delete this->data; this->data = 0; @@ -91,7 +89,7 @@ bool NetworkSession::Attach(NetClient client) this->data->clients[k] = client; } - this->data->clientCount++; + this->clientCount++; client->SetOwner(this); this->data->clientListLock.unlock(); @@ -99,7 +97,7 @@ bool NetworkSession::Attach(NetClient client) return true; } -void NetworkSession::Detach() +void NetworkSession::DetachAll() { if(this->data->owner) { @@ -117,6 +115,7 @@ void NetworkSession::Detach() this->data->clients[i] = 0; } } + this->clientCount = 0; } NetClient NetworkSession::Detach(const NetworkClient* client) @@ -131,7 +130,7 @@ NetClient NetworkSession::Detach(const NetworkClient* client) { val = this->data->clients[i]; this->data->clients[i] = 0; - this->data->clientCount--; + this->clientCount--; } } @@ -152,7 +151,29 @@ NetClient NetworkSession::Detach(short ID) { val = this->data->clients[i]; this->data->clients[i] = 0; - this->data->clientCount--; + this->clientCount--; + } + } + + this->data->clientListLock.unlock(); + + return val; +} + +NetClient NetworkSession::Detach() +{ + NetClient val; + + this->data->clientListLock.lock(); + + for (unsigned int i = 0; i < this->data->clients.Size(); i++) + { + if(this->data->clients[i]) + { + val = this->data->clients[i]; + this->data->clients[i] = 0; + this->clientCount--; + break; } } @@ -204,7 +225,7 @@ void NetworkSession::CloseSession(bool dissconnectClients) } this->data->clients.Clear(); - this->data->clientCount = 0; + this->clientCount = 0; this->data->clientListLock.unlock(); } @@ -214,16 +235,6 @@ void NetworkSession::SetOwner(NetworkSession* owner) this->data->owner = owner; } -int NetworkSession::GetClientCount() const -{ - int c = 0; - for (unsigned int i = 0; i < this->data->clients.Size(); i++) - { - if(this->data->clients[i]) c++; - } - return c; -} - void NetworkSession::ClientConnectedEvent(NetClient client) { this->Attach(client); diff --git a/Code/Network/NetworkAPI/NetworkSession.h b/Code/Network/NetworkAPI/NetworkSession.h index b8287b36..ffe047a4 100644 --- a/Code/Network/NetworkAPI/NetworkSession.h +++ b/Code/Network/NetworkAPI/NetworkSession.h @@ -11,6 +11,7 @@ #include "NetworkServerEventStruct.h" #include "NetworkClient.h" #include "Utilities.h" +#include "DynamicArray.h" namespace Oyster { @@ -38,7 +39,7 @@ namespace Oyster * Detaches all clients and sends them to owner. * If no owner is set the clients is disconnected. */ - virtual void Detach(); + virtual void DetachAll(); /** * @@ -50,6 +51,11 @@ namespace Oyster */ virtual NetClient Detach(short ID); + /** + * + */ + virtual NetClient Detach(); + /** Send a message to all clients in this session * @param message The message */ @@ -74,7 +80,7 @@ namespace Oyster /** Get the number of clients active in this session * @return The client count */ - int GetClientCount() const; + inline int GetClientCount() const { return this->clientCount; } /** * @@ -87,6 +93,7 @@ namespace Oyster virtual void ClientEventCallback(NetEvent e) = 0; private: + int clientCount; struct PrivateSessionData; PrivateSessionData* data; };