diff --git a/include/Nazara/Network/ENetCompressor.hpp b/include/Nazara/Network/ENetCompressor.hpp new file mode 100644 index 000000000..5466c80b6 --- /dev/null +++ b/include/Nazara/Network/ENetCompressor.hpp @@ -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 +#include +#include + +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 diff --git a/include/Nazara/Network/ENetHost.hpp b/include/Nazara/Network/ENetHost.hpp index 789c3d9a6..cedf7b218 100644 --- a/include/Nazara/Network/ENetHost.hpp +++ b/include/Nazara/Network/ENetHost.hpp @@ -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 #include #include +#include #include #include #include @@ -66,6 +67,8 @@ namespace Nz int Service(ENetEvent* event, UInt32 timeout); + inline void SetCompressor(std::unique_ptr&& 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 m_packetDelayDistribution; + std::unique_ptr m_compressor; std::vector m_peers; std::vector m_pendingIncomingPackets; std::vector m_pendingOutgoingPackets; diff --git a/include/Nazara/Network/ENetHost.inl b/include/Nazara/Network/ENetHost.inl index 3f35a8f39..58a74ca04 100644 --- a/include/Nazara/Network/ENetHost.inl +++ b/include/Nazara/Network/ENetHost.inl @@ -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&& compressor) + { + m_compressor = std::move(compressor); + } + inline ENetPacketRef ENetHost::AllocatePacket(ENetPacketFlags flags, NetPacket&& data) { ENetPacketRef ref = AllocatePacket(flags); diff --git a/src/Nazara/Network/ENetCompressor.cpp b/src/Nazara/Network/ENetCompressor.cpp new file mode 100644 index 000000000..373389ecc --- /dev/null +++ b/src/Nazara/Network/ENetCompressor.cpp @@ -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 +#include + +namespace Nz +{ + ENetCompressor::~ENetCompressor() = default; +} diff --git a/src/Nazara/Network/ENetHost.cpp b/src/Nazara/Network/ENetHost.cpp index 5886e09c5..f86b2289f 100644 --- a/src/Nazara/Network/ENetHost.cpp +++ b/src/Nazara/Network/ENetHost.cpp @@ -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(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;