From 4167b6a93b431c5fbfab4172d9e46e7116e547cc Mon Sep 17 00:00:00 2001 From: Sam Mario Svensson Date: Thu, 30 Jan 2014 15:02:02 +0100 Subject: [PATCH 1/4] GL- merge fix --- Code/Game/aDanBiasGameLauncher/Launcher.cpp | 2 +- Code/Network/NetworkAPI/NetworkSession.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Code/Game/aDanBiasGameLauncher/Launcher.cpp b/Code/Game/aDanBiasGameLauncher/Launcher.cpp index e13baff1..f05f021d 100644 --- a/Code/Game/aDanBiasGameLauncher/Launcher.cpp +++ b/Code/Game/aDanBiasGameLauncher/Launcher.cpp @@ -57,7 +57,7 @@ int WINAPI WinMain( HINSTANCE hinst, HINSTANCE prevInst, PSTR cmdLine, int cmdSh return cmdShow; } - serverThread = std::thread(ServerFnc); + //serverThread = std::thread(ServerFnc); Sleep(200); diff --git a/Code/Network/NetworkAPI/NetworkSession.h b/Code/Network/NetworkAPI/NetworkSession.h index b44f5c61..2ef1130c 100644 --- a/Code/Network/NetworkAPI/NetworkSession.h +++ b/Code/Network/NetworkAPI/NetworkSession.h @@ -13,7 +13,7 @@ #include "NetworkAPI_Preprocessor.h" #include "NetworkServerEventStruct.h" #include "NetworkClient.h" -#include "Utilities.h" +#include "..\Misc\Utilities.h" #include "DynamicArray.h" namespace Oyster From 0aa39275e38f67f34262d0410c8a197cfa7deb29 Mon Sep 17 00:00:00 2001 From: lindaandersson Date: Thu, 30 Jan 2014 15:20:35 +0100 Subject: [PATCH 2/4] GL - only client with server update server. Update recieverfunction --- Code/Game/DanBiasGame/DanBiasGame_Impl.cpp | 16 ++++++++++------ .../GameClientState/GameClientState.h | 1 + .../DanBiasGame/GameClientState/GameState.cpp | 1 - .../DanBiasGame/GameClientState/LobbyState.cpp | 2 +- .../DanBiasGame/GameClientState/LoginState.cpp | 2 +- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp index 64526c0b..de790e8a 100644 --- a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp +++ b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp @@ -8,6 +8,7 @@ #include "GameClientState\LoginState.h" #include #include "NetworkClient.h" +#include #include "../WindowManager/WindowShell.h" #include "L_inputClass.h" @@ -38,6 +39,7 @@ namespace DanBias InputClass* inputObj; Utility::WinTimer* timer; GameRecieverObject* recieverObj; + bool serverOwner; } data; #pragma endregion @@ -62,13 +64,8 @@ namespace DanBias return DanBiasClientReturn_Error; m_data->recieverObj = new GameRecieverObject; - /*m_data->recieverObj->Connect(desc.port, desc.IP); + m_data->serverOwner = false; - if (!m_data->recieverObj->IsConnected()) - { - // failed to connect - return DanBiasClientReturn_Error; - }*/ // Start in lobby state m_data->recieverObj->gameClientState = new Client::LoginState(); if(!m_data->recieverObj->gameClientState->Init(m_data->recieverObj)) @@ -133,8 +130,13 @@ namespace DanBias HRESULT DanBiasGame::Update(float deltaTime) { + m_data->recieverObj->Update(); m_data->inputObj->Update(); + if(m_data->serverOwner) + { + DanBias::GameServerAPI::ServerUpdate(); + } DanBias::Client::GameClientState::ClientState state = DanBias::Client::GameClientState::ClientState_Same; state = m_data->recieverObj->gameClientState->Update(deltaTime, m_data->inputObj); @@ -147,6 +149,8 @@ namespace DanBias switch (state) { + case Client::GameClientState::ClientState_LobbyCreated: + m_data->serverOwner = true; case Client::GameClientState::ClientState_Lobby: m_data->recieverObj->gameClientState = new Client::LobbyState(); break; diff --git a/Code/Game/DanBiasGame/GameClientState/GameClientState.h b/Code/Game/DanBiasGame/GameClientState/GameClientState.h index 28a671ba..a369233a 100644 --- a/Code/Game/DanBiasGame/GameClientState/GameClientState.h +++ b/Code/Game/DanBiasGame/GameClientState/GameClientState.h @@ -53,6 +53,7 @@ public: { ClientState_Login, ClientState_Lobby, + ClientState_LobbyCreated, ClientState_Game, ClientState_Same, }; diff --git a/Code/Game/DanBiasGame/GameClientState/GameState.cpp b/Code/Game/DanBiasGame/GameClientState/GameState.cpp index 2e04959d..ee069919 100644 --- a/Code/Game/DanBiasGame/GameClientState/GameState.cpp +++ b/Code/Game/DanBiasGame/GameClientState/GameState.cpp @@ -144,7 +144,6 @@ bool GameState::InitCamera(Oyster::Math::Float3 startPos) GameClientState::ClientState GameState::Update(float deltaTime, InputClass* KeyInput) { - DanBias::GameServerAPI::ServerUpdate(); switch (privData->state) { case gameStateState_loading: diff --git a/Code/Game/DanBiasGame/GameClientState/LobbyState.cpp b/Code/Game/DanBiasGame/GameClientState/LobbyState.cpp index 260bf1be..938add5f 100644 --- a/Code/Game/DanBiasGame/GameClientState/LobbyState.cpp +++ b/Code/Game/DanBiasGame/GameClientState/LobbyState.cpp @@ -87,7 +87,7 @@ GameClientState::ClientState LobbyState::Update(float deltaTime, InputClass* Key // update animation // send data to server // check data from server - DanBias::GameServerAPI::ServerUpdate(); + if( KeyInput->IsKeyPressed(DIK_G)) { diff --git a/Code/Game/DanBiasGame/GameClientState/LoginState.cpp b/Code/Game/DanBiasGame/GameClientState/LoginState.cpp index 8fa4936d..cd1e4663 100644 --- a/Code/Game/DanBiasGame/GameClientState/LoginState.cpp +++ b/Code/Game/DanBiasGame/GameClientState/LoginState.cpp @@ -103,7 +103,7 @@ GameClientState::ClientState LoginState::Update(float deltaTime, InputClass* Key // failed to connect return ClientState_Same; } - return ClientState_Lobby; + return ClientState_LobbyCreated; } // join game if( KeyInput->IsKeyPressed(DIK_J)) From 3b8a8e9c099815977f6772b133d3f1dedc949c58 Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Thu, 30 Jan 2014 23:22:46 +0100 Subject: [PATCH 3/4] Network - Fixed receiving parts of packages. --- Code/Network/NetworkAPI/NetworkClient.cpp | 89 ++++++++++++++++++- .../NetworkDependencies/OysterByte.cpp | 30 ++++++- Code/Network/NetworkDependencies/OysterByte.h | 5 +- 3 files changed, 117 insertions(+), 7 deletions(-) diff --git a/Code/Network/NetworkAPI/NetworkClient.cpp b/Code/Network/NetworkAPI/NetworkClient.cpp index 9539cdeb..fa9ad1af 100644 --- a/Code/Network/NetworkAPI/NetworkClient.cpp +++ b/Code/Network/NetworkAPI/NetworkClient.cpp @@ -16,11 +16,15 @@ #include "../../Misc/Utilities.h" #include "../../Misc/Thread/IThreadObject.h" #include "../../Misc/Thread/OysterThread.h" +#include "../../Misc/Packing/Packing.h" + +#include using namespace Oyster::Network; using namespace Oyster::Thread; using namespace Utility::DynamicMemory; using namespace Utility::Container; +using namespace std; /************************************* PrivateData @@ -35,11 +39,12 @@ struct NetworkClient::PrivateData : public IThreadObject Translator translator; OysterThread thread; + OysterByte recieveBuffer; //Message queue for sending and recieving ThreadSafeQueue sendQueue; ThreadSafeQueue> recieveQueue; - + //ID static unsigned int currID; const unsigned int ID; @@ -66,7 +71,7 @@ struct NetworkClient::PrivateData : public IThreadObject bool DoWork() override { if(!this->connection.IsConnected()) return false; - + Send(); Recv(); @@ -82,6 +87,7 @@ struct NetworkClient::PrivateData : public IThreadObject CustomNetProtocol p = this->sendQueue.Pop(); this->translator.Pack(temp, p); errorCode = this->connection.Send(temp); + if(errorCode != 0) { CEA parg; @@ -103,9 +109,13 @@ struct NetworkClient::PrivateData : public IThreadObject if(errorCode == 0 && temp.GetSize()) { + HandleRecievedData(temp); + + + /* Replaced with EmptyOutbufferedQueue() and HandleRecievedData(OysterByte) CustomNetProtocol protocol; bool ok = this->translator.Unpack(protocol, temp); - + //Check if the protocol was unpacked correctly if(ok) { @@ -114,7 +124,8 @@ struct NetworkClient::PrivateData : public IThreadObject parg.data.protocol = protocol; NetEvent e = { this->parent, parg }; this->recieveQueue.Push(e); - } + }*/ + } //else //{ @@ -127,6 +138,76 @@ struct NetworkClient::PrivateData : public IThreadObject return errorCode; } + + void HandleRecievedData(OysterByte& data) + { + //Loop through all packages that was recieved and add them to the queue. + unsigned int size = 0; + + Oyster::Network::OysterByte msg; + + //If there is part of a message in the buffer. + if(recieveBuffer.GetSize() > 0) + { + //cout << "the buffer size: " << recvBuffer.GetSize() < size) + { + msg = recieveBuffer; + recieveBuffer.Clear(); + size -= msg.GetSize(); + msg.AppendPartOfArray(data, 0, size); + UnpackMessageAndAddToQueue(msg); + } + else if(temp + data.GetSize() == size) + { + msg = recieveBuffer; + recieveBuffer.Clear(); + size -= msg.GetSize(); + msg += data; + UnpackMessageAndAddToQueue(msg); + return; + } + else + { + recieveBuffer += data; + size = data.GetSize(); + return; + } + } + + for(unsigned int i = size; i < data.GetSize(); i += size) + { + size = Oyster::Packing::Unpacki(&data.GetByteArray()[i]); + if(i+size > data.GetSize()) + { + //Add it to the recvBuffer instead. + recieveBuffer.AppendPartOfArray(data, i, data.GetSize()); + break; + } + msg.Clear(); + msg.AppendPartOfArray(data, i, i+size); + UnpackMessageAndAddToQueue(msg); + } + } + + void UnpackMessageAndAddToQueue(OysterByte& msg) + { + CustomNetProtocol protocol; + bool ok = this->translator.Unpack(protocol, msg); + + //Check if the protocol was unpacked correctly + if(ok) + { + CEA parg; + parg.type = CEA::EventType_ProtocolRecieved; + parg.data.protocol = protocol; + NetEvent e = { this->parent, parg }; + this->recieveQueue.Push(e); + } + } }; unsigned int NetworkClient::PrivateData::currID = 0; diff --git a/Code/Network/NetworkDependencies/OysterByte.cpp b/Code/Network/NetworkDependencies/OysterByte.cpp index ac6923a2..7f64a30d 100644 --- a/Code/Network/NetworkDependencies/OysterByte.cpp +++ b/Code/Network/NetworkDependencies/OysterByte.cpp @@ -48,7 +48,7 @@ void OysterByte::Resize(unsigned int cap) } } -int OysterByte::GetSize() +unsigned int OysterByte::GetSize() { return size; } @@ -60,7 +60,7 @@ unsigned char* OysterByte::GetByteArray() void OysterByte::AddSize(unsigned int size) { - int newCapacity = this->size + size; + unsigned int newCapacity = this->size + size; if(newCapacity >= capacity) { @@ -81,6 +81,32 @@ void OysterByte::SetSize(unsigned int size) this->size = size; } +void OysterByte::AppendPartOfArray(OysterByte& source, unsigned int startIndex, unsigned int endIndex) +{ + if(startIndex < 0 && startIndex >= endIndex) + return; + if(endIndex > source.size) + return; + + unsigned int totalSize = endIndex - startIndex; + totalSize += size; + + //Make sure the new data can fit in the array. + if(totalSize > capacity) + { + IncreaseCapacity(totalSize); + } + + //Copy over new data. + for(unsigned int i = size; i < totalSize; i++) + { + byteArray[i] = source.byteArray[startIndex++]; + } + + //Set the new size + size = totalSize; +} + OysterByte& OysterByte::operator =(const OysterByte& obj) { delete[] this->byteArray; diff --git a/Code/Network/NetworkDependencies/OysterByte.h b/Code/Network/NetworkDependencies/OysterByte.h index 4f6f4557..a3c0dfaf 100644 --- a/Code/Network/NetworkDependencies/OysterByte.h +++ b/Code/Network/NetworkDependencies/OysterByte.h @@ -25,7 +25,7 @@ namespace Oyster //Resizes the array with, it does not keep anything in it. void Resize(unsigned int cap); - int GetSize(); + unsigned int GetSize(); unsigned char* GetByteArray(); void AddSize(unsigned int size); @@ -34,6 +34,9 @@ namespace Oyster //Only sets the private variable 'size' void SetSize(unsigned int size); + //Copies over a part of the addFrom array and adds it to the end of this array. + void AppendPartOfArray(OysterByte& source, unsigned int startIndex, unsigned int endIndex); + OysterByte& operator =(const OysterByte& obj); operator char*(); From 3c1eed0f3f51d1e685c48e087f48900459b7fc87 Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Thu, 30 Jan 2014 23:23:37 +0100 Subject: [PATCH 4/4] GL - Added LanMenuState --- Code/Game/DanBiasGame/DanBiasGame.vcxproj | 2 + Code/Game/DanBiasGame/DanBiasGame_Impl.cpp | 1 + .../GameClientState/LanMenuState.cpp | 164 ++++++++++++++++++ .../GameClientState/LanMenuState.h | 37 ++++ 4 files changed, 204 insertions(+) create mode 100644 Code/Game/DanBiasGame/GameClientState/LanMenuState.cpp create mode 100644 Code/Game/DanBiasGame/GameClientState/LanMenuState.h diff --git a/Code/Game/DanBiasGame/DanBiasGame.vcxproj b/Code/Game/DanBiasGame/DanBiasGame.vcxproj index f1cbf970..01a92481 100644 --- a/Code/Game/DanBiasGame/DanBiasGame.vcxproj +++ b/Code/Game/DanBiasGame/DanBiasGame.vcxproj @@ -204,6 +204,7 @@ + @@ -217,6 +218,7 @@ + diff --git a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp index 64526c0b..522470cc 100644 --- a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp +++ b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp @@ -6,6 +6,7 @@ #include "GameClientState\GameState.h" #include "GameClientState\LobbyState.h" #include "GameClientState\LoginState.h" +#include "GameClientState\LanMenuState.h" #include #include "NetworkClient.h" diff --git a/Code/Game/DanBiasGame/GameClientState/LanMenuState.cpp b/Code/Game/DanBiasGame/GameClientState/LanMenuState.cpp new file mode 100644 index 00000000..fdb02fa9 --- /dev/null +++ b/Code/Game/DanBiasGame/GameClientState/LanMenuState.cpp @@ -0,0 +1,164 @@ +#include "LanMenuState.h" + +#include "C_obj/C_Player.h" +#include "C_obj/C_StaticObj.h" +#include "C_obj/C_DynamicObj.h" +#include "DllInterfaces/GFXAPI.h" + +#include + +using namespace DanBias::Client; + +struct LanMenuState::myData +{ + myData(){} + Oyster::Math3D::Float4x4 view; + Oyster::Math3D::Float4x4 proj; + C_Object* object[2]; + int modelCount; + // UI object + // game client* +}privData; + +LanMenuState::LanMenuState() +{ + +} + +LanMenuState::~LanMenuState() +{ + +} + +bool LanMenuState::Init(Oyster::Network::NetworkClient* nwClient) +{ + privData = new myData(); + this->nwClient = nwClient; + // load models + LoadModels(L"UImodels.txt"); + InitCamera(Oyster::Math::Float3(0,0,5.4f)); + + return true; +} + +bool LanMenuState::LoadModels(std::wstring file) +{ + Oyster::Graphics::Definitions::Pointlight plight; + plight.Pos = Oyster::Math::Float3(-2,3,0); + plight.Color = Oyster::Math::Float3(0,1,0); + plight.Radius = 10; + plight.Bright = 1; + Oyster::Graphics::API::AddLight(plight); + // open file + // read file + // init models + privData->modelCount = 2; + + ModelInitData modelData; + + modelData.world = Oyster::Math3D::Float4x4::identity; + modelData.visible = true; + modelData.modelPath = L"..\\Content\\Models\\box_2.dan"; + // load models + privData->object[0] = new C_StaticObj(); + privData->object[0]->Init(modelData); + + Oyster::Math3D::Float4x4 translate = Oyster::Math3D::TranslationMatrix(Oyster::Math::Float3(-2,-2,-2)); + modelData.world = modelData.world * translate; + + privData->object[1] = new C_DynamicObj(); + privData->object[1]->Init(modelData); + return true; +} + +bool LanMenuState::InitCamera(Oyster::Math::Float3 startPos) +{ + privData->proj = Oyster::Math3D::ProjectionMatrix_Perspective(Oyster::Math::pi/2,1024.0f/768.0f,.1f,1000); + //privData->proj = Oyster::Math3D::ProjectionMatrix_Orthographic(1024, 768, 1, 1000); + Oyster::Graphics::API::SetProjection(privData->proj); + + privData->view = Oyster::Math3D::OrientationMatrix_LookAtDirection(Oyster::Math::Float3(0,0,-1),Oyster::Math::Float3(0,1,0),startPos); + privData->view = Oyster::Math3D::InverseOrientationMatrix(privData->view); + return true; +} + +GameClientState::ClientState LanMenuState::Update(float deltaTime, InputClass* KeyInput) +{ + // create game + if( KeyInput->IsKeyPressed(DIK_C)) + { + DanBias::GameServerAPI::ServerInitDesc desc; + + DanBias::GameServerAPI::ServerInitiate(desc); + DanBias::GameServerAPI::ServerStart(); + // my ip + nwClient->Connect(15151, "127.0.0.1"); + + if (!nwClient->IsConnected()) + { + // failed to connect + return ClientState_Same; + } + return ClientState_Lobby; + } + // join game + if( KeyInput->IsKeyPressed(DIK_J)) + { + // game ip + nwClient->Connect(15151, "194.47.150.56"); + + if (!nwClient->IsConnected()) + { + // failed to connect + return ClientState_Same; + } + return ClientState_Lobby; + } + return ClientState_Same; +} + +bool LanMenuState::Render() +{ + Oyster::Graphics::API::SetView(privData->view); + Oyster::Graphics::API::SetProjection( privData->proj); + + + Oyster::Graphics::API::NewFrame(); + // render objects + for (int i = 0; i < privData->modelCount; i++) + { + privData->object[i]->Render(); + } + + // render effects + + // render lights + + Oyster::Graphics::API::EndFrame(); + return true; +} + +bool LanMenuState::Release() +{ + for (int i = 0; i < privData->modelCount; i++) + { + privData->object[i]->Release(); + delete privData->object[i]; + privData->object[i] = NULL; + } + + delete privData; + privData = NULL; + + return true; +} + +void LanMenuState::Protocol(ProtocolStruct* protocolStruct) +{ + if((PlayerName*)protocolStruct) + PlayerJoinProtocol((PlayerName*)protocolStruct); +} +void LanMenuState::PlayerJoinProtocol(PlayerName* name) +{ + +} \ No newline at end of file diff --git a/Code/Game/DanBiasGame/GameClientState/LanMenuState.h b/Code/Game/DanBiasGame/GameClientState/LanMenuState.h new file mode 100644 index 00000000..5182c80c --- /dev/null +++ b/Code/Game/DanBiasGame/GameClientState/LanMenuState.h @@ -0,0 +1,37 @@ +#ifndef LAN_MENU_STATE_H +#define LAN_MENU_STATE_H + +#include "GameClientState.h" +#include "OysterMath.h" + +namespace DanBias +{ + namespace Client + { + class LanMenuState : public GameClientState + { + public: + LanMenuState(); + virtual ~LanMenuState(); + + virtual bool Init(Oyster::Network::NetworkClient* nwClient); + virtual ClientState Update(float deltaTime, InputClass* KeyInput); + + bool LoadModels(std::wstring file); + bool InitCamera(Oyster::Math::Float3 startPos); + + virtual bool Render(); + virtual bool Release(); + virtual void Protocol(ProtocolStruct* protocolStruct); + + void PlayerJoinProtocol(PlayerName* name); + + private: + Oyster::Network::NetworkClient* nwClient; + struct myData; + myData* privData; + }; + } +} + +#endif \ No newline at end of file