Added Tests project, Callback functions are in progress, NetworkBuffer is done, Network Message is WIP but already has a lot of code, Serializer is done (subject to change), Utility classes are being made, and TcpClientWindows is WIP.

Every single piece of code wrote today may or not change, but it will probably change because ill find a better way to do it xD
This commit is contained in:
xX-TheDoctor-Xx
2016-07-17 02:46:42 +01:00
parent 97262ee8c6
commit 16ed3619b3
37 changed files with 483 additions and 45 deletions

View File

@ -0,0 +1 @@
<EFBFBD><EFBFBD>

View File

@ -0,0 +1,2 @@
#TargetFrameworkVersion=v4.0:PlatformToolSet=v140:EnableManagedIncrementalBuild=false:VCToolArchitecture=Native32Bit:WindowsTargetPlatformVersion=8.1
Release|Win32|F:\VoidNet\VoidNetVS\|

Binary file not shown.

Binary file not shown.

Binary file not shown.

12
TestsVS/Tests/Tests.cpp Normal file
View File

@ -0,0 +1,12 @@
#include "Utility.hpp"
#include <iostream>
int main()
{
byte *bytes = Utility::BitConverter::FromUint8(20);
std::cout << Utility::BitConverter::ToUint8(bytes, 0) << std::endl;
getchar();
return 0;
}

156
TestsVS/Tests/Tests.vcxproj Normal file
View File

@ -0,0 +1,156 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{41663B22-3949-4570-9AB5-309363EAD56F}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>Tests</RootNamespace>
<WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v140</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>F:\VoidNet\include;</AdditionalIncludeDirectories>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Tests.cpp" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\VoidNetVS\VoidNetVS\VoidNetVS.vcxproj">
<Project>{5172321e-ccb0-4a77-9f3d-faaf0084f434}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClCompile Include="Tests.cpp" />
</ItemGroup>
</Project>

Binary file not shown.

Binary file not shown.

View File

@ -5,6 +5,8 @@ VisualStudioVersion = 14.0.25123.0
MinimumVisualStudioVersion = 10.0.40219.1 MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "VoidNetVS", "VoidNetVS\VoidNetVS.vcxproj", "{5172321E-CCB0-4A77-9F3D-FAAF0084F434}" Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "VoidNetVS", "VoidNetVS\VoidNetVS.vcxproj", "{5172321E-CCB0-4A77-9F3D-FAAF0084F434}"
EndProject EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Tests", "..\TestsVS\Tests\Tests.vcxproj", "{41663B22-3949-4570-9AB5-309363EAD56F}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64 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|x64.Build.0 = Release|x64
{5172321E-CCB0-4A77-9F3D-FAAF0084F434}.Release|x86.ActiveCfg = Release|Win32 {5172321E-CCB0-4A77-9F3D-FAAF0084F434}.Release|x86.ActiveCfg = Release|Win32
{5172321E-CCB0-4A77-9F3D-FAAF0084F434}.Release|x86.Build.0 = 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 EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

@ -0,0 +1,2 @@
#TargetFrameworkVersion=v4.0:PlatformToolSet=v140:EnableManagedIncrementalBuild=false:VCToolArchitecture=Native32Bit:WindowsTargetPlatformVersion=8.1
Release|Win32|F:\VoidNet\VoidNetVS\|

View File

@ -19,18 +19,25 @@
</ProjectConfiguration> </ProjectConfiguration>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\include\Callbacks.hpp" />
<ClInclude Include="..\..\include\Config.hpp" /> <ClInclude Include="..\..\include\Config.hpp" />
<ClInclude Include="..\..\include\Defs.hpp" /> <ClInclude Include="..\..\include\Defs.hpp" />
<ClInclude Include="..\..\include\Init.hpp" /> <ClInclude Include="..\..\include\Init.hpp" />
<ClInclude Include="..\..\include\NetworkBuffer.hpp" />
<ClInclude Include="..\..\include\NetworkMessage.hpp" /> <ClInclude Include="..\..\include\NetworkMessage.hpp" />
<ClInclude Include="..\..\include\Serializer.hpp" />
<ClInclude Include="..\..\include\TcpClient.hpp" /> <ClInclude Include="..\..\include\TcpClient.hpp" />
<ClInclude Include="..\..\include\Utility.hpp" /> <ClInclude Include="..\..\include\Utility.hpp" />
<ClInclude Include="..\..\include\VoidNet.hpp" /> <ClInclude Include="..\..\include\VoidNetClient.hpp" />
<ClInclude Include="..\..\include\VoidNetServer.hpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\src\Config.cpp" /> <ClCompile Include="..\..\src\Config.cpp" />
<ClCompile Include="..\..\src\Init.cpp" /> <ClCompile Include="..\..\src\Init.cpp" />
<ClCompile Include="..\..\src\InitWindows.cpp" /> <ClCompile Include="..\..\src\InitWindows.cpp" />
<ClCompile Include="..\..\src\NetworkBuffer.cpp" />
<ClCompile Include="..\..\src\NetworkMessage.cpp" />
<ClCompile Include="..\..\src\Serializer.cpp" />
<ClCompile Include="..\..\src\TcpClient.cpp" /> <ClCompile Include="..\..\src\TcpClient.cpp" />
<ClCompile Include="..\..\src\TcpClientWindows.cpp" /> <ClCompile Include="..\..\src\TcpClientWindows.cpp" />
<ClCompile Include="..\..\src\Utility.cpp" /> <ClCompile Include="..\..\src\Utility.cpp" />

View File

@ -15,9 +15,6 @@
<ClInclude Include="..\..\include\Defs.hpp"> <ClInclude Include="..\..\include\Defs.hpp">
<Filter>include</Filter> <Filter>include</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\include\VoidNet.hpp">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\TcpClient.hpp"> <ClInclude Include="..\..\include\TcpClient.hpp">
<Filter>include</Filter> <Filter>include</Filter>
</ClInclude> </ClInclude>
@ -30,6 +27,21 @@
<ClInclude Include="..\..\include\NetworkMessage.hpp"> <ClInclude Include="..\..\include\NetworkMessage.hpp">
<Filter>include</Filter> <Filter>include</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\include\VoidNetClient.hpp">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\VoidNetServer.hpp">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\NetworkBuffer.hpp">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\Serializer.hpp">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\Callbacks.hpp">
<Filter>include</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\src\Init.cpp"> <ClCompile Include="..\..\src\Init.cpp">
@ -50,6 +62,15 @@
<ClCompile Include="..\..\src\Config.cpp"> <ClCompile Include="..\..\src\Config.cpp">
<Filter>src</Filter> <Filter>src</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\NetworkBuffer.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\src\Serializer.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\src\NetworkMessage.cpp">
<Filter>src</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Text Include="..\..\TODO" /> <Text Include="..\..\TODO" />

17
include/Callbacks.hpp Normal file
View File

@ -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

View File

@ -3,9 +3,6 @@
#ifdef _MSC_VER #ifdef _MSC_VER
#pragma once #pragma once
#endif
#ifdef _MSC_VER
#define WIN32_LEAN_AND_MEAN #define WIN32_LEAN_AND_MEAN
#define _WINSOCKAPI_ #define _WINSOCKAPI_
#include <windows.h> #include <windows.h>
@ -185,4 +182,14 @@ enum VoidCode
VOID_COULDNT_CONNECT, VOID_COULDNT_CONNECT,
}; };
enum DistributionType
{
All,
Server,
Others,
ID,
Reply,
Custom,
}
#endif // DEFS_HPP #endif // DEFS_HPP

19
include/NetworkBuffer.hpp Normal file
View File

@ -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

View File

