Network/ENet: Add compressor support

This commit is contained in:
Jérôme Leclercq 2017-09-22 15:22:43 +02:00
parent 9a665bbff6
commit cc4fdf2476
5 changed files with 94 additions and 6 deletions

View File

@ -0,0 +1,39 @@
/*
Copyright(c) 2002 - 2016 Lee Salzman
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Network module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#pragma once
#ifndef NAZARA_ENETCOMPRESSOR_HPP
#define NAZARA_ENETCOMPRESSOR_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Network/Config.hpp>
#include <Nazara/Network/NetBuffer.hpp>
namespace Nz
{
class ENetPeer;
class NAZARA_NETWORK_API ENetCompressor
{
public:
ENetCompressor() = default;
~ENetCompressor();
virtual std::size_t Compress(const ENetPeer* peer, const NetBuffer* buffers, std::size_t bufferCount, std::size_t totalInputSize, UInt8* output, std::size_t maxOutputSize) = 0;
virtual std::size_t Decompress(const ENetPeer* peer, const UInt8* input, std::size_t inputSize, UInt8* output, std::size_t maxOutputSize) = 0;
};
}
#endif // NAZARA_ENETCOMPRESSOR_HPP

View File

@ -1,4 +1,4 @@
/*
/*
Copyright(c) 2002 - 2016 Lee Salzman
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
@ -21,6 +21,7 @@
#include <Nazara/Core/Bitset.hpp>
#include <Nazara/Core/Clock.hpp>
#include <Nazara/Core/MemoryPool.hpp>
#include <Nazara/Network/ENetCompressor.hpp>
#include <Nazara/Network/ENetPeer.hpp>
#include <Nazara/Network/ENetProtocol.hpp>
#include <Nazara/Network/IpAddress.hpp>
@ -66,6 +67,8 @@ namespace Nz
int Service(ENetEvent* event, UInt32 timeout);
inline void SetCompressor(std::unique_ptr<ENetCompressor>&& compressor);
void SimulateNetwork(double packetLossProbability, UInt16 minDelay, UInt16 maxDelay);
ENetHost& operator=(const ENetHost&) = delete;
@ -130,6 +133,7 @@ namespace Nz
std::size_t m_peerCount;
std::size_t m_receivedDataLength;
std::uniform_int_distribution<UInt16> m_packetDelayDistribution;
std::unique_ptr<ENetCompressor> m_compressor;
std::vector<ENetPeer> m_peers;
std::vector<PendingIncomingPacket> m_pendingIncomingPackets;
std::vector<PendingOutgoingPacket> m_pendingOutgoingPackets;

View File

@ -52,16 +52,21 @@ namespace Nz
m_socket.Close();
}
inline Nz::IpAddress ENetHost::GetBoundAddress() const
inline IpAddress ENetHost::GetBoundAddress() const
{
return m_address;
}
inline UInt32 Nz::ENetHost::GetServiceTime() const
inline UInt32 ENetHost::GetServiceTime() const
{
return m_serviceTime;
}
inline void ENetHost::SetCompressor(std::unique_ptr<ENetCompressor>&& compressor)
{
m_compressor = std::move(compressor);
}
inline ENetPacketRef ENetHost::AllocatePacket(ENetPacketFlags flags, NetPacket&& data)
{
ENetPacketRef ref = AllocatePacket(flags);

View File

@ -0,0 +1,11 @@
// Copyright (C) 2017 Jérôme Leclercq
// This file is part of the "Nazara Engine - Network module"
// For conditions of distribution and use, see copyright notice in Config.hpp
#include <Nazara/Network/ENetCompressor.hpp>
#include <Nazara/Network/Debug.hpp>
namespace Nz
{
ENetCompressor::~ENetCompressor() = default;
}

View File

@ -521,6 +521,19 @@ namespace Nz
}
// Compression handling
if (flags & ENetProtocolHeaderFlag_Compressed)
{
if (!m_compressor)
return false;
std::size_t newSize = m_compressor->Decompress(peer, m_receivedData, m_receivedDataLength, m_packetData[1].data() + headerSize, m_packetData[1].size() - headerSize);
if (newSize == 0 || newSize > m_packetData[1].size() - headerSize)
return false;
std::memcpy(m_packetData[1].data(), header, headerSize);
m_receivedData = m_packetData[1].data();
m_receivedDataLength = headerSize + newSize;
}
// Checksum
@ -922,7 +935,7 @@ namespace Nz
++m_bufferCount;
NetBuffer& packetBuffer = m_buffers[m_bufferCount];
packetBuffer.data = outgoingCommand->packet->data.GetData() + Nz::NetPacket::HeaderSize + outgoingCommand->fragmentOffset;
packetBuffer.data = outgoingCommand->packet->data.GetData() + NetPacket::HeaderSize + outgoingCommand->fragmentOffset;
packetBuffer.dataLength = outgoingCommand->fragmentLength;
m_packetSize += packetBuffer.dataLength;
@ -1024,11 +1037,27 @@ namespace Nz
else
m_buffers[0].dataLength = NazaraOffsetOf(ENetProtocolHeader, sentTime);
// Compress packet buffers if possible
std::size_t compressedSize = 0;
if (m_compressor)
{
compressedSize = m_compressor->Compress(currentPeer, &m_buffers[1], m_bufferCount, m_packetSize - sizeof(ENetProtocolHeader), m_packetData[1].data(), m_packetData[1].size());
if (compressedSize > 0)
m_headerFlags |= ENetProtocolHeaderFlag_Compressed;
}
if (currentPeer->m_outgoingPeerID < ENetConstants::ENetProtocol_MaximumPeerId)
m_headerFlags |= currentPeer->m_outgoingSessionID << ENetProtocolHeaderSessionShift;
header->peerID = HostToNet(static_cast<UInt16>(currentPeer->m_outgoingPeerID | m_headerFlags));
if (compressedSize > 0)
{
m_buffers[1].data = m_packetData[1].data();
m_buffers[1].dataLength = compressedSize;
m_bufferCount = 2;
}
currentPeer->m_lastSendTime = m_serviceTime;
// Simulate network by adding delay to packet sending and losing some packets
@ -1038,7 +1067,7 @@ namespace Nz
sendNow = false;
if (!currentPeer->m_packetLossProbability(s_randomGenerator))
{
Nz::UInt16 delay = currentPeer->m_packetDelayDistribution(s_randomGenerator);
UInt16 delay = currentPeer->m_packetDelayDistribution(s_randomGenerator);
if (delay == 0)
sendNow = true;
else
@ -1159,7 +1188,7 @@ namespace Nz
++m_bufferCount;
NetBuffer& packetBuffer = m_buffers[m_bufferCount];
packetBuffer.data = outgoingCommand->packet->data.GetData() + Nz::NetPacket::HeaderSize + outgoingCommand->fragmentOffset;
packetBuffer.data = outgoingCommand->packet->data.GetData() + NetPacket::HeaderSize + outgoingCommand->fragmentOffset;
packetBuffer.dataLength = outgoingCommand->fragmentLength;
m_packetSize += packetBuffer.dataLength;