diff --git a/Bin/Content/crate.mtl b/Bin/Content/crate.mtl index 7c53e55a..6cce58e4 100644 --- a/Bin/Content/crate.mtl +++ b/Bin/Content/crate.mtl @@ -3,6 +3,6 @@ illum 4 Kd 0.00 0.00 0.00 Ka 0.00 0.00 0.00 Tf 1.00 1.00 1.00 -map_Kd tex_crate.png +map_Kd crate.png Ni 1.00 Ks 0.50 0.50 0.50 diff --git a/Bin/Content/tex_worldDummy.jpg b/Bin/Content/tex_worldDummy.jpg deleted file mode 100644 index 102867af..00000000 Binary files a/Bin/Content/tex_worldDummy.jpg and /dev/null differ diff --git a/Bin/Content/worldDummy.mtl b/Bin/Content/worldDummy.mtl index b961efdd..ba6007f6 100644 --- a/Bin/Content/worldDummy.mtl +++ b/Bin/Content/worldDummy.mtl @@ -3,6 +3,6 @@ illum 4 Kd 0.00 0.00 0.00 Ka 0.00 0.00 0.00 Tf 1.00 1.00 1.00 -map_Kd tex_worldDummy.jpg +map_Kd worldDummy.jpg Ni 1.00 Ks 0.50 0.50 0.50 diff --git a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp index 5dea4581..57d47292 100644 --- a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp +++ b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp @@ -24,7 +24,7 @@ namespace DanBias Oyster::Network::NetworkClient* nwClient; Client::GameClientState* gameClientState; - void ProtocolRecievedCallback(Oyster::Network::CustomNetProtocol& p) override + void NetworkCallback(Oyster::Network::CustomNetProtocol& p) override { int pType = p[0].value.netInt; switch (pType) @@ -159,7 +159,7 @@ namespace DanBias m_data->recieverObj = new MyRecieverObject; - m_data->recieverObj->nwClient = new Oyster::Network::NetworkClient(m_data->recieverObj, Oyster::Network::NetworkProtocolCallbackType_Object); + // m_data->recieverObj->nwClient = new Oyster::Network::NetworkClient(m_data->recieverObj, Oyster::Network::NetworkProtocolCallbackType_Object); m_data->recieverObj->nwClient->Connect(desc.port, desc.IP); if (!m_data->recieverObj->nwClient->IsConnected()) diff --git a/Code/Game/DanBiasLauncher/Launcher.cpp b/Code/Game/DanBiasLauncher/Launcher.cpp index 10118e69..3d3c5fdc 100644 --- a/Code/Game/DanBiasLauncher/Launcher.cpp +++ b/Code/Game/DanBiasLauncher/Launcher.cpp @@ -17,6 +17,7 @@ int WINAPI WinMain( HINSTANCE hinst, HINSTANCE prevInst, PSTR cmdLine, int cmdSh // Game client starter code goes here DanBias::DanBiasGameDesc gameDesc; gameDesc.port = 15151; + //gameDesc.port = 15152; //gameDesc.IP = "193.11.184.196"; //gameDesc.IP = "193.11.184.31"; gameDesc.IP = "127.0.0.1"; diff --git a/Code/Game/DanBiasServer/AdminInterface/AdminInterface.cpp b/Code/Game/DanBiasServer/AdminInterface/AdminInterface.cpp new file mode 100644 index 00000000..80e2f9fe --- /dev/null +++ b/Code/Game/DanBiasServer/AdminInterface/AdminInterface.cpp @@ -0,0 +1,68 @@ +#define NOMINMAX +#include "AdminInterface.h" +#include +#include +#include "..\GameServer.h" +#include "..\GameSession\GameSessionManager.h" +#include "..\GameSession\GameSession.h" +#include "..\Helpers\ServerDataReader.h" +#include "..\LobbySessions\LobbyClient.h" +#include "..\LobbySessions\NetworkSession.h" +#include "..\LobbySessions\GameLobby.h" +#include "..\LobbySessions\MainLobby.h" + +using namespace Oyster::Network; +using namespace Oyster::Thread; +using namespace DanBias; + +struct AdminInstanceData :public IThreadObject +{ + bool isCreated; + Oyster::Network::NetworkServer adminServer; + NetworkClient* admin; + NetworkSession* parentInstance; + OysterThread worker; + + bool DoWork() override + { + //((GameServer*)parentInstance)->serve + return true; + } + +}adminInstanceData; + +void AdminArrived(NetworkClient* adm) +{ + if(adminInstanceData.admin) + { + delete adm; + return; + } + + adminInstanceData.admin = adm; + adminInstanceData.worker.Create(&adminInstanceData, true, true); +} + +void AdminInterface::Toggle(bool toggle, NetworkSession* parent) +{ + if(toggle) + { + if(!parent) return; + + if(adminInstanceData.isCreated) return; + + NetworkServer::INIT_DESC desc; + desc.port = 15152; + desc.callbackType = NetworkClientCallbackType_Function; + desc.recvObj.clientConnectFnc = AdminArrived; + + if(!adminInstanceData.adminServer.Init(desc)) return; + adminInstanceData.parentInstance = parent; + + adminInstanceData.adminServer.Start(); + } + else + { + adminInstanceData.adminServer.Shutdown(); + } +} \ No newline at end of file diff --git a/Code/Game/DanBiasServer/AdminInterface/AdminInterface.h b/Code/Game/DanBiasServer/AdminInterface/AdminInterface.h new file mode 100644 index 00000000..afb00973 --- /dev/null +++ b/Code/Game/DanBiasServer/AdminInterface/AdminInterface.h @@ -0,0 +1,17 @@ +#ifndef DANBIASSERVER_ADMIN_INTERFACE_H +#define DANBIASSERVER_ADMIN_INTERFACE_H + +#include + +namespace DanBias +{ + class NetworkSession; + //Global admin + class AdminInterface + { + public: + static void Toggle(bool toggle, NetworkSession* parent); + + }; +} +#endif // !DANBIASSERVER_ADMIN_INTERFACE_H diff --git a/Code/Game/DanBiasServer/DLLMain.cpp b/Code/Game/DanBiasServer/DLLMain.cpp index 41d71e2e..7adceac7 100644 --- a/Code/Game/DanBiasServer/DLLMain.cpp +++ b/Code/Game/DanBiasServer/DLLMain.cpp @@ -1,5 +1,8 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// #define NOMINMAX -#include +#include BOOL WINAPI DllMain( _In_ HINSTANCE hinstDLL, _In_ DWORD fdwReason, _In_ LPVOID lpvReserved ) { diff --git a/Code/Game/DanBiasServer/DanBiasServer.vcxproj b/Code/Game/DanBiasServer/DanBiasServer.vcxproj index 3af47da2..529ebdc0 100644 --- a/Code/Game/DanBiasServer/DanBiasServer.vcxproj +++ b/Code/Game/DanBiasServer/DanBiasServer.vcxproj @@ -173,27 +173,34 @@ - - + + + - - - - - + + + + + + + + - - - - - - - - + + + + + + + + + + + diff --git a/Code/Game/DanBiasServer/Include/DanBiasServerAPI.cpp b/Code/Game/DanBiasServer/DanBiasServerAPI.cpp similarity index 91% rename from Code/Game/DanBiasServer/Include/DanBiasServerAPI.cpp rename to Code/Game/DanBiasServer/DanBiasServerAPI.cpp index e897a6dd..dfaf0b75 100644 --- a/Code/Game/DanBiasServer/Include/DanBiasServerAPI.cpp +++ b/Code/Game/DanBiasServer/DanBiasServerAPI.cpp @@ -2,7 +2,7 @@ // Created by [Dennis Andersen] [2013] ///////////////////////////////////////////////////////////////////// #include "DanBiasServerAPI.h" -#include "..\GameServer.h" +#include "GameServer.h" namespace DanBias { diff --git a/Code/Game/DanBiasServer/Include/DanBiasServerAPI.h b/Code/Game/DanBiasServer/DanBiasServerAPI.h similarity index 100% rename from Code/Game/DanBiasServer/Include/DanBiasServerAPI.h rename to Code/Game/DanBiasServer/DanBiasServerAPI.h diff --git a/Code/Game/DanBiasServer/GameServer.cpp b/Code/Game/DanBiasServer/GameServer.cpp index e1f03b98..837d52b0 100644 --- a/Code/Game/DanBiasServer/GameServer.cpp +++ b/Code/Game/DanBiasServer/GameServer.cpp @@ -7,11 +7,14 @@ #include #include "GameServer.h" -#include "Utilities.h" -#include "Helpers\ServerInitReader.h" +#include "Helpers\ServerDataReader.h" +#include "GameSession\GameSessionManager.h" +#include "LobbySessions\LobbyClient.h" +#include "GameSession\GameSession.h" +#include "AdminInterface\AdminInterface.h" + #include -#include "ServerObjects\ClientObject.h" -#include "ServerObjects\GameSession.h" +#include #include @@ -19,26 +22,45 @@ namespace DanBias { using namespace Oyster::Network; - static GameSession *myTest; - void GameServer::ClientConnectCallback(NetworkClient* client) + void GameServer::NetworkCallback(NetworkClient* client) { - printf("Client with ID [%i] connected.\n", client->Id()); + static GameSession *myTest = 0; + printf("Client with ID [%i] connected.\n", client->GetID()); if(!myTest) { - myTest = new GameSession(); - Utility::DynamicMemory::SmartPointer c = new ClientObject(client); - DanBias::GameSession::GameSessionDescription desc; - desc.owner = 0; - desc.clients.Push(c); + Utility::DynamicMemory::SmartPointer c = new LobbyClient(client); - myTest->Run(desc); + GameSessionDescription desc; + desc.mapName = L"test"; + desc.clients.Push(c); + desc.exitDestionation = this->mainLobby; + int sessionId = 0; + if((sessionId = GameSessionManager::AddSession(desc, true)) == 0) + printf("Failed to create a game session"); + + //myTest = new GameSession(); + // + //DanBias::GameSession::GameSessionDescription desc; + //desc.owner = 0; + //desc.clients.Push(c); + // + //if(!myTest->Create(desc)) return; + //myTest->Run(); } - // - //Utility::DynamicMemory::SmartPointer c = new ClientObject(client); - //this->mainLobby->AttachClient(c, this->mainLobby->GetPostbox()); + else + { + Utility::DynamicMemory::SmartPointer c = new LobbyClient(client); + myTest->Join(c); + } + + + //Utility::DynamicMemory::SmartPointer c = new LobbyClient(client); + //this->mainLobby->Attach(c, this->mainLobby->GetPostbox()); } + + GameServer::GameServer() : initiated(0) , running(0) @@ -67,7 +89,7 @@ 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; + //if(!WindowShell::CreateWin(WindowShell::WINDOW_INIT_DESC())) return DanBiasServerReturn_Error; this->initiated = true; return DanBiasServerReturn_Sucess; @@ -79,12 +101,18 @@ namespace DanBias if(!this->initiated) return DanBiasServerReturn_Error; if(!this->server->Start()) return DanBiasServerReturn_Error; + //Oyster::Thread::OysterThread ioThread; + //ioThread.Create(this, true, while (true) { if(!WindowShell::Frame()) break; + this->mainLobby->Frame(); + + if(GetAsyncKeyState(0x51)) + break; } return DanBiasServerReturn_Sucess; diff --git a/Code/Game/DanBiasServer/GameServer.h b/Code/Game/DanBiasServer/GameServer.h index bb066b69..2461ee9a 100644 --- a/Code/Game/DanBiasServer/GameServer.h +++ b/Code/Game/DanBiasServer/GameServer.h @@ -4,8 +4,8 @@ #ifndef DANBIASSERVER_GAME_SERVER_H #define DANBIASSERVER_GAME_SERVER_H -#include "Include\DanBiasServerAPI.h" -#include "ServerObjects\Lobby\MainLobby.h" +#include "DanBiasServerAPI.h" +#include "LobbySessions\MainLobby.h" #include #include @@ -23,7 +23,7 @@ namespace DanBias private: //static void ClientConnectCallbackFunction(Oyster::Network::NetworkClient& connectedClient); - void ClientConnectCallback(Oyster::Network::NetworkClient* client) override; + void NetworkCallback(Oyster::Network::NetworkClient* client) override; bool initiated; bool running; @@ -39,6 +39,9 @@ namespace DanBias int clients; }; bool LoadIniFile(InitData&); + + private: + friend class AdminInterface; }; }// End namspace DanBias #endif // !DANBIASSERVER_DBSERVER_H diff --git a/Code/Game/DanBiasServer/GameSession/GameClient.cpp b/Code/Game/DanBiasServer/GameSession/GameClient.cpp new file mode 100644 index 00000000..5e086e91 --- /dev/null +++ b/Code/Game/DanBiasServer/GameSession/GameClient.cpp @@ -0,0 +1,47 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// + +#include "GameClient.h" +#include "..\LobbySessions\NetworkSession.h" +#include + +using namespace Utility::DynamicMemory; +using namespace DanBias; + +GameClient::GameClient(SmartPointer client, Oyster::Callback::OysterCallback value) +{ + this->callbackValue = value; + this->client = client; + this->player = new GameLogic::Player(); + Oyster::Callback::OysterCallback c; + c.callbackType = Oyster::Callback::CallbackType_Object; + c.value = this; + this->client->SetCallback(c); + +} +GameClient::~GameClient() +{ + this->client->Disconnect(); + this->player.Release(); +} + +void GameClient::SetCallback(Oyster::Callback::OysterCallback value) +{ + this->callbackValue = value; +} + +GameLogic::Player* GameClient::GetPlayer() +{ + return this->player.Get(); +} +LobbyClient* GameClient::GetClient() +{ + return this->client; +} + +void GameClient::ObjectCallback(NetworkSession::NetEvent e) +{ + e.gameClient = this; + this->callbackValue(e); +} diff --git a/Code/Game/DanBiasServer/GameSession/GameClient.h b/Code/Game/DanBiasServer/GameSession/GameClient.h new file mode 100644 index 00000000..a79c3707 --- /dev/null +++ b/Code/Game/DanBiasServer/GameSession/GameClient.h @@ -0,0 +1,36 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// +#ifndef DANBIASSERVER_CLIENT_OBJECT_H +#define DANBIASSERVER_CLIENT_OBJECT_H + +#include "..\LobbySessions\LobbyClient.h" +#include +#include + +namespace DanBias +{ + class GameClient: Oyster::Callback::CallbackObject + { + public: + GameClient(Utility::DynamicMemory::SmartPointer client, Oyster::Callback::OysterCallback value); + virtual~GameClient(); + + void SetCallback(Oyster::Callback::OysterCallback value); + + GameLogic::Player* GetPlayer(); + LobbyClient* GetClient(); + + private: + Utility::DynamicMemory::SmartPointer player; + Utility::DynamicMemory::SmartPointer client; + Oyster::Callback::OysterCallback callbackValue; + + void ObjectCallback(NetworkSession::NetEvent) override; + + private: + friend class AdminInterface; + }; + +}//End namespace DanBias +#endif // !DANBIASSERVER_CLIENT_OBJECT_H diff --git a/Code/Game/DanBiasServer/GameSession/GameSession.cpp b/Code/Game/DanBiasServer/GameSession/GameSession.cpp new file mode 100644 index 00000000..866626c4 --- /dev/null +++ b/Code/Game/DanBiasServer/GameSession/GameSession.cpp @@ -0,0 +1,319 @@ +///////////////////////////////////////////////////////////////////// +// 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; + } + } + } + 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: + { + if(p[1].value.netBool) //bool bForward; + c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_FORWARD); + if(p[2].value.netBool) //bool bBackward; + c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_BACKWARD); + if(p[5].value.netBool) //bool bStrafeRight; + c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_RIGHT); + if(p[6].value.netBool) //bool bStrafeLeft; + c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_LEFT); + + //Oyster::Math::Float4x4 p; + Protocol_ObjectPosition op;//(c.GetPlayer()->GetRigidBody(), c.GetPlayer()->GetID()); + //op.object_ID = c.GetPlayer()->GetID(); + Send(op.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", 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 new file mode 100644 index 00000000..a128cc74 --- /dev/null +++ b/Code/Game/DanBiasServer/GameSession/GameSession.h @@ -0,0 +1,77 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// +#ifndef DANBIASSERVER_GAME_SESSION_H +#define DANBIASSERVER_GAME_SESSION_H + +//warning C4150: deletion of pointer to incomplete type, no destructor called +#pragma warning(disable: 4150) + +#include "..\LobbySessions\NetworkSession.h" +#include +#include + +namespace DanBias +{ + class LobbyClient; + class GameClient; + class GameSession : public Oyster::Thread::IThreadObject, public INetworkSession + { + public: + struct GameDescription + { + std::wstring mapName; + NetworkSession* owner; + Utility::DynamicMemory::DynamicArray> clients; + }; + + public: + GameSession(); + virtual~GameSession(); + + /** Initiates and creates a game session. */ + bool Create(GameDescription& desc); + + /** Runs the game session (ie starts the game loop). */ + void Run(); + + /** Join an existing/running game session + * @param client The client to attach to the session + */ + bool Join(Utility::DynamicMemory::SmartPointer client); + + /** + * + */ + void CloseSession(bool dissconnectClients = false); + + + inline bool IsCreated() const { return this->isCreated; } + inline bool IsRunning() const { return this->isRunning; } + + + private: + void Frame(); + void ParseEvents(); + void ParseGameplayEvent(Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c); + void ParseGeneralEvent(Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c); + + void Send(Oyster::Network::CustomNetProtocol* p); + + private: //overriden Threading functions + bool DoWork ( ) override; + + private: + Utility::DynamicMemory::DynamicArray> clients; + NetworkSession* owner; + Oyster::IPostBox *box; + Oyster::Thread::OysterThread worker; + bool isCreated; + bool isRunning; + + private: + friend class AdminInterface; + + };//End GameSession +}//End namespace DanBias +#endif // !DANBIASSERVER_GAME_SESSION_H \ No newline at end of file diff --git a/Code/Game/DanBiasServer/GameSession/GameSessionManager.cpp b/Code/Game/DanBiasServer/GameSession/GameSessionManager.cpp new file mode 100644 index 00000000..ee7c191f --- /dev/null +++ b/Code/Game/DanBiasServer/GameSession/GameSessionManager.cpp @@ -0,0 +1,116 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// +#include "GameSessionManager.h" + +#include "..\LobbySessions\LobbyClient.h" +#include "GameSession.h" +#include + +using namespace DanBias; +using namespace Utility::DynamicMemory; + +struct GameSessionData +{ + DynamicArray< SmartPointer< GameSession > > sessions; + + int freeSpot; + + int Existst(int session) + { + for (unsigned int i = 0; i < sessions.Size(); i++) + { + if(!sessions[i] && freeSpot == -1) freeSpot = i; + if(sessions[i]->GetID() == session) return i; + } + return -1; + } + int GetFree() + { + for (unsigned int i = 0; i < sessions.Size(); i++) + { + if(!sessions[i]) + { + this->freeSpot = i; + return this->freeSpot; + } + } + + this->freeSpot = -1; + return this->freeSpot; + } + +} gameSessionData; + + +int GameSessionManager::AddSession(GameSessionDescription& instance, bool run) +{ + int k = gameSessionData.GetFree(); + + SmartPointer gs = new GameSession(); + + DanBias::GameSession::GameDescription desc; + desc.owner = instance.exitDestionation; + desc.clients = instance.clients; + desc.mapName = instance.mapName; + + + if(!gs->Create(desc)) return 0; + + if(k == -1) gameSessionData.sessions.Push(gs); + else gameSessionData.sessions[k] = gs; + + if(run) gs->Run(); + + return gs->GetID(); +} + +bool GameSessionManager::StartSession(int session) +{ + int i = -1; + if((i = gameSessionData.Existst(session)) != -1) return false; + + gameSessionData.sessions[i]->Run(); + + return true; +} + +bool GameSessionManager::JoinSession(int session, Utility::DynamicMemory::SmartPointer client) +{ + int i = -1; + if((i = gameSessionData.Existst(session)) != -1) return false; + + gameSessionData.sessions[i]->Join(client); + + return true; +} + +void GameSessionManager::GetSessionInfo(int session, GameSessionInfo& data) +{ + memset(&data, 0, sizeof(GameSessionInfo)); + + int i = -1; + if((i = gameSessionData.Existst(session)) != -1) return; + + //data.gametime = gameSessionData.sessions[i]-> + //data.mapName = gameSessionData.sessions[i]-> + //data.numberOfPlayers = gameSessionData.sessions[i]-> +} + +void GameSessionManager::CloseSession(int session) +{ + int i = -1; + if((i = gameSessionData.Existst(session)) != -1) return; + + + gameSessionData.sessions[i]->CloseSession(); +} + +int GameSessionManager::GetSessionSize() +{ + return gameSessionData.sessions.Size(); +} + + + + diff --git a/Code/Game/DanBiasServer/GameSession/GameSessionManager.h b/Code/Game/DanBiasServer/GameSession/GameSessionManager.h new file mode 100644 index 00000000..4f93232b --- /dev/null +++ b/Code/Game/DanBiasServer/GameSession/GameSessionManager.h @@ -0,0 +1,77 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// +#ifndef DANBIASSERVER_GAME_SEESION_MANAGER_H +#define DANBIASSERVER_GAME_SEESION_MANAGER_H + +#include +#include +#include + +namespace DanBias +{ + class LobbyClient; + class NetworkSession; + + struct GameSessionDescription + { + std::wstring mapName; + Utility::DynamicMemory::DynamicArray> clients; + NetworkSession* exitDestionation; //The new owner when session dies + }; + struct GameSessionInfo + { + std::wstring mapName; + unsigned int numberOfPlayers; + float gametime; + }; + + class GameSessionManager + { + public: + /** + * Add a new game session. + * On success, the function returns the game instance id number greater than 0. + */ + static int AddSession(GameSessionDescription& instance, bool run); + + /** + * Starts an existing game session + * @param session The session id recieved when created. + * @param run Indicates if the game session should start imidiatly when created. + * @return Returns false if session is not found. + */ + static bool StartSession(int session); + + /** + * Join an exiting session + * @param session The session id recieved when created. + * @param client The client that is to join a game session + * @return Returns false on failure. + */ + static bool JoinSession(int session, Utility::DynamicMemory::SmartPointer client); + + /** + * Gets information about a given session + * @param session The session id recieved when created. + * @param sessionInformation The output parameter that will be filled. + */ + static void GetSessionInfo(int session, GameSessionInfo& sessionInformation); + + /** + * Close a session. + * @param session The session id recieved when created a session. + */ + static void CloseSession(int session); + + /** + * Get total sessions running + * @return Returns the total sessions curently running. + */ + static int GetSessionSize(); + + private: + friend class AdminInterface; + }; +} +#endif // !DANBIASSERVER_GAME_SEESION_MANAGER_H diff --git a/Code/Game/DanBiasServer/Helpers/GameSessionManager.cpp b/Code/Game/DanBiasServer/Helpers/GameSessionManager.cpp deleted file mode 100644 index 76d67729..00000000 --- a/Code/Game/DanBiasServer/Helpers/GameSessionManager.cpp +++ /dev/null @@ -1,58 +0,0 @@ -#include "GameSessionManager.h" - -#include "..\ServerObjects\GameSession.h" -#include "DynamicArray.h" - -struct GameSessionData -{ - Utility::DynamicMemory::DynamicArray sessions; - int freeSpot; - - int Existst(DanBias::GameSession* s) - { - for (unsigned int i = 0; i < sessions.Size(); i++) - { - if(!sessions[i] && freeSpot == -1) freeSpot = i; - if(sessions[i] == s) return i; - } - return -1; - } - int GetFree() - { - for (unsigned int i = 0; i < sessions.Size(); i++) - if(!sessions[i]) - return i; - return -1; - } - -} gameSessionData; - - -void GameSessionManager::AddSession(DanBias::GameSession* session) -{ - if(gameSessionData.Existst(session) == -1) - { - int k = gameSessionData.freeSpot; - - if( k == -1) k = gameSessionData.GetFree(); - - if(k == -1) gameSessionData.sessions.Push(session); - else gameSessionData.sessions[k] = session; - } - -} -void GameSessionManager::CloseSession() -{ - - //int i = gameSessionData.Existst(session); - // - ////Moron check... - //if(i == -1) return; - // - ////gameSessionData.sessions[i]->Close(); -} - - - - - diff --git a/Code/Game/DanBiasServer/Helpers/GameSessionManager.h b/Code/Game/DanBiasServer/Helpers/GameSessionManager.h deleted file mode 100644 index d17ef1b1..00000000 --- a/Code/Game/DanBiasServer/Helpers/GameSessionManager.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef DANBIASSERVER_GAME_SEESION_MANAGER_H -#define DANBIASSERVER_GAME_SEESION_MANAGER_H - -namespace DanBias -{ - class GameSession; -} - -class GameSessionManager -{ -public: - static void AddSession(DanBias::GameSession* session); - static void CloseSession(); -}; - -#endif // !DANBIASSERVER_GAME_SEESION_MANAGER_H diff --git a/Code/Game/DanBiasServer/Helpers/MapManager.h b/Code/Game/DanBiasServer/Helpers/MapManager.h new file mode 100644 index 00000000..34999014 --- /dev/null +++ b/Code/Game/DanBiasServer/Helpers/MapManager.h @@ -0,0 +1,27 @@ +#ifndef DANBIASSERVER_LEVELMANAGER_H +#define DANBIASSERVER_LEVELMANAGER_H + +#include "ServerDataReader.h" +#include "..\LobbySessions\LobbyClient.h" +#include "..\LobbySessions\NetworkSession.h" +#include "..\LobbySessions\GameLobby.h" +#include + +namespace DanBias +{ + class MapManager + { + public: + struct MapInitDesc + { + const wchar_t* map; + Utility::DynamicMemory::DynamicArray> clients; + }; + + public: + static bool InitiateMapPack(const MapInitDesc& desc); + + }; +} + +#endif // !DANBIASSERVER_LEVELMANAGER_H diff --git a/Code/Game/DanBiasServer/Helpers/ServerInitReader.h b/Code/Game/DanBiasServer/Helpers/ServerDataReader.h similarity index 79% rename from Code/Game/DanBiasServer/Helpers/ServerInitReader.h rename to Code/Game/DanBiasServer/Helpers/ServerDataReader.h index 68c28a9a..73048bd7 100644 --- a/Code/Game/DanBiasServer/Helpers/ServerInitReader.h +++ b/Code/Game/DanBiasServer/Helpers/ServerDataReader.h @@ -1,3 +1,6 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// #ifndef DANBIASSERVER_SERVER_INIT_READER_H #define DANBIASSERVER_SERVER_INIT_READER_H diff --git a/Code/Game/DanBiasServer/LobbySessions/GameLobby.cpp b/Code/Game/DanBiasServer/LobbySessions/GameLobby.cpp new file mode 100644 index 00000000..fc770db8 --- /dev/null +++ b/Code/Game/DanBiasServer/LobbySessions/GameLobby.cpp @@ -0,0 +1,21 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// +#include "GameLobby.h" + + +namespace DanBias +{ + GameLobby::GameLobby(Utility::DynamicMemory::SmartPointer owner) + { + + } + GameLobby::~GameLobby() + { + + } + void GameLobby::Release() + { + + } +}//End namespace DanBias \ No newline at end of file diff --git a/Code/Game/DanBiasServer/LobbySessions/GameLobby.h b/Code/Game/DanBiasServer/LobbySessions/GameLobby.h new file mode 100644 index 00000000..b6c6d4f9 --- /dev/null +++ b/Code/Game/DanBiasServer/LobbySessions/GameLobby.h @@ -0,0 +1,24 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// +#ifndef DANBIASSERVER_GAMELOBBY_H +#define DANBIASSERVER_GAMELOBBY_H + +#include "NetworkSession.h" + +namespace DanBias +{ + class GameLobby :public NetworkSession + { + public: + GameLobby(Utility::DynamicMemory::SmartPointer owner); + virtual~GameLobby(); + + void Release(); + + private: + friend class AdminInterface; + }; +}//End namespace DanBias + +#endif // !DANBIASSERVER_GAME_LOBBY_H \ No newline at end of file diff --git a/Code/Game/DanBiasServer/LobbySessions/INetworkSession.cpp b/Code/Game/DanBiasServer/LobbySessions/INetworkSession.cpp new file mode 100644 index 00000000..899e1d53 --- /dev/null +++ b/Code/Game/DanBiasServer/LobbySessions/INetworkSession.cpp @@ -0,0 +1,18 @@ +#include "INetworkSession.h" +#include + +INetworkSession::INetworkSession() + :id(GID()) +{} +INetworkSession::INetworkSession(const INetworkSession& orig) +{ + id = orig.id; +} +const INetworkSession& INetworkSession::operator=(const INetworkSession& orig) +{ + id = orig.id; + return *this; +} +INetworkSession::~INetworkSession() +{} + diff --git a/Code/Game/DanBiasServer/LobbySessions/INetworkSession.h b/Code/Game/DanBiasServer/LobbySessions/INetworkSession.h new file mode 100644 index 00000000..4495739f --- /dev/null +++ b/Code/Game/DanBiasServer/LobbySessions/INetworkSession.h @@ -0,0 +1,19 @@ +#ifndef DANBIASSERVER_INETWORKSESSION_H +#define DANBIASSERVER_INETWORKSESSION_H + +class INetworkSession +{ +public: + INetworkSession(); + INetworkSession(const INetworkSession& orig); + const INetworkSession& operator=(const INetworkSession& orig); + virtual~INetworkSession(); + + inline int GetID() const { return this->id; } + +private: + int id; + +}; + +#endif // !DANBIASSERVER_INETWORKSESSION_H diff --git a/Code/Game/DanBiasServer/LobbySessions/LobbyClient.cpp b/Code/Game/DanBiasServer/LobbySessions/LobbyClient.cpp new file mode 100644 index 00000000..af29410d --- /dev/null +++ b/Code/Game/DanBiasServer/LobbySessions/LobbyClient.cpp @@ -0,0 +1,51 @@ +#include "LobbyClient.h" +#include + +using namespace Utility::DynamicMemory; +using namespace Oyster::Network; +using namespace Oyster::Callback; +using namespace GameLogic; + +namespace DanBias +{ + LobbyClient::LobbyClient(SmartPointer client) + { + this->client = client; + this->client->SetRecieverObject(this, NetworkProtocolCallbackType_Object); + } + LobbyClient::~LobbyClient() + { + this->callbackValue.callbackType = CallbackType_Unknown; + } + + void LobbyClient::Disconnect() + { + this->client->Disconnect(); + } + void LobbyClient::SetCallback(OysterCallback value) + { + this->callbackValue = value; + } + + /** This method is NOT threadsafe. */ + void LobbyClient::NetworkCallback(CustomNetProtocol& protocol) + { + if(this->callbackValue.callbackType == CallbackType_Unknown) return; + + NetworkSession::NetEvent e; + e.sender = this; + e.protocol = protocol; + + this->callbackValue(e); + } + void LobbyClient::Disconnected() + { + if(this->callbackValue.callbackType == CallbackType_Unknown) return; + + NetworkSession::NetEvent e; + e.sender = this; + e.protocol = *GameLogic::Protocol_General_Status(Protocol_General_Status::States_disconected).GetProtocol(); + + this->callbackValue(e); + } +}//End namsapce DanBias \ No newline at end of file diff --git a/Code/Game/DanBiasServer/LobbySessions/LobbyClient.h b/Code/Game/DanBiasServer/LobbySessions/LobbyClient.h new file mode 100644 index 00000000..b827ca7e --- /dev/null +++ b/Code/Game/DanBiasServer/LobbySessions/LobbyClient.h @@ -0,0 +1,35 @@ +#ifndef DANBIASSERVER_LOBBYCLIENT_H +#define DANBIASSERVER_LOBBYCLIENT_H + +#include "NetworkSession.h" +#include +#include + +namespace DanBias +{ + class LobbyClient :public Oyster::Network::NetClientEvent + { + public: + LobbyClient(Utility::DynamicMemory::SmartPointer client); + virtual~LobbyClient(); + + void Disconnect(); + void SetCallback(Oyster::Callback::OysterCallback value); + + inline void Send(Oyster::Network::CustomProtocolObject& protocol) { this->client->Send(protocol); } + inline void Send(Oyster::Network::CustomNetProtocol* protocol) { this->client->Send(protocol); } + inline int GetID() const { return this->client->GetID(); } + + private: + Utility::DynamicMemory::SmartPointer client; + Oyster::Callback::OysterCallback callbackValue; + + private: + void NetworkCallback(Oyster::Network::CustomNetProtocol& protocol) override; + void Disconnected() override; + + private: + friend class AdminInterface; + }; +}//End namspace DanBias +#endif // !DANBIASSERVER_LOBBYCLIENT_H diff --git a/Code/Game/DanBiasServer/LobbySessions/MainLobby.cpp b/Code/Game/DanBiasServer/LobbySessions/MainLobby.cpp new file mode 100644 index 00000000..fd31f445 --- /dev/null +++ b/Code/Game/DanBiasServer/LobbySessions/MainLobby.cpp @@ -0,0 +1,109 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// +#include "MainLobby.h" +#include "LobbyClient.h" +#include +#include + +using namespace Utility::DynamicMemory; +using namespace Oyster::Network; +using namespace Oyster; + +namespace DanBias +{ + MainLobby::MainLobby() + :gameLobby(5) + { + this->box = new PostBox(); + } + MainLobby::~MainLobby() + { + delete this->box; + this->box = 0; + } + void MainLobby::Release() + { + this->CloseSession(true); + } + + void MainLobby::Frame() + { + ParseEvents(); + } + IPostBox* MainLobby::GetPostbox() + { + return this->box; + } + +//////// Private + void MainLobby::ParseEvents() + { + if(this->box && !this->box->IsEmpty()) + { + NetEvent &e = this->box->Fetch(); + + ParseProtocol(e.protocol, e.sender); + + } + } + void MainLobby::ParseProtocol(Oyster::Network::CustomNetProtocol& p, DanBias::LobbyClient* c) + { + bool update = false; + switch (p[0].value.netShort) + { + case protocol_Lobby_CreateGame: + { + GameLogic::Protocol_LobbyCreateGame val(p); + CreateGame(val, c); + update = true; + } + break; + case protocol_Lobby_JoinLobby: + { + GameLogic::Protocol_LobbyJoinLobby val(p); + JoinLobby(val, c); + } + break; + case protocol_Lobby_LeaveLobby: + { + Detach(c)->Disconnect(); + } + break; + } + + if(update) SendUpdate(); + } + + void MainLobby::CreateGame(GameLogic::Protocol_LobbyCreateGame& p, DanBias::LobbyClient* c) + { + for (unsigned int i = 0; i < this->gameLobby.Size(); i++) + { + if(!gameLobby[i]) + { + gameLobby[i] = new GameLobby(NetworkSession::Detach(c)); + return; + } + } + + this->gameLobby.Push(new GameLobby(NetworkSession::Detach(c))); + } + void MainLobby::JoinLobby(GameLogic::Protocol_LobbyJoinLobby& p, DanBias::LobbyClient* c) + { + for (unsigned int i = 0; i < this->gameLobby.Size(); i++) + { + if (this->gameLobby[i]->GetID() == p.LobbyID) + { + this->gameLobby[i]->Attach(Detach(c)); + return; + } + } + } + + void MainLobby::SendUpdate() + { + //Send Lobbys + GameLogic::Protocol_LobbyUpdate(); + } + +}//End namespace DanBias \ No newline at end of file diff --git a/Code/Game/DanBiasServer/ServerObjects/Lobby/MainLobby.h b/Code/Game/DanBiasServer/LobbySessions/MainLobby.h similarity index 65% rename from Code/Game/DanBiasServer/ServerObjects/Lobby/MainLobby.h rename to Code/Game/DanBiasServer/LobbySessions/MainLobby.h index 2c188d5c..88ae4067 100644 --- a/Code/Game/DanBiasServer/ServerObjects/Lobby/MainLobby.h +++ b/Code/Game/DanBiasServer/LobbySessions/MainLobby.h @@ -1,7 +1,10 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// #ifndef DANBIASSERVER_MAINLOBBY_H #define DANBIASSERVER_MAINLOBBY_H -#include "..\NetworkSession.h" +#include "NetworkSession.h" #include "GameLobby.h" #include #include @@ -21,16 +24,19 @@ namespace DanBias private: void ParseEvents(); - void ParseProtocol(Oyster::Network::CustomNetProtocol& p, DanBias::ClientObject& c); + void ParseProtocol(Oyster::Network::CustomNetProtocol& p, DanBias::LobbyClient* c); - void CreateGame(GameLogic::Protocol_LobbyCreateGame& p, DanBias::ClientObject& c); - //void CreateJoin(GameLogic::Protocol_LobbyJoinGame& p, DanBias::ClientObject& c); + void CreateGame(GameLogic::Protocol_LobbyCreateGame& p, DanBias::LobbyClient* c); + void JoinLobby(GameLogic::Protocol_LobbyJoinLobby& p, DanBias::LobbyClient* c); - //void ProtocolRecievedCallback(CustomNetProtocol& protocol) override; + void SendUpdate(); private: Oyster::IPostBox *box; Utility::DynamicMemory::DynamicArray> gameLobby; + + private: + friend class AdminInterface; }; }//End namespace DanBias #endif // !DANBIASGAME_GAMELOBBY_H diff --git a/Code/Game/DanBiasServer/LobbySessions/NetworkSession.cpp b/Code/Game/DanBiasServer/LobbySessions/NetworkSession.cpp new file mode 100644 index 00000000..0eecf59a --- /dev/null +++ b/Code/Game/DanBiasServer/LobbySessions/NetworkSession.cpp @@ -0,0 +1,219 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// +#include "LobbyClient.h" +#include "NetworkSession.h" +#include + + + +namespace DanBias +{ + NetworkSession::NetworkSession() + : owner(0) + , clientCount(0) + {} + NetworkSession::NetworkSession(const NetworkSession& orig) + { + this->clients = orig.clients; + this->owner = orig.owner; + this->clientCount = orig.clientCount; + } + const NetworkSession& NetworkSession::operator=(const NetworkSession& orig) + { + this->clients = orig.clients; + this->owner = orig.owner; + this->clientCount = orig.clientCount; + return *this; + } + NetworkSession::~NetworkSession() + { + this->clients.Clear(); + this->clientCount = 0; + } + + bool NetworkSession::Attach(Utility::DynamicMemory::SmartPointer client) + { + clientListLock.lock(); + + int k = -1; + for (unsigned int i = 0; (k == -1) && i < this->clients.Size(); i++) + { + if(!this->clients[i]) + k = i; + } + + if(k == -1) + { + this->clients.Push(client); + } + else + { + this->clients[k] = client; + } + this->clientCount++; + clientListLock.unlock(); + + return true; + } + + Utility::DynamicMemory::SmartPointer NetworkSession::Detach(Oyster::Network::NetworkClient* client) + { + Utility::DynamicMemory::SmartPointer val; + + clientListLock.lock(); + + for (unsigned int i = 0; i < this->clients.Size(); i++) + { + if(this->clients[0]->GetID() == client->GetID()) + { + val = this->clients[i]; + this->clients[i] = 0; + this->clientCount--; + } + } + + clientListLock.unlock(); + + return val; + } + Utility::DynamicMemory::SmartPointer NetworkSession::Detach(const LobbyClient* client) + { + Utility::DynamicMemory::SmartPointer val; + + clientListLock.lock(); + + for (unsigned int i = 0; i < this->clients.Size(); i++) + { + if(this->clients[0]->GetID() == client->GetID()) + { + val = this->clients[i]; + this->clients[i] = 0; + this->clientCount--; + } + } + + clientListLock.unlock(); + + return val; + } + Utility::DynamicMemory::SmartPointer NetworkSession::Detach(const LobbyClient& client) + { + Utility::DynamicMemory::SmartPointer val; + + clientListLock.lock(); + + for (unsigned int i = 0; i < this->clients.Size(); i++) + { + if(this->clients[0]->GetID() == client.GetID()) + { + val = this->clients[i]; + this->clients[i] = 0; + this->clientCount--; + } + } + + clientListLock.unlock(); + + return val; + } + Utility::DynamicMemory::SmartPointer NetworkSession::Detach(short ID) + { + Utility::DynamicMemory::SmartPointer val; + + clientListLock.lock(); + + for (unsigned int i = 0; i < this->clients.Size(); i++) + { + if(this->clients[0]->GetID() == ID) + { + val = this->clients[i]; + this->clients[i] = 0; + this->clientCount--; + } + } + + clientListLock.unlock(); + + return val; + } + + bool NetworkSession::Send(Oyster::Network::CustomNetProtocol& protocol) + { + bool returnValue = false; + for (unsigned int i = 0; i < this->clients.Size(); i++) + { + if(this->clients[i]) + { + this->clients[i]->Send(&protocol); + returnValue = true; + } + } + + return returnValue; + } + bool NetworkSession::Send(Oyster::Network::CustomNetProtocol& protocol, int ID) + { + for (unsigned int i = 0; i < this->clients.Size(); i++) + { + if(this->clients[i] && this->clients[i]->GetID() == ID) + { + this->clients[i]->Send(&protocol); + return true; + } + } + return false; + } + + void NetworkSession::SetCallback(Oyster::Callback::OysterCallback value) + { + for (unsigned int i = 0; i < this->clients.Size(); i++) + { + this->clients[i]->SetCallback(value); + } + } + + void NetworkSession::CloseSession(bool dissconnectClients) + { + clientListLock.lock(); + + for (unsigned int i = 0; i < this->clients.Size(); i++) + { + if(dissconnectClients) this->clients[i]->Disconnect(); + else if(this->owner) this->owner->Attach(this->clients[i]); + } + + this->clients.Clear(); + + clientListLock.unlock(); + } + Utility::DynamicMemory::SmartPointer NetworkSession::FindClient(int ID) + { + for (unsigned int i = 0; i < this->clients.Size(); i++) + { + if(this->clients[i]->GetID() == ID) + return this->clients[i]; + } + return Utility::DynamicMemory::SmartPointer(); + } + Utility::DynamicMemory::SmartPointer NetworkSession::FindClient(LobbyClient& obj) + { + for (unsigned int i = 0; i < this->clients.Size(); i++) + { + if(this->clients[i]->GetID() == obj.GetID()) + return this->clients[i]; + } + return Utility::DynamicMemory::SmartPointer(); + } + Utility::DynamicMemory::SmartPointer NetworkSession::FindClient(LobbyClient* obj) + { + for (unsigned int i = 0; i < this->clients.Size(); i++) + { + if(this->clients[i]->GetID() == obj->GetID()) + return this->clients[i]; + } + return Utility::DynamicMemory::SmartPointer(); + } + + +}//End namespace DanBias \ No newline at end of file diff --git a/Code/Game/DanBiasServer/LobbySessions/NetworkSession.h b/Code/Game/DanBiasServer/LobbySessions/NetworkSession.h new file mode 100644 index 00000000..41fb0a59 --- /dev/null +++ b/Code/Game/DanBiasServer/LobbySessions/NetworkSession.h @@ -0,0 +1,91 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// +#ifndef DANBIASSERVER_NETWORK_SESSION_H +#define DANBIASSERVER_NETWORK_SESSION_H + +//warning C4150: deletion of pointer to incomplete type, no destructor called +#pragma warning(disable : 4150) + +#define NOMINMAX + +#include "INetworkSession.h" +#include +#include +#include +#include +#include +#include +#include +#include + +namespace DanBias +{ + class LobbyClient; + class GameClient; + + class NetworkSession :public INetworkSession + { + public: + struct NetEvent + { + LobbyClient* sender; + GameClient* gameClient; + Oyster::Network::CustomNetProtocol protocol; + NetEvent():sender(0), gameClient(0){} + }; + + public: + NetworkSession(); + NetworkSession(const NetworkSession& orig); + const NetworkSession& operator=(const NetworkSession& orig); + virtual~NetworkSession(); + + virtual bool Attach(Utility::DynamicMemory::SmartPointer client); + + virtual Utility::DynamicMemory::SmartPointer Detach(Oyster::Network::NetworkClient* client); + virtual Utility::DynamicMemory::SmartPointer Detach(const LobbyClient* client); + virtual Utility::DynamicMemory::SmartPointer Detach(const LobbyClient& client); + virtual Utility::DynamicMemory::SmartPointer Detach(short ID); + + Utility::DynamicMemory::SmartPointer FindClient(LobbyClient& obj); + Utility::DynamicMemory::SmartPointer FindClient(LobbyClient* obj); + Utility::DynamicMemory::SmartPointer FindClient(int ID); + + /** + * Sends a message to all clients in this session. + */ + virtual bool Send(Oyster::Network::CustomNetProtocol& message); + /** + * Sends a message to a specific client in this session. + */ + virtual bool Send(Oyster::Network::CustomNetProtocol& protocol, int ID); + + /** + * Set the callback to all clients to where a messages is recieved. + */ + virtual void SetCallback(Oyster::Callback::OysterCallback value); + + /** + * Closes the session and sends the clients to given owner session if any. + * If session is null, clients is assumed to already be elsewhere and only releases a reference. + */ + virtual void CloseSession(bool dissconnectClients = false); + + + /** Set where the clients is returned on closed session. */ + inline void SetOwner(NetworkSession* owner) { this->owner = owner; } + + protected: + Utility::DynamicMemory::DynamicArray> clients; + NetworkSession* owner; //Where clients end up when session is closed. + + private: + std::mutex clientListLock; + int clientCount; + + private: + friend class AdminInterface; + }; +}//End namespace DanBias +#endif // !DANBIASSERVER_NETWORK_SESSION_H diff --git a/Code/Game/DanBiasServer/ServerObjects/ClientObject.cpp b/Code/Game/DanBiasServer/ServerObjects/ClientObject.cpp deleted file mode 100644 index 84b7a3db..00000000 --- a/Code/Game/DanBiasServer/ServerObjects/ClientObject.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include "ClientObject.h" - -using namespace DanBias; - -ClientObject::ClientObject(Oyster::Network::NetworkClient* client) -{ - this->client = client; - this->client->SetRecieverObject(this, Oyster::Network::NetworkProtocolCallbackType_Object); - this->box = 0; -} -ClientObject::~ClientObject() -{ - this->client->Disconnect(); -} -void ClientObject::SetProtocolCallback(Oyster::Network::ProtocolRecieverObject* object) -{ - this->GetClient()->SetRecieverObject(object, Oyster::Network::NetworkProtocolCallbackType_Object); -} -void ClientObject::SetPostbox(Oyster::IPostBox* box) -{ - this->box = box; -} -GameLogic::Player* ClientObject::GetPlayer() -{ - return this->player.Get(); -} -Oyster::Network::NetworkClient* ClientObject::GetClient() -{ - return this->client.Get(); -} - -void ClientObject::CreatePlayer() -{ - if(this->player) return; - - this->player = new GameLogic::Player(); -} -void ClientObject::ErasePlayer() -{ - while(this->player.Release()); -} - -void ClientObject::ProtocolRecievedCallback(Oyster::Network::CustomNetProtocol& protocol) -{ - if(!this->box) return; - - NetworkSession::NetEvent _event; - _event.protocol = protocol; - _event.reciever = this; - - this->box->Post(_event); -} - diff --git a/Code/Game/DanBiasServer/ServerObjects/ClientObject.h b/Code/Game/DanBiasServer/ServerObjects/ClientObject.h deleted file mode 100644 index 4bc65111..00000000 --- a/Code/Game/DanBiasServer/ServerObjects/ClientObject.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef DANBIASSERVER_CLIENT_OBJECT_H -#define DANBIASSERVER_CLIENT_OBJECT_H - - -#include "NetworkSession.h" -#include "NetworkClient.h" -#include -#include - -namespace DanBias -{ - class ClientObject - :public Oyster::Network::ProtocolRecieverObject - { - public: - ClientObject(Oyster::Network::NetworkClient* client); - virtual~ClientObject(); - - void SetPostbox(Oyster::IPostBox* box); - void SetProtocolCallback(Oyster::Network::ProtocolRecieverObject* object); - - GameLogic::Player* GetPlayer(); - Oyster::Network::NetworkClient* GetClient(); - - public: - void CreatePlayer(); - void ErasePlayer(); - - private: - - /** This method is NOT threadsafe. */ - virtual void ProtocolRecievedCallback(Oyster::Network::CustomNetProtocol& protocol) override; - - private: - Utility::DynamicMemory::SmartPointer player; - Utility::DynamicMemory::SmartPointer client; - Oyster::IPostBox* box; - }; - -}//End namespace DanBias -#endif // !DANBIASSERVER_CLIENT_OBJECT_H diff --git a/Code/Game/DanBiasServer/ServerObjects/GameSession.cpp b/Code/Game/DanBiasServer/ServerObjects/GameSession.cpp deleted file mode 100644 index c2f5fe7a..00000000 --- a/Code/Game/DanBiasServer/ServerObjects/GameSession.cpp +++ /dev/null @@ -1,263 +0,0 @@ - -#include -#include -#include "GameSession.h" -#include "ClientObject.h" -#include - - -using namespace Utility::DynamicMemory; -using namespace Oyster::Network; -using namespace Oyster; -using namespace Oyster::Thread; -using namespace GameLogic; - -namespace DanBias -{ - GameSession::GameSession() - { - this->box = 0; - } - GameSession::~GameSession() - { - delete this->box; - this->box = 0; - } - - void GameSession::Run(GameSessionDescription& desc) - { - if(this->Init(desc)) - { - if(this->worker.Create(this, true, true) != OYSTER_THREAD_ERROR_SUCCESS) return; - this->worker.SetPriority(OYSTER_THREAD_PRIORITY_1); - } - } - - void GameSession::Join(Utility::DynamicMemory::SmartPointer client) - { - AttachClient(client, this->box); - } - -////private: overriden NetworkSession functions - void GameSession::AttachClient(SmartPointer client, Oyster::IPostBox *box ) - { - NetworkSession::AttachClient(client, box); - } - void GameSession::Close() - { - - } - SmartPointer GameSession::DetachClient(NetworkClient* client) - { - return SmartPointer(); - } - SmartPointer GameSession::DetachClient(ClientObject* client) - { - return SmartPointer(); - } - SmartPointer GameSession::DetachClient(short ID) - { - return SmartPointer(); - } - void GameSession::Send(CustomNetProtocol& protocol) - { - NetworkSession::Send(protocol); - } - void GameSession::Send(CustomNetProtocol& protocol, int ID) - { - NetworkSession::Send(protocol, ID); - } - void GameSession::SetPostbox(IPostBox *box) - { - NetworkSession::SetPostbox(box); - } - void GameSession::CloseSession(NetworkSession* clientDestination) - { - - } - -////private: //overriden Threading functions - void GameSession::ThreadEntry() - { - - } - void GameSession::ThreadExit() - { - - } - bool GameSession::DoWork ( ) - { - this->ParseEvents(); - this->Frame(); - - return true; - } - -#ifndef ERIK -////private: - 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); - desc.clients[i]->CreatePlayer(); - this->clients.Push(desc.clients[i]); - } - - - 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) - { - if( ProtocolIsGameplay(p[protocol_INDEX_ID].value.netShort) ) - { - switch (p[protocol_INDEX_ID].value.netShort) - { - case protocol_Gameplay_PlayerNavigation: - { - - if(p[1].value.netBool) //bool bForward; - c.GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_FORWARD); - if(p[2].value.netBool) //bool bBackward; - c.GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_BACKWARD); - if(p[5].value.netBool) //bool bStrafeRight; - c.GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_RIGHT); - if(p[6].value.netBool) //bool bStrafeLeft; - c.GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_LEFT); - - //Oyster::Math::Float4x4 p; - //Protocol_ObjectPosition op(c.GetPlayer()->GetRigidBody()->GetOrientation(p)); - //op.object_ID = c.GetPlayer()->GetID(); - //this->Send(*op.GetProtocol()); - } - break; - case protocol_Gameplay_PlayerMouseMovement: - - break; - case protocol_Gameplay_PlayerPosition: - - break; - case protocol_Gameplay_CreateObject: - - break; - case protocol_Gameplay_ObjectPosition: - - break; - } - } - else if(ProtocolIsGeneral(p[protocol_INDEX_ID].value.netShort) ) - { - - } - } -#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/ServerObjects/GameSession.h b/Code/Game/DanBiasServer/ServerObjects/GameSession.h deleted file mode 100644 index dadf2096..00000000 --- a/Code/Game/DanBiasServer/ServerObjects/GameSession.h +++ /dev/null @@ -1,57 +0,0 @@ -#ifndef DANBIASSERVER_GAME_SESSION_H -#define DANBIASSERVER_GAME_SESSION_H - -#include "NetworkSession.h" -#include -#include - -namespace DanBias -{ - class ClientObject; - class GameSession :private NetworkSession, public Oyster::Thread::IThreadObject - { - public: - struct GameSessionDescription - { - NetworkSession* owner; - Utility::DynamicMemory::DynamicArray> clients; - }; - - public: - GameSession(); - virtual~GameSession(); - - void Run(GameSessionDescription& desc); - - void Join(Utility::DynamicMemory::SmartPointer client); - - private: //overriden NetworkSession functions - void Close(); - void AttachClient(Utility::DynamicMemory::SmartPointer client, Oyster::IPostBox *box = 0) override; - Utility::DynamicMemory::SmartPointer DetachClient(Oyster::Network::NetworkClient* client) override; - Utility::DynamicMemory::SmartPointer DetachClient(ClientObject* client) override; - Utility::DynamicMemory::SmartPointer DetachClient(short ID) override; - void Send(Oyster::Network::CustomNetProtocol& protocol) override; - void Send(Oyster::Network::CustomNetProtocol& protocol, int ID) override; - void SetPostbox(Oyster::IPostBox *box) override; - void CloseSession(NetworkSession* clientDestination) override; - - private: //overriden Threading functions - void ThreadEntry( ) override; - void ThreadExit( ) override; - bool DoWork ( ) override; - - private: - bool Init(GameSessionDescription& desc); - void Frame(); - void ParseEvents(); - void ParseProtocol(Oyster::Network::CustomNetProtocol& p, DanBias::ClientObject& c); - - private: - NetworkSession* owner; - Oyster::IPostBox *box; - Oyster::Thread::OysterThread worker; - - };//End GameSession -}//End namespace DanBias -#endif // !DANBIASSERVER_GAME_SESSION_H \ No newline at end of file diff --git a/Code/Game/DanBiasServer/ServerObjects/Lobby/GameLobby.cpp b/Code/Game/DanBiasServer/ServerObjects/Lobby/GameLobby.cpp deleted file mode 100644 index 72833d29..00000000 --- a/Code/Game/DanBiasServer/ServerObjects/Lobby/GameLobby.cpp +++ /dev/null @@ -1,25 +0,0 @@ -#include "GameLobby.h" - - -namespace DanBias -{ - GameLobby::GameLobby(Utility::DynamicMemory::SmartPointer owner) - { - - } - GameLobby::~GameLobby() - { - - } - void GameLobby::Release() - { - - } - void GameLobby::Join(Utility::DynamicMemory::SmartPointer client) - { - NetworkSession::AttachClient(client); - } - - void GameLobby::AttachClient(Utility::DynamicMemory::SmartPointer client, Oyster::IPostBox *box) - { } -}//End namespace DanBias \ No newline at end of file diff --git a/Code/Game/DanBiasServer/ServerObjects/Lobby/GameLobby.h b/Code/Game/DanBiasServer/ServerObjects/Lobby/GameLobby.h deleted file mode 100644 index 6b6ea383..00000000 --- a/Code/Game/DanBiasServer/ServerObjects/Lobby/GameLobby.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef DANBIASSERVER_GAMELOBBY_H -#define DANBIASSERVER_GAMELOBBY_H - -#include "..\NetworkSession.h" - -namespace DanBias -{ - class GameLobby :public NetworkSession - { - public: - GameLobby(Utility::DynamicMemory::SmartPointer owner); - virtual~GameLobby(); - - void Release(); - - void Join(Utility::DynamicMemory::SmartPointer client); - - private: - void AttachClient(Utility::DynamicMemory::SmartPointer client, Oyster::IPostBox *box = 0) override; - - }; -}//End namespace DanBias - -#endif // !DANBIASSERVER_GAME_LOBBY_H \ No newline at end of file diff --git a/Code/Game/DanBiasServer/ServerObjects/Lobby/MainLobby.cpp b/Code/Game/DanBiasServer/ServerObjects/Lobby/MainLobby.cpp deleted file mode 100644 index 0e1fed89..00000000 --- a/Code/Game/DanBiasServer/ServerObjects/Lobby/MainLobby.cpp +++ /dev/null @@ -1,96 +0,0 @@ - -#include "MainLobby.h" -#include "..\ClientObject.h" -#include -#include - -using namespace Utility::DynamicMemory; -using namespace Oyster::Network; -using namespace Oyster; - -namespace DanBias -{ - MainLobby::MainLobby() - :gameLobby(5) - { - this->box = new PostBox(); - } - MainLobby::~MainLobby() - { - delete this->box; - this->box = 0; - } - void MainLobby::Release() - { - this->CloseSession(0); - } - - void MainLobby::Frame() - { - ParseEvents(); - } - IPostBox* MainLobby::GetPostbox() - { - return this->box; - } - -//////// Private - void MainLobby::ParseEvents() - { - if(this->box && !this->box->IsEmpty()) - { - NetEvent &e = this->box->Fetch(); - - ParseProtocol(e.protocol, *e.reciever); - - } - } - void MainLobby::ParseProtocol(Oyster::Network::CustomNetProtocol& p, DanBias::ClientObject& c) - { - //switch (p[0].value.netChar) - //{ - // case protocol_Lobby_CreateGame: - // { - // GameLogic::Protocol_LobbyCreateGame val(p); - // CreateGame(val, c); - // } - // break; - // case protocol_Lobby_JoinGame: - // { - // - // } - // break; - // case protocol_Lobby_JoinLobby: - // { - // - // } - // break; - // case protocol_Lobby_LeaveLobby: - // { - // - // } - // break; - //} - - } - - void MainLobby::CreateGame(GameLogic::Protocol_LobbyCreateGame& p, DanBias::ClientObject& c) - { - SmartPointer sc = NetworkSession::FindClient(c); - NetworkSession::DetachClient(&c); - - if(!sc) return; - - for (unsigned int i = 0; i < this->gameLobby.Size(); i++) - { - if(!gameLobby[i]) - { - gameLobby[i] = new GameLobby(sc); - return; - } - } - - this->gameLobby.Push(new GameLobby(sc)); - } - -}//End namespace DanBias \ No newline at end of file diff --git a/Code/Game/DanBiasServer/ServerObjects/NetworkSession.cpp b/Code/Game/DanBiasServer/ServerObjects/NetworkSession.cpp deleted file mode 100644 index 9cceb951..00000000 --- a/Code/Game/DanBiasServer/ServerObjects/NetworkSession.cpp +++ /dev/null @@ -1,180 +0,0 @@ - -#include "ClientObject.h" -#include "NetworkSession.h" -#include - -static std::mutex ClientListLock; - -namespace DanBias -{ - NetworkSession::NetworkSession() - { } - NetworkSession::NetworkSession(const NetworkSession& orig) - { - this->clients = orig.clients; - } - const NetworkSession& NetworkSession::operator=(const NetworkSession& orig) - { - this->clients = orig.clients; - return *this; - } - NetworkSession::~NetworkSession() - { - this->clients.Clear(); - } - - void NetworkSession::AttachClient(Utility::DynamicMemory::SmartPointer client, Oyster::IPostBox *box) - { - ClientListLock.lock(); - - int k = -1; - for (unsigned int i = 0; (k == -1) && i < this->clients.Size(); i++) - { - if(!this->clients[i]) - k = i; - } - - if(k == -1) - { - this->clients.Push(client); - this->clients[this->clients.Size() - 1]->SetPostbox(box); - } - else - { - this->clients[k] = client; - this->clients[k]->SetPostbox(box); - } - - ClientListLock.unlock(); - - } - - Utility::DynamicMemory::SmartPointer NetworkSession::DetachClient(Oyster::Network::NetworkClient* client) - { - Utility::DynamicMemory::SmartPointer val; - - ClientListLock.lock(); - - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - if(this->clients[0]->GetClient()->Id() == client->Id()) - { - val = this->clients[i]; - this->clients[i] = 0; - } - } - - ClientListLock.unlock(); - - return val; - } - Utility::DynamicMemory::SmartPointer NetworkSession::DetachClient(ClientObject* client) - { - Utility::DynamicMemory::SmartPointer val; - - ClientListLock.lock(); - - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - if(this->clients[0]->GetClient()->Id() == client->GetClient()->Id()) - { - val = this->clients[i]; - this->clients[i] = 0; - } - } - - ClientListLock.unlock(); - - return val; - } - Utility::DynamicMemory::SmartPointer NetworkSession::DetachClient(short ID) - { - Utility::DynamicMemory::SmartPointer val; - - ClientListLock.lock(); - - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - if(this->clients[0]->GetClient()->Id() == ID) - { - val = this->clients[i]; - this->clients[i] = 0; - } - } - - ClientListLock.unlock(); - - return val; - } - - void NetworkSession::Send(Oyster::Network::CustomNetProtocol& protocol) - { - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - this->clients[i]->GetClient()->Send(&protocol); - } - } - void NetworkSession::Send(Oyster::Network::CustomNetProtocol& protocol, int ID) - { - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - if(this->clients[i]->GetClient()->Id() == ID) - { - this->clients[i]->GetClient()->Send(&protocol); - break; - } - } - } - - void NetworkSession::SetPostbox(Oyster::IPostBox *box) - { - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - this->clients[i]->SetPostbox(box); - } - } - - void NetworkSession::CloseSession(NetworkSession * owner) - { - ClientListLock.lock(); - - if(!owner) - { - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - this->clients[i]->GetClient()->Disconnect(); - } - } - else - { - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - owner->AttachClient(this->clients[i]); - } - } - - this->clients.Clear(); - - ClientListLock.unlock(); - } - - Utility::DynamicMemory::SmartPointer NetworkSession::FindClient(int ID) - { - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - if(this->clients[i]->GetClient()->Id() == ID) - return this->clients[i]; - } - return Utility::DynamicMemory::SmartPointer(); - } - Utility::DynamicMemory::SmartPointer NetworkSession::FindClient(ClientObject& obj) - { - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - if(this->clients[i]->GetClient()->Id() == obj.GetClient()->Id()) - return this->clients[i]; - } - return Utility::DynamicMemory::SmartPointer(); - } - -}//End namespace DanBias \ No newline at end of file diff --git a/Code/Game/DanBiasServer/ServerObjects/NetworkSession.h b/Code/Game/DanBiasServer/ServerObjects/NetworkSession.h deleted file mode 100644 index 02c9e28f..00000000 --- a/Code/Game/DanBiasServer/ServerObjects/NetworkSession.h +++ /dev/null @@ -1,57 +0,0 @@ -///////////////////////////////////////////////////////////////////// -// Created by [Dennis Andersen] [2013] -///////////////////////////////////////////////////////////////////// -#ifndef DANBIASSERVER_NETWORK_SESSION_H -#define DANBIASSERVER_NETWORK_SESSION_H - -#pragma warning(disable: 4150) - -#define NOMINMAX -#include -#include -#include -#include -#include -#include - - -namespace DanBias -{ - class ClientObject; - class NetworkSession - { - public: - struct NetEvent - { - ClientObject* reciever; - Oyster::Network::CustomNetProtocol protocol; - }; - - public: - NetworkSession(); - NetworkSession(const NetworkSession& orig); - const NetworkSession& operator=(const NetworkSession& orig); - virtual~NetworkSession(); - - virtual void AttachClient(Utility::DynamicMemory::SmartPointer client, Oyster::IPostBox *box = 0); - - virtual Utility::DynamicMemory::SmartPointer DetachClient(Oyster::Network::NetworkClient* client); - virtual Utility::DynamicMemory::SmartPointer DetachClient(ClientObject* client); - virtual Utility::DynamicMemory::SmartPointer DetachClient(short ID); - - virtual void Send(Oyster::Network::CustomNetProtocol& protocol); - virtual void Send(Oyster::Network::CustomNetProtocol& protocol, int ID); - - //TODO: Do more lobby features - virtual void SetPostbox(Oyster::IPostBox *box); - - virtual void CloseSession(NetworkSession* clientDestination); // FindClient(int ID); - Utility::DynamicMemory::SmartPointer FindClient(ClientObject& obj); - - protected: - Utility::DynamicMemory::DynamicArray> clients; - }; -}//End namespace DanBias -#endif // !DANBIASSERVER_NETWORK_SESSION_H diff --git a/Code/Game/DanBiasServerLauncher/DanBiasServerLauncher.vcxproj b/Code/Game/DanBiasServerLauncher/DanBiasServerLauncher.vcxproj index 019af625..ec88a915 100644 --- a/Code/Game/DanBiasServerLauncher/DanBiasServerLauncher.vcxproj +++ b/Code/Game/DanBiasServerLauncher/DanBiasServerLauncher.vcxproj @@ -71,7 +71,7 @@ $(SolutionDir)..\Bin\Executable\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName)D - $(SolutionDir)Game\DanBiasServer\Include;C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + $(SolutionDir)Game\DanBiasServer;C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) $(OutDir)..\DLL\;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) @@ -79,7 +79,7 @@ $(SolutionDir)..\Bin\Executable\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName)D - $(SolutionDir)Game\DanBiasServer\Include;C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + $(SolutionDir)Game\DanBiasServer;C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) $(OutDir)..\DLL\;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) @@ -87,7 +87,7 @@ $(SolutionDir)..\Bin\Executable\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName) - $(SolutionDir)Game\DanBiasServer\Include;C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + $(SolutionDir)Game\DanBiasServer;C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) $(OutDir)..\DLL\;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) @@ -95,7 +95,7 @@ $(SolutionDir)..\Bin\Executable\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName) - $(SolutionDir)Game\DanBiasServer\Include;C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + $(SolutionDir)Game\DanBiasServer;C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) $(OutDir)..\DLL\;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) diff --git a/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp b/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp index 2e53be36..0d26c37d 100644 --- a/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp +++ b/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp @@ -1,7 +1,8 @@ -///////////////////////////////////////////////// -// Launcher to launch Danbias server or client // -///////////////////////////////////////////////// -#define NOMINMAX +////////////////////////////////////////////////// +// Launcher to launch Danbias server or client // +// Created by [Dennis Andersen] [2013] // +////////////////////////////////////////////////// +#define NOMINMAX //Blame it on windows #include #include diff --git a/Code/Game/GameLogic/Player.cpp b/Code/Game/GameLogic/Player.cpp index e01cf8bb..ecf60dd3 100644 --- a/Code/Game/GameLogic/Player.cpp +++ b/Code/Game/GameLogic/Player.cpp @@ -54,17 +54,19 @@ void Player::Move(const PLAYER_MOVEMENT &movement) switch(movement) { case PLAYER_MOVEMENT::PLAYER_MOVEMENT_FORWARD: - //API::Instance().ApplyForceAt(rigidBody,rigidBody->GetCenter(),myData->lookDir * 100); + MoveForward(); break; case PLAYER_MOVEMENT::PLAYER_MOVEMENT_BACKWARD: - //API::Instance().ApplyForceAt(rigidBody,rigidBody->GetCenter(),-myData->lookDir * 100); + MoveBackwards(); break; case PLAYER_MOVEMENT::PLAYER_MOVEMENT_LEFT: + MoveLeft(); break; case PLAYER_MOVEMENT::PLAYER_MOVEMENT_RIGHT: + MoveRight(); break; case PLAYER_MOVEMENT::PLAYER_MOVEMENT_JUMP: @@ -84,13 +86,13 @@ void Player::MoveBackwards() void Player::MoveRight() { //Do cross product with forward vector and negative gravity vector - Oyster::Math::Float3 r = (-rigidBody->GetGravityNormal()).Cross(myData->lookDir); + //Oyster::Math::Float3 r = (-rigidBody->GetGravityNormal()).Cross(myData->lookDir); //API::Instance().ApplyForceAt(rigidBody, rigidBody->GetCenter(), r * 100); } void Player::MoveLeft() { //Do cross product with forward vector and negative gravity vector - Oyster::Math::Float3 r = -(-rigidBody->GetGravityNormal()).Cross(myData->lookDir); + //Oyster::Math::Float3 r = -(-rigidBody->GetGravityNormal()).Cross(myData->lookDir); //API::Instance().ApplyForceAt(rigidBody, rigidBody->GetCenter(), r * 100); } diff --git a/Code/Game/GameProtocols/ControlProtocols.h b/Code/Game/GameProtocols/ControlProtocols.h index 4515666e..cdb30df9 100644 --- a/Code/Game/GameProtocols/ControlProtocols.h +++ b/Code/Game/GameProtocols/ControlProtocols.h @@ -19,11 +19,18 @@ namespace GameLogic Protocol_General_Status() { - this->protocol[0].value = protocol_General_Status; - this->protocol[0].type = Oyster::Network::NetAttributeType_Short; + this->protocol[protocol_INDEX_ID].value = protocol_General_Status; + this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Short; } + Protocol_General_Status(States state) + { + this->protocol[protocol_INDEX_ID].value = protocol_General_Status; + this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->status = state; + this->protocol[1].type = Oyster::Network::NetAttributeType_Short; + } Oyster::Network::CustomNetProtocol* GetProtocol() override { this->protocol[1].value = status; @@ -34,55 +41,6 @@ namespace GameLogic private: Oyster::Network::CustomNetProtocol protocol; }; - - struct Protocol_General_Ping :public Oyster::Network::CustomProtocolObject - { - - Protocol_General_Ping() - { - this->protocol[0].value = protocol_General_Ping; - this->protocol[0].type = Oyster::Network::NetAttributeType_Short; - } - Oyster::Network::CustomNetProtocol* GetProtocol() override - { - return &protocol; - } - - private: - Oyster::Network::CustomNetProtocol protocol; - }; - - struct Protocol_General_Disconnect :public Oyster::Network::CustomProtocolObject - { - Protocol_General_Disconnect() - { - this->protocol[0].value = protocol_General_Disconnect; - this->protocol[0].type = Oyster::Network::NetAttributeType_Short; - } - Oyster::Network::CustomNetProtocol* GetProtocol() override - { - return &protocol; - } - - private: - Oyster::Network::CustomNetProtocol protocol; - }; - - struct Protocol_General_DisconnectKick :public Oyster::Network::CustomProtocolObject - { - Protocol_General_DisconnectKick() - { - this->protocol[0].value = protocol_General_DisconnectKick; - this->protocol[0].type = Oyster::Network::NetAttributeType_Short; - } - Oyster::Network::CustomNetProtocol* GetProtocol() override - { - return &protocol; - } - - private: - Oyster::Network::CustomNetProtocol protocol; - }; struct Protocol_General_Text :public Oyster::Network::CustomProtocolObject { @@ -90,8 +48,8 @@ namespace GameLogic Protocol_General_Text() { - this->protocol[0].value = protocol_General_Text; - this->protocol[0].type = Oyster::Network::NetAttributeType_Short; + this->protocol[protocol_INDEX_ID].value = protocol_General_Text; + this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_CharArray; } Oyster::Network::CustomNetProtocol* GetProtocol() override diff --git a/Code/Game/GameProtocols/GameProtocols.h b/Code/Game/GameProtocols/GameProtocols.h index a1738e33..4c52e716 100644 --- a/Code/Game/GameProtocols/GameProtocols.h +++ b/Code/Game/GameProtocols/GameProtocols.h @@ -6,7 +6,4 @@ #include "LobbyProtocols.h" #include "ControlProtocols.h" -#include "TEST_PROTOCOLS.h" - - #endif // !GAMEPROTOCOLS_GAMEPROTOCOLS_H diff --git a/Code/Game/GameProtocols/GameProtocols.vcxproj b/Code/Game/GameProtocols/GameProtocols.vcxproj index 475b2b5f..d4d5fb7a 100644 --- a/Code/Game/GameProtocols/GameProtocols.vcxproj +++ b/Code/Game/GameProtocols/GameProtocols.vcxproj @@ -160,7 +160,6 @@ - diff --git a/Code/Game/GameProtocols/LobbyProtocols.h b/Code/Game/GameProtocols/LobbyProtocols.h index 433aeec2..71e6e3b6 100644 --- a/Code/Game/GameProtocols/LobbyProtocols.h +++ b/Code/Game/GameProtocols/LobbyProtocols.h @@ -18,8 +18,8 @@ namespace GameLogic Protocol_LobbyCreateGame() { - this->protocol[0].value = protocol_Lobby_CreateGame; - this->protocol[0].type = Oyster::Network::NetAttributeType_Short; + this->protocol[protocol_INDEX_ID].value = protocol_Lobby_CreateGame; + this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_CharArray; this->protocol[2].type = Oyster::Network::NetAttributeType_Char; @@ -46,8 +46,8 @@ namespace GameLogic Protocol_LobbyJoinGame() { - this->protocol[0].value = protocol_Lobby_JoinGame; - this->protocol[0].type = Oyster::Network::NetAttributeType_Short; + this->protocol[protocol_INDEX_ID].value = protocol_Lobby_JoinGame; + this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Char; } @@ -67,8 +67,8 @@ namespace GameLogic Protocol_LobbyStartGame() { - this->protocol[0].value = protocol_Lobby_StartGame; - this->protocol[0].type = Oyster::Network::NetAttributeType_Short; + this->protocol[protocol_INDEX_ID].value = protocol_Lobby_StartGame; + this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Char; } @@ -84,13 +84,23 @@ namespace GameLogic struct Protocol_LobbyJoinLobby :public Oyster::Network::CustomProtocolObject { - Protocol_LobbyJoinLobby() + int LobbyID; + Protocol_LobbyJoinLobby(int id = -1) { - this->protocol[0].value = protocol_Lobby_JoinLobby; - this->protocol[0].type = Oyster::Network::NetAttributeType_Short; + this->protocol[protocol_INDEX_ID].value = protocol_Lobby_JoinLobby; + this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + + this->protocol[1].type = Oyster::Network::NetAttributeType_Int; + LobbyID = id; + } + Protocol_LobbyJoinLobby(Oyster::Network::CustomNetProtocol& o) + { + LobbyID = o[1].value.netInt; } Oyster::Network::CustomNetProtocol* GetProtocol() override { + this->protocol[1].value = LobbyID; + return &protocol; } @@ -103,8 +113,8 @@ namespace GameLogic Protocol_LobbyLeaveLobby() { - this->protocol[0].value = protocol_Lobby_LeaveLobby; - this->protocol[0].type = Oyster::Network::NetAttributeType_Short; + this->protocol[protocol_INDEX_ID].value = protocol_Lobby_LeaveLobby; + this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; } Oyster::Network::CustomNetProtocol* GetProtocol() override { @@ -115,6 +125,53 @@ namespace GameLogic Oyster::Network::CustomNetProtocol protocol; }; + struct Protocol_LobbyUpdate :public Oyster::Network::CustomProtocolObject + { + struct LobbyUpdateData + { + std::string mapName; + int LobbyId; + }; + int count; + LobbyUpdateData* data; + Protocol_LobbyUpdate() + { + this->protocol[protocol_INDEX_ID].value = protocol_Lobby_LeaveLobby; + this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + + this->protocol[1].type = Oyster::Network::NetAttributeType_Int; + } + Protocol_LobbyUpdate( Oyster::Network::CustomNetProtocol* p ) + { + count = (*p)[1].value.netInt; + data = new LobbyUpdateData[count]; + for (int i = 0; i < count; i++) + { + //data[i].mapName = (*p)[i].value. + } + } + ~Protocol_LobbyUpdate() + { + delete [] data; + data = 0; + } + Oyster::Network::CustomNetProtocol* GetProtocol() override + { + this->protocol[1].value.netInt = count; + for (int i = 2; i < count; i++) + { + protocol[i].type = Oyster::Network::NetAttributeType_CharArray; + protocol[i+1].type = Oyster::Network::NetAttributeType_Int; + + protocol[i].value.netCharPtr = const_cast(data[i-2].mapName.c_str()); + protocol[i+1].value.netInt = data[i-1].LobbyId; + } + return &protocol; + } + + private: + Oyster::Network::CustomNetProtocol protocol; + }; } #endif // !GAMELOGIC_PLAYER_PROTOCOLS_H diff --git a/Code/Game/GameProtocols/ObjectProtocols.h b/Code/Game/GameProtocols/ObjectProtocols.h index bc10c348..6a1cde90 100644 --- a/Code/Game/GameProtocols/ObjectProtocols.h +++ b/Code/Game/GameProtocols/ObjectProtocols.h @@ -17,29 +17,16 @@ namespace GameLogic Protocol_CreateObject() { - this->protocol[0].value = protocol_Gameplay_CreateObject; - this->protocol[0].type = Oyster::Network::NetAttributeType_Short; + this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_CreateObject; + this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Int; this->protocol[2].type = Oyster::Network::NetAttributeType_CharArray; - this->protocol[3].type = Oyster::Network::NetAttributeType_Float; - this->protocol[4].type = Oyster::Network::NetAttributeType_Float; - this->protocol[5].type = Oyster::Network::NetAttributeType_Float; - this->protocol[6].type = Oyster::Network::NetAttributeType_Float; - this->protocol[7].type = Oyster::Network::NetAttributeType_Float; - this->protocol[8].type = Oyster::Network::NetAttributeType_Float; - this->protocol[9].type = Oyster::Network::NetAttributeType_Float; - this->protocol[10].type = Oyster::Network::NetAttributeType_Float; - this->protocol[11].type = Oyster::Network::NetAttributeType_Float; - this->protocol[12].type = Oyster::Network::NetAttributeType_Float; - this->protocol[13].type = Oyster::Network::NetAttributeType_Float; - this->protocol[14].type = Oyster::Network::NetAttributeType_Float; - this->protocol[15].type = Oyster::Network::NetAttributeType_Float; - this->protocol[16].type = Oyster::Network::NetAttributeType_Float; - this->protocol[17].type = Oyster::Network::NetAttributeType_Float; - this->protocol[18].type = Oyster::Network::NetAttributeType_Float; - + for (int i = 3; i <= 18; i++) + { + this->protocol[i].type = Oyster::Network::NetAttributeType_Float; + } } Oyster::Network::CustomNetProtocol* GetProtocol() override { @@ -70,7 +57,6 @@ namespace GameLogic Oyster::Network::CustomNetProtocol protocol; }; - struct Protocol_ObjectPosition :public Oyster::Network::CustomProtocolObject { int object_ID; @@ -79,52 +65,40 @@ namespace GameLogic Protocol_ObjectPosition() { - this->protocol[0].value = protocol_Gameplay_ObjectPosition; - this->protocol[0].type = Oyster::Network::NetAttributeType_Int; + this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_ObjectPosition; + this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Int; this->protocol[1].type = Oyster::Network::NetAttributeType_Int; - - this->protocol[2].type = Oyster::Network::NetAttributeType_Float; - this->protocol[3].type = Oyster::Network::NetAttributeType_Float; - this->protocol[4].type = Oyster::Network::NetAttributeType_Float; - this->protocol[5].type = Oyster::Network::NetAttributeType_Float; - this->protocol[6].type = Oyster::Network::NetAttributeType_Float; - this->protocol[7].type = Oyster::Network::NetAttributeType_Float; - this->protocol[8].type = Oyster::Network::NetAttributeType_Float; - this->protocol[9].type = Oyster::Network::NetAttributeType_Float; - this->protocol[10].type = Oyster::Network::NetAttributeType_Float; - this->protocol[11].type = Oyster::Network::NetAttributeType_Float; - this->protocol[12].type = Oyster::Network::NetAttributeType_Float; - this->protocol[13].type = Oyster::Network::NetAttributeType_Float; - this->protocol[14].type = Oyster::Network::NetAttributeType_Float; - this->protocol[15].type = Oyster::Network::NetAttributeType_Float; - this->protocol[16].type = Oyster::Network::NetAttributeType_Float; - this->protocol[17].type = Oyster::Network::NetAttributeType_Float; - + + for (int i = 2; i <= 17; i++) + { + this->protocol[i].type = Oyster::Network::NetAttributeType_Float; + } + object_ID = -1; + memset(&worldMatrix[0], 0, sizeof(float) * 16); } - Protocol_ObjectPosition(float m[16]) + Protocol_ObjectPosition(float m[16], int id) { + this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_ObjectPosition; + this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Int; + + this->protocol[1].type = Oyster::Network::NetAttributeType_Int; + + for (int i = 2; i <= 17; i++) + { + this->protocol[i].type = Oyster::Network::NetAttributeType_Float; + } + + object_ID = id; memcpy(&worldMatrix[0], &m[0], sizeof(float)*16); } Oyster::Network::CustomNetProtocol* GetProtocol() override { this->protocol[1].value = object_ID; - this->protocol[2].value = worldMatrix[1]; - this->protocol[3].value = worldMatrix[2]; - this->protocol[4].value = worldMatrix[3]; - this->protocol[5].value = worldMatrix[4]; - this->protocol[6].value = worldMatrix[5]; - this->protocol[7].value = worldMatrix[6]; - this->protocol[8].value = worldMatrix[7]; - this->protocol[9].value = worldMatrix[8]; - this->protocol[10].value = worldMatrix[9]; - this->protocol[11].value = worldMatrix[10]; - this->protocol[12].value = worldMatrix[11]; - this->protocol[13].value = worldMatrix[12]; - this->protocol[14].value = worldMatrix[13]; - this->protocol[15].value = worldMatrix[14]; - this->protocol[16].value = worldMatrix[15]; - this->protocol[17].value = worldMatrix[16]; + for (int i = 2; i <= 17; i++) + { + this->protocol[i].value = worldMatrix[i-2]; + } return &protocol; } @@ -138,8 +112,8 @@ namespace GameLogic Protocol_RemoveObject() { - this->protocol[0].value = protocol_Gameplay_RemoveObject; - this->protocol[0].type = Oyster::Network::NetAttributeType_Short; + this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_RemoveObject; + this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Int; } diff --git a/Code/Game/GameProtocols/PlayerProtocols.h b/Code/Game/GameProtocols/PlayerProtocols.h index 8ec54cea..68065071 100644 --- a/Code/Game/GameProtocols/PlayerProtocols.h +++ b/Code/Game/GameProtocols/PlayerProtocols.h @@ -25,8 +25,8 @@ namespace GameLogic Protocol_PlayerMovement() { - this->protocol[0].value = protocol_Gameplay_PlayerNavigation; - this->protocol[0].type = Oyster::Network::NetAttributeType_Short; + this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_PlayerNavigation; + this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Bool; this->protocol[2].type = Oyster::Network::NetAttributeType_Bool; @@ -71,8 +71,8 @@ namespace GameLogic Protocol_PlayerMouse() { - this->protocol[0].value = protocol_Gameplay_PlayerMouseMovement; - this->protocol[0].type = Oyster::Network::NetAttributeType_Short; + this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_PlayerMouseMovement; + this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Float; this->protocol[2].type = Oyster::Network::NetAttributeType_Float; @@ -105,8 +105,8 @@ namespace GameLogic Protocol_PlayerPosition() { - this->protocol[0].value = protocol_Gameplay_PlayerPosition; - this->protocol[0].type = Oyster::Network::NetAttributeType_Short; + this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_PlayerPosition; + this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Float; this->protocol[2].type = Oyster::Network::NetAttributeType_Float; diff --git a/Code/Game/GameProtocols/ProtocolIdentificationID.h b/Code/Game/GameProtocols/ProtocolIdentificationID.h index 43cb4981..d99b7e65 100644 --- a/Code/Game/GameProtocols/ProtocolIdentificationID.h +++ b/Code/Game/GameProtocols/ProtocolIdentificationID.h @@ -9,30 +9,29 @@ /** Index where the identifier is located(aka protocol identification index) **/ +/* Use this as id accesser since it may change in the future. */ #define protocol_INDEX_ID 0 /***********************************/ -/********* RESERVERD PROTOCOLS *****/ +/********* RESERVERD PROTOCOLS ***************************************************************************************************/ /********** [ 0 - 100 ] *********/ - +#define protocol_RESERVED_MIN 0 +#define protocol_RESERVED_MAX 99 /***********************************/ -/********* GENERAL PROTOCOLS *******/ +/********* GENERAL PROTOCOLS ***************************************************************************************************/ /***********[ 100 - 200 ]***********/ -#define protocol_GeneralMIN 100 /* This defines lower bounds of general protocols (okay to have same value as the first since this should not realy be sent). */ -#define protocol_General_Disconnect 100 -#define protocol_General_Ping 102 -#define protocol_General_Text 103 -#define protocol_General_Status 104 -#define protocol_General_DisconnectKick 105 +#define protocol_GeneralMIN 100 /* This defines lower bounds of general protocols (okay to have same value on first element). */ +#define protocol_General_Status 100 +#define protocol_General_Text 101 #define protocol_GeneralMAX 199 /***********************************/ -/********* LOBBY PROTOCOLS *********/ +/********* LOBBY PROTOCOLS ***************************************************************************************************/ /***********[ 200 - 300 ]***********/ -#define protocol_LobbyMIN 200 +#define protocol_LobbyMIN 200 #define protocol_Lobby_CreateGame 200 #define protocol_Lobby_JoinGame 201 #define protocol_Lobby_StartGame 202 @@ -43,7 +42,7 @@ /***********************************/ -/********* GAMEPLAY PROTOCOLS ******/ +/********* GAMEPLAY PROTOCOLS ***************************************************************************************************/ /***********[ 300 - 400 ]***********/ #define protocol_GameplayMIN 300 #define protocol_Gameplay_PlayerNavigation 300 @@ -56,7 +55,7 @@ /************************************/ -/*********** PROTOCOL MACROS ********/ +/*********** PROTOCOL MACROS ***************************************************************************************************/ /************************************/ inline bool ProtocolIsLobby(short ID) { return (ID >= protocol_LobbyMIN && ID <= protocol_LobbyMAX); } inline bool ProtocolIsGeneral(short ID) { return (ID >= protocol_GeneralMIN && ID <= protocol_GeneralMAX); } @@ -65,11 +64,8 @@ inline bool ProtocolIsGameplay(short ID) { return (ID >= protocol_GameplayMIN && /***********************************/ -/*********** TEST PROTOCOLS ********/ -/***********[ 1000 - x ]************/ -#define PROTOCOL_TEST 1000 - - +/*********** TEST PROTOCOLS *******************************************************************************************************/ +/***********[ x - x ]************/ #endif // !GAMEPROTOCOL_PROTOCOL_DEFINITION_ID_H diff --git a/Code/Game/GameProtocols/TEST_PROTOCOLS.h b/Code/Game/GameProtocols/TEST_PROTOCOLS.h deleted file mode 100644 index df171c1f..00000000 --- a/Code/Game/GameProtocols/TEST_PROTOCOLS.h +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef GAMESERVER_TEST_H -#define GAMESERVER_TEST_H - -#include -#include "ProtocolIdentificationID.h" - - - -namespace GameLogic -{ - struct Protocol_TEST :public Oyster::Network::CustomProtocolObject - { - int ProtocolID; - char text; - - Protocol_TEST() - { - this->protocol[0].value = ProtocolID = PROTOCOL_TEST; - - this->protocol[0].type = Oyster::Network::NetAttributeType_Int; - this->protocol[1].type = Oyster::Network::NetAttributeType_Char; - } - Oyster::Network::CustomNetProtocol* GetProtocol() override - { - this->protocol[1].value = text; - - return &protocol; - } - - private: - Oyster::Network::CustomNetProtocol protocol; - }; -} - -#endif // !GAMESERVER_TEST_H \ No newline at end of file diff --git a/Code/Misc/DynamicArray.h b/Code/Misc/DynamicArray.h index ffc790e6..35df725f 100644 --- a/Code/Misc/DynamicArray.h +++ b/Code/Misc/DynamicArray.h @@ -33,11 +33,16 @@ namespace Utility void Clear(); - void Expand(int elements = 0); + void Resize(unsigned int size); + + void Swap(unsigned int a, unsigned int b); unsigned int Size() const; unsigned int Capacity() const; + private: + void Expand(int elements = 0); + private: T* data; int size; @@ -179,8 +184,44 @@ namespace Utility this->capacity = 0; } + template void DynamicArray::Resize(unsigned int size) + { + if (size == this->capacity) return; //No need to resize + + T* temp = new T[size]; + + for (int i = 0; i < this->size; i++) + { + temp[i] = this->data[i]; + } + this->capacity = size; + this->size = size; + + delete [] this->data; + this->data = temp; + } + + template void DynamicArray::Swap(unsigned int a, unsigned int b) + { + T temp = this->data[a]; + this->data[a] = this->data[b]; + this->data[b] = temp; + } + + template unsigned int DynamicArray::Size() const + { + return (unsigned int)this->size; + } + + template unsigned int DynamicArray::Capacity() const + { + return (unsigned int)this->capacity; + } + template void DynamicArray::Expand(int elements) { + if(elements < 1) return; + int newSize = this->size + elements; if(newSize >= this->capacity) @@ -198,16 +239,6 @@ namespace Utility } } - template unsigned int DynamicArray::Size() const - { - return (unsigned int)this->size; - } - - template unsigned int DynamicArray::Capacity() const - { - return (unsigned int)this->capacity; - } - #pragma endregion } } diff --git a/Code/Misc/OysterCallback.h b/Code/Misc/OysterCallback.h index 64ff9a8f..258408cb 100644 --- a/Code/Misc/OysterCallback.h +++ b/Code/Misc/OysterCallback.h @@ -27,19 +27,53 @@ namespace Oyster CallbackType_PostBox, CallbackType_Function, CallbackType_Object, + CallbackType_Unknown, }; template - union OysterCallback + union OysterCallbackValue { IPostBox* callbackPostBox; CallbackObject *callbackObject; typename CallbackFunction::FNC callbackFunction; - OysterCallback() { memset(this, 0, sizeof(OysterCallback)); } - OysterCallback(IPostBox* postbox) { callbackPostBox = postbox; } - OysterCallback(CallbackObject* obj) { callbackObject = obj; } - OysterCallback(typename CallbackFunction::FNC function) { callbackFunction = function; } + OysterCallbackValue() { memset(this, 0, sizeof(OysterCallbackValue)); } + OysterCallbackValue(IPostBox* postbox) { callbackPostBox = postbox; } + OysterCallbackValue(CallbackObject* obj) { callbackObject = obj; } + OysterCallbackValue(typename CallbackFunction::FNC function) { callbackFunction = function; } + }; + + template + struct OysterCallback + { + OysterCallbackValue value; + CallbackType callbackType; + + OysterCallback() :callbackType(CallbackType_Unknown){} + bool operator()() + { + return true; + } + bool operator()(ParamVal e) + { + switch (callbackType) + { + case CallbackType_Function: + value.callbackFunction(e); + return true; + break; + case CallbackType_Object: + value.callbackObject->ObjectCallback(e); + return true; + break; + case CallbackType_PostBox: + value.callbackPostBox->Post(e); + return true; + break; + } + return false; + } + }; } } diff --git a/Code/Misc/Thread/IThreadObject.h b/Code/Misc/Thread/IThreadObject.h index f6b4d638..5cf0e72a 100644 --- a/Code/Misc/Thread/IThreadObject.h +++ b/Code/Misc/Thread/IThreadObject.h @@ -11,27 +11,29 @@ namespace Oyster { namespace Thread { - /** - * Inherit this class to get threading compatibility. - */ - class IThreadObject - { - public: - /** - * Override this to get notified when the thread is started. - */ - virtual void ThreadEntry() { } - /** - * Override this to get notified when the thread is about to exit. - */ - virtual void ThreadExit() { } - /** - * This function is required to get threading working. - * Note that this function is NOT thread safe. - * OBS! Do not highjack the looping. - */ - virtual bool DoWork ( ) = 0; - }; + //This class was moved to OysterThread.h + + ///** + //* Inherit this class to get threading compatibility. + //*/ + //class IThreadObject + //{ + //public: + // /** + // * Override this to get notified when the thread is started. + // */ + // virtual void ThreadEntry() { } + // /** + // * Override this to get notified when the thread is about to exit. + // */ + // virtual void ThreadExit() { } + // /** + // * This function is required to get threading working. + // * Note that this function is NOT thread safe. + // * OBS! Do not highjack the looping. + // */ + // virtual bool DoWork ( ) = 0; + //}; } } diff --git a/Code/Misc/Thread/OysterThread.h b/Code/Misc/Thread/OysterThread.h index ec150857..84c32144 100644 --- a/Code/Misc/Thread/OysterThread.h +++ b/Code/Misc/Thread/OysterThread.h @@ -6,10 +6,34 @@ #define MISC_OYSTER_THREAD_H #include "IThreadObject.h" + namespace Oyster { namespace Thread { + /** + * Inherit this class to get threading compatibility. + */ + class IThreadObject + { + public: + /** + * Override this to get notified when the thread is started. + */ + virtual void ThreadEntry() { } + /** + * Override this to get notified when the thread is about to exit. + */ + virtual void ThreadExit() { } + /** + * This function is required to get threading working. + * Note that this function is NOT thread safe. + * OBS! Do not highjack the looping. + */ + virtual bool DoWork ( ) = 0; + }; + typedef bool (*ThreadFnc)(void); + enum OYSTER_THREAD_ERROR { OYSTER_THREAD_ERROR_SUCCESS, @@ -43,12 +67,16 @@ namespace Oyster virtual~OysterThread(); OYSTER_THREAD_ERROR Create(IThreadObject* worker, bool start, bool detach = false); + OYSTER_THREAD_ERROR Create(ThreadFnc worker, bool start, bool detach = false); + //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 Pause(); OYSTER_THREAD_ERROR Pause(int mSec); OYSTER_THREAD_ERROR Resume(); - OYSTER_THREAD_ERROR Reset(IThreadObject* worker = 0); + OYSTER_THREAD_ERROR SetWorker(IThreadObject* worker = 0); + OYSTER_THREAD_ERROR SetWorker(ThreadFnc worker = 0); OYSTER_THREAD_ERROR Terminate(bool wait = true); OYSTER_THREAD_ERROR Wait(); OYSTER_THREAD_ERROR Wait(int mSec); diff --git a/Code/Misc/Thread/OysterThread_Impl.cpp b/Code/Misc/Thread/OysterThread_Impl.cpp index 9b4b2a06..ab7f6ac6 100644 --- a/Code/Misc/Thread/OysterThread_Impl.cpp +++ b/Code/Misc/Thread/OysterThread_Impl.cpp @@ -4,6 +4,8 @@ #include "OysterThread.h" #include "..\Utilities.h" +#include "..\OysterCallback.h" + #include #include #include @@ -23,13 +25,31 @@ using namespace Utility::DynamicMemory; OYSTER_THREAD_STATE_NORMAL, OYSTER_THREAD_STATE_DEAD, }; + struct OwnerContainer + { + Oyster::Callback::CallbackType type; + union OwnerValue + { + IThreadObject* obj; + ThreadFnc fnc; + OwnerValue() { memset(this, 0, sizeof(OwnerValue)); } + OwnerValue(IThreadObject* v) { obj = v; } + OwnerValue(ThreadFnc v) { fnc = v; } + } value; + operator bool() + { + return (value.fnc ||value.obj); + } + }; struct ThreadData { - OYSTER_THREAD_STATE state; // msec; // ownerObj; // + OwnerContainer ownerObj; // + std::atomic msec; //workerThread.get_id()) if(this->workerThread.joinable()) + { this->workerThread.join(); + this->isCreated = false; + this->threadData = 0; + } } else { @@ -73,7 +99,49 @@ using namespace Utility::DynamicMemory; } return OYSTER_THREAD_ERROR_SUCCESS; } - OYSTER_THREAD_ERROR Create(ThreadFunction fnc, IThreadObject* worker, bool start, bool detach) + //OYSTER_THREAD_ERROR Create(ThreadFunction fnc, IThreadObject* worker, bool start, bool detach) + //{ + // if(this->isCreated ) return OYSTER_THREAD_ERROR_ThreadAlreadyCreated; + // + // threadData = new ThreadData(); + // if(start) + // this->threadData->state = OYSTER_THREAD_STATE_NORMAL; + // else + // this->threadData->state = OYSTER_THREAD_STATE_IDLE; + // threadData->owner = worker; + // threadData->prio = OYSTER_THREAD_PRIORITY_3; + // + // workerThread = std::thread(fnc, this->threadData); + // + // if(detach) + // this->workerThread.detach(); + // + // isCreated = true; + // + // return OYSTER_THREAD_ERROR_SUCCESS; + //} + //OYSTER_THREAD_ERROR Create(ThreadFunction fnc, Oyster::Callback::OysterCallback worker, bool start, bool detach) + //{ + // if(this->isCreated ) return OYSTER_THREAD_ERROR_ThreadAlreadyCreated; + // + // threadData = new ThreadData(); + // if(start) + // this->threadData->state = OYSTER_THREAD_STATE_NORMAL; + // else + // this->threadData->state = OYSTER_THREAD_STATE_IDLE; + // threadData->ownerObj = worker; + // threadData->prio = OYSTER_THREAD_PRIORITY_3; + // + // workerThread = std::thread(fnc, this->threadData); + // + // if(detach) + // this->workerThread.detach(); + // + // isCreated = true; + // + // return OYSTER_THREAD_ERROR_SUCCESS; + //} + OYSTER_THREAD_ERROR Create(ThreadFunction fnc, OwnerContainer worker, bool start, bool detach) { if(this->isCreated ) return OYSTER_THREAD_ERROR_ThreadAlreadyCreated; @@ -82,7 +150,7 @@ using namespace Utility::DynamicMemory; this->threadData->state = OYSTER_THREAD_STATE_NORMAL; else this->threadData->state = OYSTER_THREAD_STATE_IDLE; - threadData->owner = worker; + threadData->ownerObj = worker; threadData->prio = OYSTER_THREAD_PRIORITY_3; workerThread = std::thread(fnc, this->threadData); @@ -95,11 +163,19 @@ using namespace Utility::DynamicMemory; return OYSTER_THREAD_ERROR_SUCCESS; } }; + struct OysterThread::PrivateData { SmartPointer data; - OYSTER_THREAD_ERROR Create(ThreadFunction fnc, IThreadObject* worker, bool start, bool detach) + PrivateData(){} + ~PrivateData() + { + if(data) + if(data->threadData) + memset(&data->threadData->ownerObj, 0, sizeof(OwnerContainer)); + } + OYSTER_THREAD_ERROR Create(ThreadFunction fnc, OwnerContainer worker, bool start, bool detach) { if(data) return OYSTER_THREAD_ERROR_ThreadAlreadyCreated; data = new RefData(); @@ -107,6 +183,9 @@ using namespace Utility::DynamicMemory; } OYSTER_THREAD_ERROR Terminate(bool wait) { + if(!data) + return OYSTER_THREAD_ERROR_FAILED; + return data->Terminate(wait); } }; @@ -131,15 +210,18 @@ using namespace Utility::DynamicMemory; } static bool DoWork(ThreadData* w) { - try + switch (w->ownerObj.type) { - if(w->owner) - return w->owner->DoWork(); - } - catch( ... ) - { - printf("Something went wrong on thread with id: [%i]", std::this_thread::get_id()); + case Oyster::Callback::CallbackType_Function: + if(w->ownerObj.value.fnc) return w->ownerObj.value.fnc(); + break; + + case Oyster::Callback::CallbackType_Object: + if(w->ownerObj.value.obj) return w->ownerObj.value.obj->DoWork(); + break; } + + return true; } static void CheckStatus(ThreadData* w) @@ -154,7 +236,7 @@ using namespace Utility::DynamicMemory; { CheckStatus(w); - if(w->owner) w->owner->ThreadEntry(); + if(w->ownerObj.value.obj) w->ownerObj.value.obj->ThreadEntry(); while (w->state == OYSTER_THREAD_STATE_NORMAL) { @@ -163,9 +245,10 @@ using namespace Utility::DynamicMemory; CheckStatus(w); } - if(w->owner) w->owner->ThreadExit(); + if(w->ownerObj.value.obj) w->ownerObj.value.obj->ThreadExit(); w->state = OYSTER_THREAD_STATE_DEAD; + delete w; } }; @@ -194,15 +277,52 @@ OysterThread::~OysterThread() } OYSTER_THREAD_ERROR OysterThread::Create(IThreadObject* worker, bool start, bool detach) +{ + if(!this->privateData) this->privateData = new PrivateData(); + + OwnerContainer c; + c.type = Oyster::Callback::CallbackType_Object; + c.value = worker; + return this->privateData->Create(ThreadHelp::ThreadingFunction, c, start, detach); +} +OYSTER_THREAD_ERROR OysterThread::Create(ThreadFnc worker, bool start, bool detach) +{ + if(!this->privateData) this->privateData = new PrivateData(); + + OwnerContainer c; + c.type = Oyster::Callback::CallbackType_Function; + c.value = worker; + return this->privateData->Create(ThreadHelp::ThreadingFunction, c, start, detach); +} + +/* +OYSTER_THREAD_ERROR OysterThread::Create(Oyster::Callback::CallbackObject* worker, bool start, bool detach) { if(!this->privateData) this->privateData = new PrivateData(); + + Oyster::Callback::OysterCallback<> temp; + temp.callbackType = Oyster::Callback::CallbackType_Object; + temp.value = worker; - return this->privateData->Create(ThreadHelp::ThreadingFunction, worker, start, detach); + return this->privateData->Create(ThreadHelp::ThreadingFunction, temp, start, detach); } +OYSTER_THREAD_ERROR OysterThread::Create(Oyster::Callback::CallbackFunction::FNC worker, bool start, bool detach) +{ + if(!this->privateData) + this->privateData = new PrivateData(); + + Oyster::Callback::OysterCallback<> temp; + temp.callbackType = Oyster::Callback::CallbackType_Function; + temp.value = worker; + + return this->privateData->Create(ThreadHelp::ThreadingFunction, temp, start, detach); +} +*/ + OYSTER_THREAD_ERROR OysterThread::Start() { - if(!this->privateData->data->threadData->owner) + if(!this->privateData->data->threadData->ownerObj) return OYSTER_THREAD_ERROR_ThreadHasNoWorker; if(this->privateData->data->threadData->state == OYSTER_THREAD_STATE_DEAD) @@ -235,9 +355,19 @@ OYSTER_THREAD_ERROR OysterThread::Resume() return OYSTER_THREAD_ERROR_SUCCESS; } -OYSTER_THREAD_ERROR OysterThread::Reset(IThreadObject* worker) +OYSTER_THREAD_ERROR OysterThread::SetWorker(IThreadObject* worker) { - this->privateData->data->threadData->owner = worker; + this->privateData->data->threadData->ownerObj.value = worker; + this->privateData->data->threadData->ownerObj.type = Oyster::Callback::CallbackType_Object; + + this->privateData->data->threadData->msec = 0; + + return OYSTER_THREAD_ERROR_SUCCESS;; +} +OYSTER_THREAD_ERROR OysterThread::SetWorker(ThreadFnc worker) +{ + this->privateData->data->threadData->ownerObj.value = worker; + this->privateData->data->threadData->ownerObj.type = Oyster::Callback::CallbackType_Function; this->privateData->data->threadData->msec = 0; diff --git a/Code/Misc/ThreadSafeQueue.h b/Code/Misc/ThreadSafeQueue.h index eedd9fd9..7c3e3f02 100644 --- a/Code/Misc/ThreadSafeQueue.h +++ b/Code/Misc/ThreadSafeQueue.h @@ -68,6 +68,8 @@ namespace Oyster template < typename Type > ThreadSafeQueue::~ThreadSafeQueue() { + if(!nrOfNodes) return; + stdMutex.lock(); if(this->front != NULL) diff --git a/Code/Misc/Utilities.h b/Code/Misc/Utilities.h index ae3124f9..7e76dbba 100644 --- a/Code/Misc/Utilities.h +++ b/Code/Misc/Utilities.h @@ -10,6 +10,9 @@ #ifndef UTILITIES_H #define UTILITIES_H +//warning C4150: deletion of pointer to incomplete type, no destructor called +#pragma warning(disable : 4150) + #include #include #include diff --git a/Code/Network/NetworkAPI/NetworkCallbackHelper.h b/Code/Network/NetworkAPI/NetworkCallbackHelper.h index 40c66899..963dc6de 100644 --- a/Code/Network/NetworkAPI/NetworkCallbackHelper.h +++ b/Code/Network/NetworkAPI/NetworkCallbackHelper.h @@ -7,6 +7,7 @@ #include + namespace Oyster { namespace Network @@ -26,24 +27,46 @@ namespace Oyster class NetworkClient; class CustomNetProtocol; - typedef void (*ClientConnectCallbackMethod)(NetworkClient*); - typedef void(*ProtocolRecieverFunction)(CustomNetProtocol& protocol); - struct ClientConnectedObject - { - virtual void ClientConnectCallback(NetworkClient* client) = 0; - }; - struct ProtocolRecieverObject - { - virtual void ProtocolRecievedCallback(CustomNetProtocol& protocol) = 0; - virtual void DisconnectedCallback(CustomNetProtocol& protocol) { }; - }; + + template + struct NetRecieverObject + { + virtual void NetworkCallback(Param) = 0; + }; + struct NetClientEvent :public NetRecieverObject + { + virtual void NetworkCallback(CustomNetProtocol& protocol) = 0; + virtual void Disconnected() { }; + }; + typedef NetRecieverObject ClientConnectedObject ; + typedef NetClientEvent ProtocolRecieverObject; + + + template + struct NetCallbackFunction + { + typedef void (*FNC)(Param); + }; + typedef NetCallbackFunction::FNC ClientConnectCallbackMethod; + typedef NetCallbackFunction::FNC ProtocolRecFunction; + struct NetClientMethods + { + typedef void(*Dissconnected)(void); + ProtocolRecFunction recieved; + Dissconnected dissconnected; + void operator()(CustomNetProtocol& obj) { if(recieved) recieved(obj); } + void operator()() { if(dissconnected) dissconnected(); } + }; + typedef NetClientMethods ProtocolRecieverFunction; + union RecieverObject { ClientConnectCallbackMethod clientConnectFnc; // !< A function pointer for sending or recieving NetworkClient ProtocolRecieverFunction protocolRecieverFnc; // !< A function pointer for sending or recieving CustomNetProtocol + ClientConnectedObject *clientConnectObject; // !< An object for sending or recieving NetworkClient - ProtocolRecieverObject *protocolRecievedObject; // !< An object for sending or recieving CustomNetProtocol + ProtocolRecieverObject *protocolRecievedObject; // !< An object with collected client events methods. RecieverObject() { memset(this, 0, sizeof(RecieverObject)); } RecieverObject(ClientConnectCallbackMethod o) { clientConnectFnc = o; } diff --git a/Code/Network/NetworkAPI/NetworkClient.cpp b/Code/Network/NetworkAPI/NetworkClient.cpp index 4019e50e..f8c55476 100644 --- a/Code/Network/NetworkAPI/NetworkClient.cpp +++ b/Code/Network/NetworkAPI/NetworkClient.cpp @@ -84,8 +84,8 @@ struct NetworkClient::PrivateData : public IThreadObject bool DoWork() { - if(!this->data)return true; - if(!this->data->connection.IsConnected()) return true; + if(!this->data) return false; + if(!this->data->connection.IsConnected()) return false; Send(); Recv(); @@ -113,6 +113,22 @@ struct NetworkClient::PrivateData : public IThreadObject SmartPointer temp = new OysterByte(); this->data->translator.Pack(temp, this->data->sendPostBox->FetchMessage()); errorCode = this->data->connection.Send(temp); + if(errorCode != 0) + { + //Failed + this->data->connection.Disconnect(); + + this->data->recvObjMutex.lock(); + if(this->data->callbackType == NetworkProtocolCallbackType_Function) + { + this->data->recvObj.protocolRecieverFnc(); + } + else if(this->data->callbackType == NetworkProtocolCallbackType_Object) + { + this->data->recvObj.protocolRecievedObject->Disconnected(); + } + this->data->recvObjMutex.unlock(); + } } this->data->postBoxMutex.unlock(); @@ -141,7 +157,7 @@ struct NetworkClient::PrivateData : public IThreadObject } else if(this->data->callbackType == NetworkProtocolCallbackType_Object) { - this->data->recvObj.protocolRecievedObject->ProtocolRecievedCallback(protocol); + this->data->recvObj.protocolRecievedObject->NetworkCallback(protocol); } this->data->recvObjMutex.unlock(); } @@ -178,8 +194,8 @@ NetworkClient::NetworkClient(RecieverObject recvObj, NetworkProtocolCallbackType NetworkClient::NetworkClient(RecieverObject recvObj, NetworkProtocolCallbackType type, unsigned int socket) { privateData = new PrivateData(socket); - this->privateData->data->recvObj = recvObj; this->privateData->data->callbackType = type; + this->privateData->data->recvObj = recvObj; this->privateData->data->thread.Create(this->privateData, true); } @@ -265,7 +281,7 @@ bool NetworkClient::operator ==(const int& ID) return this->privateData->data->ID == ID; } -int NetworkClient::Id() const +int NetworkClient::GetID() const { return this->privateData->data->ID; } \ No newline at end of file diff --git a/Code/Network/NetworkAPI/NetworkClient.h b/Code/Network/NetworkAPI/NetworkClient.h index 533ff23e..5a8dd5e5 100644 --- a/Code/Network/NetworkAPI/NetworkClient.h +++ b/Code/Network/NetworkAPI/NetworkClient.h @@ -48,7 +48,7 @@ namespace Oyster bool operator ==(const NetworkClient& obj); bool operator ==(const int& ID); - int Id() const; + int GetID() const; private: struct PrivateData; diff --git a/Code/Network/NetworkAPI/NetworkServer.cpp b/Code/Network/NetworkAPI/NetworkServer.cpp index 404f6efa..e47195bf 100644 --- a/Code/Network/NetworkAPI/NetworkServer.cpp +++ b/Code/Network/NetworkAPI/NetworkServer.cpp @@ -115,18 +115,20 @@ void NetworkServer::PrivateData::Stop() started = false; - thread.Stop(); + thread.Stop(true); } void NetworkServer::PrivateData::Shutdown() { if(listener) { + listener->Shutdown(); delete listener; listener = NULL; } started = false; + thread.Terminate(); ShutdownWinSock(); } @@ -153,7 +155,7 @@ void NetworkServer::PrivateData::CheckForNewClient() else if(initDesc.callbackType == NetworkClientCallbackType_Object) { Oyster::Network::NetworkClient *client = new Oyster::Network::NetworkClient(clientSocketNum); - initDesc.recvObj.clientConnectObject->ClientConnectCallback(client); + initDesc.recvObj.clientConnectObject->NetworkCallback(client); } } } diff --git a/Code/Network/NetworkDependencies/Connection.cpp b/Code/Network/NetworkDependencies/Connection.cpp index 6b1e72bd..7d6daca6 100644 --- a/Code/Network/NetworkDependencies/Connection.cpp +++ b/Code/Network/NetworkDependencies/Connection.cpp @@ -108,7 +108,11 @@ int Connection::InitiateClient() int Connection::Disconnect() { - return CloseSocket(this->socket); + int val = CloseSocket(this->socket); + this->socket = -1; + this->closed = true; + this->stillSending = false; + return val; } int Connection::Send(OysterByte &bytes) diff --git a/Code/Network/NetworkDependencies/IListener.h b/Code/Network/NetworkDependencies/IListener.h index f88f23ea..fc7bb4f5 100644 --- a/Code/Network/NetworkDependencies/IListener.h +++ b/Code/Network/NetworkDependencies/IListener.h @@ -17,6 +17,7 @@ namespace Oyster virtual ~IListener() {} virtual bool Init(unsigned int port) = 0; virtual int Accept() = 0; + virtual void Shutdown() = 0; }; } diff --git a/Code/Network/NetworkDependencies/Listener.cpp b/Code/Network/NetworkDependencies/Listener.cpp index e371ad54..6f3aaa37 100644 --- a/Code/Network/NetworkDependencies/Listener.cpp +++ b/Code/Network/NetworkDependencies/Listener.cpp @@ -72,7 +72,7 @@ void Listener::Stop() void Listener::Shutdown() { - thread.Stop(); + thread.Stop(false); } void Listener::SetPostBox(Oyster::Network::IPostBox* postBox) diff --git a/Code/Network/NetworkDependencies/Listener.h b/Code/Network/NetworkDependencies/Listener.h index 93188c4c..b45dc1f4 100644 --- a/Code/Network/NetworkDependencies/Listener.h +++ b/Code/Network/NetworkDependencies/Listener.h @@ -25,11 +25,10 @@ namespace Oyster Listener(Oyster::Network::IPostBox* postBox); ~Listener(); - bool Init(unsigned int port); + bool Init(unsigned int port) override; bool Init(unsigned int port, bool start); bool Start(); void Stop(); - void Shutdown(); void SetPostBox(IPostBox* postBox); diff --git a/Code/WindowManager/WindowShell.cpp b/Code/WindowManager/WindowShell.cpp index c0d60b16..308e610f 100644 --- a/Code/WindowManager/WindowShell.cpp +++ b/Code/WindowManager/WindowShell.cpp @@ -1,7 +1,5 @@ #include "WindowShell.h" #include - -// debug window include #include #include #include @@ -9,6 +7,8 @@ #pragma region Declarations + #define WINDOW_SHELL_CLASS_NAME L"MainWindowShellClassName" + struct _PrivateDataContainer { HINSTANCE hIns; @@ -81,12 +81,12 @@ bool WindowShell::CreateWin(WINDOW_INIT_DESC &desc) __windowShellData.parent = desc.parent; __windowShellData.hIns = desc.hInstance; - __windowShellData.windowClassName = L"MainWindowShellClassName"; + __windowShellData.windowClassName = WINDOW_SHELL_CLASS_NAME; #pragma region Register - WNDCLASSEX wc; - wc.cbSize = sizeof(WNDCLASSEX); + WNDCLASSEXW wc; + wc.cbSize = sizeof(WNDCLASSEXW); wc.hIconSm = NULL; wc.style = desc.windowClassStyle; wc.lpfnWndProc = desc.windowProcCallback; @@ -99,7 +99,7 @@ bool WindowShell::CreateWin(WINDOW_INIT_DESC &desc) wc.lpszMenuName = NULL; wc.lpszClassName = __windowShellData.windowClassName; - if( !RegisterClassEx(&wc) ) + if( !RegisterClassExW(&wc) ) { MessageBox(0, L"Failed to register class", L"Initialization error", 0); return false; @@ -137,7 +137,7 @@ bool WindowShell::CreateWin(WINDOW_INIT_DESC &desc) if(windowed) { - __windowShellData.hWnd = CreateWindowEx( + __windowShellData.hWnd = CreateWindowExW( 0, __windowShellData.windowClassName , desc.windowName, @@ -154,7 +154,7 @@ bool WindowShell::CreateWin(WINDOW_INIT_DESC &desc) } else { - __windowShellData.hWnd = CreateWindowEx( + __windowShellData.hWnd = CreateWindowExW( 0, __windowShellData.windowClassName , desc.windowName, @@ -237,10 +237,12 @@ unsigned int WindowShell::GetHeight() } bool WindowShell::Frame() { + if(!__windowShellData.hWnd) return true; + MSG msg = {0}; while (true) { - if(!__windowShellData.parent) + if(!__windowShellData.parent) //Parent takes care of this { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {