From a087174bf8d6e79b21b4eee36eaa2d015ede121a Mon Sep 17 00:00:00 2001 From: Lynix Date: Wed, 1 Feb 2017 00:13:08 +0100 Subject: [PATCH] Network/Algorithm: Add HostToNet and NetToHost --- include/Nazara/Network/Algorithm.hpp | 7 +++++++ include/Nazara/Network/Algorithm.inl | 25 +++++++++++++++++++++++++ src/Nazara/Network/ENetHost.cpp | 24 +----------------------- src/Nazara/Network/ENetPeer.cpp | 26 ++------------------------ 4 files changed, 35 insertions(+), 47 deletions(-) diff --git a/include/Nazara/Network/Algorithm.hpp b/include/Nazara/Network/Algorithm.hpp index 001d98059..f2d99b605 100644 --- a/include/Nazara/Network/Algorithm.hpp +++ b/include/Nazara/Network/Algorithm.hpp @@ -11,10 +11,17 @@ #include #include #include +#include namespace Nz { NAZARA_NETWORK_API bool ParseIPAddress(const char* addressPtr, UInt8 result[16], UInt16* port = nullptr, bool* isIPv6 = nullptr, const char** endOfRead = nullptr); + + template + std::enable_if_t::value, T> HostToNet(T value); + + template + std::enable_if_t::value, T> NetToHost(T value); } #include diff --git a/include/Nazara/Network/Algorithm.inl b/include/Nazara/Network/Algorithm.inl index 5364298d4..4cba24136 100644 --- a/include/Nazara/Network/Algorithm.inl +++ b/include/Nazara/Network/Algorithm.inl @@ -2,6 +2,31 @@ // This file is part of the "Nazara Engine - Core module" // For conditions of distribution and use, see copyright notice in Config.hpp +#include +#include #include +namespace Nz +{ + template + std::enable_if_t::value, T> HostToNet(T value) + { +#ifdef NAZARA_LITTLE_ENDIAN + return SwapBytes(value); +#else + return value; +#endif + } + + template + std::enable_if_t::value, T> NetToHost(T value) + { +#ifdef NAZARA_LITTLE_ENDIAN + return SwapBytes(value); +#else + return value; +#endif + } +} + #include diff --git a/src/Nazara/Network/ENetHost.cpp b/src/Nazara/Network/ENetHost.cpp index 0b73a1491..0461b1053 100644 --- a/src/Nazara/Network/ENetHost.cpp +++ b/src/Nazara/Network/ENetHost.cpp @@ -1,35 +1,13 @@ #include #include -#include #include +#include #include #include #include namespace Nz { - /// Temporary - template - T HostToNet(T value) - { - #ifdef NAZARA_LITTLE_ENDIAN - return SwapBytes(value); - #else - return value; - #endif - } - - /// Temporary - template - T NetToHost(T value) - { - #ifdef NAZARA_LITTLE_ENDIAN - return SwapBytes(value); - #else - return value; - #endif - } - namespace { static std::size_t s_commandSizes[ENetProtocolCommand_Count] = diff --git a/src/Nazara/Network/ENetPeer.cpp b/src/Nazara/Network/ENetPeer.cpp index a3946e56e..152f1b4ef 100644 --- a/src/Nazara/Network/ENetPeer.cpp +++ b/src/Nazara/Network/ENetPeer.cpp @@ -1,33 +1,11 @@ #include -#include +#include #include #include #include namespace Nz { - /// Temporary - template - T HostToNet(T value) - { - #ifdef NAZARA_LITTLE_ENDIAN - return SwapBytes(value); - #else - return value; - #endif - } - - /// Temporary - template - T NetToHost(T value) - { - #ifdef NAZARA_LITTLE_ENDIAN - return SwapBytes(value); - #else - return value; - #endif - } - ENetPeer::ENetPeer(ENetHost* host, UInt16 peerId) : m_host(host), m_packetPool(sizeof(ENetPacket)), @@ -683,7 +661,7 @@ namespace Nz startCommand->fragments.Set(fragmentNumber, true); if (fragmentOffset + fragmentLength > startCommand->packet->data.GetDataSize()) - fragmentLength = startCommand->packet->data.GetDataSize() - fragmentOffset; + fragmentLength = static_cast(startCommand->packet->data.GetDataSize() - fragmentOffset); std::memcpy(startCommand->packet->data.GetData() + NetPacket::HeaderSize + fragmentOffset, reinterpret_cast(command) + sizeof(ENetProtocolSendFragment), fragmentLength);