diff --git a/TestsVS/Tests/Release/Tests.tlog/CL.command.1.tlog b/TestsVS/Tests/Release/Tests.tlog/CL.command.1.tlog new file mode 100644 index 0000000..46b134b --- /dev/null +++ b/TestsVS/Tests/Release/Tests.tlog/CL.command.1.tlog @@ -0,0 +1 @@ +ÿþ \ No newline at end of file diff --git a/TestsVS/Tests/Release/Tests.tlog/Tests.lastbuildstate b/TestsVS/Tests/Release/Tests.tlog/Tests.lastbuildstate new file mode 100644 index 0000000..3d85588 --- /dev/null +++ b/TestsVS/Tests/Release/Tests.tlog/Tests.lastbuildstate @@ -0,0 +1,2 @@ +#TargetFrameworkVersion=v4.0:PlatformToolSet=v140:EnableManagedIncrementalBuild=false:VCToolArchitecture=Native32Bit:WindowsTargetPlatformVersion=8.1 +Release|Win32|F:\VoidNet\VoidNetVS\| diff --git a/TestsVS/Tests/Release/Tests.tlog/link.command.1.tlog b/TestsVS/Tests/Release/Tests.tlog/link.command.1.tlog new file mode 100644 index 0000000..7d676d7 Binary files /dev/null and b/TestsVS/Tests/Release/Tests.tlog/link.command.1.tlog differ diff --git a/TestsVS/Tests/Release/Tests.tlog/link.read.1.tlog b/TestsVS/Tests/Release/Tests.tlog/link.read.1.tlog new file mode 100644 index 0000000..aa32f17 Binary files /dev/null and b/TestsVS/Tests/Release/Tests.tlog/link.read.1.tlog differ diff --git a/TestsVS/Tests/Release/Tests.tlog/link.write.1.tlog b/TestsVS/Tests/Release/Tests.tlog/link.write.1.tlog new file mode 100644 index 0000000..33fb381 Binary files /dev/null and b/TestsVS/Tests/Release/Tests.tlog/link.write.1.tlog differ diff --git a/TestsVS/Tests/Release/Tests.tlog/unsuccessfulbuild b/TestsVS/Tests/Release/Tests.tlog/unsuccessfulbuild new file mode 100644 index 0000000..e69de29 diff --git a/TestsVS/Tests/Tests.cpp b/TestsVS/Tests/Tests.cpp new file mode 100644 index 0000000..bde0d81 --- /dev/null +++ b/TestsVS/Tests/Tests.cpp @@ -0,0 +1,12 @@ +#include "Utility.hpp" + +#include + +int main() +{ + byte *bytes = Utility::BitConverter::FromUint8(20); + std::cout << Utility::BitConverter::ToUint8(bytes, 0) << std::endl; + getchar(); + return 0; +} + diff --git a/TestsVS/Tests/Tests.vcxproj b/TestsVS/Tests/Tests.vcxproj new file mode 100644 index 0000000..d7e1ff8 --- /dev/null +++ b/TestsVS/Tests/Tests.vcxproj @@ -0,0 +1,156 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {41663B22-3949-4570-9AB5-309363EAD56F} + Win32Proj + Tests + 8.1 + + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + Application + true + v140 + Unicode + + + Application + false + v140 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + + + + + + + Level3 + Disabled + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + F:\VoidNet\include; + + + Console + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + true + true + + + + + + + + {5172321e-ccb0-4a77-9f3d-faaf0084f434} + + + + + + \ No newline at end of file diff --git a/TestsVS/Tests/Tests.vcxproj.filters b/TestsVS/Tests/Tests.vcxproj.filters new file mode 100644 index 0000000..74f2955 --- /dev/null +++ b/TestsVS/Tests/Tests.vcxproj.filters @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/VoidNetVS/Release/Tests.iobj b/VoidNetVS/Release/Tests.iobj new file mode 100644 index 0000000..839a832 Binary files /dev/null and b/VoidNetVS/Release/Tests.iobj differ diff --git a/VoidNetVS/Release/Tests.ipdb b/VoidNetVS/Release/Tests.ipdb new file mode 100644 index 0000000..4edb2c4 Binary files /dev/null and b/VoidNetVS/Release/Tests.ipdb differ diff --git a/VoidNetVS/VoidNetVS.sln b/VoidNetVS/VoidNetVS.sln index d18a210..aabb269 100644 --- a/VoidNetVS/VoidNetVS.sln +++ b/VoidNetVS/VoidNetVS.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 14.0.25123.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "VoidNetVS", "VoidNetVS\VoidNetVS.vcxproj", "{5172321E-CCB0-4A77-9F3D-FAAF0084F434}" EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Tests", "..\TestsVS\Tests\Tests.vcxproj", "{41663B22-3949-4570-9AB5-309363EAD56F}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -21,6 +23,14 @@ Global {5172321E-CCB0-4A77-9F3D-FAAF0084F434}.Release|x64.Build.0 = Release|x64 {5172321E-CCB0-4A77-9F3D-FAAF0084F434}.Release|x86.ActiveCfg = Release|Win32 {5172321E-CCB0-4A77-9F3D-FAAF0084F434}.Release|x86.Build.0 = Release|Win32 + {41663B22-3949-4570-9AB5-309363EAD56F}.Debug|x64.ActiveCfg = Debug|x64 + {41663B22-3949-4570-9AB5-309363EAD56F}.Debug|x64.Build.0 = Debug|x64 + {41663B22-3949-4570-9AB5-309363EAD56F}.Debug|x86.ActiveCfg = Debug|Win32 + {41663B22-3949-4570-9AB5-309363EAD56F}.Debug|x86.Build.0 = Debug|Win32 + {41663B22-3949-4570-9AB5-309363EAD56F}.Release|x64.ActiveCfg = Release|x64 + {41663B22-3949-4570-9AB5-309363EAD56F}.Release|x64.Build.0 = Release|x64 + {41663B22-3949-4570-9AB5-309363EAD56F}.Release|x86.ActiveCfg = Release|Win32 + {41663B22-3949-4570-9AB5-309363EAD56F}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.command.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.command.1.tlog new file mode 100644 index 0000000..cf2b1b3 Binary files /dev/null and b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.command.1.tlog differ diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog new file mode 100644 index 0000000..3c4f5f0 Binary files /dev/null and b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog differ diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog new file mode 100644 index 0000000..f8ea4cf Binary files /dev/null and b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog differ diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/Lib-link.write.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/Lib-link.write.1.tlog new file mode 100644 index 0000000..1670021 Binary files /dev/null and b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/Lib-link.write.1.tlog differ diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/Lib.read.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/Lib.read.1.tlog new file mode 100644 index 0000000..0d32cf9 Binary files /dev/null and b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/Lib.read.1.tlog differ diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/VoidNetVS.lastbuildstate b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/VoidNetVS.lastbuildstate new file mode 100644 index 0000000..3d85588 --- /dev/null +++ b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/VoidNetVS.lastbuildstate @@ -0,0 +1,2 @@ +#TargetFrameworkVersion=v4.0:PlatformToolSet=v140:EnableManagedIncrementalBuild=false:VCToolArchitecture=Native32Bit:WindowsTargetPlatformVersion=8.1 +Release|Win32|F:\VoidNet\VoidNetVS\| diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/lib.command.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/lib.command.1.tlog new file mode 100644 index 0000000..724e276 Binary files /dev/null and b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/lib.command.1.tlog differ diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/unsuccessfulbuild b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/unsuccessfulbuild new file mode 100644 index 0000000..e69de29 diff --git a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj index ba417c9..75b4aaf 100644 --- a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj +++ b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj @@ -19,18 +19,25 @@ + + + - + + + + + diff --git a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters index 13b2038..9c8cac6 100644 --- a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters +++ b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters @@ -15,9 +15,6 @@ include - - include - include @@ -30,6 +27,21 @@ include + + include + + + include + + + include + + + include + + + include + @@ -50,6 +62,15 @@ src + + src + + + src + + + src + diff --git a/include/Callbacks.hpp b/include/Callbacks.hpp new file mode 100644 index 0000000..9a355de --- /dev/null +++ b/include/Callbacks.hpp @@ -0,0 +1,17 @@ +#ifndef CALLBACKS_HPP +#define CALLBACKS_HPP + +#ifdef _MSC_VER +#pragma once +#endif + +#include "Defs.hpp" +#include "Serializer.hpp" + +void OnMessage(byte sender, byte tag, uint16 subject, byte *data); + +void OnDisconnect(byte id); + +void OnConnect(byte id); + +#endif \ No newline at end of file diff --git a/include/Defs.hpp b/include/Defs.hpp index ac3de2d..1822fd7 100644 --- a/include/Defs.hpp +++ b/include/Defs.hpp @@ -2,10 +2,7 @@ #define DEFS_HPP #ifdef _MSC_VER - #pragma once -#endif - -#ifdef _MSC_VER +#pragma once #define WIN32_LEAN_AND_MEAN #define _WINSOCKAPI_ #include @@ -185,4 +182,14 @@ enum VoidCode VOID_COULDNT_CONNECT, }; +enum DistributionType +{ + All, + Server, + Others, + ID, + Reply, + Custom, +} + #endif // DEFS_HPP \ No newline at end of file diff --git a/include/NetworkBuffer.hpp b/include/NetworkBuffer.hpp new file mode 100644 index 0000000..fb3a850 --- /dev/null +++ b/include/NetworkBuffer.hpp @@ -0,0 +1,19 @@ +#ifndef NETWORK_BUFFER_HPP +#define NETWORK_BUFFER_HPP + +#ifdef _MSC_VER +#pragma once +#endif + +#include "Defs.hpp" + +struct NetworkBuffer +{ + NetworkBuffer(); + ~NetworkBuffer(); + + int body_size; + byte *body = nullptr; +}; + +#endif \ No newline at end of file diff --git a/include/NetworkMessage.hpp b/include/NetworkMessage.hpp index 57ebba5..47c9727 100644 --- a/include/NetworkMessage.hpp +++ b/include/NetworkMessage.hpp @@ -5,12 +5,30 @@ #pragma once #endif -class NetworkMessage -{ -public: - NetworkMessage(); +#include "Defs.hpp" +#include "NetworkBuffer.hpp" -private: +struct NetworkMessage +{ + NetworkMessage(); + NetworkMessage(uint16 sender, byte distribution_mode, uint16 destination_id, byte tag, uint16 subject, NetworkBuffer buffer); + ~NetworkMessage(); + + const NetworkMessage &EncodeMessage(); + + const NetworkMessage &NetworkMessage::DecodeMessage(const NetworkBuffer &buffer); + const NetworkMessage &NetworkMessage::DecodeMessageHeader(const NetworkBuffer &buffer); + void *DecodeMessageData(const NetworkBuffer &buffer); + + uint16 sender; + uint16 destination_id; + byte distribution_mode; + uint16 tag; + uint16 subject; + + void *data; + + NetworkBuffer buffer; }; -#endif \ No newline at end of file +#endif diff --git a/include/Serializer.hpp b/include/Serializer.hpp new file mode 100644 index 0000000..f5cdf85 --- /dev/null +++ b/include/Serializer.hpp @@ -0,0 +1,18 @@ +#ifndef SERIALIZER_HPP +#define SERIALIZER_HPP + +#ifdef _MSC_VER +#pragma once +#endif + +#include "Defs.hpp" + +#include + +class Serializer +{ + template std::array to_bytes(const T& object); + template T& from_bytes(const std::array &bytes, T& object); +}; + +#endif \ No newline at end of file diff --git a/include/TcpClient.hpp b/include/TcpClient.hpp index 4a9ed6f..d8a07a0 100644 --- a/include/TcpClient.hpp +++ b/include/TcpClient.hpp @@ -23,7 +23,7 @@ public: uint16 GetPort(); void SetPort(uint16 port); - VoidCode Connect(); // this function is used if Initialize was not called + VoidCode Connect(); char *ReceiveDataArray(); const NetworkMessage &ReceiveData(); diff --git a/include/Utility.hpp b/include/Utility.hpp index ac55726..4ead391 100644 --- a/include/Utility.hpp +++ b/include/Utility.hpp @@ -5,10 +5,24 @@ #pragma once #endif +#include "Defs.hpp" + +#include +#include + struct Utility { static void Delete(void *pointer); static void DeleteArray(void *pointer); + + struct BitConverter + { + static byte *FromUint8(uint8 number); + static uint8 ToUint8(byte *bytes, uint16 start_index); + + static byte *FromUint16(uint16 number); + static uint16 ToUint16(byte *bytes, uint16 start_index); + }; }; #endif \ No newline at end of file diff --git a/include/VoidNet.hpp b/include/VoidNet.hpp deleted file mode 100644 index ced8662..0000000 --- a/include/VoidNet.hpp +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef VOID_NET_HPP -#define VOID_NET_HPP - -#ifdef _MSC_VER -#pragma once -#endif - -#include -#include - -#endif diff --git a/include/VoidNetClient.hpp b/include/VoidNetClient.hpp new file mode 100644 index 0000000..7a74481 --- /dev/null +++ b/include/VoidNetClient.hpp @@ -0,0 +1,15 @@ +#ifndef VOID_NET_HPP +#define VOID_NET_HPP + +#ifdef _MSC_VER +#pragma once +#endif + +#include "Defs.hpp" +#include "Init.hpp" +#include "TcpClient.hpp" +#include "Callbacks.hpp" +#include "NetworkBuffer.hpp" +#include "NetworkMessage.hpp" + +#endif diff --git a/include/VoidNetServer.hpp b/include/VoidNetServer.hpp new file mode 100644 index 0000000..870304b --- /dev/null +++ b/include/VoidNetServer.hpp @@ -0,0 +1,8 @@ +#ifndef VOID_NET_SERVER_HPP +#define VOID_NET_SERVER_HPP + +#ifdef _MSC_VER +#pragma once +#endif + +#endif \ No newline at end of file diff --git a/src/NetworkBuffer.cpp b/src/NetworkBuffer.cpp new file mode 100644 index 0000000..a328b30 --- /dev/null +++ b/src/NetworkBuffer.cpp @@ -0,0 +1,11 @@ +#include "NetworkBuffer.hpp" +#include "Utility.hpp" + +NetworkBuffer::NetworkBuffer() +{ +} + +NetworkBuffer::~NetworkBuffer() +{ + Utility::DeleteArray(body); +} \ No newline at end of file diff --git a/src/NetworkMessage.cpp b/src/NetworkMessage.cpp new file mode 100644 index 0000000..1d283b9 --- /dev/null +++ b/src/NetworkMessage.cpp @@ -0,0 +1,53 @@ +#include "NetworkMessage.hpp" +#include "Utility.hpp" +#include "Serializer.hpp" + +NetworkMessage::NetworkMessage() +{ +} + +NetworkMessage::NetworkMessage(uint16 sender, byte distribution_mode, uint16 destination_id, byte tag, uint16 subject, NetworkBuffer buffer) : + sender(sender), distribution_mode(distribution_mode), destination_id(destination_id), tag(tag), subject(subject), buffer(buffer) +{ +} + +NetworkMessage::~NetworkMessage() +{ +} + +const NetworkMessage &NetworkMessage::EncodeMessage() +{ + return NetworkMessage(); +} + +const NetworkMessage & NetworkMessage::DecodeMessage(const NetworkBuffer &buffer) +{ + NetworkMessage message = DecodeMessageHeader(buffer); + message.data = DecodeMessageData(buffer); + return message; +} + +void *NetworkMessage::DecodeMessageData(const NetworkBuffer &buffer) +{ + if (buffer.body_size < 9) + return nullptr; + byte version = buffer.body[0]; + switch (version) + { + case 0: + { + break; + } + default: + { + //version nor supported + return nullptr;// WORKING HERE!!!!!!!!!!! + } + } +} + +const NetworkMessage &NetworkMessage::DecodeMessageHeader(const NetworkBuffer &buffer) +{ + return NetworkMessage(Utility::BitConverter::ToUint16(buffer.body, 1), buffer.body[3], Utility::BitConverter::ToUint16(buffer.body, 4), + buffer.body[6], Utility::BitConverter::ToUint16(buffer.body, 7), buffer); +} diff --git a/src/Serializer.cpp b/src/Serializer.cpp new file mode 100644 index 0000000..9bb9951 --- /dev/null +++ b/src/Serializer.cpp @@ -0,0 +1,22 @@ +#include "Serializer.hpp" + +template std::array Serializer::to_bytes(const T& object) +{ + std::array bytes; + + const byte *begin = reinterpret_cast(std::addressof(object)); + const byte *end = begin + sizeof(T); + std::copy(begin, end, std::begin(bytes)); + + return bytes; +} + +template T& Serializer::from_bytes(const std::array &bytes, T& object) +{ + static_assert(std::is_trivially_copyable::value, "not a TriviallyCopyable type"); + + byte *begin_object = reinterpret_cast(std::addressof(object)); + std::copy(std::begin(bytes), std::end(bytes), begin_object); + + return object; +} \ No newline at end of file diff --git a/src/TcpClientWindows.cpp b/src/TcpClientWindows.cpp index 7e9fc65..c3d2101 100644 --- a/src/TcpClientWindows.cpp +++ b/src/TcpClientWindows.cpp @@ -1,6 +1,7 @@ #include "TcpClient.hpp" #include "Utility.hpp" #include "Config.hpp" +#include "NetworkBuffer.hpp" #include @@ -57,21 +58,21 @@ TcpClient::~TcpClient() Utility::Delete(ptr); } -const std::string & TcpClient::GetIP() +const std::string &TcpClient::GetIP() { return ip; } -void TcpClient::SetIP(const std::string & ip) -{ - this->ip = ip; -} - uint16 TcpClient::GetPort() { return port; } +void TcpClient::SetIP(const std::string & ip) +{ + this->ip = ip; +} + void TcpClient::SetPort(uint16 port) { this->port = port; @@ -96,19 +97,20 @@ VoidCode TcpClient::Connect() char *TcpClient::ReceiveDataArray() { - char *header = new char[4](); - - int remote_buffer_size; - do - { - remote_buffer_size = recv(socket, header, 4, 0); + NetworkBuffer buffer; - if (WSAGetLastError() != 0) - { - // there was a problem receiving - } - } - while (remote_buffer_size > 0); + if (recv(socket, reinterpret_cast(buffer.body_size), 4, 0) != 4 || WSAGetLastError() != 0) + { + // there was a problem receiving the body size of the message + return nullptr; + } + + buffer.body = new byte[buffer.body_size](); + if (recv(socket, reinterpret_cast(buffer.body), buffer.body_size, 0) != buffer.body_size || WSAGetLastError() != 0) + { + //there was a problem receiving the body of the message + return nullptr; + } } const NetworkMessage &TcpClient::ReceiveData() diff --git a/src/Utility.cpp b/src/Utility.cpp index dd56227..2ebb0b3 100644 --- a/src/Utility.cpp +++ b/src/Utility.cpp @@ -14,4 +14,32 @@ void Utility::DeleteArray(void *pointer) return; delete[] pointer; pointer = nullptr; -} \ No newline at end of file +} + +byte *Utility::BitConverter::FromUint8(uint8 number) +{ + if (number == 0) + return nullptr; + return reinterpret_cast(number); +} + +uint8 Utility::BitConverter::ToUint8(byte *bytes, uint16 start_index) +{ + if (bytes == nullptr) + return 0; + return reinterpret_cast(&bytes[start_index]); +} + +byte *Utility::BitConverter::FromUint16(uint16 number) +{ + if (number == 0) + return nullptr; + return reinterpret_cast(number); +} + +uint16 Utility::BitConverter::ToUint16(byte *bytes, uint16 start_index) +{ + if (bytes == nullptr) + return 0; + return reinterpret_cast(&bytes[start_index]); +}