Network/Algorithm: Add HostToNet and NetToHost

This commit is contained in:
Lynix 2017-02-01 00:13:08 +01:00
parent 85257da07e
commit a087174bf8
4 changed files with 35 additions and 47 deletions

View File

@ -11,10 +11,17 @@
#include <Nazara/Network/Config.hpp>
#include <functional>
#include <tuple>
#include <type_traits>
namespace Nz
{
NAZARA_NETWORK_API bool ParseIPAddress(const char* addressPtr, UInt8 result[16], UInt16* port = nullptr, bool* isIPv6 = nullptr, const char** endOfRead = nullptr);
template<typename T>
std::enable_if_t<std::is_arithmetic<T>::value, T> HostToNet(T value);
template<typename T>
std::enable_if_t<std::is_arithmetic<T>::value, T> NetToHost(T value);
}
#include <Nazara/Network/Algorithm.inl>

View File

@ -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 <Nazara/Network/Algorithm.hpp>
#include <Nazara/Core/Endianness.hpp>
#include <Nazara/Network/Debug.hpp>
namespace Nz
{
template<typename T>
std::enable_if_t<std::is_arithmetic<T>::value, T> HostToNet(T value)
{
#ifdef NAZARA_LITTLE_ENDIAN
return SwapBytes(value);
#else
return value;
#endif
}
template<typename T>
std::enable_if_t<std::is_arithmetic<T>::value, T> NetToHost(T value)
{
#ifdef NAZARA_LITTLE_ENDIAN
return SwapBytes(value);
#else
return value;
#endif
}
}
#include <Nazara/Network/DebugOff.hpp>

View File

@ -1,35 +1,13 @@
#include <Nazara/Network/ENetHost.hpp>
#include <Nazara/Core/Clock.hpp>
#include <Nazara/Core/Endianness.hpp>
#include <Nazara/Core/OffsetOf.hpp>
#include <Nazara/Network/Algorithm.hpp>
#include <Nazara/Network/ENetPeer.hpp>
#include <Nazara/Network/NetPacket.hpp>
#include <Nazara/Network/Debug.hpp>
namespace Nz
{
/// Temporary
template<typename T>
T HostToNet(T value)
{
#ifdef NAZARA_LITTLE_ENDIAN
return SwapBytes(value);
#else
return value;
#endif
}
/// Temporary
template<typename T>
T NetToHost(T value)
{
#ifdef NAZARA_LITTLE_ENDIAN
return SwapBytes(value);
#else
return value;
#endif
}
namespace
{
static std::size_t s_commandSizes[ENetProtocolCommand_Count] =

View File

@ -1,33 +1,11 @@
#include <Nazara/Network/ENetPeer.hpp>
#include <Nazara/Core/Endianness.hpp>
#include <Nazara/Network/Algorithm.hpp>
#include <Nazara/Network/ENetHost.hpp>
#include <Nazara/Network/NetPacket.hpp>
#include <Nazara/Network/Debug.hpp>
namespace Nz
{
/// Temporary
template<typename T>
T HostToNet(T value)
{
#ifdef NAZARA_LITTLE_ENDIAN
return SwapBytes(value);
#else
return value;
#endif
}
/// Temporary
template<typename T>
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<UInt16>(startCommand->packet->data.GetDataSize() - fragmentOffset);
std::memcpy(startCommand->packet->data.GetData() + NetPacket::HeaderSize + fragmentOffset, reinterpret_cast<const UInt8*>(command) + sizeof(ENetProtocolSendFragment), fragmentLength);