From 9d722bced3f0dae9c19c5004dacbdc0f4c6948e2 Mon Sep 17 00:00:00 2001 From: Linda Andersson Date: Tue, 17 Dec 2013 13:39:10 +0100 Subject: [PATCH] GL - recieve messages from server --- Code/Game/DanBiasGame/DanBiasGame_Impl.cpp | 55 +++++++++++++------ .../GameClientState/GameClientState.h | 4 ++ .../DanBiasGame/GameClientState/GameState.cpp | 27 +++++++-- .../DanBiasGame/GameClientState/GameState.h | 1 + Code/Game/DanBiasLauncher/Launcher.cpp | 4 +- Code/Network/NetworkAPI/NetworkClient.cpp | 10 ++-- Code/Network/NetworkAPI/Translator.cpp | 5 +- Code/Network/NetworkDependencies/Packing.cpp | 2 +- 8 files changed, 77 insertions(+), 31 deletions(-) diff --git a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp index ebd2f715..a6249dc9 100644 --- a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp +++ b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp @@ -31,38 +31,55 @@ namespace DanBias { int pType = p[0].value.netInt; - Client::GameClientState::ProtocolStruct* protocolData; + //Client::GameClientState::ProtocolStruct* protocolData; switch (pType) { case protocol_Gamplay_PlayerNavigation: + { + + Client::GameClientState::KeyInput* protocolData = new Client::GameClientState::KeyInput; + for(int i = 0; i< 6; i++) + { + protocolData->key[i] = p[i+1].value.netBool; + } + + ((Client::GameState*)gameClientState)->Protocol(protocolData); + delete protocolData; + protocolData = NULL; + } break; case protocol_Gamplay_PlayerPosition: - protocolData = new Client::GameClientState::PlayerPos; - for(int i = 0; i< 3; i++) { - ((Client::GameClientState::PlayerPos*)protocolData)->playerPos[i] = p[i].value.netFloat; + Client::GameClientState::PlayerPos* protocolData = new Client::GameClientState::PlayerPos; + for(int i = 0; i< 3; i++) + { + protocolData->playerPos[i] = p[i].value.netFloat; + } + //if(dynamic_cast(gameClientState)) + gameClientState->Protocol(protocolData); + delete protocolData; + protocolData = NULL; } - if(dynamic_cast(gameClientState)) - ((Client::GameState*)gameClientState)->Protocol(protocolData); - delete protocolData; - protocolData = NULL; break; case protocol_Gamplay_ObjectPosition: - protocolData = new Client::GameClientState::ObjPos; - ((Client::GameClientState::ObjPos*)protocolData)->object_ID = p[1].value.netInt; - for(int i = 0; i< 16; i++) { - ((Client::GameClientState::ObjPos*)protocolData)->worldPos[i] = p[i+2].value.netFloat; - } - if(dynamic_cast(gameClientState)) - ((Client::GameState*)gameClientState)->Protocol(protocolData); + Client::GameClientState::ObjPos* protocolData = new Client::GameClientState::ObjPos; + protocolData->object_ID = p[1].value.netInt; + for(int i = 0; i< 16; i++) + { + protocolData->worldPos[i] = p[i+2].value.netFloat; + } + + + gameClientState->Protocol(protocolData); - delete protocolData; - protocolData = NULL; + delete protocolData; + protocolData = NULL; + } break; default: @@ -116,8 +133,10 @@ namespace DanBias QueryPerformanceCounter((LARGE_INTEGER*)&prevTimeStamp); m_data->recieverObj = new MyRecieverObject; - m_data->recieverObj->nwClient = new Oyster::Network::NetworkClient(); + + 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()) { // failed to connect diff --git a/Code/Game/DanBiasGame/GameClientState/GameClientState.h b/Code/Game/DanBiasGame/GameClientState/GameClientState.h index 1082125a..e9cda411 100644 --- a/Code/Game/DanBiasGame/GameClientState/GameClientState.h +++ b/Code/Game/DanBiasGame/GameClientState/GameClientState.h @@ -22,6 +22,10 @@ public: int object_ID; float worldPos[16]; }; + struct KeyInput :public ProtocolStruct + { + bool key[6]; + }; struct PlayerPos :public ProtocolStruct { float playerPos[3]; diff --git a/Code/Game/DanBiasGame/GameClientState/GameState.cpp b/Code/Game/DanBiasGame/GameClientState/GameState.cpp index bbcd36a4..40f46156 100644 --- a/Code/Game/DanBiasGame/GameClientState/GameState.cpp +++ b/Code/Game/DanBiasGame/GameClientState/GameState.cpp @@ -92,6 +92,7 @@ GameClientState::ClientState GameState::Update(float deltaTime, InputClass* KeyI // read server data // update objects { + bool send = false; GameLogic::Protocol_PlayerMovement movePlayer; movePlayer.bForward = false; movePlayer.bBackward = false; @@ -104,21 +105,25 @@ GameClientState::ClientState GameState::Update(float deltaTime, InputClass* KeyI if(KeyInput->IsKeyPressed(DIK_W)) { movePlayer.bForward = true; + send = true; } if(KeyInput->IsKeyPressed(DIK_S)) { movePlayer.bBackward = true; + send = true; } if(KeyInput->IsKeyPressed(DIK_A)) { movePlayer.bStrafeLeft = true; + send = true; } if(KeyInput->IsKeyPressed(DIK_D)) { movePlayer.bStrafeRight = true; + send = true; } - if (privData->nwClient->IsConnected()) + if (privData->nwClient->IsConnected() && send) { privData->nwClient->Send(movePlayer); } @@ -167,13 +172,18 @@ bool GameState::Release() void GameState::Protocol(ProtocolStruct* pos) { + // move message + /* + if ((KeyInput*)pos) + { + } if((ObjPos*)pos) ObjectPosProtocol((ObjPos*)pos); else if((PlayerPos*)pos) - PlayerPosProtocol((PlayerPos*)pos); + PlayerPosProtocol((PlayerPos*)pos);*/ } -void DanBias::Client::GameState::Protocol( PlayerPos* pos ) +void GameState::Protocol( PlayerPos* pos ) { Oyster::Math::Float4x4 world, translate; @@ -184,7 +194,7 @@ void DanBias::Client::GameState::Protocol( PlayerPos* pos ) privData->object[0]->setPos( world ); } -void DanBias::Client::GameState::Protocol( ObjPos* pos ) +void GameState::Protocol( ObjPos* pos ) { Oyster::Math::Float4x4 world; for(int i = 0; i<16; i++) @@ -194,6 +204,15 @@ void DanBias::Client::GameState::Protocol( ObjPos* pos ) privData->object[pos->object_ID]->setPos(world); } +void GameState::Protocol( KeyInput* pos ) +{ + bool key = false; + for (int i = 0; i < 6; i++) + { + key = pos->key[i]; + } +} + void GameState::PlayerPosProtocol(PlayerPos* pos) { Oyster::Math::Float4x4 world, translate; diff --git a/Code/Game/DanBiasGame/GameClientState/GameState.h b/Code/Game/DanBiasGame/GameClientState/GameState.h index 5e185cef..5ef9623d 100644 --- a/Code/Game/DanBiasGame/GameClientState/GameState.h +++ b/Code/Game/DanBiasGame/GameClientState/GameState.h @@ -34,6 +34,7 @@ public: void Protocol(ProtocolStruct* pos)override; void Protocol(PlayerPos* pos); void Protocol(ObjPos* pos); + void Protocol(KeyInput* pos); void PlayerPosProtocol(PlayerPos* pos); void ObjectPosProtocol(ObjPos* pos); //void Protocol(LightPos pos); diff --git a/Code/Game/DanBiasLauncher/Launcher.cpp b/Code/Game/DanBiasLauncher/Launcher.cpp index 2c3873cb..a8a39eee 100644 --- a/Code/Game/DanBiasLauncher/Launcher.cpp +++ b/Code/Game/DanBiasLauncher/Launcher.cpp @@ -5,8 +5,8 @@ #include #include -#include "DanBiasServerAPI.h" -//#include "DanBiasGame.h" +//#include "DanBiasServerAPI.h" +#include "DanBiasGame.h" int WINAPI WinMain( HINSTANCE hinst, HINSTANCE prevInst, PSTR cmdLine, int cmdShow) diff --git a/Code/Network/NetworkAPI/NetworkClient.cpp b/Code/Network/NetworkAPI/NetworkClient.cpp index 69b22380..5813131d 100644 --- a/Code/Network/NetworkAPI/NetworkClient.cpp +++ b/Code/Network/NetworkAPI/NetworkClient.cpp @@ -50,6 +50,7 @@ struct ClientDataContainer sendPostBox = new PostBox(); connection.InitiateClient(); connection.SetBlockingMode(false); + } ClientDataContainer(IThreadObject* o, unsigned int socket ) :connection(socket), ID(currID++) @@ -170,13 +171,14 @@ NetworkClient::NetworkClient(unsigned int socket) NetworkClient::NetworkClient(RecieverObject recvObj, NetworkProtocolCallbackType type) { privateData = new PrivateData(); - this->privateData->data->recvObj = SmartPointer(&recvObj);; + this->privateData->data->callbackType = type; + this->privateData->data->recvObj = recvObj; } NetworkClient::NetworkClient(RecieverObject recvObj, NetworkProtocolCallbackType type, unsigned int socket) { privateData = new PrivateData(socket); - this->privateData->data->recvObj = SmartPointer(&recvObj); + this->privateData->data->recvObj = recvObj; this->privateData->data->callbackType = type; this->privateData->data->thread.Create(this->privateData, true); } @@ -213,11 +215,11 @@ bool NetworkClient::Connect(unsigned short port, const char serverIP[]) if(this->privateData->data->thread.IsCreated()) return false; this->privateData->data->thread.Create(this->privateData, true); - + privateData->data->connection.SetBlockingMode(false); return true; } - privateData->data->connection.SetBlockingMode(false); + //Connect has failed return false; diff --git a/Code/Network/NetworkAPI/Translator.cpp b/Code/Network/NetworkAPI/Translator.cpp index e321f31e..59ecdfd2 100644 --- a/Code/Network/NetworkAPI/Translator.cpp +++ b/Code/Network/NetworkAPI/Translator.cpp @@ -44,7 +44,7 @@ struct Translator::PrivateData headerString.push_back(it->second.type); } - message.PackShort(size, bytes); + message.PackShort(headerString.size(), bytes); size += 2; for(int i = 0; i < (int)headerString.size(); i++) @@ -217,6 +217,7 @@ void Translator::Pack(OysterByte &bytes, CustomNetProtocol& protocol) privateData->PackHeader(bytes, protocol); privateData->PackMessage(bytes, protocol); + this->privateData->headerString.clear(); } bool Translator::Unpack(CustomNetProtocol& protocol, OysterByte &bytes) @@ -227,6 +228,6 @@ bool Translator::Unpack(CustomNetProtocol& protocol, OysterByte &bytes) } privateData->UnpackMessage(protocol, bytes); - + this->privateData->headerString.clear(); return true; } \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Packing.cpp b/Code/Network/NetworkDependencies/Packing.cpp index b6903a0a..5728cabe 100644 --- a/Code/Network/NetworkDependencies/Packing.cpp +++ b/Code/Network/NetworkDependencies/Packing.cpp @@ -169,7 +169,7 @@ namespace Oyster //bool (1-bit) bool Unpackb(unsigned char buffer[]) { - return buffer; + return *buffer; } //char (8-bit)