From 0f5517398dbb4b72c5395d13b918fcc8d5acdf11 Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Fri, 24 Jan 2014 09:00:59 +0100 Subject: [PATCH 01/23] GL - Update uml, LevelParser.h.cpp ObjectDefines.h --- Code/Dokumentation/LevelLoader.uxf | 46 +++++++++++++-------------- Code/Game/GameLogic/GameLogic.vcxproj | 3 ++ Code/Game/GameLogic/LevelParser.cpp | 2 ++ Code/Game/GameLogic/LevelParser.h | 28 ++++++++++++++++ Code/Game/GameLogic/ObjectDefines.h | 38 ++++++++++++++++++++++ 5 files changed, 94 insertions(+), 23 deletions(-) create mode 100644 Code/Game/GameLogic/LevelParser.cpp create mode 100644 Code/Game/GameLogic/LevelParser.h create mode 100644 Code/Game/GameLogic/ObjectDefines.h diff --git a/Code/Dokumentation/LevelLoader.uxf b/Code/Dokumentation/LevelLoader.uxf index 723857bb..fd3e9c79 100644 --- a/Code/Dokumentation/LevelLoader.uxf +++ b/Code/Dokumentation/LevelLoader.uxf @@ -1,11 +1,11 @@ - + 8 com.umlet.element.Package 552 - 320 + 360 584 368 @@ -16,7 +16,7 @@ com.umlet.element.Class 440 - 88 + 128 128 40 @@ -28,7 +28,7 @@ com.umlet.element.Relation 768 - 472 + 512 136 104 @@ -39,12 +39,12 @@ com.umlet.element.Class 560 - 544 + 584 232 136 <<Interface>> -Parser +LevelParser -- Functions: vector<struct> Parse(); @@ -60,7 +60,7 @@ const int FileVersion; com.umlet.element.Class 624 - 208 + 248 80 24 @@ -72,7 +72,7 @@ const int FileVersion; com.umlet.element.Relation 640 - 208 + 248 40 168 @@ -83,7 +83,7 @@ const int FileVersion; com.umlet.element.Relation 384 - 176 + 216 256 56 @@ -97,7 +97,7 @@ m2=1..1 com.umlet.element.Package 248 - 320 + 360 248 160 @@ -108,7 +108,7 @@ m2=1..1 com.umlet.element.Class 800 - 360 + 400 208 136 @@ -127,7 +127,7 @@ Privates: com.umlet.element.Class 328 - 208 + 248 80 24 @@ -138,11 +138,11 @@ Privates: com.umlet.element.Class 256 - 360 + 400 232 104 - Defines.h + ObjectDefines.h <<Header file>> -- Enum ObjectType(static, dynamic, specials); @@ -156,7 +156,7 @@ Struct specials com.umlet.element.Relation 680 - 176 + 216 152 56 @@ -170,7 +170,7 @@ Uses> com.umlet.element.Class 816 - 192 + 232 128 40 @@ -182,7 +182,7 @@ Uses> com.umlet.element.Class 928 - 560 + 600 200 120 @@ -196,7 +196,7 @@ functions for creating the right structs com.umlet.element.Relation 768 - 576 + 616 176 56 @@ -210,7 +210,7 @@ Uses> com.umlet.element.Class 560 - 360 + 400 232 136 @@ -230,7 +230,7 @@ Privates: com.umlet.element.Relation 344 - 208 + 248 40 168 @@ -241,7 +241,7 @@ Privates: com.umlet.element.Relation 840 - 208 + 248 88 168 @@ -253,7 +253,7 @@ Privates: com.umlet.element.Relation 656 - 472 + 512 40 88 @@ -264,7 +264,7 @@ Privates: com.umlet.element.Relation 544 - 64 + 104 136 160 diff --git a/Code/Game/GameLogic/GameLogic.vcxproj b/Code/Game/GameLogic/GameLogic.vcxproj index 6b66a9d5..da052b70 100644 --- a/Code/Game/GameLogic/GameLogic.vcxproj +++ b/Code/Game/GameLogic/GameLogic.vcxproj @@ -185,6 +185,8 @@ + + @@ -202,6 +204,7 @@ + diff --git a/Code/Game/GameLogic/LevelParser.cpp b/Code/Game/GameLogic/LevelParser.cpp new file mode 100644 index 00000000..65b63aef --- /dev/null +++ b/Code/Game/GameLogic/LevelParser.cpp @@ -0,0 +1,2 @@ +#include "LevelParser.h" + diff --git a/Code/Game/GameLogic/LevelParser.h b/Code/Game/GameLogic/LevelParser.h new file mode 100644 index 00000000..f1904d9c --- /dev/null +++ b/Code/Game/GameLogic/LevelParser.h @@ -0,0 +1,28 @@ +#ifndef LEVEL_PARSER_H +#define LEVEL_PARSER_H + +#include +#include "ObjectDefines.h" + +namespace GameLogic +{ + namespace LevelLoader + { + class LevelParser + { + public: + LevelParser(); + ~LevelParser(); + + // + std::vector Parse(); + + // + ObjectTypeHeader ParseHeader(); + + private: + + }; + } +} +#endif \ No newline at end of file diff --git a/Code/Game/GameLogic/ObjectDefines.h b/Code/Game/GameLogic/ObjectDefines.h new file mode 100644 index 00000000..74bca42c --- /dev/null +++ b/Code/Game/GameLogic/ObjectDefines.h @@ -0,0 +1,38 @@ +#ifndef OBJECT_DEFINES_H +#define OBJECT_DEFINES_H + +namespace GameLogic +{ + enum ObjectType + { + ObjectType_Static, + ObjectType_Dynamic, + + + ObjectType_NUM_OF_TYPES, + + ObjectType_Unknow = -1, + }; + + struct ObjectTypeHeader + { + ObjectType typeID; + + }; + + struct ObjectHeader : public ObjectTypeHeader + { + //Model, + + //Texture + + //Position + float position[3]; + //Rotation + float rotation[3]; + //Scale + float scale[3]; + }; +} + +#endif \ No newline at end of file From aaf3bf30f9a19644dc6480cbcc3d0097dec073fd Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Fri, 24 Jan 2014 10:01:58 +0100 Subject: [PATCH 02/23] GL - LevelParser half way implemented. --- Code/Game/GameLogic/LevelParser.cpp | 80 +++++++++++++++++++++++++++++ Code/Game/GameLogic/LevelParser.h | 27 +++++++++- Code/Game/GameLogic/ObjectDefines.h | 3 +- 3 files changed, 107 insertions(+), 3 deletions(-) diff --git a/Code/Game/GameLogic/LevelParser.cpp b/Code/Game/GameLogic/LevelParser.cpp index 65b63aef..21e6c020 100644 --- a/Code/Game/GameLogic/LevelParser.cpp +++ b/Code/Game/GameLogic/LevelParser.cpp @@ -1,2 +1,82 @@ #include "LevelParser.h" +using namespace GameLogic; +using namespace ::LevelLoader; + +LevelParser::LevelParser() +{ +} + +LevelParser::~LevelParser() +{ +} + +// +std::vector LevelParser::Parse(std::string filename) +{ + //Read entire level file. + + std::vector objects; + + unsigned int counter = 0; + unsigned int stringSize = 0; + while(counter < stringSize) + { + //Get typeID + int typeID = 0; + + + switch(typeID) + { + case TypeID_LevelHeader: + //Call function + counter += LevelHeaderSize; + break; + + case TypeID_Object: + //Call function + counter += ObjectHeaderSize; + break; + default: + //Couldn't find typeID. FAIL!!!!!! + break; + } + } + + return objects; +} + +// +ObjectTypeHeader LevelParser::ParseHeader(std::string filename) +{ + //Read entire level file. + + //Find the header in the returned string. + unsigned int counter = 0; + unsigned int stringSize = 0; + + ObjectTypeHeader header; + header.typeID = ObjectType_Level; + + while(counter < stringSize) + { + int typeID = 0; + switch(typeID) + { + case TypeID_LevelHeader: + //Call function + + break; + case TypeID_Object: + //Call function + counter += ObjectHeaderSize; + break; + + default: + //Couldn't find typeID. FAIL!!!!!! + break; + } + } + + return header; +} \ No newline at end of file diff --git a/Code/Game/GameLogic/LevelParser.h b/Code/Game/GameLogic/LevelParser.h index f1904d9c..f261004b 100644 --- a/Code/Game/GameLogic/LevelParser.h +++ b/Code/Game/GameLogic/LevelParser.h @@ -1,6 +1,7 @@ #ifndef LEVEL_PARSER_H #define LEVEL_PARSER_H +#include #include #include "ObjectDefines.h" @@ -15,12 +16,34 @@ namespace GameLogic ~LevelParser(); // - std::vector Parse(); + std::vector Parse(std::string filename); // - ObjectTypeHeader ParseHeader(); + ObjectTypeHeader ParseHeader(std::string filename); private: + static const int TypeHeaderSize = 4; //Including the TypeID + static const int ObjectHeaderSize = 10; //Including modelID, textureID, position, rotation, scale. + static const int LevelHeaderSize = 10; //Including Format version, Name, Map version etc. + + static const int FormatVersionMajor = 1; + static const int FormatVersionMinor = 0; + + /************************************* + Question + *************************************/ + //Could we use the IDs in "ObjectDefines.h" or do we have to use our own??? + enum TypeID + { + TypeID_LevelHeader, + TypeID_Player, + TypeID_Object, + + + TypeID_NUM_OF_TYPES, + + ObjectType_Unknown = -1, + }; }; } diff --git a/Code/Game/GameLogic/ObjectDefines.h b/Code/Game/GameLogic/ObjectDefines.h index 74bca42c..eae1b0c9 100644 --- a/Code/Game/GameLogic/ObjectDefines.h +++ b/Code/Game/GameLogic/ObjectDefines.h @@ -5,13 +5,14 @@ namespace GameLogic { enum ObjectType { + ObjectType_Level, ObjectType_Static, ObjectType_Dynamic, ObjectType_NUM_OF_TYPES, - ObjectType_Unknow = -1, + ObjectType_Unknown = -1, }; struct ObjectTypeHeader From c84ed645f00d0b256599462e093b9b3f29e64710 Mon Sep 17 00:00:00 2001 From: Sam Mario Svensson Date: Fri, 24 Jan 2014 10:22:18 +0100 Subject: [PATCH 03/23] GameLogic - implementation of LevelLoader --- Code/Dokumentation/LevelLoader.uxf | 300 +++++++++++++------------- Code/Game/GameLogic/GameLogic.vcxproj | 4 + Code/Game/GameLogic/LevelLoader.cpp | 17 ++ Code/Game/GameLogic/LevelLoader.h | 29 +++ Code/Game/GameLogic/Loader.cpp | 18 ++ Code/Game/GameLogic/Loader.h | 28 +++ 6 files changed, 246 insertions(+), 150 deletions(-) create mode 100644 Code/Game/GameLogic/LevelLoader.cpp create mode 100644 Code/Game/GameLogic/LevelLoader.h create mode 100644 Code/Game/GameLogic/Loader.cpp create mode 100644 Code/Game/GameLogic/Loader.h diff --git a/Code/Dokumentation/LevelLoader.uxf b/Code/Dokumentation/LevelLoader.uxf index 723857bb..6cfb9c29 100644 --- a/Code/Dokumentation/LevelLoader.uxf +++ b/Code/Dokumentation/LevelLoader.uxf @@ -1,61 +1,6 @@ 8 - - com.umlet.element.Package - - 552 - 320 - 584 - 368 - - LevelLoader - - - - com.umlet.element.Class - - 440 - 88 - 128 - 40 - - GameLogic -<<Erik>> - - - - com.umlet.element.Relation - - 768 - 472 - 136 - 104 - - lt=<<<<- - 120;24;120;88;24;88 - - - com.umlet.element.Class - - 560 - 544 - 232 - 136 - - <<Interface>> -Parser --- -Functions: -vector<struct> Parse(); -- -Privates: -enum headerType; -const int FileHeaderSize; -const int FileVersion; - - - com.umlet.element.Class @@ -71,85 +16,39 @@ const int FileVersion; com.umlet.element.Relation - 640 + 840 208 - 40 + 88 168 - lt=<<. - 24;24;24;152 - - - com.umlet.element.Relation - - 384 - 176 - 256 - 56 - - lt=->>>> -m1=1..1 -m2=1..1 -<Knows about - 240;40;24;40 - - - com.umlet.element.Package - - 248 - 320 - 248 - 160 - - Defines - + lt=. +<Uses + 24;24;24;64;72;64;72;152 com.umlet.element.Class - 800 - 360 - 208 - 136 + 928 + 560 + 200 + 120 - <<Interface>> -Loader + Collection of functions +<<lots of functions>> -- -Functions: -wchar* LoadFile(string fileName); -Model* LoadModel(string modelName); -Model* LoadModel(int modelID); -- -Privates: +functions for creating the right structs com.umlet.element.Class - 328 - 208 - 80 - 24 + 440 + 88 + 128 + 40 - Defines - - - - com.umlet.element.Class - - 256 - 360 - 232 - 104 - - Defines.h -<<Header file>> --- -Enum ObjectType(static, dynamic, specials); -. -Struct static; -Struct dynamic; -Struct specials + GameLogic +<<Erik>> @@ -169,28 +68,32 @@ Uses> com.umlet.element.Class - 816 - 192 - 128 - 40 + 800 + 360 + 208 + 136 - Resource Loader -<<Dennis>><<Singleton> + <<Interface>> +Loader +-- +Functions: +wchar* LoadFile(string fileName); +//Model* LoadHitBoxes(string modelName); +//Model* LoadHitBoxes(int modelID); +- +Privates: - com.umlet.element.Class + com.umlet.element.Relation - 928 - 560 - 200 - 120 + 344 + 208 + 40 + 168 - Collection of functions -<<lots of functions>> --- -functions for creating the right structs - + lt=<<. + 24;24;24;152 com.umlet.element.Relation @@ -206,6 +109,61 @@ m2=1..1 Uses> 24;40;160;40 + + com.umlet.element.Class + + 816 + 192 + 128 + 40 + + Resource Loader +<<Dennis>><<Singleton> + + + + com.umlet.element.Relation + + 768 + 472 + 136 + 104 + + lt=<<<<- + 120;24;120;88;24;88 + + + com.umlet.element.Relation + + 384 + 176 + 256 + 56 + + lt=->>>> +m1=1..1 +m2=1..1 +<Knows about + 240;40;24;40 + + + com.umlet.element.Class + + 256 + 360 + 232 + 104 + + Defines.h +<<Header file>> +-- +Enum ObjectType(static, dynamic, specials); +. +Struct static; +Struct dynamic; +Struct specials + + com.umlet.element.Class @@ -229,7 +187,7 @@ Privates: com.umlet.element.Relation - 344 + 640 208 40 168 @@ -238,27 +196,26 @@ Privates: 24;24;24;152 - com.umlet.element.Relation + com.umlet.element.Class - 840 + 328 208 - 88 - 168 + 80 + 24 - lt=. -<Uses - 24;24;24;64;72;64;72;152 + Defines + - com.umlet.element.Relation + com.umlet.element.Package - 656 - 472 - 40 - 88 + 248 + 320 + 248 + 160 - lt=<<<<- - 24;72;24;24 + Defines + com.umlet.element.Relation @@ -274,4 +231,47 @@ m2=1..1 Uses> 24;40;80;40;120;40;120;144 + + com.umlet.element.Relation + + 656 + 472 + 40 + 88 + + lt=<<<<- + 24;72;24;24 + + + com.umlet.element.Class + + 560 + 544 + 232 + 136 + + <<Interface>> +Parser +-- +Functions: +vector<struct> Parse(); +- +Privates: +enum headerType; +const int FileHeaderSize; +const int FileVersion; + + + + + com.umlet.element.Package + + 552 + 320 + 584 + 368 + + LevelLoader + + diff --git a/Code/Game/GameLogic/GameLogic.vcxproj b/Code/Game/GameLogic/GameLogic.vcxproj index 6b66a9d5..bfa96d35 100644 --- a/Code/Game/GameLogic/GameLogic.vcxproj +++ b/Code/Game/GameLogic/GameLogic.vcxproj @@ -184,6 +184,8 @@ + + @@ -202,6 +204,8 @@ + + diff --git a/Code/Game/GameLogic/LevelLoader.cpp b/Code/Game/GameLogic/LevelLoader.cpp new file mode 100644 index 00000000..0e302d37 --- /dev/null +++ b/Code/Game/GameLogic/LevelLoader.cpp @@ -0,0 +1,17 @@ +////////////////////////////////// +// Created by Sam Svensson 2013 // +////////////////////////////////// + +#include "LevelLoader.h" +using namespace GameLogic; +using namespace GameLogic::LevelLoader; + +std::vector LevelLoader::LoadLevel(std::string fileName) +{ + Parser->parse(fileName); +} + +std::vector LevelLoader::LoadLevelHeader(std::string fileName) +{ + parser->parseHeader(fileName); +} \ No newline at end of file diff --git a/Code/Game/GameLogic/LevelLoader.h b/Code/Game/GameLogic/LevelLoader.h new file mode 100644 index 00000000..c33cd09b --- /dev/null +++ b/Code/Game/GameLogic/LevelLoader.h @@ -0,0 +1,29 @@ +////////////////////////////////// +// Created by Sam Svensson 2013 // +////////////////////////////////// + +#ifndef LEVELLOADER_H +#define LEVELLOADER_H + +#include +#include +#include "ObjectDefines.h" +#include "LevelParser.h" + +namespace GameLogic +{ + class LevelLoader + { + + public: + LevelLoader(){this->parser = new Parser()}; + ~LevelLoader(){}; + std::vector LoadLevel(std::string fileName); //loads the level and objects from file + std::vector LoadLevelHeader(std::string fileName); //just for fast access for the meta information about the level. + + private: + LevelParser parser; + }; +} + +#endif \ No newline at end of file diff --git a/Code/Game/GameLogic/Loader.cpp b/Code/Game/GameLogic/Loader.cpp new file mode 100644 index 00000000..3cc68c50 --- /dev/null +++ b/Code/Game/GameLogic/Loader.cpp @@ -0,0 +1,18 @@ +////////////////////////////////// +// Created by Sam Svensson 2013 // +////////////////////////////////// + +#include "Loader.h" + +using namespace GameLogic::LevelLoader; +using namespace Oyster::Resource; +using namespace std; + +char* Loader::LoadFile(std::string fileName) +{ + //convert from string to wstring + std::wstring temp(fileName.begin(), fileName.end()); + + //convert from wstring to wchar then loads the file + return OysterResource::LoadResource(temp.c_str(), Oyster::Resource::ResourceType::ResourceType_Byte_Raw, -1 , false); +} \ No newline at end of file diff --git a/Code/Game/GameLogic/Loader.h b/Code/Game/GameLogic/Loader.h new file mode 100644 index 00000000..0d0a662f --- /dev/null +++ b/Code/Game/GameLogic/Loader.h @@ -0,0 +1,28 @@ +////////////////////////////////// +// Created by Sam Svensson 2013 // +////////////////////////////////// + +#ifndef LOADER_H +#define LOADER_H + +#include "Resource\OysterResource.h" +#include + +namespace GameLogic +{ + namespace LevelLoader + { + class Loader + { + public: + Loader(){}; + ~Loader(){}; + char* LoadFile(std::string fileName); + + //TODO: + //Add functionality to load physicsObjects (hitboxes) + }; + } +} + +#endif; \ No newline at end of file From e188794aa3f5c8572517662d1e8cacf8c884de05 Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Mon, 27 Jan 2014 10:14:02 +0100 Subject: [PATCH 04/23] GL - Load level in LevelParser --- Code/Game/GameLogic/LevelParser.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Code/Game/GameLogic/LevelParser.cpp b/Code/Game/GameLogic/LevelParser.cpp index 1f6a9398..5a709316 100644 --- a/Code/Game/GameLogic/LevelParser.cpp +++ b/Code/Game/GameLogic/LevelParser.cpp @@ -1,5 +1,7 @@ #include "LevelParser.h" +#include "Loader.h" + using namespace GameLogic; using namespace ::LevelFileLoader; @@ -15,6 +17,8 @@ LevelParser::~LevelParser() std::vector LevelParser::Parse(std::string filename) { //Read entire level file. + Loader loader; + unsigned char* buffer = (unsigned char*)loader.LoadFile(filename.c_str()); std::vector objects; @@ -25,6 +29,7 @@ std::vector LevelParser::Parse(std::string filename) //Get typeID int typeID = 0; + //Unpack ID switch(typeID) { @@ -50,6 +55,8 @@ std::vector LevelParser::Parse(std::string filename) ObjectTypeHeader LevelParser::ParseHeader(std::string filename) { //Read entire level file. + Loader loader; + unsigned char* buffer = (unsigned char*)loader.LoadFile(filename.c_str()); //Find the header in the returned string. unsigned int counter = 0; @@ -65,7 +72,8 @@ ObjectTypeHeader LevelParser::ParseHeader(std::string filename) { case TypeID_LevelHeader: //Call function - + + counter += LevelHeaderSize; break; case TypeID_Object: //Call function From 1748fe323a5d531ed73a3651f24110260475e2ef Mon Sep 17 00:00:00 2001 From: Sam Mario Svensson Date: Mon, 27 Jan 2014 10:15:39 +0100 Subject: [PATCH 05/23] GL - Added Parsing functions for levelLoader and moved Packing classes to misc --- Code/Game/GameLogic/GameLogic.vcxproj | 2 + Code/Game/GameLogic/Loader.cpp | 5 +- Code/Game/GameLogic/Loader.h | 4 +- Code/Game/GameLogic/ObjectDefines.h | 4 +- Code/Game/GameLogic/ParseFunctions.cpp | 83 +++ Code/Game/GameLogic/ParserFunctions.h | 22 + Code/Misc/Misc.vcxproj | 2 + Code/Misc/Misc.vcxproj.filters | 6 + Code/Misc/Packing/Packing.cpp | 346 +++++++++++++ Code/Misc/Packing/Packing.h | 81 +++ .../Messages/MessageHeader.cpp | 3 +- .../NetworkDependencies.vcxproj | 2 - .../NetworkDependencies.vcxproj.filters | 2 - Code/Network/NetworkDependencies/Packing.cpp | 486 ------------------ Code/Network/NetworkDependencies/Packing.h | 107 ---- 15 files changed, 551 insertions(+), 604 deletions(-) create mode 100644 Code/Game/GameLogic/ParseFunctions.cpp create mode 100644 Code/Game/GameLogic/ParserFunctions.h create mode 100644 Code/Misc/Packing/Packing.cpp create mode 100644 Code/Misc/Packing/Packing.h delete mode 100644 Code/Network/NetworkDependencies/Packing.cpp delete mode 100644 Code/Network/NetworkDependencies/Packing.h diff --git a/Code/Game/GameLogic/GameLogic.vcxproj b/Code/Game/GameLogic/GameLogic.vcxproj index ba7603a7..54ec2d7e 100644 --- a/Code/Game/GameLogic/GameLogic.vcxproj +++ b/Code/Game/GameLogic/GameLogic.vcxproj @@ -189,6 +189,7 @@ + @@ -210,6 +211,7 @@ + diff --git a/Code/Game/GameLogic/Loader.cpp b/Code/Game/GameLogic/Loader.cpp index ec537765..01047b2f 100644 --- a/Code/Game/GameLogic/Loader.cpp +++ b/Code/Game/GameLogic/Loader.cpp @@ -8,11 +8,12 @@ using namespace GameLogic::LevelFileLoader; using namespace Oyster::Resource; using namespace std; -char* Loader::LoadFile(std::string fileName) +unsigned char* Loader::LoadFile(std::string fileName, int &size) { //convert from string to wstring std::wstring temp(fileName.begin(), fileName.end()); + size = temp.size(); //convert from wstring to wchar then loads the file - return (char*)OysterResource::LoadResource(temp.c_str(), Oyster::Resource::ResourceType::ResourceType_Byte_Raw, -1 , false); + return (unsigned char*)OysterResource::LoadResource(temp.c_str(), Oyster::Resource::ResourceType::ResourceType_Byte_Raw, -1 , false); } \ No newline at end of file diff --git a/Code/Game/GameLogic/Loader.h b/Code/Game/GameLogic/Loader.h index 72d53854..d77921c5 100644 --- a/Code/Game/GameLogic/Loader.h +++ b/Code/Game/GameLogic/Loader.h @@ -15,9 +15,9 @@ namespace GameLogic class Loader { public: - Loader(){}; + Loader (){}; ~Loader(){}; - char* LoadFile(std::string fileName); + unsigned char* LoadFile(std::string fileName, int &size); //TODO: //Add functionality to load physicsObjects (hitboxes) diff --git a/Code/Game/GameLogic/ObjectDefines.h b/Code/Game/GameLogic/ObjectDefines.h index eae1b0c9..7f2307d1 100644 --- a/Code/Game/GameLogic/ObjectDefines.h +++ b/Code/Game/GameLogic/ObjectDefines.h @@ -24,9 +24,9 @@ namespace GameLogic struct ObjectHeader : public ObjectTypeHeader { //Model, - + int ModelID; //Texture - + int TextureID; //Position float position[3]; //Rotation diff --git a/Code/Game/GameLogic/ParseFunctions.cpp b/Code/Game/GameLogic/ParseFunctions.cpp new file mode 100644 index 00000000..28199e6b --- /dev/null +++ b/Code/Game/GameLogic/ParseFunctions.cpp @@ -0,0 +1,83 @@ +////////////////////////////////// +// Created by Sam Svensson 2013 // +////////////////////////////////// + +#include "ParserFunctions.h" +#include "../../Misc/Packing/Packing.h" +#include + +using namespace Oyster::Packing; +using namespace GameLogic; +using namespace std; + +ObjectTypeHeader parseObjectTypeHeader(unsigned char* buffer) +{ + int i = Unpacki(buffer); + + struct ObjectTypeHeader header; + header.typeID = (ObjectType)i; + + return header; +} +ObjectHeader parseObjectHeader (unsigned char* buffer) +{ + struct ObjectHeader header; + int x, y,z; + string s; + int start = 0; + + + //ModelID + x = Unpacki(buffer); + header.ModelID = (ObjectType)x; + + //TextureID + start += 4; + x = Unpacki(&buffer[start]); + header.TextureID = x; + + //Position + start += 4; + x = Unpacki(&buffer[start]); + + start += 4; + y = Unpacki(&buffer[start]); + + start += 4; + z = Unpacki(&buffer[start]); + + header.position[0] = x; + header.position[1] = y; + header.position[2] = z; + + //Rotation + start += 4; + x = Unpacki(&buffer[start]); + + start += 4; + y = Unpacki(&buffer[start]); + + start += 4; + z = Unpacki(&buffer[start]); + + header.rotation[0] = x; + header.rotation[1] = y; + header.rotation[2] = z; + + //Scale + start += 4; + x = Unpacki(&buffer[start]); + + start += 4; + y = Unpacki(&buffer[start]); + + start += 4; + z = Unpacki(&buffer[start]); + + header.scale[0] = x; + header.scale[1] = y; + header.scale[2] = z; + + + return header; +} \ No newline at end of file diff --git a/Code/Game/GameLogic/ParserFunctions.h b/Code/Game/GameLogic/ParserFunctions.h new file mode 100644 index 00000000..0608e77a --- /dev/null +++ b/Code/Game/GameLogic/ParserFunctions.h @@ -0,0 +1,22 @@ +////////////////////////////////// +// Created by Sam Svensson 2013 // +////////////////////////////////// + +#ifndef PARSERFUNCTIONS_H +#define PARSERFUNCTIONS_H +#include "ObjectDefines.h" + +namespace GameLogic +{ + namespace LevelFileLoader + { + namespace parseFunctions + { + ObjectTypeHeader parseObjectTypeHeader(unsigned char* buffer); //send in a char buffer, this function will seperate it and then return the right struct with the values. + ObjectHeader parseObjectHeader (unsigned char* buffer); //send in a char buffer, this function will seperate it and then return the right struct with the values. + } + } +} + + +#endif \ No newline at end of file diff --git a/Code/Misc/Misc.vcxproj b/Code/Misc/Misc.vcxproj index 0b7bf576..6c3db1ec 100644 --- a/Code/Misc/Misc.vcxproj +++ b/Code/Misc/Misc.vcxproj @@ -146,6 +146,7 @@ + @@ -164,6 +165,7 @@ + diff --git a/Code/Misc/Misc.vcxproj.filters b/Code/Misc/Misc.vcxproj.filters index 44348332..8413642a 100644 --- a/Code/Misc/Misc.vcxproj.filters +++ b/Code/Misc/Misc.vcxproj.filters @@ -48,6 +48,9 @@ Source Files + + Source Files + @@ -110,5 +113,8 @@ Header Files + + Header Files + \ No newline at end of file diff --git a/Code/Misc/Packing/Packing.cpp b/Code/Misc/Packing/Packing.cpp new file mode 100644 index 00000000..30064b0a --- /dev/null +++ b/Code/Misc/Packing/Packing.cpp @@ -0,0 +1,346 @@ +#include "Packing.h" + +/*************************** + Packing +***************************/ + +#include + +namespace Oyster +{ + namespace Packing + { + //bool (1-bit) + void Pack(unsigned char buffer[], bool i) + { + *buffer++ = i; + } + + //char (8-bit) + void Pack(unsigned char buffer[], char i) + { + *buffer++ = i; + } + + void Pack(unsigned char buffer[], unsigned char i) + { + *buffer++ = i; + } + + //short (16-bit) + void Pack(unsigned char buffer[], short i) + { + *buffer++ = i >> 8; + *buffer++ = (char)i; + } + + void Pack(unsigned char buffer[], unsigned short i) + { + *buffer++ = i >> 8; + *buffer++ = (char)i; + } + + //int (32-bit) + void Pack(unsigned char buffer[], int i) + { + *buffer++ = i >> 24; + *buffer++ = i >> 16; + *buffer++ = i >> 8; + *buffer++ = i; + } + + void Pack(unsigned char buffer[], unsigned int i) + { + *buffer++ = i >> 24; + *buffer++ = i >> 16; + *buffer++ = i >> 8; + *buffer++ = i; + } + + //__int64 (64-bit) + void Pack(unsigned char buffer[], __int64 i) + { + *buffer++ = (char)(i >> 56); + *buffer++ = (char)(i >> 48); + *buffer++ = (char)(i >> 40); + *buffer++ = (char)(i >> 32); + *buffer++ = (char)(i >> 24); + *buffer++ = (char)(i >> 16); + *buffer++ = (char)(i >> 8); + *buffer++ = (char)i; + } + + void Pack(unsigned char buffer[], unsigned __int64 i) + { + *buffer++ = (char)(i >> 56); + *buffer++ = (char)(i >> 48); + *buffer++ = (char)(i >> 40); + *buffer++ = (char)(i >> 32); + *buffer++ = (char)(i >> 24); + *buffer++ = (char)(i >> 16); + *buffer++ = (char)(i >> 8); + *buffer++ = (char)i; + } + + //floating point (32, 64-bit) + void Pack(unsigned char buffer[], float i) + { + int tempFloat = (int)Pack754(i, 32, 8); + Pack(buffer, tempFloat); + } + + void Pack(unsigned char buffer[], double i) + { + __int64 tempDouble = Pack754(i, 64, 11); + Pack(buffer, tempDouble); + } + + //string + void Pack(unsigned char buffer[], char str[]) + { + short len = (short)strlen(str); + Pack(buffer, len); + buffer += 2; + memcpy(buffer, str, len); + } + + void Pack(unsigned char buffer[], std::string& str) + { + short len = (short)str.length(); + Pack(buffer, len); + buffer += 2; + memcpy(buffer, str.c_str(), len); + } + + unsigned __int64 Pack754(long double f, unsigned bits, unsigned expbits) + { + long double fnorm; + int shift; + long long sign, exp, significand; + unsigned significandbits = bits - expbits - 1; // -1 for sign bit + + if (f == 0.0) + return 0; // get this special case out of the way + + // check sign and begin normalization + if (f < 0) + { + sign = 1; + fnorm = -f; + } + else + { + sign = 0; + fnorm = f; + } + + // get the normalized form of f and track the exponent + shift = 0; + while(fnorm >= 2.0) + { + fnorm /= 2.0; + shift++; + } + + while(fnorm < 1.0) + { + fnorm *= 2.0; + shift--; + } + + fnorm = fnorm - 1.0; + + // calculate the binary form (non-float) of the significand data + significand = (long long)(fnorm * ((1LL << significandbits) + 0.5f)); + + // get the biased exponent + exp = shift + ((1 << (expbits - 1)) - 1); // shift + bias + + // return the final answer + return (sign << (bits - 1)) | (exp << (bits - expbits - 1)) | significand; + } + + /****************************** + Unpacking + ******************************/ + + //bool (1-bit) + bool Unpackb(unsigned char buffer[]) + { + return *buffer; + } + + //char (8-bit) + char Unpackc(unsigned char buffer[]) + { + if(*buffer <= 0x7f) + { + return *buffer; + } + else + { + return (-1 - (unsigned char)(0xffu - *buffer)); + } + } + + unsigned char UnpackC(unsigned char buffer[]) + { + return *buffer; + } + + //short (16-bit) + short Unpacks(unsigned char buffer[]) + { + short i = ((short)buffer[0] << 8) | buffer[1]; + + if(i > 0x7fffu) + { + i = -1 - (unsigned short)(0xffffu - i); + } + + return i; + } + + unsigned short UnpackS(unsigned char buffer[]) + { + return ((unsigned int)buffer[0] << 8) | buffer[1]; + } + + //int (32-bit) + int Unpacki(unsigned char buffer[]) + { + int i = ((int)buffer[0] << 24) | + ((int)buffer[1] << 16) | + ((int)buffer[2] << 8) | + ((int)buffer[3]); + + if(i > 0x7fffffffu) + { + i = -1 - (int)(0xffffffffu - i); + } + + return i; + } + + unsigned int UnpackI(unsigned char buffer[]) + { + return ((unsigned int)buffer[0] << 24) | + ((unsigned int)buffer[1] << 16) | + ((unsigned int)buffer[2] << 8) | + ((unsigned int)buffer[3]); + } + + //__int64 (64-bit) + __int64 Unpacki64(unsigned char buffer[]) + { + __int64 i = ((__int64)buffer[0] << 56) | + ((__int64)buffer[1] << 48) | + ((__int64)buffer[2] << 40) | + ((__int64)buffer[3] << 32) | + ((__int64)buffer[4] << 24) | + ((__int64)buffer[5] << 16) | + ((__int64)buffer[6] << 8) | + (buffer[7]); + + if(i > 0x7fffffffffffffffu) + { + i = -1 - (__int64)(0xffffffffffffffffu - i); + } + + return i; + } + + unsigned __int64 UnpackI64(unsigned char buffer[]) + { + + return ((__int64)buffer[0] << 56) | + ((__int64)buffer[1] << 48) | + ((__int64)buffer[2] << 40) | + ((__int64)buffer[3] << 32) | + ((__int64)buffer[4] << 24) | + ((__int64)buffer[5] << 16) | + ((__int64)buffer[6] << 8) | + ((__int64)buffer[7]); + } + + //floating point (32, 64-bit) + float Unpackf(unsigned char buffer[]) + { + int tempFloat = Unpacki(buffer); + return (float)Unpack754(tempFloat, 32, 8); + } + + double Unpackd(unsigned char buffer[]) + { + __int64 tempDouble = Unpacki64(buffer); + return Unpack754(tempDouble, 64, 11); + } + + //string + char* UnpackCStr(unsigned char buffer[]) + { + short len = UnpackS(buffer); + char* str = new char[len+1]; + + buffer += 2; + for(int i = 0; i < len; i++) + { + str[i] = buffer[i]; + } + + str[len] = '\0'; + + return str; + } + + std::string UnpackStr(unsigned char buffer[]) + { + short len = UnpackS(buffer); + std::string temp; + temp.resize(len); + + buffer += 2; + for(int i = 0; i < len; i++) + { + temp[i] = buffer[i]; + } + + return temp; + } + + long double Unpack754(unsigned __int64 i, unsigned bits, unsigned expbits) + { + long double result; + long long shift; + unsigned bias; + unsigned significandbits = bits - expbits - 1; // -1 for sign bit + + if (i == 0) + return 0.0; + + // pull the significand + result = (long double)(i&((1LL << significandbits) - 1)); // mask + result /= (1LL << significandbits); // convert back to float + result += 1.0f; // add the one back on + + // deal with the exponent + bias = (1 << (expbits - 1)) - 1; + shift = ((i >> significandbits) & ((1LL << expbits) - 1)) - bias; + while(shift > 0) + { + result *= 2.0; + shift--; + } + while(shift < 0) + { + result /= 2.0; + shift++; + } + + // sign it + result *= (i >> (bits - 1)) & 1 ? -1.0 : 1.0; + + return result; + } + } +} diff --git a/Code/Misc/Packing/Packing.h b/Code/Misc/Packing/Packing.h new file mode 100644 index 00000000..47a65c51 --- /dev/null +++ b/Code/Misc/Packing/Packing.h @@ -0,0 +1,81 @@ +#ifndef PACKING_H +#define PACKING_H + +///////////////////////////////////// +// Created by Pontus Fransson 2013 // +///////////////////////////////////// + +#include + +/****************************** + Packing +******************************/ +namespace Oyster +{ + namespace Packing + { + //bool (1-bit) + void Pack(unsigned char buffer[], bool i); + + //char (8-bit) + void Pack(unsigned char buffer[], char i); + void Pack(unsigned char buffer[], unsigned char i); // unsigned + + //short (16-bit) + void Pack(unsigned char buffer[], short i); + void Pack(unsigned char buffer[], unsigned short i); // unsigned + + //int (32-bit) + void Pack(unsigned char buffer[], int i); + void Pack(unsigned char buffer[], unsigned int i); // unsigned + + //__int64 (64-bit) + void Pack(unsigned char buffer[], __int64 i); + void Pack(unsigned char buffer[], unsigned __int64 i); // unsigned + + //floating point (32, 64-bit) + void Pack(unsigned char buffer[], float i); + void Pack(unsigned char buffer[], double i); + + //string + void Pack(unsigned char buffer[], char str[]); + void Pack(unsigned char buffer[], std::string& str); + + unsigned __int64 Pack754(long double f, unsigned bits, unsigned expbits); + + /****************************** + Unpacking + ******************************/ + + //bool (1-bit) + bool Unpackb(unsigned char buffer[]); + + //char (8-bit) + char Unpackc(unsigned char buffer[]); + unsigned char UnpackC(unsigned char buffer[]); // unsigned + + //short (16-bit) + short Unpacks(unsigned char buffer[]); + unsigned short UnpackS(unsigned char buffer[]); // unsigned + + //int (32-bit) + int Unpacki(unsigned char buffer[]); + unsigned int UnpackI(unsigned char buffer[]); // unsigned + + //__int64 (64-bit) + __int64 Unpacki64(unsigned char buffer[]); + unsigned __int64 UnpackI64(unsigned char buffer[]); // unsigned + + //floating point (32, 64-bit) + float Unpackf(unsigned char buffer[]); + double Unpackd(unsigned char buffer[]); + + //string + char* UnpackCStr(unsigned char buffer[]); + std::string UnpackStr(unsigned char buffer[]); + + long double Unpack754(unsigned __int64 i, unsigned bits, unsigned expbits); + } +} + +#endif \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp b/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp index d2823b62..8f315797 100644 --- a/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp +++ b/Code/Network/NetworkDependencies/Messages/MessageHeader.cpp @@ -1,8 +1,9 @@ #include "MessageHeader.h" -#include "../Packing.h" +#include "../../../Misc/Packing/Packing.h" #include using namespace std; +using namespace Oyster::Packing; using namespace Oyster::Network; using namespace Oyster::Network::Messages; using namespace Oyster::Network::Protocols; diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj index 4d2ad291..c0048016 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj @@ -157,7 +157,6 @@ - @@ -172,7 +171,6 @@ - diff --git a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters index eadbbeb3..cce2fe94 100644 --- a/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters +++ b/Code/Network/NetworkDependencies/NetworkDependencies.vcxproj.filters @@ -7,7 +7,6 @@ - @@ -22,7 +21,6 @@ - diff --git a/Code/Network/NetworkDependencies/Packing.cpp b/Code/Network/NetworkDependencies/Packing.cpp deleted file mode 100644 index 41d059a3..00000000 --- a/Code/Network/NetworkDependencies/Packing.cpp +++ /dev/null @@ -1,486 +0,0 @@ -#include "Packing.h" - -/*************************** - Packing -***************************/ - -#include - -namespace Oyster -{ - namespace Network - { - namespace Packing - { - //bool (1-bit) - void Pack(unsigned char buffer[], bool i) - { - *buffer++ = i; - } - - //char (8-bit) - void Pack(unsigned char buffer[], char i) - { - *buffer++ = i; - } - - void Pack(unsigned char buffer[], unsigned char i) - { - *buffer++ = i; - } - - //short (16-bit) - void Pack(unsigned char buffer[], short i) - { - *buffer++ = i >> 8; - *buffer++ = (char)i; - } - - void Pack(unsigned char buffer[], unsigned short i) - { - *buffer++ = i >> 8; - *buffer++ = (char)i; - } - - //int (32-bit) - void Pack(unsigned char buffer[], int i) - { - *buffer++ = i >> 24; - *buffer++ = i >> 16; - *buffer++ = i >> 8; - *buffer++ = i; - } - - void Pack(unsigned char buffer[], unsigned int i) - { - *buffer++ = i >> 24; - *buffer++ = i >> 16; - *buffer++ = i >> 8; - *buffer++ = i; - } - - //__int64 (64-bit) - void Pack(unsigned char buffer[], __int64 i) - { - *buffer++ = (char)(i >> 56); - *buffer++ = (char)(i >> 48); - *buffer++ = (char)(i >> 40); - *buffer++ = (char)(i >> 32); - *buffer++ = (char)(i >> 24); - *buffer++ = (char)(i >> 16); - *buffer++ = (char)(i >> 8); - *buffer++ = (char)i; - } - - void Pack(unsigned char buffer[], unsigned __int64 i) - { - *buffer++ = (char)(i >> 56); - *buffer++ = (char)(i >> 48); - *buffer++ = (char)(i >> 40); - *buffer++ = (char)(i >> 32); - *buffer++ = (char)(i >> 24); - *buffer++ = (char)(i >> 16); - *buffer++ = (char)(i >> 8); - *buffer++ = (char)i; - } - - //floating point (32, 64-bit) - void Pack(unsigned char buffer[], float i) - { - int tempFloat = (int)Pack754(i, 32, 8); - Pack(buffer, tempFloat); - } - - void Pack(unsigned char buffer[], double i) - { - __int64 tempDouble = Pack754(i, 64, 11); - Pack(buffer, tempDouble); - } - - //string - void Pack(unsigned char buffer[], char str[]) - { - short len = (short)strlen(str); - Pack(buffer, len); - buffer += 2; - memcpy(buffer, str, len); - } - - void Pack(unsigned char buffer[], std::string& str) - { - short len = (short)str.length(); - Pack(buffer, len); - buffer += 2; - memcpy(buffer, str.c_str(), len); - } - - unsigned __int64 Pack754(long double f, unsigned bits, unsigned expbits) - { - long double fnorm; - int shift; - long long sign, exp, significand; - unsigned significandbits = bits - expbits - 1; // -1 for sign bit - - if (f == 0.0) - return 0; // get this special case out of the way - - // check sign and begin normalization - if (f < 0) - { - sign = 1; - fnorm = -f; - } - else - { - sign = 0; - fnorm = f; - } - - // get the normalized form of f and track the exponent - shift = 0; - while(fnorm >= 2.0) - { - fnorm /= 2.0; - shift++; - } - - while(fnorm < 1.0) - { - fnorm *= 2.0; - shift--; - } - - fnorm = fnorm - 1.0; - - // calculate the binary form (non-float) of the significand data - significand = (long long)(fnorm * ((1LL << significandbits) + 0.5f)); - - // get the biased exponent - exp = shift + ((1 << (expbits - 1)) - 1); // shift + bias - - // return the final answer - return (sign << (bits - 1)) | (exp << (bits - expbits - 1)) | significand; - } - - /****************************** - Unpacking - ******************************/ - - //bool (1-bit) - bool Unpackb(unsigned char buffer[]) - { - return *buffer; - } - - //char (8-bit) - char Unpackc(unsigned char buffer[]) - { - if(*buffer <= 0x7f) - { - return *buffer; - } - else - { - return (-1 - (unsigned char)(0xffu - *buffer)); - } - } - - unsigned char UnpackC(unsigned char buffer[]) - { - return *buffer; - } - - //short (16-bit) - short Unpacks(unsigned char buffer[]) - { - short i = ((short)buffer[0] << 8) | buffer[1]; - - if(i > 0x7fffu) - { - i = -1 - (unsigned short)(0xffffu - i); - } - - return i; - } - - unsigned short UnpackS(unsigned char buffer[]) - { - return ((unsigned int)buffer[0] << 8) | buffer[1]; - } - - //int (32-bit) - int Unpacki(unsigned char buffer[]) - { - int i = ((int)buffer[0] << 24) | - ((int)buffer[1] << 16) | - ((int)buffer[2] << 8) | - ((int)buffer[3]); - - if(i > 0x7fffffffu) - { - i = -1 - (int)(0xffffffffu - i); - } - - return i; - } - - unsigned int UnpackI(unsigned char buffer[]) - { - return ((unsigned int)buffer[0] << 24) | - ((unsigned int)buffer[1] << 16) | - ((unsigned int)buffer[2] << 8) | - ((unsigned int)buffer[3]); - } - - //__int64 (64-bit) - __int64 Unpacki64(unsigned char buffer[]) - { - __int64 i = ((__int64)buffer[0] << 56) | - ((__int64)buffer[1] << 48) | - ((__int64)buffer[2] << 40) | - ((__int64)buffer[3] << 32) | - ((__int64)buffer[4] << 24) | - ((__int64)buffer[5] << 16) | - ((__int64)buffer[6] << 8) | - (buffer[7]); - - if(i > 0x7fffffffffffffffu) - { - i = -1 - (__int64)(0xffffffffffffffffu - i); - } - - return i; - } - - unsigned __int64 UnpackI64(unsigned char buffer[]) - { - - return ((__int64)buffer[0] << 56) | - ((__int64)buffer[1] << 48) | - ((__int64)buffer[2] << 40) | - ((__int64)buffer[3] << 32) | - ((__int64)buffer[4] << 24) | - ((__int64)buffer[5] << 16) | - ((__int64)buffer[6] << 8) | - ((__int64)buffer[7]); - } - - //floating point (32, 64-bit) - float Unpackf(unsigned char buffer[]) - { - int tempFloat = Unpacki(buffer); - return (float)Unpack754(tempFloat, 32, 8); - } - - double Unpackd(unsigned char buffer[]) - { - __int64 tempDouble = Unpacki64(buffer); - return Unpack754(tempDouble, 64, 11); - } - - //string - char* UnpackCStr(unsigned char buffer[]) - { - short len = UnpackS(buffer); - char* str = new char[len+1]; - - buffer += 2; - for(int i = 0; i < len; i++) - { - str[i] = buffer[i]; - } - - str[len] = '\0'; - - return str; - } - - std::string UnpackStr(unsigned char buffer[]) - { - short len = UnpackS(buffer); - std::string temp; - temp.resize(len); - - buffer += 2; - for(int i = 0; i < len; i++) - { - temp[i] = buffer[i]; - } - - return temp; - } - - long double Unpack754(unsigned __int64 i, unsigned bits, unsigned expbits) - { - long double result; - long long shift; - unsigned bias; - unsigned significandbits = bits - expbits - 1; // -1 for sign bit - - if (i == 0) - return 0.0; - - // pull the significand - result = (long double)(i&((1LL << significandbits) - 1)); // mask - result /= (1LL << significandbits); // convert back to float - result += 1.0f; // add the one back on - - // deal with the exponent - bias = (1 << (expbits - 1)) - 1; - shift = ((i >> significandbits) & ((1LL << expbits) - 1)) - bias; - while(shift > 0) - { - result *= 2.0; - shift--; - } - while(shift < 0) - { - result /= 2.0; - shift++; - } - - // sign it - result *= (i >> (bits - 1)) & 1 ? -1.0 : 1.0; - - return result; - } - } - } -} - -/* -int32_t pack(unsigned char* buffer, char* format, ...) -{ - va_list ap; - int16_t h; - int32_t l; - int8_t c; - float f; - double d; - char* s; - int32_t size = 0, len; - - va_start(ap, format); - - for(; *format != '\0'; format++) - { - switch(*format) - { - case 'h': // 16-bit - size += 2; - h = (int16_t)va_arg(ap, int); - packi16(buffer, h); - buffer += 2; - break; - case 'l': // 32-bit - size += 4; - l = va_arg(ap, int32_t); - packi32(buffer, l); - buffer += 4; - break; - case 'c': // 8-bit - size += 1; - c = (int8_t)va_arg(ap, int); - *buffer++ = (c >> 0)&0xff; - break; - case 'f': // float (32-bit) - size += 4; - f = (float)va_arg(ap, double); - //l = pack754(f, 32, 8); - packi32(buffer, l); - buffer += 4; - break; - case 'd': // double (64-bit) - size += 8; - d = (float)va_arg(ap, double); - //l = pack754(f, 64, 11); - packi32(buffer, l); - buffer += 4; - break; - case 's': // string - s = va_arg(ap, char*); - len = strlen(s); - size += len + 2; - packi16(buffer, len); - buffer += 2; - memcpy(buffer, s, len); - buffer += len; - break; - } - } - - va_end(ap); - - return size; -} -*/ - -/* -void unpack(unsigned char* buffer, char* format, ...) -{ - va_list ap; - int16_t* h; - int32_t* l; - int32_t pf; - int64_t pd; - int8_t* c; - float* f; - double* d; - char* s; - int32_t len, count, maxstrlen = 0; - - va_start(ap, format); - - for(; *format != '\0'; format++) - { - switch(*format) - { - case 'h': // 16-bit - h = va_arg(ap, int16_t*); - *h = unpacki16(buffer); - buffer += 2; - break; - case 'l': // 32-bit - l = va_arg(ap, int32_t*); - *l = unpacki32(buffer); - buffer += 4; - break; - case 'c': // 8-bit - c = va_arg(ap, int8_t*); - *c = *buffer++; - break; - case 'f': // float - f = va_arg(ap, float*); - pf = unpacki32(buffer); - buffer += 4; - //*f = unpack754(pf, 32, 8); - break; - case 'd': // double (64-bit) - d = va_arg(ap, double*); - pd = unpacki64(buffer); - buffer += 8; - //*d = unpack754(pf, 64, 11); - break; - case 's': // string - s = va_arg(ap, char*); - len = unpacki16(buffer); - buffer += 2; - if (maxstrlen > 0 && len > maxstrlen) count = maxstrlen - 1; - else count = len; - memcpy(s, buffer, count); - s[count] = '\0'; - buffer += len; - break; - default: - if (isdigit(*format)) // track max str len - { - maxstrlen = maxstrlen * 10 + (*format-'0'); - } - } - - if(!isdigit(*format)) - maxstrlen = 0; - } - - va_end(ap); -}*/ \ No newline at end of file diff --git a/Code/Network/NetworkDependencies/Packing.h b/Code/Network/NetworkDependencies/Packing.h deleted file mode 100644 index aaf3a1b8..00000000 --- a/Code/Network/NetworkDependencies/Packing.h +++ /dev/null @@ -1,107 +0,0 @@ -#ifndef PACKING_H -#define PACKING_H - -///////////////////////////////////// -// Created by Pontus Fransson 2013 // -///////////////////////////////////// - -#include - -/****************************** - Packing -******************************/ -namespace Oyster -{ - namespace Network - { - namespace Packing - { - //bool (1-bit) - void Pack(unsigned char buffer[], bool i); - - //char (8-bit) - void Pack(unsigned char buffer[], char i); - void Pack(unsigned char buffer[], unsigned char i); // unsigned - - //short (16-bit) - void Pack(unsigned char buffer[], short i); - void Pack(unsigned char buffer[], unsigned short i); // unsigned - - //int (32-bit) - void Pack(unsigned char buffer[], int i); - void Pack(unsigned char buffer[], unsigned int i); // unsigned - - //__int64 (64-bit) - void Pack(unsigned char buffer[], __int64 i); - void Pack(unsigned char buffer[], unsigned __int64 i); // unsigned - - //floating point (32, 64-bit) - void Pack(unsigned char buffer[], float i); - void Pack(unsigned char buffer[], double i); - - //string - void Pack(unsigned char buffer[], char str[]); - void Pack(unsigned char buffer[], std::string& str); - - unsigned __int64 Pack754(long double f, unsigned bits, unsigned expbits); - - /****************************** - Unpacking - ******************************/ - - //bool (1-bit) - bool Unpackb(unsigned char buffer[]); - - //char (8-bit) - char Unpackc(unsigned char buffer[]); - unsigned char UnpackC(unsigned char buffer[]); // unsigned - - //short (16-bit) - short Unpacks(unsigned char buffer[]); - unsigned short UnpackS(unsigned char buffer[]); // unsigned - - //int (32-bit) - int Unpacki(unsigned char buffer[]); - unsigned int UnpackI(unsigned char buffer[]); // unsigned - - //__int64 (64-bit) - __int64 Unpacki64(unsigned char buffer[]); - unsigned __int64 UnpackI64(unsigned char buffer[]); // unsigned - - //floating point (32, 64-bit) - float Unpackf(unsigned char buffer[]); - double Unpackd(unsigned char buffer[]); - - //string - char* UnpackCStr(unsigned char buffer[]); - std::string UnpackStr(unsigned char buffer[]); - - long double Unpack754(unsigned __int64 i, unsigned bits, unsigned expbits); - } - } -} - - -//int32_t pack(unsigned char* buffer, char* format, ...); - -//void unpack(unsigned char* buffer, char* format, ...); - - -/*********************************************** -* This table is used for naming pack/unpack functions. -* It's also used to identify types in the 'format' string in function pack()/unpack() -* -* bits |signed unsigned float string -* -----+---------------------------------- -* 1 | b -* 8 | c C -* 16 | s S f -* 32 | i I d -* 64 | q Q g -* - | str -* -* (16-bit unsigned length is automatically added in front of strings) -* -*/ - -#endif \ No newline at end of file From 51a1b3ecff054787361f4021826a5c6b0de97f4b Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Mon, 27 Jan 2014 13:54:56 +0100 Subject: [PATCH 06/23] GL - Resource get size --- Code/Misc/Resource/OResourceHandler.cpp | 7 +++++++ Code/Misc/Resource/OysterResource.h | 5 +++++ 2 files changed, 12 insertions(+) diff --git a/Code/Misc/Resource/OResourceHandler.cpp b/Code/Misc/Resource/OResourceHandler.cpp index 918ecd1d..52d93af0 100644 --- a/Code/Misc/Resource/OResourceHandler.cpp +++ b/Code/Misc/Resource/OResourceHandler.cpp @@ -214,7 +214,14 @@ int OysterResource::GetResourceId(const wchar_t c[]) return -1; } +int OysterResource::GetResourceSize(const OHRESOURCE& resource) +{ + OResource* t = resourcePrivate.FindResource(resource); + if(t) return t->GetResourceSize(); + + return -1; +} OResource* ResourcePrivate::FindResource(const OHRESOURCE& h) const { diff --git a/Code/Misc/Resource/OysterResource.h b/Code/Misc/Resource/OysterResource.h index d6509b54..8adce704 100644 --- a/Code/Misc/Resource/OysterResource.h +++ b/Code/Misc/Resource/OysterResource.h @@ -150,6 +150,11 @@ namespace Oyster * @return Returns the accociated ID */ static int GetResourceId(const wchar_t filename[]); + + + static int GetResourceSize(const OHRESOURCE& resource); + + }; } From 91bf6ce901e4b56d1e7ac3fba9a23541be924c30 Mon Sep 17 00:00:00 2001 From: Sam Mario Svensson Date: Mon, 27 Jan 2014 13:57:18 +0100 Subject: [PATCH 07/23] GL- LevelFormat fix --- Bin/map.txt | Bin 0 -> 48 bytes Code/Game/GameLogic/GameLogic.vcxproj | 2 +- Code/Game/GameLogic/LevelParser.cpp | 26 +++--- Code/Game/GameLogic/ParseFunctions.cpp | 110 +++++++++++++------------ Code/Game/GameLogic/ParseFunctions.h | 19 +++++ Code/Game/GameLogic/ParserFunctions.h | 22 ----- 6 files changed, 94 insertions(+), 85 deletions(-) create mode 100644 Bin/map.txt create mode 100644 Code/Game/GameLogic/ParseFunctions.h delete mode 100644 Code/Game/GameLogic/ParserFunctions.h diff --git a/Bin/map.txt b/Bin/map.txt new file mode 100644 index 0000000000000000000000000000000000000000..c9d1861205c2960ed93f42a764db732d9c326d2e GIT binary patch literal 48 gcmZQzU|?imU|<4bW~XmUqMWnxW;m}o+K7t{08$7NVE_OC literal 0 HcmV?d00001 diff --git a/Code/Game/GameLogic/GameLogic.vcxproj b/Code/Game/GameLogic/GameLogic.vcxproj index 54ec2d7e..df1d09aa 100644 --- a/Code/Game/GameLogic/GameLogic.vcxproj +++ b/Code/Game/GameLogic/GameLogic.vcxproj @@ -189,7 +189,7 @@ - + diff --git a/Code/Game/GameLogic/LevelParser.cpp b/Code/Game/GameLogic/LevelParser.cpp index 5a709316..a03da6eb 100644 --- a/Code/Game/GameLogic/LevelParser.cpp +++ b/Code/Game/GameLogic/LevelParser.cpp @@ -1,10 +1,12 @@ #include "LevelParser.h" #include "Loader.h" +#include "ParseFunctions.h" using namespace GameLogic; using namespace ::LevelFileLoader; + LevelParser::LevelParser() { } @@ -16,22 +18,23 @@ LevelParser::~LevelParser() // std::vector LevelParser::Parse(std::string filename) { + int stringSize = 0; //Read entire level file. Loader loader; - unsigned char* buffer = (unsigned char*)loader.LoadFile(filename.c_str()); + unsigned char* buffer = (unsigned char*)loader.LoadFile(filename.c_str(), stringSize); std::vector objects; unsigned int counter = 0; - unsigned int stringSize = 0; + while(counter < stringSize) { //Get typeID - int typeID = 0; - + ObjectTypeHeader typeID; + typeID = parseObjectTypeHeader(buffer); //Unpack ID - switch(typeID) + switch((int)typeID.typeID) { case TypeID_LevelHeader: //Call function @@ -51,24 +54,27 @@ std::vector LevelParser::Parse(std::string filename) return objects; } -// +//för meta information om leveln. Måste göra så den returnerar rätt strukt så fort vi +//vi definierat en! ObjectTypeHeader LevelParser::ParseHeader(std::string filename) { + int stringSize = 0; //Read entire level file. Loader loader; - unsigned char* buffer = (unsigned char*)loader.LoadFile(filename.c_str()); + unsigned char* buffer = (unsigned char*)loader.LoadFile(filename.c_str(), stringSize); //Find the header in the returned string. unsigned int counter = 0; - unsigned int stringSize = 0; + ObjectTypeHeader header; header.typeID = ObjectType_Level; while(counter < stringSize) { - int typeID = 0; - switch(typeID) + ObjectTypeHeader typeID; + typeID = parseObjectTypeHeader(buffer); + switch(typeID.typeID) { case TypeID_LevelHeader: //Call function diff --git a/Code/Game/GameLogic/ParseFunctions.cpp b/Code/Game/GameLogic/ParseFunctions.cpp index 28199e6b..11e7b0ab 100644 --- a/Code/Game/GameLogic/ParseFunctions.cpp +++ b/Code/Game/GameLogic/ParseFunctions.cpp @@ -2,82 +2,88 @@ // Created by Sam Svensson 2013 // ////////////////////////////////// -#include "ParserFunctions.h" +#include "ParseFunctions.h" #include "../../Misc/Packing/Packing.h" #include using namespace Oyster::Packing; +using namespace GameLogic::LevelFileLoader; using namespace GameLogic; using namespace std; -ObjectTypeHeader parseObjectTypeHeader(unsigned char* buffer) +namespace GameLogic { - int i = Unpacki(buffer); + namespace LevelFileLoader + { + ObjectTypeHeader parseObjectTypeHeader(unsigned char* buffer) + { + struct ObjectTypeHeader header; + int i = Unpacki(buffer); + header.typeID = (ObjectType)i; + return header; + } - struct ObjectTypeHeader header; - header.typeID = (ObjectType)i; - - return header; -} -ObjectHeader parseObjectHeader (unsigned char* buffer) -{ - struct ObjectHeader header; - int x, y,z; - string s; - int start = 0; + ObjectHeader parseObjectHeader (unsigned char* buffer) + { + struct ObjectHeader header; + int x, y,z; + string s; + int start = 0; - //ModelID - x = Unpacki(buffer); - header.ModelID = (ObjectType)x; + //ModelID + x = Unpacki(buffer); + header.ModelID = (ObjectType)x; - //TextureID - start += 4; - x = Unpacki(&buffer[start]); - header.TextureID = x; + //TextureID + start += 4; + x = Unpacki(&buffer[start]); + header.TextureID = x; - //Position - start += 4; - x = Unpacki(&buffer[start]); + //Position + start += 4; + x = Unpacki(&buffer[start]); - start += 4; - y = Unpacki(&buffer[start]); + start += 4; + y = Unpacki(&buffer[start]); - start += 4; - z = Unpacki(&buffer[start]); + start += 4; + z = Unpacki(&buffer[start]); - header.position[0] = x; - header.position[1] = y; - header.position[2] = z; + header.position[0] = x; + header.position[1] = y; + header.position[2] = z; - //Rotation - start += 4; - x = Unpacki(&buffer[start]); + //Rotation + start += 4; + x = Unpacki(&buffer[start]); - start += 4; - y = Unpacki(&buffer[start]); + start += 4; + y = Unpacki(&buffer[start]); - start += 4; - z = Unpacki(&buffer[start]); + start += 4; + z = Unpacki(&buffer[start]); - header.rotation[0] = x; - header.rotation[1] = y; - header.rotation[2] = z; + header.rotation[0] = x; + header.rotation[1] = y; + header.rotation[2] = z; - //Scale - start += 4; - x = Unpacki(&buffer[start]); + //Scale + start += 4; + x = Unpacki(&buffer[start]); - start += 4; - y = Unpacki(&buffer[start]); + start += 4; + y = Unpacki(&buffer[start]); - start += 4; - z = Unpacki(&buffer[start]); + start += 4; + z = Unpacki(&buffer[start]); - header.scale[0] = x; - header.scale[1] = y; - header.scale[2] = z; + header.scale[0] = x; + header.scale[1] = y; + header.scale[2] = z; - return header; + return header; + } + } } \ No newline at end of file diff --git a/Code/Game/GameLogic/ParseFunctions.h b/Code/Game/GameLogic/ParseFunctions.h new file mode 100644 index 00000000..180f82c3 --- /dev/null +++ b/Code/Game/GameLogic/ParseFunctions.h @@ -0,0 +1,19 @@ +////////////////////////////////// +// Created by Sam Svensson 2013 // +////////////////////////////////// + +#ifndef PARSERFUNCTIONS_H +#define PARSERFUNCTIONS_H +#include "ObjectDefines.h" + +namespace GameLogic +{ + namespace LevelFileLoader + { + ObjectTypeHeader parseObjectTypeHeader(unsigned char* buffer); //send in a char buffer, this function will seperate it and then return the right struct with the values. + ObjectHeader parseObjectHeader (unsigned char* buffer); //send in a char buffer, this function will seperate it and then return the right struct with the values. + } +} + + +#endif \ No newline at end of file diff --git a/Code/Game/GameLogic/ParserFunctions.h b/Code/Game/GameLogic/ParserFunctions.h deleted file mode 100644 index 0608e77a..00000000 --- a/Code/Game/GameLogic/ParserFunctions.h +++ /dev/null @@ -1,22 +0,0 @@ -////////////////////////////////// -// Created by Sam Svensson 2013 // -////////////////////////////////// - -#ifndef PARSERFUNCTIONS_H -#define PARSERFUNCTIONS_H -#include "ObjectDefines.h" - -namespace GameLogic -{ - namespace LevelFileLoader - { - namespace parseFunctions - { - ObjectTypeHeader parseObjectTypeHeader(unsigned char* buffer); //send in a char buffer, this function will seperate it and then return the right struct with the values. - ObjectHeader parseObjectHeader (unsigned char* buffer); //send in a char buffer, this function will seperate it and then return the right struct with the values. - } - } -} - - -#endif \ No newline at end of file From dd2b214122186dd1797ccd2d7af1b6388e14c8ab Mon Sep 17 00:00:00 2001 From: Sam Mario Svensson Date: Mon, 27 Jan 2014 14:57:18 +0100 Subject: [PATCH 08/23] =?UTF-8?q?GL-=20la=20in=20memcpy=20f=C3=B6r=20parsi?= =?UTF-8?q?ng=20i=20parseFunctions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Code/Game/GameLogic/LevelParser.cpp | 8 +-- Code/Game/GameLogic/Loader.cpp | 5 +- Code/Game/GameLogic/ParseFunctions.cpp | 70 +++----------------------- Code/Game/GameLogic/ParseFunctions.h | 4 +- 4 files changed, 17 insertions(+), 70 deletions(-) diff --git a/Code/Game/GameLogic/LevelParser.cpp b/Code/Game/GameLogic/LevelParser.cpp index a03da6eb..a957b88a 100644 --- a/Code/Game/GameLogic/LevelParser.cpp +++ b/Code/Game/GameLogic/LevelParser.cpp @@ -31,7 +31,8 @@ std::vector LevelParser::Parse(std::string filename) { //Get typeID ObjectTypeHeader typeID; - typeID = parseObjectTypeHeader(buffer); + typeID = ParseObjectTypeHeader(&buffer[counter]); + //counter += 4; //Unpack ID switch((int)typeID.typeID) @@ -43,7 +44,8 @@ std::vector LevelParser::Parse(std::string filename) case TypeID_Object: //Call function - counter += ObjectHeaderSize; + objects.push_back(ParseObjectHeader(&buffer[counter])); + counter += sizeof(ObjectHeader); break; default: //Couldn't find typeID. FAIL!!!!!! @@ -73,7 +75,7 @@ ObjectTypeHeader LevelParser::ParseHeader(std::string filename) while(counter < stringSize) { ObjectTypeHeader typeID; - typeID = parseObjectTypeHeader(buffer); + typeID = ParseObjectTypeHeader(buffer); switch(typeID.typeID) { case TypeID_LevelHeader: diff --git a/Code/Game/GameLogic/Loader.cpp b/Code/Game/GameLogic/Loader.cpp index 01047b2f..0afb48de 100644 --- a/Code/Game/GameLogic/Loader.cpp +++ b/Code/Game/GameLogic/Loader.cpp @@ -13,7 +13,8 @@ unsigned char* Loader::LoadFile(std::string fileName, int &size) //convert from string to wstring std::wstring temp(fileName.begin(), fileName.end()); - size = temp.size(); //convert from wstring to wchar then loads the file - return (unsigned char*)OysterResource::LoadResource(temp.c_str(), Oyster::Resource::ResourceType::ResourceType_Byte_Raw, -1 , false); + unsigned char* buffer = (unsigned char*)OysterResource::LoadResource(temp.c_str(), Oyster::Resource::ResourceType::ResourceType_Byte_Raw, -1 , false); + size = OysterResource::GetResourceSize(buffer); + return buffer; } \ No newline at end of file diff --git a/Code/Game/GameLogic/ParseFunctions.cpp b/Code/Game/GameLogic/ParseFunctions.cpp index 11e7b0ab..64405ff5 100644 --- a/Code/Game/GameLogic/ParseFunctions.cpp +++ b/Code/Game/GameLogic/ParseFunctions.cpp @@ -15,75 +15,19 @@ namespace GameLogic { namespace LevelFileLoader { - ObjectTypeHeader parseObjectTypeHeader(unsigned char* buffer) + ObjectTypeHeader ParseObjectTypeHeader(unsigned char* buffer) { struct ObjectTypeHeader header; - int i = Unpacki(buffer); - header.typeID = (ObjectType)i; + memcpy(&header, buffer, sizeof(ObjectTypeHeader)); return header; } - ObjectHeader parseObjectHeader (unsigned char* buffer) + ObjectHeader ParseObjectHeader (unsigned char* buffer) { - struct ObjectHeader header; - int x, y,z; - string s; - int start = 0; - - - //ModelID - x = Unpacki(buffer); - header.ModelID = (ObjectType)x; - - //TextureID - start += 4; - x = Unpacki(&buffer[start]); - header.TextureID = x; - - //Position - start += 4; - x = Unpacki(&buffer[start]); - - start += 4; - y = Unpacki(&buffer[start]); - - start += 4; - z = Unpacki(&buffer[start]); - - header.position[0] = x; - header.position[1] = y; - header.position[2] = z; - - //Rotation - start += 4; - x = Unpacki(&buffer[start]); - - start += 4; - y = Unpacki(&buffer[start]); - - start += 4; - z = Unpacki(&buffer[start]); - - header.rotation[0] = x; - header.rotation[1] = y; - header.rotation[2] = z; - - //Scale - start += 4; - x = Unpacki(&buffer[start]); - - start += 4; - y = Unpacki(&buffer[start]); - - start += 4; - z = Unpacki(&buffer[start]); - - header.scale[0] = x; - header.scale[1] = y; - header.scale[2] = z; - - - return header; + struct ObjectHeader header; + memcpy(&header, buffer, sizeof(ObjectHeader)); + + return header; } } } \ No newline at end of file diff --git a/Code/Game/GameLogic/ParseFunctions.h b/Code/Game/GameLogic/ParseFunctions.h index 180f82c3..5638f419 100644 --- a/Code/Game/GameLogic/ParseFunctions.h +++ b/Code/Game/GameLogic/ParseFunctions.h @@ -10,8 +10,8 @@ namespace GameLogic { namespace LevelFileLoader { - ObjectTypeHeader parseObjectTypeHeader(unsigned char* buffer); //send in a char buffer, this function will seperate it and then return the right struct with the values. - ObjectHeader parseObjectHeader (unsigned char* buffer); //send in a char buffer, this function will seperate it and then return the right struct with the values. + ObjectTypeHeader ParseObjectTypeHeader(unsigned char* buffer); //send in a char buffer, this function will seperate it and then return the right struct with the values. + ObjectHeader ParseObjectHeader (unsigned char* buffer); //send in a char buffer, this function will seperate it and then return the right struct with the values. } } From 91e825dbfba5e627aedc36dafa96de17d36a0d14 Mon Sep 17 00:00:00 2001 From: Dennis Andersen Date: Tue, 28 Jan 2014 09:00:02 +0100 Subject: [PATCH 09/23] Broekn stuff to tobias --- Code/DanBias.sln | 45 ++- Code/Dokumentation/GameServer.uxf | 181 +++++------ .../DanBiasGame/GameClientState/GameState.cpp | 2 +- .../AdminInterface/AdminInterface.cpp | 68 ---- .../AdminInterface/AdminInterface.h | 20 -- Code/Game/DanBiasServer/DanBiasServerAPI.cpp | 28 -- Code/Game/DanBiasServer/GameServer.cpp | 161 ---------- Code/Game/DanBiasServer/GameServer.h | 50 --- .../DanBiasServer/GameSession/GameClient.cpp | 67 ---- .../DanBiasServer/GameSession/GameClient.h | 43 --- Code/Game/DanBiasServer/Helpers/MapManager.h | 30 -- .../DanBiasServer/Helpers/ServerDataReader.h | 44 --- .../DanBiasServer/LobbySessions/GameLobby.h | 24 -- .../LobbySessions/INetworkSession.cpp | 18 -- .../LobbySessions/INetworkSession.h | 22 -- .../LobbySessions/LobbyClient.cpp | 51 --- .../DanBiasServer/LobbySessions/LobbyClient.h | 38 --- .../LobbyGeneralProtocolParser.cpp | 64 ---- .../LobbySessions/LobbyProtocolParser.cpp | 37 --- .../DanBiasServer/LobbySessions/MainLobby.cpp | 62 ---- .../DanBiasServer/LobbySessions/MainLobby.h | 49 --- .../LobbySessions/NetworkSession.cpp | 219 ------------- .../LobbySessions/NetworkSession.h | 91 ------ .../DanBiasServerLauncher.vcxproj | 8 +- .../DanBiasServerLauncher/ServerLauncher.cpp | 12 +- Code/Game/GameProtocols/LobbyProtocols.h | 16 +- Code/Game/GameServer/GameClient.h | 34 ++ Code/Game/GameServer/GameLobby.h | 48 +++ Code/Game/GameServer/GameServer.h | 33 ++ .../GameServer.vcxproj} | 77 +++-- .../GameServer.vcxproj.user} | 0 .../GameServerAPI.h} | 14 +- .../GameSession => GameServer}/GameSession.h | 16 +- .../GameSessionManager.h | 12 +- .../Implementation}/DLLMain.cpp | 0 .../GameServer/Implementation/GameClient.cpp | 50 +++ .../GameServer/Implementation/GameLobby.cpp | 51 +++ .../GameServer/Implementation/GameServer.cpp | 139 +++++++++ .../Implementation}/GameSessionManager.cpp | 10 +- .../Implementation}/GameSession_Events.cpp | 5 +- .../Implementation}/GameSession_General.cpp | 6 +- .../Implementation}/GameSession_Logic.cpp | 4 +- .../Implementation}/GameSession_Network.cpp | 4 +- .../LobbyGeneralProtocolParser.cpp | 52 ++++ .../Implementation/LobbyProtocolParser.cpp | 58 ++++ Code/Game/aDanBiasGameLauncher/Launcher.cpp | 1 + .../aDanBiasGameLauncher.vcxproj | 3 - Code/Misc/DynamicArray.h | 2 + Code/Misc/Thread/OysterThread.h | 3 +- Code/Misc/Thread/OysterThread_Impl.cpp | 7 +- Code/Misc/ThreadSafeQueue.h | 8 + Code/Network/NetworkAPI/CustomNetProtocol.h | 9 +- Code/Network/NetworkAPI/NetworkAPI.vcxproj | 13 +- .../NetworkAPI/NetworkAPI_Preprocessor.h | 10 + .../NetworkAPI/NetworkCallbackHelper.h | 80 ----- Code/Network/NetworkAPI/NetworkClient.cpp | 294 ++++++++---------- Code/Network/NetworkAPI/NetworkClient.h | 102 ++++-- Code/Network/NetworkAPI/NetworkServer.cpp | 276 ++++++++-------- Code/Network/NetworkAPI/NetworkServer.h | 72 ++++- .../NetworkAPI/NetworkServerEventStruct.h | 17 + Code/Network/NetworkAPI/NetworkSession.cpp | 184 +++++++++++ Code/Network/NetworkAPI/NetworkSession.h | 83 +++++ Code/Network/NetworkAPI/Translator.cpp | 1 + Code/Network/NetworkAPI/Translator.h | 11 +- .../NetworkDependencies/Connection.cpp | 15 +- Code/Network/NetworkDependencies/Connection.h | 3 +- Code/Network/NetworkDependencies/Listener.cpp | 2 +- 67 files changed, 1412 insertions(+), 1847 deletions(-) delete mode 100644 Code/Game/DanBiasServer/AdminInterface/AdminInterface.cpp delete mode 100644 Code/Game/DanBiasServer/AdminInterface/AdminInterface.h delete mode 100644 Code/Game/DanBiasServer/DanBiasServerAPI.cpp delete mode 100644 Code/Game/DanBiasServer/GameServer.cpp delete mode 100644 Code/Game/DanBiasServer/GameServer.h delete mode 100644 Code/Game/DanBiasServer/GameSession/GameClient.cpp delete mode 100644 Code/Game/DanBiasServer/GameSession/GameClient.h delete mode 100644 Code/Game/DanBiasServer/Helpers/MapManager.h delete mode 100644 Code/Game/DanBiasServer/Helpers/ServerDataReader.h delete mode 100644 Code/Game/DanBiasServer/LobbySessions/GameLobby.h delete mode 100644 Code/Game/DanBiasServer/LobbySessions/INetworkSession.cpp delete mode 100644 Code/Game/DanBiasServer/LobbySessions/INetworkSession.h delete mode 100644 Code/Game/DanBiasServer/LobbySessions/LobbyClient.cpp delete mode 100644 Code/Game/DanBiasServer/LobbySessions/LobbyClient.h delete mode 100644 Code/Game/DanBiasServer/LobbySessions/LobbyGeneralProtocolParser.cpp delete mode 100644 Code/Game/DanBiasServer/LobbySessions/LobbyProtocolParser.cpp delete mode 100644 Code/Game/DanBiasServer/LobbySessions/MainLobby.cpp delete mode 100644 Code/Game/DanBiasServer/LobbySessions/MainLobby.h delete mode 100644 Code/Game/DanBiasServer/LobbySessions/NetworkSession.cpp delete mode 100644 Code/Game/DanBiasServer/LobbySessions/NetworkSession.h create mode 100644 Code/Game/GameServer/GameClient.h create mode 100644 Code/Game/GameServer/GameLobby.h create mode 100644 Code/Game/GameServer/GameServer.h rename Code/Game/{DanBiasServer/DanBiasServer.vcxproj => GameServer/GameServer.vcxproj} (78%) rename Code/Game/{DanBiasServer/DanBiasServer.vcxproj.user => GameServer/GameServer.vcxproj.user} (100%) rename Code/Game/{DanBiasServer/DanBiasServerAPI.h => GameServer/GameServerAPI.h} (73%) rename Code/Game/{DanBiasServer/GameSession => GameServer}/GameSession.h (85%) rename Code/Game/{DanBiasServer/GameSession => GameServer}/GameSessionManager.h (90%) rename Code/Game/{DanBiasServer => GameServer/Implementation}/DLLMain.cpp (100%) create mode 100644 Code/Game/GameServer/Implementation/GameClient.cpp create mode 100644 Code/Game/GameServer/Implementation/GameLobby.cpp create mode 100644 Code/Game/GameServer/Implementation/GameServer.cpp rename Code/Game/{DanBiasServer/GameSession => GameServer/Implementation}/GameSessionManager.cpp (93%) rename Code/Game/{DanBiasServer/GameSession => GameServer/Implementation}/GameSession_Events.cpp (96%) rename Code/Game/{DanBiasServer/GameSession => GameServer/Implementation}/GameSession_General.cpp (96%) rename Code/Game/{DanBiasServer/GameSession => GameServer/Implementation}/GameSession_Logic.cpp (96%) rename Code/Game/{DanBiasServer/GameSession => GameServer/Implementation}/GameSession_Network.cpp (92%) create mode 100644 Code/Game/GameServer/Implementation/LobbyGeneralProtocolParser.cpp create mode 100644 Code/Game/GameServer/Implementation/LobbyProtocolParser.cpp create mode 100644 Code/Network/NetworkAPI/NetworkAPI_Preprocessor.h delete mode 100644 Code/Network/NetworkAPI/NetworkCallbackHelper.h create mode 100644 Code/Network/NetworkAPI/NetworkServerEventStruct.h create mode 100644 Code/Network/NetworkAPI/NetworkSession.cpp create mode 100644 Code/Network/NetworkAPI/NetworkSession.h diff --git a/Code/DanBias.sln b/Code/DanBias.sln index 68c4cdfa..540348f9 100644 --- a/Code/DanBias.sln +++ b/Code/DanBias.sln @@ -31,8 +31,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Game", "Game", "{20720CA7-7 EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "GameLogic", "Game\GameLogic\GameLogic.vcxproj", "{B1195BB9-B3A5-47F0-906C-8DEA384D1520}" EndProject -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "DanBiasServer", "Game\DanBiasServer\DanBiasServer.vcxproj", "{52380DAA-0F4A-4D97-8E57-98DF39319CAF}" -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} @@ -46,6 +44,10 @@ 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 GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Mixed Platforms = Debug|Mixed Platforms @@ -212,18 +214,6 @@ Global {B1195BB9-B3A5-47F0-906C-8DEA384D1520}.Release|Win32.Build.0 = Release|Win32 {B1195BB9-B3A5-47F0-906C-8DEA384D1520}.Release|x64.ActiveCfg = Release|x64 {B1195BB9-B3A5-47F0-906C-8DEA384D1520}.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 {8690FDDF-C5B7-4C42-A337-BD5243F29B85}.Debug|Mixed Platforms.ActiveCfg = Debug|Win32 {8690FDDF-C5B7-4C42-A337-BD5243F29B85}.Debug|Mixed Platforms.Build.0 = Debug|Win32 {8690FDDF-C5B7-4C42-A337-BD5243F29B85}.Debug|Win32.ActiveCfg = Debug|Win32 @@ -284,6 +274,30 @@ 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 + {143BD516-20A1-4890-A3E4-F8BFD02220E7}.Debug|Win32.Build.0 = Debug|Win32 + {143BD516-20A1-4890-A3E4-F8BFD02220E7}.Debug|x64.ActiveCfg = Debug|x64 + {143BD516-20A1-4890-A3E4-F8BFD02220E7}.Debug|x64.Build.0 = Debug|x64 + {143BD516-20A1-4890-A3E4-F8BFD02220E7}.Release|Mixed Platforms.ActiveCfg = Release|Win32 + {143BD516-20A1-4890-A3E4-F8BFD02220E7}.Release|Mixed Platforms.Build.0 = Release|Win32 + {143BD516-20A1-4890-A3E4-F8BFD02220E7}.Release|Win32.ActiveCfg = Release|Win32 + {143BD516-20A1-4890-A3E4-F8BFD02220E7}.Release|Win32.Build.0 = Release|Win32 + {143BD516-20A1-4890-A3E4-F8BFD02220E7}.Release|x64.ActiveCfg = Release|x64 + {143BD516-20A1-4890-A3E4-F8BFD02220E7}.Release|x64.Build.0 = Release|x64 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -295,10 +309,11 @@ Global {460D625F-2AC9-4559-B809-0BA89CEAEDF4} = {C27B926E-B3EF-4990-8822-47580E43A0BE} {2A1BC987-AF42-4500-802D-89CD32FC1309} = {20720CA7-795C-45AD-A302-9383A6DD503A} {B1195BB9-B3A5-47F0-906C-8DEA384D1520} = {20720CA7-795C-45AD-A302-9383A6DD503A} - {52380DAA-0F4A-4D97-8E57-98DF39319CAF} = {20720CA7-795C-45AD-A302-9383A6DD503A} {8690FDDF-C5B7-4C42-A337-BD5243F29B85} = {20720CA7-795C-45AD-A302-9383A6DD503A} {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 9b416e02..039b4dda 100644 --- a/Code/Dokumentation/GameServer.uxf +++ b/Code/Dokumentation/GameServer.uxf @@ -4,8 +4,8 @@ UMLClass - 530 - 320 + 380 + 360 100 30 @@ -16,20 +16,8 @@ UMLClass - 530 - 250 - 100 - 30 - - MainLobby - - - - - UMLClass - - 510 - 500 + 360 + 540 160 80 @@ -43,8 +31,8 @@ UMLClass - 710 - 320 + 560 + 360 100 30 @@ -54,19 +42,8 @@ com.umlet.element.Relation - 550 - 250 - 50 - 90 - - lt=->>>> - 30;70;30;30 - - - com.umlet.element.Relation - - 550 - 420 + 400 + 460 50 100 @@ -76,8 +53,8 @@ com.umlet.element.Relation - 600 - 300 + 450 + 340 130 50 @@ -87,19 +64,19 @@ com.umlet.element.Relation - 600 - 230 - 180 + 460 + 270 + 170 110 lt=>>>- - 160;90;160;30;30;30 + 150;90;150;30;30;30 com.umlet.element.Relation - 640 - 410 + 490 + 450 140 140 @@ -109,8 +86,8 @@ com.umlet.element.Relation - 610 - 140 + 460 + 180 400 150 @@ -120,10 +97,10 @@ UMLClass - 520 - 160 + 370 + 200 120 - 30 + 120 GameServer @@ -131,19 +108,19 @@ com.umlet.element.Relation - 550 - 160 + 400 + 290 50 - 110 + 90 lt=>>>>- - 30;90;30;30 + 30;70;30;30 com.umlet.element.Relation - 750 - 260 + 600 + 300 260 80 @@ -153,8 +130,8 @@ com.umlet.element.Package - 990 - 250 + 840 + 290 120 50 @@ -165,8 +142,8 @@ bg=#a21aff UMLClass - 510 - 20 + 360 + 120 130 40 @@ -177,20 +154,20 @@ DanBiasServerAPI com.umlet.element.Relation - 550 - 30 + 400 + 130 50 - 150 + 90 lt=>>>>- - 30;130;30;30 + 30;70;30;30 UMLClass - 300 - 320 + 150 + 360 120 30 @@ -200,19 +177,19 @@ DanBiasServerAPI com.umlet.element.Relation - 330 - 230 - 220 + 180 + 270 + 210 110 lt=->>>>> - 200;30;30;30;30;90 + 190;30;30;30;30;90 com.umlet.element.Relation - 390 - 300 + 240 + 340 160 50 @@ -222,8 +199,8 @@ DanBiasServerAPI com.umlet.element.Relation - 330 - 320 + 180 + 360 200 210 @@ -233,8 +210,8 @@ DanBiasServerAPI com.umlet.element.Package - 990 - 370 + 840 + 410 120 50 @@ -245,8 +222,8 @@ bg=blue com.umlet.element.Relation - 750 - 320 + 600 + 360 260 90 @@ -256,8 +233,8 @@ bg=blue com.umlet.element.Package - 990 - 310 + 840 + 350 120 40 @@ -270,8 +247,8 @@ bg=blue com.umlet.element.Relation - 780 - 300 + 630 + 340 230 50 @@ -281,8 +258,8 @@ bg=blue com.umlet.element.Relation - 640 - 380 + 490 + 420 370 190 @@ -292,8 +269,8 @@ bg=blue UMLClass - 320 - 120 + 170 + 160 130 90 @@ -308,8 +285,8 @@ elementstyle=wordwrap com.umlet.element.Relation - 420 - 140 + 270 + 180 120 50 @@ -320,8 +297,8 @@ elementstyle=wordwrap UMLClass - 510 - 400 + 360 + 440 160 50 @@ -333,8 +310,8 @@ elementstyle=wordwrap com.umlet.element.Relation - 550 - 320 + 400 + 360 50 100 @@ -344,8 +321,8 @@ elementstyle=wordwrap com.umlet.element.Package - 990 - 430 + 840 + 470 120 40 @@ -356,8 +333,8 @@ bg=#aaaaa com.umlet.element.Relation - 310 - 320 + 160 + 360 770 300 @@ -367,8 +344,8 @@ bg=#aaaaa com.umlet.element.Relation - 1080 - 260 + 930 + 300 70 210 @@ -378,8 +355,8 @@ bg=#aaaaa UMLClass - 160 - 310 + 10 + 350 120 40 @@ -390,8 +367,8 @@ INetworkSession com.umlet.element.Relation - 250 - 300 + 100 + 340 70 50 @@ -401,8 +378,8 @@ INetworkSession UMLClass - 710 - 410 + 560 + 450 100 30 @@ -412,8 +389,8 @@ INetworkSession com.umlet.element.Relation - 730 - 320 + 580 + 360 50 110 @@ -423,8 +400,8 @@ INetworkSession UMLClass - 150 - 420 + 0 + 460 160 50 diff --git a/Code/Game/DanBiasGame/GameClientState/GameState.cpp b/Code/Game/DanBiasGame/GameClientState/GameState.cpp index 9093654c..70385e07 100644 --- a/Code/Game/DanBiasGame/GameClientState/GameState.cpp +++ b/Code/Game/DanBiasGame/GameClientState/GameState.cpp @@ -290,7 +290,7 @@ void GameState::Protocol( ObjPos* pos ) for (unsigned int i = 0; i < privData->object.size(); i++) { - if(privData->object[i]->GetId() == pos->object_ID) + if(privData->object[i] && privData->object[i]->GetId() == pos->object_ID) { privData->object[i]->setPos(world); diff --git a/Code/Game/DanBiasServer/AdminInterface/AdminInterface.cpp b/Code/Game/DanBiasServer/AdminInterface/AdminInterface.cpp deleted file mode 100644 index 80e2f9fe..00000000 --- a/Code/Game/DanBiasServer/AdminInterface/AdminInterface.cpp +++ /dev/null @@ -1,68 +0,0 @@ -#define NOMINMAX -#include "AdminInterface.h" -#include -#include -#include "..\GameServer.h" -#include "..\GameSession\GameSessionManager.h" -#include "..\GameSession\GameSession.h" -#include "..\Helpers\ServerDataReader.h" -#include "..\LobbySessions\LobbyClient.h" -#include "..\LobbySessions\NetworkSession.h" -#include "..\LobbySessions\GameLobby.h" -#include "..\LobbySessions\MainLobby.h" - -using namespace Oyster::Network; -using namespace Oyster::Thread; -using namespace DanBias; - -struct AdminInstanceData :public IThreadObject -{ - bool isCreated; - Oyster::Network::NetworkServer adminServer; - NetworkClient* admin; - NetworkSession* parentInstance; - OysterThread worker; - - bool DoWork() override - { - //((GameServer*)parentInstance)->serve - return true; - } - -}adminInstanceData; - -void AdminArrived(NetworkClient* adm) -{ - if(adminInstanceData.admin) - { - delete adm; - return; - } - - adminInstanceData.admin = adm; - adminInstanceData.worker.Create(&adminInstanceData, true, true); -} - -void AdminInterface::Toggle(bool toggle, NetworkSession* parent) -{ - if(toggle) - { - if(!parent) return; - - if(adminInstanceData.isCreated) return; - - NetworkServer::INIT_DESC desc; - desc.port = 15152; - desc.callbackType = NetworkClientCallbackType_Function; - desc.recvObj.clientConnectFnc = AdminArrived; - - if(!adminInstanceData.adminServer.Init(desc)) return; - adminInstanceData.parentInstance = parent; - - adminInstanceData.adminServer.Start(); - } - else - { - adminInstanceData.adminServer.Shutdown(); - } -} \ No newline at end of file diff --git a/Code/Game/DanBiasServer/AdminInterface/AdminInterface.h b/Code/Game/DanBiasServer/AdminInterface/AdminInterface.h deleted file mode 100644 index 4acc20a2..00000000 --- a/Code/Game/DanBiasServer/AdminInterface/AdminInterface.h +++ /dev/null @@ -1,20 +0,0 @@ -///////////////////////////////////////// -// Created by [Dennis Andersen] [2013] // -///////////////////////////////////////// -#ifndef DANBIASSERVER_ADMIN_INTERFACE_H -#define DANBIASSERVER_ADMIN_INTERFACE_H - -#include - -namespace DanBias -{ - class NetworkSession; - //Global admin - class AdminInterface - { - public: - static void Toggle(bool toggle, NetworkSession* parent); - - }; -} -#endif // !DANBIASSERVER_ADMIN_INTERFACE_H diff --git a/Code/Game/DanBiasServer/DanBiasServerAPI.cpp b/Code/Game/DanBiasServer/DanBiasServerAPI.cpp deleted file mode 100644 index dfaf0b75..00000000 --- a/Code/Game/DanBiasServer/DanBiasServerAPI.cpp +++ /dev/null @@ -1,28 +0,0 @@ -///////////////////////////////////////////////////////////////////// -// Created by [Dennis Andersen] [2013] -///////////////////////////////////////////////////////////////////// -#include "DanBiasServerAPI.h" -#include "GameServer.h" - -namespace DanBias -{ - -#pragma region Server Data - static GameServer server; -#pragma endregion - - - DanBiasServerReturn DanBiasServerAPI::Initiate() - { - return server.Create(); - } - DanBiasServerReturn DanBiasServerAPI::Run() - { - return server.Run(); - } - DanBiasServerReturn DanBiasServerAPI::Release() - { - return server.Release(); - } - -} //End namspace DanBias \ No newline at end of file diff --git a/Code/Game/DanBiasServer/GameServer.cpp b/Code/Game/DanBiasServer/GameServer.cpp deleted file mode 100644 index 1da2b5ba..00000000 --- a/Code/Game/DanBiasServer/GameServer.cpp +++ /dev/null @@ -1,161 +0,0 @@ -///////////////////////////////////////////////////////////////////// -// Created by [Dennis Andersen] [2013] -///////////////////////////////////////////////////////////////////// -#define NOMINMAX -#include -#include -#include - -#include "GameServer.h" -#include "Helpers\ServerDataReader.h" -#include "GameSession\GameSessionManager.h" -#include "LobbySessions\LobbyClient.h" -#include "GameSession\GameSession.h" -#include "AdminInterface\AdminInterface.h" - -#include -#include - -#include - -namespace DanBias -{ - using namespace Oyster::Network; - - GameServer* GameServer::instance = 0; - - void GameServer::NetworkCallback(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 LobbyClient(client); - - GameSessionDescription desc; - desc.mapName = L"test"; - desc.clients.Push(c); - desc.exitDestionation = this->mainLobby; - 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 LobbyClient(client); - GameSessionManager::JoinSession(sessionId, c); - } - - - //Utility::DynamicMemory::SmartPointer c = new LobbyClient(client); - //this->mainLobby->Attach(c, this->mainLobby->GetPostbox()); - } - - - GameServer::GameServer() - : initiated(0) - , running(0) - , released(0) - , maxClients(0) - , mainLobby(0) - , server(0) - { this->instance = this; } - GameServer::~GameServer() - { - - } - DanBiasServerReturn GameServer::Create() - { - this->server = new NetworkServer(); - this->mainLobby = new MainLobby(); - - InitData data; - if(!LoadIniFile(data)) return DanBiasServerReturn_Error; - - NetworkServer::INIT_DESC serverDesc; - this->maxClients = data.clients; - serverDesc.port = data.port; - serverDesc.recvObj = this; - serverDesc.callbackType = Oyster::Network::NetworkClientCallbackType_Object; - - if(!this->server->Init(serverDesc)) return DanBiasServerReturn_Error; - if(!WindowShell::CreateConsoleWindow()) return DanBiasServerReturn_Error; - - this->initiated = true; - return DanBiasServerReturn_Sucess; - } - DanBiasServerReturn GameServer::Run() - { - if(this->running) return DanBiasServerReturn_Error; - if(this->released) return DanBiasServerReturn_Error; - if(!this->initiated) return DanBiasServerReturn_Error; - - if(!this->server->Start()) return DanBiasServerReturn_Error; - //Oyster::Thread::OysterThread ioThread; - //ioThread.Create(this, true, - - while (true) - { - if(!WindowShell::Frame()) break; - - this->mainLobby->Frame(); - - if(GetAsyncKeyState(0x51)) //Q for exit - break; - } - - return DanBiasServerReturn_Sucess; - } - DanBiasServerReturn GameServer::Release() - { - GameSessionManager::CloseSession(); - this->mainLobby->Release(); - delete this->mainLobby; - this->server->Shutdown(); - delete this->server; - this->released = true; - return DanBiasServerReturn_Sucess; - } - - NetworkSession* GameServer::MainLobbyInstance() - { - return GameServer::instance->mainLobby; - } - - bool GameServer::LoadIniFile(InitData& ini) - { - std::ifstream in; - std::string f = GetInitPath(InitPath_ServerIni); - in.open(f, std::ios::in); - if(!in.is_open()) return false; - - std::string buffer; - while (!in.eof()) - { - in >> buffer; - - if(buffer == "port") - { - in >> ini.port; - } - else if(buffer == "clients") - { - in >> ini.clients; - } - - } - - in.close(); - return true; - } -}//End namespace DanBias diff --git a/Code/Game/DanBiasServer/GameServer.h b/Code/Game/DanBiasServer/GameServer.h deleted file mode 100644 index ccdb6e05..00000000 --- a/Code/Game/DanBiasServer/GameServer.h +++ /dev/null @@ -1,50 +0,0 @@ -///////////////////////////////////////////////////////////////////// -// Created by [Dennis Andersen] [2013] -///////////////////////////////////////////////////////////////////// -#ifndef DANBIASSERVER_GAME_SERVER_H -#define DANBIASSERVER_GAME_SERVER_H - -#include "DanBiasServerAPI.h" -#include "LobbySessions\MainLobby.h" -#include -#include - -namespace DanBias -{ - class GameServer :public Oyster::Network::ClientConnectedObject - { - public: - GameServer(); - virtual~GameServer(); - - DanBiasServerReturn Create(); - DanBiasServerReturn Run(); - DanBiasServerReturn Release(); - - static NetworkSession* MainLobbyInstance(); - - private: - //static void ClientConnectCallbackFunction(Oyster::Network::NetworkClient& connectedClient); - void NetworkCallback(Oyster::Network::NetworkClient* client) override; - - bool initiated; - bool running; - bool released; - int maxClients; - MainLobby *mainLobby; - Oyster::Network::NetworkServer *server; - static GameServer* instance; - - private: - struct InitData - { - int port; - int clients; - }; - bool LoadIniFile(InitData&); - - private: - friend class AdminInterface; - }; -}// End namspace DanBias -#endif // !DANBIASSERVER_DBSERVER_H diff --git a/Code/Game/DanBiasServer/GameSession/GameClient.cpp b/Code/Game/DanBiasServer/GameSession/GameClient.cpp deleted file mode 100644 index 23f0f3e2..00000000 --- a/Code/Game/DanBiasServer/GameSession/GameClient.cpp +++ /dev/null @@ -1,67 +0,0 @@ -///////////////////////////////////////////////////////////////////// -// Created by [Dennis Andersen] [2013] -///////////////////////////////////////////////////////////////////// - -#include "GameClient.h" -#include "..\LobbySessions\NetworkSession.h" -#include - -using namespace Utility::DynamicMemory; -using namespace DanBias; -using namespace GameLogic; - -static int gameClientIDCount = 1; - -GameClient::GameClient(SmartPointer client, IPlayerData* player, Oyster::Callback::OysterCallback value) -{ - this->callbackValue = value; - this->client = client; - this->id = gameClientIDCount++; - this->player = player; - Oyster::Callback::OysterCallback c; - c.callbackType = Oyster::Callback::CallbackType_Object; - c.value = this; - this->client->SetCallback(c); - -} -GameClient::~GameClient() -{ - if(this->client) this->client->Disconnect(); - this->player = 0; - this->id = -1; -} - -void GameClient::SetCallback(Oyster::Callback::OysterCallback value) -{ - this->callbackValue = value; -} - -GameLogic::IPlayerData* GameClient::GetPlayer() -{ - return this->player; -} -GameLogic::IPlayerData* GameClient::ReleasePlayer() -{ - GameLogic::IPlayerData *temp = this->player; - this->player = 0; - return temp; -} -LobbyClient* GameClient::GetClient() const -{ - return this->client; -} -Utility::DynamicMemory::SmartPointer GameClient::ReleaseClient() -{ - SmartPointer temp = this->client; - this->client = 0; - return temp; -} -int GameClient::GetID() const -{ - return this->id; -} -void GameClient::ObjectCallback(NetworkSession::NetEvent e) -{ - e.gameClient = this; - this->callbackValue(e); -} diff --git a/Code/Game/DanBiasServer/GameSession/GameClient.h b/Code/Game/DanBiasServer/GameSession/GameClient.h deleted file mode 100644 index 0a74479a..00000000 --- a/Code/Game/DanBiasServer/GameSession/GameClient.h +++ /dev/null @@ -1,43 +0,0 @@ -///////////////////////////////////////////////////////////////////// -// Created by [Dennis Andersen] [2013] -///////////////////////////////////////////////////////////////////// -#ifndef DANBIASSERVER_CLIENT_OBJECT_H -#define DANBIASSERVER_CLIENT_OBJECT_H - -#include "..\LobbySessions\LobbyClient.h" -#include -#include - -namespace DanBias -{ - class GameClient: Oyster::Callback::CallbackObject - { - public: - GameClient(Utility::DynamicMemory::SmartPointer client, GameLogic::IPlayerData* player, Oyster::Callback::OysterCallback value); - virtual~GameClient(); - - void SetCallback(Oyster::Callback::OysterCallback value); - - /* */ - GameLogic::IPlayerData* GetPlayer(); - - GameLogic::IPlayerData* ReleasePlayer(); - - LobbyClient* GetClient() const; - Utility::DynamicMemory::SmartPointer ReleaseClient(); - int GetID() const; - - private: - //Utility::DynamicMemory::SmartPointer player; - GameLogic::IPlayerData* player; - Utility::DynamicMemory::SmartPointer client; - Oyster::Callback::OysterCallback callbackValue; - int id; - void ObjectCallback(NetworkSession::NetEvent) override; - - private: - friend class AdminInterface; - }; - -}//End namespace DanBias -#endif // !DANBIASSERVER_CLIENT_OBJECT_H diff --git a/Code/Game/DanBiasServer/Helpers/MapManager.h b/Code/Game/DanBiasServer/Helpers/MapManager.h deleted file mode 100644 index 49342a56..00000000 --- a/Code/Game/DanBiasServer/Helpers/MapManager.h +++ /dev/null @@ -1,30 +0,0 @@ -///////////////////////////////////////// -// Created by [Dennis Andersen] [2013] // -///////////////////////////////////////// -#ifndef DANBIASSERVER_LEVELMANAGER_H -#define DANBIASSERVER_LEVELMANAGER_H - -#include "ServerDataReader.h" -#include "..\LobbySessions\LobbyClient.h" -#include "..\LobbySessions\NetworkSession.h" -#include "..\LobbySessions\GameLobby.h" -#include - -namespace DanBias -{ - class MapManager - { - public: - struct MapInitDesc - { - const wchar_t* map; - Utility::DynamicMemory::DynamicArray> clients; - }; - - public: - static bool InitiateMapPack(const MapInitDesc& desc); - - }; -} - -#endif // !DANBIASSERVER_LEVELMANAGER_H diff --git a/Code/Game/DanBiasServer/Helpers/ServerDataReader.h b/Code/Game/DanBiasServer/Helpers/ServerDataReader.h deleted file mode 100644 index 73048bd7..00000000 --- a/Code/Game/DanBiasServer/Helpers/ServerDataReader.h +++ /dev/null @@ -1,44 +0,0 @@ -///////////////////////////////////////////////////////////////////// -// Created by [Dennis Andersen] [2013] -///////////////////////////////////////////////////////////////////// -#ifndef DANBIASSERVER_SERVER_INIT_READER_H -#define DANBIASSERVER_SERVER_INIT_READER_H - -#include -#include - -namespace DanBias -{ - enum InitPath - { - InitPath_ServerIni, - }; - static std::string GetInitPath(InitPath file) - { - std::string type = ""; - std::string path = ""; - std::string flag = ""; - - switch (file) - { - case DanBias::InitPath_ServerIni: - flag = "ServerInit"; - break; - } - - std::fstream in; - in.open("..\\Settings\\serversearchpath.ini", std::ios::in); - if(!in.is_open()) return ""; - - while (!in.eof() && type != flag) - { - in >> type; - in >> path; - } - - in.close(); - return path; - } -} - -#endif // !DANBIASSERVER_SERVER_INIT_READER_H diff --git a/Code/Game/DanBiasServer/LobbySessions/GameLobby.h b/Code/Game/DanBiasServer/LobbySessions/GameLobby.h deleted file mode 100644 index b6c6d4f9..00000000 --- a/Code/Game/DanBiasServer/LobbySessions/GameLobby.h +++ /dev/null @@ -1,24 +0,0 @@ -///////////////////////////////////////////////////////////////////// -// Created by [Dennis Andersen] [2013] -///////////////////////////////////////////////////////////////////// -#ifndef DANBIASSERVER_GAMELOBBY_H -#define DANBIASSERVER_GAMELOBBY_H - -#include "NetworkSession.h" - -namespace DanBias -{ - class GameLobby :public NetworkSession - { - public: - GameLobby(Utility::DynamicMemory::SmartPointer owner); - virtual~GameLobby(); - - void Release(); - - private: - friend class AdminInterface; - }; -}//End namespace DanBias - -#endif // !DANBIASSERVER_GAME_LOBBY_H \ No newline at end of file diff --git a/Code/Game/DanBiasServer/LobbySessions/INetworkSession.cpp b/Code/Game/DanBiasServer/LobbySessions/INetworkSession.cpp deleted file mode 100644 index 899e1d53..00000000 --- a/Code/Game/DanBiasServer/LobbySessions/INetworkSession.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#include "INetworkSession.h" -#include - -INetworkSession::INetworkSession() - :id(GID()) -{} -INetworkSession::INetworkSession(const INetworkSession& orig) -{ - id = orig.id; -} -const INetworkSession& INetworkSession::operator=(const INetworkSession& orig) -{ - id = orig.id; - return *this; -} -INetworkSession::~INetworkSession() -{} - diff --git a/Code/Game/DanBiasServer/LobbySessions/INetworkSession.h b/Code/Game/DanBiasServer/LobbySessions/INetworkSession.h deleted file mode 100644 index dfad66ae..00000000 --- a/Code/Game/DanBiasServer/LobbySessions/INetworkSession.h +++ /dev/null @@ -1,22 +0,0 @@ -///////////////////////////////////////// -// Created by [Dennis Andersen] [2013] // -///////////////////////////////////////// -#ifndef DANBIASSERVER_INETWORKSESSION_H -#define DANBIASSERVER_INETWORKSESSION_H - -class INetworkSession -{ -public: - INetworkSession(); - INetworkSession(const INetworkSession& orig); - const INetworkSession& operator=(const INetworkSession& orig); - virtual~INetworkSession(); - - inline int GetID() const { return this->id; } - -private: - int id; - -}; - -#endif // !DANBIASSERVER_INETWORKSESSION_H diff --git a/Code/Game/DanBiasServer/LobbySessions/LobbyClient.cpp b/Code/Game/DanBiasServer/LobbySessions/LobbyClient.cpp deleted file mode 100644 index 95f18e68..00000000 --- a/Code/Game/DanBiasServer/LobbySessions/LobbyClient.cpp +++ /dev/null @@ -1,51 +0,0 @@ -#include "LobbyClient.h" -#include - -using namespace Utility::DynamicMemory; -using namespace Oyster::Network; -using namespace Oyster::Callback; -using namespace GameLogic; - -namespace DanBias -{ - LobbyClient::LobbyClient(SmartPointer client) - { - this->client = client; - this->client->SetRecieverObject(this, NetworkProtocolCallbackType_Object); - } - LobbyClient::~LobbyClient() - { - this->callbackValue.callbackType = CallbackType_Unknown; - } - - void LobbyClient::Disconnect() - { - this->client->Disconnect(); - } - void LobbyClient::SetCallback(OysterCallback value) - { - this->callbackValue = value; - } - - /** This method is NOT threadsafe. */ - void LobbyClient::NetworkCallback(CustomNetProtocol& protocol) - { - if(this->callbackValue.callbackType == CallbackType_Unknown) return; - - NetworkSession::NetEvent e; - e.sender = this; - e.protocol = protocol; - - this->callbackValue(e); - } - void LobbyClient::Disconnected() - { - if(this->callbackValue.callbackType == CallbackType_Unknown) return; - - NetworkSession::NetEvent e; - e.sender = this; - e.protocol = *GameLogic::Protocol_General_Status(Protocol_General_Status::States_disconected).GetProtocol(); - - this->callbackValue(e); - } -}//End namsapce DanBias \ No newline at end of file diff --git a/Code/Game/DanBiasServer/LobbySessions/LobbyClient.h b/Code/Game/DanBiasServer/LobbySessions/LobbyClient.h deleted file mode 100644 index 33b99d78..00000000 --- a/Code/Game/DanBiasServer/LobbySessions/LobbyClient.h +++ /dev/null @@ -1,38 +0,0 @@ -///////////////////////////////////////// -// Created by [Dennis Andersen] [2013] // -///////////////////////////////////////// -#ifndef DANBIASSERVER_LOBBYCLIENT_H -#define DANBIASSERVER_LOBBYCLIENT_H - -#include "NetworkSession.h" -#include -#include - -namespace DanBias -{ - class LobbyClient :public Oyster::Network::NetClientEvent - { - public: - LobbyClient(Utility::DynamicMemory::SmartPointer client); - virtual~LobbyClient(); - - void Disconnect(); - void SetCallback(Oyster::Callback::OysterCallback value); - - inline void Send(Oyster::Network::CustomProtocolObject& protocol) { this->client->Send(protocol); } - inline void Send(Oyster::Network::CustomNetProtocol* protocol) { this->client->Send(protocol); } - inline int GetID() const { return this->client->GetID(); } - - private: - Utility::DynamicMemory::SmartPointer client; - Oyster::Callback::OysterCallback callbackValue; - - private: - void NetworkCallback(Oyster::Network::CustomNetProtocol& protocol) override; - void Disconnected() override; - - private: - friend class AdminInterface; - }; -}//End namspace DanBias -#endif // !DANBIASSERVER_LOBBYCLIENT_H diff --git a/Code/Game/DanBiasServer/LobbySessions/LobbyGeneralProtocolParser.cpp b/Code/Game/DanBiasServer/LobbySessions/LobbyGeneralProtocolParser.cpp deleted file mode 100644 index cb19336f..00000000 --- a/Code/Game/DanBiasServer/LobbySessions/LobbyGeneralProtocolParser.cpp +++ /dev/null @@ -1,64 +0,0 @@ -#include "MainLobby.h" -#include "LobbyClient.h" - -using namespace DanBias; -using namespace GameLogic; - -void MainLobby::ParseGeneralProtocol(Oyster::Network::CustomNetProtocol& p, DanBias::LobbyClient* c) -{ - switch (p[0].value.netShort) - { - case protocol_General_Status: - { - GeneralStatus(GameLogic::Protocol_General_Status(p), c); - } break; - case protocol_General_Text: - { - GameLogic::Protocol_General_Text(p); - } break; - case protocol_Lobby_Login: - { - - } break; - case protocol_Lobby_Join: - { - JoinLobby(GameLogic::Protocol_LobbyJoin(p), c); - } break; - } -} - -void MainLobby::GeneralStatus(GameLogic::Protocol_General_Status& p, DanBias::LobbyClient* 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 MainLobby::JoinLobby(GameLogic::Protocol_LobbyJoin& p, DanBias::LobbyClient* 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/DanBiasServer/LobbySessions/LobbyProtocolParser.cpp b/Code/Game/DanBiasServer/LobbySessions/LobbyProtocolParser.cpp deleted file mode 100644 index 27aade71..00000000 --- a/Code/Game/DanBiasServer/LobbySessions/LobbyProtocolParser.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "MainLobby.h" - -using namespace DanBias; - -void MainLobby::ParseLobbyProtocol(Oyster::Network::CustomNetProtocol& p, DanBias::LobbyClient* c) -{ - switch (p[0].value.netShort) - { - case protocol_Lobby_Create: - CreateGame(GameLogic::Protocol_LobbyCreateGame(p), c); - break; - - case protocol_Lobby_Start: - - break; - - case protocol_Lobby_Refresh: - GameLogic::Protocol_LobbyRefresh(); - break; - } -} - -void MainLobby::CreateGame(GameLogic::Protocol_LobbyCreateGame& p, DanBias::LobbyClient* c) -{ - for (unsigned int i = 0; i < this->gameLobby.Size(); i++) - { - if(!gameLobby[i]) - { - gameLobby[i] = new GameLobby(NetworkSession::Detach(c)); - return; - } - } - - this->gameLobby.Push(new GameLobby(NetworkSession::Detach(c))); -} - - diff --git a/Code/Game/DanBiasServer/LobbySessions/MainLobby.cpp b/Code/Game/DanBiasServer/LobbySessions/MainLobby.cpp deleted file mode 100644 index ef5765c7..00000000 --- a/Code/Game/DanBiasServer/LobbySessions/MainLobby.cpp +++ /dev/null @@ -1,62 +0,0 @@ -///////////////////////////////////////////////////////////////////// -// Created by [Dennis Andersen] [2013] -///////////////////////////////////////////////////////////////////// -#include "MainLobby.h" -#include "LobbyClient.h" -#include -#include - -using namespace Utility::DynamicMemory; -using namespace Oyster::Network; -using namespace Oyster; - -namespace DanBias -{ - MainLobby::MainLobby() - :gameLobby(5) - { - this->box = new PostBox(); - } - MainLobby::~MainLobby() - { - - } - void MainLobby::Release() - { - delete this->box; - this->box = 0; - this->CloseSession(true); - } - - void MainLobby::Frame() - { - ParseEvents(); - } - IPostBox* MainLobby::GetPostbox() - { - return this->box; - } - void MainLobby::SetRefreshFrequency(float delta) - { - this->refreshFrequency = delta; - } - - float MainLobby::GetRefreshFrequency() const - { - return this->refreshFrequency; - } -//////// Private - void MainLobby::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); - } - } - -}//End namespace DanBias \ No newline at end of file diff --git a/Code/Game/DanBiasServer/LobbySessions/MainLobby.h b/Code/Game/DanBiasServer/LobbySessions/MainLobby.h deleted file mode 100644 index 13e1bab5..00000000 --- a/Code/Game/DanBiasServer/LobbySessions/MainLobby.h +++ /dev/null @@ -1,49 +0,0 @@ -///////////////////////////////////////////////////////////////////// -// Created by [Dennis Andersen] [2013] -///////////////////////////////////////////////////////////////////// -#ifndef DANBIASSERVER_MAINLOBBY_H -#define DANBIASSERVER_MAINLOBBY_H - -#include "NetworkSession.h" -#include "GameLobby.h" -#include -#include -#include - -namespace DanBias -{ - class MainLobby :public NetworkSession - { - public: - MainLobby(); - virtual~MainLobby(); - void Release(); - - void Frame(); - - void SetPostbox(Oyster::IPostBox* box); - Oyster::IPostBox* GetPostbox(); - - void SetRefreshFrequency(float delta); - float GetRefreshFrequency() const; - - private: - void ParseEvents(); - void ParseGeneralProtocol(Oyster::Network::CustomNetProtocol& p, DanBias::LobbyClient* c); - void ParseLobbyProtocol(Oyster::Network::CustomNetProtocol& p, DanBias::LobbyClient* c); - - void GeneralStatus(GameLogic::Protocol_General_Status& p, DanBias::LobbyClient* c); - void CreateGame(GameLogic::Protocol_LobbyCreateGame& p, DanBias::LobbyClient* c); - void JoinLobby(GameLogic::Protocol_LobbyJoin& p, DanBias::LobbyClient* c); - - private: - Oyster::IPostBox *box; - Utility::DynamicMemory::DynamicArray> gameLobby; - Utility::WinTimer timer; - float refreshFrequency; - - private: - friend class AdminInterface; - }; -}//End namespace DanBias -#endif // !DANBIASGAME_GAMELOBBY_H diff --git a/Code/Game/DanBiasServer/LobbySessions/NetworkSession.cpp b/Code/Game/DanBiasServer/LobbySessions/NetworkSession.cpp deleted file mode 100644 index 0eecf59a..00000000 --- a/Code/Game/DanBiasServer/LobbySessions/NetworkSession.cpp +++ /dev/null @@ -1,219 +0,0 @@ -///////////////////////////////////////////////////////////////////// -// Created by [Dennis Andersen] [2013] -///////////////////////////////////////////////////////////////////// -#include "LobbyClient.h" -#include "NetworkSession.h" -#include - - - -namespace DanBias -{ - NetworkSession::NetworkSession() - : owner(0) - , clientCount(0) - {} - NetworkSession::NetworkSession(const NetworkSession& orig) - { - this->clients = orig.clients; - this->owner = orig.owner; - this->clientCount = orig.clientCount; - } - const NetworkSession& NetworkSession::operator=(const NetworkSession& orig) - { - this->clients = orig.clients; - this->owner = orig.owner; - this->clientCount = orig.clientCount; - return *this; - } - NetworkSession::~NetworkSession() - { - this->clients.Clear(); - this->clientCount = 0; - } - - bool NetworkSession::Attach(Utility::DynamicMemory::SmartPointer client) - { - clientListLock.lock(); - - int k = -1; - for (unsigned int i = 0; (k == -1) && i < this->clients.Size(); i++) - { - if(!this->clients[i]) - k = i; - } - - if(k == -1) - { - this->clients.Push(client); - } - else - { - this->clients[k] = client; - } - this->clientCount++; - clientListLock.unlock(); - - return true; - } - - Utility::DynamicMemory::SmartPointer NetworkSession::Detach(Oyster::Network::NetworkClient* client) - { - Utility::DynamicMemory::SmartPointer val; - - clientListLock.lock(); - - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - if(this->clients[0]->GetID() == client->GetID()) - { - val = this->clients[i]; - this->clients[i] = 0; - this->clientCount--; - } - } - - clientListLock.unlock(); - - return val; - } - Utility::DynamicMemory::SmartPointer NetworkSession::Detach(const LobbyClient* client) - { - Utility::DynamicMemory::SmartPointer val; - - clientListLock.lock(); - - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - if(this->clients[0]->GetID() == client->GetID()) - { - val = this->clients[i]; - this->clients[i] = 0; - this->clientCount--; - } - } - - clientListLock.unlock(); - - return val; - } - Utility::DynamicMemory::SmartPointer NetworkSession::Detach(const LobbyClient& client) - { - Utility::DynamicMemory::SmartPointer val; - - clientListLock.lock(); - - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - if(this->clients[0]->GetID() == client.GetID()) - { - val = this->clients[i]; - this->clients[i] = 0; - this->clientCount--; - } - } - - clientListLock.unlock(); - - return val; - } - Utility::DynamicMemory::SmartPointer NetworkSession::Detach(short ID) - { - Utility::DynamicMemory::SmartPointer val; - - clientListLock.lock(); - - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - if(this->clients[0]->GetID() == ID) - { - val = this->clients[i]; - this->clients[i] = 0; - this->clientCount--; - } - } - - clientListLock.unlock(); - - return val; - } - - bool NetworkSession::Send(Oyster::Network::CustomNetProtocol& protocol) - { - bool returnValue = false; - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - if(this->clients[i]) - { - this->clients[i]->Send(&protocol); - returnValue = true; - } - } - - return returnValue; - } - bool NetworkSession::Send(Oyster::Network::CustomNetProtocol& protocol, int ID) - { - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - if(this->clients[i] && this->clients[i]->GetID() == ID) - { - this->clients[i]->Send(&protocol); - return true; - } - } - return false; - } - - void NetworkSession::SetCallback(Oyster::Callback::OysterCallback value) - { - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - this->clients[i]->SetCallback(value); - } - } - - void NetworkSession::CloseSession(bool dissconnectClients) - { - clientListLock.lock(); - - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - if(dissconnectClients) this->clients[i]->Disconnect(); - else if(this->owner) this->owner->Attach(this->clients[i]); - } - - this->clients.Clear(); - - clientListLock.unlock(); - } - Utility::DynamicMemory::SmartPointer NetworkSession::FindClient(int ID) - { - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - if(this->clients[i]->GetID() == ID) - return this->clients[i]; - } - return Utility::DynamicMemory::SmartPointer(); - } - Utility::DynamicMemory::SmartPointer NetworkSession::FindClient(LobbyClient& obj) - { - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - if(this->clients[i]->GetID() == obj.GetID()) - return this->clients[i]; - } - return Utility::DynamicMemory::SmartPointer(); - } - Utility::DynamicMemory::SmartPointer NetworkSession::FindClient(LobbyClient* obj) - { - for (unsigned int i = 0; i < this->clients.Size(); i++) - { - if(this->clients[i]->GetID() == obj->GetID()) - return this->clients[i]; - } - return Utility::DynamicMemory::SmartPointer(); - } - - -}//End namespace DanBias \ No newline at end of file diff --git a/Code/Game/DanBiasServer/LobbySessions/NetworkSession.h b/Code/Game/DanBiasServer/LobbySessions/NetworkSession.h deleted file mode 100644 index 41fb0a59..00000000 --- a/Code/Game/DanBiasServer/LobbySessions/NetworkSession.h +++ /dev/null @@ -1,91 +0,0 @@ -///////////////////////////////////////////////////////////////////// -// Created by [Dennis Andersen] [2013] -///////////////////////////////////////////////////////////////////// -#ifndef DANBIASSERVER_NETWORK_SESSION_H -#define DANBIASSERVER_NETWORK_SESSION_H - -//warning C4150: deletion of pointer to incomplete type, no destructor called -#pragma warning(disable : 4150) - -#define NOMINMAX - -#include "INetworkSession.h" -#include -#include -#include -#include -#include -#include -#include -#include - -namespace DanBias -{ - class LobbyClient; - class GameClient; - - class NetworkSession :public INetworkSession - { - public: - struct NetEvent - { - LobbyClient* sender; - GameClient* gameClient; - Oyster::Network::CustomNetProtocol protocol; - NetEvent():sender(0), gameClient(0){} - }; - - public: - NetworkSession(); - NetworkSession(const NetworkSession& orig); - const NetworkSession& operator=(const NetworkSession& orig); - virtual~NetworkSession(); - - virtual bool Attach(Utility::DynamicMemory::SmartPointer client); - - virtual Utility::DynamicMemory::SmartPointer Detach(Oyster::Network::NetworkClient* client); - virtual Utility::DynamicMemory::SmartPointer Detach(const LobbyClient* client); - virtual Utility::DynamicMemory::SmartPointer Detach(const LobbyClient& client); - virtual Utility::DynamicMemory::SmartPointer Detach(short ID); - - Utility::DynamicMemory::SmartPointer FindClient(LobbyClient& obj); - Utility::DynamicMemory::SmartPointer FindClient(LobbyClient* obj); - Utility::DynamicMemory::SmartPointer FindClient(int ID); - - /** - * Sends a message to all clients in this session. - */ - virtual bool Send(Oyster::Network::CustomNetProtocol& message); - /** - * Sends a message to a specific client in this session. - */ - virtual bool Send(Oyster::Network::CustomNetProtocol& protocol, int ID); - - /** - * Set the callback to all clients to where a messages is recieved. - */ - virtual void SetCallback(Oyster::Callback::OysterCallback value); - - /** - * Closes the session and sends the clients to given owner session if any. - * If session is null, clients is assumed to already be elsewhere and only releases a reference. - */ - virtual void CloseSession(bool dissconnectClients = false); - - - /** Set where the clients is returned on closed session. */ - inline void SetOwner(NetworkSession* owner) { this->owner = owner; } - - protected: - Utility::DynamicMemory::DynamicArray> clients; - NetworkSession* owner; //Where clients end up when session is closed. - - private: - std::mutex clientListLock; - int clientCount; - - private: - friend class AdminInterface; - }; -}//End namespace DanBias -#endif // !DANBIASSERVER_NETWORK_SESSION_H diff --git a/Code/Game/DanBiasServerLauncher/DanBiasServerLauncher.vcxproj b/Code/Game/DanBiasServerLauncher/DanBiasServerLauncher.vcxproj index ec88a915..62344a57 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\DanBiasServer;C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + $(SolutionDir)Game\GameServer;C:\Program Files %28x86%29\Visual Leak Detector\include;$(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\DanBiasServer;C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + $(SolutionDir)Game\GameServer;C:\Program Files %28x86%29\Visual Leak Detector\include;$(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\DanBiasServer;C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + $(SolutionDir)Game\GameServer;C:\Program Files %28x86%29\Visual Leak Detector\include;$(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\DanBiasServer;C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + $(SolutionDir)Game\GameServer;C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) $(OutDir)..\DLL\;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) diff --git a/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp b/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp index 17d1a045..7781717d 100644 --- a/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp +++ b/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp @@ -7,7 +7,7 @@ #include -#include +#include int WINAPI WinMain( HINSTANCE hinst, HINSTANCE prevInst, PSTR cmdLine, int cmdShow) { @@ -15,10 +15,14 @@ int WINAPI WinMain( HINSTANCE hinst, HINSTANCE prevInst, PSTR cmdLine, int cmdSh { return cmdShow; } - if( DanBias::DanBiasServerAPI::Initiate() == DanBias::DanBiasServerReturn_Sucess) + + DanBias::GameServerAPI::GameInitDesc desc; + desc.connectionPort = 15151; + desc.maxNumberOfClients = 0; + desc.threaded = false; + if( !DanBias::GameServerAPI::Create(desc) == DanBias::DanBiasServerReturn_Sucess) { - DanBias::DanBiasServerAPI::Run(); - DanBias::DanBiasServerAPI::Release(); + } return cmdShow; } \ No newline at end of file diff --git a/Code/Game/GameProtocols/LobbyProtocols.h b/Code/Game/GameProtocols/LobbyProtocols.h index f043a7a2..4a262404 100644 --- a/Code/Game/GameProtocols/LobbyProtocols.h +++ b/Code/Game/GameProtocols/LobbyProtocols.h @@ -45,14 +45,18 @@ namespace GameLogic struct Protocol_LobbyStartGame :public Oyster::Network::CustomProtocolObject { - char gameId; + short gameId; Protocol_LobbyStartGame() { this->protocol[protocol_INDEX_ID].value = protocol_Lobby_Start; this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; - this->protocol[1].type = Oyster::Network::NetAttributeType_Char; + this->protocol[1].type = Oyster::Network::NetAttributeType_Short; + } + Protocol_LobbyStartGame(Oyster::Network::CustomNetProtocol& o) + { + gameId = o[1].value.netInt; } Oyster::Network::CustomNetProtocol* GetProtocol() override { @@ -73,6 +77,10 @@ namespace GameLogic this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Short; + } + Protocol_LobbyLogin(Oyster::Network::CustomNetProtocol& p) + { + } Oyster::Network::CustomNetProtocol* GetProtocol() override { @@ -116,6 +124,10 @@ namespace GameLogic { this->protocol[protocol_INDEX_ID].value = protocol_Lobby_Login; this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + } + Protocol_LobbyRefresh(Oyster::Network::CustomNetProtocol& o) + { + } Oyster::Network::CustomNetProtocol* GetProtocol() override { return &protocol; } diff --git a/Code/Game/GameServer/GameClient.h b/Code/Game/GameServer/GameClient.h new file mode 100644 index 00000000..3fa0a6f6 --- /dev/null +++ b/Code/Game/GameServer/GameClient.h @@ -0,0 +1,34 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// +#ifndef DANBIASSERVER_CLIENT_OBJECT_H +#define DANBIASSERVER_CLIENT_OBJECT_H + +#include +#include +#include + +namespace DanBias +{ + class GameClient + { + public: + GameClient(Oyster::Network::NetworkClient 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(); + int GetID() const; + + private: + GameLogic::IPlayerData* player; + Oyster::Network::NetworkClient client; + int id; + }; + +}//End namespace DanBias +#endif // !DANBIASSERVER_CLIENT_OBJECT_H diff --git a/Code/Game/GameServer/GameLobby.h b/Code/Game/GameServer/GameLobby.h new file mode 100644 index 00000000..7a47638d --- /dev/null +++ b/Code/Game/GameServer/GameLobby.h @@ -0,0 +1,48 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// +#ifndef DANBIASSERVER_MAINLOBBY_H +#define DANBIASSERVER_MAINLOBBY_H + +#include +#include +#include +#include + +namespace DanBias +{ + class GameLobby :public Oyster::Network::NetworkSession + { + public: + GameLobby(); + virtual~GameLobby(); + + void Release(); + + void Frame(); + + //void ClientEventCallback(NetEvent e) override; + + private: + void ParseEvents(); + void ParseGeneralProtocol(Oyster::Network::CustomNetProtocol& p, Oyster::Network::NetworkClient* c); + void ParseLobbyProtocol(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); + + private: + void ClientEventCallback(Oyster::Network::NetEvent e) override; + void ClientConnectedEvent(Oyster::Network::NetEvent e) override; + + private: + Utility::WinTimer timer; + float refreshFrequency; + }; +}//End namespace DanBias +#endif // !DANBIASGAME_GAMELOBBY_H diff --git a/Code/Game/GameServer/GameServer.h b/Code/Game/GameServer/GameServer.h new file mode 100644 index 00000000..73857b2d --- /dev/null +++ b/Code/Game/GameServer/GameServer.h @@ -0,0 +1,33 @@ +///////////////////////////////////////////////////////////////////// +// 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/DanBiasServer/DanBiasServer.vcxproj b/Code/Game/GameServer/GameServer.vcxproj similarity index 78% rename from Code/Game/DanBiasServer/DanBiasServer.vcxproj rename to Code/Game/GameServer/GameServer.vcxproj index 74223981..e736a111 100644 --- a/Code/Game/DanBiasServer/DanBiasServer.vcxproj +++ b/Code/Game/GameServer/GameServer.vcxproj @@ -19,9 +19,10 @@ - {52380DAA-0F4A-4D97-8E57-98DF39319CAF} + {143BD516-20A1-4890-A3E4-F8BFD02220E7} Win32Proj - DanBiasServer + GameServer + GameServer @@ -73,32 +74,32 @@ $(SolutionDir)..\Bin\DLL\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName)D - $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Game\GameLogic\;$(SolutionDir)Network\NetworkAPI\;$(SolutionDir)OysterMath\;$(SolutionDir)GamePhysics\;$(SolutionDir)Misc\;$(SolutionDir)WindowManager\;C:\Program Files %28x86%29\Visual Leak Detector\include;$(SolutionDir)OysterPhysics3D\;$(IncludePath) - $(OutDir);$(SolutionDir)..\External\Lib\WindowManager\;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) + $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Game\GameLogic\;$(SolutionDir)Network\NetworkAPI\;$(SolutionDir)OysterMath\;$(SolutionDir)GamePhysics\;$(SolutionDir)Misc\;$(SolutionDir)WindowManager\;$(SolutionDir)OysterPhysics3D\;$(SolutionDir)Game\ServerDependencies;C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + $(OutDir);$(SolutionDir)..\External\Lib\WindowManager\;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(SolutionDir)..\External\Lib\ServerDependencies\;$(LibraryPath) true $(SolutionDir)..\Bin\DLL\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName)D - $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Game\GameLogic\;$(SolutionDir)Network\NetworkAPI\;$(SolutionDir)OysterMath\;$(SolutionDir)GamePhysics\;$(SolutionDir)Misc\;$(SolutionDir)WindowManager\;C:\Program Files %28x86%29\Visual Leak Detector\include;$(SolutionDir)OysterPhysics3D\;$(IncludePath) - $(OutDir);$(SolutionDir)..\External\Lib\WindowManager\;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) + $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Game\GameLogic\;$(SolutionDir)Network\NetworkAPI\;$(SolutionDir)OysterMath\;$(SolutionDir)GamePhysics\;$(SolutionDir)Misc\;$(SolutionDir)WindowManager\;$(SolutionDir)OysterPhysics3D\;$(SolutionDir)Game\ServerDependencies;C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + $(OutDir);$(SolutionDir)..\External\Lib\WindowManager\;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(SolutionDir)..\External\Lib\ServerDependencies\;$(LibraryPath) false $(SolutionDir)..\Bin\DLL\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName) - $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Game\GameLogic\;$(SolutionDir)Network\NetworkAPI\;$(SolutionDir)OysterMath\;$(SolutionDir)GamePhysics\;$(SolutionDir)Misc\;$(SolutionDir)WindowManager\;C:\Program Files %28x86%29\Visual Leak Detector\include;$(SolutionDir)OysterPhysics3D\;$(IncludePath) - $(OutDir);$(SolutionDir)..\External\Lib\WindowManager\;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) + $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Game\GameLogic\;$(SolutionDir)Network\NetworkAPI\;$(SolutionDir)OysterMath\;$(SolutionDir)GamePhysics\;$(SolutionDir)Misc\;$(SolutionDir)WindowManager\;$(SolutionDir)OysterPhysics3D\;$(SolutionDir)Game\ServerDependencies;C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + $(OutDir);$(SolutionDir)..\External\Lib\WindowManager\;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(SolutionDir)..\External\Lib\ServerDependencies\;$(LibraryPath) false $(SolutionDir)..\Bin\DLL\ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName) - $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Game\GameLogic\;$(SolutionDir)Network\NetworkAPI\;$(SolutionDir)OysterMath\;$(SolutionDir)GamePhysics\;$(SolutionDir)Misc\;$(SolutionDir)WindowManager\;C:\Program Files %28x86%29\Visual Leak Detector\include;$(SolutionDir)OysterPhysics3D\;$(IncludePath) - $(OutDir);$(SolutionDir)..\External\Lib\WindowManager\;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) + $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Game\GameLogic\;$(SolutionDir)Network\NetworkAPI\;$(SolutionDir)OysterMath\;$(SolutionDir)GamePhysics\;$(SolutionDir)Misc\;$(SolutionDir)WindowManager\;$(SolutionDir)OysterPhysics3D\;$(SolutionDir)Game\ServerDependencies;C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + $(OutDir);$(SolutionDir)..\External\Lib\WindowManager\;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(SolutionDir)..\External\Lib\ServerDependencies\;$(LibraryPath) @@ -173,39 +174,35 @@ - - - - - - - - - - - - - - - - - + + + + + + + + + + + true + true + true + true + + + true + true + true + true + - - + + - - - - - - - - - - - + + + diff --git a/Code/Game/DanBiasServer/DanBiasServer.vcxproj.user b/Code/Game/GameServer/GameServer.vcxproj.user similarity index 100% rename from Code/Game/DanBiasServer/DanBiasServer.vcxproj.user rename to Code/Game/GameServer/GameServer.vcxproj.user diff --git a/Code/Game/DanBiasServer/DanBiasServerAPI.h b/Code/Game/GameServer/GameServerAPI.h similarity index 73% rename from Code/Game/DanBiasServer/DanBiasServerAPI.h rename to Code/Game/GameServer/GameServerAPI.h index 1d514053..08e25114 100644 --- a/Code/Game/DanBiasServer/DanBiasServerAPI.h +++ b/Code/Game/GameServer/GameServerAPI.h @@ -24,12 +24,18 @@ namespace DanBias extern "C" { - class DANBIAS_SERVER_DLL DanBiasServerAPI + class DANBIAS_SERVER_DLL GameServerAPI { public: - static DanBiasServerReturn Initiate(); - static DanBiasServerReturn Run(); - static DanBiasServerReturn Release(); + struct GameInitDesc + { + //stuff + int connectionPort; + int maxNumberOfClients; + bool threaded; + }; + public: + static DanBiasServerReturn Create(const GameInitDesc& desc); };//End class DanBiasServer }//End Extern "C" } //End namspace DanBias diff --git a/Code/Game/DanBiasServer/GameSession/GameSession.h b/Code/Game/GameServer/GameSession.h similarity index 85% rename from Code/Game/DanBiasServer/GameSession/GameSession.h rename to Code/Game/GameServer/GameSession.h index aee9b29d..eed2b4b4 100644 --- a/Code/Game/DanBiasServer/GameSession/GameSession.h +++ b/Code/Game/GameServer/GameSession.h @@ -7,18 +7,18 @@ //warning C4150: deletion of pointer to incomplete type, no destructor called, because of forward decleration and the use of smartpointer. #pragma warning(disable: 4150) -#include "..\LobbySessions\NetworkSession.h" + +#include "GameClient.h" #include #include #include #include #include +#include namespace DanBias { - class LobbyClient; - class GameClient; - class GameSession : public Oyster::Thread::IThreadObject, public INetworkSession + class GameSession : public Oyster::Thread::IThreadObject { public: /** @@ -28,7 +28,7 @@ namespace DanBias { std::wstring mapName; NetworkSession* owner; - Utility::DynamicMemory::DynamicArray> clients; + Utility::DynamicMemory::DynamicArray clients; }; public: @@ -44,7 +44,7 @@ namespace DanBias /** Join an existing/running game session * @param client The client to attach to the session */ - bool Join(Utility::DynamicMemory::SmartPointer client); + bool Join(Utility::DynamicMemory::SmartPointer client); /** * Closes the game session @@ -82,7 +82,7 @@ namespace DanBias private: Utility::DynamicMemory::DynamicArray> clients; - Oyster::IPostBox *box; + //Oyster::PostBox *box; Oyster::Thread::OysterThread worker; GameLogic::GameAPI& gameInstance; GameLogic::ILevelData *levelData; @@ -93,8 +93,6 @@ namespace DanBias static void ObjectMove(GameLogic::IObjectData* movedObject); - private: - friend class AdminInterface; };//End GameSession }//End namespace DanBias diff --git a/Code/Game/DanBiasServer/GameSession/GameSessionManager.h b/Code/Game/GameServer/GameSessionManager.h similarity index 90% rename from Code/Game/DanBiasServer/GameSession/GameSessionManager.h rename to Code/Game/GameServer/GameSessionManager.h index 75b0e64d..a2c8a3ee 100644 --- a/Code/Game/DanBiasServer/GameSession/GameSessionManager.h +++ b/Code/Game/GameServer/GameSessionManager.h @@ -4,19 +4,19 @@ #ifndef DANBIASSERVER_GAME_SEESION_MANAGER_H #define DANBIASSERVER_GAME_SEESION_MANAGER_H +#include #include #include #include +using namespace Oyster::Network; + namespace DanBias { - class LobbyClient; - class NetworkSession; - struct GameSessionDescription { std::wstring mapName; - Utility::DynamicMemory::DynamicArray> clients; + Utility::DynamicMemory::DynamicArray clients; NetworkSession* exitDestionation; //The new owner when session dies }; struct GameSessionInfo @@ -49,7 +49,7 @@ namespace DanBias * @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); + static bool JoinSession(int session, Utility::DynamicMemory::SmartPointer client); /** * Gets information about a given session @@ -67,7 +67,7 @@ namespace DanBias /** * Close all sessions. */ - static void CloseSession(); + static void CloseSessions(); /** * Get total sessions running diff --git a/Code/Game/DanBiasServer/DLLMain.cpp b/Code/Game/GameServer/Implementation/DLLMain.cpp similarity index 100% rename from Code/Game/DanBiasServer/DLLMain.cpp rename to Code/Game/GameServer/Implementation/DLLMain.cpp diff --git a/Code/Game/GameServer/Implementation/GameClient.cpp b/Code/Game/GameServer/Implementation/GameClient.cpp new file mode 100644 index 00000000..94ac807a --- /dev/null +++ b/Code/Game/GameServer/Implementation/GameClient.cpp @@ -0,0 +1,50 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// + +#include "..\GameClient.h" +#include +#include + +using namespace Utility::DynamicMemory; +using namespace DanBias; +using namespace GameLogic; + +static int gameClientIDCount = 1; + +GameClient::GameClient(Oyster::Network::NetworkClient client, GameLogic::IPlayerData* player) +{ + this->client = client; + this->id = gameClientIDCount++; + this->player = player; +} +GameClient::~GameClient() +{ + this->client.Disconnect(); + this->player = 0; + this->id = -1; +} + +GameLogic::IPlayerData* GameClient::GetPlayer() +{ + return this->player; +} +GameLogic::IPlayerData* GameClient::ReleasePlayer() +{ + GameLogic::IPlayerData *temp = this->player; + this->player = 0; + return temp; +} +Oyster::Network::NetworkClient* GameClient::GetClient() +{ + return &this->client; +} +Oyster::Network::NetworkClient GameClient::ReleaseClient() +{ + Oyster::Network::NetworkClient temp = this->client; + return temp; +} +int GameClient::GetID() const +{ + return this->id; +} diff --git a/Code/Game/GameServer/Implementation/GameLobby.cpp b/Code/Game/GameServer/Implementation/GameLobby.cpp new file mode 100644 index 00000000..ad3de1c7 --- /dev/null +++ b/Code/Game/GameServer/Implementation/GameLobby.cpp @@ -0,0 +1,51 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// +#include "..\GameLobby.h" +#include +#include + +using namespace Utility::DynamicMemory; +using namespace Oyster::Network; +using namespace Oyster; + +namespace DanBias +{ + GameLobby::GameLobby() + { } + + GameLobby::~GameLobby() + { } + + void GameLobby::Release() + { } + + void GameLobby::Frame() + { + ParseEvents(); + } + + void GameLobby::ClientEventCallback(NetEvent e) + { + + } + void GameLobby::ClientConnectedEvent(NetEvent e) + { + + } + +//////// 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); + //} + } + +}//End namespace DanBias \ No newline at end of file diff --git a/Code/Game/GameServer/Implementation/GameServer.cpp b/Code/Game/GameServer/Implementation/GameServer.cpp new file mode 100644 index 00000000..90620c18 --- /dev/null +++ b/Code/Game/GameServer/Implementation/GameServer.cpp @@ -0,0 +1,139 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// +#define NOMINMAX +#include +#include +#include + + +#include "..\GameServer.h" +#include "..\GameSessionManager.h" +#include "..\GameSession.h" + +#include +#include +#include +#include +#include + +namespace DanBias +{ + using namespace Oyster::Network; + + GameServer* instance = 0; + std::thread workerThread; + typedef void(*WorkerThreadFnc)(GameServer*); + + DanBiasServerReturn GameServerAPI::Create(const GameInitDesc& desc) + { + if(!instance) + instance = new GameServer(); + + return instance->Create(desc); + } + + + + 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(); + + 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/DanBiasServer/GameSession/GameSessionManager.cpp b/Code/Game/GameServer/Implementation/GameSessionManager.cpp similarity index 93% rename from Code/Game/DanBiasServer/GameSession/GameSessionManager.cpp rename to Code/Game/GameServer/Implementation/GameSessionManager.cpp index 23dadb65..5eb8d664 100644 --- a/Code/Game/DanBiasServer/GameSession/GameSessionManager.cpp +++ b/Code/Game/GameServer/Implementation/GameSessionManager.cpp @@ -1,10 +1,8 @@ ///////////////////////////////////////////////////////////////////// // Created by [Dennis Andersen] [2013] ///////////////////////////////////////////////////////////////////// -#include "GameSessionManager.h" - -#include "..\LobbySessions\LobbyClient.h" -#include "GameSession.h" +#include "..\GameSessionManager.h" +#include "..\GameSession.h" #include using namespace DanBias; @@ -75,7 +73,7 @@ bool GameSessionManager::StartSession(int session) return true; } -bool GameSessionManager::JoinSession(int session, Utility::DynamicMemory::SmartPointer client) +bool GameSessionManager::JoinSession(int session, Utility::DynamicMemory::SmartPointer client) { int i = -1; if((i = gameSessionData.Existst(session)) == -1) return false; @@ -97,7 +95,7 @@ void GameSessionManager::GetSessionInfo(int session, GameSessionInfo& data) //data.numberOfPlayers = gameSessionData.sessions[i]-> } -void GameSessionManager::CloseSession() +void GameSessionManager::CloseSessions() { for (unsigned int i = 0; i < gameSessionData.sessions.Size(); i++) { diff --git a/Code/Game/DanBiasServer/GameSession/GameSession_Events.cpp b/Code/Game/GameServer/Implementation/GameSession_Events.cpp similarity index 96% rename from Code/Game/DanBiasServer/GameSession/GameSession_Events.cpp rename to Code/Game/GameServer/Implementation/GameSession_Events.cpp index 5dac3c3e..b3b831f6 100644 --- a/Code/Game/DanBiasServer/GameSession/GameSession_Events.cpp +++ b/Code/Game/GameServer/Implementation/GameSession_Events.cpp @@ -1,9 +1,10 @@ ///////////////////////////////////////////////////////////////////// // Created by [Dennis Andersen] [2013] ///////////////////////////////////////////////////////////////////// -#include "GameSession.h" -#include "GameClient.h" +#include "..\GameSession.h" +#include "..\GameClient.h" +#include #include #include #include diff --git a/Code/Game/DanBiasServer/GameSession/GameSession_General.cpp b/Code/Game/GameServer/Implementation/GameSession_General.cpp similarity index 96% rename from Code/Game/DanBiasServer/GameSession/GameSession_General.cpp rename to Code/Game/GameServer/Implementation/GameSession_General.cpp index 0f9f3c57..e68ac8dc 100644 --- a/Code/Game/DanBiasServer/GameSession/GameSession_General.cpp +++ b/Code/Game/GameServer/Implementation/GameSession_General.cpp @@ -1,8 +1,8 @@ ///////////////////////////////////////////////////////////////////// // Created by [Dennis Andersen] [2013] ///////////////////////////////////////////////////////////////////// -#include "GameSession.h" -#include "GameClient.h" +#include "..\GameSession.h" +#include "..\GameClient.h" #include "..\GameServer.h" #include #include @@ -105,7 +105,7 @@ namespace DanBias } } - bool GameSession::Join(Utility::DynamicMemory::SmartPointer client) + bool GameSession::Join(Utility::DynamicMemory::SmartPointer client) { if(!this->isCreated) return false; diff --git a/Code/Game/DanBiasServer/GameSession/GameSession_Logic.cpp b/Code/Game/GameServer/Implementation/GameSession_Logic.cpp similarity index 96% rename from Code/Game/DanBiasServer/GameSession/GameSession_Logic.cpp rename to Code/Game/GameServer/Implementation/GameSession_Logic.cpp index ddf41582..1388f6fe 100644 --- a/Code/Game/DanBiasServer/GameSession/GameSession_Logic.cpp +++ b/Code/Game/GameServer/Implementation/GameSession_Logic.cpp @@ -1,8 +1,8 @@ ///////////////////////////////////////////////////////////////////// // Created by [Dennis Andersen] [2013] ///////////////////////////////////////////////////////////////////// -#include "GameSession.h" -#include "GameClient.h" +#include "..\GameSession.h" +#include "..\GameClient.h" #include #include diff --git a/Code/Game/DanBiasServer/GameSession/GameSession_Network.cpp b/Code/Game/GameServer/Implementation/GameSession_Network.cpp similarity index 92% rename from Code/Game/DanBiasServer/GameSession/GameSession_Network.cpp rename to Code/Game/GameServer/Implementation/GameSession_Network.cpp index 729ef417..df546b35 100644 --- a/Code/Game/DanBiasServer/GameSession/GameSession_Network.cpp +++ b/Code/Game/GameServer/Implementation/GameSession_Network.cpp @@ -1,8 +1,8 @@ ///////////////////////////////////////////////////////////////////// // Created by [Dennis Andersen] [2013] ///////////////////////////////////////////////////////////////////// -#include "GameSession.h" -#include "GameClient.h" +#include "..\GameSession.h" +#include "..\GameClient.h" #include #include diff --git a/Code/Game/GameServer/Implementation/LobbyGeneralProtocolParser.cpp b/Code/Game/GameServer/Implementation/LobbyGeneralProtocolParser.cpp new file mode 100644 index 00000000..23195087 --- /dev/null +++ b/Code/Game/GameServer/Implementation/LobbyGeneralProtocolParser.cpp @@ -0,0 +1,52 @@ +#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 new file mode 100644 index 00000000..4063db18 --- /dev/null +++ b/Code/Game/GameServer/Implementation/LobbyProtocolParser.cpp @@ -0,0 +1,58 @@ +#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 c4ff4194..7e533171 100644 --- a/Code/Game/aDanBiasGameLauncher/Launcher.cpp +++ b/Code/Game/aDanBiasGameLauncher/Launcher.cpp @@ -29,6 +29,7 @@ void ClientFnc() //gameDesc.IP = "193.11.184.31"; //gameDesc.IP = "194.47.150.56"; gameDesc.IP = "127.0.0.1"; + //gameDesc.IP = "194.47.150.184"; if( DanBias::DanBiasGame::Initiate(gameDesc) == DanBias::DanBiasClientReturn_Sucess) { diff --git a/Code/Game/aDanBiasGameLauncher/aDanBiasGameLauncher.vcxproj b/Code/Game/aDanBiasGameLauncher/aDanBiasGameLauncher.vcxproj index b2b5b766..5f33ada6 100644 --- a/Code/Game/aDanBiasGameLauncher/aDanBiasGameLauncher.vcxproj +++ b/Code/Game/aDanBiasGameLauncher/aDanBiasGameLauncher.vcxproj @@ -177,9 +177,6 @@ {2a1bc987-af42-4500-802d-89cd32fc1309} - - {52380daa-0f4a-4d97-8e57-98df39319caf} - diff --git a/Code/Misc/DynamicArray.h b/Code/Misc/DynamicArray.h index e2041f99..1db30719 100644 --- a/Code/Misc/DynamicArray.h +++ b/Code/Misc/DynamicArray.h @@ -4,6 +4,8 @@ #ifndef MISC_DYNAMIC_ARRAY_H #define MISC_DYNAMIC_ARRAY_H +#include + namespace Utility { namespace DynamicMemory diff --git a/Code/Misc/Thread/OysterThread.h b/Code/Misc/Thread/OysterThread.h index cd4bfd3f..739487d9 100644 --- a/Code/Misc/Thread/OysterThread.h +++ b/Code/Misc/Thread/OysterThread.h @@ -72,8 +72,7 @@ namespace Oyster //OYSTER_THREAD_ERROR Create(Oyster::Callback::CallbackFunction::FNC worker, bool start, bool detach = false); OYSTER_THREAD_ERROR Start(); OYSTER_THREAD_ERROR Stop(); - OYSTER_THREAD_ERROR Pause(); - OYSTER_THREAD_ERROR Pause(int mSec); + OYSTER_THREAD_ERROR Stop(int msec); OYSTER_THREAD_ERROR Resume(); OYSTER_THREAD_ERROR SetWorker(IThreadObject* worker = 0); OYSTER_THREAD_ERROR SetWorker(ThreadFnc worker = 0); diff --git a/Code/Misc/Thread/OysterThread_Impl.cpp b/Code/Misc/Thread/OysterThread_Impl.cpp index 6c89567c..a09574df 100644 --- a/Code/Misc/Thread/OysterThread_Impl.cpp +++ b/Code/Misc/Thread/OysterThread_Impl.cpp @@ -229,6 +229,7 @@ OYSTER_THREAD_ERROR OysterThread::Create(IThreadObject* worker, bool start, bool { 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; @@ -257,15 +258,11 @@ OYSTER_THREAD_ERROR OysterThread::Start() return OYSTER_THREAD_ERROR_SUCCESS; } OYSTER_THREAD_ERROR OysterThread::Stop() -{ - return this->Terminate(); -} -OYSTER_THREAD_ERROR OysterThread::Pause() { this->privateData->data->threadData->state = OYSTER_THREAD_STATE_IDLE; return OYSTER_THREAD_ERROR_SUCCESS; } -OYSTER_THREAD_ERROR OysterThread::Pause(int msec) +OYSTER_THREAD_ERROR OysterThread::Stop(int msec) { this->privateData->data->threadData->msec = msec; return OYSTER_THREAD_ERROR_SUCCESS; diff --git a/Code/Misc/ThreadSafeQueue.h b/Code/Misc/ThreadSafeQueue.h index 3fab7c70..3afb1cb3 100644 --- a/Code/Misc/ThreadSafeQueue.h +++ b/Code/Misc/ThreadSafeQueue.h @@ -10,6 +10,7 @@ ///////////////////////////////////////////// #include "IQueue.h" +#include namespace Utility { @@ -20,6 +21,7 @@ namespace Utility { public: ThreadSafeQueue(); + ThreadSafeQueue(const ThreadSafeQueue& obj); virtual ~ThreadSafeQueue(); virtual void Push( Type item ); @@ -64,6 +66,12 @@ namespace Utility } + template < typename Type > + ThreadSafeQueue::ThreadSafeQueue(const ThreadSafeQueue& obj) + { + + } + template < typename Type > ThreadSafeQueue::~ThreadSafeQueue() { diff --git a/Code/Network/NetworkAPI/CustomNetProtocol.h b/Code/Network/NetworkAPI/CustomNetProtocol.h index 85997c51..8e531f17 100644 --- a/Code/Network/NetworkAPI/CustomNetProtocol.h +++ b/Code/Network/NetworkAPI/CustomNetProtocol.h @@ -6,12 +6,7 @@ #include //#include - -#ifdef CUSTOM_NET_PROTOCOL_EXPORT - #define NET_PROTOCOL_EXPORT __declspec(dllexport) -#else - #define NET_PROTOCOL_EXPORT __declspec(dllimport) -#endif +#include "NetworkAPI_Preprocessor.h" namespace Oyster { @@ -78,7 +73,7 @@ namespace Oyster virtual CustomNetProtocol* GetProtocol() = 0; }; - class NET_PROTOCOL_EXPORT CustomNetProtocol + class NET_API_EXPORT CustomNetProtocol { public: CustomNetProtocol(); diff --git a/Code/Network/NetworkAPI/NetworkAPI.vcxproj b/Code/Network/NetworkAPI/NetworkAPI.vcxproj index fe2f5c09..b4b8a31f 100644 --- a/Code/Network/NetworkAPI/NetworkAPI.vcxproj +++ b/Code/Network/NetworkAPI/NetworkAPI.vcxproj @@ -105,7 +105,7 @@ Level3 Disabled - CUSTOM_NET_PROTOCOL_EXPORT;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + NETWORKAPI_EXPORT;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) true @@ -119,7 +119,7 @@ Level3 Disabled - CUSTOM_NET_PROTOCOL_EXPORT;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + NETWORKAPI_EXPORT;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) true @@ -135,7 +135,7 @@ MaxSpeed true true - CUSTOM_NET_PROTOCOL_EXPORT;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + NETWORKAPI_EXPORT;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true @@ -153,7 +153,7 @@ MaxSpeed true true - CUSTOM_NET_PROTOCOL_EXPORT;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + NETWORKAPI_EXPORT;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true @@ -167,13 +167,16 @@ + - + + + diff --git a/Code/Network/NetworkAPI/NetworkAPI_Preprocessor.h b/Code/Network/NetworkAPI/NetworkAPI_Preprocessor.h new file mode 100644 index 00000000..351dbaf0 --- /dev/null +++ b/Code/Network/NetworkAPI/NetworkAPI_Preprocessor.h @@ -0,0 +1,10 @@ +#ifndef NETWORK_API_NETWORK_API_PREPROCESSOR_H +#define NETWORK_API_NETWORK_API_PREPROCESSOR_H + +#ifdef NETWORKAPI_EXPORT + #define NET_API_EXPORT __declspec(dllexport) +#else + #define NET_API_EXPORT __declspec(dllimport) +#endif + +#endif // !NETWORK_API_NETWORK_API_PREPROCESSOR_H diff --git a/Code/Network/NetworkAPI/NetworkCallbackHelper.h b/Code/Network/NetworkAPI/NetworkCallbackHelper.h deleted file mode 100644 index 963dc6de..00000000 --- a/Code/Network/NetworkAPI/NetworkCallbackHelper.h +++ /dev/null @@ -1,80 +0,0 @@ -#ifndef NETWORK_API_NETWORK_CALLBACK_HELPER_H -#define NETWORK_API_NETWORK_CALLBACK_HELPER_H - -///////////////////////////////////// -// Created by Dennis Andersen 2013 // -///////////////////////////////////// - -#include - - -namespace Oyster -{ - namespace Network - { - enum NetworkProtocolCallbackType - { - NetworkProtocolCallbackType_Function, - NetworkProtocolCallbackType_Object, - NetworkProtocolCallbackType_Unknown = -1, - }; - enum NetworkClientCallbackType - { - NetworkClientCallbackType_Function, - NetworkClientCallbackType_Object, - NetworkClientCallbackType_Unknown = -1, - }; - - class NetworkClient; - class CustomNetProtocol; - - - template - struct NetRecieverObject - { - virtual void NetworkCallback(Param) = 0; - }; - struct NetClientEvent :public NetRecieverObject - { - virtual void NetworkCallback(CustomNetProtocol& protocol) = 0; - virtual void Disconnected() { }; - }; - typedef NetRecieverObject ClientConnectedObject ; - typedef NetClientEvent ProtocolRecieverObject; - - - template - struct NetCallbackFunction - { - typedef void (*FNC)(Param); - }; - typedef NetCallbackFunction::FNC ClientConnectCallbackMethod; - typedef NetCallbackFunction::FNC ProtocolRecFunction; - struct NetClientMethods - { - typedef void(*Dissconnected)(void); - ProtocolRecFunction recieved; - Dissconnected dissconnected; - void operator()(CustomNetProtocol& obj) { if(recieved) recieved(obj); } - void operator()() { if(dissconnected) dissconnected(); } - }; - typedef NetClientMethods ProtocolRecieverFunction; - - union RecieverObject - { - ClientConnectCallbackMethod clientConnectFnc; // !< A function pointer for sending or recieving NetworkClient - ProtocolRecieverFunction protocolRecieverFnc; // !< A function pointer for sending or recieving CustomNetProtocol - - ClientConnectedObject *clientConnectObject; // !< An object for sending or recieving NetworkClient - ProtocolRecieverObject *protocolRecievedObject; // !< An object with collected client events methods. - - RecieverObject() { memset(this, 0, sizeof(RecieverObject)); } - RecieverObject(ClientConnectCallbackMethod o) { clientConnectFnc = o; } - RecieverObject(ProtocolRecieverFunction o) { protocolRecieverFnc = o; } - RecieverObject(ClientConnectedObject* o) { clientConnectObject = o; } - RecieverObject(ProtocolRecieverObject* o) { protocolRecievedObject = o; } - }; - } -} - -#endif \ No newline at end of file diff --git a/Code/Network/NetworkAPI/NetworkClient.cpp b/Code/Network/NetworkAPI/NetworkClient.cpp index b55e4d7e..71614f77 100644 --- a/Code/Network/NetworkAPI/NetworkClient.cpp +++ b/Code/Network/NetworkAPI/NetworkClient.cpp @@ -7,6 +7,7 @@ #include "Translator.h" #include "CustomNetProtocol.h" +#include "NetworkSession.h" #include "../NetworkDependencies/Connection.h" #include "../NetworkDependencies/PostBox.h" @@ -19,73 +20,53 @@ using namespace Oyster::Network; using namespace Oyster::Thread; using namespace Utility::DynamicMemory; +using namespace Utility::Container; /************************************* PrivateData *************************************/ -struct ClientDataContainer -{ +typedef NetworkClient::ClientEventArgs CEA; +struct NetDataContainer : public IThreadObject +{ //This struct is contained within a smart pointer. To avoide dependencies in link its implemented here.. + NetworkSession *owner; + NetworkClient *parent; Connection connection; - - SmartPointer> sendPostBox; - - RecieverObject recvObj; - NetworkProtocolCallbackType callbackType; - - Oyster::Thread::OysterThread thread; - std::mutex recvObjMutex; - std::mutex postBoxMutex; - Translator translator; + OysterThread thread; + + //Message queue for sending and recieving + ThreadSafeQueue sendQueue; + ThreadSafeQueue> recieveQueue; + //ID static unsigned int currID; const unsigned int ID; - ClientDataContainer(IThreadObject* o) - : ID(currID++) - { - InitWinSock(); - callbackType = NetworkProtocolCallbackType_Unknown; - sendPostBox = new PostBox(); - connection.InitiateClient(); - connection.SetBlockingMode(false); + NetDataContainer() + : ID(currID++) + , parent(0) + , owner(0) + { - } - ClientDataContainer(IThreadObject* o, unsigned int socket ) - :connection(socket), ID(currID++) - { InitWinSock(); - callbackType = NetworkProtocolCallbackType_Unknown; - sendPostBox = new PostBox(); - connection.InitiateClient(); - connection.SetBlockingMode(false); + this->thread.Create(this, true); + this->thread.SetPriority(Oyster::Thread::OYSTER_THREAD_PRIORITY_1); } - ~ClientDataContainer() - { - connection.Disconnect(); - thread.Stop(); - callbackType = NetworkProtocolCallbackType_Unknown; - + NetDataContainer(const NetDataContainer& obj) + :ID(obj.ID) { } + ~NetDataContainer() + { ShutdownWinSock(); + this->connection.Disconnect(); + this->thread.Terminate(); + this->owner = 0; + this->parent = 0; } - -}; -unsigned int ClientDataContainer::currID = 0; - -struct NetworkClient::PrivateData : public IThreadObject -{ - Utility::DynamicMemory::SmartPointer data; - - PrivateData() { this->data = new ClientDataContainer(this); } - PrivateData(unsigned int socket) { this->data = new ClientDataContainer(this, socket); } - ~PrivateData() { } - - bool DoWork() + bool DoWork() override { - if(!this->data) return false; - if(!this->data->connection.IsConnected()) return false; + if(!this->connection.IsConnected()) return false; Send(); Recv(); @@ -93,45 +74,25 @@ struct NetworkClient::PrivateData : public IThreadObject return true; } - - void Send(CustomNetProtocol* protocol) - { - if(!data) return; - - this->data->postBoxMutex.lock(); - this->data->sendPostBox->PostMessage(*protocol); - this->data->postBoxMutex.unlock(); - } - int Send() { int errorCode = 0; - if(!data) return -1; - this->data->postBoxMutex.lock(); - if(this->data->sendPostBox->IsFull()) + if(!this->sendQueue.IsEmpty()) { SmartPointer temp = new OysterByte(); - this->data->translator.Pack(temp, this->data->sendPostBox->FetchMessage()); - errorCode = this->data->connection.Send(temp); + CustomNetProtocol p = this->sendQueue.Pop(); + this->translator.Pack(temp, p); + errorCode = this->connection.Send(temp); if(errorCode != 0) { - //Failed - this->data->connection.Disconnect(); - - this->data->recvObjMutex.lock(); - if(this->data->callbackType == NetworkProtocolCallbackType_Function) - { - this->data->recvObj.protocolRecieverFnc(); - } - else if(this->data->callbackType == NetworkProtocolCallbackType_Object) - { - this->data->recvObj.protocolRecievedObject->Disconnected(); - } - this->data->recvObjMutex.unlock(); + CEA parg; + parg.type = CEA::EventType_ProtocolFailedToSend; + parg.data.protocol = p; + NetEvent e = { this->parent, parg }; + this->recieveQueue.Push(e); } } - this->data->postBoxMutex.unlock(); return errorCode; } @@ -141,105 +102,129 @@ struct NetworkClient::PrivateData : public IThreadObject int errorCode = -1; OysterByte temp = OysterByte(); - errorCode = this->data->connection.Recieve(temp); + errorCode = this->connection.Recieve(temp); if(errorCode == 0 && temp.GetSize()) { CustomNetProtocol protocol; - bool ok = this->data->translator.Unpack(protocol, temp); + bool ok = this->translator.Unpack(protocol, temp); //Check if the protocol was unpacked correctly if(ok) { - this->data->recvObjMutex.lock(); - if(this->data->callbackType == NetworkProtocolCallbackType_Function) - { - this->data->recvObj.protocolRecieverFnc(protocol); - } - else if(this->data->callbackType == NetworkProtocolCallbackType_Object) - { - this->data->recvObj.protocolRecievedObject->NetworkCallback(protocol); - } - this->data->recvObjMutex.unlock(); + CEA parg; + parg.type = CEA::EventType_ProtocolRecieved; + parg.data.protocol = protocol; + 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; + /************************************* NetworkClient *************************************/ NetworkClient::NetworkClient() -{ - privateData = new PrivateData(); - this->privateData->data->thread.SetPriority(Oyster::Thread::OYSTER_THREAD_PRIORITY_1); -} - -NetworkClient::NetworkClient(unsigned int socket) -{ - privateData = new PrivateData(socket); - this->privateData->data->thread.Create(this->privateData, true); - this->privateData->data->thread.SetPriority(Oyster::Thread::OYSTER_THREAD_PRIORITY_1); -} - -NetworkClient::NetworkClient(RecieverObject recvObj, NetworkProtocolCallbackType type) -{ - privateData = new PrivateData(); - this->privateData->data->callbackType = type; - this->privateData->data->recvObj = recvObj; -} - -NetworkClient::NetworkClient(RecieverObject recvObj, NetworkProtocolCallbackType type, unsigned int socket) -{ - privateData = new PrivateData(socket); - this->privateData->data->callbackType = type; - this->privateData->data->recvObj = recvObj; - this->privateData->data->thread.Create(this->privateData, true); - this->privateData->data->thread.SetPriority(Oyster::Thread::OYSTER_THREAD_PRIORITY_1); -} + : privateData(0) +{ } NetworkClient::NetworkClient(const NetworkClient& obj) { - this->privateData = new PrivateData(*obj.privateData); + if(obj.privateData) this->privateData = new PrivateData(*obj.privateData); + else this->privateData = 0; } NetworkClient& NetworkClient::operator =(const NetworkClient& obj) { delete privateData; - this->privateData = new PrivateData(*obj.privateData); + this->privateData = 0; + if(obj.privateData) this->privateData = new PrivateData(*obj.privateData); + return *this; } NetworkClient::~NetworkClient() { - if(privateData) + if(this->privateData) { - delete privateData; - privateData = NULL; + delete this->privateData; + this->privateData = NULL; } } +bool NetworkClient::operator ==(const NetworkClient& obj) +{ + return (this->privateData->dat->ID == obj.privateData->dat->ID); +} + +bool NetworkClient::operator ==(const int& ID) +{ + return this->privateData->dat->ID == ID; +} + +void NetworkClient::ProcessMessages() +{ + while (!this->privateData->dat->recieveQueue.IsEmpty()) + { + if(this->privateData->dat->owner) + { + this->privateData->dat->owner->ClientEventCallback(this->privateData->dat->recieveQueue.Pop()); + } + } +} + +bool NetworkClient::Connect(int socket) +{ + if(this->IsConnected()) return true; + if(this->privateData) return false; + if(!this->privateData) this->privateData = new PrivateData(); + + int result = this->privateData->dat->connection.Connect(socket, true); + + //Connect has succeeded + if(result == 0) return true; + + //Connect has failed + return false; +} + bool NetworkClient::Connect(unsigned short port, const char serverIP[]) { - privateData->data->connection.SetBlockingMode(true); - int result = this->privateData->data->connection.Connect(port, serverIP); + if(this->IsConnected()) return false; + if(this->privateData) return false; + if(!this->privateData) this->privateData = new PrivateData(); + + int result = this->privateData->dat->connection.Connect(port, serverIP, false); //Connect has succeeded - if(result == 0) - { - if(this->privateData->data->thread.IsCreated()) return false; - - this->privateData->data->thread.Create(this->privateData, true); - privateData->data->connection.SetBlockingMode(false); - return true; - } - - + if(result == 0) return true; //Connect has failed return false; @@ -247,46 +232,35 @@ bool NetworkClient::Connect(unsigned short port, const char serverIP[]) void NetworkClient::Disconnect() { - privateData->data->connection.Disconnect(); - privateData->data->thread.Terminate(); -} - -bool NetworkClient::IsConnected() -{ - return privateData->data->connection.IsConnected(); + privateData->dat->connection.Disconnect(); + privateData->dat->thread.Terminate(); } void NetworkClient::Send(CustomProtocolObject& protocol) { - this->privateData->Send(protocol.GetProtocol()); + this->privateData->dat->sendQueue.Push(*protocol.GetProtocol()); } void NetworkClient::Send(CustomNetProtocol* protocol) { - this->privateData->Send(protocol); + this->privateData->dat->sendQueue.Push(*protocol); } -void NetworkClient::SetRecieverObject(RecieverObject recvObj, NetworkProtocolCallbackType type) +void NetworkClient::SetOwner(NetworkSession* owner) { - if (type == NetworkProtocolCallbackType_Unknown) return; //It should probably still be set even if it is unknown. - - privateData->data->recvObjMutex.lock(); - privateData->data->recvObj = recvObj; - privateData->data->callbackType = type; - privateData->data->recvObjMutex.unlock(); + this->privateData->dat->owner = owner; } -bool NetworkClient::operator ==(const NetworkClient& obj) +bool NetworkClient::IsConnected() { - return (this->privateData->data->ID == obj.privateData->data->ID); -} - -bool NetworkClient::operator ==(const int& ID) -{ - return this->privateData->data->ID == ID; + if(!this->privateData) return false; + return privateData->dat->connection.IsConnected(); } int NetworkClient::GetID() const { - return this->privateData->data->ID; -} \ No newline at end of file + return this->privateData->dat->ID; +} + + + diff --git a/Code/Network/NetworkAPI/NetworkClient.h b/Code/Network/NetworkAPI/NetworkClient.h index 5a8dd5e5..39350537 100644 --- a/Code/Network/NetworkAPI/NetworkClient.h +++ b/Code/Network/NetworkAPI/NetworkClient.h @@ -1,59 +1,101 @@ #ifndef NETWORK_API_NETWORK_CLIENT_H #define NETWORK_API_NETWORK_CLIENT_H -///////////////////////////////////// -// Created by Pontus Fransson 2013 // -///////////////////////////////////// +////////////////////////////////////// +// Created by Pontus Fransson 2013 // +// Modified by Dennis Andersen 2014 // +////////////////////////////////////// -#ifdef CUSTOM_NET_PROTOCOL_EXPORT - #define NET_PROTOCOL_EXPORT __declspec(dllexport) -#else - #define NET_PROTOCOL_EXPORT __declspec(dllimport) -#endif - -#include "NetworkCallbackHelper.h" -//#include +#include "CustomNetProtocol.h" +#include "NetworkServerEventStruct.h" +#include "NetworkAPI_Preprocessor.h" namespace Oyster { namespace Network { + class NetworkSession; + extern "C" { - struct CustomProtocolObject; - class NET_PROTOCOL_EXPORT NetworkClient + class NET_API_EXPORT NetworkClient { + public: + struct ClientEventArgs + { + enum EventType + { + EventType_ProtocolFailedToRecieve, // No data + EventType_ProtocolFailedToSend, // Data in data.protocol + EventType_ProtocolRecieved, // Data in data.protocol + EventType_Disconnect, // No data + } type; + + union EventData + { + struct { Oyster::Network::CustomNetProtocol protocol; }; + void * nothing; + } data; + }; + typedef void(*ClientEventFunction)(NetEvent e); + public: NetworkClient(); - NetworkClient(unsigned int socket); - NetworkClient(RecieverObject recvObj, NetworkProtocolCallbackType type); - NetworkClient(RecieverObject recvObj, NetworkProtocolCallbackType type, unsigned int socket); NetworkClient(const NetworkClient& obj); - NetworkClient& operator =(const NetworkClient& obj); virtual ~NetworkClient(); - bool Connect(unsigned short port, const char serverIP[]); - void Disconnect(); - - bool IsConnected(); - - //Adds the protocol to the queue of protocols to be sent. - void Send(CustomProtocolObject& protocol); - void Send(CustomNetProtocol* protocol); - - void SetRecieverObject(RecieverObject recvObj, NetworkProtocolCallbackType type); - - //Compares the internal ID. bool operator ==(const NetworkClient& obj); bool operator ==(const int& ID); + /** + * + */ + void ProcessMessages(); + + /** + * + */ + bool Connect(int socket); + + /** + * + */ + bool Connect(unsigned short port, const char serverIP[]); + + /** + * + */ + void Disconnect(); + + /** + * + */ + void Send(CustomProtocolObject& protocol); + + /** + * + */ + void Send(CustomNetProtocol* protocol); + + /** + * + */ + void SetOwner(NetworkSession* owner); + + /** + * + */ + bool IsConnected(); + + /** + * + */ int GetID() const; private: struct PrivateData; PrivateData* privateData; - }; } } diff --git a/Code/Network/NetworkAPI/NetworkServer.cpp b/Code/Network/NetworkAPI/NetworkServer.cpp index ec4da231..de341f81 100644 --- a/Code/Network/NetworkAPI/NetworkServer.cpp +++ b/Code/Network/NetworkAPI/NetworkServer.cpp @@ -23,146 +23,50 @@ using namespace Oyster::Thread; struct NetworkServer::PrivateData : public IThreadObject { - PrivateData(); - ~PrivateData(); - - bool Init(INIT_DESC& initDesc); - bool Start(); - void Stop(); - void Shutdown(); - - void CheckForNewClient(); +public: + PrivateData() + : listener(0) + , mainSession(0) + , isInitiated(0) + , isReleased(0) + , isRunning(0) + { } + ~PrivateData() + { } bool DoWork(); - // +public: IListener* listener; - INIT_DESC initDesc; - bool started; - - //Postbox for new clients - PostBox postBox; - - //Server thread - OysterThread thread; + PostBox postBox; //Postbox for new clients + OysterThread thread; //Server thread + NetworkSession *mainSession; + Utility::Container::ThreadSafeQueue clientQueue; + bool isInitiated; + bool isReleased; + bool isRunning; + int port; }; -NetworkServer::PrivateData::PrivateData() -{ - listener = 0; - started = false; - //postBox = new PostBox; -} - -NetworkServer::PrivateData::~PrivateData() -{ - Shutdown(); -} - -bool NetworkServer::PrivateData::Init(INIT_DESC& initDesc) -{ - //Check if it's a valid port - if(initDesc.port == 0) - { - return false; - } - - if(!InitWinSock()) - return false; - - this->initDesc = initDesc; - - //Initiate listener - listener = new Listener(&postBox); - if(!((Listener*)listener)->Init(this->initDesc.port, false)) - { - return false; - } - - if(thread.Create(this, false) == OYSTER_THREAD_ERROR_FAILED) - { - return false; - } - - return true; -} - -bool NetworkServer::PrivateData::Start() -{ - //Start listener - if(!((Listener*)listener)->Start()) - { - return false; - } - - started = true; - - if(thread.Start() == OYSTER_THREAD_ERROR_FAILED) - { - return false; - } - - return true; -} - -void NetworkServer::PrivateData::Stop() -{ - if(listener) - { - ((Listener*)listener)->Stop(); - } - - started = false; - - thread.Stop(); -} - -void NetworkServer::PrivateData::Shutdown() -{ - if(listener) - { - listener->Shutdown(); - delete listener; - listener = NULL; - } - - started = false; - thread.Terminate(); - - ShutdownWinSock(); -} - -//Checks for new clients and sends them to the proc function. -void NetworkServer::PrivateData::CheckForNewClient() +bool NetworkServer::PrivateData::DoWork() { + /** Check for new clients **/ if(postBox.IsFull()) { int clientSocketNum = postBox.FetchMessage(); - //Safety check that is probably not needed. if(clientSocketNum == -1) { - return; + //Something went wrong somewhere... do we care? } - //Create client and Proc function if the pointer is not NULL - if(initDesc.callbackType == NetworkClientCallbackType_Function) - { - Oyster::Network::NetworkClient *client = new Oyster::Network::NetworkClient(clientSocketNum); - initDesc.recvObj.clientConnectFnc(client); - } - else if(initDesc.callbackType == NetworkClientCallbackType_Object) - { - Oyster::Network::NetworkClient *client = new Oyster::Network::NetworkClient(clientSocketNum); - initDesc.recvObj.clientConnectObject->NetworkCallback(client); - } + + Oyster::Network::NetworkClient client; + client.Connect(clientSocketNum); + if(this->mainSession) + this->clientQueue.Push(client); } -} - -bool NetworkServer::PrivateData::DoWork() -{ - CheckForNewClient(); return true; } @@ -173,48 +77,142 @@ bool NetworkServer::PrivateData::DoWork() NetworkServer::NetworkServer() { - privateData = new PrivateData(); + this->privateData = new PrivateData(); +} +NetworkServer::NetworkServer(const NetworkServer& obj) +{ + delete this->privateData; + this->privateData = new PrivateData(*obj.privateData); +} +const NetworkServer& NetworkServer::operator=(const NetworkServer& obj) +{ + delete this->privateData; + this->privateData = new PrivateData(*obj.privateData); + return *this; } - NetworkServer::~NetworkServer() { - if(privateData) + if(this->privateData) { - delete privateData; + delete this->privateData; + this->privateData = 0; } } -bool NetworkServer::Init(INIT_DESC& initDesc) +NetworkServer::ServerReturnCode NetworkServer::Init(const int& port, NetworkSession const* mainSession) { - if(!privateData->Init(initDesc)) + //Check if it's a valid port + if(port == 0 || port == -1) { - return false; + return NetworkServer::ServerReturnCode_Error; + } + else if(this->privateData->port != 0 && this->privateData->port != -1) + { + return NetworkServer::ServerReturnCode_Error; + } + if(!InitWinSock()) + { + return NetworkServer::ServerReturnCode_Error; } - return true; + //Initiate listener + this->privateData->listener = new Listener(&this->privateData->postBox); + if(!((Listener*)this->privateData->listener)->Init(port, false)) + { + return NetworkServer::ServerReturnCode_Error; + } + + if(this->privateData->thread.Create(this->privateData, false) == OYSTER_THREAD_ERROR_FAILED) + { + return NetworkServer::ServerReturnCode_Error; + } + + this->privateData->isInitiated = true; + this->privateData->isReleased = false; + this->privateData->mainSession = 0; + return NetworkServer::ServerReturnCode_Sucess; } -bool NetworkServer::Start() +NetworkServer::ServerReturnCode NetworkServer::Start() { - if(!privateData->Start()) + //Start listener + if(!((Listener*)this->privateData->listener)->Start()) { - return false; + return NetworkServer::ServerReturnCode_Error; } - return true; + if(this->privateData->thread.Start() == OYSTER_THREAD_ERROR_FAILED) + { + return NetworkServer::ServerReturnCode_Error; + } + + this->privateData->isRunning = true; + return NetworkServer::ServerReturnCode_Sucess; } void NetworkServer::Stop() { - privateData->Stop(); + if(this->privateData->listener) + { + ((Listener*)this->privateData->listener)->Stop(); + } + + this->privateData->thread.Stop(); + + this->privateData->isRunning = false; } void NetworkServer::Shutdown() { - privateData->Shutdown(); + if(this->privateData->listener) + { + this->privateData->listener->Shutdown(); + delete this->privateData->listener; + this->privateData->listener = NULL; + } + + this->privateData->thread.Terminate(); + + ShutdownWinSock(); + + this->privateData->isRunning = false; + this->privateData->mainSession = 0; + this->privateData->isReleased = true; +} + +void NetworkServer::ProcessConnectedClients() +{ + while(!this->privateData->clientQueue.IsEmpty()) + { + if(this->privateData->mainSession) this->privateData->mainSession->Attach(this->privateData->clientQueue.Pop()); + } +} + +void NetworkServer::SetSession(NetworkSession const* mainSession) +{ + this->privateData->mainSession = const_cast(mainSession); +} + +NetworkSession const* NetworkServer::GetMainSession() +{ + return this->privateData->mainSession; +} + +NetworkSession const* NetworkServer::ReleaseMainSessionSession() +{ + NetworkSession const * temp; + temp = this->privateData->mainSession; + this->privateData->mainSession = 0; + return temp; } bool NetworkServer::IsStarted() const { - return privateData->started; -} \ No newline at end of file + return this->privateData->isRunning; +} + + + + + + diff --git a/Code/Network/NetworkAPI/NetworkServer.h b/Code/Network/NetworkAPI/NetworkServer.h index 8edc59ff..c3385f86 100644 --- a/Code/Network/NetworkAPI/NetworkServer.h +++ b/Code/Network/NetworkAPI/NetworkServer.h @@ -1,18 +1,15 @@ #ifndef NETWORK_API_NETWORK_SERVER_H #define NETWORK_API_NETWORK_SERVER_H -///////////////////////////////////// -// Created by Pontus Fransson 2013 // -///////////////////////////////////// +////////////////////////////////////// +// Created by Pontus Fransson 2013 // +// Modified by Dennis Andersen 2014 // +////////////////////////////////////// -#ifdef CUSTOM_NET_PROTOCOL_EXPORT - #define NET_PROTOCOL_EXPORT __declspec(dllexport) -#else - #define NET_PROTOCOL_EXPORT __declspec(dllimport) -#endif +#include "NetworkAPI_Preprocessor.h" #include "NetworkClient.h" -#include "NetworkCallbackHelper.h" +#include "NetworkSession.h" #include namespace Oyster @@ -21,27 +18,68 @@ namespace Oyster { extern "C" { - class NET_PROTOCOL_EXPORT NetworkServer + class NET_API_EXPORT NetworkServer { public: - struct INIT_DESC + enum ServerReturnCode { - unsigned short port; //Port the server should be accepting clients on. - - NetworkClientCallbackType callbackType; //The recieverObject type. Function or object. - RecieverObject recvObj; //The functions that is called when a new client has connected. + ServerReturnCode_Error, + ServerReturnCode_Sucess }; + public: NetworkServer(); + NetworkServer(const NetworkServer&); + const NetworkServer& operator=(const NetworkServer&); virtual ~NetworkServer(); - bool Init(INIT_DESC& initDesc); - bool Start(); + + /** Creates a server that clients can connect to + * @param port The port the server will be listening for clients. + * @param mainSession The main session the server will send connected clients to. + * @return The server returncode + */ + ServerReturnCode Init(const int& port, NetworkSession const* mainSession); + + /** Starts the server allowing clients to connect + * @return The server returncode + */ + ServerReturnCode Start(); + + /** + * + */ void Stop(); + + /** Shutdown the server and return all resources. + */ void Shutdown(); + /** Parses asynchronous connected clients. + */ + void ProcessConnectedClients(); + + /** Set the main session connected clients will enter when connected to server. + * @param mainSession The session to connect as main server session. + */ + void SetSession(NetworkSession const* mainSession); + + /** Get the main session connected with the server + * @return Returns the main session + */ + NetworkSession const* GetMainSession(); + + /** Sets the main session to NULL and returns it + * @return Returns the main session + */ + NetworkSession const* ReleaseMainSessionSession(); + + /** + * + */ bool IsStarted() const; + private: struct PrivateData; PrivateData* privateData; diff --git a/Code/Network/NetworkAPI/NetworkServerEventStruct.h b/Code/Network/NetworkAPI/NetworkServerEventStruct.h new file mode 100644 index 00000000..a48d97de --- /dev/null +++ b/Code/Network/NetworkAPI/NetworkServerEventStruct.h @@ -0,0 +1,17 @@ +#ifndef NETWORK_API_SERVEREVENT_H +#define NETWORK_API_SERVEREVENT_H + +namespace Oyster +{ + namespace Network + { + template + struct NetEvent + { + Sender sender; + Args args; + }; + } +} + +#endif // !SERVERDEPENDENCIES_SERVEREVENT_H diff --git a/Code/Network/NetworkAPI/NetworkSession.cpp b/Code/Network/NetworkAPI/NetworkSession.cpp new file mode 100644 index 00000000..e026413e --- /dev/null +++ b/Code/Network/NetworkAPI/NetworkSession.cpp @@ -0,0 +1,184 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// + +#include "NetworkSession.h" +#include "..\..\Misc\Utilities.h" +#include "..\..\Misc\DynamicArray.h" +#include "..\..\Misc\GID.h" +#include "CustomNetProtocol.h" +#include +#include + + +using namespace Oyster::Network; + + +struct NetworkSession::PrivateSessionData +{ + Utility::DynamicMemory::DynamicArray clients; + NetworkClient::ClientEventFunction messageCallback; + std::mutex clientListLock; + NetworkSession* owner; //Where clients end up when session is closed. + int clientCount; + int id; + NetworkSession::PrivateSessionData() + : clientCount(0) + , owner(0) + , id(GID()) + {} +}; + + + +NetworkSession::NetworkSession() + : data(0) +{} +NetworkSession::NetworkSession(const NetworkSession& orig) +{ + this->data->clients = orig.data->clients; + this->data->owner = orig.data->owner; + this->data->clientCount = orig.data->clientCount; + this->data->id = orig.data->id; + this->data->messageCallback = orig.data->messageCallback; +} + +const NetworkSession& NetworkSession::operator=(const NetworkSession& orig) +{ + this->data->clients = orig.data->clients; + this->data->owner = orig.data->owner; + this->data->clientCount = orig.data->clientCount; + this->data->id = orig.data->id; + this->data->messageCallback = orig.data->messageCallback; + + return *this; +} + +NetworkSession::~NetworkSession() +{ + this->data->clients.Clear(); + this->data->clientCount = 0; + this->data->messageCallback = 0; +} + +void NetworkSession::ProcessClients() +{ + for (unsigned int i = 0; i < this->data->clients.Size(); i++) + { + this->data->clients[i].ProcessMessages(); + } +} + +bool NetworkSession::Attach(NetworkClient 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.. + k = i; + } + + if(k == -1) + { + this->data->clients.Push(client); + } + else + { + this->data->clients[k] = client; + } + + this->data->clientCount++; + + this->data->clientListLock.unlock(); + + return true; +} + +NetworkClient NetworkSession::Detach(const NetworkClient& client) +{ + NetworkClient val; + + this->data->clientListLock.lock(); + + for (unsigned int i = 0; i < this->data->clients.Size(); i++) + { + if(this->data->clients[0].GetID() == client.GetID()) + { + val = this->data->clients[i]; + this->data->clients[i] = NetworkClient(); + this->data->clientCount--; + } + } + + this->data->clientListLock.unlock(); + + return val; +} + +NetworkClient NetworkSession::Detach(short ID) +{ + NetworkClient val; + + this->data->clientListLock.lock(); + + for (unsigned int i = 0; i < this->data->clients.Size(); i++) + { + if(this->data->clients[0].GetID() == ID) + { + val = this->data->clients[i]; + this->data->clients[i] = NetworkClient(); + this->data->clientCount--; + } + } + + this->data->clientListLock.unlock(); + + return val; +} + +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; + } + + return returnValue; +} + +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) + { + this->data->clients[i].Send(&protocol); + return true; + } + } + return false; +} + +void NetworkSession::CloseSession(bool dissconnectClients) +{ + this->data->clientListLock.lock(); + + 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(); + } + + this->data->clients.Clear(); + this->data->clientCount = 0; + + this->data->clientListLock.unlock(); +} + + + diff --git a/Code/Network/NetworkAPI/NetworkSession.h b/Code/Network/NetworkAPI/NetworkSession.h new file mode 100644 index 00000000..7c50afd0 --- /dev/null +++ b/Code/Network/NetworkAPI/NetworkSession.h @@ -0,0 +1,83 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// +#ifndef NETWORK_API_NETWORK_SESSION_H +#define NETWORK_API_NETWORK_SESSION_H + +//warning C4150: deletion of pointer to incomplete type, no destructor called +#pragma warning(disable : 4150) + +#include "NetworkAPI_Preprocessor.h" +#include "NetworkServerEventStruct.h" +#include "NetworkClient.h" + +namespace Oyster +{ + namespace Network + { + class NET_API_EXPORT NetworkSession + { + public: + NetworkSession(); + NetworkSession(const NetworkSession& orig); + const NetworkSession& operator=(const NetworkSession& orig); + virtual~NetworkSession(); + + /** Parse session events such as protocols recieved etc. + */ + void ProcessClients(); + + /** + * + */ + bool Attach(NetworkClient client); + + /** + * + */ + NetworkClient Detach(const NetworkClient& client); + + /** + * + */ + NetworkClient Detach(short ID); + + /** Send a message to all clients in this session + * @param message The message + */ + 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); + + /** + * + */ + 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 ClientEventCallback(NetEvent e) = 0; + + /** + * + */ + virtual void ClientConnectedEvent(NetEvent e) = 0; + + private: + struct PrivateSessionData; + PrivateSessionData* data; + }; + } +}//End namespace DanBias + +#endif // !DANBIASSERVER_NETWORK_SESSION_H diff --git a/Code/Network/NetworkAPI/Translator.cpp b/Code/Network/NetworkAPI/Translator.cpp index 0829913e..b1196e88 100644 --- a/Code/Network/NetworkAPI/Translator.cpp +++ b/Code/Network/NetworkAPI/Translator.cpp @@ -5,6 +5,7 @@ #include "CustomNetProtocol.h" +#include "../../Misc/Utilities.h" #include "../NetworkDependencies/Messages/MessageHeader.h" #include "../NetworkDependencies/OysterByte.h" diff --git a/Code/Network/NetworkAPI/Translator.h b/Code/Network/NetworkAPI/Translator.h index f413492e..7913c16a 100644 --- a/Code/Network/NetworkAPI/Translator.h +++ b/Code/Network/NetworkAPI/Translator.h @@ -27,13 +27,7 @@ "100F" */ -#ifdef CUSTOM_NET_PROTOCOL_EXPORT - #define NET_PROTOCOL_EXPORT __declspec(dllexport) -#else - #define NET_PROTOCOL_EXPORT __declspec(dllimport) -#endif - -#include "../../Misc/Utilities.h" +#include "NetworkAPI_Preprocessor.h" namespace Oyster { @@ -43,7 +37,8 @@ namespace Oyster { class OysterByte; class CustomNetProtocol; - class NET_PROTOCOL_EXPORT Translator + + class NET_API_EXPORT Translator { public: Translator (); diff --git a/Code/Network/NetworkDependencies/Connection.cpp b/Code/Network/NetworkDependencies/Connection.cpp index 5ff4eeda..c79d5fe6 100644 --- a/Code/Network/NetworkDependencies/Connection.cpp +++ b/Code/Network/NetworkDependencies/Connection.cpp @@ -40,7 +40,18 @@ Connection::~Connection() CloseSocket( this->socket ); } -int Connection::Connect(unsigned short port , const char serverName[]) +int Connection::Connect(int socket, bool blocking) +{ + this->socket = socket; + this->stillSending = true; + this->closed = false; + + SetBlockingMode(blocking); + //connection succesfull! + return 0; +} + +int Connection::Connect(unsigned short port , const char serverName[], bool blocking) { struct hostent *hostEnt; if((hostEnt = gethostbyname(serverName)) == NULL) @@ -61,6 +72,8 @@ int Connection::Connect(unsigned short port , const char serverName[]) closed = false; stillSending = true; + SetBlockingMode(blocking); + //connection succesfull! return 0; } diff --git a/Code/Network/NetworkDependencies/Connection.h b/Code/Network/NetworkDependencies/Connection.h index ae76a3f7..d2e815d3 100644 --- a/Code/Network/NetworkDependencies/Connection.h +++ b/Code/Network/NetworkDependencies/Connection.h @@ -27,7 +27,8 @@ namespace Oyster virtual int Recieve( OysterByte &bytes ); virtual int Disconnect(); - virtual int Connect( unsigned short port , const char serverName[] ); + virtual int Connect(int socket, bool blocking = false); + virtual int Connect(unsigned short port , const char serverName[], bool blocking = false); virtual int Listen(); diff --git a/Code/Network/NetworkDependencies/Listener.cpp b/Code/Network/NetworkDependencies/Listener.cpp index deb0d992..360ccebb 100644 --- a/Code/Network/NetworkDependencies/Listener.cpp +++ b/Code/Network/NetworkDependencies/Listener.cpp @@ -120,7 +120,7 @@ void Listener::StopListen() this->isListening = false; Connection c; c.InitiateClient(); - c.Connect(this->port, "127.0.0.1"); + c.Connect(this->port, "127.0.0.1", false); } } bool Listener::DoWork() From 4de1c1aafd438efcd3080de77a7ebd9e5d28ee71 Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Tue, 28 Jan 2014 11:27:29 +0100 Subject: [PATCH 10/23] GL - More Object structs, LevelLoader api comments. Changed the LevelParser to use the new functions from ParserFunctions. Might need to cast something to (void*). --- Code/Game/GameLogic/LevelLoader.cpp | 2 +- Code/Game/GameLogic/LevelLoader.h | 16 ++++- Code/Game/GameLogic/LevelParser.cpp | 97 +++++++++++++++++------------ Code/Game/GameLogic/LevelParser.h | 25 +------- Code/Game/GameLogic/ObjectDefines.h | 78 +++++++++++++++++++++-- 5 files changed, 148 insertions(+), 70 deletions(-) diff --git a/Code/Game/GameLogic/LevelLoader.cpp b/Code/Game/GameLogic/LevelLoader.cpp index 3827251a..a6f8bb98 100644 --- a/Code/Game/GameLogic/LevelLoader.cpp +++ b/Code/Game/GameLogic/LevelLoader.cpp @@ -12,7 +12,7 @@ std::vector LevelLoader::LoadLevel(std::string fileName) return parser->Parse(fileName); } -ObjectTypeHeader LevelLoader::LoadLevelHeader(std::string fileName) +LevelMetaData LevelLoader::LoadLevelHeader(std::string fileName) { return parser->ParseHeader(fileName); } \ No newline at end of file diff --git a/Code/Game/GameLogic/LevelLoader.h b/Code/Game/GameLogic/LevelLoader.h index ed7aab4d..8f87d141 100644 --- a/Code/Game/GameLogic/LevelLoader.h +++ b/Code/Game/GameLogic/LevelLoader.h @@ -19,8 +19,20 @@ namespace GameLogic public: LevelLoader(){this->parser = new GameLogic::LevelFileLoader::LevelParser(); } ~LevelLoader(){} - std::vector LoadLevel(std::string fileName); //loads the level and objects from file - ObjectTypeHeader LoadLevelHeader(std::string fileName); //just for fast access for the meta information about the level. + + /******************************************************** + * Loads the level and objects from file. + * @param fileName: Path to the level-file that you want to load. + * @return: Returns all structs with objects and information about the level. + ********************************************************/ + std::vector LoadLevel(std::string fileName); + + /******************************************************** + * Just for fast access for the meta information about the level. + * @param fileName: Path to the level-file that you want to load. + * @return: Returns the meta information about the level. + ********************************************************/ + LevelMetaData LoadLevelHeader(std::string fileName); //. private: GameLogic::LevelFileLoader::LevelParser *parser; diff --git a/Code/Game/GameLogic/LevelParser.cpp b/Code/Game/GameLogic/LevelParser.cpp index a957b88a..acc52c69 100644 --- a/Code/Game/GameLogic/LevelParser.cpp +++ b/Code/Game/GameLogic/LevelParser.cpp @@ -6,9 +6,10 @@ using namespace GameLogic; using namespace ::LevelFileLoader; - LevelParser::LevelParser() { + formatVersion.formatVersionMajor = 1; + formatVersion.formatVersionMinor = 0; } LevelParser::~LevelParser() @@ -18,35 +19,43 @@ LevelParser::~LevelParser() // std::vector LevelParser::Parse(std::string filename) { - int stringSize = 0; - //Read entire level file. - Loader loader; - unsigned char* buffer = (unsigned char*)loader.LoadFile(filename.c_str(), stringSize); + int bufferSize = 0; + unsigned int counter = 0; std::vector objects; - unsigned int counter = 0; - - while(counter < stringSize) + //Read entire level file. + Loader loader; + unsigned char* buffer = (unsigned char*)loader.LoadFile(filename.c_str(), bufferSize); + + //Read format version + FormatVersion levelFormatVersion; + ParseObject(&buffer[counter], formatVersion, sizeof(formatVersion)); + if(this->formatVersion != levelFormatVersion) + { + //Do something if it's not the same version + } + + while(counter < bufferSize) { //Get typeID ObjectTypeHeader typeID; - typeID = ParseObjectTypeHeader(&buffer[counter]); - //counter += 4; - //Unpack ID + ParseObject(&buffer[counter], typeID, sizeof(typeID)); switch((int)typeID.typeID) { - case TypeID_LevelHeader: - //Call function - counter += LevelHeaderSize; + case ObjectType_LevelMetaData: + LevelMetaData header; + //ParseObject(&buffer[counter], header, sizeof(header)); + objects.push_back(header); + counter += sizeof(header); break; - case TypeID_Object: - //Call function - objects.push_back(ParseObjectHeader(&buffer[counter])); - counter += sizeof(ObjectHeader); - break; + case ObjectType_Dynamic: + ObjectHeader header; + ParseObject(&buffer[counter], header, sizeof(header)); + objects.push_back(header); + counter += sizeof(header); default: //Couldn't find typeID. FAIL!!!!!! break; @@ -56,36 +65,44 @@ std::vector LevelParser::Parse(std::string filename) return objects; } -//för meta information om leveln. Måste göra så den returnerar rätt strukt så fort vi -//vi definierat en! -ObjectTypeHeader LevelParser::ParseHeader(std::string filename) +//för meta information om leveln. +LevelMetaData LevelParser::ParseHeader(std::string filename) { - int stringSize = 0; + int bufferSize = 0; + unsigned int counter = 0; + + LevelMetaData levelHeader; + levelHeader.typeID = ObjectType::ObjectType_Unknown; + //Read entire level file. Loader loader; - unsigned char* buffer = (unsigned char*)loader.LoadFile(filename.c_str(), stringSize); + unsigned char* buffer = (unsigned char*)loader.LoadFile(filename.c_str(), bufferSize); + + //Read format version + FormatVersion levelFormatVersion; + ParseObject(&buffer[counter], formatVersion, sizeof(formatVersion)); + if(this->formatVersion != levelFormatVersion) + { + //Do something if it's not the same version + } //Find the header in the returned string. - unsigned int counter = 0; - - - ObjectTypeHeader header; - header.typeID = ObjectType_Level; - - while(counter < stringSize) + while(counter < bufferSize) { ObjectTypeHeader typeID; - typeID = ParseObjectTypeHeader(buffer); + ParseObject(&buffer[counter], typeID, sizeof(typeID)); + switch(typeID.typeID) { - case TypeID_LevelHeader: - //Call function - - counter += LevelHeaderSize; + case ObjectType_LevelMetaData: + //ParseObject(&buffer[counter], levelHeader, sizeof(levelHeader)); + return levelHeader; + counter += sizeof(LevelMetaData); break; - case TypeID_Object: - //Call function - counter += ObjectHeaderSize; + case ObjectType_Dynamic: + //Do not call parse this object, since we are only interested in the LevelMetaData + //Only increase the counter size + counter += sizeof(ObjectHeader); break; default: @@ -94,5 +111,5 @@ ObjectTypeHeader LevelParser::ParseHeader(std::string filename) } } - return header; + return levelHeader; } \ No newline at end of file diff --git a/Code/Game/GameLogic/LevelParser.h b/Code/Game/GameLogic/LevelParser.h index bcb9cb31..6dce25f8 100644 --- a/Code/Game/GameLogic/LevelParser.h +++ b/Code/Game/GameLogic/LevelParser.h @@ -19,31 +19,10 @@ namespace GameLogic std::vector Parse(std::string filename); // - ObjectTypeHeader ParseHeader(std::string filename); + LevelMetaData ParseHeader(std::string filename); private: - static const int TypeHeaderSize = 4; //Including the TypeID - static const int ObjectHeaderSize = 10; //Including modelID, textureID, position, rotation, scale. - static const int LevelHeaderSize = 10; //Including Format version, Name, Map version etc. - - static const int FormatVersionMajor = 1; - static const int FormatVersionMinor = 0; - - /************************************* - Question - *************************************/ - //Could we use the IDs in "ObjectDefines.h" or do we have to use our own??? - enum TypeID - { - TypeID_LevelHeader, - TypeID_Player, - TypeID_Object, - - - TypeID_NUM_OF_TYPES, - - ObjectType_Unknown = -1, - }; + FormatVersion formatVersion; }; } diff --git a/Code/Game/GameLogic/ObjectDefines.h b/Code/Game/GameLogic/ObjectDefines.h index 7f2307d1..2d3e09f0 100644 --- a/Code/Game/GameLogic/ObjectDefines.h +++ b/Code/Game/GameLogic/ObjectDefines.h @@ -1,27 +1,97 @@ #ifndef OBJECT_DEFINES_H #define OBJECT_DEFINES_H +#include +#include + namespace GameLogic { + /************************************ + Enums + *************************************/ + enum ObjectType { - ObjectType_Level, + ObjectType_LevelMetaData, ObjectType_Static, ObjectType_Dynamic, - + //Etc ObjectType_NUM_OF_TYPES, ObjectType_Unknown = -1, }; + enum UsePhysics + { + UsePhysics_UseFullPhysics, + UsePhysics_IgnoreGravity, + UsePhysics_IgnorePhysics, + + UsePhysics_Count, + UsePhysics_Unknown = -1, + }; + + //Should this be moved somewhere else? + enum GameMode + { + GameMode_FreeForAll, + GameMode_TeamDeathMatch, + //Etc + + GameMode_Count, + GameMode_Unknown = -1, + }; + + + /************************************ + Structs + *************************************/ + + struct FormatVersion + { + int formatVersionMajor; + int formatVersionMinor; + + bool operator ==(const FormatVersion& obj) + { + return (this->formatVersionMajor != obj.formatVersionMajor && this->formatVersionMinor != obj.formatVersionMinor); + } + + bool operator !=(const FormatVersion& obj) + { + return !(*this == obj); + } + }; + struct ObjectTypeHeader { ObjectType typeID; - }; - struct ObjectHeader : public ObjectTypeHeader + struct PhysicsObject + { + float mass; + float elasticity; + float frictionCoeffStatic; + float frictionCoeffDynamic; + float inertiaTensor[16]; + UsePhysics usePhysics; + }; + + struct LevelMetaData : ObjectTypeHeader + { + std::string levelName; + FormatVersion levelVersion; + std::string levelDescription; + std::string levelAuthor; + int maxNumberOfPlayer; + float worldSize; + int overviewPictureID; + std::vector gameModesSupported; + }; + + struct ObjectHeader : public PhysicsObject, public ObjectTypeHeader { //Model, int ModelID; From d2ebad74452c02865ada3104056bc6959c3dde00 Mon Sep 17 00:00:00 2001 From: Sam Mario Svensson Date: Tue, 28 Jan 2014 11:29:35 +0100 Subject: [PATCH 11/23] GL - Fixed ParseFunction so now we only have 1 + specialcases functions instead of 1 function for every case --- Code/Dokumentation/LevelLoader.uxf | 350 +++++++++--------- Code/Game/GameLogic/GameLogic.vcxproj | 18 +- .../{ => LevelLoader}/LevelLoader.cpp | 0 .../GameLogic/{ => LevelLoader}/LevelLoader.h | 0 .../{ => LevelLoader}/LevelParser.cpp | 1 - .../GameLogic/{ => LevelLoader}/LevelParser.h | 0 .../GameLogic/{ => LevelLoader}/Loader.cpp | 2 + .../Game/GameLogic/{ => LevelLoader}/Loader.h | 2 +- .../{ => LevelLoader}/ObjectDefines.h | 0 .../GameLogic/LevelLoader/ParseFunctions.cpp | 55 +++ .../GameLogic/LevelLoader/ParseFunctions.h | 19 + Code/Game/GameLogic/ParseFunctions.cpp | 33 -- Code/Game/GameLogic/ParseFunctions.h | 19 - 13 files changed, 261 insertions(+), 238 deletions(-) rename Code/Game/GameLogic/{ => LevelLoader}/LevelLoader.cpp (100%) rename Code/Game/GameLogic/{ => LevelLoader}/LevelLoader.h (100%) rename Code/Game/GameLogic/{ => LevelLoader}/LevelParser.cpp (99%) rename Code/Game/GameLogic/{ => LevelLoader}/LevelParser.h (100%) rename Code/Game/GameLogic/{ => LevelLoader}/Loader.cpp (94%) rename Code/Game/GameLogic/{ => LevelLoader}/Loader.h (88%) rename Code/Game/GameLogic/{ => LevelLoader}/ObjectDefines.h (100%) create mode 100644 Code/Game/GameLogic/LevelLoader/ParseFunctions.cpp create mode 100644 Code/Game/GameLogic/LevelLoader/ParseFunctions.h delete mode 100644 Code/Game/GameLogic/ParseFunctions.cpp delete mode 100644 Code/Game/GameLogic/ParseFunctions.h diff --git a/Code/Dokumentation/LevelLoader.uxf b/Code/Dokumentation/LevelLoader.uxf index 90dcb0f7..b5cef8f0 100644 --- a/Code/Dokumentation/LevelLoader.uxf +++ b/Code/Dokumentation/LevelLoader.uxf @@ -4,8 +4,20 @@ com.umlet.element.Relation - 640 - 256 + 632 + 232 + 88 + 176 + + lt=. +<Uses + 24;24;24;64;72;64;72;160 + + + com.umlet.element.Relation + + 136 + 232 40 168 @@ -15,20 +27,111 @@ com.umlet.element.Relation - 840 - 256 - 88 + 448 + 496 + 40 + 88 + + lt=<<<<- + 24;72;24;24 + + + com.umlet.element.Relation + + 432 + 232 + 40 168 - lt=. -<Uses - 24;24;24;64;72;64;72;152 + lt=<<. + 24;24;24;152 com.umlet.element.Class + + 608 + 216 + 128 + 40 + + Resource Loader +<<Dennis>><<Singleton> + + + + com.umlet.element.Class + + 232 + 112 + 128 + 40 + + GameLogic +<<Erik>> + + + + com.umlet.element.Class + + 120 + 232 + 80 + 24 + + Defines + + + + com.umlet.element.Class + + 352 + 568 + 232 + 136 + + <<Interface>> +LevelParser +-- +Functions: +vector<struct> Parse(); +- +Privates: +enum headerType; +const int FileHeaderSize; +const int FileVersion; + + + + + com.umlet.element.Relation + + 336 + 88 + 136 + 160 + + lt=lt=->>>> +m1=1..1 +m2=1..1 +Uses> + 24;40;80;40;120;40;120;144 + + + com.umlet.element.Relation 560 - 408 + 496 + 136 + 104 + + lt=<<<<- + 120;24;120;88;24;88 + + + com.umlet.element.Class + + 352 + 384 232 136 @@ -45,54 +148,79 @@ Privates: - com.umlet.element.Class + com.umlet.element.Relation - 440 - 136 - 128 - 40 + 560 + 600 + 176 + 56 - GameLogic -<<Erik>> - + lt=- +m1=1..1 +m2=1..1 +Uses> + 24;40;160;40 com.umlet.element.Package - 552 - 368 + 344 + 344 584 368 LevelLoader + + com.umlet.element.Relation + + 472 + 200 + 152 + 56 + + lt=- +m1=1..1 +m2=1..1 +Uses> + 24;40;136;40 + com.umlet.element.Class - 560 - 592 - 232 - 136 + 416 + 232 + 80 + 24 - <<Interface>> -Parser --- -Functions: -vector<struct> Parse(); -- -Privates: -enum headerType; -const int FileHeaderSize; -const int FileVersion; + LevelLoader com.umlet.element.Class - 928 - 608 + 48 + 384 + 232 + 104 + + ObjectDefines.h +<<Header file>> +-- +Enum ObjectType(static, dynamic, specials); +. +Struct static; +Struct dynamic; +Struct specials + + + + com.umlet.element.Class + + 720 + 584 200 120 @@ -103,23 +231,23 @@ functions for creating the right structs - com.umlet.element.Relation + com.umlet.element.Package - 768 - 520 - 136 - 104 + 40 + 344 + 248 + 160 - lt=<<<<- - 120;24;120;88;24;88 + Defines + com.umlet.element.Class - 800 - 408 + 592 + 392 208 - 136 + 128 <<Interface>> Loader @@ -135,67 +263,8 @@ Privates: com.umlet.element.Relation - 656 - 520 - 40 - 88 - - lt=<<<<- - 24;72;24;24 - - - com.umlet.element.Class - - 328 - 256 - 80 - 24 - - Defines - - - - com.umlet.element.Relation - - 680 - 224 - 152 - 56 - - lt=- -m1=1..1 -m2=1..1 -Uses> - 24;40;136;40 - - - com.umlet.element.Class - - 624 - 256 - 80 - 24 - - LevelLoader - - - - - com.umlet.element.Relation - - 344 - 256 - 40 - 168 - - lt=<<. - 24;24;24;152 - - - com.umlet.element.Relation - - 384 - 224 + 176 + 200 256 56 @@ -205,73 +274,4 @@ m2=1..1 <Knows about 240;40;24;40 - - com.umlet.element.Relation - - 544 - 112 - 136 - 160 - - lt=lt=->>>> -m1=1..1 -m2=1..1 -Uses> - 24;40;80;40;120;40;120;144 - - - com.umlet.element.Relation - - 768 - 624 - 176 - 56 - - lt=- -m1=1..1 -m2=1..1 -Uses> - 24;40;160;40 - - - com.umlet.element.Package - - 248 - 368 - 248 - 160 - - Defines - - - - com.umlet.element.Class - - 256 - 408 - 232 - 104 - - Defines.h -<<Header file>> --- -Enum ObjectType(static, dynamic, specials); -. -Struct static; -Struct dynamic; -Struct specials - - - - com.umlet.element.Class - - 816 - 240 - 128 - 40 - - Resource Loader -<<Dennis>><<Singleton> - - diff --git a/Code/Game/GameLogic/GameLogic.vcxproj b/Code/Game/GameLogic/GameLogic.vcxproj index df1d09aa..809c82b5 100644 --- a/Code/Game/GameLogic/GameLogic.vcxproj +++ b/Code/Game/GameLogic/GameLogic.vcxproj @@ -184,12 +184,12 @@ - - + + - - - + + + @@ -207,11 +207,11 @@ - - - + + + - + diff --git a/Code/Game/GameLogic/LevelLoader.cpp b/Code/Game/GameLogic/LevelLoader/LevelLoader.cpp similarity index 100% rename from Code/Game/GameLogic/LevelLoader.cpp rename to Code/Game/GameLogic/LevelLoader/LevelLoader.cpp diff --git a/Code/Game/GameLogic/LevelLoader.h b/Code/Game/GameLogic/LevelLoader/LevelLoader.h similarity index 100% rename from Code/Game/GameLogic/LevelLoader.h rename to Code/Game/GameLogic/LevelLoader/LevelLoader.h diff --git a/Code/Game/GameLogic/LevelParser.cpp b/Code/Game/GameLogic/LevelLoader/LevelParser.cpp similarity index 99% rename from Code/Game/GameLogic/LevelParser.cpp rename to Code/Game/GameLogic/LevelLoader/LevelParser.cpp index a957b88a..08c4da7e 100644 --- a/Code/Game/GameLogic/LevelParser.cpp +++ b/Code/Game/GameLogic/LevelLoader/LevelParser.cpp @@ -15,7 +15,6 @@ LevelParser::~LevelParser() { } -// std::vector LevelParser::Parse(std::string filename) { int stringSize = 0; diff --git a/Code/Game/GameLogic/LevelParser.h b/Code/Game/GameLogic/LevelLoader/LevelParser.h similarity index 100% rename from Code/Game/GameLogic/LevelParser.h rename to Code/Game/GameLogic/LevelLoader/LevelParser.h diff --git a/Code/Game/GameLogic/Loader.cpp b/Code/Game/GameLogic/LevelLoader/Loader.cpp similarity index 94% rename from Code/Game/GameLogic/Loader.cpp rename to Code/Game/GameLogic/LevelLoader/Loader.cpp index 0afb48de..26818202 100644 --- a/Code/Game/GameLogic/Loader.cpp +++ b/Code/Game/GameLogic/LevelLoader/Loader.cpp @@ -15,6 +15,8 @@ unsigned char* Loader::LoadFile(std::string fileName, int &size) //convert from wstring to wchar then loads the file unsigned char* buffer = (unsigned char*)OysterResource::LoadResource(temp.c_str(), Oyster::Resource::ResourceType::ResourceType_Byte_Raw, -1 , false); + + //gets the size of the char buffer. size = OysterResource::GetResourceSize(buffer); return buffer; } \ No newline at end of file diff --git a/Code/Game/GameLogic/Loader.h b/Code/Game/GameLogic/LevelLoader/Loader.h similarity index 88% rename from Code/Game/GameLogic/Loader.h rename to Code/Game/GameLogic/LevelLoader/Loader.h index d77921c5..2d605169 100644 --- a/Code/Game/GameLogic/Loader.h +++ b/Code/Game/GameLogic/LevelLoader/Loader.h @@ -5,7 +5,7 @@ #ifndef LOADER_H #define LOADER_H -#include "Resource\OysterResource.h" +#include "..\Misc\Resource\OysterResource.h" #include namespace GameLogic diff --git a/Code/Game/GameLogic/ObjectDefines.h b/Code/Game/GameLogic/LevelLoader/ObjectDefines.h similarity index 100% rename from Code/Game/GameLogic/ObjectDefines.h rename to Code/Game/GameLogic/LevelLoader/ObjectDefines.h diff --git a/Code/Game/GameLogic/LevelLoader/ParseFunctions.cpp b/Code/Game/GameLogic/LevelLoader/ParseFunctions.cpp new file mode 100644 index 00000000..abca143c --- /dev/null +++ b/Code/Game/GameLogic/LevelLoader/ParseFunctions.cpp @@ -0,0 +1,55 @@ +////////////////////////////////// +// Created by Sam Svensson 2013 // +////////////////////////////////// + +#include "ParseFunctions.h" +#include "../../../Misc/Packing/Packing.h" +#include + +using namespace Oyster::Packing; +using namespace GameLogic::LevelFileLoader; +using namespace GameLogic; +using namespace std; + +namespace GameLogic +{ + namespace LevelFileLoader + { + void ParseObject(unsigned char* buffer, void* header, int size) + { + memcpy(header, buffer, size); + } + + void ParseLevelMetaData(unsigned char* buffer, struct LevelMetaData &header) + { + int start = 0; + int tempSize; + memcpy(&header.typeID, &buffer[start], 4); + start += 4; + memcpy(&tempSize , &buffer[start], 4); + start += 4; + memcpy(&header.levelName, &buffer[start], tempSize); + start += tempSize; + memcpy(&header.levelVersion, &buffer[start], 8) + start += 8; + memcpy(&tempSize, &buffer[start], 4); + start +=4; + memcpy(&header.description, &buffer[start], tempSize); + start += tempSize; + memcpy(&tempSize, &buffer[start], 4); + start += 4; + memcpy(&header.author, &buffer[start], tempSize); + start += tempSize; + memcpy(&header.nrOfPlayers, &buffer[start], 4); + start += 4; + memcpy(&header.worldSize, &buffer[start], 4); + start += 4; + memcpy(&header.map, &buffer[start], 4); + start += 4; + memcpy(&tempSize, &buffer[start], 4); + start += 4; + memcpy(&header.gameModes, &buffer[start], 4 * tempSize); + start += tempSize; + } + } +} \ No newline at end of file diff --git a/Code/Game/GameLogic/LevelLoader/ParseFunctions.h b/Code/Game/GameLogic/LevelLoader/ParseFunctions.h new file mode 100644 index 00000000..7a6ab93a --- /dev/null +++ b/Code/Game/GameLogic/LevelLoader/ParseFunctions.h @@ -0,0 +1,19 @@ +////////////////////////////////// +// Created by Sam Svensson 2013 // +////////////////////////////////// + +#ifndef PARSERFUNCTIONS_H +#define PARSERFUNCTIONS_H +#include "ObjectDefines.h" + +namespace GameLogic +{ + namespace LevelFileLoader + { + void ParseObject(unsigned char* buffer, void* header, int size); + void ParseLevelMetaData(unsigned char* buffer, struct ObjectTypeHeader &header); + } +} + + +#endif \ No newline at end of file diff --git a/Code/Game/GameLogic/ParseFunctions.cpp b/Code/Game/GameLogic/ParseFunctions.cpp deleted file mode 100644 index 64405ff5..00000000 --- a/Code/Game/GameLogic/ParseFunctions.cpp +++ /dev/null @@ -1,33 +0,0 @@ -////////////////////////////////// -// Created by Sam Svensson 2013 // -////////////////////////////////// - -#include "ParseFunctions.h" -#include "../../Misc/Packing/Packing.h" -#include - -using namespace Oyster::Packing; -using namespace GameLogic::LevelFileLoader; -using namespace GameLogic; -using namespace std; - -namespace GameLogic -{ - namespace LevelFileLoader - { - ObjectTypeHeader ParseObjectTypeHeader(unsigned char* buffer) - { - struct ObjectTypeHeader header; - memcpy(&header, buffer, sizeof(ObjectTypeHeader)); - return header; - } - - ObjectHeader ParseObjectHeader (unsigned char* buffer) - { - struct ObjectHeader header; - memcpy(&header, buffer, sizeof(ObjectHeader)); - - return header; - } - } -} \ No newline at end of file diff --git a/Code/Game/GameLogic/ParseFunctions.h b/Code/Game/GameLogic/ParseFunctions.h deleted file mode 100644 index 5638f419..00000000 --- a/Code/Game/GameLogic/ParseFunctions.h +++ /dev/null @@ -1,19 +0,0 @@ -////////////////////////////////// -// Created by Sam Svensson 2013 // -////////////////////////////////// - -#ifndef PARSERFUNCTIONS_H -#define PARSERFUNCTIONS_H -#include "ObjectDefines.h" - -namespace GameLogic -{ - namespace LevelFileLoader - { - ObjectTypeHeader ParseObjectTypeHeader(unsigned char* buffer); //send in a char buffer, this function will seperate it and then return the right struct with the values. - ObjectHeader ParseObjectHeader (unsigned char* buffer); //send in a char buffer, this function will seperate it and then return the right struct with the values. - } -} - - -#endif \ No newline at end of file From f968adebebdede3eb1f49c082c5fd2ece2608f5b Mon Sep 17 00:00:00 2001 From: Sam Mario Svensson Date: Tue, 28 Jan 2014 16:15:10 +0100 Subject: [PATCH 12/23] GL - LevelLoader = done and done --- Bin/Level.txt | Bin 0 -> 156 bytes Bin/map | Bin 0 -> 156 bytes Bin/map.txt | Bin 48 -> 60 bytes .../GameLogic/LevelLoader/LevelLoader.cpp | 4 +- Code/Game/GameLogic/LevelLoader/LevelLoader.h | 4 +- .../GameLogic/LevelLoader/LevelParser.cpp | 39 ++++++++------- Code/Game/GameLogic/LevelLoader/Loader.cpp | 16 ++++++- Code/Game/GameLogic/LevelLoader/Loader.h | 2 +- .../GameLogic/LevelLoader/ObjectDefines.h | 2 +- .../GameLogic/LevelLoader/ParseFunctions.cpp | 45 ++++++++++++++---- .../GameLogic/LevelLoader/ParseFunctions.h | 4 +- 11 files changed, 78 insertions(+), 38 deletions(-) create mode 100644 Bin/Level.txt create mode 100644 Bin/map diff --git a/Bin/Level.txt b/Bin/Level.txt new file mode 100644 index 0000000000000000000000000000000000000000..d86dd3db4f89e0c84adfa01d7b0ec5a42dd61adb GIT binary patch literal 156 zcmZQzU|?VYV!y=PR7M~T0wBIiYH>0$&A`y$pam3X0x19jkXjHMgqeXjYRNaJ8F^XG hjYrow<6?ux*}DpzTmbMiDYF0o literal 0 HcmV?d00001 diff --git a/Bin/map b/Bin/map new file mode 100644 index 0000000000000000000000000000000000000000..a578cc3216cf4c730d47a3cae791ddccf2b29610 GIT binary patch literal 156 zcmZQ#U|?VZVrC$YTJp_lMqZY47AJ$m6N^*QIDs?+LxY1BkYob085kIWGyuTm1-1YH literal 48 gcmZQzU|?imU|<4bW~XmUqMWnxW;m}o+K7t{08$7NVE_OC diff --git a/Code/Game/GameLogic/LevelLoader/LevelLoader.cpp b/Code/Game/GameLogic/LevelLoader/LevelLoader.cpp index a6f8bb98..d60aac66 100644 --- a/Code/Game/GameLogic/LevelLoader/LevelLoader.cpp +++ b/Code/Game/GameLogic/LevelLoader/LevelLoader.cpp @@ -9,10 +9,10 @@ using namespace GameLogic::LevelFileLoader; std::vector LevelLoader::LoadLevel(std::string fileName) { - return parser->Parse(fileName); + return parser.Parse(fileName); } LevelMetaData LevelLoader::LoadLevelHeader(std::string fileName) { - return parser->ParseHeader(fileName); + return parser.ParseHeader(fileName); } \ No newline at end of file diff --git a/Code/Game/GameLogic/LevelLoader/LevelLoader.h b/Code/Game/GameLogic/LevelLoader/LevelLoader.h index 8f87d141..4ac7a950 100644 --- a/Code/Game/GameLogic/LevelLoader/LevelLoader.h +++ b/Code/Game/GameLogic/LevelLoader/LevelLoader.h @@ -17,7 +17,7 @@ namespace GameLogic { public: - LevelLoader(){this->parser = new GameLogic::LevelFileLoader::LevelParser(); } + LevelLoader(){this->parser = GameLogic::LevelFileLoader::LevelParser(); } ~LevelLoader(){} /******************************************************** @@ -35,7 +35,7 @@ namespace GameLogic LevelMetaData LoadLevelHeader(std::string fileName); //. private: - GameLogic::LevelFileLoader::LevelParser *parser; + GameLogic::LevelFileLoader::LevelParser parser; }; } diff --git a/Code/Game/GameLogic/LevelLoader/LevelParser.cpp b/Code/Game/GameLogic/LevelLoader/LevelParser.cpp index 72d68d97..3ab3c203 100644 --- a/Code/Game/GameLogic/LevelLoader/LevelParser.cpp +++ b/Code/Game/GameLogic/LevelLoader/LevelParser.cpp @@ -19,17 +19,17 @@ LevelParser::~LevelParser() std::vector LevelParser::Parse(std::string filename) { int bufferSize = 0; - unsigned int counter = 0; + int counter = 0; std::vector objects; //Read entire level file. Loader loader; - unsigned char* buffer = (unsigned char*)loader.LoadFile(filename.c_str(), bufferSize); + char* buffer = (char*)loader.LoadFile(filename.c_str(), bufferSize); //Read format version FormatVersion levelFormatVersion; - ParseObject(&buffer[counter], formatVersion, sizeof(formatVersion)); + //ParseObject(&buffer[counter], &levelFormatVersion, sizeof(formatVersion)); if(this->formatVersion != levelFormatVersion) { //Do something if it's not the same version @@ -39,22 +39,26 @@ std::vector LevelParser::Parse(std::string filename) { //Get typeID ObjectTypeHeader typeID; - ParseObject(&buffer[counter], typeID, sizeof(typeID)); - + ParseObject(&buffer[counter], &typeID, sizeof(typeID)); switch((int)typeID.typeID) { case ObjectType_LevelMetaData: + { LevelMetaData header; - //ParseObject(&buffer[counter], header, sizeof(header)); + ParseLevelMetaData(&buffer[counter], header, counter); + objects.push_back(header); + break; + } + + case ObjectType_Dynamic: + { + ObjectHeader header; + ParseObject(&buffer[counter], &header, sizeof(header)); objects.push_back(header); counter += sizeof(header); break; - - case ObjectType_Dynamic: - ObjectHeader header; - ParseObject(&buffer[counter], header, sizeof(header)); - objects.push_back(header); - counter += sizeof(header); + } + default: //Couldn't find typeID. FAIL!!!!!! break; @@ -68,18 +72,18 @@ std::vector LevelParser::Parse(std::string filename) LevelMetaData LevelParser::ParseHeader(std::string filename) { int bufferSize = 0; - unsigned int counter = 0; + int counter = 0; LevelMetaData levelHeader; levelHeader.typeID = ObjectType::ObjectType_Unknown; //Read entire level file. Loader loader; - unsigned char* buffer = (unsigned char*)loader.LoadFile(filename.c_str(), bufferSize); + char* buffer = (char*)loader.LoadFile(filename.c_str(), bufferSize); //Read format version FormatVersion levelFormatVersion; - ParseObject(&buffer[counter], formatVersion, sizeof(formatVersion)); + //ParseObject(&buffer[counter], &levelFormatVersion, sizeof(formatVersion)); if(this->formatVersion != levelFormatVersion) { //Do something if it's not the same version @@ -89,14 +93,13 @@ LevelMetaData LevelParser::ParseHeader(std::string filename) while(counter < bufferSize) { ObjectTypeHeader typeID; - ParseObject(&buffer[counter], typeID, sizeof(typeID)); + ParseObject(&buffer[counter], &typeID, sizeof(typeID)); switch(typeID.typeID) { case ObjectType_LevelMetaData: - //ParseObject(&buffer[counter], levelHeader, sizeof(levelHeader)); + ParseLevelMetaData(&buffer[counter], levelHeader, counter); return levelHeader; - counter += sizeof(LevelMetaData); break; case ObjectType_Dynamic: //Do not call parse this object, since we are only interested in the LevelMetaData diff --git a/Code/Game/GameLogic/LevelLoader/Loader.cpp b/Code/Game/GameLogic/LevelLoader/Loader.cpp index 26818202..a0206d89 100644 --- a/Code/Game/GameLogic/LevelLoader/Loader.cpp +++ b/Code/Game/GameLogic/LevelLoader/Loader.cpp @@ -3,19 +3,31 @@ ////////////////////////////////// #include "Loader.h" +#include using namespace GameLogic::LevelFileLoader; using namespace Oyster::Resource; using namespace std; -unsigned char* Loader::LoadFile(std::string fileName, int &size) +char* Loader::LoadFile(std::string fileName, int &size) { //convert from string to wstring std::wstring temp(fileName.begin(), fileName.end()); //convert from wstring to wchar then loads the file - unsigned char* buffer = (unsigned char*)OysterResource::LoadResource(temp.c_str(), Oyster::Resource::ResourceType::ResourceType_Byte_Raw, -1 , false); + char* buffer = (char*)OysterResource::LoadResource(temp.c_str(), Oyster::Resource::ResourceType::ResourceType_Byte_Raw, -1 , false); + //std::ifstream f; + //f.open(fileName, std::ios::binary); + //if (!f.is_open()) + // return 0; + //f.seekg(0, std::ifstream::end); + //size = f.tellg(); + //f.seekg(0); + //char* buffer = new char[size]; + //f.read(buffer, size); + // + //f.close(); //gets the size of the char buffer. size = OysterResource::GetResourceSize(buffer); return buffer; diff --git a/Code/Game/GameLogic/LevelLoader/Loader.h b/Code/Game/GameLogic/LevelLoader/Loader.h index 2d605169..198c2a87 100644 --- a/Code/Game/GameLogic/LevelLoader/Loader.h +++ b/Code/Game/GameLogic/LevelLoader/Loader.h @@ -17,7 +17,7 @@ namespace GameLogic public: Loader (){}; ~Loader(){}; - unsigned char* LoadFile(std::string fileName, int &size); + char* LoadFile(std::string fileName, int &size); //TODO: //Add functionality to load physicsObjects (hitboxes) diff --git a/Code/Game/GameLogic/LevelLoader/ObjectDefines.h b/Code/Game/GameLogic/LevelLoader/ObjectDefines.h index 2d3e09f0..dcf960a5 100644 --- a/Code/Game/GameLogic/LevelLoader/ObjectDefines.h +++ b/Code/Game/GameLogic/LevelLoader/ObjectDefines.h @@ -91,7 +91,7 @@ namespace GameLogic std::vector gameModesSupported; }; - struct ObjectHeader : public PhysicsObject, public ObjectTypeHeader + struct ObjectHeader : public ObjectTypeHeader { //Model, int ModelID; diff --git a/Code/Game/GameLogic/LevelLoader/ParseFunctions.cpp b/Code/Game/GameLogic/LevelLoader/ParseFunctions.cpp index abca143c..f456b2d7 100644 --- a/Code/Game/GameLogic/LevelLoader/ParseFunctions.cpp +++ b/Code/Game/GameLogic/LevelLoader/ParseFunctions.cpp @@ -15,41 +15,66 @@ namespace GameLogic { namespace LevelFileLoader { - void ParseObject(unsigned char* buffer, void* header, int size) + void ParseObject(char* buffer, void *header, int size) { memcpy(header, buffer, size); } - void ParseLevelMetaData(unsigned char* buffer, struct LevelMetaData &header) + void ParseLevelMetaData(char* buffer, LevelMetaData &header, int &size) { int start = 0; int tempSize; + char tempName[100]; + memcpy(&header.typeID, &buffer[start], 4); start += 4; + memcpy(&tempSize , &buffer[start], 4); start += 4; - memcpy(&header.levelName, &buffer[start], tempSize); + + memcpy(&tempName, &buffer[start], tempSize); + header.levelName.assign(&tempName[0], &tempName[tempSize]); start += tempSize; - memcpy(&header.levelVersion, &buffer[start], 8) + + memcpy(&header.levelVersion, &buffer[start], 8); start += 8; + memcpy(&tempSize, &buffer[start], 4); start +=4; - memcpy(&header.description, &buffer[start], tempSize); + + memcpy(&tempName, &buffer[start], tempSize); + header.levelDescription.assign(&tempName[0], &tempName[tempSize]); start += tempSize; + memcpy(&tempSize, &buffer[start], 4); start += 4; - memcpy(&header.author, &buffer[start], tempSize); + + memcpy(&tempName, &buffer[start], tempSize); + header.levelAuthor.assign(&tempName[0], &tempName[tempSize]); start += tempSize; - memcpy(&header.nrOfPlayers, &buffer[start], 4); + + memcpy(&header.maxNumberOfPlayer, &buffer[start], 4); start += 4; + memcpy(&header.worldSize, &buffer[start], 4); start += 4; - memcpy(&header.map, &buffer[start], 4); + + memcpy(&header.overviewPictureID, &buffer[start], 4); start += 4; + memcpy(&tempSize, &buffer[start], 4); start += 4; - memcpy(&header.gameModes, &buffer[start], 4 * tempSize); - start += tempSize; + + int temp; + + for(int i = 0; i < tempSize; i++) + { + memcpy(&temp, &buffer[start], 4); + start += 4; + header.gameModesSupported.push_back((GameMode)temp); + } + + size += start; } } } \ No newline at end of file diff --git a/Code/Game/GameLogic/LevelLoader/ParseFunctions.h b/Code/Game/GameLogic/LevelLoader/ParseFunctions.h index 7a6ab93a..08962b4a 100644 --- a/Code/Game/GameLogic/LevelLoader/ParseFunctions.h +++ b/Code/Game/GameLogic/LevelLoader/ParseFunctions.h @@ -10,8 +10,8 @@ namespace GameLogic { namespace LevelFileLoader { - void ParseObject(unsigned char* buffer, void* header, int size); - void ParseLevelMetaData(unsigned char* buffer, struct ObjectTypeHeader &header); + void ParseObject(char* buffer, void *header, int size); + void ParseLevelMetaData(char* buffer, LevelMetaData &header, int &size); } } From 36f057d2cc1dcb18c6f520bae8509c31fd3db9f6 Mon Sep 17 00:00:00 2001 From: Dennis Andersen Date: Wed, 29 Jan 2014 10:18:01 +0100 Subject: [PATCH 13/23] Broekn branch not broekn more --- Code/DanBias.sln | 18 - Code/Dokumentation/GameServer.uxf | 407 ++++++------------ Code/Game/DanBiasGame/DanBiasGame.vcxproj | 2 + Code/Game/DanBiasGame/DanBiasGame_Impl.cpp | 19 +- .../Game/DanBiasGame/GameClientRecieverFunc.h | 5 +- .../GameClientState/C_obj/C_Player.cpp | 3 +- .../DanBiasGame/GameClientState/Camera.cpp | 192 +++++++++ .../Game/DanBiasGame/GameClientState/Camera.h | 63 +++ .../DanBiasGame/GameClientState/GameState.cpp | 220 ++++++---- .../DanBiasGame/GameClientState/GameState.h | 4 + .../DanBiasServer/LobbySessions/GameLobby.cpp | 21 - .../DanBiasServerLauncher.vcxproj | 29 +- .../DanBiasServerLauncher/ServerLauncher.cpp | 16 +- Code/Game/GameProtocols/PlayerProtocols.h | 51 ++- .../GameProtocols/ProtocolIdentificationID.h | 15 +- Code/Game/GameServer/GameClient.h | 14 +- Code/Game/GameServer/GameLobby.h | 30 +- Code/Game/GameServer/GameServer.h | 33 -- Code/Game/GameServer/GameServer.vcxproj | 23 +- Code/Game/GameServer/GameServerAPI.h | 10 +- Code/Game/GameServer/GameSession.h | 47 +- Code/Game/GameServer/GameSessionManager.h | 82 ---- .../GameServer/Implementation/GameClient.cpp | 14 +- .../GameServer/Implementation/GameLobby.cpp | 43 +- .../GameLobby_ProtocolParser.cpp | 100 +++++ .../GameServer/Implementation/GameServer.cpp | 194 ++++----- .../Implementation/GameSessionManager.cpp | 121 ------ ...on_Events.cpp => GameSession_Gameplay.cpp} | 72 ++-- .../Implementation/GameSession_General.cpp | 111 +---- .../Implementation/GameSession_Logic.cpp | 74 ---- .../Implementation/GameSession_Network.cpp | 29 -- .../LobbyGeneralProtocolParser.cpp | 52 --- .../Implementation/LobbyProtocolParser.cpp | 58 --- Code/Game/aDanBiasGameLauncher/Launcher.cpp | 11 +- .../aDanBiasGameLauncher.vcxproj | 19 +- Code/Misc/Thread/OysterThread_Impl.cpp | 20 +- Code/Misc/WinTimer.h | 1 + Code/Network/NetworkAPI/CustomNetProtocol.cpp | 63 +-- Code/Network/NetworkAPI/NetworkAPI.vcxproj | 12 +- Code/Network/NetworkAPI/NetworkClient.cpp | 123 +++--- Code/Network/NetworkAPI/NetworkClient.h | 18 +- Code/Network/NetworkAPI/NetworkServer.cpp | 106 ++++- Code/Network/NetworkAPI/NetworkServer.h | 8 +- Code/Network/NetworkAPI/NetworkSession.cpp | 89 +++- Code/Network/NetworkAPI/NetworkSession.h | 37 +- Code/Network/NetworkAPI/Translator.cpp | 1 + .../NetworkDependencies/Connection.cpp | 2 + Code/Network/NetworkDependencies/IListener.h | 27 -- Code/Network/NetworkDependencies/Listener.cpp | 2 +- Code/Network/NetworkDependencies/Listener.h | 61 ++- .../NetworkDependencies.vcxproj | 1 - .../NetworkDependencies.vcxproj.filters | 1 - Code/OysterGraphics/FileLoader/DanLoader.cpp | 17 +- 53 files changed, 1305 insertions(+), 1486 deletions(-) create mode 100644 Code/Game/DanBiasGame/GameClientState/Camera.cpp create mode 100644 Code/Game/DanBiasGame/GameClientState/Camera.h delete mode 100644 Code/Game/DanBiasServer/LobbySessions/GameLobby.cpp delete mode 100644 Code/Game/GameServer/GameServer.h delete mode 100644 Code/Game/GameServer/GameSessionManager.h create mode 100644 Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp delete mode 100644 Code/Game/GameServer/Implementation/GameSessionManager.cpp rename Code/Game/GameServer/Implementation/{GameSession_Events.cpp => GameSession_Gameplay.cpp} (57%) delete mode 100644 Code/Game/GameServer/Implementation/GameSession_Logic.cpp delete mode 100644 Code/Game/GameServer/Implementation/GameSession_Network.cpp delete mode 100644 Code/Game/GameServer/Implementation/LobbyGeneralProtocolParser.cpp delete mode 100644 Code/Game/GameServer/Implementation/LobbyProtocolParser.cpp delete mode 100644 Code/Network/NetworkDependencies/IListener.h 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(); From 1e89bb4e307727e6803ff9d9ba576ab21914847f Mon Sep 17 00:00:00 2001 From: Dennis Andersen Date: Wed, 29 Jan 2014 15:01:14 +0100 Subject: [PATCH 14/23] Fixed protocol messaging. Known crash when recieveing protocol on server after a while... --- Code/Game/GameLogic/Game.cpp | 24 --- Code/Game/GameLogic/Game.h | 2 +- Code/Game/GameLogic/GameAPI.h | 2 +- Code/Game/GameLogic/Game_PlayerData.cpp | 4 +- Code/Game/GameProtocols/GameProtocols.vcxproj | 1 - Code/Game/GameProtocols/GameplayProtocols.h | 13 -- Code/Game/GameProtocols/LobbyProtocols.h | 11 ++ Code/Game/GameProtocols/ObjectProtocols.h | 24 +++ Code/Game/GameProtocols/PlayerProtocols.h | 16 ++ Code/Game/GameProtocols/Protocols.h | 1 - Code/Game/GameServer/GameLobby.h | 8 +- Code/Game/GameServer/GameSession.h | 24 ++- .../GameServer/Implementation/GameLobby.cpp | 10 +- .../GameLobby_ProtocolParser.cpp | 33 ++-- .../GameServer/Implementation/GameServer.cpp | 5 +- .../Implementation/GameSession_Gameplay.cpp | 185 +++++++++++------- Code/Network/NetworkAPI/NetworkClient.cpp | 7 +- Code/Network/NetworkAPI/NetworkClient.h | 5 +- Code/Network/NetworkAPI/NetworkServer.cpp | 8 +- .../NetworkDependencies/Connection.cpp | 32 ++- Code/Network/NetworkDependencies/Connection.h | 9 +- .../Network/NetworkDependencies/IConnection.h | 8 +- Code/Network/NetworkDependencies/Listener.cpp | 10 +- Code/Network/NetworkDependencies/Listener.h | 6 +- 24 files changed, 278 insertions(+), 170 deletions(-) delete mode 100644 Code/Game/GameProtocols/GameplayProtocols.h diff --git a/Code/Game/GameLogic/Game.cpp b/Code/Game/GameLogic/Game.cpp index b22b3522..4380777e 100644 --- a/Code/Game/GameLogic/Game.cpp +++ b/Code/Game/GameLogic/Game.cpp @@ -100,30 +100,6 @@ bool Game::NewFrame() API::Instance().Update(); - for (unsigned int i = 0; i < this->players.Size(); i++) - { - if(this->players[i]->player) this->players[i]->player->EndFrame(); - } - - for (unsigned int i = 0; i < this->players.Size(); i++) - { - if(this->players[i]->player) this->players[i]->player->BeginFrame(); - } - - API::Instance().Update(); - - for (unsigned int i = 0; i < this->players.Size(); i++) - { - if(this->players[i]->player) this->players[i]->player->EndFrame(); - } - - for (unsigned int i = 0; i < this->players.Size(); i++) - { - if(this->players[i]->player) this->players[i]->player->BeginFrame(); - } - - API::Instance().Update(); - for (unsigned int i = 0; i < this->players.Size(); i++) { if(this->players[i]->player) this->players[i]->player->EndFrame(); diff --git a/Code/Game/GameLogic/Game.h b/Code/Game/GameLogic/Game.h index 11c8d325..7d40a3c5 100644 --- a/Code/Game/GameLogic/Game.h +++ b/Code/Game/GameLogic/Game.h @@ -39,7 +39,7 @@ namespace GameLogic Oyster::Math::Float4x4 GetOrientation() override; int GetID() const override; OBJECT_TYPE GetObjectType() const override; - void Rotate(const float x, const float y) override; + void Rotate(const Oyster::Math3D::Float3 lookDir) override; Player *player; }; diff --git a/Code/Game/GameLogic/GameAPI.h b/Code/Game/GameLogic/GameAPI.h index 12f5022f..2f052927 100644 --- a/Code/Game/GameLogic/GameAPI.h +++ b/Code/Game/GameLogic/GameAPI.h @@ -79,7 +79,7 @@ namespace GameLogic * @param x: The relative x axis * @param y: The relative y axis **/ - virtual void Rotate(const float x, const float y) = 0; + virtual void Rotate(const Oyster::Math3D::Float3 lookDir) = 0; /******************************************************** * Uses the chosen players weapon based on input diff --git a/Code/Game/GameLogic/Game_PlayerData.cpp b/Code/Game/GameLogic/Game_PlayerData.cpp index e651a02f..ed628c5e 100644 --- a/Code/Game/GameLogic/Game_PlayerData.cpp +++ b/Code/Game/GameLogic/Game_PlayerData.cpp @@ -49,7 +49,7 @@ OBJECT_TYPE Game::PlayerData::GetObjectType() const { return this->player->GetType(); } -void Game::PlayerData::Rotate(const float x, const float y) +void Game::PlayerData::Rotate(const Oyster::Math3D::Float3 lookDir) { - this->player->Rotate(x, y); + } \ No newline at end of file diff --git a/Code/Game/GameProtocols/GameProtocols.vcxproj b/Code/Game/GameProtocols/GameProtocols.vcxproj index a5d5b19f..826f9df4 100644 --- a/Code/Game/GameProtocols/GameProtocols.vcxproj +++ b/Code/Game/GameProtocols/GameProtocols.vcxproj @@ -155,7 +155,6 @@ - diff --git a/Code/Game/GameProtocols/GameplayProtocols.h b/Code/Game/GameProtocols/GameplayProtocols.h deleted file mode 100644 index 8ffcd827..00000000 --- a/Code/Game/GameProtocols/GameplayProtocols.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef GAMEPROTOCOLS_GAMEPLAYPROTOCOLS_H -#define GAMEPROTOCOLS_GAMEPLAYPROTOCOLS_H - -#include -#include -#include "ProtocolIdentificationID.h" - -namespace GameLogic -{ - -} - -#endif // !GAMEPROTOCOLS_GAMEPLAYPROTOCOLS_H diff --git a/Code/Game/GameProtocols/LobbyProtocols.h b/Code/Game/GameProtocols/LobbyProtocols.h index 4a262404..8968de74 100644 --- a/Code/Game/GameProtocols/LobbyProtocols.h +++ b/Code/Game/GameProtocols/LobbyProtocols.h @@ -156,6 +156,13 @@ namespace GameLogic list.Reserve(10); } + Protocol_LobbyGameData(Oyster::Network::CustomNetProtocol& p) + { + this->protocol[protocol_INDEX_ID].value = protocol_Lobby_GameData; + this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + + list.Reserve(10); + } Oyster::Network::CustomNetProtocol* GetProtocol() override { int a = 1; @@ -186,6 +193,10 @@ namespace GameLogic this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Short; + } + Protocol_LobbyMainData(Oyster::Network::CustomNetProtocol& p) + { + } Oyster::Network::CustomNetProtocol* GetProtocol() override { diff --git a/Code/Game/GameProtocols/ObjectProtocols.h b/Code/Game/GameProtocols/ObjectProtocols.h index d37aee44..9c4e2df1 100644 --- a/Code/Game/GameProtocols/ObjectProtocols.h +++ b/Code/Game/GameProtocols/ObjectProtocols.h @@ -21,6 +21,10 @@ namespace GameLogic object_ID = -1; pickup_ID = -1; + } + Protocol_ObjectPickup(Oyster::Network::CustomNetProtocol& p) + { + } Protocol_ObjectPickup(int objectID, short pickupID) { @@ -60,6 +64,10 @@ namespace GameLogic object_ID = -1; health = 0.0f; + } + Protocol_ObjectDamage(Oyster::Network::CustomNetProtocol& p) + { + } Protocol_ObjectDamage(int id, float hp) { @@ -100,6 +108,10 @@ namespace GameLogic } object_ID = -1; memset(&worldMatrix[0], 0, sizeof(float) * 16); + } + Protocol_ObjectPosition(Oyster::Network::CustomNetProtocol& p) + { + } Protocol_ObjectPosition(float m[16], int id) { @@ -148,6 +160,10 @@ namespace GameLogic } object_ID = -1; memset(&worldMatrix[0], 0, sizeof(float) * 16); + } + Protocol_ObjectEnable(Oyster::Network::CustomNetProtocol& p) + { + } Protocol_ObjectEnable(float m[16], int id) { @@ -188,6 +204,10 @@ namespace GameLogic this->protocol[1].type = Oyster::Network::NetAttributeType_Int; this->protocol[2].type = Oyster::Network::NetAttributeType_Float; + } + Protocol_ObjectDisable(Oyster::Network::CustomNetProtocol& p) + { + } Protocol_ObjectDisable(int id, float time) { @@ -229,6 +249,10 @@ namespace GameLogic { this->protocol[i].type = Oyster::Network::NetAttributeType_Float; } + } + Protocol_ObjectCreate(Oyster::Network::CustomNetProtocol& p) + { + } Protocol_ObjectCreate(float m[16], int id, char *path) { diff --git a/Code/Game/GameProtocols/PlayerProtocols.h b/Code/Game/GameProtocols/PlayerProtocols.h index ffb009cf..988693ce 100644 --- a/Code/Game/GameProtocols/PlayerProtocols.h +++ b/Code/Game/GameProtocols/PlayerProtocols.h @@ -32,6 +32,10 @@ namespace GameLogic this->protocol[2].type = Oyster::Network::NetAttributeType_Bool; this->protocol[3].type = Oyster::Network::NetAttributeType_Bool; this->protocol[4].type = Oyster::Network::NetAttributeType_Bool; + } + Protocol_PlayerMovement(Oyster::Network::CustomNetProtocol& p) + { + } const Protocol_PlayerMovement& operator=(Oyster::Network::CustomNetProtocol& val) { @@ -72,6 +76,10 @@ namespace GameLogic this->protocol[2].type = Oyster::Network::NetAttributeType_Float; this->protocol[3].type = Oyster::Network::NetAttributeType_Float; + } + Protocol_PlayerLook(Oyster::Network::CustomNetProtocol& p) + { + } const Protocol_PlayerLook& operator=(Oyster::Network::CustomNetProtocol& val) { @@ -108,6 +116,10 @@ namespace GameLogic this->protocol[2].type = Oyster::Network::NetAttributeType_Float; this->protocol[3].type = Oyster::Network::NetAttributeType_Float; + } + Protocol_PlayerChangeWeapon(Oyster::Network::CustomNetProtocol& p) + { + } const Protocol_PlayerChangeWeapon& operator=(Oyster::Network::CustomNetProtocol& val) { @@ -132,6 +144,10 @@ namespace GameLogic this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Bool; + } + Protocol_PlayerShot(Oyster::Network::CustomNetProtocol& p) + { + } const Protocol_PlayerShot& operator=(Oyster::Network::CustomNetProtocol& val) { diff --git a/Code/Game/GameProtocols/Protocols.h b/Code/Game/GameProtocols/Protocols.h index 1e7236c8..06b0df82 100644 --- a/Code/Game/GameProtocols/Protocols.h +++ b/Code/Game/GameProtocols/Protocols.h @@ -5,6 +5,5 @@ #include "PlayerProtocols.h" #include "LobbyProtocols.h" #include "GeneralProtocols.h" -#include "GameplayProtocols.h" #endif // !GAMEPROTOCOLS_GAMEPROTOCOLS_H diff --git a/Code/Game/GameServer/GameLobby.h b/Code/Game/GameServer/GameLobby.h index 1391641e..47e3b329 100644 --- a/Code/Game/GameServer/GameLobby.h +++ b/Code/Game/GameServer/GameLobby.h @@ -29,11 +29,11 @@ namespace DanBias void GeneralText(GameLogic::Protocol_General_Text& p, Oyster::Network::NetworkClient* c); //id = protocol_General_Text: void LobbyCreateGame(GameLogic::Protocol_LobbyCreateGame& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Create: void LobbyStartGame(GameLogic::Protocol_LobbyStartGame& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Start: - void LobbyJoin(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Join: - void LobbyLogin(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Login: + void LobbyJoin(GameLogic::Protocol_LobbyJoin& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Join: + void LobbyLogin(GameLogic::Protocol_LobbyLogin& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Login: void LobbyRefresh(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Refresh: - void LobbyMainData(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_MainData: - void LobbyGameData(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_GameData: + void LobbyMainData(GameLogic::Protocol_LobbyMainData& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_MainData: + void LobbyGameData(GameLogic::Protocol_LobbyGameData& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_GameData: private: void ClientEventCallback(Oyster::Network::NetEvent e) override; diff --git a/Code/Game/GameServer/GameSession.h b/Code/Game/GameServer/GameSession.h index ed480c35..e75be66d 100644 --- a/Code/Game/GameServer/GameSession.h +++ b/Code/Game/GameServer/GameSession.h @@ -16,6 +16,7 @@ #include #include #include +#include namespace DanBias @@ -55,17 +56,31 @@ namespace DanBias //Private member functions private: - //Handles all events recieved - //void ParseEvents(); - + // TODO: find out what this method does.. void ClientEventCallback(Oyster::Network::NetEvent e) override; - void ParseProtocol(Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c); + //Sends a client to the owner, if obj is NULL then all clients is sent void SendToOwner(DanBias::GameClient* obj); //Frame function, derived from IThreadObject bool DoWork ( ) override; + + private: + void ParseProtocol (Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c); + + void Gameplay_PlayerMovement ( GameLogic::Protocol_PlayerMovement& p, DanBias::GameClient* c ); + void Gameplay_PlayerLookDir ( GameLogic::Protocol_PlayerLook& p, DanBias::GameClient* c ); + void Gameplay_PlayerChangeWeapon ( GameLogic::Protocol_PlayerChangeWeapon& p, DanBias::GameClient* c ); + void Gameplay_PlayerShot ( GameLogic::Protocol_PlayerShot& p, DanBias::GameClient* c ); + void Gameplay_ObjectPickup ( GameLogic::Protocol_ObjectPickup& p, DanBias::GameClient* c ); + void Gameplay_ObjectDamage ( GameLogic::Protocol_ObjectDamage& p, DanBias::GameClient* c ); + void Gameplay_ObjectPosition ( GameLogic::Protocol_ObjectPosition& p, DanBias::GameClient* c ); + void Gameplay_ObjectEnabled ( GameLogic::Protocol_ObjectEnable& p, DanBias::GameClient* c ); + void Gameplay_ObjectDisabled ( GameLogic::Protocol_ObjectDisable& p, DanBias::GameClient* c ); + void Gameplay_ObjectCreate ( GameLogic::Protocol_ObjectCreate& p, DanBias::GameClient* c ); + void General_Status ( GameLogic::Protocol_General_Status& p, DanBias::GameClient* c ); + void General_Text ( GameLogic::Protocol_General_Text& p, DanBias::GameClient* c ); //Private member variables private: @@ -78,6 +93,7 @@ namespace DanBias bool isRunning; Utility::WinTimer timer; + //Callback method recieving from gamelogic static void ObjectMove(GameLogic::IObjectData* movedObject); diff --git a/Code/Game/GameServer/Implementation/GameLobby.cpp b/Code/Game/GameServer/Implementation/GameLobby.cpp index cd90793a..9396d71d 100644 --- a/Code/Game/GameServer/Implementation/GameLobby.cpp +++ b/Code/Game/GameServer/Implementation/GameLobby.cpp @@ -4,6 +4,7 @@ #include "..\GameLobby.h" #include #include +#include using namespace Utility::DynamicMemory; using namespace Oyster::Network; @@ -24,6 +25,9 @@ namespace DanBias void GameLobby::Update() { + if(GetAsyncKeyState(VK_DOWN)) + this->Send(*GameLogic::Protocol_General_Status().GetProtocol()); + this->ProcessClients(); } GameLobby::operator bool() @@ -40,15 +44,19 @@ namespace DanBias case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToRecieve: break; case NetworkClient::ClientEventArgs::EventType_ProtocolFailedToSend: + printf("\t(%i : %s) - EventType_ProtocolFailedToSend\n", e.sender->GetID(), e.sender->GetIpAddress().c_str()); + e.sender->Disconnect(); break; case NetworkClient::ClientEventArgs::EventType_ProtocolRecieved: + printf("\t(%i : %s) - EventType_ProtocolRecieved\n", e.sender->GetID(), e.sender->GetIpAddress().c_str()); this->ParseProtocol(e.args.data.protocol, e.sender); break; } } void GameLobby::ClientConnectedEvent(Utility::DynamicMemory::SmartPointer client) { - //Attach(client); + printf("New client(%i) connected - %s \n", client->GetID(), client->GetIpAddress().c_str()); + Attach(client); } }//End namespace DanBias \ No newline at end of file diff --git a/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp b/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp index 45ec4dfa..335bf55b 100644 --- a/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp +++ b/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp @@ -9,30 +9,23 @@ void GameLobby::ParseProtocol(Oyster::Network::CustomNetProtocol& p, NetworkClie { switch (p[0].value.netShort) { - //LobbyStartGame(GameLogic::Protocol_LobbyStartGame(p), c); - //LobbyRefresh(GameLogic::Protocol_LobbyRefresh(p), c); - //LobbyLogin(GameLogic::Protocol_LobbyLogin(p), c); - //LobbyJoin(GameLogic::Protocol_LobbyJoin(p), c); - //GeneralStatus(GameLogic::Protocol_General_Status(p), c); - //GeneralText(GameLogic::Protocol_General_Text(p), c); - - case protocol_General_Status: + case protocol_General_Status: this->GeneralStatus (Protocol_General_Status (p), c); break; - case protocol_General_Text: + case protocol_General_Text: this->GeneralText (Protocol_General_Text (p), c); break; - case protocol_Lobby_Create: + case protocol_Lobby_Create: this->LobbyCreateGame (Protocol_LobbyCreateGame (p), c); break; - case protocol_Lobby_Start: + case protocol_Lobby_Start: this->LobbyStartGame (Protocol_LobbyStartGame (p), c); break; - case protocol_Lobby_Join: + case protocol_Lobby_Join: this->LobbyJoin (Protocol_LobbyJoin (p), c); break; - case protocol_Lobby_Login: + case protocol_Lobby_Login: this->LobbyLogin (Protocol_LobbyLogin (p), c); break; - case protocol_Lobby_Refresh: + case protocol_Lobby_Refresh: this->LobbyRefresh (Protocol_LobbyRefresh (p), c); break; - case protocol_Lobby_MainData: + case protocol_Lobby_MainData: this->LobbyMainData (Protocol_LobbyMainData (p), c); break; - case protocol_Lobby_GameData: + case protocol_Lobby_GameData: this->LobbyGameData (Protocol_LobbyGameData (p), c); break; } } @@ -69,7 +62,7 @@ void GameLobby::LobbyStartGame(GameLogic::Protocol_LobbyStartGame& p, Oyster::Ne { } -void GameLobby::LobbyJoin(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c) +void GameLobby::LobbyJoin(GameLogic::Protocol_LobbyJoin& p, Oyster::Network::NetworkClient* c) { //for (unsigned int i = 0; i < this->gameLobby.Size(); i++) //{ @@ -80,7 +73,7 @@ void GameLobby::LobbyJoin(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network:: // } //} } -void GameLobby::LobbyLogin(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c) +void GameLobby::LobbyLogin(GameLogic::Protocol_LobbyLogin& p, Oyster::Network::NetworkClient* c) { } @@ -88,11 +81,11 @@ void GameLobby::LobbyRefresh(GameLogic::Protocol_LobbyRefresh& p, Oyster::Networ { //Dont need to handle this on the server... } -void GameLobby::LobbyMainData(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c) +void GameLobby::LobbyMainData(GameLogic::Protocol_LobbyMainData& p, Oyster::Network::NetworkClient* c) { } -void GameLobby::LobbyGameData(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c) +void GameLobby::LobbyGameData(GameLogic::Protocol_LobbyGameData& p, Oyster::Network::NetworkClient* c) { } diff --git a/Code/Game/GameServer/Implementation/GameServer.cpp b/Code/Game/GameServer/Implementation/GameServer.cpp index ac2abc8a..d98794a4 100644 --- a/Code/Game/GameServer/Implementation/GameServer.cpp +++ b/Code/Game/GameServer/Implementation/GameServer.cpp @@ -41,7 +41,7 @@ DanBiasServerReturn GameServerAPI::Create(const GameInitDesc& desc) return DanBiasServerReturn_Error; } - std::printf("Server created!\t-\t%s: [%i]\n", server.GetLanAddress().c_str(), desc.listenPort); + std::printf("Server created!\t-\t%s: [%i]\n\n", server.GetLanAddress().c_str(), desc.listenPort); return DanBiasServerReturn_Sucess; } @@ -53,8 +53,7 @@ void GameServerAPI::Start() while (true) { - int c = server.ProcessConnectedClients(); - if(c > 0) printf(" - [%i] client(s) connected!\n", c); + server.ProcessConnectedClients(); lobby.Update(); if(GetAsyncKeyState(0x51)) //Q for exit diff --git a/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp b/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp index ef1898be..151cc739 100644 --- a/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp +++ b/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp @@ -45,91 +45,142 @@ namespace DanBias return this->isRunning; } - //void GameSession::ParseEvents() - //{ - // if( !this->box->IsEmpty() ) - // { - // NetworkSession::NetEvent &e = this->box->Fetch(); - // static int ii = 0; - // printf("%i - Message recieved! [%i]\n", ii++, e.protocol[0].value); - // - // if(e.protocol[0].type != Oyster::Network::NetAttributeType_Short) return; - // - // if( ProtocolIsGameplay(e.protocol[protocol_INDEX_ID].value.netShort) ) - // ParseGameplayEvent(e.protocol, e.gameClient); - // - // if( ProtocolIsGeneral(e.protocol[protocol_INDEX_ID].value.netShort) ) - // ParseGeneralEvent(e.protocol, e.gameClient); - // } - //} + void GameSession::ClientEventCallback(NetEvent e) + { + + } + + void GameSession::ObjectMove(GameLogic::IObjectData* movedObject) + { + //GameLogic::IObjectData* obj = NULL; + //if(dynamic_cast(movedObject)) + // obj =((GameLogic::ILevelData*)movedObject)->GetObjectAt(0); + //if(obj) + //{ + // if(obj->GetType() == OBJECT_TYPE_BOX) + // { + // obj->GetID(); + // Oyster::Math::Float4x4 world =obj->GetOrientation(); + // Protocol_ObjectPosition p(world, 1); + // GameSession::gameSession->Send(p.GetProtocol()); + // } + //} + + } + +//*****************************************************// +//****************** Protocol methods *****************// +//******************************************************************************************************************// void GameSession::ParseProtocol(Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c) { switch (p[protocol_INDEX_ID].value.netShort) { - case protocol_Gameplay_PlayerMovement: - { - if(p[1].value.netBool) //bool bForward; - c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_FORWARD); - if(p[2].value.netBool) //bool bBackward; - c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_BACKWARD); - if(p[3].value.netBool) //bool bStrafeLeft; - c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_LEFT); - if(p[4].value.netBool) //bool bStrafeRight; - c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_RIGHT); - } + case protocol_Gameplay_PlayerMovement: this->Gameplay_PlayerMovement ( Protocol_PlayerMovement (p), c ); break; - case protocol_Gameplay_PlayerLookDir: - { - Protocol_PlayerLook m; m = p; - //c->GetPlayer()->Rotate(m.dxMouse, m.dyMouse); - } + case protocol_Gameplay_PlayerLookDir: this->Gameplay_PlayerLookDir ( Protocol_PlayerLook (p), c ); break; - case protocol_Gameplay_PlayerChangeWeapon: - + case protocol_Gameplay_PlayerChangeWeapon: this->Gameplay_PlayerChangeWeapon ( Protocol_PlayerChangeWeapon (p), c ); break; - case protocol_Gameplay_ObjectDamage: - + case protocol_Gameplay_PlayerShot: this->Gameplay_PlayerShot ( Protocol_PlayerShot (p), c ); break; - case protocol_General_Status: - switch (p[1].value.netInt) - { - case GameLogic::Protocol_General_Status::States_disconected: - printf("Client with ID [%i] dissconnected\n", c->GetClient()->GetID()); - this->Detach(c->GetClient()->GetID()); - break; - - case GameLogic::Protocol_General_Status::States_idle: - - break; - - case GameLogic::Protocol_General_Status::States_ready: - - break; - - case GameLogic::Protocol_General_Status::States_leave: - - break; - } + case protocol_Gameplay_ObjectPickup: this->Gameplay_ObjectPickup ( Protocol_ObjectPickup (p), c ); break; - case protocol_General_Text: - { - GameLogic::Protocol_General_Text temp(p); - printf("Message recieved from (%i):\t %s\n", c->GetID(), temp.text.c_str()); - } + case protocol_Gameplay_ObjectDamage: this->Gameplay_ObjectDamage ( Protocol_ObjectDamage (p), c ); + break; + case protocol_Gameplay_ObjectPosition: this->Gameplay_ObjectPosition ( Protocol_ObjectPosition (p), c ); + break; + case protocol_Gameplay_ObjectEnabled: this->Gameplay_ObjectEnabled ( Protocol_ObjectEnable (p), c ); + break; + case protocol_Gameplay_ObjectDisabled: this->Gameplay_ObjectDisabled ( Protocol_ObjectDisable (p), c ); + break; + case protocol_Gameplay_ObjectCreate: this->Gameplay_ObjectCreate ( Protocol_ObjectCreate (p), c ); + break; + case protocol_General_Status: this->General_Status ( Protocol_General_Status (p), c ); + break; + case protocol_General_Text: this->General_Text ( Protocol_General_Text (p), c ); break; } } - void GameSession::ObjectMove(GameLogic::IObjectData* movedObject) + void GameSession::Gameplay_PlayerMovement ( Protocol_PlayerMovement& p, DanBias::GameClient* c ) { - movedObject->GetID(); - movedObject->GetOrientation(); + if(p.bForward) c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_FORWARD); + if(p.bBackward) c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_BACKWARD); + if(p.bLeft) c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_LEFT); + if(p.bRight) c->GetPlayer()->Move(GameLogic::PLAYER_MOVEMENT_RIGHT); } - - void GameSession::ClientEventCallback(NetEvent e) + void GameSession::Gameplay_PlayerLookDir ( Protocol_PlayerLook& p, DanBias::GameClient* c ) + { + Oyster::Math3D::Float3 lookDir; + lookDir.x = p.lookDirX; + lookDir.y = p.lookDirY; + lookDir.z = p.lookDirZ; + c->GetPlayer()->Rotate(lookDir); + } + void GameSession::Gameplay_PlayerChangeWeapon ( Protocol_PlayerChangeWeapon& p, DanBias::GameClient* c ) { } + void GameSession::Gameplay_PlayerShot ( Protocol_PlayerShot& p, DanBias::GameClient* c ) + { + c->GetPlayer()->UseWeapon(GameLogic::WEAPON_USE_PRIMARY_PRESS); + } + void GameSession::Gameplay_ObjectPickup ( Protocol_ObjectPickup& p, DanBias::GameClient* c ) + { + + } + void GameSession::Gameplay_ObjectDamage ( Protocol_ObjectDamage& p, DanBias::GameClient* c ) + { + + } + void GameSession::Gameplay_ObjectPosition ( Protocol_ObjectPosition& p, DanBias::GameClient* c ) + { + + } + void GameSession::Gameplay_ObjectEnabled ( Protocol_ObjectEnable& p, DanBias::GameClient* c ) + { + + } + void GameSession::Gameplay_ObjectDisabled ( Protocol_ObjectDisable& p, DanBias::GameClient* c ) + { + + } + void GameSession::Gameplay_ObjectCreate ( Protocol_ObjectCreate& p, DanBias::GameClient* c ) + { + + } + void GameSession::General_Status ( Protocol_General_Status& p, DanBias::GameClient* c ) + { + switch (p.status) + { + case GameLogic::Protocol_General_Status::States_disconected: + printf("Client with ID [%i] dissconnected\n", c->GetClient()->GetID()); + this->Detach(c->GetClient()->GetID()); + break; + + case GameLogic::Protocol_General_Status::States_idle: + + break; + + case GameLogic::Protocol_General_Status::States_ready: + + break; + + case GameLogic::Protocol_General_Status::States_leave: + + break; + } + } + void GameSession::General_Text ( Protocol_General_Text& p, DanBias::GameClient* c ) + { + printf("Message recieved from (%i):\t %s\n", c->GetID(), p.text.c_str()); + } + }//End namespace DanBias + + + + + diff --git a/Code/Network/NetworkAPI/NetworkClient.cpp b/Code/Network/NetworkAPI/NetworkClient.cpp index f5d7c643..9539cdeb 100644 --- a/Code/Network/NetworkAPI/NetworkClient.cpp +++ b/Code/Network/NetworkAPI/NetworkClient.cpp @@ -171,7 +171,7 @@ void NetworkClient::Update() } } -bool NetworkClient::Connect(int socket) +bool NetworkClient::Connect(ConnectionInfo& socket) { if(this->IsConnected()) return true; if(this->privateData) return false; @@ -250,4 +250,7 @@ void NetworkClient::DataRecieved(NetEvent e) void NetworkClient::NetworkCallback(Oyster::Network::CustomNetProtocol& p) {} - +std::string NetworkClient::GetIpAddress() +{ + return this->privateData->connection.GetIpAddress(); +} diff --git a/Code/Network/NetworkAPI/NetworkClient.h b/Code/Network/NetworkAPI/NetworkClient.h index 869a5100..dfe247de 100644 --- a/Code/Network/NetworkAPI/NetworkClient.h +++ b/Code/Network/NetworkAPI/NetworkClient.h @@ -15,6 +15,7 @@ namespace Oyster namespace Network { class NetworkSession; + struct ConnectionInfo; extern "C" { @@ -54,7 +55,7 @@ namespace Oyster /** * */ - bool Connect(int socket); + bool Connect(ConnectionInfo& data); /** * @@ -102,6 +103,8 @@ namespace Oyster */ virtual void NetworkCallback(Oyster::Network::CustomNetProtocol& p); + virtual std::string GetIpAddress(); + private: NetworkClient(const NetworkClient& obj); NetworkClient& operator =(const NetworkClient& obj); diff --git a/Code/Network/NetworkAPI/NetworkServer.cpp b/Code/Network/NetworkAPI/NetworkServer.cpp index 9d6db207..9a4195a2 100644 --- a/Code/Network/NetworkAPI/NetworkServer.cpp +++ b/Code/Network/NetworkAPI/NetworkServer.cpp @@ -77,7 +77,7 @@ public: public: Listener* listener; - PostBox postBox; //Postbox for new clients + PostBox postBox; //Postbox for new clients OysterThread thread; //Server thread NetworkSession *mainSession; Utility::Container::ThreadSafeQueue> clientQueue; @@ -95,9 +95,9 @@ bool NetworkServer::PrivateData::DoWork() /** Check for new clients **/ if(postBox.IsFull()) { - int clientSocketNum = postBox.FetchMessage(); - - if(clientSocketNum == -1) + ConnectionInfo clientSocketNum = postBox.FetchMessage(); + + if(clientSocketNum.socket == -1) { //Something went wrong somewhere... do we care? } diff --git a/Code/Network/NetworkDependencies/Connection.cpp b/Code/Network/NetworkDependencies/Connection.cpp index 27aa296a..99cb8a71 100644 --- a/Code/Network/NetworkDependencies/Connection.cpp +++ b/Code/Network/NetworkDependencies/Connection.cpp @@ -1,6 +1,7 @@ #include "Connection.h" #include +#include #include #include #include @@ -40,11 +41,13 @@ Connection::~Connection() CloseSocket( this->socket ); } -int Connection::Connect(int socket, bool blocking) +int Connection::Connect(ConnectionInfo info, bool blocking) { - this->socket = socket; + this->addr = info.addr; + this->socket = info.socket; this->stillSending = true; this->closed = false; + SetBlockingMode(blocking); //connection succesfull! @@ -167,17 +170,24 @@ int Connection::Recieve(OysterByte &bytes) } //Listen will only return the correct socket or -1 for failure. -int Connection::Listen() +ConnectionInfo Connection::Listen() { - if(this->closed) return -1; + ConnectionInfo val = { 0 }; + if(this->closed) return val; - int clientSocket; - if((clientSocket = (int)accept(this->socket, NULL, NULL)) == INVALID_SOCKET) + SOCKADDR_IN client_info = { 0 }; + int addrsize = sizeof(client_info); + + if((val.socket = (int)accept(this->socket, (struct sockaddr*)&client_info, &addrsize)) == INVALID_SOCKET) { - return (int)INVALID_SOCKET;//WSAGetLastError(); + val.socket = WSAGetLastError(); + } + else + { + val.addr = inet_ntoa(client_info.sin_addr); } - return clientSocket; + return val; } bool Connection::IsSending() @@ -213,6 +223,12 @@ int Connection::SetBlockingMode(bool blocking) return 0; } + +std::string Connection::GetIpAddress() +{ + return this->addr; +} + /////////////////////////////////////// //Private functions /////////////////////////////////////// diff --git a/Code/Network/NetworkDependencies/Connection.h b/Code/Network/NetworkDependencies/Connection.h index d2e815d3..0f46a599 100644 --- a/Code/Network/NetworkDependencies/Connection.h +++ b/Code/Network/NetworkDependencies/Connection.h @@ -14,7 +14,6 @@ namespace Oyster { class Connection : public IConnection { - public: Connection(); Connection( int socket ); @@ -27,10 +26,10 @@ namespace Oyster virtual int Recieve( OysterByte &bytes ); virtual int Disconnect(); - virtual int Connect(int socket, bool blocking = false); + virtual int Connect(ConnectionInfo info, bool blocking = false); virtual int Connect(unsigned short port , const char serverName[], bool blocking = false); - virtual int Listen(); + virtual ConnectionInfo Listen(); bool IsSending(); bool IsConnected(); @@ -38,6 +37,8 @@ namespace Oyster //Setting the socket to blocking/non-blocking mode. int SetBlockingMode( bool blocking ); + std::string GetIpAddress(); + private: int InitiateSocket(); @@ -45,7 +46,7 @@ namespace Oyster bool stillSending; bool closed; - + std::string addr; }; } } diff --git a/Code/Network/NetworkDependencies/IConnection.h b/Code/Network/NetworkDependencies/IConnection.h index 76736071..bd59de65 100644 --- a/Code/Network/NetworkDependencies/IConnection.h +++ b/Code/Network/NetworkDependencies/IConnection.h @@ -11,6 +11,12 @@ namespace Oyster { namespace Network { + struct ConnectionInfo + { + int socket; + std::string addr; + }; + class OysterByte; class IConnection { @@ -27,7 +33,7 @@ namespace Oyster virtual int InitiateClient() { return false; }; //Listen function to let client connect, only used by the server - virtual int Listen() { return -1; }; + virtual ConnectionInfo Listen() { return ConnectionInfo(); }; //enables the client to connect with a server with use of name and port //(servers uses Listen instead of connect) diff --git a/Code/Network/NetworkDependencies/Listener.cpp b/Code/Network/NetworkDependencies/Listener.cpp index 106ac3b9..d4ee396a 100644 --- a/Code/Network/NetworkDependencies/Listener.cpp +++ b/Code/Network/NetworkDependencies/Listener.cpp @@ -11,7 +11,7 @@ Listener::Listener() connection = NULL; } -Listener::Listener(Oyster::Network::IPostBox* postBox) +Listener::Listener(Oyster::Network::IPostBox* postBox) { this->isListening = false; connection = NULL; @@ -88,7 +88,7 @@ void Listener::Shutdown() StopListen(); } -void Listener::SetPostBox(Oyster::Network::IPostBox* postBox) +void Listener::SetPostBox(Oyster::Network::IPostBox* postBox) { stdMutex.lock(); this->postBox = postBox; @@ -97,21 +97,21 @@ void Listener::SetPostBox(Oyster::Network::IPostBox* postBox) int Listener::Accept() { - int clientSocket = -1; + ConnectionInfo clientSocket = {0}; clientSocket = connection->Listen(); if(!this->isListening.load()) { return -1; } - if(clientSocket != -1) + if(clientSocket.socket != -1) { stdMutex.lock(); postBox->PostMessage(clientSocket); stdMutex.unlock(); } - return clientSocket; + return clientSocket.socket; } void Listener::StopListen() { diff --git a/Code/Network/NetworkDependencies/Listener.h b/Code/Network/NetworkDependencies/Listener.h index 0c55f86e..49449990 100644 --- a/Code/Network/NetworkDependencies/Listener.h +++ b/Code/Network/NetworkDependencies/Listener.h @@ -20,7 +20,7 @@ namespace Oyster { public: Listener(); - Listener(Oyster::Network::IPostBox* postBox); + Listener(Oyster::Network::IPostBox* postBox); ~Listener(); bool Init(unsigned int port); @@ -29,7 +29,7 @@ namespace Oyster void Stop(); void Shutdown(); - void SetPostBox(IPostBox* postBox); + void SetPostBox(IPostBox* postBox); private: //Thread functions @@ -48,7 +48,7 @@ namespace Oyster OysterMutex mutex; std::mutex stdMutex; - IPostBox* postBox; + IPostBox* postBox; std::atomic isListening; int port; }; From 05740665dac9cc7a7630d57a04ef8446f843017f Mon Sep 17 00:00:00 2001 From: Dennis Andersen Date: Thu, 30 Jan 2014 11:11:04 +0100 Subject: [PATCH 15/23] GameServer - Fixed a small error regarding not reaching some data --- Code/Game/DanBiasGame/DanBiasGame.vcxproj | 11 +- Code/Game/DanBiasGame/DanBiasMaincpp.cpp | 2 +- .../Game/DanBiasGame/GameClientRecieverFunc.h | 7 -- .../DanBiasGame/GameClientState/GameState.cpp | 4 +- .../DanBiasServerLauncher/ServerLauncher.cpp | 4 +- Code/Game/GameProtocols/LobbyProtocols.h | 106 +++++++++--------- Code/Game/GameServer/GameLobby.h | 20 +++- Code/Game/GameServer/GameSession.h | 2 - .../GameServer/Implementation/GameLobby.cpp | 32 +++++- .../GameLobby_ProtocolParser.cpp | 42 +++---- .../GameServer/Implementation/GameServer.cpp | 21 ++-- Code/Network/NetworkAPI/NetworkSession.cpp | 53 +++++---- Code/Network/NetworkAPI/NetworkSession.h | 11 +- 13 files changed, 179 insertions(+), 136 deletions(-) diff --git a/Code/Game/DanBiasGame/DanBiasGame.vcxproj b/Code/Game/DanBiasGame/DanBiasGame.vcxproj index 25696efd..955107ef 100644 --- a/Code/Game/DanBiasGame/DanBiasGame.vcxproj +++ b/Code/Game/DanBiasGame/DanBiasGame.vcxproj @@ -72,7 +72,7 @@ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName)D $(SolutionDir)..\External\Lib\;$(SolutionDir)..\Bin\DLL;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) - $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Network/NetworkAPI;$(IncludePath);C:\Program Files %28x86%29\Visual Leak Detector\include + $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Network/NetworkAPI;$(IncludePath);C:\Program Files %28x86%29\Visual Leak Detector\include;$(SolutionDir)Game\GameServer\ true @@ -80,7 +80,7 @@ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName)D $(SolutionDir)..\External\Lib\;$(SolutionDir)..\Bin\DLL;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) - $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Network/NetworkAPI;$(IncludePath);C:\Program Files %28x86%29\Visual Leak Detector\include + $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Network/NetworkAPI;$(IncludePath);C:\Program Files %28x86%29\Visual Leak Detector\include;$(SolutionDir)Game\GameServer\ false @@ -88,7 +88,7 @@ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName) $(SolutionDir)..\External\Lib\;$(SolutionDir)..\Bin\DLL;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) - $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Network/NetworkAPI;$(IncludePath);C:\Program Files %28x86%29\Visual Leak Detector\include + $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Network/NetworkAPI;$(IncludePath);C:\Program Files %28x86%29\Visual Leak Detector\include;$(SolutionDir)Game\GameServer\ false @@ -96,7 +96,7 @@ $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ $(ProjectName)_$(PlatformShortName) $(SolutionDir)..\External\Lib\;$(SolutionDir)..\Bin\DLL;C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) - $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Network/NetworkAPI;$(IncludePath);C:\Program Files %28x86%29\Visual Leak Detector\include + $(SolutionDir)..\External\Include\;$(SolutionDir)Game\GameProtocols\;$(SolutionDir)Network/NetworkAPI;$(IncludePath);C:\Program Files %28x86%29\Visual Leak Detector\include;$(SolutionDir)Game\GameServer\ @@ -190,6 +190,9 @@ {f10cbc03-9809-4cba-95d8-327c287b18ee} + + {143bd516-20a1-4890-a3e4-f8bfd02220e7} + diff --git a/Code/Game/DanBiasGame/DanBiasMaincpp.cpp b/Code/Game/DanBiasGame/DanBiasMaincpp.cpp index 0b7ea666..b62dbe11 100644 --- a/Code/Game/DanBiasGame/DanBiasMaincpp.cpp +++ b/Code/Game/DanBiasGame/DanBiasMaincpp.cpp @@ -7,7 +7,7 @@ //-------------------------------------------------------------------------------------- #define NOMINMAX #include - +#include < #include "DllInterfaces/GFXAPI.h" //#include "IGame.h" diff --git a/Code/Game/DanBiasGame/GameClientRecieverFunc.h b/Code/Game/DanBiasGame/GameClientRecieverFunc.h index f7214b32..e7710b67 100644 --- a/Code/Game/DanBiasGame/GameClientRecieverFunc.h +++ b/Code/Game/DanBiasGame/GameClientRecieverFunc.h @@ -13,13 +13,6 @@ struct GameRecieverObject :public Oyster::Network::NetworkClient // parsing protocols and sending it to the gameState void NetworkCallback(Oyster::Network::CustomNetProtocol& p) override { - - //if( IsGameplayProtocol(p[protocol_INDEX_ID].value.netShort) ) - //ParseGameplayEvent(e.protocol, e.gameClient); - - //if( IsGeneralProtocol(p[protocol_INDEX_ID].value.netShort) ) - //ParseGeneralEvent(e.protocol, e.gameClient); - int pType = p[0].value.netInt; switch (pType) { diff --git a/Code/Game/DanBiasGame/GameClientState/GameState.cpp b/Code/Game/DanBiasGame/GameClientState/GameState.cpp index 871ff558..ba3508a7 100644 --- a/Code/Game/DanBiasGame/GameClientState/GameState.cpp +++ b/Code/Game/DanBiasGame/GameClientState/GameState.cpp @@ -335,8 +335,8 @@ void GameState::Protocol( ObjPos* pos ) //camera->setLook((Oyster::Math::Float3(world[8], world[9], world[10]))); if(i == 2) // playerobj { - //camera->SetPosition(Oyster::Math::Float3(world[12], world[13], world[14])); - //camera->UpdateViewMatrix(); + camera->SetPosition(Oyster::Math::Float3(world[12], world[13], world[14])); + camera->UpdateViewMatrix(); } } } diff --git a/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp b/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp index 62e2b828..a27ab406 100644 --- a/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp +++ b/Code/Game/DanBiasServerLauncher/ServerLauncher.cpp @@ -2,7 +2,7 @@ // Launcher to launch Danbias server // // Created by [Dennis Andersen] [2013] // ////////////////////////////////////////////////// -#define NOMINMAX //Blame it on windows +#define NOMINMAX //Blame it on microsoft #include #include #include @@ -23,7 +23,9 @@ int WINAPI WinMain( HINSTANCE hinst, HINSTANCE prevInst, PSTR cmdLine, int cmdSh desc.listenPort = 15151; if(DanBias::GameServerAPI::ServerInitiate(desc) == DanBias::DanBiasServerReturn_Sucess) { + DanBias::GameServerAPI::ServerStart(); + //DanBias::GameServerAPI::GameStart(); while (!(GetAsyncKeyState(0x51))) //Q for exit { DanBias::GameServerAPI::ServerUpdate(); diff --git a/Code/Game/GameProtocols/LobbyProtocols.h b/Code/Game/GameProtocols/LobbyProtocols.h index 87aad62e..8b333a54 100644 --- a/Code/Game/GameProtocols/LobbyProtocols.h +++ b/Code/Game/GameProtocols/LobbyProtocols.h @@ -92,32 +92,32 @@ namespace GameLogic Oyster::Network::CustomNetProtocol protocol; }; - struct Protocol_LobbyJoin :public Oyster::Network::CustomProtocolObject - { - short value; - - Protocol_LobbyJoin() - { - this->protocol[protocol_INDEX_ID].value = protocol_Lobby_Join; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; - this->protocol[1].type = Oyster::Network::NetAttributeType_Short; - } - Protocol_LobbyJoin(Oyster::Network::CustomNetProtocol& p) - { - this->protocol[protocol_INDEX_ID].value = protocol_Lobby_Join; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; - this->protocol[1].type = Oyster::Network::NetAttributeType_Short; - value = p[1].value.netShort; - } - Oyster::Network::CustomNetProtocol* GetProtocol() override - { - protocol[1].value = value; - return &protocol; - } - - private: - Oyster::Network::CustomNetProtocol protocol; - }; + //struct Protocol_LobbyJoin :public Oyster::Network::CustomProtocolObject + //{ + // short value; + // + // Protocol_LobbyJoin() + // { + // this->protocol[protocol_INDEX_ID].value = protocol_Lobby_Join; + // this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + // this->protocol[1].type = Oyster::Network::NetAttributeType_Short; + // } + // Protocol_LobbyJoin(Oyster::Network::CustomNetProtocol& p) + // { + // this->protocol[protocol_INDEX_ID].value = protocol_Lobby_Join; + // this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + // this->protocol[1].type = Oyster::Network::NetAttributeType_Short; + // value = p[1].value.netShort; + // } + // Oyster::Network::CustomNetProtocol* GetProtocol() override + // { + // protocol[1].value = value; + // return &protocol; + // } + // + // private: + // Oyster::Network::CustomNetProtocol protocol; + //}; struct Protocol_LobbyRefresh :public Oyster::Network::CustomProtocolObject { @@ -140,7 +140,8 @@ namespace GameLogic /** * A protocol that contains all data to send to client when update game lobby */ - struct Protocol_LobbyGameData :public Oyster::Network::CustomProtocolObject + + struct Protocol_LobbyClientData :public Oyster::Network::CustomProtocolObject { // Player list struct PlayerData @@ -150,14 +151,14 @@ namespace GameLogic }; Utility::DynamicMemory::DynamicArray list; - Protocol_LobbyGameData() + Protocol_LobbyClientData() { this->protocol[protocol_INDEX_ID].value = protocol_Lobby_GameData; this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; list.Reserve(10); } - Protocol_LobbyGameData(Oyster::Network::CustomNetProtocol& p) + Protocol_LobbyClientData(Oyster::Network::CustomNetProtocol& p) { this->protocol[protocol_INDEX_ID].value = protocol_Lobby_GameData; this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; @@ -181,32 +182,33 @@ namespace GameLogic private: Oyster::Network::CustomNetProtocol protocol; }; + /** * A protocol that contains all data to send to client when update main lobby */ - struct Protocol_LobbyMainData :public Oyster::Network::CustomProtocolObject - { - // Game instance list - - Protocol_LobbyMainData() - { - this->protocol[protocol_INDEX_ID].value = protocol_Lobby_MainData; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; - - this->protocol[1].type = Oyster::Network::NetAttributeType_Short; - } - Protocol_LobbyMainData(Oyster::Network::CustomNetProtocol& p) - { - - } - Oyster::Network::CustomNetProtocol* GetProtocol() override - { - return &protocol; - } - - private: - Oyster::Network::CustomNetProtocol protocol; - }; + //struct Protocol_LobbyMainData :public Oyster::Network::CustomProtocolObject + //{ + // // Game instance list + // + // Protocol_LobbyMainData() + // { + // this->protocol[protocol_INDEX_ID].value = protocol_Lobby_MainData; + // this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + // + // this->protocol[1].type = Oyster::Network::NetAttributeType_Short; + // } + // Protocol_LobbyMainData(Oyster::Network::CustomNetProtocol& p) + // { + // + // } + // Oyster::Network::CustomNetProtocol* GetProtocol() override + // { + // return &protocol; + // } + // + // private: + // Oyster::Network::CustomNetProtocol protocol; + //}; } #endif // !GAMELOGIC_PLAYER_PROTOCOLS_H diff --git a/Code/Game/GameServer/GameLobby.h b/Code/Game/GameServer/GameLobby.h index 2b9f4bc4..8463b9d9 100644 --- a/Code/Game/GameServer/GameLobby.h +++ b/Code/Game/GameServer/GameLobby.h @@ -12,6 +12,14 @@ namespace DanBias { + struct LobbyLevelData + { + int mapNumber; + int maxClients; + int gameMode; + int gameTime; + std::string gameName; + }; class GameLobby :public Oyster::Network::NetworkSession { public: @@ -20,8 +28,8 @@ namespace DanBias void Release(); void Update(); - void SetGameDesc(const GameSession::GameDescription& desc); - void GetGameDesc(GameSession::GameDescription& desc); + void SetGameDesc(const LobbyLevelData& desc); + void GetGameDesc(LobbyLevelData& desc); bool StartGameSession(); private: @@ -31,11 +39,11 @@ namespace DanBias void GeneralText(GameLogic::Protocol_General_Text& p, Oyster::Network::NetworkClient* c); //id = protocol_General_Text: //void LobbyCreateGame(GameLogic::Protocol_LobbyCreateGame& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Create: void LobbyStartGame(GameLogic::Protocol_LobbyStartGame& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Start: - void LobbyJoin(GameLogic::Protocol_LobbyJoin& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Join: + //void LobbyJoin(GameLogic::Protocol_LobbyJoin& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Join: void LobbyLogin(GameLogic::Protocol_LobbyLogin& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Login: void LobbyRefresh(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Refresh: - void LobbyMainData(GameLogic::Protocol_LobbyMainData& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_MainData: - void LobbyGameData(GameLogic::Protocol_LobbyGameData& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_GameData: + void LobbyMainData(GameLogic::Protocol_LobbyClientData& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_MainData: + //void LobbyGameData(GameLogic::Protocol_LobbyGameData& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_GameData: private: void ClientEventCallback(Oyster::Network::NetEvent e) override; @@ -45,7 +53,7 @@ namespace DanBias Utility::WinTimer timer; float refreshFrequency; GameSession gameSession; - GameSession::GameDescription description; + LobbyLevelData description; }; }//End namespace DanBias #endif // !DANBIASGAME_GAMELOBBY_H diff --git a/Code/Game/GameServer/GameSession.h b/Code/Game/GameServer/GameSession.h index 4685440a..cdae58e5 100644 --- a/Code/Game/GameServer/GameSession.h +++ b/Code/Game/GameServer/GameSession.h @@ -32,10 +32,8 @@ namespace DanBias struct GameDescription { int mapNumber; - int maxClients; int gameMode; int gameTime; - std::string gameName; Oyster::Network::NetworkSession* owner; Utility::DynamicMemory::DynamicArray clients; }; diff --git a/Code/Game/GameServer/Implementation/GameLobby.cpp b/Code/Game/GameServer/Implementation/GameLobby.cpp index 24ca7887..308e4ae6 100644 --- a/Code/Game/GameServer/Implementation/GameLobby.cpp +++ b/Code/Game/GameServer/Implementation/GameLobby.cpp @@ -9,6 +9,7 @@ using namespace Utility::DynamicMemory; using namespace Oyster::Network; using namespace Oyster; +using namespace GameLogic; namespace DanBias { @@ -30,17 +31,34 @@ namespace DanBias this->ProcessClients(); } - void GameLobby::SetGameDesc(const GameSession::GameDescription& desc) + void GameLobby::SetGameDesc(const LobbyLevelData& desc) { - this->description = desc; + this->description.gameMode = desc.gameMode; + this->description.gameTime = desc.gameTime; + this->description.mapNumber = desc.mapNumber; + this->description.maxClients = desc.maxClients; } - void GameLobby::GetGameDesc(GameSession::GameDescription& desc) + void GameLobby::GetGameDesc(LobbyLevelData& desc) { - desc = this->description; + desc.gameMode = this->description.gameMode; + desc.gameTime = this->description.gameTime; + desc.mapNumber = this->description.mapNumber; + desc.maxClients = this->description.maxClients; } bool GameLobby::StartGameSession() { - if(this->gameSession.Create(this->description)) + GameSession::GameDescription desc; + desc.gameMode = this->description.gameMode; + desc.gameTime = this->description.gameTime; + desc.mapNumber = this->description.mapNumber; + desc.owner = this; + while (this->GetClientCount()) + { + NetClient c; + if((c = this->Detach())) + desc.clients.Push(c); + } + if(this->gameSession.Create(desc)) { this->gameSession.Run(); return true; @@ -70,6 +88,10 @@ namespace DanBias { printf("New client(%i) connected - %s \n", client->GetID(), client->GetIpAddress().c_str()); Attach(client); + + Protocol_LobbyClientData p; + + client->Send(p.GetProtocol()); } }//End namespace DanBias \ No newline at end of file diff --git a/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp b/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp index 0de7b062..1c841519 100644 --- a/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp +++ b/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp @@ -17,16 +17,16 @@ void GameLobby::ParseProtocol(Oyster::Network::CustomNetProtocol& p, NetworkClie //break; case protocol_Lobby_Start: this->LobbyStartGame (Protocol_LobbyStartGame (p), c); break; - case protocol_Lobby_Join: this->LobbyJoin (Protocol_LobbyJoin (p), c); - break; + //case protocol_Lobby_Join: this->LobbyJoin (Protocol_LobbyJoin (p), c); + //break; case protocol_Lobby_Login: this->LobbyLogin (Protocol_LobbyLogin (p), c); break; case protocol_Lobby_Refresh: this->LobbyRefresh (Protocol_LobbyRefresh (p), c); break; - case protocol_Lobby_MainData: this->LobbyMainData (Protocol_LobbyMainData (p), c); - break; - case protocol_Lobby_GameData: this->LobbyGameData (Protocol_LobbyGameData (p), c); + case protocol_Lobby_MainData: this->LobbyMainData (Protocol_LobbyClientData (p), c); break; + //case protocol_Lobby_GameData: this->LobbyGameData (Protocol_LobbyGameData (p), c); + //break; } } @@ -62,17 +62,17 @@ void GameLobby::LobbyStartGame(GameLogic::Protocol_LobbyStartGame& p, Oyster::Ne { } -void GameLobby::LobbyJoin(GameLogic::Protocol_LobbyJoin& 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::LobbyJoin(GameLogic::Protocol_LobbyJoin& 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_LobbyLogin& p, Oyster::Network::NetworkClient* c) { @@ -81,13 +81,13 @@ void GameLobby::LobbyRefresh(GameLogic::Protocol_LobbyRefresh& p, Oyster::Networ { //Dont need to handle this on the server... } -void GameLobby::LobbyMainData(GameLogic::Protocol_LobbyMainData& p, Oyster::Network::NetworkClient* c) -{ - -} -void GameLobby::LobbyGameData(GameLogic::Protocol_LobbyGameData& p, Oyster::Network::NetworkClient* c) +void GameLobby::LobbyMainData(GameLogic::Protocol_LobbyClientData& p, Oyster::Network::NetworkClient* c) { } +//void GameLobby::LobbyGameData(GameLogic::Protocol_LobbyGameData& p, Oyster::Network::NetworkClient* c) +//{ +// +//} diff --git a/Code/Game/GameServer/Implementation/GameServer.cpp b/Code/Game/GameServer/Implementation/GameServer.cpp index 0489a12d..416503e5 100644 --- a/Code/Game/GameServer/Implementation/GameServer.cpp +++ b/Code/Game/GameServer/Implementation/GameServer.cpp @@ -39,9 +39,6 @@ DanBiasServerReturn GameServerAPI::ServerInitiate(const ServerInitDesc& desc) return DanBiasServerReturn_Error; } GameSession::GameDescription d; - lobby.GetGameDesc(d); - d.gameName.assign(desc.serverName); - lobby.SetGameDesc(d); std::printf("Server created!\t-\t%s: [%i]\n\n", server.GetLanAddress().c_str(), desc.listenPort); @@ -95,59 +92,59 @@ GameServerAPI::GameServerInfo GameServerAPI::ServerGetInfo() } void GameServerAPI::GameSetMapId(const int& val) { - GameSession::GameDescription d; + LobbyLevelData d; lobby.GetGameDesc(d); d.mapNumber = val; lobby.SetGameDesc(d); } void GameServerAPI::GameSetMaxClients(const int& val) { - GameSession::GameDescription d; + LobbyLevelData d; lobby.GetGameDesc(d); d.maxClients = val; lobby.SetGameDesc(d); } void GameServerAPI::GameSetGameMode(const int& val) { - GameSession::GameDescription d; + LobbyLevelData d; lobby.GetGameDesc(d); d.gameMode = val; lobby.SetGameDesc(d); } void GameServerAPI::GameSetGameTime(const int& val) { - GameSession::GameDescription d; + LobbyLevelData d; lobby.GetGameDesc(d); d.gameTime = val; lobby.SetGameDesc(d); } int GameServerAPI::GameGetMapId() { - GameSession::GameDescription d; + LobbyLevelData d; lobby.GetGameDesc(d); return d.mapNumber; } int GameServerAPI::GameGetMaxClients() { - GameSession::GameDescription d; + LobbyLevelData d; lobby.GetGameDesc(d); return d.maxClients; } int GameServerAPI::GameGetGameMode() { - GameSession::GameDescription d; + LobbyLevelData d; lobby.GetGameDesc(d); return d.gameMode; } int GameServerAPI::GameGetGameTime() { - GameSession::GameDescription d; + LobbyLevelData d; lobby.GetGameDesc(d); return d.gameTime; } const char* GameServerAPI::GameGetGameName() { - GameSession::GameDescription d; + LobbyLevelData d; lobby.GetGameDesc(d); return d.gameName.c_str(); } diff --git a/Code/Network/NetworkAPI/NetworkSession.cpp b/Code/Network/NetworkAPI/NetworkSession.cpp index d1fc7fec..35352243 100644 --- a/Code/Network/NetworkAPI/NetworkSession.cpp +++ b/Code/Network/NetworkAPI/NetworkSession.cpp @@ -20,11 +20,9 @@ struct NetworkSession::PrivateSessionData NetworkClient::ClientEventFunction messageCallback; std::mutex clientListLock; NetworkSession* owner; //Where clients end up when session is closed. - int clientCount; int id; NetworkSession::PrivateSessionData() - : clientCount(0) - , owner(0) + : owner(0) , id(GID()) {} }; @@ -38,7 +36,7 @@ NetworkSession::NetworkSession(const NetworkSession& orig) { this->data->clients = orig.data->clients; this->data->owner = orig.data->owner; - this->data->clientCount = orig.data->clientCount; + this->clientCount = orig.clientCount; this->data->id = orig.data->id; this->data->messageCallback = orig.data->messageCallback; } @@ -47,7 +45,7 @@ const NetworkSession& NetworkSession::operator=(const NetworkSession& orig) { this->data->clients = orig.data->clients; this->data->owner = orig.data->owner; - this->data->clientCount = orig.data->clientCount; + this->clientCount = orig.clientCount; this->data->id = orig.data->id; this->data->messageCallback = orig.data->messageCallback; @@ -57,7 +55,7 @@ const NetworkSession& NetworkSession::operator=(const NetworkSession& orig) NetworkSession::~NetworkSession() { this->data->clients.Clear(); - this->data->clientCount = 0; + this->clientCount = 0; this->data->messageCallback = 0; delete this->data; this->data = 0; @@ -91,7 +89,7 @@ bool NetworkSession::Attach(NetClient client) this->data->clients[k] = client; } - this->data->clientCount++; + this->clientCount++; client->SetOwner(this); this->data->clientListLock.unlock(); @@ -99,7 +97,7 @@ bool NetworkSession::Attach(NetClient client) return true; } -void NetworkSession::Detach() +void NetworkSession::DetachAll() { if(this->data->owner) { @@ -117,6 +115,7 @@ void NetworkSession::Detach() this->data->clients[i] = 0; } } + this->clientCount = 0; } NetClient NetworkSession::Detach(const NetworkClient* client) @@ -131,7 +130,7 @@ NetClient NetworkSession::Detach(const NetworkClient* client) { val = this->data->clients[i]; this->data->clients[i] = 0; - this->data->clientCount--; + this->clientCount--; } } @@ -152,7 +151,29 @@ NetClient NetworkSession::Detach(short ID) { val = this->data->clients[i]; this->data->clients[i] = 0; - this->data->clientCount--; + this->clientCount--; + } + } + + this->data->clientListLock.unlock(); + + return val; +} + +NetClient NetworkSession::Detach() +{ + NetClient val; + + this->data->clientListLock.lock(); + + for (unsigned int i = 0; i < this->data->clients.Size(); i++) + { + if(this->data->clients[i]) + { + val = this->data->clients[i]; + this->data->clients[i] = 0; + this->clientCount--; + break; } } @@ -204,7 +225,7 @@ void NetworkSession::CloseSession(bool dissconnectClients) } this->data->clients.Clear(); - this->data->clientCount = 0; + this->clientCount = 0; this->data->clientListLock.unlock(); } @@ -214,16 +235,6 @@ 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 b8287b36..ffe047a4 100644 --- a/Code/Network/NetworkAPI/NetworkSession.h +++ b/Code/Network/NetworkAPI/NetworkSession.h @@ -11,6 +11,7 @@ #include "NetworkServerEventStruct.h" #include "NetworkClient.h" #include "Utilities.h" +#include "DynamicArray.h" namespace Oyster { @@ -38,7 +39,7 @@ namespace Oyster * Detaches all clients and sends them to owner. * If no owner is set the clients is disconnected. */ - virtual void Detach(); + virtual void DetachAll(); /** * @@ -50,6 +51,11 @@ namespace Oyster */ virtual NetClient Detach(short ID); + /** + * + */ + virtual NetClient Detach(); + /** Send a message to all clients in this session * @param message The message */ @@ -74,7 +80,7 @@ namespace Oyster /** Get the number of clients active in this session * @return The client count */ - int GetClientCount() const; + inline int GetClientCount() const { return this->clientCount; } /** * @@ -87,6 +93,7 @@ namespace Oyster virtual void ClientEventCallback(NetEvent e) = 0; private: + int clientCount; struct PrivateSessionData; PrivateSessionData* data; }; From 1ccf98e3eb4526f400c3c0b8ae0d0d082499e791 Mon Sep 17 00:00:00 2001 From: lindaandersson Date: Thu, 30 Jan 2014 11:58:44 +0100 Subject: [PATCH 16/23] GL - added loginstate --- Code/Game/DanBiasGame/DanBiasGame.vcxproj | 2 + Code/Game/DanBiasGame/DanBiasGame_Impl.cpp | 8 +- .../GameClientState/GameClientState.h | 1 + .../GameClientState/LobbyState.cpp | 2 +- .../DanBiasGame/GameClientState/LobbyState.h | 2 + .../GameClientState/LoginState.cpp | 166 ++++++++++++++++++ .../DanBiasGame/GameClientState/LoginState.h | 33 ++++ 7 files changed, 210 insertions(+), 4 deletions(-) create mode 100644 Code/Game/DanBiasGame/GameClientState/LoginState.cpp create mode 100644 Code/Game/DanBiasGame/GameClientState/LoginState.h diff --git a/Code/Game/DanBiasGame/DanBiasGame.vcxproj b/Code/Game/DanBiasGame/DanBiasGame.vcxproj index 955107ef..f1cbf970 100644 --- a/Code/Game/DanBiasGame/DanBiasGame.vcxproj +++ b/Code/Game/DanBiasGame/DanBiasGame.vcxproj @@ -206,6 +206,7 @@ + @@ -216,6 +217,7 @@ + diff --git a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp index 07d2d0ec..64526c0b 100644 --- a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp +++ b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp @@ -5,6 +5,7 @@ #include "GameClientState/GameClientState.h" #include "GameClientState\GameState.h" #include "GameClientState\LobbyState.h" +#include "GameClientState\LoginState.h" #include #include "NetworkClient.h" @@ -61,15 +62,15 @@ namespace DanBias return DanBiasClientReturn_Error; m_data->recieverObj = new GameRecieverObject; - m_data->recieverObj->Connect(desc.port, desc.IP); + /*m_data->recieverObj->Connect(desc.port, desc.IP); if (!m_data->recieverObj->IsConnected()) { // failed to connect return DanBiasClientReturn_Error; - } + }*/ // Start in lobby state - m_data->recieverObj->gameClientState = new Client::LobbyState(); + m_data->recieverObj->gameClientState = new Client::LoginState(); if(!m_data->recieverObj->gameClientState->Init(m_data->recieverObj)) return DanBiasClientReturn_Error; @@ -134,6 +135,7 @@ namespace DanBias m_data->inputObj->Update(); + DanBias::Client::GameClientState::ClientState state = DanBias::Client::GameClientState::ClientState_Same; state = m_data->recieverObj->gameClientState->Update(deltaTime, m_data->inputObj); diff --git a/Code/Game/DanBiasGame/GameClientState/GameClientState.h b/Code/Game/DanBiasGame/GameClientState/GameClientState.h index 2ba930b7..28a671ba 100644 --- a/Code/Game/DanBiasGame/GameClientState/GameClientState.h +++ b/Code/Game/DanBiasGame/GameClientState/GameClientState.h @@ -51,6 +51,7 @@ public: }; enum ClientState { + ClientState_Login, ClientState_Lobby, ClientState_Game, ClientState_Same, diff --git a/Code/Game/DanBiasGame/GameClientState/LobbyState.cpp b/Code/Game/DanBiasGame/GameClientState/LobbyState.cpp index 89d8b1b7..d30fcd59 100644 --- a/Code/Game/DanBiasGame/GameClientState/LobbyState.cpp +++ b/Code/Game/DanBiasGame/GameClientState/LobbyState.cpp @@ -31,7 +31,7 @@ LobbyState::~LobbyState(void) bool LobbyState::Init(Oyster::Network::NetworkClient* nwClient) { privData = new myData(); - + this->nwClient = nwClient; // load models LoadModels(L"UImodels.txt"); InitCamera(Oyster::Math::Float3(0,0,5.4f)); diff --git a/Code/Game/DanBiasGame/GameClientState/LobbyState.h b/Code/Game/DanBiasGame/GameClientState/LobbyState.h index e7cb7b8c..057fc850 100644 --- a/Code/Game/DanBiasGame/GameClientState/LobbyState.h +++ b/Code/Game/DanBiasGame/GameClientState/LobbyState.h @@ -3,6 +3,7 @@ #include "GameClientState.h" #include "OysterMath.h" +#include "NetworkClient.h" #include namespace DanBias { @@ -12,6 +13,7 @@ namespace DanBias class LobbyState : public GameClientState { private: + Oyster::Network::NetworkClient* nwClient; struct myData; myData* privData; public: diff --git a/Code/Game/DanBiasGame/GameClientState/LoginState.cpp b/Code/Game/DanBiasGame/GameClientState/LoginState.cpp new file mode 100644 index 00000000..8fa4936d --- /dev/null +++ b/Code/Game/DanBiasGame/GameClientState/LoginState.cpp @@ -0,0 +1,166 @@ +#include "LoginState.h" +#include "DllInterfaces/GFXAPI.h" +#include "OysterMath.h" +#include "C_obj/C_Player.h" +#include "C_obj/C_StaticObj.h" +#include "C_obj/C_DynamicObj.h" +#include + +using namespace DanBias::Client; + +struct LoginState::myData +{ + myData(){} + Oyster::Math3D::Float4x4 view; + Oyster::Math3D::Float4x4 proj; + C_Object* object[2]; + int modelCount; + // UI object + // game client* +}privData; + +LoginState::LoginState(void) +{ + +} + +LoginState::~LoginState(void) +{ + +} + +bool LoginState::Init(Oyster::Network::NetworkClient* nwClient) +{ + privData = new myData(); + this->nwClient = nwClient; + // load models + LoadModels(L"UImodels.txt"); + InitCamera(Oyster::Math::Float3(0,0,5.4f)); + return true; +} +bool LoginState::LoadModels(std::wstring file) +{ + Oyster::Graphics::Definitions::Pointlight plight; + plight.Pos = Oyster::Math::Float3(-2,3,0); + plight.Color = Oyster::Math::Float3(0,1,0); + plight.Radius = 10; + plight.Bright = 1; + Oyster::Graphics::API::AddLight(plight); + // open file + // read file + // init models + privData->modelCount = 2; + + ModelInitData modelData; + + modelData.world = Oyster::Math3D::Float4x4::identity; + modelData.visible = true; + modelData.modelPath = L"..\\Content\\Models\\box_2.dan"; + // load models + privData->object[0] = new C_StaticObj(); + privData->object[0]->Init(modelData); + + Oyster::Math3D::Float4x4 translate = Oyster::Math3D::TranslationMatrix(Oyster::Math::Float3(-2,-2,-2)); + modelData.world = modelData.world * translate; + + privData->object[1] = new C_DynamicObj(); + privData->object[1]->Init(modelData); + return true; +} + +bool LoginState::InitCamera(Oyster::Math::Float3 startPos) +{ + privData->proj = Oyster::Math3D::ProjectionMatrix_Perspective(Oyster::Math::pi/2,1024.0f/768.0f,.1f,1000); + //privData->proj = Oyster::Math3D::ProjectionMatrix_Orthographic(1024, 768, 1, 1000); + Oyster::Graphics::API::SetProjection(privData->proj); + + privData->view = Oyster::Math3D::OrientationMatrix_LookAtDirection(Oyster::Math::Float3(0,0,-1),Oyster::Math::Float3(0,1,0),startPos); + privData->view = Oyster::Math3D::InverseOrientationMatrix(privData->view); + return true; +} +GameClientState::ClientState LoginState::Update(float deltaTime, InputClass* KeyInput) +{ + // picking + // mouse events + // different menus + // play sounds + // update animation + // send data to server + // check data from server + + // create game + if( KeyInput->IsKeyPressed(DIK_C)) + { + DanBias::GameServerAPI::ServerInitDesc desc; + + DanBias::GameServerAPI::ServerInitiate(desc); + DanBias::GameServerAPI::ServerStart(); + // my ip + nwClient->Connect(15151, "127.0.0.1"); + + if (!nwClient->IsConnected()) + { + // failed to connect + return ClientState_Same; + } + return ClientState_Lobby; + } + // join game + if( KeyInput->IsKeyPressed(DIK_J)) + { + // game ip + nwClient->Connect(15151, "194.47.150.56"); + + if (!nwClient->IsConnected()) + { + // failed to connect + return ClientState_Same; + } + return ClientState_Lobby; + } + return ClientState_Same; +} +bool LoginState::Render() +{ + + Oyster::Graphics::API::SetView(privData->view); + Oyster::Graphics::API::SetProjection( privData->proj); + + + Oyster::Graphics::API::NewFrame(); + // render objects + for (int i = 0; i < privData->modelCount; i++) + { + privData->object[i]->Render(); + } + + // render effects + + // render lights + + Oyster::Graphics::API::EndFrame(); + return true; +} +bool LoginState::Release() +{ + for (int i = 0; i < privData->modelCount; i++) + { + privData->object[i]->Release(); + delete privData->object[i]; + privData->object[i] = NULL; + } + + delete privData; + privData = NULL; + return true; +} +void LoginState::Protocol(ProtocolStruct* protocol) +{ + if((PlayerName*)protocol) + PlayerJoinProtocol((PlayerName*)protocol); + +} +void LoginState::PlayerJoinProtocol(PlayerName* name) +{ + +} \ No newline at end of file diff --git a/Code/Game/DanBiasGame/GameClientState/LoginState.h b/Code/Game/DanBiasGame/GameClientState/LoginState.h new file mode 100644 index 00000000..d426187d --- /dev/null +++ b/Code/Game/DanBiasGame/GameClientState/LoginState.h @@ -0,0 +1,33 @@ +#ifndef DANBIAS_CLIENT_LOGINSTATE_H +#define DANBIAS_CLIENT_LOGINSTATE_H + +#include "GameClientState.h" +#include "OysterMath.h" +#include "NetworkClient.h" +#include +namespace DanBias +{ + namespace Client + { + + class LoginState : public GameClientState + { + private: + Oyster::Network::NetworkClient* nwClient; + struct myData; + myData* privData; + public: + LoginState(void); + ~LoginState(void); + bool Init(Oyster::Network::NetworkClient* nwClient); + bool LoadModels(std::wstring file); + bool InitCamera(Oyster::Math::Float3 startPos); + ClientState Update(float deltaTime, InputClass* KeyInput); + + bool Render(); + bool Release(); + void Protocol(ProtocolStruct* protocol)override; + void PlayerJoinProtocol(PlayerName* name); + + };};}; +#endif // ! DANBIAS_CLIENT_LOGINSTATE_H \ No newline at end of file From c78bbba3eb17f848c6eb32c4b44a86ca9ccb0e5a Mon Sep 17 00:00:00 2001 From: Sam Mario Svensson Date: Thu, 30 Jan 2014 13:33:59 +0100 Subject: [PATCH 17/23] GL- Removed commentet unnecessary code --- Code/Game/GameLogic/LevelLoader/Loader.cpp | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/Code/Game/GameLogic/LevelLoader/Loader.cpp b/Code/Game/GameLogic/LevelLoader/Loader.cpp index a0206d89..3e15315c 100644 --- a/Code/Game/GameLogic/LevelLoader/Loader.cpp +++ b/Code/Game/GameLogic/LevelLoader/Loader.cpp @@ -17,18 +17,6 @@ char* Loader::LoadFile(std::string fileName, int &size) //convert from wstring to wchar then loads the file char* buffer = (char*)OysterResource::LoadResource(temp.c_str(), Oyster::Resource::ResourceType::ResourceType_Byte_Raw, -1 , false); - //std::ifstream f; - //f.open(fileName, std::ios::binary); - //if (!f.is_open()) - // return 0; - //f.seekg(0, std::ifstream::end); - //size = f.tellg(); - //f.seekg(0); - //char* buffer = new char[size]; - //f.read(buffer, size); - // - //f.close(); - //gets the size of the char buffer. size = OysterResource::GetResourceSize(buffer); return buffer; } \ No newline at end of file From 09f39ec26d4bd30cad18c4176cb9526370e5ea21 Mon Sep 17 00:00:00 2001 From: Dennis Andersen Date: Thu, 30 Jan 2014 14:15:25 +0100 Subject: [PATCH 18/23] GameServer - Added acces directly to clients in network session --- Code/Game/GameProtocols/GeneralProtocols.h | 10 +- Code/Game/GameProtocols/LobbyProtocols.h | 107 +++++++++++++----- Code/Game/GameProtocols/ObjectProtocols.h | 48 ++++---- Code/Game/GameProtocols/PlayerProtocols.h | 20 ++-- .../GameProtocols/ProtocolIdentificationID.h | 7 +- Code/Game/GameServer/GameLobby.h | 2 +- .../GameServer/Implementation/GameLobby.cpp | 30 +++-- .../GameLobby_ProtocolParser.cpp | 14 ++- .../Implementation/GameSession_Gameplay.cpp | 2 +- Code/Network/NetworkAPI/NetworkSession.cpp | 79 +++++++------ Code/Network/NetworkAPI/NetworkSession.h | 7 ++ 11 files changed, 199 insertions(+), 127 deletions(-) diff --git a/Code/Game/GameProtocols/GeneralProtocols.h b/Code/Game/GameProtocols/GeneralProtocols.h index 98b7c060..02763ba0 100644 --- a/Code/Game/GameProtocols/GeneralProtocols.h +++ b/Code/Game/GameProtocols/GeneralProtocols.h @@ -19,8 +19,8 @@ namespace GameLogic Protocol_General_Status() { - this->protocol[protocol_INDEX_ID].value = protocol_General_Status; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_General_Status; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Short; } @@ -31,8 +31,8 @@ namespace GameLogic } Protocol_General_Status(States state) { - this->protocol[protocol_INDEX_ID].value = protocol_General_Status; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_General_Status; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->status = state; this->protocol[1].type = Oyster::Network::NetAttributeType_Short; } @@ -61,7 +61,7 @@ namespace GameLogic } Oyster::Network::CustomNetProtocol* GetProtocol() override { - this->protocol.Set(protocol_INDEX_ID, protocol_General_Text, Oyster::Network::NetAttributeType_Short); + this->protocol.Set(0, protocol_General_Text, Oyster::Network::NetAttributeType_Short); this->protocol.Set(1, destination, Oyster::Network::NetAttributeType_Int); this->protocol.Set(2, text); return &protocol; diff --git a/Code/Game/GameProtocols/LobbyProtocols.h b/Code/Game/GameProtocols/LobbyProtocols.h index 8b333a54..c653f556 100644 --- a/Code/Game/GameProtocols/LobbyProtocols.h +++ b/Code/Game/GameProtocols/LobbyProtocols.h @@ -22,8 +22,8 @@ namespace GameLogic Protocol_LobbyCreateGame() { - this->protocol[protocol_INDEX_ID].value = protocol_Lobby_Create; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_Lobby_Create; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_CharArray; this->protocol[2].type = Oyster::Network::NetAttributeType_Char; @@ -50,8 +50,8 @@ namespace GameLogic Protocol_LobbyStartGame() { - this->protocol[protocol_INDEX_ID].value = protocol_Lobby_Start; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_Lobby_Start; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Short; } @@ -74,8 +74,8 @@ namespace GameLogic // Login stuff Protocol_LobbyLogin() { - this->protocol[protocol_INDEX_ID].value = protocol_Lobby_Join; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_Lobby_Join; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Short; } @@ -98,14 +98,14 @@ namespace GameLogic // // Protocol_LobbyJoin() // { - // this->protocol[protocol_INDEX_ID].value = protocol_Lobby_Join; - // this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + // this->protocol[0].value = protocol_Lobby_Join; + // this->protocol[0].type = Oyster::Network::NetAttributeType_Short; // this->protocol[1].type = Oyster::Network::NetAttributeType_Short; // } // Protocol_LobbyJoin(Oyster::Network::CustomNetProtocol& p) // { - // this->protocol[protocol_INDEX_ID].value = protocol_Lobby_Join; - // this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + // this->protocol[0].value = protocol_Lobby_Join; + // this->protocol[0].type = Oyster::Network::NetAttributeType_Short; // this->protocol[1].type = Oyster::Network::NetAttributeType_Short; // value = p[1].value.netShort; // } @@ -123,8 +123,8 @@ namespace GameLogic { Protocol_LobbyRefresh() { - this->protocol[protocol_INDEX_ID].value = protocol_Lobby_Login; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_Lobby_Login; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; } Protocol_LobbyRefresh(Oyster::Network::CustomNetProtocol& o) { @@ -147,35 +147,90 @@ namespace GameLogic struct PlayerData { std::string name; + std::string ip; int id; + int team; }; Utility::DynamicMemory::DynamicArray list; - + Protocol_LobbyClientData() { - this->protocol[protocol_INDEX_ID].value = protocol_Lobby_GameData; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_Lobby_ClientData; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; + + this->protocol[1].type = Oyster::Network::NetAttributeType_UnsignedInt; //DataType list.Reserve(10); } Protocol_LobbyClientData(Oyster::Network::CustomNetProtocol& p) { - this->protocol[protocol_INDEX_ID].value = protocol_Lobby_GameData; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; - - list.Reserve(10); + unsigned int size = this->protocol[1].value.netUInt; + list.Reserve(size); + int a = 2; + for (unsigned int i = 0; i < list.Size(); i++) + { + PlayerData d; + d.id = this->protocol[a++].value.netInt; + d.team = this->protocol[a++].value.netInt; + d.name = this->protocol.Get(a++).value.netCharPtr; + d.ip = this->protocol.Get(a++).value.netCharPtr; + list.Push(d); + } } Oyster::Network::CustomNetProtocol* GetProtocol() override { - int a = 1; + this->protocol[1].value = list.Size(); + + int a = 2; for (unsigned int i = 0; i < list.Size(); i++) { - this->protocol[a].type = Oyster::Network::NetAttributeType_Int; - this->protocol[a].type = Oyster::Network::NetAttributeType_CharArray; + this->protocol[a].type = Oyster::Network::NetAttributeType_Int; // client-id + this->protocol[a++].value = list[i].id; - this->protocol[a].value = list[i].id; - this->protocol.Set(a, list[i].name); + this->protocol[a].type = Oyster::Network::NetAttributeType_Int; // team-id + this->protocol[a++].value = list[i].team; + + this->protocol[a].type = Oyster::Network::NetAttributeType_CharArray; // clientName + this->protocol.Set(a++, list[i].name); + + this->protocol[a].type = Oyster::Network::NetAttributeType_CharArray; // clientIP + this->protocol.Set(a++, list[i].ip); } + + return &protocol; + } + + private: + Oyster::Network::CustomNetProtocol protocol; + }; + + struct Protocol_LobbyGameData :public Oyster::Network::CustomProtocolObject + { + std::string mapName; + int majorVersion; + int minorVersion; + + Protocol_LobbyGameData() + { + this->protocol[0].value = protocol_Lobby_GameData; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; + + this->protocol[1].type = Oyster::Network::NetAttributeType_Int; + this->protocol[2].type = Oyster::Network::NetAttributeType_Int; + this->protocol[3].type = Oyster::Network::NetAttributeType_CharArray; + } + Protocol_LobbyGameData(Oyster::Network::CustomNetProtocol& p) + { + majorVersion = (int)p.Get(1).value.netInt; + minorVersion = (int)p.Get(2).value.netInt; + mapName = p.Get(3).value.netCharPtr; + } + Oyster::Network::CustomNetProtocol* GetProtocol() override + { + this->protocol[1].value = majorVersion; + this->protocol[2].value = minorVersion; + this->protocol[3].value.netCharPtr = const_cast(mapName.c_str()); + return &protocol; } @@ -192,8 +247,8 @@ namespace GameLogic // // Protocol_LobbyMainData() // { - // this->protocol[protocol_INDEX_ID].value = protocol_Lobby_MainData; - // this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + // this->protocol[0].value = protocol_Lobby_MainData; + // this->protocol[0].type = Oyster::Network::NetAttributeType_Short; // // this->protocol[1].type = Oyster::Network::NetAttributeType_Short; // } diff --git a/Code/Game/GameProtocols/ObjectProtocols.h b/Code/Game/GameProtocols/ObjectProtocols.h index 9c4e2df1..5df97855 100644 --- a/Code/Game/GameProtocols/ObjectProtocols.h +++ b/Code/Game/GameProtocols/ObjectProtocols.h @@ -13,8 +13,8 @@ namespace GameLogic Protocol_ObjectPickup() { - this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_ObjectPickup; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_Gameplay_ObjectPickup; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Int; this->protocol[2].type = Oyster::Network::NetAttributeType_Short; @@ -28,8 +28,8 @@ namespace GameLogic } Protocol_ObjectPickup(int objectID, short pickupID) { - this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_ObjectPosition; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_Gameplay_ObjectPosition; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Int; this->protocol[2].type = Oyster::Network::NetAttributeType_Short; @@ -56,8 +56,8 @@ namespace GameLogic Protocol_ObjectDamage() { - this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_ObjectDamage; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_Gameplay_ObjectDamage; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Int; this->protocol[2].type = Oyster::Network::NetAttributeType_Float; @@ -71,8 +71,8 @@ namespace GameLogic } Protocol_ObjectDamage(int id, float hp) { - this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_ObjectDamage; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_Gameplay_ObjectDamage; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->protocol[2].type = Oyster::Network::NetAttributeType_Float; @@ -97,8 +97,8 @@ namespace GameLogic Protocol_ObjectPosition() { - this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_ObjectPosition; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_Gameplay_ObjectPosition; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Int; @@ -115,8 +115,8 @@ namespace GameLogic } Protocol_ObjectPosition(float m[16], int id) { - this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_ObjectPosition; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_Gameplay_ObjectPosition; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Int; @@ -149,8 +149,8 @@ namespace GameLogic Protocol_ObjectEnable() { - this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_ObjectEnabled; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_Gameplay_ObjectEnabled; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Int; @@ -167,8 +167,8 @@ namespace GameLogic } Protocol_ObjectEnable(float m[16], int id) { - this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_ObjectEnabled; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_Gameplay_ObjectEnabled; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Int; @@ -199,8 +199,8 @@ namespace GameLogic Protocol_ObjectDisable() { - this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_ObjectDisabled; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_Gameplay_ObjectDisabled; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Int; this->protocol[2].type = Oyster::Network::NetAttributeType_Float; @@ -211,8 +211,8 @@ namespace GameLogic } Protocol_ObjectDisable(int id, float time) { - this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_ObjectDisabled; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_Gameplay_ObjectDisabled; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Int; this->protocol[2].type = Oyster::Network::NetAttributeType_Float; @@ -239,8 +239,8 @@ namespace GameLogic Protocol_ObjectCreate() { - this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_ObjectCreate; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_Gameplay_ObjectCreate; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Int; this->protocol[2].type = Oyster::Network::NetAttributeType_CharArray; @@ -256,8 +256,8 @@ namespace GameLogic } Protocol_ObjectCreate(float m[16], int id, char *path) { - this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_ObjectCreate; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Int; + this->protocol[0].value = protocol_Gameplay_ObjectCreate; + this->protocol[0].type = Oyster::Network::NetAttributeType_Int; this->protocol[1].type = Oyster::Network::NetAttributeType_Int; this->protocol[2].type = Oyster::Network::NetAttributeType_CharArray; diff --git a/Code/Game/GameProtocols/PlayerProtocols.h b/Code/Game/GameProtocols/PlayerProtocols.h index d33ef9c3..7f8b81f0 100644 --- a/Code/Game/GameProtocols/PlayerProtocols.h +++ b/Code/Game/GameProtocols/PlayerProtocols.h @@ -25,8 +25,8 @@ namespace GameLogic Protocol_PlayerMovement() { - this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_PlayerMovement; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_Gameplay_PlayerMovement; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Bool; this->protocol[2].type = Oyster::Network::NetAttributeType_Bool; @@ -69,8 +69,8 @@ namespace GameLogic Protocol_PlayerLook() { - this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_PlayerLookDir; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_Gameplay_PlayerLookDir; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Float; this->protocol[2].type = Oyster::Network::NetAttributeType_Float; @@ -109,8 +109,8 @@ namespace GameLogic Protocol_PlayerChangeWeapon() { - this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_PlayerChangeWeapon; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_Gameplay_PlayerChangeWeapon; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Float; this->protocol[2].type = Oyster::Network::NetAttributeType_Float; @@ -140,8 +140,8 @@ namespace GameLogic Protocol_PlayerShot() { - this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_PlayerShot; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_Gameplay_PlayerShot; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Bool; } @@ -170,8 +170,8 @@ namespace GameLogic Protocol_PlayerJump() { - this->protocol[protocol_INDEX_ID].value = protocol_Gameplay_PlayerJump; - this->protocol[protocol_INDEX_ID].type = Oyster::Network::NetAttributeType_Short; + this->protocol[0].value = protocol_Gameplay_PlayerJump; + this->protocol[0].type = Oyster::Network::NetAttributeType_Short; this->protocol[1].type = Oyster::Network::NetAttributeType_Bool; } diff --git a/Code/Game/GameProtocols/ProtocolIdentificationID.h b/Code/Game/GameProtocols/ProtocolIdentificationID.h index 180f6541..cb596a1c 100644 --- a/Code/Game/GameProtocols/ProtocolIdentificationID.h +++ b/Code/Game/GameProtocols/ProtocolIdentificationID.h @@ -8,11 +8,6 @@ /* THERE CAN ABSOLUTLEY NOT BE TWO DEFINITIONS WITH THE SAME ID!! */ -/** Index where the identifier is located(aka protocol identification index) **/ -/* Use this as id accesser since it may change in the future. */ -#define protocol_INDEX_ID 0 - - /***********************************/ /********* RESERVERD PROTOCOLS ***************************************************************************************************/ /********** [ 0 - 99 ] *********/ @@ -37,7 +32,7 @@ #define protocol_Lobby_Join 202 #define protocol_Lobby_Login 203 #define protocol_Lobby_Refresh 204 -#define protocol_Lobby_MainData 205 +#define protocol_Lobby_ClientData 205 #define protocol_Lobby_GameData 206 #define protocol_LobbyMAX 299 diff --git a/Code/Game/GameServer/GameLobby.h b/Code/Game/GameServer/GameLobby.h index 8463b9d9..8a421e1d 100644 --- a/Code/Game/GameServer/GameLobby.h +++ b/Code/Game/GameServer/GameLobby.h @@ -42,8 +42,8 @@ namespace DanBias //void LobbyJoin(GameLogic::Protocol_LobbyJoin& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Join: void LobbyLogin(GameLogic::Protocol_LobbyLogin& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Login: void LobbyRefresh(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_Refresh: + void LobbyGameData(GameLogic::Protocol_LobbyGameData& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_GameData: void LobbyMainData(GameLogic::Protocol_LobbyClientData& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_MainData: - //void LobbyGameData(GameLogic::Protocol_LobbyGameData& p, Oyster::Network::NetworkClient* c); //id = protocol_Lobby_GameData: private: void ClientEventCallback(Oyster::Network::NetEvent e) override; diff --git a/Code/Game/GameServer/Implementation/GameLobby.cpp b/Code/Game/GameServer/Implementation/GameLobby.cpp index 308e4ae6..4291f44b 100644 --- a/Code/Game/GameServer/Implementation/GameLobby.cpp +++ b/Code/Game/GameServer/Implementation/GameLobby.cpp @@ -52,12 +52,8 @@ namespace DanBias desc.gameTime = this->description.gameTime; desc.mapNumber = this->description.mapNumber; desc.owner = this; - while (this->GetClientCount()) - { - NetClient c; - if((c = this->Detach())) - desc.clients.Push(c); - } + desc.clients = this->clients; + if(this->gameSession.Create(desc)) { this->gameSession.Run(); @@ -89,9 +85,27 @@ namespace DanBias printf("New client(%i) connected - %s \n", client->GetID(), client->GetIpAddress().c_str()); Attach(client); - Protocol_LobbyClientData p; + Protocol_LobbyClientData p1; + Protocol_LobbyGameData p2; - client->Send(p.GetProtocol()); + for (unsigned int i = 0; i < this->clients.Size(); i++) + { + if(this->clients[i]) + { + Protocol_LobbyClientData::PlayerData t; + t.id = this->clients[i]->GetID(); + t.ip = this->clients[i]->GetIpAddress(); + t.team = 0; + t.name = "DennisÄrKung"; + p1.list.Push(t); + } + } + p2.majorVersion = 1; + p2.minorVersion = 0; + p2.mapName = "BetsMap"; + + client->Send(p1.GetProtocol()); + client->Send(p2.GetProtocol()); } }//End namespace DanBias \ No newline at end of file diff --git a/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp b/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp index 1c841519..3a86bc6b 100644 --- a/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp +++ b/Code/Game/GameServer/Implementation/GameLobby_ProtocolParser.cpp @@ -23,7 +23,9 @@ void GameLobby::ParseProtocol(Oyster::Network::CustomNetProtocol& p, NetworkClie break; case protocol_Lobby_Refresh: this->LobbyRefresh (Protocol_LobbyRefresh (p), c); break; - case protocol_Lobby_MainData: this->LobbyMainData (Protocol_LobbyClientData (p), c); + case protocol_Lobby_GameData: this->LobbyGameData (Protocol_LobbyGameData (p), c); + break; + case protocol_Lobby_ClientData: this->LobbyMainData (Protocol_LobbyClientData (p), c); break; //case protocol_Lobby_GameData: this->LobbyGameData (Protocol_LobbyGameData (p), c); //break; @@ -60,7 +62,7 @@ void GameLobby::GeneralText(GameLogic::Protocol_General_Text& p, Oyster::Network //} void GameLobby::LobbyStartGame(GameLogic::Protocol_LobbyStartGame& p, Oyster::Network::NetworkClient* c) { - + //TODO: Prio 1 } //void GameLobby::LobbyJoin(GameLogic::Protocol_LobbyJoin& p, Oyster::Network::NetworkClient* c) //{ @@ -80,14 +82,14 @@ void GameLobby::LobbyLogin(GameLogic::Protocol_LobbyLogin& p, Oyster::Network::N void GameLobby::LobbyRefresh(GameLogic::Protocol_LobbyRefresh& p, Oyster::Network::NetworkClient* c) { //Dont need to handle this on the server... +} +void GameLobby::LobbyGameData(GameLogic::Protocol_LobbyGameData& p, Oyster::Network::NetworkClient* c) +{ + } void GameLobby::LobbyMainData(GameLogic::Protocol_LobbyClientData& p, Oyster::Network::NetworkClient* c) { } -//void GameLobby::LobbyGameData(GameLogic::Protocol_LobbyGameData& p, Oyster::Network::NetworkClient* c) -//{ -// -//} diff --git a/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp b/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp index a17f1ce8..0a528f04 100644 --- a/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp +++ b/Code/Game/GameServer/Implementation/GameSession_Gameplay.cpp @@ -74,7 +74,7 @@ namespace DanBias void GameSession::ParseProtocol(Oyster::Network::CustomNetProtocol& p, DanBias::GameClient* c) { - switch (p[protocol_INDEX_ID].value.netShort) + switch (p[0].value.netShort) { case protocol_Gameplay_PlayerMovement: this->Gameplay_PlayerMovement ( Protocol_PlayerMovement (p), c ); break; diff --git a/Code/Network/NetworkAPI/NetworkSession.cpp b/Code/Network/NetworkAPI/NetworkSession.cpp index 35352243..c7bf15ba 100644 --- a/Code/Network/NetworkAPI/NetworkSession.cpp +++ b/Code/Network/NetworkAPI/NetworkSession.cpp @@ -16,7 +16,6 @@ using namespace Oyster::Network; struct NetworkSession::PrivateSessionData { - Utility::DynamicMemory::DynamicArray clients; NetworkClient::ClientEventFunction messageCallback; std::mutex clientListLock; NetworkSession* owner; //Where clients end up when session is closed. @@ -34,7 +33,7 @@ NetworkSession::NetworkSession() {} NetworkSession::NetworkSession(const NetworkSession& orig) { - this->data->clients = orig.data->clients; + this->clients = orig.clients; this->data->owner = orig.data->owner; this->clientCount = orig.clientCount; this->data->id = orig.data->id; @@ -43,7 +42,7 @@ NetworkSession::NetworkSession(const NetworkSession& orig) const NetworkSession& NetworkSession::operator=(const NetworkSession& orig) { - this->data->clients = orig.data->clients; + this->clients = orig.clients; this->data->owner = orig.data->owner; this->clientCount = orig.clientCount; this->data->id = orig.data->id; @@ -54,7 +53,7 @@ const NetworkSession& NetworkSession::operator=(const NetworkSession& orig) NetworkSession::~NetworkSession() { - this->data->clients.Clear(); + this->clients.Clear(); this->clientCount = 0; this->data->messageCallback = 0; delete this->data; @@ -63,9 +62,9 @@ NetworkSession::~NetworkSession() void NetworkSession::ProcessClients() { - for (unsigned int i = 0; i < this->data->clients.Size(); i++) + for (unsigned int i = 0; i < this->clients.Size(); i++) { - if(this->data->clients[i]) this->data->clients[i]->Update(); + if(this->clients[i]) this->clients[i]->Update(); } } @@ -74,19 +73,19 @@ 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++) + for (unsigned int i = 0; (k == -1) && i < this->clients.Size(); i++) { - if(!this->data->clients[i]->IsConnected()) //TODO: Dont check connection status, check more general status.. + if(!this->clients[i]->IsConnected()) //TODO: Dont check connection status, check more general status.. k = i; } if(k == -1) { - this->data->clients.Push(client); + this->clients.Push(client); } else { - this->data->clients[k] = client; + this->clients[k] = client; } this->clientCount++; @@ -101,18 +100,18 @@ void NetworkSession::DetachAll() { if(this->data->owner) { - for (unsigned int i = 0; i < this->data->clients.Size(); i++) + for (unsigned int i = 0; i < this->clients.Size(); i++) { - this->data->owner->Attach(this->data->clients[i]); - this->data->clients[i] = 0; + this->data->owner->Attach(this->clients[i]); + this->clients[i] = 0; } } else { - for (unsigned int i = 0; i < this->data->clients.Size(); i++) + for (unsigned int i = 0; i < this->clients.Size(); i++) { - this->data->clients[i]->Disconnect(); - this->data->clients[i] = 0; + this->clients[i]->Disconnect(); + this->clients[i] = 0; } } this->clientCount = 0; @@ -124,12 +123,12 @@ NetClient NetworkSession::Detach(const NetworkClient* client) this->data->clientListLock.lock(); - for (unsigned int i = 0; i < this->data->clients.Size(); i++) + for (unsigned int i = 0; i < this->clients.Size(); i++) { - if(this->data->clients[i] && this->data->clients[0]->GetID() == client->GetID()) + if(this->clients[i] && this->clients[0]->GetID() == client->GetID()) { - val = this->data->clients[i]; - this->data->clients[i] = 0; + val = this->clients[i]; + this->clients[i] = 0; this->clientCount--; } } @@ -145,12 +144,12 @@ NetClient NetworkSession::Detach(short ID) this->data->clientListLock.lock(); - for (unsigned int i = 0; i < this->data->clients.Size(); i++) + for (unsigned int i = 0; i < this->clients.Size(); i++) { - if(this->data->clients[i] && this->data->clients[0]->GetID() == ID) + if(this->clients[i] && this->clients[0]->GetID() == ID) { - val = this->data->clients[i]; - this->data->clients[i] = 0; + val = this->clients[i]; + this->clients[i] = 0; this->clientCount--; } } @@ -166,12 +165,12 @@ NetClient NetworkSession::Detach() this->data->clientListLock.lock(); - for (unsigned int i = 0; i < this->data->clients.Size(); i++) + for (unsigned int i = 0; i < this->clients.Size(); i++) { - if(this->data->clients[i]) + if(this->clients[i]) { - val = this->data->clients[i]; - this->data->clients[i] = 0; + val = this->clients[i]; + this->clients[i] = 0; this->clientCount--; break; } @@ -185,11 +184,11 @@ NetClient NetworkSession::Detach() bool NetworkSession::Send(Oyster::Network::CustomNetProtocol& protocol) { bool returnValue = false; - for (unsigned int i = 0; i < this->data->clients.Size(); i++) + for (unsigned int i = 0; i < this->clients.Size(); i++) { - if(this->data->clients[i]) + if(this->clients[i]) { - this->data->clients[i]->Send(&protocol); + this->clients[i]->Send(&protocol); returnValue = true; } } @@ -199,11 +198,11 @@ bool NetworkSession::Send(Oyster::Network::CustomNetProtocol& protocol) bool NetworkSession::Send(Oyster::Network::CustomNetProtocol& protocol, int ID) { - for (unsigned int i = 0; i < this->data->clients.Size(); i++) + for (unsigned int i = 0; i < this->clients.Size(); i++) { - if(this->data->clients[i] && this->data->clients[i]->GetID() == ID) + if(this->clients[i] && this->clients[i]->GetID() == ID) { - this->data->clients[i]->Send(&protocol); + this->clients[i]->Send(&protocol); return true; } } @@ -214,17 +213,17 @@ void NetworkSession::CloseSession(bool dissconnectClients) { this->data->clientListLock.lock(); - for (unsigned int i = 0; i < this->data->clients.Size(); i++) + for (unsigned int i = 0; i < this->clients.Size(); i++) { - if(this->data->clients[i]) + if(this->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.. + if(dissconnectClients) this->clients[i]->Disconnect(); + else if(this->data->owner) this->data->owner->Attach(this->clients[i]); + else this->clients[i]->Disconnect(); //Idiot check, clients have to go somewhere.. } } - this->data->clients.Clear(); + this->clients.Clear(); this->clientCount = 0; this->data->clientListLock.unlock(); diff --git a/Code/Network/NetworkAPI/NetworkSession.h b/Code/Network/NetworkAPI/NetworkSession.h index ffe047a4..b44f5c61 100644 --- a/Code/Network/NetworkAPI/NetworkSession.h +++ b/Code/Network/NetworkAPI/NetworkSession.h @@ -6,6 +6,9 @@ //warning C4150: deletion of pointer to incomplete type, no destructor called #pragma warning(disable : 4150) +//needs to have dll-interface to be used by clients of class 'Oyster::Network::NetworkSession' +#pragma warning(disable : 4251) + #include "NetworkAPI_Preprocessor.h" #include "NetworkServerEventStruct.h" @@ -18,6 +21,7 @@ namespace Oyster namespace Network { typedef Utility::DynamicMemory::SmartPointer NetClient; + typedef Utility::DynamicMemory::DynamicArray NetClientList; class NET_API_EXPORT NetworkSession { public: @@ -92,6 +96,9 @@ namespace Oyster */ virtual void ClientEventCallback(NetEvent e) = 0; + protected: + NetClientList clients; + private: int clientCount; struct PrivateSessionData; From 05e24d051417bb8c2f88b769aff57a155846ae8d Mon Sep 17 00:00:00 2001 From: lindaandersson Date: Thu, 30 Jan 2014 14:17:50 +0100 Subject: [PATCH 19/23] GL - uppdate server --- Code/Game/DanBiasGame/GameClientState/GameState.cpp | 2 ++ Code/Game/DanBiasGame/GameClientState/LobbyState.cpp | 8 +++++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Code/Game/DanBiasGame/GameClientState/GameState.cpp b/Code/Game/DanBiasGame/GameClientState/GameState.cpp index 437a40f4..2e04959d 100644 --- a/Code/Game/DanBiasGame/GameClientState/GameState.cpp +++ b/Code/Game/DanBiasGame/GameClientState/GameState.cpp @@ -5,6 +5,7 @@ #include #include "NetworkClient.h" #include "Camera.h" +#include using namespace DanBias::Client; @@ -143,6 +144,7 @@ bool GameState::InitCamera(Oyster::Math::Float3 startPos) GameClientState::ClientState GameState::Update(float deltaTime, InputClass* KeyInput) { + DanBias::GameServerAPI::ServerUpdate(); switch (privData->state) { case gameStateState_loading: diff --git a/Code/Game/DanBiasGame/GameClientState/LobbyState.cpp b/Code/Game/DanBiasGame/GameClientState/LobbyState.cpp index d30fcd59..260bf1be 100644 --- a/Code/Game/DanBiasGame/GameClientState/LobbyState.cpp +++ b/Code/Game/DanBiasGame/GameClientState/LobbyState.cpp @@ -4,6 +4,7 @@ #include "C_obj/C_Player.h" #include "C_obj/C_StaticObj.h" #include "C_obj/C_DynamicObj.h" +#include using namespace DanBias::Client; @@ -86,9 +87,14 @@ GameClientState::ClientState LobbyState::Update(float deltaTime, InputClass* Key // update animation // send data to server // check data from server + DanBias::GameServerAPI::ServerUpdate(); if( KeyInput->IsKeyPressed(DIK_G)) - return ClientState_Game; + { + DanBias::GameServerAPI::GameStart(); + return ClientState_Game; + } + return ClientState_Same; } bool LobbyState::Render() From 4167b6a93b431c5fbfab4172d9e46e7116e547cc Mon Sep 17 00:00:00 2001 From: Sam Mario Svensson Date: Thu, 30 Jan 2014 15:02:02 +0100 Subject: [PATCH 20/23] GL- merge fix --- Code/Game/aDanBiasGameLauncher/Launcher.cpp | 2 +- Code/Network/NetworkAPI/NetworkSession.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Code/Game/aDanBiasGameLauncher/Launcher.cpp b/Code/Game/aDanBiasGameLauncher/Launcher.cpp index e13baff1..f05f021d 100644 --- a/Code/Game/aDanBiasGameLauncher/Launcher.cpp +++ b/Code/Game/aDanBiasGameLauncher/Launcher.cpp @@ -57,7 +57,7 @@ int WINAPI WinMain( HINSTANCE hinst, HINSTANCE prevInst, PSTR cmdLine, int cmdSh return cmdShow; } - serverThread = std::thread(ServerFnc); + //serverThread = std::thread(ServerFnc); Sleep(200); diff --git a/Code/Network/NetworkAPI/NetworkSession.h b/Code/Network/NetworkAPI/NetworkSession.h index b44f5c61..2ef1130c 100644 --- a/Code/Network/NetworkAPI/NetworkSession.h +++ b/Code/Network/NetworkAPI/NetworkSession.h @@ -13,7 +13,7 @@ #include "NetworkAPI_Preprocessor.h" #include "NetworkServerEventStruct.h" #include "NetworkClient.h" -#include "Utilities.h" +#include "..\Misc\Utilities.h" #include "DynamicArray.h" namespace Oyster From 0aa39275e38f67f34262d0410c8a197cfa7deb29 Mon Sep 17 00:00:00 2001 From: lindaandersson Date: Thu, 30 Jan 2014 15:20:35 +0100 Subject: [PATCH 21/23] GL - only client with server update server. Update recieverfunction --- Code/Game/DanBiasGame/DanBiasGame_Impl.cpp | 16 ++++++++++------ .../GameClientState/GameClientState.h | 1 + .../DanBiasGame/GameClientState/GameState.cpp | 1 - .../DanBiasGame/GameClientState/LobbyState.cpp | 2 +- .../DanBiasGame/GameClientState/LoginState.cpp | 2 +- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp index 64526c0b..de790e8a 100644 --- a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp +++ b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp @@ -8,6 +8,7 @@ #include "GameClientState\LoginState.h" #include #include "NetworkClient.h" +#include #include "../WindowManager/WindowShell.h" #include "L_inputClass.h" @@ -38,6 +39,7 @@ namespace DanBias InputClass* inputObj; Utility::WinTimer* timer; GameRecieverObject* recieverObj; + bool serverOwner; } data; #pragma endregion @@ -62,13 +64,8 @@ namespace DanBias return DanBiasClientReturn_Error; m_data->recieverObj = new GameRecieverObject; - /*m_data->recieverObj->Connect(desc.port, desc.IP); + m_data->serverOwner = false; - if (!m_data->recieverObj->IsConnected()) - { - // failed to connect - return DanBiasClientReturn_Error; - }*/ // Start in lobby state m_data->recieverObj->gameClientState = new Client::LoginState(); if(!m_data->recieverObj->gameClientState->Init(m_data->recieverObj)) @@ -133,8 +130,13 @@ namespace DanBias HRESULT DanBiasGame::Update(float deltaTime) { + m_data->recieverObj->Update(); m_data->inputObj->Update(); + if(m_data->serverOwner) + { + DanBias::GameServerAPI::ServerUpdate(); + } DanBias::Client::GameClientState::ClientState state = DanBias::Client::GameClientState::ClientState_Same; state = m_data->recieverObj->gameClientState->Update(deltaTime, m_data->inputObj); @@ -147,6 +149,8 @@ namespace DanBias switch (state) { + case Client::GameClientState::ClientState_LobbyCreated: + m_data->serverOwner = true; case Client::GameClientState::ClientState_Lobby: m_data->recieverObj->gameClientState = new Client::LobbyState(); break; diff --git a/Code/Game/DanBiasGame/GameClientState/GameClientState.h b/Code/Game/DanBiasGame/GameClientState/GameClientState.h index 28a671ba..a369233a 100644 --- a/Code/Game/DanBiasGame/GameClientState/GameClientState.h +++ b/Code/Game/DanBiasGame/GameClientState/GameClientState.h @@ -53,6 +53,7 @@ public: { ClientState_Login, ClientState_Lobby, + ClientState_LobbyCreated, ClientState_Game, ClientState_Same, }; diff --git a/Code/Game/DanBiasGame/GameClientState/GameState.cpp b/Code/Game/DanBiasGame/GameClientState/GameState.cpp index 2e04959d..ee069919 100644 --- a/Code/Game/DanBiasGame/GameClientState/GameState.cpp +++ b/Code/Game/DanBiasGame/GameClientState/GameState.cpp @@ -144,7 +144,6 @@ bool GameState::InitCamera(Oyster::Math::Float3 startPos) GameClientState::ClientState GameState::Update(float deltaTime, InputClass* KeyInput) { - DanBias::GameServerAPI::ServerUpdate(); switch (privData->state) { case gameStateState_loading: diff --git a/Code/Game/DanBiasGame/GameClientState/LobbyState.cpp b/Code/Game/DanBiasGame/GameClientState/LobbyState.cpp index 260bf1be..938add5f 100644 --- a/Code/Game/DanBiasGame/GameClientState/LobbyState.cpp +++ b/Code/Game/DanBiasGame/GameClientState/LobbyState.cpp @@ -87,7 +87,7 @@ GameClientState::ClientState LobbyState::Update(float deltaTime, InputClass* Key // update animation // send data to server // check data from server - DanBias::GameServerAPI::ServerUpdate(); + if( KeyInput->IsKeyPressed(DIK_G)) { diff --git a/Code/Game/DanBiasGame/GameClientState/LoginState.cpp b/Code/Game/DanBiasGame/GameClientState/LoginState.cpp index 8fa4936d..cd1e4663 100644 --- a/Code/Game/DanBiasGame/GameClientState/LoginState.cpp +++ b/Code/Game/DanBiasGame/GameClientState/LoginState.cpp @@ -103,7 +103,7 @@ GameClientState::ClientState LoginState::Update(float deltaTime, InputClass* Key // failed to connect return ClientState_Same; } - return ClientState_Lobby; + return ClientState_LobbyCreated; } // join game if( KeyInput->IsKeyPressed(DIK_J)) From 3b8a8e9c099815977f6772b133d3f1dedc949c58 Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Thu, 30 Jan 2014 23:22:46 +0100 Subject: [PATCH 22/23] Network - Fixed receiving parts of packages. --- Code/Network/NetworkAPI/NetworkClient.cpp | 89 ++++++++++++++++++- .../NetworkDependencies/OysterByte.cpp | 30 ++++++- Code/Network/NetworkDependencies/OysterByte.h | 5 +- 3 files changed, 117 insertions(+), 7 deletions(-) diff --git a/Code/Network/NetworkAPI/NetworkClient.cpp b/Code/Network/NetworkAPI/NetworkClient.cpp index 9539cdeb..fa9ad1af 100644 --- a/Code/Network/NetworkAPI/NetworkClient.cpp +++ b/Code/Network/NetworkAPI/NetworkClient.cpp @@ -16,11 +16,15 @@ #include "../../Misc/Utilities.h" #include "../../Misc/Thread/IThreadObject.h" #include "../../Misc/Thread/OysterThread.h" +#include "../../Misc/Packing/Packing.h" + +#include using namespace Oyster::Network; using namespace Oyster::Thread; using namespace Utility::DynamicMemory; using namespace Utility::Container; +using namespace std; /************************************* PrivateData @@ -35,11 +39,12 @@ struct NetworkClient::PrivateData : public IThreadObject Translator translator; OysterThread thread; + OysterByte recieveBuffer; //Message queue for sending and recieving ThreadSafeQueue sendQueue; ThreadSafeQueue> recieveQueue; - + //ID static unsigned int currID; const unsigned int ID; @@ -66,7 +71,7 @@ struct NetworkClient::PrivateData : public IThreadObject bool DoWork() override { if(!this->connection.IsConnected()) return false; - + Send(); Recv(); @@ -82,6 +87,7 @@ struct NetworkClient::PrivateData : public IThreadObject CustomNetProtocol p = this->sendQueue.Pop(); this->translator.Pack(temp, p); errorCode = this->connection.Send(temp); + if(errorCode != 0) { CEA parg; @@ -103,9 +109,13 @@ struct NetworkClient::PrivateData : public IThreadObject if(errorCode == 0 && temp.GetSize()) { + HandleRecievedData(temp); + + + /* Replaced with EmptyOutbufferedQueue() and HandleRecievedData(OysterByte) CustomNetProtocol protocol; bool ok = this->translator.Unpack(protocol, temp); - + //Check if the protocol was unpacked correctly if(ok) { @@ -114,7 +124,8 @@ struct NetworkClient::PrivateData : public IThreadObject parg.data.protocol = protocol; NetEvent e = { this->parent, parg }; this->recieveQueue.Push(e); - } + }*/ + } //else //{ @@ -127,6 +138,76 @@ struct NetworkClient::PrivateData : public IThreadObject return errorCode; } + + void HandleRecievedData(OysterByte& data) + { + //Loop through all packages that was recieved and add them to the queue. + unsigned int size = 0; + + Oyster::Network::OysterByte msg; + + //If there is part of a message in the buffer. + if(recieveBuffer.GetSize() > 0) + { + //cout << "the buffer size: " << recvBuffer.GetSize() < size) + { + msg = recieveBuffer; + recieveBuffer.Clear(); + size -= msg.GetSize(); + msg.AppendPartOfArray(data, 0, size); + UnpackMessageAndAddToQueue(msg); + } + else if(temp + data.GetSize() == size) + { + msg = recieveBuffer; + recieveBuffer.Clear(); + size -= msg.GetSize(); + msg += data; + UnpackMessageAndAddToQueue(msg); + return; + } + else + { + recieveBuffer += data; + size = data.GetSize(); + return; + } + } + + for(unsigned int i = size; i < data.GetSize(); i += size) + { + size = Oyster::Packing::Unpacki(&data.GetByteArray()[i]); + if(i+size > data.GetSize()) + { + //Add it to the recvBuffer instead. + recieveBuffer.AppendPartOfArray(data, i, data.GetSize()); + break; + } + msg.Clear(); + msg.AppendPartOfArray(data, i, i+size); + UnpackMessageAndAddToQueue(msg); + } + } + + void UnpackMessageAndAddToQueue(OysterByte& msg) + { + CustomNetProtocol protocol; + bool ok = this->translator.Unpack(protocol, msg); + + //Check if the protocol was unpacked correctly + if(ok) + { + CEA parg; + parg.type = CEA::EventType_ProtocolRecieved; + parg.data.protocol = protocol; + NetEvent e = { this->parent, parg }; + this->recieveQueue.Push(e); + } + } }; unsigned int NetworkClient::PrivateData::currID = 0; diff --git a/Code/Network/NetworkDependencies/OysterByte.cpp b/Code/Network/NetworkDependencies/OysterByte.cpp index ac6923a2..7f64a30d 100644 --- a/Code/Network/NetworkDependencies/OysterByte.cpp +++ b/Code/Network/NetworkDependencies/OysterByte.cpp @@ -48,7 +48,7 @@ void OysterByte::Resize(unsigned int cap) } } -int OysterByte::GetSize() +unsigned int OysterByte::GetSize() { return size; } @@ -60,7 +60,7 @@ unsigned char* OysterByte::GetByteArray() void OysterByte::AddSize(unsigned int size) { - int newCapacity = this->size + size; + unsigned int newCapacity = this->size + size; if(newCapacity >= capacity) { @@ -81,6 +81,32 @@ void OysterByte::SetSize(unsigned int size) this->size = size; } +void OysterByte::AppendPartOfArray(OysterByte& source, unsigned int startIndex, unsigned int endIndex) +{ + if(startIndex < 0 && startIndex >= endIndex) + return; + if(endIndex > source.size) + return; + + unsigned int totalSize = endIndex - startIndex; + totalSize += size; + + //Make sure the new data can fit in the array. + if(totalSize > capacity) + { + IncreaseCapacity(totalSize); + } + + //Copy over new data. + for(unsigned int i = size; i < totalSize; i++) + { + byteArray[i] = source.byteArray[startIndex++]; + } + + //Set the new size + size = totalSize; +} + OysterByte& OysterByte::operator =(const OysterByte& obj) { delete[] this->byteArray; diff --git a/Code/Network/NetworkDependencies/OysterByte.h b/Code/Network/NetworkDependencies/OysterByte.h index 4f6f4557..a3c0dfaf 100644 --- a/Code/Network/NetworkDependencies/OysterByte.h +++ b/Code/Network/NetworkDependencies/OysterByte.h @@ -25,7 +25,7 @@ namespace Oyster //Resizes the array with, it does not keep anything in it. void Resize(unsigned int cap); - int GetSize(); + unsigned int GetSize(); unsigned char* GetByteArray(); void AddSize(unsigned int size); @@ -34,6 +34,9 @@ namespace Oyster //Only sets the private variable 'size' void SetSize(unsigned int size); + //Copies over a part of the addFrom array and adds it to the end of this array. + void AppendPartOfArray(OysterByte& source, unsigned int startIndex, unsigned int endIndex); + OysterByte& operator =(const OysterByte& obj); operator char*(); From 3c1eed0f3f51d1e685c48e087f48900459b7fc87 Mon Sep 17 00:00:00 2001 From: Pontus Fransson Date: Thu, 30 Jan 2014 23:23:37 +0100 Subject: [PATCH 23/23] GL - Added LanMenuState --- Code/Game/DanBiasGame/DanBiasGame.vcxproj | 2 + Code/Game/DanBiasGame/DanBiasGame_Impl.cpp | 1 + .../GameClientState/LanMenuState.cpp | 164 ++++++++++++++++++ .../GameClientState/LanMenuState.h | 37 ++++ 4 files changed, 204 insertions(+) create mode 100644 Code/Game/DanBiasGame/GameClientState/LanMenuState.cpp create mode 100644 Code/Game/DanBiasGame/GameClientState/LanMenuState.h diff --git a/Code/Game/DanBiasGame/DanBiasGame.vcxproj b/Code/Game/DanBiasGame/DanBiasGame.vcxproj index f1cbf970..01a92481 100644 --- a/Code/Game/DanBiasGame/DanBiasGame.vcxproj +++ b/Code/Game/DanBiasGame/DanBiasGame.vcxproj @@ -204,6 +204,7 @@ + @@ -217,6 +218,7 @@ + diff --git a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp index 64526c0b..522470cc 100644 --- a/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp +++ b/Code/Game/DanBiasGame/DanBiasGame_Impl.cpp @@ -6,6 +6,7 @@ #include "GameClientState\GameState.h" #include "GameClientState\LobbyState.h" #include "GameClientState\LoginState.h" +#include "GameClientState\LanMenuState.h" #include #include "NetworkClient.h" diff --git a/Code/Game/DanBiasGame/GameClientState/LanMenuState.cpp b/Code/Game/DanBiasGame/GameClientState/LanMenuState.cpp new file mode 100644 index 00000000..fdb02fa9 --- /dev/null +++ b/Code/Game/DanBiasGame/GameClientState/LanMenuState.cpp @@ -0,0 +1,164 @@ +#include "LanMenuState.h" + +#include "C_obj/C_Player.h" +#include "C_obj/C_StaticObj.h" +#include "C_obj/C_DynamicObj.h" +#include "DllInterfaces/GFXAPI.h" + +#include + +using namespace DanBias::Client; + +struct LanMenuState::myData +{ + myData(){} + Oyster::Math3D::Float4x4 view; + Oyster::Math3D::Float4x4 proj; + C_Object* object[2]; + int modelCount; + // UI object + // game client* +}privData; + +LanMenuState::LanMenuState() +{ + +} + +LanMenuState::~LanMenuState() +{ + +} + +bool LanMenuState::Init(Oyster::Network::NetworkClient* nwClient) +{ + privData = new myData(); + this->nwClient = nwClient; + // load models + LoadModels(L"UImodels.txt"); + InitCamera(Oyster::Math::Float3(0,0,5.4f)); + + return true; +} + +bool LanMenuState::LoadModels(std::wstring file) +{ + Oyster::Graphics::Definitions::Pointlight plight; + plight.Pos = Oyster::Math::Float3(-2,3,0); + plight.Color = Oyster::Math::Float3(0,1,0); + plight.Radius = 10; + plight.Bright = 1; + Oyster::Graphics::API::AddLight(plight); + // open file + // read file + // init models + privData->modelCount = 2; + + ModelInitData modelData; + + modelData.world = Oyster::Math3D::Float4x4::identity; + modelData.visible = true; + modelData.modelPath = L"..\\Content\\Models\\box_2.dan"; + // load models + privData->object[0] = new C_StaticObj(); + privData->object[0]->Init(modelData); + + Oyster::Math3D::Float4x4 translate = Oyster::Math3D::TranslationMatrix(Oyster::Math::Float3(-2,-2,-2)); + modelData.world = modelData.world * translate; + + privData->object[1] = new C_DynamicObj(); + privData->object[1]->Init(modelData); + return true; +} + +bool LanMenuState::InitCamera(Oyster::Math::Float3 startPos) +{ + privData->proj = Oyster::Math3D::ProjectionMatrix_Perspective(Oyster::Math::pi/2,1024.0f/768.0f,.1f,1000); + //privData->proj = Oyster::Math3D::ProjectionMatrix_Orthographic(1024, 768, 1, 1000); + Oyster::Graphics::API::SetProjection(privData->proj); + + privData->view = Oyster::Math3D::OrientationMatrix_LookAtDirection(Oyster::Math::Float3(0,0,-1),Oyster::Math::Float3(0,1,0),startPos); + privData->view = Oyster::Math3D::InverseOrientationMatrix(privData->view); + return true; +} + +GameClientState::ClientState LanMenuState::Update(float deltaTime, InputClass* KeyInput) +{ + // create game + if( KeyInput->IsKeyPressed(DIK_C)) + { + DanBias::GameServerAPI::ServerInitDesc desc; + + DanBias::GameServerAPI::ServerInitiate(desc); + DanBias::GameServerAPI::ServerStart(); + // my ip + nwClient->Connect(15151, "127.0.0.1"); + + if (!nwClient->IsConnected()) + { + // failed to connect + return ClientState_Same; + } + return ClientState_Lobby; + } + // join game + if( KeyInput->IsKeyPressed(DIK_J)) + { + // game ip + nwClient->Connect(15151, "194.47.150.56"); + + if (!nwClient->IsConnected()) + { + // failed to connect + return ClientState_Same; + } + return ClientState_Lobby; + } + return ClientState_Same; +} + +bool LanMenuState::Render() +{ + Oyster::Graphics::API::SetView(privData->view); + Oyster::Graphics::API::SetProjection( privData->proj); + + + Oyster::Graphics::API::NewFrame(); + // render objects + for (int i = 0; i < privData->modelCount; i++) + { + privData->object[i]->Render(); + } + + // render effects + + // render lights + + Oyster::Graphics::API::EndFrame(); + return true; +} + +bool LanMenuState::Release() +{ + for (int i = 0; i < privData->modelCount; i++) + { + privData->object[i]->Release(); + delete privData->object[i]; + privData->object[i] = NULL; + } + + delete privData; + privData = NULL; + + return true; +} + +void LanMenuState::Protocol(ProtocolStruct* protocolStruct) +{ + if((PlayerName*)protocolStruct) + PlayerJoinProtocol((PlayerName*)protocolStruct); +} +void LanMenuState::PlayerJoinProtocol(PlayerName* name) +{ + +} \ No newline at end of file diff --git a/Code/Game/DanBiasGame/GameClientState/LanMenuState.h b/Code/Game/DanBiasGame/GameClientState/LanMenuState.h new file mode 100644 index 00000000..5182c80c --- /dev/null +++ b/Code/Game/DanBiasGame/GameClientState/LanMenuState.h @@ -0,0 +1,37 @@ +#ifndef LAN_MENU_STATE_H +#define LAN_MENU_STATE_H + +#include "GameClientState.h" +#include "OysterMath.h" + +namespace DanBias +{ + namespace Client + { + class LanMenuState : public GameClientState + { + public: + LanMenuState(); + virtual ~LanMenuState(); + + virtual bool Init(Oyster::Network::NetworkClient* nwClient); + virtual ClientState Update(float deltaTime, InputClass* KeyInput); + + bool LoadModels(std::wstring file); + bool InitCamera(Oyster::Math::Float3 startPos); + + virtual bool Render(); + virtual bool Release(); + virtual void Protocol(ProtocolStruct* protocolStruct); + + void PlayerJoinProtocol(PlayerName* name); + + private: + Oyster::Network::NetworkClient* nwClient; + struct myData; + myData* privData; + }; + } +} + +#endif \ No newline at end of file