Network: Remove NetPacket class
It was badly designed
This commit is contained in:
@@ -18,13 +18,13 @@
|
||||
#define NAZARA_NETWORK_ENETHOST_HPP
|
||||
|
||||
#include <NazaraUtils/Prerequisites.hpp>
|
||||
#include <Nazara/Core/ByteArray.hpp>
|
||||
#include <Nazara/Core/Clock.hpp>
|
||||
#include <Nazara/Network/ENetCompressor.hpp>
|
||||
#include <Nazara/Network/ENetPeer.hpp>
|
||||
#include <Nazara/Network/ENetProtocol.hpp>
|
||||
#include <Nazara/Network/IpAddress.hpp>
|
||||
#include <Nazara/Network/NetBuffer.hpp>
|
||||
#include <Nazara/Network/NetPacket.hpp>
|
||||
#include <Nazara/Network/SocketPoller.hpp>
|
||||
#include <Nazara/Network/UdpSocket.hpp>
|
||||
#include <NazaraUtils/Flags.hpp>
|
||||
@@ -45,11 +45,11 @@ namespace Nz
|
||||
inline ~ENetHost();
|
||||
|
||||
ENetPacketRef AllocatePacket(ENetPacketFlags flags);
|
||||
inline ENetPacketRef AllocatePacket(ENetPacketFlags flags, NetPacket&& data);
|
||||
inline ENetPacketRef AllocatePacket(ENetPacketFlags flags, ByteArray&& payload);
|
||||
|
||||
inline void AllowsIncomingConnections(bool allow = true);
|
||||
|
||||
void Broadcast(UInt8 channelId, ENetPacketFlags flags, NetPacket&& packet);
|
||||
void Broadcast(UInt8 channelId, ENetPacketFlags flags, ByteArray&& packet);
|
||||
|
||||
bool CheckEvents(ENetEvent* event);
|
||||
|
||||
@@ -112,15 +112,15 @@ namespace Nz
|
||||
|
||||
struct PendingIncomingPacket
|
||||
{
|
||||
ByteArray data;
|
||||
IpAddress from;
|
||||
NetPacket data;
|
||||
UInt32 deliveryTime;
|
||||
};
|
||||
|
||||
struct PendingOutgoingPacket
|
||||
{
|
||||
ByteArray data;
|
||||
IpAddress to;
|
||||
NetPacket data;
|
||||
UInt32 deliveryTime;
|
||||
};
|
||||
|
||||
|
||||
@@ -18,10 +18,10 @@ namespace Nz
|
||||
Destroy();
|
||||
}
|
||||
|
||||
inline ENetPacketRef ENetHost::AllocatePacket(ENetPacketFlags flags, NetPacket&& data)
|
||||
inline ENetPacketRef ENetHost::AllocatePacket(ENetPacketFlags flags, ByteArray&& payload)
|
||||
{
|
||||
ENetPacketRef ref = AllocatePacket(flags);
|
||||
ref->data = std::move(data);
|
||||
ref->data = std::move(payload);
|
||||
|
||||
return ref;
|
||||
}
|
||||
@@ -110,7 +110,7 @@ namespace Nz
|
||||
inline void ENetHost::UpdateServiceTime()
|
||||
{
|
||||
// Use high precision clock for extra precision
|
||||
m_serviceTime = static_cast<UInt32>(GetElapsedNanoseconds().AsMilliseconds());
|
||||
m_serviceTime = static_cast<UInt32>(GetElapsedNanoseconds().AsMilliseconds()); // overflow is permitted
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,8 @@
|
||||
#define NAZARA_NETWORK_ENETPACKET_HPP
|
||||
|
||||
#include <NazaraUtils/Prerequisites.hpp>
|
||||
#include <Nazara/Network/NetPacket.hpp>
|
||||
#include <Nazara/Core/ByteArray.hpp>
|
||||
#include <Nazara/Network/Export.hpp>
|
||||
#include <NazaraUtils/MemoryPool.hpp>
|
||||
#include <NazaraUtils/MovablePtr.hpp>
|
||||
#include <NazaraUtils/Signal.hpp>
|
||||
@@ -34,8 +35,8 @@ namespace Nz
|
||||
|
||||
struct ENetPacket
|
||||
{
|
||||
ByteArray data;
|
||||
ENetPacketFlags flags;
|
||||
NetPacket data;
|
||||
std::size_t poolIndex;
|
||||
std::size_t referenceCount = 0;
|
||||
UInt32 remainingFragments; // for ack
|
||||
|
||||
@@ -73,7 +73,7 @@ namespace Nz
|
||||
void Reset();
|
||||
|
||||
bool Send(UInt8 channelId, ENetPacketRef packetRef);
|
||||
bool Send(UInt8 channelId, ENetPacketFlags flags, NetPacket&& packet);
|
||||
bool Send(UInt8 channelId, ENetPacketFlags flags, ByteArray&& payload);
|
||||
|
||||
void SimulateNetwork(double packetLossProbability, UInt16 minDelay, UInt16 maxDelay);
|
||||
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
// Copyright (C) 2024 Jérôme "SirLynix" Leclercq (lynix680@gmail.com)
|
||||
// This file is part of the "Nazara Engine - Network module"
|
||||
// For conditions of distribution and use, see copyright notice in Export.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef NAZARA_NETWORK_NETPACKET_HPP
|
||||
#define NAZARA_NETWORK_NETPACKET_HPP
|
||||
|
||||
#include <NazaraUtils/Prerequisites.hpp>
|
||||
#include <Nazara/Core/ByteStream.hpp>
|
||||
#include <Nazara/Core/MemoryStream.hpp>
|
||||
#include <Nazara/Network/Export.hpp>
|
||||
#include <mutex>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
class NAZARA_NETWORK_API NetPacket : public ByteStream
|
||||
{
|
||||
friend class Network;
|
||||
|
||||
public:
|
||||
inline NetPacket();
|
||||
inline NetPacket(UInt16 netCode, std::size_t minCapacity = 0);
|
||||
inline NetPacket(UInt16 netCode, const void* ptr, std::size_t size);
|
||||
NetPacket(const NetPacket&) = delete;
|
||||
NetPacket(NetPacket&& packet);
|
||||
inline ~NetPacket();
|
||||
|
||||
inline const UInt8* GetConstData() const;
|
||||
inline UInt8* GetData() const;
|
||||
inline size_t GetDataSize() const;
|
||||
inline UInt16 GetNetCode() const;
|
||||
|
||||
virtual void OnReceive(UInt16 netCode, const void* data, std::size_t size);
|
||||
virtual const void* OnSend(std::size_t* newSize) const;
|
||||
|
||||
inline void Reset();
|
||||
inline void Reset(UInt16 netCode, std::size_t minCapacity = 0);
|
||||
inline void Reset(UInt16 netCode, const void* ptr, std::size_t size);
|
||||
|
||||
inline void Resize(std::size_t newSize);
|
||||
|
||||
inline void SetNetCode(UInt16 netCode);
|
||||
|
||||
NetPacket& operator=(const NetPacket&) = delete;
|
||||
NetPacket& operator=(NetPacket&& packet);
|
||||
|
||||
static bool DecodeHeader(const void* data, UInt32* packetSize, UInt16* netCode);
|
||||
static bool EncodeHeader(void* data, UInt32 packetSize, UInt16 netCode);
|
||||
|
||||
static constexpr std::size_t HeaderSize = sizeof(UInt32) + sizeof(UInt16); //< PacketSize + NetCode
|
||||
|
||||
private:
|
||||
void OnEmptyStream() override;
|
||||
|
||||
void FreeStream();
|
||||
void InitStream(std::size_t minCapacity, UInt64 cursorPos, OpenModeFlags openMode);
|
||||
|
||||
static bool Initialize();
|
||||
static void Uninitialize();
|
||||
|
||||
std::unique_ptr<ByteArray> m_buffer;
|
||||
MemoryStream m_memoryStream;
|
||||
UInt16 m_netCode;
|
||||
|
||||
static std::recursive_mutex s_availableBuffersMutex;
|
||||
static std::vector<std::pair<std::size_t, std::unique_ptr<ByteArray>>> s_availableBuffers;
|
||||
};
|
||||
}
|
||||
|
||||
#include <Nazara/Network/NetPacket.inl>
|
||||
|
||||
#endif // NAZARA_NETWORK_NETPACKET_HPP
|
||||
@@ -1,222 +0,0 @@
|
||||
// Copyright (C) 2024 Jérôme "SirLynix" Leclercq (lynix680@gmail.com)
|
||||
// This file is part of the "Nazara Engine - Network module"
|
||||
// For conditions of distribution and use, see copyright notice in Export.hpp
|
||||
|
||||
#include <Nazara/Core/ByteArray.hpp>
|
||||
#include <Nazara/Core/Error.hpp>
|
||||
#include <Nazara/Network/Enums.hpp>
|
||||
#include <cstring>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
/*!
|
||||
* \brief Constructs a NetPacket object by default
|
||||
*/
|
||||
inline NetPacket::NetPacket() :
|
||||
m_netCode(0)
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Constructs a NetPacket object with a packet number and a minimal capacity
|
||||
*
|
||||
* \param netCode Packet number
|
||||
* \param minCapacity Minimal capacity of the packet
|
||||
*/
|
||||
|
||||
inline NetPacket::NetPacket(UInt16 netCode, std::size_t minCapacity)
|
||||
{
|
||||
Reset(netCode, minCapacity);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Constructs a NetPacket object with a packet number and raw memory
|
||||
*
|
||||
* \param netCode Packet number
|
||||
* \param ptr Raw memory
|
||||
* \param size Size of the memory
|
||||
*/
|
||||
|
||||
inline NetPacket::NetPacket(UInt16 netCode, const void* ptr, std::size_t size)
|
||||
{
|
||||
Reset(netCode, ptr, size);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Constructs a NetPacket object with another one by move semantic
|
||||
*
|
||||
* \param packet NetPacket to move into this
|
||||
*/
|
||||
|
||||
inline NetPacket::NetPacket(NetPacket&& packet) :
|
||||
ByteStream(std::move(packet)),
|
||||
m_buffer(std::move(packet.m_buffer)),
|
||||
m_memoryStream(std::move(packet.m_memoryStream)),
|
||||
m_netCode(packet.m_netCode)
|
||||
{
|
||||
///< Redirect memory stream to the moved buffer
|
||||
if (m_buffer)
|
||||
{
|
||||
m_memoryStream.SetBuffer(m_buffer.get(), m_memoryStream.GetOpenMode());
|
||||
SetStream(&m_memoryStream);
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Destructs the object
|
||||
*/
|
||||
|
||||
inline NetPacket::~NetPacket()
|
||||
{
|
||||
FlushBits(); //< Needs to be done here as the stream will be freed before ByteStream calls it
|
||||
FreeStream();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the raw buffer
|
||||
* \return Constant raw buffer
|
||||
*
|
||||
* \remark Produces a NazaraAssert if internal buffer is invalid
|
||||
*/
|
||||
|
||||
inline const UInt8* NetPacket::GetConstData() const
|
||||
{
|
||||
NazaraAssert(m_buffer, "Invalid buffer");
|
||||
|
||||
return m_buffer->GetConstBuffer();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the raw buffer
|
||||
* \return Raw buffer
|
||||
*
|
||||
* \remark Produces a NazaraAssert if internal buffer is invalid
|
||||
*/
|
||||
|
||||
inline UInt8* NetPacket::GetData() const
|
||||
{
|
||||
NazaraAssert(m_buffer, "Invalid buffer");
|
||||
|
||||
return m_buffer->GetBuffer();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the size of the data
|
||||
* \return Size of the data
|
||||
*/
|
||||
|
||||
inline size_t NetPacket::GetDataSize() const
|
||||
{
|
||||
if (m_buffer)
|
||||
return m_buffer->GetSize() - HeaderSize;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Gets the packet number
|
||||
* \return Packet number
|
||||
*/
|
||||
|
||||
inline UInt16 NetPacket::GetNetCode() const
|
||||
{
|
||||
return m_netCode;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Resets the packet
|
||||
*/
|
||||
|
||||
inline void NetPacket::Reset()
|
||||
{
|
||||
FreeStream();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Resets the packet with a packet number and a minimal capacity
|
||||
*
|
||||
* \param netCode Packet number
|
||||
* \param minCapacity Minimal capacity of the packet
|
||||
*/
|
||||
|
||||
inline void NetPacket::Reset(UInt16 netCode, std::size_t minCapacity)
|
||||
{
|
||||
InitStream(HeaderSize + minCapacity, HeaderSize, OpenMode_ReadWrite);
|
||||
m_netCode = netCode;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Resets the packet with a packet number and raw memory
|
||||
*
|
||||
* \param netCode Packet number
|
||||
* \param ptr Raw memory
|
||||
* \param size Size of the memory
|
||||
*/
|
||||
|
||||
inline void NetPacket::Reset(UInt16 netCode, const void* ptr, std::size_t size)
|
||||
{
|
||||
InitStream(HeaderSize + size, HeaderSize, OpenMode::Read);
|
||||
m_buffer->Resize(HeaderSize + size);
|
||||
|
||||
if (ptr)
|
||||
std::memcpy(m_buffer->GetBuffer() + HeaderSize, ptr, size);
|
||||
|
||||
m_netCode = netCode;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Resizes the packet
|
||||
*
|
||||
* \param newSize Size for the resizing operation
|
||||
*
|
||||
* \remark Produces a NazaraAssert if internal buffer is invalid
|
||||
*/
|
||||
|
||||
inline void NetPacket::Resize(std::size_t newSize)
|
||||
{
|
||||
NazaraAssert(m_buffer, "Invalid buffer");
|
||||
|
||||
m_buffer->Resize(newSize);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sets the packet number
|
||||
*
|
||||
* \param netCode Packet number
|
||||
*/
|
||||
|
||||
inline void NetPacket::SetNetCode(UInt16 netCode)
|
||||
{
|
||||
m_netCode = netCode;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Moves the NetPacket into this
|
||||
* \return A reference to this
|
||||
*
|
||||
* \param packet NetPacket to move in this
|
||||
*/
|
||||
|
||||
inline NetPacket& NetPacket::operator=(NetPacket&& packet)
|
||||
{
|
||||
FreeStream();
|
||||
|
||||
ByteStream::operator=(std::move(packet));
|
||||
|
||||
m_buffer = std::move(packet.m_buffer);
|
||||
m_memoryStream = std::move(packet.m_memoryStream);
|
||||
m_netCode = packet.m_netCode;
|
||||
|
||||
///< Redirect memory stream to the moved buffer
|
||||
if (m_buffer)
|
||||
{
|
||||
m_memoryStream.SetBuffer(m_buffer.get(), m_memoryStream.GetOpenMode());
|
||||
SetStream(&m_memoryStream);
|
||||
}
|
||||
else
|
||||
SetStream(static_cast<Stream*>(nullptr));
|
||||
|
||||
return *this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
namespace Nz
|
||||
{
|
||||
struct NetBuffer;
|
||||
class NetPacket;
|
||||
|
||||
class NAZARA_NETWORK_API TcpClient : public AbstractSocket, public Stream
|
||||
{
|
||||
@@ -46,11 +45,9 @@ namespace Nz
|
||||
SocketState PollForConnected(UInt64 waitDuration = 0);
|
||||
|
||||
bool Receive(void* buffer, std::size_t size, std::size_t* received);
|
||||
bool ReceivePacket(NetPacket* packet);
|
||||
|
||||
bool Send(const void* buffer, std::size_t size, std::size_t* sent);
|
||||
bool SendMultiple(const NetBuffer* buffers, std::size_t bufferCount, std::size_t* sent);
|
||||
bool SendPacket(const NetPacket& packet);
|
||||
|
||||
SocketState WaitForConnected(UInt64 msTimeout = 3000);
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@
|
||||
namespace Nz
|
||||
{
|
||||
struct NetBuffer;
|
||||
class NetPacket;
|
||||
|
||||
class NAZARA_NETWORK_API UdpSocket : public AbstractSocket
|
||||
{
|
||||
@@ -40,11 +39,9 @@ namespace Nz
|
||||
|
||||
bool Receive(void* buffer, std::size_t size, IpAddress* from, std::size_t* received);
|
||||
bool ReceiveMultiple(NetBuffer* buffers, std::size_t bufferCount, IpAddress* from, std::size_t* received);
|
||||
bool ReceivePacket(NetPacket* packet, IpAddress* from);
|
||||
|
||||
bool Send(const IpAddress& to, const void* buffer, std::size_t size, std::size_t* sent);
|
||||
bool SendMultiple(const IpAddress& to, const NetBuffer* buffers, std::size_t bufferCount, std::size_t* sent);
|
||||
bool SendPacket(const IpAddress& to, const NetPacket& packet);
|
||||
|
||||
UdpSocket& operator=(const UdpSocket& udpSocket) = delete;
|
||||
UdpSocket& operator=(UdpSocket && udpSocket) noexcept = default;
|
||||
|
||||
Reference in New Issue
Block a user