From ce89e6bc980709a82d98d44bbfcfd09181ef0463 Mon Sep 17 00:00:00 2001 From: Dennis Andersen Date: Mon, 13 Jan 2014 12:44:33 +0100 Subject: [PATCH] GameServer - Fixed some minor bugs, one that made server crash on exit. Also integrated some of GameLogics new API functions --- Code/Dokumentation/GameServer.uxf | 10 +- Code/Dokumentation/Threading.uxf | 58 +--- Code/Game/DanBiasGame/DLLMain.cpp | 1 - Code/Game/DanBiasGame/Include/DanBiasGame.h | 3 + .../AdminInterface/AdminInterface.h | 3 + Code/Game/DanBiasServer/DanBiasServer.vcxproj | 5 +- Code/Game/DanBiasServer/DanBiasServerAPI.h | 6 +- Code/Game/DanBiasServer/GameServer.cpp | 14 +- Code/Game/DanBiasServer/GameServer.h | 3 + .../DanBiasServer/GameSession/GameClient.cpp | 45 ++- .../DanBiasServer/GameSession/GameClient.h | 20 +- .../DanBiasServer/GameSession/GameSession.cpp | 326 ------------------ .../DanBiasServer/GameSession/GameSession.h | 38 +- .../GameSession/GameSession_Events.cpp | 114 ++++++ .../GameSession/GameSession_General.cpp | 167 +++++++++ .../GameSession/GameSession_Logic.cpp | 37 ++ .../GameSession/GameSession_Network.cpp | 29 ++ Code/Game/DanBiasServer/Helpers/MapManager.h | 3 + .../LobbySessions/INetworkSession.h | 3 + .../DanBiasServer/LobbySessions/LobbyClient.h | 3 + .../DanBiasServerLauncher/ServerLauncher.cpp | 2 +- Code/Game/GameLogic/Game.cpp | 8 +- Code/Game/GameLogic/Game.h | 8 +- Code/Game/GameLogic/Player.cpp | 2 +- Code/Game/GameLogic/Player.h | 3 +- Code/Misc/Thread/OysterThread.h | 4 +- Code/Misc/Thread/OysterThread_Impl.cpp | 16 +- Code/Misc/Utilities-Impl.h | 3 +- Code/Network/NetworkAPI/NetworkServer.cpp | 2 +- Code/OysterGraphics/Model/Model.h | 2 + 30 files changed, 508 insertions(+), 430 deletions(-) delete mode 100644 Code/Game/DanBiasServer/GameSession/GameSession.cpp create mode 100644 Code/Game/DanBiasServer/GameSession/GameSession_Events.cpp create mode 100644 Code/Game/DanBiasServer/GameSession/GameSession_General.cpp create mode 100644 Code/Game/DanBiasServer/GameSession/GameSession_Logic.cpp create mode 100644 Code/Game/DanBiasServer/GameSession/GameSession_Network.cpp diff --git a/Code/Dokumentation/GameServer.uxf b/Code/Dokumentation/GameServer.uxf index 0b15fff4..9b416e02 100644 --- a/Code/Dokumentation/GameServer.uxf +++ b/Code/Dokumentation/GameServer.uxf @@ -423,14 +423,14 @@ INetworkSession UMLClass - 170 - 400 - 130 - 90 + 150 + 420 + 160 + 50 MapManager -- -Manages and makes sure maps are avalible to all clinents. +Manages all map stuff. elementstyle=wordwrap diff --git a/Code/Dokumentation/Threading.uxf b/Code/Dokumentation/Threading.uxf index 9628a18c..30499d26 100644 --- a/Code/Dokumentation/Threading.uxf +++ b/Code/Dokumentation/Threading.uxf @@ -1,12 +1,12 @@ - + 12 com.umlet.element.Class 540 - 456 - 132 + 468 + 336 36 OysterThread @@ -16,9 +16,9 @@ com.umlet.element.Class 372 - 408 + 396 120 - 36 + 48 /Some class/ @@ -29,11 +29,11 @@ 336 300 228 - 132 + 120 lt=>>>>>- <<implements>> - 96;108;96;60;204;60 + 96;96;96;60;204;60 com.umlet.element.Relation @@ -41,52 +41,30 @@ 396 408 168 - 96 + 108 lt=<<<<- Creates>> - 36;36;36;72;132;72;144;72 + 36;36;36;84;132;84;144;84 com.umlet.element.Relation - 564 - 372 + 660 + 384 60 108 lt=<<<- 36;84;36;36 - - com.umlet.element.Class - - 540 - 540 - 132 - 36 - - OysterMutex - - - - com.umlet.element.Relation - - 564 - 456 - 60 - 108 - - lt=<<<<- - 36;36;36;84 - UMLClass 540 312 336 - 96 + 108 /<<interface>>/ IThreadObject @@ -123,16 +101,4 @@ bg=red lt=- 36;36;36;108 - - com.umlet.element.Relation - - 336 - 408 - 228 - 168 - - lt=>>. -uses - 60;36;60;144;204;144 - diff --git a/Code/Game/DanBiasGame/DLLMain.cpp b/Code/Game/DanBiasGame/DLLMain.cpp index e2d438ba..dfd6ca1a 100644 --- a/Code/Game/DanBiasGame/DLLMain.cpp +++ b/Code/Game/DanBiasGame/DLLMain.cpp @@ -3,6 +3,5 @@ BOOL WINAPI DllMain( _In_ HINSTANCE hinstDLL, _In_ DWORD fdwReason, _In_ LPVOID lpvReserved ) { - //MessageBox(0, L"DanBiasGame Loaded", 0, 0); return TRUE; } \ No newline at end of file diff --git a/Code/Game/DanBiasGame/Include/DanBiasGame.h b/Code/Game/DanBiasGame/Include/DanBiasGame.h index 2041e6fe..184960e3 100644 --- a/Code/Game/DanBiasGame/Include/DanBiasGame.h +++ b/Code/Game/DanBiasGame/Include/DanBiasGame.h @@ -1,3 +1,6 @@ +///////////////////////////////////////// +// Created by [Dennis Andersen] [2013] // +///////////////////////////////////////// #ifndef DANBIASGAME_DANBIASGAME_H #define DANBIASGAME_DANBIASGAME_H diff --git a/Code/Game/DanBiasServer/AdminInterface/AdminInterface.h b/Code/Game/DanBiasServer/AdminInterface/AdminInterface.h index afb00973..4acc20a2 100644 --- a/Code/Game/DanBiasServer/AdminInterface/AdminInterface.h +++ b/Code/Game/DanBiasServer/AdminInterface/AdminInterface.h @@ -1,3 +1,6 @@ +///////////////////////////////////////// +// Created by [Dennis Andersen] [2013] // +///////////////////////////////////////// #ifndef DANBIASSERVER_ADMIN_INTERFACE_H #define DANBIASSERVER_ADMIN_INTERFACE_H diff --git a/Code/Game/DanBiasServer/DanBiasServer.vcxproj b/Code/Game/DanBiasServer/DanBiasServer.vcxproj index 529ebdc0..82b62bee 100644 --- a/Code/Game/DanBiasServer/DanBiasServer.vcxproj +++ b/Code/Game/DanBiasServer/DanBiasServer.vcxproj @@ -179,10 +179,13 @@ + + + - + diff --git a/Code/Game/DanBiasServer/DanBiasServerAPI.h b/Code/Game/DanBiasServer/DanBiasServerAPI.h index 1165ba48..1d514053 100644 --- a/Code/Game/DanBiasServer/DanBiasServerAPI.h +++ b/Code/Game/DanBiasServer/DanBiasServerAPI.h @@ -1,6 +1,6 @@ -///////////////////////////////////////////////////////////////////// -// Created by [Dennis Andersen] [2013] -///////////////////////////////////////////////////////////////////// +///////////////////////////////////////// +// Created by [Dennis Andersen] [2013] // +///////////////////////////////////////// #ifndef DANBIAS_SERVER_DANBIAS_SERVER_H #define DANBIAS_SERVER_DANBIAS_SERVER_H diff --git a/Code/Game/DanBiasServer/GameServer.cpp b/Code/Game/DanBiasServer/GameServer.cpp index a5210123..1da2b5ba 100644 --- a/Code/Game/DanBiasServer/GameServer.cpp +++ b/Code/Game/DanBiasServer/GameServer.cpp @@ -22,6 +22,7 @@ namespace DanBias { using namespace Oyster::Network; + GameServer* GameServer::instance = 0; void GameServer::NetworkCallback(NetworkClient* client) { @@ -38,7 +39,7 @@ namespace DanBias desc.clients.Push(c); desc.exitDestionation = this->mainLobby; if((sessionId = GameSessionManager::AddSession(desc, true)) == 0) - printf("Failed to create a game session"); + printf("Failed to create a game session\n"); myTest = true; //myTest = new GameSession(); // @@ -68,7 +69,7 @@ namespace DanBias , maxClients(0) , mainLobby(0) , server(0) - { } + { this->instance = this; } GameServer::~GameServer() { @@ -89,7 +90,6 @@ namespace DanBias if(!this->server->Init(serverDesc)) return DanBiasServerReturn_Error; if(!WindowShell::CreateConsoleWindow()) return DanBiasServerReturn_Error; - //if(!WindowShell::CreateWin(WindowShell::WINDOW_INIT_DESC())) return DanBiasServerReturn_Error; this->initiated = true; return DanBiasServerReturn_Sucess; @@ -108,10 +108,9 @@ namespace DanBias { if(!WindowShell::Frame()) break; - this->mainLobby->Frame(); - if(GetAsyncKeyState(0x51)) + if(GetAsyncKeyState(0x51)) //Q for exit break; } @@ -128,6 +127,11 @@ namespace DanBias return DanBiasServerReturn_Sucess; } + NetworkSession* GameServer::MainLobbyInstance() + { + return GameServer::instance->mainLobby; + } + bool GameServer::LoadIniFile(InitData& ini) { std::ifstream in; diff --git a/Code/Game/DanBiasServer/GameServer.h b/Code/Game/DanBiasServer/GameServer.h index 2461ee9a..ccdb6e05 100644 --- a/Code/Game/DanBiasServer/GameServer.h +++ b/Code/Game/DanBiasServer/GameServer.h @@ -21,6 +21,8 @@ namespace DanBias DanBiasServerReturn Run(); DanBiasServerReturn Release(); + static NetworkSession* MainLobbyInstance(); + private: //static void ClientConnectCallbackFunction(Oyster::Network::NetworkClient& connectedClient); void NetworkCallback(Oyster::Network::NetworkClient* client) override; @@ -31,6 +33,7 @@ namespace DanBias int maxClients; MainLobby *mainLobby; Oyster::Network::NetworkServer *server; + static GameServer* instance; private: struct InitData diff --git a/Code/Game/DanBiasServer/GameSession/GameClient.cpp b/Code/Game/DanBiasServer/GameSession/GameClient.cpp index 5e086e91..a856a751 100644 --- a/Code/Game/DanBiasServer/GameSession/GameClient.cpp +++ b/Code/Game/DanBiasServer/GameSession/GameClient.cpp @@ -4,16 +4,22 @@ #include "GameClient.h" #include "..\LobbySessions\NetworkSession.h" +#include #include using namespace Utility::DynamicMemory; using namespace DanBias; +using namespace GameLogic; -GameClient::GameClient(SmartPointer client, Oyster::Callback::OysterCallback value) +static int gameClientIDCount = 1; + +GameClient::GameClient(SmartPointer client, Game::PlayerData player, Oyster::Callback::OysterCallback value) { this->callbackValue = value; this->client = client; - this->player = new GameLogic::Player(); + //this->player = new GameLogic::Player(); + this->id = gameClientIDCount++; + this->player = player; Oyster::Callback::OysterCallback c; c.callbackType = Oyster::Callback::CallbackType_Object; c.value = this; @@ -22,8 +28,11 @@ GameClient::GameClient(SmartPointer client, Oyster::Callback::Oyste } GameClient::~GameClient() { - this->client->Disconnect(); - this->player.Release(); + if(this->client) this->client->Disconnect(); + this->player.playerID = 0; + this->player.teamID = 0; + this->id = -1; + //this->player.Release(); } void GameClient::SetCallback(Oyster::Callback::OysterCallback value) @@ -31,15 +40,35 @@ void GameClient::SetCallback(Oyster::Callback::OysterCallbackcallbackValue = value; } -GameLogic::Player* GameClient::GetPlayer() +//GameLogic::Player* GameClient::GetPlayer() +//{ +// return this->player.Get(); +//} +GameLogic::Game::PlayerData* GameClient::GetPlayer() { - return this->player.Get(); + return &this->player; } -LobbyClient* GameClient::GetClient() +GameLogic::Game::PlayerData GameClient::ReleasePlayer() +{ + GameLogic::Game::PlayerData temp = this->player; + this->player.playerID = 0; + this->player.teamID = 0; + return temp; +} +LobbyClient* GameClient::GetClient() const { return this->client; } - +Utility::DynamicMemory::SmartPointer GameClient::ReleaseClient() +{ + SmartPointer temp = this->client; + this->client = 0; + return temp; +} +int GameClient::GetID() const +{ + return this->id; +} void GameClient::ObjectCallback(NetworkSession::NetEvent e) { e.gameClient = this; diff --git a/Code/Game/DanBiasServer/GameSession/GameClient.h b/Code/Game/DanBiasServer/GameSession/GameClient.h index a79c3707..f3d10a77 100644 --- a/Code/Game/DanBiasServer/GameSession/GameClient.h +++ b/Code/Game/DanBiasServer/GameSession/GameClient.h @@ -6,28 +6,34 @@ #include "..\LobbySessions\LobbyClient.h" #include -#include +#include namespace DanBias { class GameClient: Oyster::Callback::CallbackObject { public: - GameClient(Utility::DynamicMemory::SmartPointer client, Oyster::Callback::OysterCallback value); + GameClient(Utility::DynamicMemory::SmartPointer client, GameLogic::Game::PlayerData player, Oyster::Callback::OysterCallback value); virtual~GameClient(); void SetCallback(Oyster::Callback::OysterCallback value); - GameLogic::Player* GetPlayer(); - LobbyClient* GetClient(); + //GameLogic::Player* GetPlayer(); + GameLogic::Game::PlayerData* GetPlayer(); + GameLogic::Game::PlayerData ReleasePlayer(); + + LobbyClient* GetClient() const; + Utility::DynamicMemory::SmartPointer ReleaseClient(); + int GetID() const; private: - Utility::DynamicMemory::SmartPointer player; + //Utility::DynamicMemory::SmartPointer player; + GameLogic::Game::PlayerData player; Utility::DynamicMemory::SmartPointer client; Oyster::Callback::OysterCallback callbackValue; - + int id; void ObjectCallback(NetworkSession::NetEvent) override; - + private: friend class AdminInterface; }; diff --git a/Code/Game/DanBiasServer/GameSession/GameSession.cpp b/Code/Game/DanBiasServer/GameSession/GameSession.cpp deleted file mode 100644 index 745e1369..00000000 --- a/Code/Game/DanBiasServer/GameSession/GameSession.cpp +++ /dev/null @@ -1,326 +0,0 @@ -///////////////////////////////////////////////////////////////////// -// Created by [Dennis Andersen] [2013] -///////////////////////////////////////////////////////////////////// -#include "GameSession.h" -#include "GameClient.h" - -#include -#include -#include - -#include - - -using namespace Utility::DynamicMemory; -using namespace Oyster; -using namespace Oyster::Network; -using namespace Oyster::Thread; -using namespace GameLogic; - -namespace DanBias -{ - void InsertObject(DynamicArray>& clients, SmartPointer obj) - { - for (unsigned int i = 0; i < clients.Size(); i++) - { - if(!clients[i]) - { - clients[i] = obj; - return; - } - } - clients.Push(obj); - } - void RemoveObject(DynamicArray>& clients, DanBias::GameClient* obj) - { - for (unsigned int i = 0; i < clients.Size(); i++) - { - if(clients[i] && clients[i]->GetClient()->GetID() == obj->GetClient()->GetID()) - { - clients[i] = 0; - return; - } - } - } - - GameSession::GameSession() - { - this->owner = 0; - this->box = 0; - this->isCreated = false; - this->isRunning = false; - } - GameSession::~GameSession() - { - delete this->box; - this->box = 0; - this->owner = 0; - } - - bool GameSession::Create(GameDescription& desc) - { - if(desc.clients.Size() == 0) return false; - if(!desc.owner) return false; - if(!desc.mapName.size()) return false; - if(this->isCreated) return false; - - this->clients.Resize(desc.clients.Size()); - - this->box = new PostBox(); - this->owner = desc.owner; - - Oyster::Callback::OysterCallback c; - c.value.callbackPostBox = this->box; - c.callbackType = Oyster::Callback::CallbackType_PostBox; - - for (unsigned int i = 0; i < desc.clients.Size(); i++) - { - this->clients[i] = new GameClient(desc.clients[i], c); - } - - this->isCreated = true; - return true; - } - - void GameSession::Run() - { - if(this->isRunning) return; - if(this->clients.Size() > 0) - { - if(this->worker.Create(this, true, true) != OYSTER_THREAD_ERROR_SUCCESS) return; - this->worker.SetPriority(OYSTER_THREAD_PRIORITY_2); - this->isRunning = true; - } - } - - bool GameSession::Join(Utility::DynamicMemory::SmartPointer client) - { - if(!this->isCreated) return false; - - Oyster::Callback::OysterCallback c; - c.value.callbackPostBox = this->box; - c.callbackType = Oyster::Callback::CallbackType_PostBox; - - SmartPointer obj = new GameClient(client, c); - InsertObject(this->clients, obj); - - return true; - } - void GameSession::CloseSession(bool dissconnectClients) - { - this->worker.Stop(false); - //NetworkSession::CloseSession(dissconnectClients); - this->isCreated = false; - this->isRunning = false; - } - -////private: //overriden Threading functions - bool GameSession::DoWork ( ) - { - this->Frame(); - - return true; - } - -#ifndef ERIK -////private: - void GameSession::Frame() - { - this->ParseEvents(); - - if(GetAsyncKeyState(VK_UP)) - { - Protocol_General_Status p(Protocol_General_Status::States_ready); - Send(p.GetProtocol()); - Sleep(100); - } - } - void GameSession::ParseEvents() - { - if( !this->box->IsEmpty() ) - { - NetworkSession::NetEvent &e = this->box->Fetch(); - - if(e.protocol[0].type != Oyster::Network::NetAttributeType_Short) return; - - if( ProtocolIsGameplay(e.protocol[protocol_INDEX_ID].value.netShort) ) - ParseGameplayEvent(e.protocol, e.gameClient); - - if( ProtocolIsGeneral(e.protocol[protocol_INDEX_ID].value.netShort) ) - ParseGeneralEvent(e.protocol, e.gameClient); - } - } - - void GameSession::ParseGameplayEvent(Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c) - { - switch (p[protocol_INDEX_ID].value.netShort) - { - case protocol_Gameplay_PlayerNavigation: - { - - Oyster::Math::Float4x4 world = Oyster::Math::Matrix::identity; - if(p[1].value.netBool) //bool bForward; - world.v[3].x = 2; - //c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_FORWARD); - if(p[2].value.netBool) //bool bBackward; - world.v[3].x = -2; - //c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_BACKWARD); - if(p[5].value.netBool) //bool bStrafeRight; - world.v[3].y = 2; - //c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_RIGHT); - if(p[6].value.netBool) //bool bStrafeLeft; - world.v[3].y = -2; - //c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_LEFT); - - Protocol_ObjectPosition res(world, 0); - Send(res.GetProtocol()); - - - } - break; - case protocol_Gameplay_PlayerMouseMovement: - - break; - case protocol_Gameplay_PlayerPosition: - - break; - case protocol_Gameplay_CreateObject: - - break; - case protocol_Gameplay_ObjectPosition: - - break; - } - } - void GameSession::ParseGeneralEvent(Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c) - { - switch (p[protocol_INDEX_ID].value.netShort) - { - case protocol_General_Status: - switch (p[1].value.netInt) - { - case GameLogic::Protocol_General_Status::States_bussy: - - break; - - case GameLogic::Protocol_General_Status::States_disconected: - printf("Client with ID [%i] dissconnected\n", c->GetClient()->GetID()); - RemoveObject(this->clients, c); - break; - - case GameLogic::Protocol_General_Status::States_idle: - break; - - case GameLogic::Protocol_General_Status::States_ready: - - break; - } - break; - - case protocol_General_Text: - - break; - } - } - void GameSession::Send(Oyster::Network::CustomNetProtocol* p) - { - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - if(this->clients[i] && this->clients[i]->GetClient()) - this->clients[i]->GetClient()->Send(p); - } - } - -#else -#include "DynamicObject.h" -//#include "CollisionManager.h" -//#include "GameLogicStates.h" -//#include - - /* - using namespace GameLogic; - //VARIABLES GOES HERE - DynamicObject* objectBox; - - bool GameSession::Init(GameSessionDescription& desc) - { - if(desc.clients.Size() == 0) return false; - this->box = new PostBox(); - this->owner = desc.owner; - for (unsigned int i = 0; i < desc.clients.Size(); i++) - { - desc.clients[i]->SetPostbox(this->box); - this->clients.Push(desc.clients[i]); - } - - CollisionManager::BoxCollision(0,0); - - objectBox = new DynamicObject(CollisionManager::BoxCollision, OBJECT_TYPE::OBJECT_TYPE_BOX); - - Protocol_CreateObject objectCreation; - objectCreation.object_ID = objectBox->GetID(); - objectCreation.path = "crate"; - Oyster::Math::Float4x4 worldMat = objectBox->GetRigidBody()->GetOrientation(); - - for (int i = 0; i < 16; i++) - { - objectCreation.worldMatrix[i] = worldMat[i]; - } - - for (int i = 0; i < clients.Size(); i++) - { - clients[i]->NetClient_Object()->Send(objectCreation); - } - - return true; - } - void GameSession::Frame() - { - } - void GameSession::ParseEvents() - { - if(this->box && !this->box->IsEmpty()) - { - NetEvent &e = this->box->Fetch(); - - if(e.protocol[0].type != Oyster::Network::NetAttributeType_Short) return; - - ParseProtocol(e.protocol, *e.reciever); - } - } - void GameSession::ParseProtocol(Oyster::Network::CustomNetProtocol& p, DanBias::ClientObject& c) - { - switch (p[0].value.netShort) - { - case protocol_Gamplay_PlayerNavigation: - { - if(p[1].value.netBool) //bool bForward; - c.Logic_Object()->Move(GameLogic::PLAYER_MOVEMENT_FORWARD); - if(p[2].value.netBool) //bool bBackward; - c.Logic_Object()->Move(GameLogic::PLAYER_MOVEMENT_BACKWARD); - if(p[5].value.netBool) //bool bStrafeRight; - c.Logic_Object()->Move(GameLogic::PLAYER_MOVEMENT_RIGHT); - if(p[6].value.netBool) //bool bStrafeLeft; - c.Logic_Object()->Move(GameLogic::PLAYER_MOVEMENT_LEFT); - } - break; - case protocol_Gamplay_PlayerMouseMovement: - - break; - case protocol_Gamplay_PlayerPosition: - - break; - case protocol_Gamplay_CreateObject: - - break; - case protocol_Gamplay_ObjectPosition: - - break; - } - } - */ - -#endif - -}//End namespace DanBias - diff --git a/Code/Game/DanBiasServer/GameSession/GameSession.h b/Code/Game/DanBiasServer/GameSession/GameSession.h index a128cc74..c4318db4 100644 --- a/Code/Game/DanBiasServer/GameSession/GameSession.h +++ b/Code/Game/DanBiasServer/GameSession/GameSession.h @@ -4,12 +4,14 @@ #ifndef DANBIASSERVER_GAME_SESSION_H #define DANBIASSERVER_GAME_SESSION_H -//warning C4150: deletion of pointer to incomplete type, no destructor called +//warning C4150: deletion of pointer to incomplete type, no destructor called, because of forward decleration and the use of smartpointer. #pragma warning(disable: 4150) #include "..\LobbySessions\NetworkSession.h" +#include #include #include +#include namespace DanBias { @@ -18,6 +20,9 @@ namespace DanBias class GameSession : public Oyster::Thread::IThreadObject, public INetworkSession { public: + /** + * A container to use when initiating a new session + */ struct GameDescription { std::wstring mapName; @@ -41,31 +46,44 @@ namespace DanBias bool Join(Utility::DynamicMemory::SmartPointer client); /** - * + * Closes the game session + * @param disconnectClients If set to true clients is dissconnected from the server, if false the clients is sent to the given owner of the session. */ - void CloseSession(bool dissconnectClients = false); - + void CloseSession(bool disconnectClients = false); inline bool IsCreated() const { return this->isCreated; } inline bool IsRunning() const { return this->isRunning; } - + //Private member functions private: - void Frame(); + //Handles all events recieved void ParseEvents(); + //Handles all gameplay events void ParseGameplayEvent(Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c); + //Handles all general events void ParseGeneralEvent(Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c); - + //Adds a client to the client list + void AddClient(Utility::DynamicMemory::SmartPointer obj); + //Removes a client from the client list + void RemoveClient(DanBias::GameClient* obj); + //Sends a protocol ta all clients in session void Send(Oyster::Network::CustomNetProtocol* p); - - private: //overriden Threading functions + //Frame function, derived from IThreadObject bool DoWork ( ) override; + //Sends a client to the owner, if obj is NULL then all clients is sent + void SendToOwner(DanBias::GameClient* obj); + //Do a cleanup on all the private data + void Clean(); + //Private member variables private: + Utility::DynamicMemory::DynamicArray> clients; - NetworkSession* owner; Oyster::IPostBox *box; Oyster::Thread::OysterThread worker; + GameLogic::Game gameInstance; + NetworkSession* owner; + Utility::WinTimer timer; bool isCreated; bool isRunning; diff --git a/Code/Game/DanBiasServer/GameSession/GameSession_Events.cpp b/Code/Game/DanBiasServer/GameSession/GameSession_Events.cpp new file mode 100644 index 00000000..b8906867 --- /dev/null +++ b/Code/Game/DanBiasServer/GameSession/GameSession_Events.cpp @@ -0,0 +1,114 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// +#include "GameSession.h" +#include "GameClient.h" + +#include +#include +#include +#include + +#include + + +using namespace Utility::DynamicMemory; +using namespace Oyster; +using namespace Oyster::Network; +using namespace Oyster::Thread; +using namespace GameLogic; + +namespace DanBias +{ + void GameSession::ParseEvents() + { + if( !this->box->IsEmpty() ) + { + NetworkSession::NetEvent &e = this->box->Fetch(); + + if(e.protocol[0].type != Oyster::Network::NetAttributeType_Short) return; + + if( ProtocolIsGameplay(e.protocol[protocol_INDEX_ID].value.netShort) ) + ParseGameplayEvent(e.protocol, e.gameClient); + + if( ProtocolIsGeneral(e.protocol[protocol_INDEX_ID].value.netShort) ) + ParseGeneralEvent(e.protocol, e.gameClient); + } + } + + void GameSession::ParseGameplayEvent(Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c) + { + switch (p[protocol_INDEX_ID].value.netShort) + { + case protocol_Gameplay_PlayerNavigation: + { + + Oyster::Math::Float4x4 world = Oyster::Math::Matrix::identity; + if(p[1].value.netBool) //bool bForward; + world.v[3].x = 2; + //c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_FORWARD); + if(p[2].value.netBool) //bool bBackward; + world.v[3].x = -2; + //c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_BACKWARD); + if(p[5].value.netBool) //bool bStrafeRight; + world.v[3].y = 2; + //c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_RIGHT); + if(p[6].value.netBool) //bool bStrafeLeft; + world.v[3].y = -2; + //c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_LEFT); + + Protocol_ObjectPosition res(world, 0); + Send(res.GetProtocol()); + + + } + break; + case protocol_Gameplay_PlayerMouseMovement: + + break; + case protocol_Gameplay_PlayerPosition: + + break; + case protocol_Gameplay_CreateObject: + + break; + case protocol_Gameplay_ObjectPosition: + + break; + } + } + + void GameSession::ParseGeneralEvent(Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c) + { + switch (p[protocol_INDEX_ID].value.netShort) + { + case protocol_General_Status: + switch (p[1].value.netInt) + { + case GameLogic::Protocol_General_Status::States_bussy: + + break; + + case GameLogic::Protocol_General_Status::States_disconected: + printf("Client with ID [%i] dissconnected\n", c->GetClient()->GetID()); + this->RemoveClient(c); + break; + + case GameLogic::Protocol_General_Status::States_idle: + + break; + + case GameLogic::Protocol_General_Status::States_ready: + + break; + } + break; + + case protocol_General_Text: + + break; + } + } + +}//End namespace DanBias + diff --git a/Code/Game/DanBiasServer/GameSession/GameSession_General.cpp b/Code/Game/DanBiasServer/GameSession/GameSession_General.cpp new file mode 100644 index 00000000..e0ba12ee --- /dev/null +++ b/Code/Game/DanBiasServer/GameSession/GameSession_General.cpp @@ -0,0 +1,167 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// +#include "GameSession.h" +#include "GameClient.h" +#include "..\GameServer.h" +#include +#include +#include + +#include + + +using namespace Utility::DynamicMemory; +using namespace Oyster; +using namespace Oyster::Network; +using namespace Oyster::Thread; +using namespace GameLogic; + +namespace DanBias +{ + GameSession::GameSession() + { + this->owner = 0; + this->box = 0; + this->isCreated = false; + this->isRunning = false; + } + + GameSession::~GameSession() + { + delete this->box; + this->box = 0; + this->owner = 0; + } + + bool GameSession::Create(GameDescription& desc) + { + if(desc.clients.Size() == 0) return false; + if(!desc.owner) return false; + if(!desc.mapName.size()) return false; + if(this->isCreated) return false; + + this->clients.Resize(desc.clients.Size()); + + this->box = new PostBox(); + this->owner = desc.owner; + + Oyster::Callback::OysterCallback c; + c.value.callbackPostBox = this->box; + c.callbackType = Oyster::Callback::CallbackType_PostBox; + + for (unsigned int i = 0; i < desc.clients.Size(); i++) + { + this->clients[i] = new GameClient(desc.clients[i], gameInstance.CreatePlayer(), c); + } + + if(this->worker.Create(this, true, true) != OYSTER_THREAD_ERROR_SUCCESS) return false; + + this->isCreated = true; + + return true; + } + + void GameSession::Run() + { + + if(this->isRunning) return; + + if(this->clients.Size() > 0) + { + + this->worker.SetPriority(OYSTER_THREAD_PRIORITY_2); + this->isRunning = true; + } + } + + bool GameSession::Join(Utility::DynamicMemory::SmartPointer client) + { + if(!this->isCreated) return false; + + Oyster::Callback::OysterCallback c; + c.value.callbackPostBox = this->box; + c.callbackType = Oyster::Callback::CallbackType_PostBox; + + SmartPointer obj = new GameClient(client, this->gameInstance.CreatePlayer(), c); + AddClient(obj); + + return true; + } + + void GameSession::CloseSession(bool dissconnectClients) + { + if(dissconnectClients) + { + for (int i = 0; i < this->clients.Size(); i++) + { + this->clients[i]->GetClient()->Disconnect(); + } + } + else + { + this->SendToOwner(0); //Send all clients to the current owner + } + this->Clean(); + } + + void GameSession::AddClient(SmartPointer obj) + { + for (unsigned int i = 0; i < clients.Size(); i++) + { + if(!clients[i]) + { + clients[i] = obj; + return; + } + } + clients.Push(obj); + } + + void GameSession::RemoveClient(DanBias::GameClient* obj) + { + for (unsigned int i = 0; i < clients.Size(); i++) + { + if(clients[i] && clients[i]->GetID() == obj->GetID()) + { + clients[i] = 0; + return; + } + } + } + + void GameSession::SendToOwner(DanBias::GameClient* obj) + { + DanBias::NetworkSession *s = GameServer::MainLobbyInstance(); + + if(this->owner) s = this->owner; + + if(obj) + { + s->Attach(obj->ReleaseClient()); + RemoveClient(obj); + } + else + { + for (int i = 0; i < this->clients.Size(); i++) + { + s->Attach(this->clients[i]->ReleaseClient()); + RemoveClient(this->clients[i]); + } + } + } + + void GameSession::Clean() + { + this->worker.Terminate(); + this->clients.Clear(); + delete this->box; + this->box = 0; + this->gameInstance; + this->owner = 0; + this->isCreated = false; + this->isRunning = false; + } + +}//End namespace DanBias + diff --git a/Code/Game/DanBiasServer/GameSession/GameSession_Logic.cpp b/Code/Game/DanBiasServer/GameSession/GameSession_Logic.cpp new file mode 100644 index 00000000..ab1bcdf5 --- /dev/null +++ b/Code/Game/DanBiasServer/GameSession/GameSession_Logic.cpp @@ -0,0 +1,37 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// +#include "GameSession.h" +#include "GameClient.h" + +#include +#include +#include +#include + +using namespace Utility::DynamicMemory; +using namespace Oyster; +using namespace Oyster::Network; +using namespace Oyster::Thread; +using namespace GameLogic; + +namespace DanBias +{ + bool GameSession::DoWork( ) + { + this->gameInstance.NewFrame(); + + this->ParseEvents(); + + if(GetAsyncKeyState(VK_UP)) + { + Protocol_General_Status p(Protocol_General_Status::States_ready); + Send(p.GetProtocol()); + Sleep(100); + } + + return this->isRunning; + } + +}//End namespace DanBias + diff --git a/Code/Game/DanBiasServer/GameSession/GameSession_Network.cpp b/Code/Game/DanBiasServer/GameSession/GameSession_Network.cpp new file mode 100644 index 00000000..f23ab118 --- /dev/null +++ b/Code/Game/DanBiasServer/GameSession/GameSession_Network.cpp @@ -0,0 +1,29 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// +#include "GameSession.h" +#include "GameClient.h" + +#include +#include +#include +#include + +using namespace Utility::DynamicMemory; +using namespace Oyster; +using namespace Oyster::Network; +using namespace Oyster::Thread; +using namespace GameLogic; + +namespace DanBias +{ + void GameSession::Send(Oyster::Network::CustomNetProtocol* p) + { + for (unsigned int i = 0; i < this->clients.Size(); i++) + { + if(this->clients[i] && this->clients[i]->GetClient()) + this->clients[i]->GetClient()->Send(p); + } + } +}//End namespace DanBias + diff --git a/Code/Game/DanBiasServer/Helpers/MapManager.h b/Code/Game/DanBiasServer/Helpers/MapManager.h index 34999014..49342a56 100644 --- a/Code/Game/DanBiasServer/Helpers/MapManager.h +++ b/Code/Game/DanBiasServer/Helpers/MapManager.h @@ -1,3 +1,6 @@ +///////////////////////////////////////// +// Created by [Dennis Andersen] [2013] // +///////////////////////////////////////// #ifndef DANBIASSERVER_LEVELMANAGER_H #define DANBIASSERVER_LEVELMANAGER_H diff --git a/Code/Game/DanBiasServer/LobbySessions/INetworkSession.h b/Code/Game/DanBiasServer/LobbySessions/INetworkSession.h index 4495739f..dfad66ae 100644 --- a/Code/Game/DanBiasServer/LobbySessions/INetworkSession.h +++ b/Code/Game/DanBiasServer/LobbySessions/INetworkSession.h @@ -1,3 +1,6 @@ +///////////////////////////////////////// +// Created by [Dennis Andersen] [2013] // +///////////////////////////////////////// #ifndef DANBIASSERVER_INETWORKSESSION_H #define DANBIASSERVER_INETWORKSESSION_H diff --git a/Code/Game/DanBiasServer/LobbySessions/LobbyClient.h b/Code/Game/DanBiasServer/LobbySessions/LobbyClient.h index b827ca7e..33b99d78 100644 --- a/Code/Game/DanBiasServer/LobbySessions/LobbyClient.h +++ b/Code/Game/DanBiasServer/LobbySessions/LobbyClient.h @@ -1,3 +1,6 @@ +///////////////////////////////////////// +// Created by [Dennis Andersen] [2013] // +///////////////////////////////////////// #ifndef DANBIASSERVER_LOBBYCLIENT_H #define DANBIASSERVER_LOBBYCLIENT_H diff --git a/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp b/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp index 0d26c37d..17d1a045 100644 --- a/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp +++ b/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp @@ -1,5 +1,5 @@ ////////////////////////////////////////////////// -// Launcher to launch Danbias server or client // +// Launcher to launch Danbias server // // Created by [Dennis Andersen] [2013] // ////////////////////////////////////////////////// #define NOMINMAX //Blame it on windows diff --git a/Code/Game/GameLogic/Game.cpp b/Code/Game/GameLogic/Game.cpp index 806ac6d2..3e985cb8 100644 --- a/Code/Game/GameLogic/Game.cpp +++ b/Code/Game/GameLogic/Game.cpp @@ -1,6 +1,7 @@ #include "Game.h" #include "Player.h" #include "Level.h" +#include using namespace GameLogic; @@ -57,7 +58,12 @@ void Game::GetAllPlayerPos() Game::PlayerData Game::CreatePlayer() { - + PlayerData data; + //Yo mammma + data.playerID = GID(); + data.teamID = -1; + + return data; } void Game::CreateTeam() diff --git a/Code/Game/GameLogic/Game.h b/Code/Game/GameLogic/Game.h index a70dc842..3948f266 100644 --- a/Code/Game/GameLogic/Game.h +++ b/Code/Game/GameLogic/Game.h @@ -1,10 +1,16 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Erik Persson] [2013] +///////////////////////////////////////////////////////////////////// + #ifndef GAME_H #define GAME_H +#include "GameLogicDef.h" #include "GameLogicStates.h" + namespace GameLogic { - class Game + class DANBIAS_GAMELOGIC_DLL Game { public: diff --git a/Code/Game/GameLogic/Player.cpp b/Code/Game/GameLogic/Player.cpp index a3702a53..08af0b9e 100644 --- a/Code/Game/GameLogic/Player.cpp +++ b/Code/Game/GameLogic/Player.cpp @@ -132,7 +132,7 @@ Oyster::Math::Float3 Player::GetPos() Oyster::Math::Float3 Player::GetLookDir() { - return myData->lookDir; + return myData->lookDir.xyz; } int Player::GetTeamID() diff --git a/Code/Game/GameLogic/Player.h b/Code/Game/GameLogic/Player.h index e9ad03da..0743dc00 100644 --- a/Code/Game/GameLogic/Player.h +++ b/Code/Game/GameLogic/Player.h @@ -4,14 +4,13 @@ #ifndef PLAYER_H #define PLAYER_H #include "GameLogicStates.h" -#include "GameLogicDef.h" #include "OysterMath.h" #include "Object.h" namespace GameLogic { - class DANBIAS_GAMELOGIC_DLL Player : public Object + class Player : public Object { public: diff --git a/Code/Misc/Thread/OysterThread.h b/Code/Misc/Thread/OysterThread.h index 84c32144..cd4bfd3f 100644 --- a/Code/Misc/Thread/OysterThread.h +++ b/Code/Misc/Thread/OysterThread.h @@ -71,13 +71,13 @@ namespace Oyster //OYSTER_THREAD_ERROR Create(Oyster::Callback::CallbackObject* worker, bool start, bool detach = false); //OYSTER_THREAD_ERROR Create(Oyster::Callback::CallbackFunction::FNC worker, bool start, bool detach = false); OYSTER_THREAD_ERROR Start(); - OYSTER_THREAD_ERROR Stop(bool wait = false); + OYSTER_THREAD_ERROR Stop(); OYSTER_THREAD_ERROR Pause(); OYSTER_THREAD_ERROR Pause(int mSec); OYSTER_THREAD_ERROR Resume(); OYSTER_THREAD_ERROR SetWorker(IThreadObject* worker = 0); OYSTER_THREAD_ERROR SetWorker(ThreadFnc worker = 0); - OYSTER_THREAD_ERROR Terminate(bool wait = true); + OYSTER_THREAD_ERROR Terminate(); OYSTER_THREAD_ERROR Wait(); OYSTER_THREAD_ERROR Wait(int mSec); OYSTER_THREAD_ERROR Swap(const OysterThread* other); diff --git a/Code/Misc/Thread/OysterThread_Impl.cpp b/Code/Misc/Thread/OysterThread_Impl.cpp index 16668c04..534c992f 100644 --- a/Code/Misc/Thread/OysterThread_Impl.cpp +++ b/Code/Misc/Thread/OysterThread_Impl.cpp @@ -72,10 +72,10 @@ using namespace Utility::DynamicMemory; ~RefData() { //threadWaitFunctionLock.lock(); - Terminate(true); + Terminate(); //threadWaitFunctionLock.unlock(); } - OYSTER_THREAD_ERROR Terminate(bool wait) + OYSTER_THREAD_ERROR Terminate() { if(!threadData) return OYSTER_THREAD_ERROR_SUCCESS; @@ -129,12 +129,12 @@ using namespace Utility::DynamicMemory; data = new RefData(); return data->Create(fnc, worker, start, detach); } - OYSTER_THREAD_ERROR Terminate(bool wait) + OYSTER_THREAD_ERROR Terminate() { if(!data) return OYSTER_THREAD_ERROR_FAILED; - return data->Terminate(wait); + return data->Terminate(); } }; @@ -256,9 +256,9 @@ OYSTER_THREAD_ERROR OysterThread::Start() return OYSTER_THREAD_ERROR_SUCCESS; } -OYSTER_THREAD_ERROR OysterThread::Stop(bool wait) +OYSTER_THREAD_ERROR OysterThread::Stop() { - return this->Terminate(wait); + return this->Terminate(); } OYSTER_THREAD_ERROR OysterThread::Pause() { @@ -297,9 +297,9 @@ OYSTER_THREAD_ERROR OysterThread::SetWorker(ThreadFnc worker) return OYSTER_THREAD_ERROR_SUCCESS;; } -OYSTER_THREAD_ERROR OysterThread::Terminate(bool wait) +OYSTER_THREAD_ERROR OysterThread::Terminate() { - return this->privateData->Terminate(wait); + return this->privateData->Terminate(); } OYSTER_THREAD_ERROR OysterThread::Wait() { diff --git a/Code/Misc/Utilities-Impl.h b/Code/Misc/Utilities-Impl.h index 6ede28d0..476861f6 100644 --- a/Code/Misc/Utilities-Impl.h +++ b/Code/Misc/Utilities-Impl.h @@ -292,7 +292,8 @@ namespace Utility this->_ptr = p; - if(p) this->_rc->Incref(); + if(p) this->_rc->Incref(); + else this->_rc = 0; } return *this; } diff --git a/Code/Network/NetworkAPI/NetworkServer.cpp b/Code/Network/NetworkAPI/NetworkServer.cpp index e47195bf..ec4da231 100644 --- a/Code/Network/NetworkAPI/NetworkServer.cpp +++ b/Code/Network/NetworkAPI/NetworkServer.cpp @@ -115,7 +115,7 @@ void NetworkServer::PrivateData::Stop() started = false; - thread.Stop(true); + thread.Stop(); } void NetworkServer::PrivateData::Shutdown() diff --git a/Code/OysterGraphics/Model/Model.h b/Code/OysterGraphics/Model/Model.h index 0103b143..9fa5fb66 100644 --- a/Code/OysterGraphics/Model/Model.h +++ b/Code/OysterGraphics/Model/Model.h @@ -9,9 +9,11 @@ namespace Oyster { namespace Model { + //struct ModelInfo; struct Model { //! do not Edit, linked to render data + //ModelInfo* info; void* info; Oyster::Math::Float4x4 WorldMatrix; bool Visible;