diff --git a/Code/DanBias.sln b/Code/DanBias.sln index 540348f9..5e8f837e 100644 --- a/Code/DanBias.sln +++ b/Code/DanBias.sln @@ -32,9 +32,6 @@ EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "GameLogic", "Game\GameLogic\GameLogic.vcxproj", "{B1195BB9-B3A5-47F0-906C-8DEA384D1520}" EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DanBiasLauncher", "Game\DanBiasLauncher\DanBiasLauncher.vcxproj", "{8690FDDF-C5B7-4C42-A337-BD5243F29B85}" - ProjectSection(ProjectDependencies) = postProject - {52380DAA-0F4A-4D97-8E57-98DF39319CAF} = {52380DAA-0F4A-4D97-8E57-98DF39319CAF} - EndProjectSection EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "NetworkAPI", "Network\NetworkAPI\NetworkAPI.vcxproj", "{460D625F-2AC9-4559-B809-0BA89CEAEDF4}" EndProject @@ -44,8 +41,6 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DanBiasServerLauncher", "Ga EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "aDanBiasGameLauncher", "Game\aDanBiasGameLauncher\aDanBiasGameLauncher.vcxproj", "{666FEA52-975F-41CD-B224-B19AF3C0ABBA}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ServerDependencies", "Game\ServerDependencies\ServerDependencies.vcxproj", "{52380DAA-0F4A-4D97-8E57-98DF39319CAF}" -EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "GameServer", "Game\GameServer\GameServer.vcxproj", "{143BD516-20A1-4890-A3E4-F8BFD02220E7}" EndProject Global @@ -274,18 +269,6 @@ Global {666FEA52-975F-41CD-B224-B19AF3C0ABBA}.Release|Win32.Build.0 = Release|Win32 {666FEA52-975F-41CD-B224-B19AF3C0ABBA}.Release|x64.ActiveCfg = Release|x64 {666FEA52-975F-41CD-B224-B19AF3C0ABBA}.Release|x64.Build.0 = Release|x64 - {52380DAA-0F4A-4D97-8E57-98DF39319CAF}.Debug|Mixed Platforms.ActiveCfg = Debug|Win32 - {52380DAA-0F4A-4D97-8E57-98DF39319CAF}.Debug|Mixed Platforms.Build.0 = Debug|Win32 - {52380DAA-0F4A-4D97-8E57-98DF39319CAF}.Debug|Win32.ActiveCfg = Debug|Win32 - {52380DAA-0F4A-4D97-8E57-98DF39319CAF}.Debug|Win32.Build.0 = Debug|Win32 - {52380DAA-0F4A-4D97-8E57-98DF39319CAF}.Debug|x64.ActiveCfg = Debug|x64 - {52380DAA-0F4A-4D97-8E57-98DF39319CAF}.Debug|x64.Build.0 = Debug|x64 - {52380DAA-0F4A-4D97-8E57-98DF39319CAF}.Release|Mixed Platforms.ActiveCfg = Release|Win32 - {52380DAA-0F4A-4D97-8E57-98DF39319CAF}.Release|Mixed Platforms.Build.0 = Release|Win32 - {52380DAA-0F4A-4D97-8E57-98DF39319CAF}.Release|Win32.ActiveCfg = Release|Win32 - {52380DAA-0F4A-4D97-8E57-98DF39319CAF}.Release|Win32.Build.0 = Release|Win32 - {52380DAA-0F4A-4D97-8E57-98DF39319CAF}.Release|x64.ActiveCfg = Release|x64 - {52380DAA-0F4A-4D97-8E57-98DF39319CAF}.Release|x64.Build.0 = Release|x64 {143BD516-20A1-4890-A3E4-F8BFD02220E7}.Debug|Mixed Platforms.ActiveCfg = Debug|Win32 {143BD516-20A1-4890-A3E4-F8BFD02220E7}.Debug|Mixed Platforms.Build.0 = Debug|Win32 {143BD516-20A1-4890-A3E4-F8BFD02220E7}.Debug|Win32.ActiveCfg = Debug|Win32 @@ -313,7 +296,6 @@ Global {DA2AA800-ED64-4649-8B3B-E7F1E3968B78} = {20720CA7-795C-45AD-A302-9383A6DD503A} {060B1890-CBF3-4808-BA99-A4776222093B} = {20720CA7-795C-45AD-A302-9383A6DD503A} {666FEA52-975F-41CD-B224-B19AF3C0ABBA} = {20720CA7-795C-45AD-A302-9383A6DD503A} - {52380DAA-0F4A-4D97-8E57-98DF39319CAF} = {20720CA7-795C-45AD-A302-9383A6DD503A} {143BD516-20A1-4890-A3E4-F8BFD02220E7} = {20720CA7-795C-45AD-A302-9383A6DD503A} EndGlobalSection EndGlobal diff --git a/Code/Dokumentation/GameServer.uxf b/Code/Dokumentation/GameServer.uxf index 039b4dda..735a2592 100644 --- a/Code/Dokumentation/GameServer.uxf +++ b/Code/Dokumentation/GameServer.uxf @@ -1,11 +1,11 @@ - + 10 UMLClass - 380 - 360 + 610 + 340 100 30 @@ -16,8 +16,8 @@ UMLClass - 360 - 540 + 580 + 480 160 80 @@ -28,77 +28,33 @@ /players./ - - UMLClass - - 560 - 360 - 100 - 30 - - LobbyClient - - com.umlet.element.Relation - 400 - 460 - 50 - 100 + 620 + 340 + 60 + 160 lt=->>>> - 30;80;30;30 + 30;140;40;30 com.umlet.element.Relation - 450 - 340 - 130 + 710 + 480 + 100 50 - lt=>>>- - 110;30;30;30 - - - com.umlet.element.Relation - - 460 - 270 - 170 - 110 - - lt=>>>- - 150;90;150;30;30;30 - - - com.umlet.element.Relation - - 490 - 450 - 140 - 140 - - lt=>>>- - 120;30;120;120;30;120 - - - com.umlet.element.Relation - - 460 - 180 - 400 - 150 - - lt=<<. - 380;130;170;130;170;30;30;30 + lt=>>>>- + 80;30;30;30 UMLClass - 370 - 200 + 600 + 180 120 120 @@ -108,42 +64,19 @@ com.umlet.element.Relation - 400 - 290 + 630 + 270 50 90 lt=>>>>- 30;70;30;30 - - com.umlet.element.Relation - - 600 - 300 - 260 - 80 - - lt=<<. - 240;30;30;30;30;60 - - - com.umlet.element.Package - - 840 - 290 - 120 - 50 - - NetworkAPI -bg=#a21aff - - UMLClass - 360 - 120 + 590 + 100 130 40 @@ -154,8 +87,8 @@ DanBiasServerAPI com.umlet.element.Relation - 400 - 130 + 630 + 110 50 90 @@ -166,10 +99,10 @@ DanBiasServerAPI UMLClass - 150 - 360 + 1060 + 330 120 - 30 + 50 NetworkSession @@ -177,41 +110,41 @@ DanBiasServerAPI com.umlet.element.Relation - 180 - 270 - 210 - 110 + 690 + 220 + 390 + 50 - lt=->>>>> - 190;30;30;30;30;90 + lt=-<<<< + 30;30;370;30 com.umlet.element.Relation - 240 - 340 - 160 + 680 + 320 + 400 50 lt=->>>>> - 140;30;30;30 + 30;30;380;30 com.umlet.element.Relation - 180 - 360 - 200 - 210 + 690 + 340 + 390 + 160 lt=->>>>> - 180;190;30;190;30;30 + 30;140;40;40;370;30 com.umlet.element.Package - 840 - 410 + 460 + 640 120 50 @@ -222,164 +155,19 @@ bg=blue com.umlet.element.Relation - 600 - 360 - 260 - 90 + 370 + 530 + 350 + 290 lt=<<. - 240;70;30;70;30;30 - - - com.umlet.element.Package - - 840 - 350 - 120 - 40 - - PhysicsAPI -bg=blue --- - - - - - com.umlet.element.Relation - - 630 - 340 - 230 - 50 - - lt=.<< - 210;30;30;30 - - - com.umlet.element.Relation - - 490 - 420 - 370 - 190 - - lt=<<. - 350;30;190;30;190;170;30;170 + 90;150;30;270;330;240;240;30 UMLClass - 170 - 160 - 130 - 90 - - ServerInitReader --- -Helper to load ini files to server - -elementstyle=wordwrap - - - - - com.umlet.element.Relation - - 270 - 180 - 120 - 50 - - lt=>>. - - 100;30;30;30 - - - UMLClass - - 360 - 440 - 160 - 50 - - GameSessionManager --- -/Creates game sessions/ - - - - com.umlet.element.Relation - - 400 - 360 - 50 - 100 - - lt=>>- - 30;30;30;80 - - - com.umlet.element.Package - - 840 - 470 - 120 - 40 - - ProtocolManager -bg=#aaaaa - - - - com.umlet.element.Relation - - 160 - 360 - 770 - 300 - - lt=<<. - 750;150;750;280;30;280;30;30 - - - com.umlet.element.Relation - - 930 - 300 - 70 - 210 - - lt=<<. - 30;190;50;190;50;30;30;30 - - - UMLClass - - 10 - 350 - 120 - 40 - - /<<interface>>/ -INetworkSession - - - - com.umlet.element.Relation - - 100 - 340 - 70 - 50 - - lt=->>>>> - 50;30;30;30 - - - UMLClass - - 560 - 450 + 790 + 500 100 30 @@ -389,28 +177,91 @@ INetworkSession com.umlet.element.Relation - 580 - 360 - 50 - 110 + 670 + 530 + 150 + 180 - lt=>>>- - 30;30;30;90 + lt=<<. + 130;160;30;30 + + + com.umlet.element.Package + + 800 + 670 + 120 + 40 + + GameProtocols +bg=#aaaaa + + + + com.umlet.element.Package + + 1040 + 200 + 160 + 190 + + NetworkAPI +bg=#a21aff + UMLClass - 0 - 460 - 160 - 50 + 1060 + 280 + 120 + 40 - MapManager --- -Manages all map stuff. - -elementstyle=wordwrap - + NetworkClient + + UMLClass + + 1060 + 230 + 120 + 40 + + NetworkServer + + + + com.umlet.element.Relation + + 690 + 240 + 390 + 80 + + lt=-> + 30;30;200;30;200;60;370;60 + + + com.umlet.element.Relation + + 690 + 260 + 390 + 100 + + lt=-<<<< + 30;30;180;30;180;80;370;80 + + + com.umlet.element.Relation + + 860 + 270 + 380 + 260 + + lt=>>>- + 320;30;360;30;360;230;30;240 + diff --git a/Code/Game/DanBiasGame/DanBiasGame.vcxproj b/Code/Game/DanBiasGame/DanBiasGame.vcxproj index 0e856428..25696efd 100644 --- a/Code/Game/DanBiasGame/DanBiasGame.vcxproj +++ b/Code/Game/DanBiasGame/DanBiasGame.vcxproj @@ -192,6 +192,7 @@ + @@ -205,6 +206,7 @@ + diff --git a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp index 930d4bad..07d2d0ec 100644 --- a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp +++ b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp @@ -61,22 +61,20 @@ namespace DanBias return DanBiasClientReturn_Error; m_data->recieverObj = new GameRecieverObject; - - m_data->recieverObj->nwClient = new Oyster::Network::NetworkClient(m_data->recieverObj, Oyster::Network::NetworkProtocolCallbackType_Object); - m_data->recieverObj->nwClient->Connect(desc.port, desc.IP); + m_data->recieverObj->Connect(desc.port, desc.IP); - if (!m_data->recieverObj->nwClient->IsConnected()) + if (!m_data->recieverObj->IsConnected()) { // failed to connect return DanBiasClientReturn_Error; } // Start in lobby state m_data->recieverObj->gameClientState = new Client::LobbyState(); - if(!m_data->recieverObj->gameClientState->Init(m_data->recieverObj->nwClient)) + if(!m_data->recieverObj->gameClientState->Init(m_data->recieverObj)) return DanBiasClientReturn_Error; - m_data->timer = new Utility::WinTimer(); //why dynamic memory? - m_data->timer->reset(); + m_data->timer = new Utility::WinTimer(); //why dynamic memory? + m_data->timer->reset(); return DanBiasClientReturn_Sucess; } @@ -157,7 +155,7 @@ namespace DanBias return E_FAIL; break; } - m_data->recieverObj->gameClientState->Init(m_data->recieverObj->nwClient); // send game client + m_data->recieverObj->gameClientState->Init(m_data->recieverObj); // send game client } return S_OK; @@ -184,10 +182,9 @@ namespace DanBias { m_data->recieverObj->gameClientState->Release(); delete m_data->recieverObj->gameClientState; - m_data->recieverObj->nwClient->Disconnect(); - delete m_data->recieverObj->nwClient; - delete m_data->timer; + m_data->recieverObj->Disconnect(); delete m_data->recieverObj; + delete m_data->timer; delete m_data->inputObj; delete m_data; diff --git a/Code/Game/DanBiasGame/GameClientRecieverFunc.h b/Code/Game/DanBiasGame/GameClientRecieverFunc.h index 5e129fca..360d1c60 100644 --- a/Code/Game/DanBiasGame/GameClientRecieverFunc.h +++ b/Code/Game/DanBiasGame/GameClientRecieverFunc.h @@ -1,11 +1,12 @@ #ifndef DANBIAS_CLIENTRECIEVEROBJECT_H #define DANBIAS_CLIENTRECIEVEROBJECT_H +//WTF!? No headers included??? + namespace DanBias { -struct GameRecieverObject :public Oyster::Network::ProtocolRecieverObject +struct GameRecieverObject :public Oyster::Network::NetworkClient { - Oyster::Network::NetworkClient* nwClient; Client::GameClientState* gameClientState; // receiver function for server messages diff --git a/Code/Game/DanBiasGame/GameClientState/C_obj/C_Player.cpp b/Code/Game/DanBiasGame/GameClientState/C_obj/C_Player.cpp index 4cd6fbd3..49c450b5 100644 --- a/Code/Game/DanBiasGame/GameClientState/C_obj/C_Player.cpp +++ b/Code/Game/DanBiasGame/GameClientState/C_obj/C_Player.cpp @@ -8,6 +8,7 @@ struct C_Player::myData Oyster::Math3D::Float4x4 view; Oyster::Math3D::Float4x4 proj; Oyster::Graphics::Model::Model *model; + Oyster::Math3D::Float4 lookDir; int ID; }privData; @@ -29,7 +30,7 @@ void C_Player::Init(ModelInitData modelInit) privData->model->WorldMatrix = modelInit.world; privData->model->Visible = modelInit.visible; privData->ID = modelInit.id; - + privData->lookDir = Oyster::Math3D::Float4 (0,0,1,0); } void C_Player::setPos(Oyster::Math::Float4x4 world) { diff --git a/Code/Game/DanBiasGame/GameClientState/Camera.cpp b/Code/Game/DanBiasGame/GameClientState/Camera.cpp new file mode 100644 index 00000000..13b5a70f --- /dev/null +++ b/Code/Game/DanBiasGame/GameClientState/Camera.cpp @@ -0,0 +1,192 @@ +#include "Camera.h" + +Camera::Camera() +{ + this->m_position = Oyster::Math::Float3(0, 50, 0); + this->mRight = Oyster::Math::Float3(1, 0, 0); + this->mUp = Oyster::Math::Float3(0, 1, 0); + this->mLook = Oyster::Math::Float3(0, 0, 1); +} + +Camera::~Camera() +{ +} + +void Camera::SetPosition(const Oyster::Math::Float3& v) +{ + this->m_position = v; +} + +Oyster::Math::Float3 Camera::GetPosition()const +{ + return this->m_position; +} + +Oyster::Math::Float3 Camera::GetRight()const +{ + return this->mRight; +} + +Oyster::Math::Float3 Camera::GetUp()const +{ + return this->mUp; +} + +Oyster::Math::Float3 Camera::GetLook()const +{ + return this->mLook; +} + +float Camera::GetNearZ()const +{ + return this->mNearZ; +} + +float Camera::GetFarZ()const +{ + return this->mFarZ; +} + +float Camera::GetAspect()const +{ + return this->mAspect; +} + +Oyster::Math::Float3 Camera::CrossMatrix(const Oyster::Math::Float3& vector, const Oyster::Math::Float4x4& matrix) +{ + Oyster::Math::Float3 vec; + vec.x = matrix.m11*vector.x + matrix.m12*vector.y + matrix.m13*vector.z; + vec.y = matrix.m21*vector.x + matrix.m22*vector.y + matrix.m23*vector.z; + vec.z = matrix.m31*vector.x + matrix.m32*vector.y + matrix.m33*vector.z; + return vec; +} + +void Camera::SetLens(float fovY, float aspect, float zn, float zf) +{ + this->mFovY = fovY; + this->mAspect = aspect; + this->mNearZ = zn; + this->mFarZ = zf; + + /*float yScale = tan((Oyster::Math::pi*0.5f) - (mFovY*0.5f)); + float xScale = yScale/this->mAspect; + + mProj = Oyster::Math::Float4x4(xScale, 0, 0, 0, + 0, yScale, 0, 0, + 0, 0, zf/(zf-zn), 1, + 0, 0, -zn*zf/(zf-zn), 0); + mProj.Transpose();*/ + mProj = Oyster::Math3D::ProjectionMatrix_Perspective(fovY,aspect,zn,zf); +} + +void Camera::LookAt(Oyster::Math::Float3 pos, Oyster::Math::Float3 target, Oyster::Math::Float3 worldUp) +{ + Oyster::Math::Float3 L; + + L = target - pos; + L.Normalize(); + + Oyster::Math::Float3 R; + R = worldUp.Cross(L); + R.Normalize(); + + Oyster::Math::Float3 U; + U = L.Cross(R); + + this->m_position = pos; + this->mLook = L; + this->mRight = R; + this->mUp = U; +} + +Oyster::Math::Float4x4 Camera::View()const +{ + return this->mView; +} + +Oyster::Math::Float4x4 Camera::Proj()const +{ + return this->mProj; +} + +Oyster::Math::Float4x4 Camera::ViewsProj()const +{ + Oyster::Math::Float4x4 M; + M = mView * mProj; + return M; +} + +void Camera::Walk(float dist) +{ + this->m_position += dist*this->mLook; +} + +void Camera::Strafe(float dist) +{ + this->m_position += dist*this->mRight; +} + +void Camera::Pitch(float angle) +{ + float radians = angle * 0.0174532925f; + + Oyster::Math::Float4x4 R; + + Oyster::Math3D::RotationMatrix(radians,-mRight,R); + this->mUp = CrossMatrix(this->mUp, R); + this->mLook = CrossMatrix(this->mLook, R); +} + +void Camera::Yaw(float angle) +{ + float radians = angle * 0.0174532925f; + + Oyster::Math::Float4x4 R; + + Oyster::Math::Float3 up(0,1,0); + Oyster::Math3D::RotationMatrix(radians,-up,R); + + this->mRight = CrossMatrix(this->mRight, R); + this->mUp = CrossMatrix(mUp, R); + this->mLook = CrossMatrix(this->mLook, R); +} + +void Camera::UpdateViewMatrix() +{ + mLook.Normalize(); + mUp = mLook.Cross(mRight); + mUp.Normalize(); + mRight = mUp.Cross(mLook); + mView = Oyster::Math3D::ViewMatrix_LookAtDirection(mLook, mUp, m_position); + /* + mLook.Normalize(); + mUp = mLook.Cross(mRight); + mUp.Normalize(); + mRight = mUp.Cross(mLook); + + float x = -m_position.Dot(mRight); + float y = -m_position.Dot(mUp); + float z = -m_position.Dot(mLook); + + mView.m11 = mRight.x; + mView.m21 = mRight.y; + mView.m31 = mRight.z; + mView.m41 = x; + + mView.m12 = mUp.x; + mView.m22 = mUp.y; + mView.m32 = mUp.z; + mView.m42 = y; + + mView.m13 = mLook.x; + mView.m23 = mLook.y; + mView.m33 = mLook.z; + mView.m43 = z; + + mView.m14 = 0.0f; + mView.m24 = 0.0f; + mView.m34 = 0.0f; + mView.m44 = 1.0f; + + mView.Transpose();*/ +} \ No newline at end of file diff --git a/Code/Game/DanBiasGame/GameClientState/Camera.h b/Code/Game/DanBiasGame/GameClientState/Camera.h new file mode 100644 index 00000000..56ea5569 --- /dev/null +++ b/Code/Game/DanBiasGame/GameClientState/Camera.h @@ -0,0 +1,63 @@ +#ifndef CAMERA__H +#define CAMERA__H + +#include "OysterMath.h" + +class Camera +{ +private: + + Oyster::Math::Float3 m_position; + Oyster::Math::Float3 mRight; + Oyster::Math::Float3 mUp; + Oyster::Math::Float3 mLook; + + + + float mNearZ; + float mFarZ; + float mAspect; + float mFovY; + + Oyster::Math::Float4x4 mView; + Oyster::Math::Float4x4 mProj; + +public: + Camera(); + virtual ~Camera(); + + void SetPosition(const Oyster::Math::Float3& v); + + Oyster::Math::Float3 GetPosition()const; + + Oyster::Math::Float3 GetRight()const; + Oyster::Math::Float3 GetUp()const; + Oyster::Math::Float3 GetLook()const; + + float GetNearZ()const; + float GetFarZ()const; + float GetAspect()const; + + Oyster::Math::Float3 CrossMatrix(const Oyster::Math::Float3& v, const Oyster::Math::Float4x4& m); + + void SetLens(float fovY, float aspect, float zn, float zf); + + void LookAt(Oyster::Math::Float3 pos, Oyster::Math::Float3 target, Oyster::Math::Float3 worldUp); + + void setLook(Oyster::Math::Float3 look) { mLook = look; } + void setUp(Oyster::Math::Float3 up) { mUp = up; } + void setRight(Oyster::Math::Float3 right) { mRight = right; } + + Oyster::Math::Float4x4 View()const; + Oyster::Math::Float4x4 Proj()const; + Oyster::Math::Float4x4 ViewsProj()const; + + void Walk(float dist); + void Strafe(float dist); + + void Pitch(float angle); + void Yaw(float angle); + + void UpdateViewMatrix(); +}; +#endif \ No newline at end of file diff --git a/Code/Game/DanBiasGame/GameClientState/GameState.cpp b/Code/Game/DanBiasGame/GameClientState/GameState.cpp index 70385e07..3e89a824 100644 --- a/Code/Game/DanBiasGame/GameClientState/GameState.cpp +++ b/Code/Game/DanBiasGame/GameClientState/GameState.cpp @@ -4,7 +4,7 @@ #include "C_obj/C_DynamicObj.h" #include #include "NetworkClient.h" - +#include "Camera.h" using namespace DanBias::Client; @@ -17,7 +17,6 @@ struct GameState::myData int modelCount; Oyster::Network::NetworkClient* nwClient; gameStateState state; - }privData; @@ -38,10 +37,12 @@ GameState::~GameState(void) bool GameState::Init(Oyster::Network::NetworkClient* nwClient) { // load models + camera = new Camera; privData = new myData(); privData->state = gameStateState_loading; privData->nwClient = nwClient; privData->state = LoadGame(); + return true; } GameState::gameStateState GameState::LoadGame() @@ -92,14 +93,14 @@ bool GameState::LoadModels(std::wstring mapFile) modelData.modelPath = L"..\\Content\\Models\\char_white.dan"; modelData.id ++; - obj = new C_DynamicObj(); + obj = new C_Player(); privData->object.push_back(obj); privData->object[privData->object.size() -1 ]->Init(modelData); - translate = Oyster::Math3D::TranslationMatrix(Oyster::Math::Float3(-2,-2,-2)); + /*translate = Oyster::Math3D::TranslationMatrix(Oyster::Math::Float3(-2,-2,-2)); modelData.world = modelData.world * translate; modelData.modelPath = L"..\\Content\\Models\\char_white.dan"; - modelData.id ++; + modelData.id ++;*/ translate = Oyster::Math3D::TranslationMatrix(Oyster::Math::Float3(0,0,0)); Oyster::Math3D::Float4x4 scale = Oyster::Math3D::Float4x4::identity; @@ -110,7 +111,7 @@ bool GameState::LoadModels(std::wstring mapFile) modelData.modelPath = L"..\\Content\\Models\\ball.dan"; modelData.id ++; - obj = new C_DynamicObj(); + obj = new C_Player(); privData->object.push_back(obj); privData->object[privData->object.size() -1 ]->Init(modelData); @@ -119,10 +120,19 @@ bool GameState::LoadModels(std::wstring mapFile) } bool GameState::InitCamera(Oyster::Math::Float3 startPos) { + Oyster::Math::Float3 dir = Oyster::Math::Float3(0,0,-1); + Oyster::Math::Float3 up =Oyster::Math::Float3(0,1,0); + Oyster::Math::Float3 pos = Oyster::Math::Float3(0, 0, 20); + + camera->LookAt(pos, dir, up); + camera->SetLens(3.14f/2, 1024/768, 1, 1000); + 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); - + camera->UpdateViewMatrix(); + privData->view = camera->View(); + privData->view = Oyster::Math3D::ViewMatrix_LookAtDirection(Oyster::Math::Float3(0,0,-1),Oyster::Math::Float3(0,1,0),startPos); 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; @@ -147,85 +157,9 @@ 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; - movePlayer.bLeft = false; - movePlayer.bRight = false; + readKeyInput(KeyInput); + camera->UpdateViewMatrix(); - if(KeyInput->IsKeyPressed(DIK_W)) - { - - if(!key_forward) - { - movePlayer.bForward = true; - send = true; - key_forward = true; - - GameLogic::Protocol_General_Text tp; - tp.text = "What!?"; - this->privData->nwClient->Send(tp); - } - } - else - key_forward = false; - - if(KeyInput->IsKeyPressed(DIK_S)) - { - if(!key_backward) - { - movePlayer.bBackward = true; - send = true; - key_backward = true; - } - } - else - key_backward = false; - - if(KeyInput->IsKeyPressed(DIK_A)) - { - if(!key_strafeLeft) - { - movePlayer.bLeft = true; - send = true; - key_strafeLeft = true; - } - } - else - key_strafeLeft = false; - - if(KeyInput->IsKeyPressed(DIK_D)) - { - if(!key_strafeRight) - { - movePlayer.bRight = true; - send = true; - key_strafeRight = true; - } - } - else - key_strafeRight = false; - - - if (privData->nwClient->IsConnected() && send) - { - privData->nwClient->Send(movePlayer); - } - - //send delta mouse movement - if (KeyInput->IsMousePressed()) - { - GameLogic::Protocol_PlayerMouse deltaMouseMove; - deltaMouseMove.dxMouse = KeyInput->GetYaw(); - deltaMouseMove.dyMouse = KeyInput->GetPitch(); - //privData->nwClient->Send(deltaMouseMove); - } - - // send event data - // - if(KeyInput->IsKeyPressed(DIK_L)) - privData->state = GameState::gameStateState_end; } break; case gameStateState_end: @@ -240,7 +174,9 @@ GameClientState::ClientState GameState::Update(float deltaTime, InputClass* KeyI } bool GameState::Render() { - Oyster::Graphics::API::SetView(privData->view); + Oyster::Graphics::API::SetView(camera->View()); + //Oyster::Graphics::API::SetProjection(camera->Proj()); + //Oyster::Graphics::API::SetView(privData->view); Oyster::Graphics::API::SetProjection(privData->proj); Oyster::Graphics::API::NewFrame(); for (unsigned int i = 0; i < privData->object.size(); i++) @@ -259,10 +195,108 @@ bool GameState::Release() privData->object[i] = NULL; } + delete this->camera; + delete privData; privData = NULL; return true; } +void GameState::readKeyInput(InputClass* KeyInput) +{ + + bool send = false; + GameLogic::Protocol_PlayerMovement movePlayer; + movePlayer.bForward = false; + movePlayer.bBackward = false; + movePlayer.bLeft = false; + movePlayer.bRight = false; + + if(KeyInput->IsKeyPressed(DIK_W)) + { + + if(!key_forward) + { + movePlayer.bForward = true; + send = true; + key_forward = true; + } + } + else + key_forward = false; + + if(KeyInput->IsKeyPressed(DIK_S)) + { + if(!key_backward) + { + movePlayer.bBackward = true; + send = true; + key_backward = true; + } + } + else + key_backward = false; + + if(KeyInput->IsKeyPressed(DIK_A)) + { + if(!key_strafeLeft) + { + movePlayer.bLeft = true; + send = true; + key_strafeLeft = true; + } + } + else + key_strafeLeft = false; + + if(KeyInput->IsKeyPressed(DIK_D)) + { + if(!key_strafeRight) + { + movePlayer.bRight = true; + send = true; + key_strafeRight = true; + } + } + else + key_strafeRight = false; + + + if (privData->nwClient->IsConnected() && send) + { + privData->nwClient->Send(movePlayer); + } + + //send delta mouse movement + if (KeyInput->IsMousePressed()) + { + camera->Yaw(KeyInput->GetYaw()); + camera->Pitch(KeyInput->GetPitch()); + camera->UpdateViewMatrix(); + GameLogic::Protocol_PlayerLook playerLookDir; + Oyster::Math::Float3 look = camera->GetLook(); + playerLookDir.lookDirX = look.x; + playerLookDir.lookDirY = look.y; + playerLookDir.lookDirZ = look.z; + privData->nwClient->Send(playerLookDir); + } + if(KeyInput->IsKeyPressed(DIK_Z)) + { + if(!key_Shoot) + { + GameLogic::Protocol_PlayerShot playerShot; + playerShot.hasShot = true; + privData->nwClient->Send(playerShot); + key_Shoot = true; + } + } + else + key_Shoot = false; + + // send event data + // + if(KeyInput->IsKeyPressed(DIK_L)) + privData->state = GameState::gameStateState_end; +} void GameState::Protocol(ProtocolStruct* pos) { @@ -290,13 +324,17 @@ void GameState::Protocol( ObjPos* pos ) for (unsigned int i = 0; i < privData->object.size(); i++) { - if(privData->object[i] && privData->object[i]->GetId() == pos->object_ID) + if(privData->object[i]->GetId() == pos->object_ID) { privData->object[i]->setPos(world); - - //privData->view = world; - //privData->view = Oyster::Math3D::InverseOrientationMatrix(privData->view); - + //camera->setRight((Oyster::Math::Float3(world[0], world[1], world[2]))); + //camera->setUp((Oyster::Math::Float3(world[4], world[5], world[6]))); + //camera->setLook((Oyster::Math::Float3(world[8], world[9], world[10]))); + if(i == 0) + { + camera->SetPosition(Oyster::Math::Float3(world[12], world[13], world[14])); + camera->UpdateViewMatrix(); + } } } } diff --git a/Code/Game/DanBiasGame/GameClientState/GameState.h b/Code/Game/DanBiasGame/GameClientState/GameState.h index 3942afba..30884043 100644 --- a/Code/Game/DanBiasGame/GameClientState/GameState.h +++ b/Code/Game/DanBiasGame/GameClientState/GameState.h @@ -3,6 +3,7 @@ #include "GameClientState.h" #include "OysterMath.h" #include +#include "Camera.h" namespace DanBias { namespace Client @@ -21,6 +22,8 @@ private: bool key_backward; bool key_strafeRight; bool key_strafeLeft; + bool key_Shoot; + Camera* camera; struct myData; myData* privData; @@ -33,6 +36,7 @@ public: bool InitCamera(Oyster::Math::Float3 startPos) ; gameStateState LoadGame(); + void readKeyInput(InputClass* KeyInput); bool Render()override; bool Release()override; diff --git a/Code/Game/DanBiasServer/LobbySessions/GameLobby.cpp b/Code/Game/DanBiasServer/LobbySessions/GameLobby.cpp deleted file mode 100644 index fc770db8..00000000 --- a/Code/Game/DanBiasServer/LobbySessions/GameLobby.cpp +++ /dev/null @@ -1,21 +0,0 @@ -///////////////////////////////////////////////////////////////////// -// 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/DanBiasServerLauncher/DanBiasServerLauncher.vcxproj b/Code/Game/DanBiasServerLauncher/DanBiasServerLauncher.vcxproj index 62344a57..dde29257 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\GameServer;C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + $(SolutionDir)Game\GameServer;C:\Program Files %28x86%29\Visual Leak Detector\include;$(SolutionDir)WindowManager\;$(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\GameServer;C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + $(SolutionDir)Game\GameServer;C:\Program Files %28x86%29\Visual Leak Detector\include;$(SolutionDir)WindowManager\;$(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\GameServer;C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + $(SolutionDir)Game\GameServer;C:\Program Files %28x86%29\Visual Leak Detector\include;$(SolutionDir)WindowManager\;$(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\GameServer;C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + $(SolutionDir)Game\GameServer;C:\Program Files %28x86%29\Visual Leak Detector\include;$(SolutionDir)WindowManager\;$(IncludePath) $(OutDir)..\DLL\;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) @@ -111,8 +111,8 @@ Windows true - DanBiasGame_$(PlatformShortName)D.dll;DanBiasServer_$(PlatformShortName)D.dll;%(DelayLoadDLLs) - DanBiasGame_$(PlatformShortName)D.lib;DanBiasServer_$(PlatformShortName)D.lib;%(AdditionalDependencies) + DanBiasGame_$(PlatformShortName)D.dll;GameServer_$(PlatformShortName)D.dll + GameServer_$(PlatformShortName)D.lib;DanBiasGame_$(PlatformShortName)D.lib;%(AdditionalDependencies) @@ -128,8 +128,8 @@ Windows true - DanBiasServer_$(PlatformShortName)D.dll;DanBiasGame_$(PlatformShortName)D.dll;%(DelayLoadDLLs) - DanBiasGame_$(PlatformShortName)D.lib;DanBiasServer_$(PlatformShortName)D.lib;%(AdditionalDependencies) + DanBiasGame_$(PlatformShortName)D.dll;GameServer_$(PlatformShortName)D.dll + GameServer_$(PlatformShortName)D.lib;DanBiasGame_$(PlatformShortName)D.lib;%(AdditionalDependencies) @@ -149,8 +149,8 @@ true true true - DanBiasServer_$(PlatformShortName).dll;DanBiasGame_$(PlatformShortName).dll;%(DelayLoadDLLs) - DanBiasServer_$(PlatformShortName).lib;DanBiasGame_$(PlatformShortName).lib;%(AdditionalDependencies) + DanBiasGame_$(PlatformShortName).dll;GameServer_$(PlatformShortName).dll;%(DelayLoadDLLs) + GameServer_$(PlatformShortName).lib;DanBiasGame_$(PlatformShortName).lib;%(AdditionalDependencies) @@ -170,13 +170,18 @@ true true true - DanBiasServer_$(PlatformShortName).dll;DanBiasGame_$(PlatformShortName).dll;%(DelayLoadDLLs) - DanBiasServer_$(PlatformShortName).lib;DanBiasGame_$(PlatformShortName).lib;%(AdditionalDependencies) + DanBiasGame_$(PlatformShortName).dll;GameServer_$(PlatformShortName).dll;%(DelayLoadDLLs) + GameServer_$(PlatformShortName).lib;DanBiasGame_$(PlatformShortName).lib;%(AdditionalDependencies) + + + {35aea3c0-e0a7-4e1e-88cd-514aa5a442b1} + + diff --git a/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp b/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp index 7781717d..fbf19b57 100644 --- a/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp +++ b/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp @@ -5,7 +5,8 @@ #define NOMINMAX //Blame it on windows #include #include - +#include +#include #include @@ -16,13 +17,14 @@ int WINAPI WinMain( HINSTANCE hinst, HINSTANCE prevInst, PSTR cmdLine, int cmdSh return cmdShow; } - DanBias::GameServerAPI::GameInitDesc desc; - desc.connectionPort = 15151; - desc.maxNumberOfClients = 0; - desc.threaded = false; - if( !DanBias::GameServerAPI::Create(desc) == DanBias::DanBiasServerReturn_Sucess) - { + WindowShell::CreateConsoleWindow(); + DanBias::GameServerAPI::GameInitDesc desc; + desc.listenPort = 15151; + if(DanBias::GameServerAPI::Create(desc) == DanBias::DanBiasServerReturn_Sucess) + { + DanBias::GameServerAPI::Start(); + DanBias::GameServerAPI::Terminate(); } return cmdShow; } \ No newline at end of file diff --git a/Code/Game/GameProtocols/PlayerProtocols.h b/Code/Game/GameProtocols/PlayerProtocols.h index b91cb46d..ffb009cf 100644 --- a/Code/Game/GameProtocols/PlayerProtocols.h +++ b/Code/Game/GameProtocols/PlayerProtocols.h @@ -56,33 +56,36 @@ namespace GameLogic Oyster::Network::CustomNetProtocol protocol; }; - struct Protocol_PlayerMouse :public Oyster::Network::CustomProtocolObject + struct Protocol_PlayerLook :public Oyster::Network::CustomProtocolObject { - float dxMouse; - float dyMouse; - + float lookDirX; + float lookDirY; + float lookDirZ; - Protocol_PlayerMouse() + Protocol_PlayerLook() { - this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_PlayerMouseMovement; + this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_PlayerLookDir; 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; + this->protocol[3].type = Oyster::Network::NetAttributeType_Float; } - const Protocol_PlayerMouse& operator=(Oyster::Network::CustomNetProtocol& val) + const Protocol_PlayerLook& operator=(Oyster::Network::CustomNetProtocol& val) { - dxMouse = val[1].value.netFloat; - dyMouse = val[2].value.netFloat; + lookDirX = val[1].value.netFloat; + lookDirY = val[2].value.netFloat; + lookDirZ = val[3].value.netFloat; return *this; } Oyster::Network::CustomNetProtocol* GetProtocol() override { - this->protocol[1].value = dxMouse; - this->protocol[2].value = dyMouse; + this->protocol[1].value = lookDirX; + this->protocol[2].value = lookDirY; + this->protocol[3].value = lookDirZ; return &protocol; } @@ -119,6 +122,32 @@ namespace GameLogic Oyster::Network::CustomNetProtocol protocol; }; + struct Protocol_PlayerShot :public Oyster::Network::CustomProtocolObject + { + bool hasShot; + + Protocol_PlayerShot() + { + this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_PlayerShot; + this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + + this->protocol[1].type = Oyster::Network::NetAttributeType_Bool; + } + const Protocol_PlayerShot& operator=(Oyster::Network::CustomNetProtocol& val) + { + hasShot = val[1].value.netBool; + return *this; + } + Oyster::Network::CustomNetProtocol* GetProtocol() override + { + this->protocol[1].value = hasShot; + return &protocol; + } + + private: + Oyster::Network::CustomNetProtocol protocol; + }; + } #endif // !GAMELOGIC_PLAYER_PROTOCOLS_H diff --git a/Code/Game/GameProtocols/ProtocolIdentificationID.h b/Code/Game/GameProtocols/ProtocolIdentificationID.h index 7b742eb9..0bb902c6 100644 --- a/Code/Game/GameProtocols/ProtocolIdentificationID.h +++ b/Code/Game/GameProtocols/ProtocolIdentificationID.h @@ -47,14 +47,15 @@ /***********[ 300 - 399 ]***********/ #define protocol_GameplayMIN 300 #define protocol_Gameplay_PlayerMovement 300 -#define protocol_Gameplay_PlayerMouseMovement 301 +#define protocol_Gameplay_PlayerLookDir 301 #define protocol_Gameplay_PlayerChangeWeapon 302 -#define protocol_Gameplay_ObjectPickup 303 -#define protocol_Gameplay_ObjectDamage 304 -#define protocol_Gameplay_ObjectPosition 305 -#define protocol_Gameplay_ObjectEnabled 306 -#define protocol_Gameplay_ObjectDisabled 307 -#define protocol_Gameplay_ObjectCreate 308 +#define protocol_Gameplay_PlayerShot 303 +#define protocol_Gameplay_ObjectPickup 304 +#define protocol_Gameplay_ObjectDamage 305 +#define protocol_Gameplay_ObjectPosition 306 +#define protocol_Gameplay_ObjectEnabled 307 +#define protocol_Gameplay_ObjectDisabled 308 +#define protocol_Gameplay_ObjectCreate 309 #define protocol_GameplayMAX 399 diff --git a/Code/Game/GameServer/GameClient.h b/Code/Game/GameServer/GameClient.h index 3fa0a6f6..241f13ca 100644 --- a/Code/Game/GameServer/GameClient.h +++ b/Code/Game/GameServer/GameClient.h @@ -7,26 +7,28 @@ #include #include #include +#include namespace DanBias { + /** + * Container to keep logic player and network client together as a unit. + */ class GameClient { public: - GameClient(Oyster::Network::NetworkClient client, GameLogic::IPlayerData* player); + GameClient(Utility::DynamicMemory::SmartPointer client, GameLogic::IPlayerData* player); virtual~GameClient(); - //void SetCallback(Oyster::Callback::OysterCallback value); - GameLogic::IPlayerData* GetPlayer(); GameLogic::IPlayerData* ReleasePlayer(); - Oyster::Network::NetworkClient* GetClient(); - Oyster::Network::NetworkClient ReleaseClient(); + Utility::DynamicMemory::SmartPointer GetClient(); + Utility::DynamicMemory::SmartPointer ReleaseClient(); int GetID() const; private: GameLogic::IPlayerData* player; - Oyster::Network::NetworkClient client; + Utility::DynamicMemory::SmartPointer client; int id; }; diff --git a/Code/Game/GameServer/GameLobby.h b/Code/Game/GameServer/GameLobby.h index 7a47638d..1391641e 100644 --- a/Code/Game/GameServer/GameLobby.h +++ b/Code/Game/GameServer/GameLobby.h @@ -8,6 +8,7 @@ #include #include #include +#include "GameSession.h" namespace DanBias { @@ -16,33 +17,32 @@ namespace DanBias public: GameLobby(); virtual~GameLobby(); - void Release(); + void Update(); - void Frame(); - - //void ClientEventCallback(NetEvent e) override; + operator bool(); private: - void ParseEvents(); - void ParseGeneralProtocol(Oyster::Network::CustomNetProtocol& p, Oyster::Network::NetworkClient* c); - void ParseLobbyProtocol(Oyster::Network::CustomNetProtocol& p, Oyster::Network::NetworkClient* c); + void ParseProtocol(Oyster::Network::CustomNetProtocol& p, Oyster::Network::NetworkClient* c); - //Lobby events - void LobbyStartGame(GameLogic::Protocol_LobbyStartGame& p, Oyster::Network::NetworkClient* c); - void LobbyRefresh(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c); - - //General events - void GeneralStatus(GameLogic::Protocol_General_Status& p, Oyster::Network::NetworkClient* c); - void GeneralText(GameLogic::Protocol_General_Text& p, Oyster::Network::NetworkClient* c); + void GeneralStatus(GameLogic::Protocol_General_Status& p, Oyster::Network::NetworkClient* c); //id = protocol_General_Status: + 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 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: private: void ClientEventCallback(Oyster::Network::NetEvent e) override; - void ClientConnectedEvent(Oyster::Network::NetEvent e) override; + void ClientConnectedEvent(Utility::DynamicMemory::SmartPointer client) override; private: Utility::WinTimer timer; float refreshFrequency; + GameSession* gameSession; }; }//End namespace DanBias #endif // !DANBIASGAME_GAMELOBBY_H diff --git a/Code/Game/GameServer/GameServer.h b/Code/Game/GameServer/GameServer.h deleted file mode 100644 index 73857b2d..00000000 --- a/Code/Game/GameServer/GameServer.h +++ /dev/null @@ -1,33 +0,0 @@ -///////////////////////////////////////////////////////////////////// -// Created by [Dennis Andersen] [2013] -///////////////////////////////////////////////////////////////////// -#ifndef DANBIASSERVER_GAME_SERVER_H -#define DANBIASSERVER_GAME_SERVER_H - -#include "GameServerAPI.h" -#include "GameLobby.h" -#include -#include - -namespace DanBias -{ - class GameServer - { - public: - GameServer(); - virtual~GameServer(); - - DanBiasServerReturn Create(const GameServerAPI::GameInitDesc& desc); - - private: - static void Run(GameServer* owner); - void Run(); - void Release(); - - private: - int maxClients; - Utility::DynamicMemory::SmartPointer lobby; - Utility::DynamicMemory::SmartPointer server; - }; -}// End namspace DanBias -#endif // !DANBIASSERVER_DBSERVER_H diff --git a/Code/Game/GameServer/GameServer.vcxproj b/Code/Game/GameServer/GameServer.vcxproj index e736a111..6c0f3ea9 100644 --- a/Code/Game/GameServer/GameServer.vcxproj +++ b/Code/Game/GameServer/GameServer.vcxproj @@ -178,31 +178,20 @@ - - - - - - true - true - true - true - - - true - true - true - true + + + false + false + false + false - - diff --git a/Code/Game/GameServer/GameServerAPI.h b/Code/Game/GameServer/GameServerAPI.h index 08e25114..cc03ec01 100644 --- a/Code/Game/GameServer/GameServerAPI.h +++ b/Code/Game/GameServer/GameServerAPI.h @@ -20,6 +20,7 @@ namespace DanBias { DanBiasServerReturn_Error, DanBiasServerReturn_Sucess, + DanBiasServerReturn_GameNotCreated, }; extern "C" @@ -29,13 +30,16 @@ namespace DanBias public: struct GameInitDesc { - //stuff - int connectionPort; - int maxNumberOfClients; + int listenPort; bool threaded; }; + public: static DanBiasServerReturn Create(const GameInitDesc& desc); + static void Start(); + static void Stop(); + static void Terminate(); + };//End class DanBiasServer }//End Extern "C" } //End namspace DanBias diff --git a/Code/Game/GameServer/GameSession.h b/Code/Game/GameServer/GameSession.h index eed2b4b4..ed480c35 100644 --- a/Code/Game/GameServer/GameSession.h +++ b/Code/Game/GameServer/GameSession.h @@ -15,20 +15,24 @@ #include #include #include +#include + namespace DanBias { - class GameSession : public Oyster::Thread::IThreadObject + class GameSession : public Oyster::Network::NetworkSession + , public Oyster::Thread::IThreadObject { public: + /** * A container to use when initiating a new session */ struct GameDescription { std::wstring mapName; - NetworkSession* owner; - Utility::DynamicMemory::DynamicArray clients; + Oyster::Network::NetworkSession* owner; + Utility::DynamicMemory::DynamicArray clients; }; public: @@ -44,45 +48,28 @@ namespace DanBias /** Join an existing/running game session * @param client The client to attach to the session */ - bool Join(Utility::DynamicMemory::SmartPointer client); + bool Attach(Oyster::Network::NetClient client) override; - /** - * Closes the game session - * @param disconnectClients If set to true clients is dissconnected from the server, if false the clients is sent to the given owner of the session. - */ - void CloseSession(bool disconnectClients = false); - inline bool IsCreated() const { return this->isCreated; } inline bool IsRunning() const { return this->isRunning; } //Private member functions private: //Handles all events recieved - void ParseEvents(); - //Handles all gameplay events - void ParseGameplayEvent(Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c); - //Handles all general events - void ParseGeneralEvent(Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c); - //Adds a client to the client list - void InsertClient(Utility::DynamicMemory::SmartPointer obj); - //Removes a client from the client list - void RemoveClient(DanBias::GameClient* obj); - //Sends a protocol ta all clients in session - void Send(Oyster::Network::CustomNetProtocol* p); - //Frame function, derived from IThreadObject - bool DoWork ( ) override; + //void ParseEvents(); + + 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); - //Do a cleanup on all the private data - void Clean(); - //Update game objects if needed - void UpdateGameObjects(); - + + //Frame function, derived from IThreadObject + bool DoWork ( ) override; + //Private member variables private: - Utility::DynamicMemory::DynamicArray> clients; - //Oyster::PostBox *box; Oyster::Thread::OysterThread worker; GameLogic::GameAPI& gameInstance; GameLogic::ILevelData *levelData; diff --git a/Code/Game/GameServer/GameSessionManager.h b/Code/Game/GameServer/GameSessionManager.h deleted file mode 100644 index a2c8a3ee..00000000 --- a/Code/Game/GameServer/GameSessionManager.h +++ /dev/null @@ -1,82 +0,0 @@ -///////////////////////////////////////////////////////////////////// -// Created by [Dennis Andersen] [2013] -///////////////////////////////////////////////////////////////////// -#ifndef DANBIASSERVER_GAME_SEESION_MANAGER_H -#define DANBIASSERVER_GAME_SEESION_MANAGER_H - -#include -#include -#include -#include - -using namespace Oyster::Network; - -namespace DanBias -{ - 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); - - /** - * Close all sessions. - */ - static void CloseSessions(); - - /** - * 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/GameServer/Implementation/GameClient.cpp b/Code/Game/GameServer/Implementation/GameClient.cpp index 94ac807a..07999205 100644 --- a/Code/Game/GameServer/Implementation/GameClient.cpp +++ b/Code/Game/GameServer/Implementation/GameClient.cpp @@ -7,12 +7,13 @@ #include using namespace Utility::DynamicMemory; +using namespace Oyster::Network; using namespace DanBias; using namespace GameLogic; static int gameClientIDCount = 1; -GameClient::GameClient(Oyster::Network::NetworkClient client, GameLogic::IPlayerData* player) +GameClient::GameClient(SmartPointer client, GameLogic::IPlayerData* player) { this->client = client; this->id = gameClientIDCount++; @@ -20,7 +21,7 @@ GameClient::GameClient(Oyster::Network::NetworkClient client, GameLogic::IPlayer } GameClient::~GameClient() { - this->client.Disconnect(); + this->client->Disconnect(); this->player = 0; this->id = -1; } @@ -35,13 +36,14 @@ GameLogic::IPlayerData* GameClient::ReleasePlayer() this->player = 0; return temp; } -Oyster::Network::NetworkClient* GameClient::GetClient() +SmartPointer GameClient::GetClient() { - return &this->client; + return this->client; } -Oyster::Network::NetworkClient GameClient::ReleaseClient() +SmartPointer GameClient::ReleaseClient() { - Oyster::Network::NetworkClient temp = this->client; + SmartPointer temp = this->client; + this->client = 0; return temp; } int GameClient::GetID() const diff --git a/Code/Game/GameServer/Implementation/GameLobby.cpp b/Code/Game/GameServer/Implementation/GameLobby.cpp index ad3de1c7..cd90793a 100644 --- a/Code/Game/GameServer/Implementation/GameLobby.cpp +++ b/Code/Game/GameServer/Implementation/GameLobby.cpp @@ -18,34 +18,37 @@ namespace DanBias { } void GameLobby::Release() - { } + { + NetworkSession::CloseSession(true); + } - void GameLobby::Frame() + void GameLobby::Update() { - ParseEvents(); + this->ProcessClients(); + } + GameLobby::operator bool() + { + return true; } void GameLobby::ClientEventCallback(NetEvent e) { - + switch (e.args.type) + { + case NetworkClient::ClientEventArgs::EventType_Disconnect: + break; + case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToRecieve: + break; + case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToSend: + break; + case NetworkClient::ClientEventArgs::EventType_ProtocolRecieved: + this->ParseProtocol(e.args.data.protocol, e.sender); + break; + } } - void GameLobby::ClientConnectedEvent(NetEvent e) + void GameLobby::ClientConnectedEvent(Utility::DynamicMemory::SmartPointer client) { - - } - -//////// Private - void GameLobby::ParseEvents() - { - //if(this->box && !this->box->IsEmpty()) - //{ - // NetEvent &e = this->box->Fetch(); - // - // short type = e.protocol[0].value.netShort; - // - // if(ProtocolIsLobby(type)) ParseLobbyProtocol(e.protocol, e.sender); - // else if(ProtocolIsGeneral(type)) ParseGeneralProtocol(e.protocol, e.sender); - //} + //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 new file mode 100644 index 00000000..45ec4dfa --- /dev/null +++ b/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp @@ -0,0 +1,100 @@ +#include "..\GameLobby.h" + +using namespace DanBias; +using namespace GameLogic; +using namespace Oyster::Network; + + +void GameLobby::ParseProtocol(Oyster::Network::CustomNetProtocol& p, NetworkClient* c) +{ + 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: + break; + case protocol_General_Text: + break; + case protocol_Lobby_Create: + break; + case protocol_Lobby_Start: + break; + case protocol_Lobby_Join: + break; + case protocol_Lobby_Login: + break; + case protocol_Lobby_Refresh: + break; + case protocol_Lobby_MainData: + break; + case protocol_Lobby_GameData: + break; + } +} + + +void GameLobby::GeneralStatus(GameLogic::Protocol_General_Status& p, Oyster::Network::NetworkClient* c) +{ + switch (p.status) + { + case Protocol_General_Status::States_ready: + { + + } + case Protocol_General_Status::States_idle: + { + + } + case Protocol_General_Status::States_leave: + case Protocol_General_Status::States_disconected: + { + Detach(c)->Disconnect(); + } + } +} +void GameLobby::GeneralText(GameLogic::Protocol_General_Text& p, Oyster::Network::NetworkClient* c) +{ + printf(p.text.c_str()); +} +void GameLobby::LobbyCreateGame(GameLogic::Protocol_LobbyCreateGame& p, Oyster::Network::NetworkClient* c) +{ + +} +void GameLobby::LobbyStartGame(GameLogic::Protocol_LobbyStartGame& p, Oyster::Network::NetworkClient* c) +{ + +} +void GameLobby::LobbyJoin(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c) +{ + //for (unsigned int i = 0; i < this->gameLobby.Size(); i++) + //{ + // if (this->gameLobby[i]->GetID() == p.value) + // { + // this->gameLobby[i]->Attach(Detach(c)); + // return; + // } + //} +} +void GameLobby::LobbyLogin(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c) +{ + +} +void GameLobby::LobbyRefresh(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c) +{ + //Dont need to handle this on the server... +} +void GameLobby::LobbyMainData(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c) +{ + +} +void GameLobby::LobbyGameData(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c) +{ + +} + + diff --git a/Code/Game/GameServer/Implementation/GameServer.cpp b/Code/Game/GameServer/Implementation/GameServer.cpp index 90620c18..ac2abc8a 100644 --- a/Code/Game/GameServer/Implementation/GameServer.cpp +++ b/Code/Game/GameServer/Implementation/GameServer.cpp @@ -3,137 +3,93 @@ ///////////////////////////////////////////////////////////////////// #define NOMINMAX #include -#include #include +#include - -#include "..\GameServer.h" -#include "..\GameSessionManager.h" +#include "..\GameServerAPI.h" +#include "..\GameLobby.h" #include "..\GameSession.h" -#include -#include +#include #include -#include -#include -namespace DanBias +#include +#include +#include +#include + +using namespace DanBias; +using namespace Oyster::Network; +using namespace Oyster::Thread; +using namespace Utility; + +namespace { - using namespace Oyster::Network; + GameLobby lobby; + NetworkServer server; + WinTimer timer; + GameServerAPI instance; + //typedef void(*WorkerThreadFnc)(GameServerAPI*); +} - GameServer* instance = 0; - std::thread workerThread; - typedef void(*WorkerThreadFnc)(GameServer*); - DanBiasServerReturn GameServerAPI::Create(const GameInitDesc& desc) +DanBiasServerReturn GameServerAPI::Create(const GameInitDesc& desc) +{ + + if(server.Init(desc.listenPort, &lobby) == NetworkServer::ServerReturnCode_Error) { - if(!instance) - instance = new GameServer(); - - return instance->Create(desc); + return DanBiasServerReturn_Error; } + std::printf("Server created!\t-\t%s: [%i]\n", server.GetLanAddress().c_str(), desc.listenPort); + + return DanBiasServerReturn_Sucess; +} +void GameServerAPI::Start() +{ + server.Start(); + + timer.reset(); + + while (true) + { + int c = server.ProcessConnectedClients(); + if(c > 0) printf(" - [%i] client(s) connected!\n", c); + lobby.Update(); + + if(GetAsyncKeyState(0x51)) //Q for exit + break; + } + + double total = timer.getElapsedSeconds(); + int time = (int)total; + int hour, min, sec; + + hour=time / 3600; + time=time % 3600; + min=time / 60; + time=time % 60; + sec = time; + + printf( "Server has been running for: %i:%i:%i - [hh:mm:ss] \n\n", hour, min, sec ); + printf( "Terminating in : "); + for (int i = 0; i < 4; i++) + { + printf( "%i ", 3-i ); + Sleep(1000); + } +} +void GameServerAPI::Stop() +{ + server.Stop(); + lobby.ProcessClients(); +} +void GameServerAPI::Terminate() +{ + lobby.Release(); + server.Shutdown(); - GameServer::GameServer() - : maxClients(0) - { } - GameServer::~GameServer() - { } - DanBiasServerReturn GameServer::Create(const GameServerAPI::GameInitDesc& desc) - { - this->maxClients = desc.maxNumberOfClients; - this->server = new NetworkServer(); - this->lobby = new GameLobby(); + printf( "Server terminated!" ); - if(desc.threaded) - { - if(!this->server->Init(desc.connectionPort, this->lobby)) - return DanBiasServerReturn_Error; - - if(!this->server->Start()) - return DanBiasServerReturn_Error; - - WorkerThreadFnc temp = GameServer::Run; - workerThread = std::thread(temp, this); - } - else - { - if(!this->server->Init(desc.connectionPort, this->lobby)) - return DanBiasServerReturn_Error; - - if(!this->server->Start()) - return DanBiasServerReturn_Error; - - Run(); - } - - return DanBiasServerReturn_Sucess; - } - void GameServer::Run(GameServer* owner) - { - while (true) - { - owner->server->ProcessConnectedClients(); - owner->lobby->ProcessClients(); - - if(GetAsyncKeyState(0x51)) //Q for exit - break; - } - } - void GameServer::Run() - { - while (true) - { - this->server->ProcessConnectedClients(); - this->lobby->Frame(); - - if(GetAsyncKeyState(0x51)) //Q for exit - break; - } - } - void GameServer::Release() - { - GameSessionManager::CloseSessions(); - } - - - - - //void GameServer::ClientConnected(NetworkClient* client) - //{ - // static bool myTest = false; - // static int sessionId = -1; - // printf("Client with ID [%i] connected.\n", client->GetID()); - // - // if(!myTest) - // { - // Utility::DynamicMemory::SmartPointer c = new Client(client); - // - // GameSessionDescription desc; - // desc.mapName = L"test"; - // desc.clients.Push(c); - // desc.exitDestionation = this->lobby; - // if((sessionId = GameSessionManager::AddSession(desc, true)) == 0) - // printf("Failed to create a game session\n"); - // myTest = true; - // //myTest = new GameSession(); - // // - // //DanBias::GameSession::GameSessionDescription desc; - // //desc.owner = 0; - // //desc.clients.Push(c); - // // - // //if(!myTest->Create(desc)) return; - // //myTest->Run(); - // } - // else - // { - // Utility::DynamicMemory::SmartPointer c = new NetworkSession(client); - // GameSessionManager::JoinSession(sessionId, c); - // } - // - // - // //Utility::DynamicMemory::SmartPointer c = new NetworkSession(client); - // //this->mainLobby->Attach(c, this->mainLobby->GetPostbox()); - //} -}//End namespace DanBias +} diff --git a/Code/Game/GameServer/Implementation/GameSessionManager.cpp b/Code/Game/GameServer/Implementation/GameSessionManager.cpp deleted file mode 100644 index 5eb8d664..00000000 --- a/Code/Game/GameServer/Implementation/GameSessionManager.cpp +++ /dev/null @@ -1,121 +0,0 @@ -///////////////////////////////////////////////////////////////////// -// Created by [Dennis Andersen] [2013] -///////////////////////////////////////////////////////////////////// -#include "..\GameSessionManager.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::CloseSessions() -{ - for (unsigned int i = 0; i < gameSessionData.sessions.Size(); i++) - { - gameSessionData.sessions[i]->CloseSession(); - } -} -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/GameServer/Implementation/GameSession_Events.cpp b/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp similarity index 57% rename from Code/Game/GameServer/Implementation/GameSession_Events.cpp rename to Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp index b3b831f6..ef1898be 100644 --- a/Code/Game/GameServer/Implementation/GameSession_Events.cpp +++ b/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp @@ -4,14 +4,18 @@ #include "..\GameSession.h" #include "..\GameClient.h" -#include #include #include #include #include - +#define NOMINMAX #include +#define DELTA_TIME_20 0.05f +#define DELTA_TIME_24 0.04166666666666666666666666666667f +#define DELTA_TIME_30 0.03333333333333333333333333333333f +#define DELTA_TIME_60 0.01666666666666666666666666666667f +#define DELTA_TIME_120 0.00833333333333333333333333333333f using namespace Utility::DynamicMemory; using namespace Oyster; @@ -21,25 +25,45 @@ using namespace GameLogic; namespace DanBias { - void GameSession::ParseEvents() + bool GameSession::DoWork( ) { - if( !this->box->IsEmpty() ) + if(this->isRunning) { - NetworkSession::NetEvent &e = this->box->Fetch(); - static int ii = 0; - printf("%i - Message recieved! [%i]\n", ii++, e.protocol[0].value); + double dt = this->timer.getElapsedSeconds(); + gameInstance.SetFrameTimeLength((float)dt); - if(e.protocol[0].type != Oyster::Network::NetAttributeType_Short) return; + if(dt >= DELTA_TIME_20) + { + this->ProcessClients(); - if( ProtocolIsGameplay(e.protocol[protocol_INDEX_ID].value.netShort) ) - ParseGameplayEvent(e.protocol, e.gameClient); + this->gameInstance.NewFrame(); - if( ProtocolIsGeneral(e.protocol[protocol_INDEX_ID].value.netShort) ) - ParseGeneralEvent(e.protocol, e.gameClient); + this->timer.reset(); + } } + + return this->isRunning; } - void GameSession::ParseGameplayEvent(Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c) + //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::ParseProtocol(Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c) { switch (p[protocol_INDEX_ID].value.netShort) { @@ -55,10 +79,10 @@ namespace DanBias c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_RIGHT); } break; - case protocol_Gameplay_PlayerMouseMovement: + case protocol_Gameplay_PlayerLookDir: { - Protocol_PlayerMouse m; m = p; - c->GetPlayer()->Rotate(m.dxMouse, m.dyMouse); + Protocol_PlayerLook m; m = p; + //c->GetPlayer()->Rotate(m.dxMouse, m.dyMouse); } break; case protocol_Gameplay_PlayerChangeWeapon: @@ -67,19 +91,12 @@ namespace DanBias case protocol_Gameplay_ObjectDamage: 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_disconected: printf("Client with ID [%i] dissconnected\n", c->GetClient()->GetID()); - this->RemoveClient(c); + this->Detach(c->GetClient()->GetID()); break; case GameLogic::Protocol_General_Status::States_idle: @@ -101,17 +118,18 @@ namespace DanBias printf("Message recieved from (%i):\t %s\n", c->GetID(), temp.text.c_str()); } break; - } } - void GameSession::ObjectMove(GameLogic::IObjectData* movedObject) { movedObject->GetID(); movedObject->GetOrientation(); - } + void GameSession::ClientEventCallback(NetEvent e) + { + + } }//End namespace DanBias diff --git a/Code/Game/GameServer/Implementation/GameSession_General.cpp b/Code/Game/GameServer/Implementation/GameSession_General.cpp index e68ac8dc..306837d9 100644 --- a/Code/Game/GameServer/Implementation/GameSession_General.cpp +++ b/Code/Game/GameServer/Implementation/GameSession_General.cpp @@ -3,11 +3,11 @@ ///////////////////////////////////////////////////////////////////// #include "..\GameSession.h" #include "..\GameClient.h" -#include "..\GameServer.h" #include #include #include +#define NOMINMAX #include @@ -23,16 +23,18 @@ namespace DanBias :gameInstance(GameAPI::Instance()) { this->owner = 0; - this->box = 0; this->isCreated = false; this->isRunning = false; } GameSession::~GameSession() { - delete this->box; - this->box = 0; + this->worker.Terminate(); + this->clients.Clear(); + this->gameInstance; this->owner = 0; + this->isCreated = false; + this->isRunning = false; } bool GameSession::Create(GameDescription& desc) @@ -45,7 +47,6 @@ namespace DanBias /* standard initialization of some data */ this->clients.Resize(desc.clients.Size()); - this->box = new PostBox(); this->owner = desc.owner; /* Initiate the game instance */ @@ -61,18 +62,13 @@ namespace DanBias return false; } - /* Create a callback object */ - Oyster::Callback::OysterCallback c; - c.value.callbackPostBox = this->box; - c.callbackType = Oyster::Callback::CallbackType_PostBox; - /* Create the players in the game instance */ GameLogic::IPlayerData* p = 0; for (unsigned int i = 0; i < desc.clients.Size(); i++) { if( (p = this->gameInstance.CreatePlayer()) ) { - this->clients[i] = new GameClient(desc.clients[i], p, c); + this->clients[i] = new GameClient(desc.clients[i], p); } else { @@ -81,12 +77,12 @@ namespace DanBias } /* Create the worker thread */ - if(this->worker.Create(this, true, true) != OYSTER_THREAD_ERROR_SUCCESS) + if(this->worker.Create(this, false) != OYSTER_THREAD_ERROR_SUCCESS) return false; this->worker.SetPriority(Oyster::Thread::OYSTER_THREAD_PRIORITY_3); - /* Set some gameinstance data options */ + /* Set some game instance data options */ this->gameInstance.SetSubscription(GameLogic::GameEvent::ObjectEventFunctionType_OnMove, GameSession::ObjectMove); this->isCreated = true; @@ -95,106 +91,39 @@ namespace DanBias void GameSession::Run() { - if(this->isRunning) return; if(this->clients.Size() > 0) { + this->worker.Start(); this->worker.SetPriority(OYSTER_THREAD_PRIORITY_1); this->isRunning = true; } } - bool GameSession::Join(Utility::DynamicMemory::SmartPointer client) + + + bool GameSession::Attach(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, this->gameInstance.CreatePlayer(), c); - InsertClient(obj); - - return true; - } - - void GameSession::CloseSession(bool dissconnectClients) - { - if(dissconnectClients) - { - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - this->clients[i]->GetClient()->Disconnect(); - } - } - else - { - this->SendToOwner(0); //Send all clients to the current owner - } - this->Clean(); - } - - void GameSession::InsertClient(SmartPointer obj) - { + client->SetOwner(this); + SmartPointer obj = new GameClient(client, this->gameInstance.CreatePlayer()); + for (unsigned int i = 0; i < clients.Size(); i++) { if(!clients[i]) { clients[i] = obj; - return; + return true; } } + clients.Push(obj); + + return true; } - void GameSession::RemoveClient(DanBias::GameClient* obj) - { - for (unsigned int i = 0; i < clients.Size(); i++) - { - if(clients[i] && clients[i]->GetID() == obj->GetID()) - { - clients[i] = 0; - return; - } - } - } - - void GameSession::SendToOwner(DanBias::GameClient* obj) - { - DanBias::NetworkSession *s = GameServer::MainLobbyInstance(); - - if(this->owner) s = this->owner; - - if(obj) - { - s->Attach(obj->ReleaseClient()); - RemoveClient(obj); - } - else - { - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - if(this->clients[i]) - { - s->Attach(this->clients[i]->ReleaseClient()); - RemoveClient(this->clients[i]); - } - } - } - } - - void GameSession::Clean() - { - this->worker.Terminate(); - this->clients.Clear(); - delete this->box; - this->box = 0; - this->gameInstance; - this->owner = 0; - this->isCreated = false; - this->isRunning = false; - } }//End namespace DanBias diff --git a/Code/Game/GameServer/Implementation/GameSession_Logic.cpp b/Code/Game/GameServer/Implementation/GameSession_Logic.cpp deleted file mode 100644 index 1388f6fe..00000000 --- a/Code/Game/GameServer/Implementation/GameSession_Logic.cpp +++ /dev/null @@ -1,74 +0,0 @@ -///////////////////////////////////////////////////////////////////// -// Created by [Dennis Andersen] [2013] -///////////////////////////////////////////////////////////////////// -#include "..\GameSession.h" -#include "..\GameClient.h" - -#include -#include -#include -#include -#include - -#define DELTA_TIME_20 0.05f -#define DELTA_TIME_24 0.04166666666666666666666666666667f -#define DELTA_TIME_30 0.03333333333333333333333333333333f -#define DELTA_TIME_60 0.01666666666666666666666666666667f -#define DELTA_TIME_120 0.00833333333333333333333333333333f - -using namespace Utility::DynamicMemory; -using namespace Oyster; -using namespace Oyster::Network; -using namespace Oyster::Thread; -using namespace GameLogic; - -namespace DanBias -{ - bool GameSession::DoWork( ) - { - if(this->isRunning) - { - if(GetAsyncKeyState(VK_UP)) - { - Protocol_General_Status p(Protocol_General_Status::States_ready); - Send(p.GetProtocol()); - Sleep(100); - } - if(GetAsyncKeyState(VK_DOWN)) - { - Oyster::Math::Float4x4 world = Oyster::Math::Matrix::identity; - Protocol_ObjectCreate p(world, 2, "../Content/crate"); - Send(p.GetProtocol()); - Sleep(100); - } - - double dt = this->timer.getElapsedSeconds(); - gameInstance.SetFrameTimeLength((float)dt); - - if(dt >= DELTA_TIME_20) - { - this->ParseEvents(); - - this->gameInstance.NewFrame(); - - this->UpdateGameObjects(); - - this->timer.reset(); - } - } - - return this->isRunning; - } - - void GameSession::UpdateGameObjects() - { - if(clients.Size() >= 1 && clients[0]) - { - Oyster::Math::Float4x4 world = this->clients[0]->GetPlayer()->GetOrientation(); - Protocol_ObjectPosition p(world, 1); - Send(p.GetProtocol()); - } - } - -}//End namespace DanBias - diff --git a/Code/Game/GameServer/Implementation/GameSession_Network.cpp b/Code/Game/GameServer/Implementation/GameSession_Network.cpp deleted file mode 100644 index df546b35..00000000 --- a/Code/Game/GameServer/Implementation/GameSession_Network.cpp +++ /dev/null @@ -1,29 +0,0 @@ -///////////////////////////////////////////////////////////////////// -// 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 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); - } - } -}//End namespace DanBias - diff --git a/Code/Game/GameServer/Implementation/LobbyGeneralProtocolParser.cpp b/Code/Game/GameServer/Implementation/LobbyGeneralProtocolParser.cpp deleted file mode 100644 index 23195087..00000000 --- a/Code/Game/GameServer/Implementation/LobbyGeneralProtocolParser.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include "GameLobby.h" -#include "NetworkSession.h" - -using namespace DanBias; -using namespace GameLogic; - -void GameLobby::ParseGeneralProtocol(Oyster::Network::CustomNetProtocol& p, DanBias::NetworkSession* c) -{ - switch (p[0].value.netShort) - { - case protocol_General_Status: - { - GeneralStatus(GameLogic::Protocol_General_Status(p), c); - } break; - case protocol_General_Text: - { - GeneralText(GameLogic::Protocol_General_Text(p), c); - } break; - } -} - -////////////////////////////////////////////////////// - -void GameLobby::GeneralStatus(GameLogic::Protocol_General_Status& p, DanBias::NetworkSession* c) -{ - switch (p.status) - { - case Protocol_General_Status::States_ready: - { - - } - case Protocol_General_Status::States_idle: - { - - } - case Protocol_General_Status::States_leave: - { - - } - case Protocol_General_Status::States_disconected: - { - Detach(c)->Disconnect(); - } - } -} - -void GameLobby::GeneralText(GameLogic::Protocol_General_Text& p, DanBias::NetworkSession* c) -{ - -} - - diff --git a/Code/Game/GameServer/Implementation/LobbyProtocolParser.cpp b/Code/Game/GameServer/Implementation/LobbyProtocolParser.cpp deleted file mode 100644 index 4063db18..00000000 --- a/Code/Game/GameServer/Implementation/LobbyProtocolParser.cpp +++ /dev/null @@ -1,58 +0,0 @@ -#include "..\GameLobby.h" - -using namespace DanBias; - -void GameLobby::ParseLobbyProtocol(Oyster::Network::CustomNetProtocol& p, DanBias::NetworkSession* c) -{ - switch (p[0].value.netShort) - { - case protocol_Lobby_Start: - LobbyStartGame(GameLogic::Protocol_LobbyStartGame(p), c); - break; - - case protocol_Lobby_Refresh: - LobbyRefresh(GameLogic::Protocol_LobbyRefresh(p), c); - break; - case protocol_Lobby_Login: - { - LobbyLogin(GameLogic::Protocol_LobbyLogin(p), c); - } break; - case protocol_Lobby_Join: - { - LobbyJoin(GameLogic::Protocol_LobbyJoin(p), c); - } break; - } -} - -void GameLobby::LobbyStartGame(GameLogic::Protocol_LobbyStartGame& p, DanBias::NetworkSession* c) -{ - -} - -void GameLobby::LobbyRefresh(GameLogic::Protocol_LobbyRefresh& p, DanBias::NetworkSession* c) -{ - double now = this->timer.getElapsedSeconds() + c->lastPoll; - - if(now > this->refreshFrequency) - { - c->lastPoll = (float)now; - } -} - -void GameLobby::LobbyLogin(GameLogic::Protocol_LobbyLogin& p, DanBias::NetworkSession* c) -{ - -} - -void GameLobby::LobbyJoin(GameLogic::Protocol_LobbyJoin& p, DanBias::NetworkSession* c) -{ - for (unsigned int i = 0; i < this->gameLobby.Size(); i++) - { - if (this->gameLobby[i]->GetID() == p.value) - { - this->gameLobby[i]->Attach(Detach(c)); - return; - } - } -} - diff --git a/Code/Game/aDanBiasGameLauncher/Launcher.cpp b/Code/Game/aDanBiasGameLauncher/Launcher.cpp index 7e533171..17e3c8bc 100644 --- a/Code/Game/aDanBiasGameLauncher/Launcher.cpp +++ b/Code/Game/aDanBiasGameLauncher/Launcher.cpp @@ -4,18 +4,19 @@ #include #include "DanBiasGame.h" -#include +#include #include void ServerFnc() { - - if( DanBias::DanBiasServerAPI::Initiate() == DanBias::DanBiasServerReturn_Sucess) + DanBias::GameServerAPI::GameInitDesc desc; + desc.listenPort = 15151; + if( DanBias::GameServerAPI::Create(desc) == DanBias::DanBiasServerReturn_Sucess) { - DanBias::DanBiasServerAPI::Run(); - DanBias::DanBiasServerAPI::Release(); + DanBias::GameServerAPI::Start(); + DanBias::GameServerAPI::Terminate(); } Sleep(100); } diff --git a/Code/Game/aDanBiasGameLauncher/aDanBiasGameLauncher.vcxproj b/Code/Game/aDanBiasGameLauncher/aDanBiasGameLauncher.vcxproj index 5f33ada6..303d075f 100644 --- a/Code/Game/aDanBiasGameLauncher/aDanBiasGameLauncher.vcxproj +++ b/Code/Game/aDanBiasGameLauncher/aDanBiasGameLauncher.vcxproj @@ -71,7 +71,7 @@ $(SolutionDir)..\Bin\Executable\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName)D - $(SolutionDir)..\External\Include\;C:\Program Files %28x86%29\Visual Leak Detector\include;C:\Users\Dennis\Desktop\Skola\DV1477 - Stort spelutvecklingsprojekt\DanBias\Code\Game\DanBiasServer;$(IncludePath) + $(SolutionDir)..\External\Include\;C:\Program Files %28x86%29\Visual Leak Detector\include;C:\Users\Dennis\Desktop\Skola\DV1477 - Stort spelutvecklingsprojekt\DanBias\Code\Game\GameServer;$(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)..\External\Include\;C:\Program Files %28x86%29\Visual Leak Detector\include;C:\Users\Dennis\Desktop\Skola\DV1477 - Stort spelutvecklingsprojekt\DanBias\Code\Game\DanBiasServer;$(IncludePath) + $(SolutionDir)..\External\Include\;C:\Program Files %28x86%29\Visual Leak Detector\include;C:\Users\Dennis\Desktop\Skola\DV1477 - Stort spelutvecklingsprojekt\DanBias\Code\Game\GameServer;$(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)..\External\Include\;C:\Program Files %28x86%29\Visual Leak Detector\include;C:\Users\Dennis\Desktop\Skola\DV1477 - Stort spelutvecklingsprojekt\DanBias\Code\Game\DanBiasServer;$(IncludePath) + $(SolutionDir)..\External\Include\;C:\Program Files %28x86%29\Visual Leak Detector\include;C:\Users\Dennis\Desktop\Skola\DV1477 - Stort spelutvecklingsprojekt\DanBias\Code\Game\GameServer;$(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)..\External\Include\;C:\Program Files %28x86%29\Visual Leak Detector\include;C:\Users\Dennis\Desktop\Skola\DV1477 - Stort spelutvecklingsprojekt\DanBias\Code\Game\DanBiasServer;$(IncludePath) + $(SolutionDir)..\External\Include\;C:\Program Files %28x86%29\Visual Leak Detector\include;C:\Users\Dennis\Desktop\Skola\DV1477 - Stort spelutvecklingsprojekt\DanBias\Code\Game\GameServer;$(IncludePath) $(OutDir)..\DLL\;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) @@ -110,7 +110,7 @@ Windows true - DanBiasGame_$(PlatformShortName)D.dll;DanBiasServer_$(PlatformShortName)D.dll;%(DelayLoadDLLs) + DanBiasGame_$(PlatformShortName)D.dll;GameServer_$(PlatformShortName)D.dll;%(DelayLoadDLLs) DanBiasGame_$(PlatformShortName)D.lib;DanBiasServer_$(PlatformShortName)D.lib;%(AdditionalDependencies) @@ -126,7 +126,7 @@ Windows true - DanBiasGame_$(PlatformShortName)D.dll;DanBiasServer_$(PlatformShortName)D.dll;%(DelayLoadDLLs) + DanBiasGame_$(PlatformShortName)D.dll;GameServer_$(PlatformShortName)D.dll;%(DelayLoadDLLs) DanBiasGame_$(PlatformShortName)D.lib;%(AdditionalDependencies) @@ -146,7 +146,7 @@ true true true - DanBiasGame_$(PlatformShortName).dll;DanBiasServer_$(PlatformShortName).dll;%(DelayLoadDLLs) + DanBiasGame_$(PlatformShortName).dll;GameServer_$(PlatformShortName).dll;%(DelayLoadDLLs) DanBiasGame_$(PlatformShortName).lib;%(AdditionalDependencies) @@ -166,7 +166,7 @@ true true true - DanBiasGame_$(PlatformShortName).dll;DanBiasServer_$(PlatformShortName).dll;%(DelayLoadDLLs) + DanBiasGame_$(PlatformShortName).dll;GameServer_$(PlatformShortName).dll;%(DelayLoadDLLs) DanBiasGame_$(PlatformShortName).lib;%(AdditionalDependencies) @@ -177,6 +177,9 @@ {2a1bc987-af42-4500-802d-89cd32fc1309} + + {143bd516-20a1-4890-a3e4-f8bfd02220e7} + diff --git a/Code/Misc/Thread/OysterThread_Impl.cpp b/Code/Misc/Thread/OysterThread_Impl.cpp index a09574df..45807e84 100644 --- a/Code/Misc/Thread/OysterThread_Impl.cpp +++ b/Code/Misc/Thread/OysterThread_Impl.cpp @@ -118,15 +118,17 @@ using namespace Utility::DynamicMemory; { SmartPointer data; - PrivateData(){} + PrivateData() + { + data = new RefData(); + } ~PrivateData() { - data.Release(); + data = 0; } OYSTER_THREAD_ERROR Create(ThreadFunction fnc, OwnerContainer worker, bool start, bool detach) { - if(data) return OYSTER_THREAD_ERROR_ThreadAlreadyCreated; - data = new RefData(); + if(!data) data = new RefData(); return data->Create(fnc, worker, start, detach); } OYSTER_THREAD_ERROR Terminate() @@ -205,9 +207,8 @@ using namespace Utility::DynamicMemory; OysterThread::OysterThread() -{ - this->privateData = new PrivateData(); -} + :privateData(0) +{ } OysterThread::OysterThread(const OysterThread& original) { this->privateData = new PrivateData(*original.privateData); @@ -227,9 +228,8 @@ OysterThread::~OysterThread() OYSTER_THREAD_ERROR OysterThread::Create(IThreadObject* worker, bool start, bool detach) { - if(!this->privateData) this->privateData = new PrivateData(); + if(!this->privateData) this->privateData = new PrivateData(); - if(this->privateData->data->isCreated) return OYSTER_THREAD_ERROR_ThreadAlreadyCreated; OwnerContainer c; c.type = Oyster::Callback::CallbackType_Object; c.value = worker; @@ -237,7 +237,7 @@ OYSTER_THREAD_ERROR OysterThread::Create(IThreadObject* worker, bool start, bool } OYSTER_THREAD_ERROR OysterThread::Create(ThreadFnc worker, bool start, bool detach) { - if(!this->privateData) this->privateData = new PrivateData(); + if(!this->privateData) this->privateData = new PrivateData(); OwnerContainer c; c.type = Oyster::Callback::CallbackType_Function; diff --git a/Code/Misc/WinTimer.h b/Code/Misc/WinTimer.h index 73e9091f..60d7a28d 100644 --- a/Code/Misc/WinTimer.h +++ b/Code/Misc/WinTimer.h @@ -7,6 +7,7 @@ #ifndef WINTIMER_H #define WINTIMER_H +#define NOMINMAX #include namespace Utility diff --git a/Code/Network/NetworkAPI/CustomNetProtocol.cpp b/Code/Network/NetworkAPI/CustomNetProtocol.cpp index 6d4ea1bf..2ac6fcf5 100644 --- a/Code/Network/NetworkAPI/CustomNetProtocol.cpp +++ b/Code/Network/NetworkAPI/CustomNetProtocol.cpp @@ -4,36 +4,27 @@ #include "CustomNetProtocol.h" #include #include "Translator.h" +#include "Utilities.h" using namespace Oyster::Network; +using namespace Utility::DynamicMemory; struct CustomNetProtocol::PrivateData { - std::map attributes; + std::map attributes; //...Im an idiot + Utility::DynamicMemory::ReferenceCount *c; PrivateData() - { } - PrivateData( const CustomNetProtocol::PrivateData& o) - { - for (auto i = o.attributes.begin(); i != o.attributes.end(); i++) - { - if(i->second.type == NetAttributeType_CharArray) - { - size_t size = strlen(i->second.value.netCharPtr); - if(size == 0) continue; - - attributes[i->first].value.netCharPtr = new char[size + 1]; - memcpy(&attributes[i->first].value.netCharPtr[0], &i->second.value.netCharPtr[0], size + 1); - attributes[i->first].type = NetAttributeType_CharArray; - } - else - { - attributes[i->first] = i->second; - } - } + { + //this->attributes = new std::map(); + this->c = new ReferenceCount(); + c->Incref(); } + ~PrivateData() { + delete c; + c = 0; for (auto i = attributes.begin(); i != attributes.end(); i++) { RemoveAttribute(i->first); @@ -49,7 +40,6 @@ struct CustomNetProtocol::PrivateData { case NetAttributeType_CharArray: delete [] i->second.value.netCharPtr; - //i->second.value.netCharPtr = 0; break; } } @@ -64,17 +54,40 @@ CustomNetProtocol::CustomNetProtocol() } CustomNetProtocol::CustomNetProtocol(const CustomNetProtocol& o) { - this->privateData = new PrivateData(*o.privateData); + this->privateData = o.privateData; + if(this->privateData) + { + this->privateData->c = o.privateData->c; + this->privateData->c->Incref(); + } } const CustomNetProtocol& CustomNetProtocol::operator=(const CustomNetProtocol& o) { - delete this->privateData; - this->privateData = new PrivateData(*o.privateData); + if(this->privateData && this->privateData->c) + { + if(this->privateData->c->Decref() == 0) + { + delete this->privateData; + } + } + + this->privateData = o.privateData; + if(this->privateData) + { + this->privateData->c = o.privateData->c; + this->privateData->c->Incref(); + } return *this; } CustomNetProtocol::~CustomNetProtocol() { - delete this->privateData; + if(this->privateData && this->privateData->c) + { + if(this->privateData->c->Decref() == 0) + { + delete this->privateData; + } + } } NetAttributeContainer& CustomNetProtocol::operator[](int ID) { diff --git a/Code/Network/NetworkAPI/NetworkAPI.vcxproj b/Code/Network/NetworkAPI/NetworkAPI.vcxproj index b4b8a31f..13ab5af9 100644 --- a/Code/Network/NetworkAPI/NetworkAPI.vcxproj +++ b/Code/Network/NetworkAPI/NetworkAPI.vcxproj @@ -88,16 +88,16 @@ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(SolutionDir)..\Bin\DLL\ $(ProjectName)_$(PlatformShortName) - C:\Program Files %28x86%29\Visual Leak Detector\include;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); - C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(VCInstallDir)lib;$(VCInstallDir)atlmfc\lib;$(WindowsSDK_LibraryPath_x86); + C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) false $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(SolutionDir)..\Bin\DLL\ $(ProjectName)_$(PlatformShortName) - C:\Program Files %28x86%29\Visual Leak Detector\lib\Win64;$(VCInstallDir)lib\amd64;$(VCInstallDir)atlmfc\lib\amd64;$(WindowsSDK_LibraryPath_x64); - C:\Program Files %28x86%29\Visual Leak Detector\include;$(VCInstallDir)include;$(VCInstallDir)atlmfc\include;$(WindowsSDK_IncludePath); + C:\Program Files %28x86%29\Visual Leak Detector\lib\Win64;$(LibraryPath) + C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) @@ -107,6 +107,7 @@ Disabled NETWORKAPI_EXPORT;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) true + $(SolutionDir)Misc\ Console @@ -121,6 +122,7 @@ Disabled NETWORKAPI_EXPORT;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) true + $(SolutionDir)Misc\ Console @@ -137,6 +139,7 @@ true NETWORKAPI_EXPORT;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true + $(SolutionDir)Misc\ Console @@ -155,6 +158,7 @@ true NETWORKAPI_EXPORT;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true + $(SolutionDir)Misc\ Console diff --git a/Code/Network/NetworkAPI/NetworkClient.cpp b/Code/Network/NetworkAPI/NetworkClient.cpp index 71614f77..f5d7c643 100644 --- a/Code/Network/NetworkAPI/NetworkClient.cpp +++ b/Code/Network/NetworkAPI/NetworkClient.cpp @@ -26,8 +26,9 @@ using namespace Utility::Container; PrivateData *************************************/ typedef NetworkClient::ClientEventArgs CEA; -struct NetDataContainer : public IThreadObject -{ //This struct is contained within a smart pointer. To avoide dependencies in link its implemented here.. + +struct NetworkClient::PrivateData : public IThreadObject +{ NetworkSession *owner; NetworkClient *parent; Connection connection; @@ -43,19 +44,17 @@ struct NetDataContainer : public IThreadObject static unsigned int currID; const unsigned int ID; - NetDataContainer() + PrivateData() : ID(currID++) , parent(0) , owner(0) { InitWinSock(); - this->thread.Create(this, true); + this->thread.Create(this, false); this->thread.SetPriority(Oyster::Thread::OYSTER_THREAD_PRIORITY_1); } - NetDataContainer(const NetDataContainer& obj) - :ID(obj.ID) { } - ~NetDataContainer() + ~PrivateData() { ShutdownWinSock(); this->connection.Disconnect(); @@ -73,7 +72,6 @@ struct NetDataContainer : public IThreadObject return true; } - int Send() { int errorCode = 0; @@ -96,7 +94,6 @@ struct NetDataContainer : public IThreadObject return errorCode; } - int Recv() { int errorCode = -1; @@ -119,33 +116,19 @@ struct NetDataContainer : public IThreadObject this->recieveQueue.Push(e); } } - else - { - CEA parg; - parg.type = CEA::EventType_ProtocolFailedToRecieve; - parg.data.nothing = 0; - NetEvent e = { this->parent, parg }; - this->recieveQueue.Push(e); - } + //else + //{ + // CEA parg; + // parg.type = CEA::EventType_ProtocolFailedToRecieve; + // parg.data.nothing = 0; + // NetEvent e = { this->parent, parg }; + // this->recieveQueue.Push(e); + //} return errorCode; } }; - - -struct NetworkClient::PrivateData -{ - SmartPointer dat; - -public: - PrivateData() - { this->dat = new NetDataContainer(); } - PrivateData(const PrivateData& obj) - { this->dat = obj.dat; } - ~PrivateData() - { this->dat = 0; } -}; -unsigned int NetDataContainer::currID = 0; +unsigned int NetworkClient::PrivateData::currID = 0; /************************************* NetworkClient @@ -155,21 +138,6 @@ NetworkClient::NetworkClient() : privateData(0) { } -NetworkClient::NetworkClient(const NetworkClient& obj) -{ - if(obj.privateData) this->privateData = new PrivateData(*obj.privateData); - else this->privateData = 0; -} - -NetworkClient& NetworkClient::operator =(const NetworkClient& obj) -{ - delete privateData; - this->privateData = 0; - if(obj.privateData) this->privateData = new PrivateData(*obj.privateData); - - return *this; -} - NetworkClient::~NetworkClient() { if(this->privateData) @@ -181,22 +149,25 @@ NetworkClient::~NetworkClient() bool NetworkClient::operator ==(const NetworkClient& obj) { - return (this->privateData->dat->ID == obj.privateData->dat->ID); + return (this->privateData->ID == obj.privateData->ID); } bool NetworkClient::operator ==(const int& ID) { - return this->privateData->dat->ID == ID; + return this->privateData->ID == ID; } -void NetworkClient::ProcessMessages() +void NetworkClient::Update() { - while (!this->privateData->dat->recieveQueue.IsEmpty()) + while (!this->privateData->recieveQueue.IsEmpty()) { - if(this->privateData->dat->owner) - { - this->privateData->dat->owner->ClientEventCallback(this->privateData->dat->recieveQueue.Pop()); - } + NetEvent temp = this->privateData->recieveQueue.Pop(); + + this->DataRecieved(temp); + + //--------- Deprecate --------- + this->NetworkCallback(temp.args.data.protocol); + //------------------------------ } } @@ -206,13 +177,16 @@ bool NetworkClient::Connect(int socket) if(this->privateData) return false; if(!this->privateData) this->privateData = new PrivateData(); - int result = this->privateData->dat->connection.Connect(socket, true); + int result = this->privateData->connection.Connect(socket, false); //Connect has succeeded - if(result == 0) return true; + if(result != 0) return false; + + this->privateData->parent = this; + this->privateData->thread.Start(); //Connect has failed - return false; + return true; } bool NetworkClient::Connect(unsigned short port, const char serverIP[]) @@ -221,46 +195,59 @@ bool NetworkClient::Connect(unsigned short port, const char serverIP[]) if(this->privateData) return false; if(!this->privateData) this->privateData = new PrivateData(); - int result = this->privateData->dat->connection.Connect(port, serverIP, false); + int result = this->privateData->connection.Connect(port, serverIP, false); //Connect has succeeded - if(result == 0) return true; + if(result != 0) return false; + + this->privateData->parent = this; + this->privateData->thread.Start(); //Connect has failed - return false; + return true; } void NetworkClient::Disconnect() { - privateData->dat->connection.Disconnect(); - privateData->dat->thread.Terminate(); + privateData->connection.Disconnect(); + privateData->thread.Terminate(); } void NetworkClient::Send(CustomProtocolObject& protocol) { - this->privateData->dat->sendQueue.Push(*protocol.GetProtocol()); + this->privateData->sendQueue.Push(*protocol.GetProtocol()); } void NetworkClient::Send(CustomNetProtocol* protocol) { - this->privateData->dat->sendQueue.Push(*protocol); + this->privateData->sendQueue.Push(*protocol); } void NetworkClient::SetOwner(NetworkSession* owner) { - this->privateData->dat->owner = owner; + this->privateData->owner = owner; } bool NetworkClient::IsConnected() { if(!this->privateData) return false; - return privateData->dat->connection.IsConnected(); + return privateData->connection.IsConnected(); } int NetworkClient::GetID() const { - return this->privateData->dat->ID; + return this->privateData->ID; } +void NetworkClient::DataRecieved(NetEvent e) +{ + if(this->privateData->owner) + { + this->privateData->owner->ClientEventCallback(e); + } +} + +void NetworkClient::NetworkCallback(Oyster::Network::CustomNetProtocol& p) +{} diff --git a/Code/Network/NetworkAPI/NetworkClient.h b/Code/Network/NetworkAPI/NetworkClient.h index 39350537..869a5100 100644 --- a/Code/Network/NetworkAPI/NetworkClient.h +++ b/Code/Network/NetworkAPI/NetworkClient.h @@ -41,8 +41,6 @@ namespace Oyster public: NetworkClient(); - NetworkClient(const NetworkClient& obj); - NetworkClient& operator =(const NetworkClient& obj); virtual ~NetworkClient(); bool operator ==(const NetworkClient& obj); @@ -51,7 +49,7 @@ namespace Oyster /** * */ - void ProcessMessages(); + void Update(); /** * @@ -93,7 +91,21 @@ namespace Oyster */ int GetID() const; + /** + * + */ + virtual void DataRecieved(NetEvent e); + + /** ! Deprecate ! + * Do not use this furthermore, instead use void DataRecieved(NetEvent e); + * @see DataRecieved + */ + virtual void NetworkCallback(Oyster::Network::CustomNetProtocol& p); + private: + NetworkClient(const NetworkClient& obj); + NetworkClient& operator =(const NetworkClient& obj); + struct PrivateData; PrivateData* privateData; }; diff --git a/Code/Network/NetworkAPI/NetworkServer.cpp b/Code/Network/NetworkAPI/NetworkServer.cpp index de341f81..9d6db207 100644 --- a/Code/Network/NetworkAPI/NetworkServer.cpp +++ b/Code/Network/NetworkAPI/NetworkServer.cpp @@ -9,11 +9,14 @@ #include "../NetworkDependencies/PostBox.h" #include "../NetworkDependencies/WinsockFunctions.h" -#include "../../Misc/Utilities.h" -#include "../../Misc/Thread/OysterThread.h" +#include "Utilities.h" +#include "Thread/OysterThread.h" + +#ifndef _DEBUG +#include +#endif using namespace Oyster::Network; -using namespace ::Server; using namespace Utility::DynamicMemory; using namespace Oyster::Thread; @@ -21,6 +24,41 @@ using namespace Oyster::Thread; PrivateData *************************************/ +void Broadcast() +{ + char pkt[4]; + size_t pkt_length = 4; + sockaddr_in dest; + sockaddr_in local; + WSAData data; + WSAStartup( MAKEWORD( 2, 2 ), &data ); + + local.sin_family = AF_INET; + local.sin_addr.s_addr = inet_addr( "127.0.0.1" ); + local.sin_port = 15151; // choose any + + dest.sin_family = AF_INET; + dest.sin_port = htons( 15151 ); + + // create the socket + SOCKET s = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP ); + // bind to the local address + bind( s, (sockaddr *)&local, sizeof(local) ); + + std::string addr; + for (int i = 0; i < 256; i++) + { + addr = "192.168.0."; + char buff[5]; + _itoa_s<5>(i, buff, 10); + + addr.append(buff); + dest.sin_addr.s_addr = inet_addr( addr.c_str() ); + // send the pkt + int ret = sendto( s, pkt, pkt_length, 0, (sockaddr *)&dest, sizeof(dest) ); + } +} + struct NetworkServer::PrivateData : public IThreadObject { public: @@ -30,6 +68,7 @@ public: , isInitiated(0) , isReleased(0) , isRunning(0) + , port(-1) { } ~PrivateData() { } @@ -37,11 +76,11 @@ public: bool DoWork(); public: - IListener* listener; + Listener* listener; PostBox postBox; //Postbox for new clients OysterThread thread; //Server thread NetworkSession *mainSession; - Utility::Container::ThreadSafeQueue clientQueue; + Utility::Container::ThreadSafeQueue> clientQueue; bool isInitiated; bool isReleased; @@ -51,6 +90,8 @@ public: bool NetworkServer::PrivateData::DoWork() { + //Broadcast(); + /** Check for new clients **/ if(postBox.IsFull()) { @@ -60,12 +101,11 @@ bool NetworkServer::PrivateData::DoWork() { //Something went wrong somewhere... do we care? } - - Oyster::Network::NetworkClient client; - client.Connect(clientSocketNum); - if(this->mainSession) - this->clientQueue.Push(client); + SmartPointer client(new NetworkClient()); + client->Connect(clientSocketNum); + + this->clientQueue.Push(client); } return true; @@ -101,6 +141,7 @@ NetworkServer::~NetworkServer() NetworkServer::ServerReturnCode NetworkServer::Init(const int& port, NetworkSession const* mainSession) { + this->privateData->mainSession = const_cast(mainSession); //Check if it's a valid port if(port == 0 || port == -1) { @@ -117,7 +158,7 @@ NetworkServer::ServerReturnCode NetworkServer::Init(const int& port, NetworkSess //Initiate listener this->privateData->listener = new Listener(&this->privateData->postBox); - if(!((Listener*)this->privateData->listener)->Init(port, false)) + if(!this->privateData->listener->Init(port, false)) { return NetworkServer::ServerReturnCode_Error; } @@ -129,14 +170,13 @@ NetworkServer::ServerReturnCode NetworkServer::Init(const int& port, NetworkSess this->privateData->isInitiated = true; this->privateData->isReleased = false; - this->privateData->mainSession = 0; return NetworkServer::ServerReturnCode_Sucess; } NetworkServer::ServerReturnCode NetworkServer::Start() { //Start listener - if(!((Listener*)this->privateData->listener)->Start()) + if(!this->privateData->listener->Start()) { return NetworkServer::ServerReturnCode_Error; } @@ -154,7 +194,7 @@ void NetworkServer::Stop() { if(this->privateData->listener) { - ((Listener*)this->privateData->listener)->Stop(); + this->privateData->listener->Stop(); } this->privateData->thread.Stop(); @@ -164,6 +204,10 @@ void NetworkServer::Stop() void NetworkServer::Shutdown() { + if(this->privateData->mainSession) + { + this->privateData->mainSession->CloseSession(true); + } if(this->privateData->listener) { this->privateData->listener->Shutdown(); @@ -180,12 +224,23 @@ void NetworkServer::Shutdown() this->privateData->isReleased = true; } -void NetworkServer::ProcessConnectedClients() +int NetworkServer::ProcessConnectedClients() { + int c = 0; while(!this->privateData->clientQueue.IsEmpty()) { - if(this->privateData->mainSession) this->privateData->mainSession->Attach(this->privateData->clientQueue.Pop()); + if(this->privateData->mainSession) + { + this->privateData->mainSession->ClientConnectedEvent(this->privateData->clientQueue.Pop()); + c++; + } + else + { + //Clients have nowhere to go? + this->privateData->clientQueue.Pop()->Disconnect(); + } } + return c; } void NetworkServer::SetSession(NetworkSession const* mainSession) @@ -198,7 +253,7 @@ NetworkSession const* NetworkServer::GetMainSession() return this->privateData->mainSession; } -NetworkSession const* NetworkServer::ReleaseMainSessionSession() +NetworkSession const* NetworkServer::ReleaseMainSession() { NetworkSession const * temp; temp = this->privateData->mainSession; @@ -211,7 +266,22 @@ bool NetworkServer::IsStarted() const return this->privateData->isRunning; } - +std::string NetworkServer::GetLanAddress() +{ + std::string szLocalIP; + char szHostName[255]; + struct hostent *host_entry; + + gethostname(szHostName, 255); + + host_entry = gethostbyname(szHostName); + char* temp = inet_ntoa (*(struct in_addr *)*host_entry->h_addr_list); + + char buff[255]; + strcpy_s(buff, temp); + szLocalIP = buff; + return szLocalIP; +} diff --git a/Code/Network/NetworkAPI/NetworkServer.h b/Code/Network/NetworkAPI/NetworkServer.h index c3385f86..ca33ebbe 100644 --- a/Code/Network/NetworkAPI/NetworkServer.h +++ b/Code/Network/NetworkAPI/NetworkServer.h @@ -57,7 +57,7 @@ namespace Oyster /** Parses asynchronous connected clients. */ - void ProcessConnectedClients(); + int ProcessConnectedClients(); /** Set the main session connected clients will enter when connected to server. * @param mainSession The session to connect as main server session. @@ -72,13 +72,17 @@ namespace Oyster /** Sets the main session to NULL and returns it * @return Returns the main session */ - NetworkSession const* ReleaseMainSessionSession(); + NetworkSession const* ReleaseMainSession(); /** * */ bool IsStarted() const; + /** + * + */ + std::string GetLanAddress(); private: struct PrivateData; diff --git a/Code/Network/NetworkAPI/NetworkSession.cpp b/Code/Network/NetworkAPI/NetworkSession.cpp index e026413e..d1fc7fec 100644 --- a/Code/Network/NetworkAPI/NetworkSession.cpp +++ b/Code/Network/NetworkAPI/NetworkSession.cpp @@ -10,13 +10,13 @@ #include #include - +using namespace Utility::DynamicMemory; using namespace Oyster::Network; struct NetworkSession::PrivateSessionData { - Utility::DynamicMemory::DynamicArray clients; + Utility::DynamicMemory::DynamicArray clients; NetworkClient::ClientEventFunction messageCallback; std::mutex clientListLock; NetworkSession* owner; //Where clients end up when session is closed. @@ -32,7 +32,7 @@ struct NetworkSession::PrivateSessionData NetworkSession::NetworkSession() - : data(0) + : data(new PrivateSessionData()) {} NetworkSession::NetworkSession(const NetworkSession& orig) { @@ -59,24 +59,26 @@ NetworkSession::~NetworkSession() this->data->clients.Clear(); this->data->clientCount = 0; this->data->messageCallback = 0; + delete this->data; + this->data = 0; } void NetworkSession::ProcessClients() { for (unsigned int i = 0; i < this->data->clients.Size(); i++) { - this->data->clients[i].ProcessMessages(); + if(this->data->clients[i]) this->data->clients[i]->Update(); } } -bool NetworkSession::Attach(NetworkClient client) +bool NetworkSession::Attach(NetClient client) { this->data->clientListLock.lock(); int k = -1; for (unsigned int i = 0; (k == -1) && i < this->data->clients.Size(); i++) { - if(!this->data->clients[i].IsConnected()) //TODO: Dont check connection status, check more general status.. + if(!this->data->clients[i]->IsConnected()) //TODO: Dont check connection status, check more general status.. k = i; } @@ -91,23 +93,44 @@ bool NetworkSession::Attach(NetworkClient client) this->data->clientCount++; + client->SetOwner(this); this->data->clientListLock.unlock(); return true; } -NetworkClient NetworkSession::Detach(const NetworkClient& client) +void NetworkSession::Detach() { - NetworkClient val; + if(this->data->owner) + { + for (unsigned int i = 0; i < this->data->clients.Size(); i++) + { + this->data->owner->Attach(this->data->clients[i]); + this->data->clients[i] = 0; + } + } + else + { + for (unsigned int i = 0; i < this->data->clients.Size(); i++) + { + this->data->clients[i]->Disconnect(); + this->data->clients[i] = 0; + } + } +} + +NetClient NetworkSession::Detach(const NetworkClient* client) +{ + NetClient val; this->data->clientListLock.lock(); for (unsigned int i = 0; i < this->data->clients.Size(); i++) { - if(this->data->clients[0].GetID() == client.GetID()) + if(this->data->clients[i] && this->data->clients[0]->GetID() == client->GetID()) { val = this->data->clients[i]; - this->data->clients[i] = NetworkClient(); + this->data->clients[i] = 0; this->data->clientCount--; } } @@ -117,18 +140,18 @@ NetworkClient NetworkSession::Detach(const NetworkClient& client) return val; } -NetworkClient NetworkSession::Detach(short ID) +NetClient NetworkSession::Detach(short ID) { - NetworkClient val; + NetClient val; this->data->clientListLock.lock(); for (unsigned int i = 0; i < this->data->clients.Size(); i++) { - if(this->data->clients[0].GetID() == ID) + if(this->data->clients[i] && this->data->clients[0]->GetID() == ID) { val = this->data->clients[i]; - this->data->clients[i] = NetworkClient(); + this->data->clients[i] = 0; this->data->clientCount--; } } @@ -143,8 +166,11 @@ bool NetworkSession::Send(Oyster::Network::CustomNetProtocol& protocol) bool returnValue = false; for (unsigned int i = 0; i < this->data->clients.Size(); i++) { - this->data->clients[i].Send(&protocol); - returnValue = true; + if(this->data->clients[i]) + { + this->data->clients[i]->Send(&protocol); + returnValue = true; + } } return returnValue; @@ -154,9 +180,9 @@ bool NetworkSession::Send(Oyster::Network::CustomNetProtocol& protocol, int ID) { for (unsigned int i = 0; i < this->data->clients.Size(); i++) { - if(this->data->clients[i].GetID() == ID) + if(this->data->clients[i] && this->data->clients[i]->GetID() == ID) { - this->data->clients[i].Send(&protocol); + this->data->clients[i]->Send(&protocol); return true; } } @@ -169,9 +195,12 @@ void NetworkSession::CloseSession(bool dissconnectClients) for (unsigned int i = 0; i < this->data->clients.Size(); i++) { - if(dissconnectClients) this->data->clients[i].Disconnect(); - else if(this->data->owner) this->data->owner->Attach(this->data->clients[i]); - else this->data->clients[i].Disconnect(); + if(this->data->clients[i]) + { + if(dissconnectClients) this->data->clients[i]->Disconnect(); + else if(this->data->owner) this->data->owner->Attach(this->data->clients[i]); + else this->data->clients[i]->Disconnect(); //Idiot check, clients have to go somewhere.. + } } this->data->clients.Clear(); @@ -180,5 +209,23 @@ void NetworkSession::CloseSession(bool dissconnectClients) this->data->clientListLock.unlock(); } +void NetworkSession::SetOwner(NetworkSession* owner) +{ + this->data->owner = owner; +} +int NetworkSession::GetClientCount() const +{ + int c = 0; + for (unsigned int i = 0; i < this->data->clients.Size(); i++) + { + if(this->data->clients[i]) c++; + } + return c; +} + +void NetworkSession::ClientConnectedEvent(NetClient client) +{ + this->Attach(client); +} diff --git a/Code/Network/NetworkAPI/NetworkSession.h b/Code/Network/NetworkAPI/NetworkSession.h index 7c50afd0..b8287b36 100644 --- a/Code/Network/NetworkAPI/NetworkSession.h +++ b/Code/Network/NetworkAPI/NetworkSession.h @@ -10,11 +10,13 @@ #include "NetworkAPI_Preprocessor.h" #include "NetworkServerEventStruct.h" #include "NetworkClient.h" +#include "Utilities.h" namespace Oyster { namespace Network { + typedef Utility::DynamicMemory::SmartPointer NetClient; class NET_API_EXPORT NetworkSession { public: @@ -30,49 +32,60 @@ namespace Oyster /** * */ - bool Attach(NetworkClient client); + virtual bool Attach(NetClient client); + /** + * Detaches all clients and sends them to owner. + * If no owner is set the clients is disconnected. + */ + virtual void Detach(); + /** * */ - NetworkClient Detach(const NetworkClient& client); + virtual NetClient Detach(const NetworkClient* client); /** * */ - NetworkClient Detach(short ID); + virtual NetClient Detach(short ID); /** Send a message to all clients in this session * @param message The message */ - bool Send(Oyster::Network::CustomNetProtocol& message); + virtual bool Send(Oyster::Network::CustomNetProtocol& message); /** Send a message to a specific client in this session * @param message The message */ - bool Send(Oyster::Network::CustomNetProtocol& protocol, int ID); + virtual bool Send(Oyster::Network::CustomNetProtocol& protocol, int ID); /** * */ - void CloseSession( bool dissconnectClients = false ); + virtual void CloseSession( bool dissconnectClients = false ); /** * Set the owner that clients will be returned to. * @param owner If owner is NULL, clients will be disconnected when session is over. */ - void SetOwner(NetworkSession* owner); + virtual void SetOwner(NetworkSession* owner); + + /** Get the number of clients active in this session + * @return The client count + */ + int GetClientCount() const; + + /** + * + */ + virtual void ClientConnectedEvent(NetClient client); /** * */ virtual void ClientEventCallback(NetEvent e) = 0; - /** - * - */ - virtual void ClientConnectedEvent(NetEvent e) = 0; - private: struct PrivateSessionData; PrivateSessionData* data; diff --git a/Code/Network/NetworkAPI/Translator.cpp b/Code/Network/NetworkAPI/Translator.cpp index b1196e88..9b855d47 100644 --- a/Code/Network/NetworkAPI/Translator.cpp +++ b/Code/Network/NetworkAPI/Translator.cpp @@ -18,6 +18,7 @@ using namespace std; struct MyCastingStruct { std::map attributes; + Utility::DynamicMemory::ReferenceCount *c; }; // TODO: Check if the package has been packed correctly. diff --git a/Code/Network/NetworkDependencies/Connection.cpp b/Code/Network/NetworkDependencies/Connection.cpp index c79d5fe6..27aa296a 100644 --- a/Code/Network/NetworkDependencies/Connection.cpp +++ b/Code/Network/NetworkDependencies/Connection.cpp @@ -53,6 +53,8 @@ int Connection::Connect(int socket, bool blocking) int Connection::Connect(unsigned short port , const char serverName[], bool blocking) { + if(this->socket == -1 || this->socket == 0) InitiateSocket(); + struct hostent *hostEnt; if((hostEnt = gethostbyname(serverName)) == NULL) { diff --git a/Code/Network/NetworkDependencies/IListener.h b/Code/Network/NetworkDependencies/IListener.h deleted file mode 100644 index fc7bb4f5..00000000 --- a/Code/Network/NetworkDependencies/IListener.h +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef NETWORK_SERVER_ILISTENER_H -#define NETWORK_SERVER_ILISTENER_H - -///////////////////////////////////// -// Created by Pontus Fransson 2013 // -///////////////////////////////////// - -namespace Oyster -{ - namespace Network - { - namespace Server - { - class IListener - { - public: - virtual ~IListener() {} - virtual bool Init(unsigned int port) = 0; - virtual int Accept() = 0; - virtual void Shutdown() = 0; - - }; - } - } -} - -#endif \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Listener.cpp b/Code/Network/NetworkDependencies/Listener.cpp index 360ccebb..106ac3b9 100644 --- a/Code/Network/NetworkDependencies/Listener.cpp +++ b/Code/Network/NetworkDependencies/Listener.cpp @@ -1,8 +1,8 @@ #include "Listener.h" -using namespace Oyster::Network::Server; using namespace Utility::DynamicMemory; using namespace Oyster::Thread; +using namespace Oyster::Network; Listener::Listener() { diff --git a/Code/Network/NetworkDependencies/Listener.h b/Code/Network/NetworkDependencies/Listener.h index 422592e7..0c55f86e 100644 --- a/Code/Network/NetworkDependencies/Listener.h +++ b/Code/Network/NetworkDependencies/Listener.h @@ -5,7 +5,6 @@ // Created by Pontus Fransson 2013 // ///////////////////////////////////// -#include "IListener.h" #include "Connection.h" #include "IPostBox.h" #include "../../Misc/Thread/OysterThread.h" @@ -17,45 +16,43 @@ namespace Oyster { namespace Network { - namespace Server + class Listener : public ::Oyster::Thread::IThreadObject { - class Listener : public IListener, public ::Oyster::Thread::IThreadObject - { - public: - Listener(); - Listener(Oyster::Network::IPostBox* postBox); - ~Listener(); + public: + Listener(); + Listener(Oyster::Network::IPostBox* postBox); + ~Listener(); - bool Init(unsigned int port) override; - bool Init(unsigned int port, bool start); - bool Start(); - void Stop(); - void Shutdown(); + bool Init(unsigned int port); + bool Init(unsigned int port, bool start); + bool Start(); + void Stop(); + void Shutdown(); - void SetPostBox(IPostBox* postBox); + void SetPostBox(IPostBox* postBox); - private: - //Thread functions - bool DoWork(); - void ThreadEntry(); - void ThreadExit(); + private: + //Thread functions + bool DoWork(); + void ThreadEntry(); + void ThreadExit(); - //Function that runs in the thread. - int Accept(); - void StopListen(); + //Function that runs in the thread. + int Accept(); + void StopListen(); - private: - ::Oyster::Network::Connection* connection; + private: + ::Oyster::Network::Connection* connection; - ::Oyster::Thread::OysterThread thread; - OysterMutex mutex; - std::mutex stdMutex; + ::Oyster::Thread::OysterThread thread; + OysterMutex mutex; + std::mutex stdMutex; + + IPostBox* postBox; + std::atomic isListening; + int port; + }; - IPostBox* postBox; - std::atomic isListening; - int port; - }; - } } } diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj index 4d2ad291..061d3c4d 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj @@ -164,7 +164,6 @@ - diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters index eadbbeb3..188a4377 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters @@ -14,7 +14,6 @@ - diff --git a/Code/OysterGraphics/FileLoader/DanLoader.cpp b/Code/OysterGraphics/FileLoader/DanLoader.cpp index 73f0c8c8..6ac0cc24 100644 --- a/Code/OysterGraphics/FileLoader/DanLoader.cpp +++ b/Code/OysterGraphics/FileLoader/DanLoader.cpp @@ -132,13 +132,14 @@ void Oyster::Graphics::Loading::UnloadDAN(void* data) delete info; } -static wchar_t* charToWChar(const char* text) +static std::wstring charToWChar(const char* text) { // Convert to a wchar_t* size_t origsize = strlen(text) + 1; size_t convertedChars = 0; - wchar_t* wcstring = new wchar_t[origsize]; - mbstowcs_s(&convertedChars, wcstring, origsize, text, _TRUNCATE); + //wchar_t* wcstring = new wchar_t[origsize]; + std::wstring wcstring; wcstring.resize(origsize); + mbstowcs_s(&convertedChars, &wcstring[0], origsize, text, _TRUNCATE); return wcstring; } @@ -175,7 +176,7 @@ void Oyster::Graphics::Loading::LoadDAN(const wchar_t filename[], Oyster::Resour buffer = new char[4]; danFile.read(buffer, 4); memcpy(&headerType, buffer, 4); - //delete[] buffer; // ( note: may crash here.) + delete[] buffer; // ( note: may crash here.) // handle header type switch ((HeaderType)headerType) @@ -276,11 +277,14 @@ void Oyster::Graphics::Loading::LoadDAN(const wchar_t filename[], Oyster::Resour delete[] buffer; // ( note: may crash here.) // - ID3D11ShaderResourceView* diffuseMap = (ID3D11ShaderResourceView*)Oyster::Resource::OysterResource::LoadResource(charToWChar(materialHeader.diffuseMapPath), Oyster::Graphics::Loading::LoadTexture); - ID3D11ShaderResourceView* normalMap = (ID3D11ShaderResourceView*)Oyster::Resource::OysterResource::LoadResource(charToWChar(materialHeader.normalMapPath), Oyster::Graphics::Loading::LoadTexture); + ID3D11ShaderResourceView* diffuseMap = (ID3D11ShaderResourceView*)Oyster::Resource::OysterResource::LoadResource(charToWChar(materialHeader.diffuseMapPath).c_str(), Oyster::Graphics::Loading::LoadTexture); + ID3D11ShaderResourceView* normalMap = (ID3D11ShaderResourceView*)Oyster::Resource::OysterResource::LoadResource(charToWChar(materialHeader.normalMapPath).c_str(), Oyster::Graphics::Loading::LoadTexture); modelInfo->Material.push_back(diffuseMap); modelInfo->Material.push_back(normalMap); + delete materialHeader.normalMapPath; + delete materialHeader.diffuseMapPath; + break; } // skeleton header @@ -298,6 +302,7 @@ void Oyster::Graphics::Loading::LoadDAN(const wchar_t filename[], Oyster::Resour } } + // close file danFile.close();