From cb8fee92275e0b9c3c5a520f305ce235adb4e666 Mon Sep 17 00:00:00 2001 From: dean11 Date: Fri, 6 Dec 2013 10:00:58 +0100 Subject: [PATCH] NETWORK - Added network custom protocol --- .../NetworkProtocol/CustomNetProtocol.cpp | 296 ++++++++++++++++++ .../NetworkProtocol/CustomNetProtocol.h | 118 +++++++ .../NetworkProtocol/NetworkProtocol.vcxproj | 171 ++++++++++ 3 files changed, 585 insertions(+) create mode 100644 Code/Network/NetworkProtocol/CustomNetProtocol.cpp create mode 100644 Code/Network/NetworkProtocol/CustomNetProtocol.h create mode 100644 Code/Network/NetworkProtocol/NetworkProtocol.vcxproj diff --git a/Code/Network/NetworkProtocol/CustomNetProtocol.cpp b/Code/Network/NetworkProtocol/CustomNetProtocol.cpp new file mode 100644 index 00000000..ef9d3fc2 --- /dev/null +++ b/Code/Network/NetworkProtocol/CustomNetProtocol.cpp @@ -0,0 +1,296 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// +#include "CustomNetProtocol.h" +#include + +using namespace Network; + +enum NetContainerType +{ + NetContainer_Array, + NetContainer_Single, + NetContainer_UNKNOWN, +}; +struct NetAttributeArray +{ + NetAttributeContainer *value; + int count; + NetAttributeArray() + :value(0) + , count(0) + {} +}; +union NetAttributeData +{ + struct { NetAttributeContainer singleVal; }; + struct { NetAttributeArray arrayVal; }; +}; +struct NetDataValue +{ + NetContainerType containerType; + NetAttributeData attributeData; + NetDataValue() + { + //memset(&attributeData, 0, sizeof(NetAttributeData)); + containerType = NetContainer_UNKNOWN; + attributeData.singleVal.type = NetAttributeType_UNKNOWN; + } +}; + + + +struct CustomNetProtocol::PrivateData +{ + std::map attributes; + //DataValue *val; + //unsigned int size; + + PrivateData() + { + //val = new DataValue[_size]; + //size = _size; + } + ~PrivateData() + { + for (auto i = attributes.begin(); i != attributes.end(); i++) + { + RemoveAttribute(i->first); + } + } + void RemoveAttribute(int ID) + { + auto i = attributes.find(ID); + if(i == attributes.end()) return; + + if(i->second.containerType == NetContainer_Single) + { + switch (i->second.attributeData.singleVal.type) + { + case NetAttributeType_CharArray: + delete [] i->second.attributeData.singleVal.value.netCharPtr; + break; + } + } + else if(i->second.containerType == NetContainer_Array) + { + } + + } + + //Do network stuff +}; + + +CustomNetProtocol::CustomNetProtocol() +{ + this->privateData = new PrivateData(); +} +CustomNetProtocol::~CustomNetProtocol() +{ + delete this->privateData; +} +NetAttributeContainer* CustomNetProtocol::operator[](int ID) +{ + NetAttributeContainer *retVal = 0; + + if(this->privateData->attributes.find(ID) == this->privateData->attributes.end()) + return retVal; + + if(this->privateData->attributes[ID].containerType == NetContainer_Single) + retVal = &this->privateData->attributes[ID].attributeData.singleVal; + + if(this->privateData->attributes[ID].containerType == NetContainer_Array) + retVal = this->privateData->attributes[ID].attributeData.arrayVal.value; + + return retVal; +} + + +NetAttributeContainer* CustomNetProtocol::BuildValue(NetAttributeContainer val, short ID) +{ + if(val.type == NetAttributeType_UNKNOWN) return 0; + + this->privateData->RemoveAttribute(ID); + + this->privateData->attributes[ID].attributeData.singleVal = val; + + return &this->privateData->attributes[ID].attributeData.singleVal; +} +NetAttributeContainer* CustomNetProtocol::BuildValue(NetAttributeValue value, NetAttributeType type, short ID) +{ + if(type == NetAttributeType_UNKNOWN) return 0; + + this->privateData->RemoveAttribute(ID); + + this->privateData->attributes[ID].attributeData.singleVal.value = value; + this->privateData->attributes[ID].attributeData.singleVal.type = type; + + return &this->privateData->attributes[ID].attributeData.singleVal; +} +NetAttributeContainer* CustomNetProtocol::BuildValue(const bool & attribute, short attributeId) +{ + this->privateData->RemoveAttribute(attributeId); + + this->privateData->attributes[attributeId].attributeData.singleVal.value.netBool = attribute; + this->privateData->attributes[attributeId].attributeData.singleVal.type = NetAttributeType_Bool; + this->privateData->attributes[attributeId].containerType = NetContainer_Single; + + return &this->privateData->attributes[attributeId].attributeData.singleVal; +} +NetAttributeContainer* CustomNetProtocol::BuildValue(const char & attribute, short attributeId) +{ + this->privateData->RemoveAttribute(attributeId); + + this->privateData->attributes[attributeId].attributeData.singleVal.value.netChar = attribute; + this->privateData->attributes[attributeId].attributeData.singleVal.type = NetAttributeType_Char; + this->privateData->attributes[attributeId].containerType = NetContainer_Single; + + return &this->privateData->attributes[attributeId].attributeData.singleVal; +} +NetAttributeContainer* CustomNetProtocol::BuildValue(const unsigned char & attribute, short attributeId) +{ + this->privateData->RemoveAttribute(attributeId); + + this->privateData->attributes[attributeId].attributeData.singleVal.value.netUChar = attribute; + this->privateData->attributes[attributeId].attributeData.singleVal.type = NetAttributeType_UnsignedChar; + this->privateData->attributes[attributeId].containerType = NetContainer_Single; + + return &this->privateData->attributes[attributeId].attributeData.singleVal; +} +NetAttributeContainer* CustomNetProtocol::BuildValue(const short & attribute, short attributeId) +{ + this->privateData->RemoveAttribute(attributeId); + + this->privateData->attributes[attributeId].attributeData.singleVal.value.netShort = attribute; + this->privateData->attributes[attributeId].attributeData.singleVal.type = NetAttributeType_Short; + this->privateData->attributes[attributeId].containerType = NetContainer_Single; + + return &this->privateData->attributes[attributeId].attributeData.singleVal; +} +NetAttributeContainer* CustomNetProtocol::BuildValue(const unsigned short & attribute, short attributeId) +{ + this->privateData->RemoveAttribute(attributeId); + + this->privateData->attributes[attributeId].attributeData.singleVal.value.netUShort = attribute; + this->privateData->attributes[attributeId].attributeData.singleVal.type = NetAttributeType_UnsignedShort; + this->privateData->attributes[attributeId].containerType = NetContainer_Single; + + return &this->privateData->attributes[attributeId].attributeData.singleVal; +} +NetAttributeContainer* CustomNetProtocol::BuildValue(const int & attribute, short attributeId) +{ + this->privateData->RemoveAttribute(attributeId); + + this->privateData->attributes[attributeId].attributeData.singleVal.value.netInt = attribute; + this->privateData->attributes[attributeId].attributeData.singleVal.type = NetAttributeType_Int; + this->privateData->attributes[attributeId].containerType = NetContainer_Single; + + return &this->privateData->attributes[attributeId].attributeData.singleVal; +} +NetAttributeContainer* CustomNetProtocol::BuildValue(const unsigned int & attribute, short attributeId) +{ + this->privateData->RemoveAttribute(attributeId); + + this->privateData->attributes[attributeId].attributeData.singleVal.value.netUInt = attribute; + this->privateData->attributes[attributeId].attributeData.singleVal.type = NetAttributeType_UnsignedInt; + this->privateData->attributes[attributeId].containerType = NetContainer_Single; + + return &this->privateData->attributes[attributeId].attributeData.singleVal; +} +NetAttributeContainer* CustomNetProtocol::BuildValue(const __int64 & attribute, short attributeId) +{ + this->privateData->RemoveAttribute(attributeId); + + this->privateData->attributes[attributeId].attributeData.singleVal.value.netInt64 = attribute; + this->privateData->attributes[attributeId].attributeData.singleVal.type = NetAttributeType_Int64; + this->privateData->attributes[attributeId].containerType = NetContainer_Single; + + return &this->privateData->attributes[attributeId].attributeData.singleVal; +} +NetAttributeContainer* CustomNetProtocol::BuildValue(const unsigned __int64 & attribute, short attributeId) +{ + this->privateData->RemoveAttribute(attributeId); + + this->privateData->attributes[attributeId].attributeData.singleVal.value.netUInt64 = attribute; + this->privateData->attributes[attributeId].attributeData.singleVal.type = NetAttributeType_UnsignedInt64; + this->privateData->attributes[attributeId].containerType = NetContainer_Single; + + return &this->privateData->attributes[attributeId].attributeData.singleVal; +} +NetAttributeContainer* CustomNetProtocol::BuildValue(const float & attribute, short attributeId) +{ + this->privateData->RemoveAttribute(attributeId); + + this->privateData->attributes[attributeId].attributeData.singleVal.value.netFloat = attribute; + this->privateData->attributes[attributeId].attributeData.singleVal.type = NetAttributeType_Float; + this->privateData->attributes[attributeId].containerType = NetContainer_Single; + + return &this->privateData->attributes[attributeId].attributeData.singleVal; +} +NetAttributeContainer* CustomNetProtocol::BuildValue(const double & attribute, short attributeId) +{ + this->privateData->RemoveAttribute(attributeId); + + this->privateData->attributes[attributeId].attributeData.singleVal.value.netDouble = attribute; + this->privateData->attributes[attributeId].attributeData.singleVal.type = NetAttributeType_Double; + this->privateData->attributes[attributeId].containerType = NetContainer_Single; + + return &this->privateData->attributes[attributeId].attributeData.singleVal; +} +NetAttributeContainer* CustomNetProtocol::BuildValue(const std::string & attribute, short attributeId) +{ + this->privateData->RemoveAttribute(attributeId); + + this->privateData->attributes[attributeId].attributeData.singleVal.value.netCharPtr = _strdup(attribute.c_str()); + this->privateData->attributes[attributeId].attributeData.singleVal.type = NetAttributeType_CharArray; + this->privateData->attributes[attributeId].containerType = NetContainer_Single; + + return &this->privateData->attributes[attributeId].attributeData.singleVal; +} +NetAttributeContainer* CustomNetProtocol::BuildValue(const char* attribute, short attributeId) +{ + this->privateData->RemoveAttribute(attributeId); + + this->privateData->attributes[attributeId].attributeData.singleVal.value.netCharPtr = _strdup(attribute); + this->privateData->attributes[attributeId].attributeData.singleVal.type = NetAttributeType_CharArray; + this->privateData->attributes[attributeId].containerType = NetContainer_Single; + + return &this->privateData->attributes[attributeId].attributeData.singleVal; +} + + +//void CustomNetProtocol::SetArrayValue(NetAttributeContainer attribute[], unsigned int attributeCount, int ID) +//{ +// if(this->privateData->attributes.find(ID) == this->privateData->attributes.end()) +// { +// this->privateData->attributes[ID] = NetDataValue(); +// } +// +// if(this->privateData->attributes[ID].containerType == NetContainer_UNKNOWN) +// this->privateData->attributes[ID].containerType = NetContainer_Array; +// +// this->privateData->attributes[ID].attributeData.arrayVal.value = attribute; +//} + +NetAttributeContainer& CustomNetProtocol::GetSingleValue(int attributeID) +{ + return this->privateData->attributes[attributeID].attributeData.singleVal; +} +//NetAttributeContainer* CustomNetProtocol::GetArrayValue(int attributeID) +//{ +// return this->privateData->attributes[attributeID].attributeData.arrayVal.value; +//} + + + + + + + + + + + + + diff --git a/Code/Network/NetworkProtocol/CustomNetProtocol.h b/Code/Network/NetworkProtocol/CustomNetProtocol.h new file mode 100644 index 00000000..2845250f --- /dev/null +++ b/Code/Network/NetworkProtocol/CustomNetProtocol.h @@ -0,0 +1,118 @@ +///////////////////////////////////////////////////////////////////// +// Created by [Dennis Andersen] [2013] +///////////////////////////////////////////////////////////////////// +#ifndef NETWORK_CUSTOM_NETWORK_PROTOCOL_H +#define NETWORK_CUSTOM_NETWORK_PROTOCOL_H + +#include + +#ifdef CUSTOM_NET_PROTOCOL_EXPORT + #define NET_PROTOCOL_EXPORT __declspec(dllexport) +#else + #define NET_PROTOCOL_EXPORT __declspec(dllimport) +#endif + +namespace Network +{ + extern "C" + { + enum NetAttributeType + { + NetAttributeType_Bool, + NetAttributeType_Char, + NetAttributeType_UnsignedChar, + NetAttributeType_Short, + NetAttributeType_UnsignedShort, + NetAttributeType_Int, + NetAttributeType_UnsignedInt, + NetAttributeType_Int64, + NetAttributeType_UnsignedInt64, + NetAttributeType_Float, + NetAttributeType_Double, + NetAttributeType_CharArray, + NetAttributeType_UNKNOWN, + }; + union NetAttributeValue + { + bool netBool; + char netChar; + unsigned char netUChar; + short netShort; + unsigned short netUShort; + int netInt; + unsigned int netUInt; + __int64 netInt64; + unsigned __int64 netUInt64; + float netFloat; + double netDouble; + char* netCharPtr; + + NetAttributeValue(){ memset(this, 0, sizeof(NetAttributeValue)); } + NetAttributeValue(bool v) : netBool (v) {} + NetAttributeValue(char v) : netChar (v) {} + NetAttributeValue(unsigned char v) : netUChar (v) {} + NetAttributeValue(short v) : netShort (v) {} + NetAttributeValue(unsigned short v) : netUShort (v) {} + NetAttributeValue(int v) : netInt (v) {} + NetAttributeValue(unsigned int v) : netUInt (v) {} + NetAttributeValue(__int64 v) : netInt64 (v) {} + NetAttributeValue(unsigned __int64 v) : netUInt64 (v) {} + NetAttributeValue(float v) : netFloat (v) {} + NetAttributeValue(double v) : netDouble (v) {} + NetAttributeValue(char* v) : netCharPtr(v) {} + }; + struct NetAttributeContainer + { + NetAttributeType type; + NetAttributeValue value; + NetAttributeContainer() { type = NetAttributeType_UNKNOWN; } + }; + class NET_PROTOCOL_EXPORT CustomNetProtocol + { + public: + CustomNetProtocol(); + ~CustomNetProtocol(); + + NetAttributeContainer* operator[](int ID); + + NetAttributeContainer* BuildValue(NetAttributeContainer value, short attributeId); + NetAttributeContainer* BuildValue(NetAttributeValue value, NetAttributeType type, short attributeId); + NetAttributeContainer* BuildValue(const bool & attribute, short attributeId); + NetAttributeContainer* BuildValue(const char & attribute, short attributeId); + NetAttributeContainer* BuildValue(const unsigned char & attribute, short attributeId); + NetAttributeContainer* BuildValue(const short & attribute, short attributeId); + NetAttributeContainer* BuildValue(const unsigned short & attribute, short attributeId); + NetAttributeContainer* BuildValue(const int & attribute, short attributeId); + NetAttributeContainer* BuildValue(const unsigned int & attribute, short attributeId); + NetAttributeContainer* BuildValue(const __int64 & attribute, short attributeId); + NetAttributeContainer* BuildValue(const unsigned __int64 & attribute, short attributeId); + NetAttributeContainer* BuildValue(const float & attribute, short attributeId); + NetAttributeContainer* BuildValue(const double & attribute, short attributeId); + NetAttributeContainer* BuildValue(const std::string & attribute, short attributeId); + NetAttributeContainer* BuildValue(const char * attribute, short attributeId); + + //void SetArrayValue(NetAttributeContainer attribute[], unsigned int attributeCount, int attributeId); + + NetAttributeContainer& GetSingleValue(int attributeID); + //NetAttributeContainer* GetArrayValue(int attributeID); + + private: + struct PrivateData; + PrivateData* privateData; + }; + + }//End extern "C" +} //End namespace Network +#endif // !NETWORK_CUSTOM_NETWORK_PROTOCOL_H + + + + + + + + + + + + diff --git a/Code/Network/NetworkProtocol/NetworkProtocol.vcxproj b/Code/Network/NetworkProtocol/NetworkProtocol.vcxproj new file mode 100644 index 00000000..36dddd75 --- /dev/null +++ b/Code/Network/NetworkProtocol/NetworkProtocol.vcxproj @@ -0,0 +1,171 @@ + + + + + Debug + Win32 + + + Debug + x64 + + + Release + Win32 + + + Release + x64 + + + + {460D625F-2AC9-4559-B809-0BA89CEAEDF4} + Win32Proj + NetworkProtocol + NetworkProtocol + + + + DynamicLibrary + true + v110 + Unicode + + + DynamicLibrary + true + v110 + Unicode + + + DynamicLibrary + false + v110 + true + Unicode + + + DynamicLibrary + false + v110 + true + Unicode + + + + + + + + + + + + + + + + + + + true + C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) + $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)..\Bin\DLL\ + $(ProjectName)_$(PlatformShortName)D + + + true + C:\Program Files %28x86%29\Visual Leak Detector\include;$(IncludePath) + C:\Program Files %28x86%29\Visual Leak Detector\lib\Win32;$(LibraryPath) + $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)..\Bin\DLL\ + $(ProjectName)_$(PlatformShortName)D + + + false + $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)..\Bin\DLL\ + $(ProjectName)_$(PlatformShortName) + + + false + $(SolutionDir)..\Obj\$(ProjectName)\$(PlatformShortName)\$(Configuration)\ + $(SolutionDir)..\Bin\DLL\ + $(ProjectName)_$(PlatformShortName) + + + + + + Level3 + Disabled + CUSTOM_NET_PROTOCOL_EXPORT;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + + + Level3 + Disabled + CUSTOM_NET_PROTOCOL_EXPORT;WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + CUSTOM_NET_PROTOCOL_EXPORT;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + CUSTOM_NET_PROTOCOL_EXPORT;WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + \ No newline at end of file