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 43edd84a..93c4a60a 100644
--- a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp
+++ b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp
@@ -6,8 +6,10 @@
#include "GameClientState\GameState.h"
#include "GameClientState\LobbyState.h"
#include "GameClientState\LoginState.h"
+#include "GameClientState\LanMenuState.h"
#include
#include "NetworkClient.h"
+#include
#include "../WindowManager/WindowShell.h"
#include "L_inputClass.h"
@@ -38,6 +40,7 @@ namespace DanBias
InputClass* inputObj;
Utility::WinTimer* timer;
GameRecieverObject* recieverObj;
+ bool serverOwner;
} data;
#pragma endregion
@@ -63,13 +66,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,9 +131,14 @@ namespace DanBias
HRESULT DanBiasGame::Update(float deltaTime)
{
+ m_data->recieverObj->Update();
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);
@@ -148,6 +151,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/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
diff --git a/Code/Game/DanBiasGame/GameClientState/LobbyState.cpp b/Code/Game/DanBiasGame/GameClientState/LobbyState.cpp
index 5d6bff79..35a5f295 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))
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/NetworkClient.cpp b/Code/Network/NetworkAPI/NetworkClient.cpp
index 8e8c28fa..717a2946 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/NetworkAPI/NetworkSession.h b/Code/Network/NetworkAPI/NetworkSession.h
index f0677c77..ddae7f24 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
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*();