@ -5,12 +5,30 @@
#pragma once #pragma once
#endif #endif
class NetworkMessage #include "Defs.hpp"
{ #include "NetworkBuffer.hpp"
public:
NetworkMessage();
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 #endif

18
include/Serializer.hpp Normal file
View File

@ -0,0 +1,18 @@
#ifndef SERIALIZER_HPP
#define SERIALIZER_HPP
#ifdef _MSC_VER
#pragma once
#endif
#include "Defs.hpp"
#include <array>
class Serializer
{
template<typename T> std::array<byte, sizeof(T)> to_bytes(const T& object);
template<typename T> T& from_bytes(const std::array<byte, sizeof(T)> &bytes, T& object);
};
#endif

View File

@ -23,7 +23,7 @@ public:
uint16 GetPort(); uint16 GetPort();
void SetPort(uint16 port); void SetPort(uint16 port);
VoidCode Connect(); // this function is used if Initialize was not called VoidCode Connect();
char *ReceiveDataArray(); char *ReceiveDataArray();
const NetworkMessage &ReceiveData(); const NetworkMessage &ReceiveData();

View File

@ -5,10 +5,24 @@
#pragma once #pragma once
#endif #endif
#include "Defs.hpp"
#include <streambuf>
#include <istream>
struct Utility struct Utility
{ {
static void Delete(void *pointer); static void Delete(void *pointer);
static void DeleteArray(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 #endif

View File

@ -1,11 +0,0 @@
#ifndef VOID_NET_HPP
#define VOID_NET_HPP
#ifdef _MSC_VER
#pragma once
#endif
#include <Defs.hpp>
#include <Init.hpp>
#endif

15
include/VoidNetClient.hpp Normal file
View File

@ -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

View File

@ -0,0 +1,8 @@
#ifndef VOID_NET_SERVER_HPP
#define VOID_NET_SERVER_HPP
#ifdef _MSC_VER
#pragma once
#endif
#endif

11
src/NetworkBuffer.cpp Normal file
View File

@ -0,0 +1,11 @@
#include "NetworkBuffer.hpp"
#include "Utility.hpp"
NetworkBuffer::NetworkBuffer()
{
}
NetworkBuffer::~NetworkBuffer()
{
Utility::DeleteArray(body);
}

53
src/NetworkMessage.cpp Normal file
View File

@ -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);
}

22
src/Serializer.cpp Normal file
View File

@ -0,0 +1,22 @@
#include "Serializer.hpp"
template<typename T> std::array<byte, sizeof(T)> Serializer::to_bytes(const T& object)
{
std::array<byte, sizeof(T)> bytes;
const byte *begin = reinterpret_cast<const byte*>(std::addressof(object));
const byte *end = begin + sizeof(T);
std::copy(begin, end, std::begin(bytes));
return bytes;
}
template<typename T> T& Serializer::from_bytes(const std::array<byte, sizeof(T)> &bytes, T& object)
{
static_assert(std::is_trivially_copyable<T>::value, "not a TriviallyCopyable type");
byte *begin_object = reinterpret_cast<byte*>(std::addressof(object));
std::copy(std::begin(bytes), std::end(bytes), begin_object);
return object;
}

View File

@ -1,6 +1,7 @@
#include "TcpClient.hpp" #include "TcpClient.hpp"
#include "Utility.hpp" #include "Utility.hpp"
#include "Config.hpp" #include "Config.hpp"
#include "NetworkBuffer.hpp"
#include <iostream> #include <iostream>
@ -62,16 +63,16 @@ const std::string & TcpClient::GetIP()
return ip; return ip;
} }
void TcpClient::SetIP(const std::string & ip)
{
this->ip = ip;
}
uint16 TcpClient::GetPort() uint16 TcpClient::GetPort()
{ {
return port; return port;
} }
void TcpClient::SetIP(const std::string & ip)
{
this->ip = ip;
}
void TcpClient::SetPort(uint16 port) void TcpClient::SetPort(uint16 port)
{ {
this->port = port; this->port = port;
@ -96,20 +97,21 @@ VoidCode TcpClient::Connect()
char *TcpClient::ReceiveDataArray() char *TcpClient::ReceiveDataArray()
{ {
char *header = new char[4](); NetworkBuffer buffer;
int remote_buffer_size; if (recv(socket, reinterpret_cast<char*>(buffer.body_size), 4, 0) != 4 || WSAGetLastError() != 0)
do
{ {
remote_buffer_size = recv(socket, header, 4, 0); // there was a problem receiving the body size of the message
return nullptr;
}
if (WSAGetLastError() != 0) buffer.body = new byte[buffer.body_size]();
if (recv(socket, reinterpret_cast<char*>(buffer.body), buffer.body_size, 0) != buffer.body_size || WSAGetLastError() != 0)
{ {
// there was a problem receiving //there was a problem receiving the body of the message
return nullptr;
} }
} }
while (remote_buffer_size > 0);
}
const NetworkMessage &TcpClient::ReceiveData() const NetworkMessage &TcpClient::ReceiveData()
{ {

View File

@ -15,3 +15,31 @@ void Utility::DeleteArray(void *pointer)
delete[] pointer; delete[] pointer;
pointer = nullptr; pointer = nullptr;
} }
byte *Utility::BitConverter::FromUint8(uint8 number)
{
if (number == 0)
return nullptr;
return reinterpret_cast<byte*>(number);
}
uint8 Utility::BitConverter::ToUint8(byte *bytes, uint16 start_index)
{
if (bytes == nullptr)
return 0;
return reinterpret_cast<uint8>(&bytes[start_index]);
}
byte *Utility::BitConverter::FromUint16(uint16 number)
{
if (number == 0)
return nullptr;
return reinterpret_cast<byte*>(number);
}
uint16 Utility::BitConverter::ToUint16(byte *bytes, uint16 start_index)
{
if (bytes == nullptr)
return 0;
return reinterpret_cast<uint16>(&bytes[start_index]);
}