From 1e89bb4e307727e6803ff9d9ba576ab21914847f Mon Sep 17 00:00:00 2001 From: Dennis Andersen Date: Wed, 29 Jan 2014 15:01:14 +0100 Subject: [PATCH] Fixed protocol messaging. Known crash when recieveing protocol on server after a while... --- Code/Game/GameLogic/Game.cpp | 24 --- Code/Game/GameLogic/Game.h | 2 +- Code/Game/GameLogic/GameAPI.h | 2 +- Code/Game/GameLogic/Game_PlayerData.cpp | 4 +- Code/Game/GameProtocols/GameProtocols.vcxproj | 1 - Code/Game/GameProtocols/GameplayProtocols.h | 13 -- Code/Game/GameProtocols/LobbyProtocols.h | 11 ++ Code/Game/GameProtocols/ObjectProtocols.h | 24 +++ Code/Game/GameProtocols/PlayerProtocols.h | 16 ++ Code/Game/GameProtocols/Protocols.h | 1 - Code/Game/GameServer/GameLobby.h | 8 +- Code/Game/GameServer/GameSession.h | 24 ++- .../GameServer/Implementation/GameLobby.cpp | 10 +- .../GameLobby_ProtocolParser.cpp | 33 ++-- .../GameServer/Implementation/GameServer.cpp | 5 +- .../Implementation/GameSession_Gameplay.cpp | 185 +++++++++++------- Code/Network/NetworkAPI/NetworkClient.cpp | 7 +- Code/Network/NetworkAPI/NetworkClient.h | 5 +- Code/Network/NetworkAPI/NetworkServer.cpp | 8 +- .../NetworkDependencies/Connection.cpp | 32 ++- Code/Network/NetworkDependencies/Connection.h | 9 +- .../Network/NetworkDependencies/IConnection.h | 8 +- Code/Network/NetworkDependencies/Listener.cpp | 10 +- Code/Network/NetworkDependencies/Listener.h | 6 +- 24 files changed, 278 insertions(+), 170 deletions(-) delete mode 100644 Code/Game/GameProtocols/GameplayProtocols.h diff --git a/Code/Game/GameLogic/Game.cpp b/Code/Game/GameLogic/Game.cpp index b22b3522..4380777e 100644 --- a/Code/Game/GameLogic/Game.cpp +++ b/Code/Game/GameLogic/Game.cpp @@ -100,30 +100,6 @@ bool Game::NewFrame() API::Instance().Update(); - for (unsigned int i = 0; i < this->players.Size(); i++) - { - if(this->players[i]->player) this->players[i]->player->EndFrame(); - } - - for (unsigned int i = 0; i < this->players.Size(); i++) - { - if(this->players[i]->player) this->players[i]->player->BeginFrame(); - } - - API::Instance().Update(); - - for (unsigned int i = 0; i < this->players.Size(); i++) - { - if(this->players[i]->player) this->players[i]->player->EndFrame(); - } - - for (unsigned int i = 0; i < this->players.Size(); i++) - { - if(this->players[i]->player) this->players[i]->player->BeginFrame(); - } - - API::Instance().Update(); - for (unsigned int i = 0; i < this->players.Size(); i++) { if(this->players[i]->player) this->players[i]->player->EndFrame(); diff --git a/Code/Game/GameLogic/Game.h b/Code/Game/GameLogic/Game.h index 11c8d325..7d40a3c5 100644 --- a/Code/Game/GameLogic/Game.h +++ b/Code/Game/GameLogic/Game.h @@ -39,7 +39,7 @@ namespace GameLogic Oyster::Math::Float4x4 GetOrientation() override; int GetID() const override; OBJECT_TYPE GetObjectType() const override; - void Rotate(const float x, const float y) override; + void Rotate(const Oyster::Math3D::Float3 lookDir) override; Player *player; }; diff --git a/Code/Game/GameLogic/GameAPI.h b/Code/Game/GameLogic/GameAPI.h index 12f5022f..2f052927 100644 --- a/Code/Game/GameLogic/GameAPI.h +++ b/Code/Game/GameLogic/GameAPI.h @@ -79,7 +79,7 @@ namespace GameLogic * @param x: The relative x axis * @param y: The relative y axis **/ - virtual void Rotate(const float x, const float y) = 0; + virtual void Rotate(const Oyster::Math3D::Float3 lookDir) = 0; /******************************************************** * Uses the chosen players weapon based on input diff --git a/Code/Game/GameLogic/Game_PlayerData.cpp b/Code/Game/GameLogic/Game_PlayerData.cpp index e651a02f..ed628c5e 100644 --- a/Code/Game/GameLogic/Game_PlayerData.cpp +++ b/Code/Game/GameLogic/Game_PlayerData.cpp @@ -49,7 +49,7 @@ OBJECT_TYPE Game::PlayerData::GetObjectType() const { return this->player->GetType(); } -void Game::PlayerData::Rotate(const float x, const float y) +void Game::PlayerData::Rotate(const Oyster::Math3D::Float3 lookDir) { - this->player->Rotate(x, y); + } \ No newline at end of file diff --git a/Code/Game/GameProtocols/GameProtocols.vcxproj b/Code/Game/GameProtocols/GameProtocols.vcxproj index a5d5b19f..826f9df4 100644 --- a/Code/Game/GameProtocols/GameProtocols.vcxproj +++ b/Code/Game/GameProtocols/GameProtocols.vcxproj @@ -155,7 +155,6 @@ - diff --git a/Code/Game/GameProtocols/GameplayProtocols.h b/Code/Game/GameProtocols/GameplayProtocols.h deleted file mode 100644 index 8ffcd827..00000000 --- a/Code/Game/GameProtocols/GameplayProtocols.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef GAMEPROTOCOLS_GAMEPLAYPROTOCOLS_H -#define GAMEPROTOCOLS_GAMEPLAYPROTOCOLS_H - -#include -#include -#include "ProtocolIdentificationID.h" - -namespace GameLogic -{ - -} - -#endif // !GAMEPROTOCOLS_GAMEPLAYPROTOCOLS_H diff --git a/Code/Game/GameProtocols/LobbyProtocols.h b/Code/Game/GameProtocols/LobbyProtocols.h index 4a262404..8968de74 100644 --- a/Code/Game/GameProtocols/LobbyProtocols.h +++ b/Code/Game/GameProtocols/LobbyProtocols.h @@ -156,6 +156,13 @@ namespace GameLogic list.Reserve(10); } + Protocol_LobbyGameData(Oyster::Network::CustomNetProtocol& p) + { + this->protocol[protocol_INDEX_ID].value = protocol_Lobby_GameData; + this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + + list.Reserve(10); + } Oyster::Network::CustomNetProtocol* GetProtocol() override { int a = 1; @@ -186,6 +193,10 @@ namespace GameLogic this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Short; + } + Protocol_LobbyMainData(Oyster::Network::CustomNetProtocol& p) + { + } Oyster::Network::CustomNetProtocol* GetProtocol() override { diff --git a/Code/Game/GameProtocols/ObjectProtocols.h b/Code/Game/GameProtocols/ObjectProtocols.h index d37aee44..9c4e2df1 100644 --- a/Code/Game/GameProtocols/ObjectProtocols.h +++ b/Code/Game/GameProtocols/ObjectProtocols.h @@ -21,6 +21,10 @@ namespace GameLogic object_ID = -1; pickup_ID = -1; + } + Protocol_ObjectPickup(Oyster::Network::CustomNetProtocol& p) + { + } Protocol_ObjectPickup(int objectID, short pickupID) { @@ -60,6 +64,10 @@ namespace GameLogic object_ID = -1; health = 0.0f; + } + Protocol_ObjectDamage(Oyster::Network::CustomNetProtocol& p) + { + } Protocol_ObjectDamage(int id, float hp) { @@ -100,6 +108,10 @@ namespace GameLogic } object_ID = -1; memset(&worldMatrix[0], 0, sizeof(float) * 16); + } + Protocol_ObjectPosition(Oyster::Network::CustomNetProtocol& p) + { + } Protocol_ObjectPosition(float m[16], int id) { @@ -148,6 +160,10 @@ namespace GameLogic } object_ID = -1; memset(&worldMatrix[0], 0, sizeof(float) * 16); + } + Protocol_ObjectEnable(Oyster::Network::CustomNetProtocol& p) + { + } Protocol_ObjectEnable(float m[16], int id) { @@ -188,6 +204,10 @@ namespace GameLogic this->protocol[1].type = Oyster::Network::NetAttributeType_Int; this->protocol[2].type = Oyster::Network::NetAttributeType_Float; + } + Protocol_ObjectDisable(Oyster::Network::CustomNetProtocol& p) + { + } Protocol_ObjectDisable(int id, float time) { @@ -229,6 +249,10 @@ namespace GameLogic { this->protocol[i].type = Oyster::Network::NetAttributeType_Float; } + } + Protocol_ObjectCreate(Oyster::Network::CustomNetProtocol& p) + { + } Protocol_ObjectCreate(float m[16], int id, char *path) { diff --git a/Code/Game/GameProtocols/PlayerProtocols.h b/Code/Game/GameProtocols/PlayerProtocols.h index ffb009cf..988693ce 100644 --- a/Code/Game/GameProtocols/PlayerProtocols.h +++ b/Code/Game/GameProtocols/PlayerProtocols.h @@ -32,6 +32,10 @@ namespace GameLogic this->protocol[2].type = Oyster::Network::NetAttributeType_Bool; this->protocol[3].type = Oyster::Network::NetAttributeType_Bool; this->protocol[4].type = Oyster::Network::NetAttributeType_Bool; + } + Protocol_PlayerMovement(Oyster::Network::CustomNetProtocol& p) + { + } const Protocol_PlayerMovement& operator=(Oyster::Network::CustomNetProtocol& val) { @@ -72,6 +76,10 @@ namespace GameLogic this->protocol[2].type = Oyster::Network::NetAttributeType_Float; this->protocol[3].type = Oyster::Network::NetAttributeType_Float; + } + Protocol_PlayerLook(Oyster::Network::CustomNetProtocol& p) + { + } const Protocol_PlayerLook& operator=(Oyster::Network::CustomNetProtocol& val) { @@ -108,6 +116,10 @@ namespace GameLogic this->protocol[2].type = Oyster::Network::NetAttributeType_Float; this->protocol[3].type = Oyster::Network::NetAttributeType_Float; + } + Protocol_PlayerChangeWeapon(Oyster::Network::CustomNetProtocol& p) + { + } const Protocol_PlayerChangeWeapon& operator=(Oyster::Network::CustomNetProtocol& val) { @@ -132,6 +144,10 @@ namespace GameLogic this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Bool; + } + Protocol_PlayerShot(Oyster::Network::CustomNetProtocol& p) + { + } const Protocol_PlayerShot& operator=(Oyster::Network::CustomNetProtocol& val) { diff --git a/Code/Game/GameProtocols/Protocols.h b/Code/Game/GameProtocols/Protocols.h index 1e7236c8..06b0df82 100644 --- a/Code/Game/GameProtocols/Protocols.h +++ b/Code/Game/GameProtocols/Protocols.h @@ -5,6 +5,5 @@ #include "PlayerProtocols.h" #include "LobbyProtocols.h" #include "GeneralProtocols.h" -#include "GameplayProtocols.h" #endif // !GAMEPROTOCOLS_GAMEPROTOCOLS_H diff --git a/Code/Game/GameServer/GameLobby.h b/Code/Game/GameServer/GameLobby.h index 1391641e..47e3b329 100644 --- a/Code/Game/GameServer/GameLobby.h +++ b/Code/Game/GameServer/GameLobby.h @@ -29,11 +29,11 @@ namespace DanBias void GeneralText(GameLogic::Protocol_General_Text& p, Oyster::Network::NetworkClient* c); //id = protocol_General_Text: void LobbyCreateGame(GameLogic::Protocol_LobbyCreateGame& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Create: void LobbyStartGame(GameLogic::Protocol_LobbyStartGame& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Start: - void LobbyJoin(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Join: - void LobbyLogin(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Login: + void LobbyJoin(GameLogic::Protocol_LobbyJoin& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Join: + void LobbyLogin(GameLogic::Protocol_LobbyLogin& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Login: void LobbyRefresh(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Refresh: - void LobbyMainData(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_MainData: - void LobbyGameData(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_GameData: + void LobbyMainData(GameLogic::Protocol_LobbyMainData& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_MainData: + void LobbyGameData(GameLogic::Protocol_LobbyGameData& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_GameData: private: void ClientEventCallback(Oyster::Network::NetEvent e) override; diff --git a/Code/Game/GameServer/GameSession.h b/Code/Game/GameServer/GameSession.h index ed480c35..e75be66d 100644 --- a/Code/Game/GameServer/GameSession.h +++ b/Code/Game/GameServer/GameSession.h @@ -16,6 +16,7 @@ #include #include #include +#include namespace DanBias @@ -55,17 +56,31 @@ namespace DanBias //Private member functions private: - //Handles all events recieved - //void ParseEvents(); - + // TODO: find out what this method does.. void ClientEventCallback(Oyster::Network::NetEvent e) override; - void ParseProtocol(Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c); + //Sends a client to the owner, if obj is NULL then all clients is sent void SendToOwner(DanBias::GameClient* obj); //Frame function, derived from IThreadObject bool DoWork ( ) override; + + private: + void ParseProtocol (Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c); + + void Gameplay_PlayerMovement ( GameLogic::Protocol_PlayerMovement& p, DanBias::GameClient* c ); + void Gameplay_PlayerLookDir ( GameLogic::Protocol_PlayerLook& p, DanBias::GameClient* c ); + void Gameplay_PlayerChangeWeapon ( GameLogic::Protocol_PlayerChangeWeapon& p, DanBias::GameClient* c ); + void Gameplay_PlayerShot ( GameLogic::Protocol_PlayerShot& p, DanBias::GameClient* c ); + void Gameplay_ObjectPickup ( GameLogic::Protocol_ObjectPickup& p, DanBias::GameClient* c ); + void Gameplay_ObjectDamage ( GameLogic::Protocol_ObjectDamage& p, DanBias::GameClient* c ); + void Gameplay_ObjectPosition ( GameLogic::Protocol_ObjectPosition& p, DanBias::GameClient* c ); + void Gameplay_ObjectEnabled ( GameLogic::Protocol_ObjectEnable& p, DanBias::GameClient* c ); + void Gameplay_ObjectDisabled ( GameLogic::Protocol_ObjectDisable& p, DanBias::GameClient* c ); + void Gameplay_ObjectCreate ( GameLogic::Protocol_ObjectCreate& p, DanBias::GameClient* c ); + void General_Status ( GameLogic::Protocol_General_Status& p, DanBias::GameClient* c ); + void General_Text ( GameLogic::Protocol_General_Text& p, DanBias::GameClient* c ); //Private member variables private: @@ -78,6 +93,7 @@ namespace DanBias bool isRunning; Utility::WinTimer timer; + //Callback method recieving from gamelogic static void ObjectMove(GameLogic::IObjectData* movedObject); diff --git a/Code/Game/GameServer/Implementation/GameLobby.cpp b/Code/Game/GameServer/Implementation/GameLobby.cpp index cd90793a..9396d71d 100644 --- a/Code/Game/GameServer/Implementation/GameLobby.cpp +++ b/Code/Game/GameServer/Implementation/GameLobby.cpp @@ -4,6 +4,7 @@ #include "..\GameLobby.h" #include #include +#include using namespace Utility::DynamicMemory; using namespace Oyster::Network; @@ -24,6 +25,9 @@ namespace DanBias void GameLobby::Update() { + if(GetAsyncKeyState(VK_DOWN)) + this->Send(*GameLogic::Protocol_General_Status().GetProtocol()); + this->ProcessClients(); } GameLobby::operator bool() @@ -40,15 +44,19 @@ namespace DanBias case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToRecieve: break; case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToSend: + printf("\t(%i : %s) - EventType_ProtocolFailedToSend\n", e.sender->GetID(), e.sender->GetIpAddress().c_str()); + e.sender->Disconnect(); break; case NetworkClient::ClientEventArgs::EventType_ProtocolRecieved: + printf("\t(%i : %s) - EventType_ProtocolRecieved\n", e.sender->GetID(), e.sender->GetIpAddress().c_str()); this->ParseProtocol(e.args.data.protocol, e.sender); break; } } void GameLobby::ClientConnectedEvent(Utility::DynamicMemory::SmartPointer client) { - //Attach(client); + printf("New client(%i) connected - %s \n", client->GetID(), client->GetIpAddress().c_str()); + Attach(client); } }//End namespace DanBias \ No newline at end of file diff --git a/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp b/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp index 45ec4dfa..335bf55b 100644 --- a/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp +++ b/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp @@ -9,30 +9,23 @@ void GameLobby::ParseProtocol(Oyster::Network::CustomNetProtocol& p, NetworkClie { switch (p[0].value.netShort) { - //LobbyStartGame(GameLogic::Protocol_LobbyStartGame(p), c); - //LobbyRefresh(GameLogic::Protocol_LobbyRefresh(p), c); - //LobbyLogin(GameLogic::Protocol_LobbyLogin(p), c); - //LobbyJoin(GameLogic::Protocol_LobbyJoin(p), c); - //GeneralStatus(GameLogic::Protocol_General_Status(p), c); - //GeneralText(GameLogic::Protocol_General_Text(p), c); - - case protocol_General_Status: + case protocol_General_Status: this->GeneralStatus (Protocol_General_Status (p), c); break; - case protocol_General_Text: + case protocol_General_Text: this->GeneralText (Protocol_General_Text (p), c); break; - case protocol_Lobby_Create: + case protocol_Lobby_Create: this->LobbyCreateGame (Protocol_LobbyCreateGame (p), c); break; - case protocol_Lobby_Start: + case protocol_Lobby_Start: this->LobbyStartGame (Protocol_LobbyStartGame (p), c); break; - case protocol_Lobby_Join: + case protocol_Lobby_Join: this->LobbyJoin (Protocol_LobbyJoin (p), c); break; - case protocol_Lobby_Login: + case protocol_Lobby_Login: this->LobbyLogin (Protocol_LobbyLogin (p), c); break; - case protocol_Lobby_Refresh: + case protocol_Lobby_Refresh: this->LobbyRefresh (Protocol_LobbyRefresh (p), c); break; - case protocol_Lobby_MainData: + case protocol_Lobby_MainData: this->LobbyMainData (Protocol_LobbyMainData (p), c); break; - case protocol_Lobby_GameData: + case protocol_Lobby_GameData: this->LobbyGameData (Protocol_LobbyGameData (p), c); break; } } @@ -69,7 +62,7 @@ void GameLobby::LobbyStartGame(GameLogic::Protocol_LobbyStartGame& p, Oyster::Ne { } -void GameLobby::LobbyJoin(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c) +void GameLobby::LobbyJoin(GameLogic::Protocol_LobbyJoin& p, Oyster::Network::NetworkClient* c) { //for (unsigned int i = 0; i < this->gameLobby.Size(); i++) //{ @@ -80,7 +73,7 @@ void GameLobby::LobbyJoin(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network:: // } //} } -void GameLobby::LobbyLogin(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c) +void GameLobby::LobbyLogin(GameLogic::Protocol_LobbyLogin& p, Oyster::Network::NetworkClient* c) { } @@ -88,11 +81,11 @@ void GameLobby::LobbyRefresh(GameLogic::Protocol_LobbyRefresh& p, Oyster::Networ { //Dont need to handle this on the server... } -void GameLobby::LobbyMainData(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c) +void GameLobby::LobbyMainData(GameLogic::Protocol_LobbyMainData& p, Oyster::Network::NetworkClient* c) { } -void GameLobby::LobbyGameData(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c) +void GameLobby::LobbyGameData(GameLogic::Protocol_LobbyGameData& p, Oyster::Network::NetworkClient* c) { } diff --git a/Code/Game/GameServer/Implementation/GameServer.cpp b/Code/Game/GameServer/Implementation/GameServer.cpp index ac2abc8a..d98794a4 100644 --- a/Code/Game/GameServer/Implementation/GameServer.cpp +++ b/Code/Game/GameServer/Implementation/GameServer.cpp @@ -41,7 +41,7 @@ DanBiasServerReturn GameServerAPI::Create(const GameInitDesc& desc) return DanBiasServerReturn_Error; } - std::printf("Server created!\t-\t%s: [%i]\n", server.GetLanAddress().c_str(), desc.listenPort); + std::printf("Server created!\t-\t%s: [%i]\n\n", server.GetLanAddress().c_str(), desc.listenPort); return DanBiasServerReturn_Sucess; } @@ -53,8 +53,7 @@ void GameServerAPI::Start() while (true) { - int c = server.ProcessConnectedClients(); - if(c > 0) printf(" - [%i] client(s) connected!\n", c); + server.ProcessConnectedClients(); lobby.Update(); if(GetAsyncKeyState(0x51)) //Q for exit diff --git a/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp b/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp index ef1898be..151cc739 100644 --- a/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp +++ b/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp @@ -45,91 +45,142 @@ namespace DanBias return this->isRunning; } - //void GameSession::ParseEvents() - //{ - // if( !this->box->IsEmpty() ) - // { - // NetworkSession::NetEvent &e = this->box->Fetch(); - // static int ii = 0; - // printf("%i - Message recieved! [%i]\n", ii++, e.protocol[0].value); - // - // 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::ClientEventCallback(NetEvent e) + { + + } + + void GameSession::ObjectMove(GameLogic::IObjectData* movedObject) + { + //GameLogic::IObjectData* obj = NULL; + //if(dynamic_cast(movedObject)) + // obj =((GameLogic::ILevelData*)movedObject)->GetObjectAt(0); + //if(obj) + //{ + // if(obj->GetType() == OBJECT_TYPE_BOX) + // { + // obj->GetID(); + // Oyster::Math::Float4x4 world =obj->GetOrientation(); + // Protocol_ObjectPosition p(world, 1); + // GameSession::gameSession->Send(p.GetProtocol()); + // } + //} + + } + +//*****************************************************// +//****************** Protocol methods *****************// +//******************************************************************************************************************// void GameSession::ParseProtocol(Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c) { switch (p[protocol_INDEX_ID].value.netShort) { - case protocol_Gameplay_PlayerMovement: - { - 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[3].value.netBool) //bool bStrafeLeft; - c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_LEFT); - if(p[4].value.netBool) //bool bStrafeRight; - c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_RIGHT); - } + case protocol_Gameplay_PlayerMovement: this->Gameplay_PlayerMovement ( Protocol_PlayerMovement (p), c ); break; - case protocol_Gameplay_PlayerLookDir: - { - Protocol_PlayerLook m; m = p; - //c->GetPlayer()->Rotate(m.dxMouse, m.dyMouse); - } + case protocol_Gameplay_PlayerLookDir: this->Gameplay_PlayerLookDir ( Protocol_PlayerLook (p), c ); break; - case protocol_Gameplay_PlayerChangeWeapon: - + case protocol_Gameplay_PlayerChangeWeapon: this->Gameplay_PlayerChangeWeapon ( Protocol_PlayerChangeWeapon (p), c ); break; - case protocol_Gameplay_ObjectDamage: - + case protocol_Gameplay_PlayerShot: this->Gameplay_PlayerShot ( Protocol_PlayerShot (p), c ); break; - case protocol_General_Status: - switch (p[1].value.netInt) - { - case GameLogic::Protocol_General_Status::States_disconected: - printf("Client with ID [%i] dissconnected\n", c->GetClient()->GetID()); - this->Detach(c->GetClient()->GetID()); - break; - - case GameLogic::Protocol_General_Status::States_idle: - - break; - - case GameLogic::Protocol_General_Status::States_ready: - - break; - - case GameLogic::Protocol_General_Status::States_leave: - - break; - } + case protocol_Gameplay_ObjectPickup: this->Gameplay_ObjectPickup ( Protocol_ObjectPickup (p), c ); break; - case protocol_General_Text: - { - GameLogic::Protocol_General_Text temp(p); - printf("Message recieved from (%i):\t %s\n", c->GetID(), temp.text.c_str()); - } + case protocol_Gameplay_ObjectDamage: this->Gameplay_ObjectDamage ( Protocol_ObjectDamage (p), c ); + break; + case protocol_Gameplay_ObjectPosition: this->Gameplay_ObjectPosition ( Protocol_ObjectPosition (p), c ); + break; + case protocol_Gameplay_ObjectEnabled: this->Gameplay_ObjectEnabled ( Protocol_ObjectEnable (p), c ); + break; + case protocol_Gameplay_ObjectDisabled: this->Gameplay_ObjectDisabled ( Protocol_ObjectDisable (p), c ); + break; + case protocol_Gameplay_ObjectCreate: this->Gameplay_ObjectCreate ( Protocol_ObjectCreate (p), c ); + break; + case protocol_General_Status: this->General_Status ( Protocol_General_Status (p), c ); + break; + case protocol_General_Text: this->General_Text ( Protocol_General_Text (p), c ); break; } } - void GameSession::ObjectMove(GameLogic::IObjectData* movedObject) + void GameSession::Gameplay_PlayerMovement ( Protocol_PlayerMovement& p, DanBias::GameClient* c ) { - movedObject->GetID(); - movedObject->GetOrientation(); + if(p.bForward) c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_FORWARD); + if(p.bBackward) c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_BACKWARD); + if(p.bLeft) c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_LEFT); + if(p.bRight) c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_RIGHT); } - - void GameSession::ClientEventCallback(NetEvent e) + void GameSession::Gameplay_PlayerLookDir ( Protocol_PlayerLook& p, DanBias::GameClient* c ) + { + Oyster::Math3D::Float3 lookDir; + lookDir.x = p.lookDirX; + lookDir.y = p.lookDirY; + lookDir.z = p.lookDirZ; + c->GetPlayer()->Rotate(lookDir); + } + void GameSession::Gameplay_PlayerChangeWeapon ( Protocol_PlayerChangeWeapon& p, DanBias::GameClient* c ) { } + void GameSession::Gameplay_PlayerShot ( Protocol_PlayerShot& p, DanBias::GameClient* c ) + { + c->GetPlayer()->UseWeapon(GameLogic::WEAPON_USE_PRIMARY_PRESS); + } + void GameSession::Gameplay_ObjectPickup ( Protocol_ObjectPickup& p, DanBias::GameClient* c ) + { + + } + void GameSession::Gameplay_ObjectDamage ( Protocol_ObjectDamage& p, DanBias::GameClient* c ) + { + + } + void GameSession::Gameplay_ObjectPosition ( Protocol_ObjectPosition& p, DanBias::GameClient* c ) + { + + } + void GameSession::Gameplay_ObjectEnabled ( Protocol_ObjectEnable& p, DanBias::GameClient* c ) + { + + } + void GameSession::Gameplay_ObjectDisabled ( Protocol_ObjectDisable& p, DanBias::GameClient* c ) + { + + } + void GameSession::Gameplay_ObjectCreate ( Protocol_ObjectCreate& p, DanBias::GameClient* c ) + { + + } + void GameSession::General_Status ( Protocol_General_Status& p, DanBias::GameClient* c ) + { + switch (p.status) + { + case GameLogic::Protocol_General_Status::States_disconected: + printf("Client with ID [%i] dissconnected\n", c->GetClient()->GetID()); + this->Detach(c->GetClient()->GetID()); + break; + + case GameLogic::Protocol_General_Status::States_idle: + + break; + + case GameLogic::Protocol_General_Status::States_ready: + + break; + + case GameLogic::Protocol_General_Status::States_leave: + + break; + } + } + void GameSession::General_Text ( Protocol_General_Text& p, DanBias::GameClient* c ) + { + printf("Message recieved from (%i):\t %s\n", c->GetID(), p.text.c_str()); + } + }//End namespace DanBias + + + + + diff --git a/Code/Network/NetworkAPI/NetworkClient.cpp b/Code/Network/NetworkAPI/NetworkClient.cpp index f5d7c643..9539cdeb 100644 --- a/Code/Network/NetworkAPI/NetworkClient.cpp +++ b/Code/Network/NetworkAPI/NetworkClient.cpp @@ -171,7 +171,7 @@ void NetworkClient::Update() } } -bool NetworkClient::Connect(int socket) +bool NetworkClient::Connect(ConnectionInfo& socket) { if(this->IsConnected()) return true; if(this->privateData) return false; @@ -250,4 +250,7 @@ void NetworkClient::DataRecieved(NetEvent e) void NetworkClient::NetworkCallback(Oyster::Network::CustomNetProtocol& p) {} - +std::string NetworkClient::GetIpAddress() +{ + return this->privateData->connection.GetIpAddress(); +} diff --git a/Code/Network/NetworkAPI/NetworkClient.h b/Code/Network/NetworkAPI/NetworkClient.h index 869a5100..dfe247de 100644 --- a/Code/Network/NetworkAPI/NetworkClient.h +++ b/Code/Network/NetworkAPI/NetworkClient.h @@ -15,6 +15,7 @@ namespace Oyster namespace Network { class NetworkSession; + struct ConnectionInfo; extern "C" { @@ -54,7 +55,7 @@ namespace Oyster /** * */ - bool Connect(int socket); + bool Connect(ConnectionInfo& data); /** * @@ -102,6 +103,8 @@ namespace Oyster */ virtual void NetworkCallback(Oyster::Network::CustomNetProtocol& p); + virtual std::string GetIpAddress(); + private: NetworkClient(const NetworkClient& obj); NetworkClient& operator =(const NetworkClient& obj); diff --git a/Code/Network/NetworkAPI/NetworkServer.cpp b/Code/Network/NetworkAPI/NetworkServer.cpp index 9d6db207..9a4195a2 100644 --- a/Code/Network/NetworkAPI/NetworkServer.cpp +++ b/Code/Network/NetworkAPI/NetworkServer.cpp @@ -77,7 +77,7 @@ public: public: Listener* listener; - PostBox postBox; //Postbox for new clients + PostBox postBox; //Postbox for new clients OysterThread thread; //Server thread NetworkSession *mainSession; Utility::Container::ThreadSafeQueue> clientQueue; @@ -95,9 +95,9 @@ bool NetworkServer::PrivateData::DoWork() /** Check for new clients **/ if(postBox.IsFull()) { - int clientSocketNum = postBox.FetchMessage(); - - if(clientSocketNum == -1) + ConnectionInfo clientSocketNum = postBox.FetchMessage(); + + if(clientSocketNum.socket == -1) { //Something went wrong somewhere... do we care? } diff --git a/Code/Network/NetworkDependencies/Connection.cpp b/Code/Network/NetworkDependencies/Connection.cpp index 27aa296a..99cb8a71 100644 --- a/Code/Network/NetworkDependencies/Connection.cpp +++ b/Code/Network/NetworkDependencies/Connection.cpp @@ -1,6 +1,7 @@ #include "Connection.h" #include +#include #include #include #include @@ -40,11 +41,13 @@ Connection::~Connection() CloseSocket( this->socket ); } -int Connection::Connect(int socket, bool blocking) +int Connection::Connect(ConnectionInfo info, bool blocking) { - this->socket = socket; + this->addr = info.addr; + this->socket = info.socket; this->stillSending = true; this->closed = false; + SetBlockingMode(blocking); //connection succesfull! @@ -167,17 +170,24 @@ int Connection::Recieve(OysterByte &bytes) } //Listen will only return the correct socket or -1 for failure. -int Connection::Listen() +ConnectionInfo Connection::Listen() { - if(this->closed) return -1; + ConnectionInfo val = { 0 }; + if(this->closed) return val; - int clientSocket; - if((clientSocket = (int)accept(this->socket, NULL, NULL)) == INVALID_SOCKET) + SOCKADDR_IN client_info = { 0 }; + int addrsize = sizeof(client_info); + + if((val.socket = (int)accept(this->socket, (struct sockaddr*)&client_info, &addrsize)) == INVALID_SOCKET) { - return (int)INVALID_SOCKET;//WSAGetLastError(); + val.socket = WSAGetLastError(); + } + else + { + val.addr = inet_ntoa(client_info.sin_addr); } - return clientSocket; + return val; } bool Connection::IsSending() @@ -213,6 +223,12 @@ int Connection::SetBlockingMode(bool blocking) return 0; } + +std::string Connection::GetIpAddress() +{ + return this->addr; +} + /////////////////////////////////////// //Private functions /////////////////////////////////////// diff --git a/Code/Network/NetworkDependencies/Connection.h b/Code/Network/NetworkDependencies/Connection.h index d2e815d3..0f46a599 100644 --- a/Code/Network/NetworkDependencies/Connection.h +++ b/Code/Network/NetworkDependencies/Connection.h @@ -14,7 +14,6 @@ namespace Oyster { class Connection : public IConnection { - public: Connection(); Connection( int socket ); @@ -27,10 +26,10 @@ namespace Oyster virtual int Recieve( OysterByte &bytes ); virtual int Disconnect(); - virtual int Connect(int socket, bool blocking = false); + virtual int Connect(ConnectionInfo info, bool blocking = false); virtual int Connect(unsigned short port , const char serverName[], bool blocking = false); - virtual int Listen(); + virtual ConnectionInfo Listen(); bool IsSending(); bool IsConnected(); @@ -38,6 +37,8 @@ namespace Oyster //Setting the socket to blocking/non-blocking mode. int SetBlockingMode( bool blocking ); + std::string GetIpAddress(); + private: int InitiateSocket(); @@ -45,7 +46,7 @@ namespace Oyster bool stillSending; bool closed; - + std::string addr; }; } } diff --git a/Code/Network/NetworkDependencies/IConnection.h b/Code/Network/NetworkDependencies/IConnection.h index 76736071..bd59de65 100644 --- a/Code/Network/NetworkDependencies/IConnection.h +++ b/Code/Network/NetworkDependencies/IConnection.h @@ -11,6 +11,12 @@ namespace Oyster { namespace Network { + struct ConnectionInfo + { + int socket; + std::string addr; + }; + class OysterByte; class IConnection { @@ -27,7 +33,7 @@ namespace Oyster virtual int InitiateClient() { return false; }; //Listen function to let client connect, only used by the server - virtual int Listen() { return -1; }; + virtual ConnectionInfo Listen() { return ConnectionInfo(); }; //enables the client to connect with a server with use of name and port //(servers uses Listen instead of connect) diff --git a/Code/Network/NetworkDependencies/Listener.cpp b/Code/Network/NetworkDependencies/Listener.cpp index 106ac3b9..d4ee396a 100644 --- a/Code/Network/NetworkDependencies/Listener.cpp +++ b/Code/Network/NetworkDependencies/Listener.cpp @@ -11,7 +11,7 @@ Listener::Listener() connection = NULL; } -Listener::Listener(Oyster::Network::IPostBox* postBox) +Listener::Listener(Oyster::Network::IPostBox* postBox) { this->isListening = false; connection = NULL; @@ -88,7 +88,7 @@ void Listener::Shutdown() StopListen(); } -void Listener::SetPostBox(Oyster::Network::IPostBox* postBox) +void Listener::SetPostBox(Oyster::Network::IPostBox* postBox) { stdMutex.lock(); this->postBox = postBox; @@ -97,21 +97,21 @@ void Listener::SetPostBox(Oyster::Network::IPostBox* postBox) int Listener::Accept() { - int clientSocket = -1; + ConnectionInfo clientSocket = {0}; clientSocket = connection->Listen(); if(!this->isListening.load()) { return -1; } - if(clientSocket != -1) + if(clientSocket.socket != -1) { stdMutex.lock(); postBox->PostMessage(clientSocket); stdMutex.unlock(); } - return clientSocket; + return clientSocket.socket; } void Listener::StopListen() { diff --git a/Code/Network/NetworkDependencies/Listener.h b/Code/Network/NetworkDependencies/Listener.h index 0c55f86e..49449990 100644 --- a/Code/Network/NetworkDependencies/Listener.h +++ b/Code/Network/NetworkDependencies/Listener.h @@ -20,7 +20,7 @@ namespace Oyster { public: Listener(); - Listener(Oyster::Network::IPostBox* postBox); + Listener(Oyster::Network::IPostBox* postBox); ~Listener(); bool Init(unsigned int port); @@ -29,7 +29,7 @@ namespace Oyster void Stop(); void Shutdown(); - void SetPostBox(IPostBox* postBox); + void SetPostBox(IPostBox* postBox); private: //Thread functions @@ -48,7 +48,7 @@ namespace Oyster OysterMutex mutex; std::mutex stdMutex; - IPostBox* postBox; + IPostBox* postBox; std::atomic isListening; int port; };