Network/ENet: Move all packet allocation to host

This commit is contained in:
Lynix 2017-02-16 23:26:00 +01:00
parent 8225ad3b41
commit b7ee6d7b29
6 changed files with 37 additions and 29 deletions

View File

@ -71,6 +71,9 @@ namespace Nz
ENetHost& operator=(ENetHost&&) = default;
private:
ENetPacketRef AllocatePacket(ENetPacketFlags flags);
inline ENetPacketRef AllocatePacket(ENetPacketFlags flags, NetPacket&& data);
bool InitSocket(const IpAddress& address);
void AddToDispatchQueue(ENetPeer* peer);

View File

@ -56,6 +56,14 @@ namespace Nz
{
return m_serviceTime;
}
inline ENetPacketRef ENetHost::AllocatePacket(ENetPacketFlags flags, NetPacket&& data)
{
ENetPacketRef ref = AllocatePacket(flags);
ref->data = std::move(data);
return ref;
}
}
#include <Nazara/Network/DebugOff.hpp>

View File

@ -20,7 +20,6 @@
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Bitset.hpp>
#include <Nazara/Core/Clock.hpp>
#include <Nazara/Core/MemoryPool.hpp>
#include <Nazara/Network/ENetPacket.hpp>
#include <Nazara/Network/ENetProtocol.hpp>
#include <Nazara/Network/IpAddress.hpp>
@ -40,7 +39,7 @@ namespace Nz
friend struct PacketRef;
public:
ENetPeer(ENetHost* host, UInt16 peerId);
inline ENetPeer(ENetHost* host, UInt16 peerId);
ENetPeer(const ENetPeer&) = delete;
ENetPeer(ENetPeer&&) = default;
~ENetPeer() = default;
@ -186,7 +185,6 @@ namespace Nz
std::size_t m_totalWaitingData;
std::vector<Acknowledgement> m_acknowledgements;
std::vector<Channel> m_channels;
MemoryPool m_packetPool;
ENetPeerState m_state;
UInt8 m_incomingSessionID;
UInt8 m_outgoingSessionID;

View File

@ -8,6 +8,15 @@
namespace Nz
{
inline ENetPeer::ENetPeer(ENetHost* host, UInt16 peerId) :
m_host(host),
m_incomingSessionID(0xFF),
m_outgoingSessionID(0xFF),
m_incomingPeerID(peerId)
{
Reset();
}
inline const IpAddress& ENetPeer::GetAddress() const
{
return m_address;

View File

@ -45,10 +45,7 @@ namespace Nz
void ENetHost::Broadcast(UInt8 channelId, ENetPacketFlags flags, NetPacket&& packet)
{
ENetPacketRef enetPacket = m_packetPool.New<ENetPacket>();
enetPacket->flags = flags;
enetPacket->data = std::move(packet);
enetPacket->owner = &m_packetPool;
ENetPacketRef enetPacket = AllocatePacket(flags, std::move(packet));
for (ENetPeer& peer : m_peers)
{
@ -309,6 +306,15 @@ namespace Nz
}
}
ENetPacketRef ENetHost::AllocatePacket(ENetPacketFlags flags)
{
ENetPacketRef enetPacket = m_packetPool.New<ENetPacket>();
enetPacket->flags = flags;
enetPacket->owner = &m_packetPool;
return enetPacket;
}
bool ENetHost::InitSocket(const IpAddress& address)
{
if (!m_socket.Create(address.GetProtocol()))
@ -841,7 +847,7 @@ namespace Nz
{
UInt32 windowSize = (peer->m_packetThrottle * peer->m_windowSize) / ENetPeer_PacketThrottleScale;
if (peer->m_reliableDataInTransit + outgoingCommand->fragmentLength > std::max(windowSize, peer->m_mtu))
if (peer->m_reliableDataInTransit + outgoingCommand->fragmentLength > std::max(windowSize, peer->GetMtu()))
windowExceeded = true;
}
@ -855,8 +861,8 @@ namespace Nz
canPing = false;
std::size_t commandSize = s_commandSizes[outgoingCommand->command.header.command & ENetProtocolCommand_Mask];
if (m_commandCount >= m_commands.size() || m_bufferCount + 1 >= m_buffers.size() || peer->m_mtu - m_packetSize < commandSize ||
(outgoingCommand->packet && UInt16(peer->m_mtu - m_packetSize) < UInt16(commandSize + outgoingCommand->fragmentLength)))
if (m_commandCount >= m_commands.size() || m_bufferCount + 1 >= m_buffers.size() || peer->GetMtu() - m_packetSize < commandSize ||
(outgoingCommand->packet && UInt16(peer->GetMtu() - m_packetSize) < UInt16(commandSize + outgoingCommand->fragmentLength)))
{
m_continueSending = true;
break;
@ -1092,6 +1098,7 @@ namespace Nz
m_packetSize += packetBuffer.dataLength;
// In order to keep the packet buffer alive until we send it, place it into a temporary queue
peer->m_sentUnreliableCommands.emplace_back(std::move(*outgoingCommand));
}

View File

@ -20,16 +20,6 @@
namespace Nz
{
ENetPeer::ENetPeer(ENetHost* host, UInt16 peerId) :
m_host(host),
m_packetPool(sizeof(ENetPacket)),
m_incomingSessionID(0xFF),
m_outgoingSessionID(0xFF),
m_incomingPeerID(peerId)
{
Reset();
}
void ENetPeer::Disconnect(UInt32 data)
{
if (m_state == ENetPeerState::Disconnecting ||
@ -184,12 +174,7 @@ namespace Nz
bool ENetPeer::Send(UInt8 channelId, ENetPacketFlags flags, NetPacket&& packet)
{
ENetPacket* enetPacket = m_packetPool.New<ENetPacket>();
enetPacket->flags = flags;
enetPacket->data = std::move(packet);
enetPacket->owner = &m_packetPool;
return Send(channelId, enetPacket);
return Send(channelId, m_host->AllocatePacket(flags, std::move(packet));
}
bool ENetPeer::Send(UInt8 channelId, ENetPacketRef packetRef)
@ -1214,10 +1199,8 @@ namespace Nz
if (m_totalWaitingData >= m_host->m_maximumWaitingData)
return nullptr;
ENetPacket* packet = m_packetPool.New<ENetPacket>();
packet->flags = flags;
ENetPacketRef packet = m_host->AllocatePacket(flags);
packet->data.Reset(0, data, dataLength);
packet->owner = &m_packetPool;
IncomingCommmand incomingCommand;
incomingCommand.reliableSequenceNumber = command.header.reliableSequenceNumber;