Merge pull request #125 from DigitalPulseSoftware/enet_wip_nothing_to_see_here

ENet support
This commit is contained in:
Jérôme Leclercq 2017-06-10 22:31:17 +02:00 committed by GitHub
commit fd24583fdf
15 changed files with 3776 additions and 9 deletions

View File

@ -59,11 +59,11 @@ namespace Ndk
friend EntityList;
public:
inline iterator(const iterator& iterator);
inline iterator(const iterator& it);
const EntityHandle& operator*() const;
inline iterator& operator=(const iterator& iterator);
inline iterator& operator=(const iterator& it);
inline iterator& operator++();
inline iterator operator++(int);

View File

@ -207,16 +207,16 @@ namespace Ndk
{
}
inline EntityList::iterator::iterator(const iterator& iterator) :
m_nextEntityId(iterator.m_nextEntityId),
m_list(iterator.m_list)
inline EntityList::iterator::iterator(const iterator& it) :
m_nextEntityId(it.m_nextEntityId),
m_list(it.m_list)
{
}
inline EntityList::iterator& EntityList::iterator::operator=(const iterator& iterator)
inline EntityList::iterator& EntityList::iterator::operator=(const iterator& it)
{
m_nextEntityId = iterator.m_nextEntityId;
m_list = iterator.m_list;
m_nextEntityId = it.m_nextEntityId;
m_list = it.m_list;
return *this;
}

View File

@ -12,6 +12,7 @@
#include <Nazara/Network/Enums.hpp>
#include <functional>
#include <tuple>
#include <type_traits>
namespace Nz
{
@ -19,6 +20,12 @@ namespace Nz
NAZARA_NETWORK_API const char* ErrorToString(Nz::SocketError socketError);
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

@ -0,0 +1,166 @@
/*
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_ENETHOST_HPP
#define NAZARA_ENETHOST_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Bitset.hpp>
#include <Nazara/Core/Clock.hpp>
#include <Nazara/Core/MemoryPool.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 <deque>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
namespace Nz
{
class NAZARA_NETWORK_API ENetHost
{
friend ENetPeer;
friend class Network;
public:
inline ENetHost();
ENetHost(const ENetHost&) = delete;
ENetHost(ENetHost&&) = default;
inline ~ENetHost();
void Broadcast(UInt8 channelId, ENetPacketFlags flags, NetPacket&& packet);
bool CheckEvents(ENetEvent* event);
ENetPeer* Connect(const IpAddress& remoteAddress, std::size_t channelCount = 0, UInt32 data = 0);
ENetPeer* Connect(const String& hostName, NetProtocol protocol = NetProtocol_Any, const String& service = "http", ResolveError* error = nullptr, std::size_t channelCount = 0, UInt32 data = 0);
inline bool Create(NetProtocol protocol, UInt16 port, std::size_t peerCount, std::size_t channelCount = 0);
bool Create(const IpAddress& address, std::size_t peerCount, std::size_t channelCount = 0);
bool Create(const IpAddress& address, std::size_t peerCount, std::size_t channelCount, UInt32 incomingBandwidth, UInt32 outgoingBandwidth);
void Destroy();
void Flush();
inline Nz::IpAddress GetBoundAddress() const;
inline UInt32 GetServiceTime() const;
int Service(ENetEvent* event, UInt32 timeout);
void SimulateNetwork(double packetLossProbability, UInt16 minDelay, UInt16 maxDelay);
ENetHost& operator=(const ENetHost&) = delete;
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);
void RemoveFromDispatchQueue(ENetPeer* peer);
bool DispatchIncomingCommands(ENetEvent* event);
ENetPeer* HandleConnect(ENetProtocolHeader* header, ENetProtocol* command);
bool HandleIncomingCommands(ENetEvent* event);
int ReceiveIncomingCommands(ENetEvent* event);
void NotifyConnect(ENetPeer* peer, ENetEvent* event, bool incoming);
void NotifyDisconnect(ENetPeer*, ENetEvent* event);
void SendAcknowledgements(ENetPeer* peer);
bool SendReliableOutgoingCommands(ENetPeer* peer);
int SendOutgoingCommands(ENetEvent* event, bool checkForTimeouts);
void SendUnreliableOutgoingCommands(ENetPeer* peer);
void ThrottleBandwidth();
static std::size_t GetCommandSize(UInt8 commandNumber);
static bool Initialize();
static void Uninitialize();
struct PendingIncomingPacket
{
IpAddress from;
NetPacket data;
UInt32 deliveryTime;
};
struct PendingOutgoingPacket
{
IpAddress to;
NetPacket data;
UInt32 deliveryTime;
};
std::array<ENetProtocol, ENetConstants::ENetProtocol_MaximumPacketCommands> m_commands;
std::array<NetBuffer, ENetConstants::ENetProtocol_MaximumPacketCommands * 2 + 1> m_buffers;
std::array<UInt8, ENetConstants::ENetProtocol_MaximumMTU> m_packetData[2];
std::bernoulli_distribution m_packetLossProbability;
std::size_t m_bandwidthLimitedPeers;
std::size_t m_bufferCount;
std::size_t m_channelLimit;
std::size_t m_commandCount;
std::size_t m_duplicatePeers;
std::size_t m_maximumPacketSize;
std::size_t m_maximumWaitingData;
std::size_t m_packetSize;
std::size_t m_peerCount;
std::size_t m_receivedDataLength;
std::uniform_int_distribution<UInt16> m_packetDelayDistribution;
std::vector<ENetPeer> m_peers;
std::vector<PendingIncomingPacket> m_pendingIncomingPackets;
std::vector<PendingOutgoingPacket> m_pendingOutgoingPackets;
UInt8* m_receivedData;
Bitset<UInt64> m_dispatchQueue;
MemoryPool m_packetPool;
IpAddress m_address;
IpAddress m_receivedAddress;
SocketPoller m_poller;
UdpSocket m_socket;
UInt16 m_headerFlags;
UInt32 m_bandwidthThrottleEpoch;
UInt32 m_connectedPeers;
UInt32 m_mtu;
UInt32 m_randomSeed;
UInt32 m_incomingBandwidth;
UInt32 m_outgoingBandwidth;
UInt32 m_serviceTime;
UInt32 m_totalSentPackets;
UInt32 m_totalReceivedPackets;
UInt64 m_totalSentData;
UInt64 m_totalReceivedData;
bool m_continueSending;
bool m_isSimulationEnabled;
bool m_recalculateBandwidthLimits;
static std::mt19937 s_randomGenerator;
static std::mt19937_64 s_randomGenerator64;
};
}
#include <Nazara/Network/ENetHost.inl>
#endif // NAZARA_ENETHOST_HPP

View File

@ -0,0 +1,74 @@
// 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/ENetHost.hpp>
#include <utility>
#include <Nazara/Network/Debug.hpp>
namespace Nz
{
inline ENetHost::ENetHost() :
m_packetPool(sizeof(ENetPacket)),
m_isSimulationEnabled(false)
{
}
inline ENetHost::~ENetHost()
{
Destroy();
}
inline bool ENetHost::Create(NetProtocol protocol, UInt16 port, std::size_t peerCount, std::size_t channelCount)
{
NazaraAssert(protocol != NetProtocol_Any, "Any protocol not supported for Listen"); //< TODO
NazaraAssert(protocol != NetProtocol_Unknown, "Invalid protocol");
IpAddress any;
switch (protocol)
{
case NetProtocol_Any:
case NetProtocol_Unknown:
NazaraInternalError("Invalid protocol Any at this point");
return false;
case NetProtocol_IPv4:
any = IpAddress::AnyIpV4;
break;
case NetProtocol_IPv6:
any = IpAddress::AnyIpV6;
break;
}
any.SetPort(port);
return Create(any, peerCount, channelCount);
}
inline void ENetHost::Destroy()
{
m_poller.Clear();
m_peers.clear();
m_socket.Close();
}
inline Nz::IpAddress ENetHost::GetBoundAddress() const
{
return m_address;
}
inline UInt32 Nz::ENetHost::GetServiceTime() const
{
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

@ -0,0 +1,107 @@
// 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_ENETPACKET_HPP
#define NAZARA_ENETPACKET_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Network/NetPacket.hpp>
namespace Nz
{
enum ENetPacketFlag
{
ENetPacketFlag_Reliable,
ENetPacketFlag_Unsequenced,
ENetPacketFlag_UnreliableFragment
};
template<>
struct EnumAsFlags<ENetPacketFlag>
{
static constexpr bool value = true;
static constexpr int max = ENetPacketFlag_UnreliableFragment;
};
using ENetPacketFlags = Flags<ENetPacketFlag>;
constexpr ENetPacketFlags ENetPacketFlag_Unreliable = 0;
class MemoryPool;
struct ENetPacket
{
MemoryPool* owner;
ENetPacketFlags flags;
NetPacket data;
std::size_t referenceCount = 0;
};
struct NAZARA_NETWORK_API ENetPacketRef
{
ENetPacketRef() = default;
ENetPacketRef(ENetPacket* packet)
{
Reset(packet);
}
ENetPacketRef(const ENetPacketRef& packet) :
ENetPacketRef()
{
Reset(packet);
}
ENetPacketRef(ENetPacketRef&& packet) :
m_packet(packet.m_packet)
{
packet.m_packet = nullptr;
}
~ENetPacketRef()
{
Reset();
}
void Reset(ENetPacket* packet = nullptr);
operator ENetPacket*() const
{
return m_packet;
}
ENetPacket* operator->() const
{
return m_packet;
}
ENetPacketRef& operator=(ENetPacket* packet)
{
Reset(packet);
return *this;
}
ENetPacketRef& operator=(const ENetPacketRef& packet)
{
Reset(packet);
return *this;
}
ENetPacketRef& operator=(ENetPacketRef&& packet)
{
m_packet = packet.m_packet;
packet.m_packet = nullptr;
return *this;
}
ENetPacket* m_packet = nullptr;
};
}
#endif // NAZARA_ENETPACKET_HPP

View File

@ -0,0 +1,248 @@
/*
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_ENETPEER_HPP
#define NAZARA_ENETPEER_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Bitset.hpp>
#include <Nazara/Core/Clock.hpp>
#include <Nazara/Network/ENetPacket.hpp>
#include <Nazara/Network/ENetProtocol.hpp>
#include <Nazara/Network/IpAddress.hpp>
#include <Nazara/Network/NetPacket.hpp>
#include <Nazara/Network/UdpSocket.hpp>
#include <array>
#include <list>
#include <random>
#include <vector>
namespace Nz
{
class ENetHost;
class NAZARA_NETWORK_API ENetPeer
{
friend ENetHost;
friend struct PacketRef;
public:
inline ENetPeer(ENetHost* host, UInt16 peerId);
ENetPeer(const ENetPeer&) = delete;
ENetPeer(ENetPeer&&) = default;
~ENetPeer() = default;
void Disconnect(UInt32 data);
void DisconnectLater(UInt32 data);
void DisconnectNow(UInt32 data);
inline const IpAddress& GetAddress() const;
inline UInt32 GetMtu() const;
inline UInt32 GetPacketThrottleAcceleration() const;
inline UInt32 GetPacketThrottleDeceleration() const;
inline UInt32 GetPacketThrottleInterval() const;
inline UInt16 GetPeerId() const;
inline UInt32 GetRoundTripTime() const;
inline ENetPeerState GetState() const;
inline bool HasPendingCommands();
inline bool IsConnected() const;
inline bool IsSimulationEnabled() const;
void Ping();
bool Receive(ENetPacketRef* packet, UInt8* channelId);
void Reset();
bool Send(UInt8 channelId, ENetPacketRef packetRef);
bool Send(UInt8 channelId, ENetPacketFlags flags, NetPacket&& packet);
void SimulateNetwork(double packetLossProbability, UInt16 minDelay, UInt16 maxDelay);
void ThrottleConfigure(UInt32 interval, UInt32 acceleration, UInt32 deceleration);
ENetPeer& operator=(const ENetPeer&) = delete;
ENetPeer& operator=(ENetPeer&&) = default;
private:
void InitIncoming(std::size_t channelCount, const IpAddress& address, ENetProtocolConnect& incomingCommand);
void InitOutgoing(std::size_t channelCount, const IpAddress& address, UInt32 connectId, UInt32 windowSize);
struct Acknowledgement;
struct Channel;
struct IncomingCommmand;
struct OutgoingCommand;
inline void ChangeState(ENetPeerState state);
bool CheckTimeouts(ENetEvent* event);
void DispatchState(ENetPeerState state);
void DispatchIncomingReliableCommands(Channel& channel);
void DispatchIncomingUnreliableCommands(Channel& channel);
bool HandleAcknowledge(const ENetProtocol* command, ENetEvent* event);
bool HandleBandwidthLimit(const ENetProtocol* command);
bool HandleDisconnect(const ENetProtocol* command);
bool HandlePing(const ENetProtocol* command);
bool HandleSendFragment(const ENetProtocol* command, UInt8** data);
bool HandleSendReliable(const ENetProtocol* command, UInt8** data);
bool HandleSendUnreliable(const ENetProtocol* command, UInt8** data);
bool HandleSendUnreliableFragment(const ENetProtocol* command, UInt8** data);
bool HandleSendUnsequenced(const ENetProtocol* command, UInt8** data);
bool HandleThrottleConfigure(const ENetProtocol* command);
bool HandleVerifyConnect(const ENetProtocol* command, ENetEvent* event);
void OnConnect();
void OnDisconnect();
ENetProtocolCommand RemoveSentReliableCommand(UInt16 reliableSequenceNumber, UInt8 channelId);
void RemoveSentUnreliableCommands();
void ResetQueues();
bool QueueAcknowledgement(ENetProtocol* command, UInt16 sentTime);
IncomingCommmand* QueueIncomingCommand(const ENetProtocol& command, const void* data, std::size_t dataLength, UInt32 flags, UInt32 fragmentCount);
inline void QueueOutgoingCommand(ENetProtocol& command);
void QueueOutgoingCommand(ENetProtocol& command, ENetPacketRef packet, UInt32 offset, UInt16 length);
void SetupOutgoingCommand(OutgoingCommand& outgoingCommand);
int Throttle(UInt32 rtt);
struct Acknowledgement
{
ENetProtocol command;
UInt32 sentTime;
};
struct Channel
{
Channel()
{
incomingReliableSequenceNumber = 0;
incomingUnreliableSequenceNumber = 0;
outgoingReliableSequenceNumber = 0;
outgoingUnreliableSequenceNumber = 0;
usedReliableWindows = 0;
reliableWindows.fill(0);
}
std::array<UInt16, ENetPeer_ReliableWindows> reliableWindows;
std::list<IncomingCommmand> incomingReliableCommands;
std::list<IncomingCommmand> incomingUnreliableCommands;
UInt16 incomingReliableSequenceNumber;
UInt16 incomingUnreliableSequenceNumber;
UInt16 outgoingReliableSequenceNumber;
UInt16 outgoingUnreliableSequenceNumber;
UInt16 usedReliableWindows;
};
struct IncomingCommmand
{
ENetProtocol command;
Bitset<> fragments;
ENetPacketRef packet;
UInt16 reliableSequenceNumber;
UInt16 unreliableSequenceNumber;
UInt32 fragmentsRemaining;
};
struct OutgoingCommand
{
ENetProtocol command;
ENetPacketRef packet;
UInt16 fragmentLength;
UInt16 reliableSequenceNumber;
UInt16 sendAttempts;
UInt16 unreliableSequenceNumber;
UInt32 fragmentOffset;
UInt32 roundTripTimeout;
UInt32 roundTripTimeoutLimit;
UInt32 sentTime;
};
static constexpr std::size_t unsequencedWindow = ENetPeer_ReliableWindowSize / 32;
ENetHost* m_host;
IpAddress m_address; /**< Internet address of the peer */
std::array<UInt32, unsequencedWindow> m_unsequencedWindow;
std::bernoulli_distribution m_packetLossProbability;
std::list<IncomingCommmand> m_dispatchedCommands;
std::list<OutgoingCommand> m_outgoingReliableCommands;
std::list<OutgoingCommand> m_outgoingUnreliableCommands;
std::list<OutgoingCommand> m_sentReliableCommands;
std::list<OutgoingCommand> m_sentUnreliableCommands;
std::size_t m_totalWaitingData;
std::uniform_int_distribution<UInt16> m_packetDelayDistribution;
std::vector<Acknowledgement> m_acknowledgements;
std::vector<Channel> m_channels;
ENetPeerState m_state;
UInt8 m_incomingSessionID;
UInt8 m_outgoingSessionID;
UInt16 m_incomingPeerID;
UInt16 m_incomingUnsequencedGroup;
UInt16 m_outgoingPeerID;
UInt16 m_outgoingReliableSequenceNumber;
UInt16 m_outgoingUnsequencedGroup;
UInt32 m_connectID;
UInt32 m_earliestTimeout;
UInt32 m_eventData;
UInt32 m_highestRoundTripTimeVariance;
UInt32 m_incomingBandwidth; /**< Downstream bandwidth of the client in bytes/second */
UInt32 m_incomingBandwidthThrottleEpoch;
UInt32 m_incomingDataTotal;
UInt32 m_lastReceiveTime;
UInt32 m_lastRoundTripTime;
UInt32 m_lastRoundTripTimeVariance;
UInt32 m_lastSendTime;
UInt32 m_lowestRoundTripTime;
UInt32 m_mtu;
UInt32 m_nextTimeout;
UInt32 m_outgoingBandwidth; /**< Upstream bandwidth of the client in bytes/second */
UInt32 m_outgoingBandwidthThrottleEpoch;
UInt32 m_outgoingDataTotal;
UInt32 m_packetLoss; /**< mean packet loss of reliable packets as a ratio with respect to the constant ENET_PEER_PACKET_LOSS_SCALE */
UInt32 m_packetLossEpoch;
UInt32 m_packetLossVariance;
UInt32 m_packetThrottle;
UInt32 m_packetThrottleAcceleration;
UInt32 m_packetThrottleCounter;
UInt32 m_packetThrottleDeceleration;
UInt32 m_packetThrottleEpoch;
UInt32 m_packetThrottleInterval;
UInt32 m_packetThrottleLimit;
UInt32 m_packetsLost;
UInt32 m_packetsSent;
UInt32 m_pingInterval;
UInt32 m_reliableDataInTransit;
UInt32 m_roundTripTime; /**< mean round trip time (RTT), in milliseconds, between sending a reliable packet and receiving its acknowledgment */
UInt32 m_roundTripTimeVariance;
UInt32 m_timeoutLimit;
UInt32 m_timeoutMaximum;
UInt32 m_timeoutMinimum;
UInt32 m_windowSize;
UInt64 m_totalPacketLost;
UInt64 m_totalPacketSent;
bool m_isSimulationEnabled;
};
}
#include <Nazara/Network/ENetPeer.inl>
#endif // NAZARA_ENETPEER_HPP

View File

@ -0,0 +1,92 @@
// 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/ENetPeer.hpp>
#include <utility>
#include <Nazara/Network/Debug.hpp>
namespace Nz
{
inline ENetPeer::ENetPeer(ENetHost* host, UInt16 peerId) :
m_host(host),
m_incomingSessionID(0xFF),
m_outgoingSessionID(0xFF),
m_incomingPeerID(peerId),
m_isSimulationEnabled(false)
{
Reset();
}
inline const IpAddress& ENetPeer::GetAddress() const
{
return m_address;
}
inline UInt32 ENetPeer::GetMtu() const
{
return m_mtu;
}
inline UInt32 ENetPeer::GetPacketThrottleAcceleration() const
{
return m_packetThrottleAcceleration;
}
inline UInt32 ENetPeer::GetPacketThrottleDeceleration() const
{
return m_packetThrottleDeceleration;
}
inline UInt32 ENetPeer::GetPacketThrottleInterval() const
{
return m_packetThrottleInterval;
}
inline UInt16 ENetPeer::GetPeerId() const
{
return m_incomingPeerID;
}
inline UInt32 ENetPeer::GetRoundTripTime() const
{
return m_roundTripTime;
}
inline ENetPeerState ENetPeer::GetState() const
{
return m_state;
}
inline bool ENetPeer::HasPendingCommands()
{
return m_outgoingReliableCommands.empty() && m_outgoingUnreliableCommands.empty() && m_sentReliableCommands.empty();
}
inline bool ENetPeer::IsConnected() const
{
return m_state == ENetPeerState::Connected || m_state == ENetPeerState::DisconnectLater;
}
inline bool ENetPeer::IsSimulationEnabled() const
{
return m_isSimulationEnabled;
}
inline void ENetPeer::ChangeState(ENetPeerState state)
{
if (state == ENetPeerState::Connected || state == ENetPeerState::DisconnectLater)
OnConnect();
else
OnDisconnect();
m_state = state;
}
inline void ENetPeer::QueueOutgoingCommand(ENetProtocol& command)
{
QueueOutgoingCommand(command, ENetPacketRef(), 0, 0);
}
}
#include <Nazara/Network/DebugOff.hpp>

View File

@ -0,0 +1,311 @@
// 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_ENETPROTOCOL_HPP
#define NAZARA_ENETPROTOCOL_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Network/ENetPacket.hpp>
#include <vector>
namespace Nz
{
constexpr UInt32 ENetTimeOverflow = 24 * 60 * 60 * 1000;
inline UInt32 ENetTimeDifference(UInt32 a, UInt32 b);
inline bool ENetTimeLess(UInt32 a, UInt32 b);
inline bool ENetTimeLessEqual(UInt32 a, UInt32 b);
inline bool ENetTimeGreater(UInt32 a, UInt32 b);
inline bool ENetTimeGreaterEqual(UInt32 a, UInt32 b);
class ENetPeer;
// Constants for the ENet implementation and protocol
enum ENetConstants
{
ENetHost_BandwidthThrottleInterval = 1000,
ENetHost_DefaultMaximumPacketSize = 32 * 1024 * 1024,
ENetHost_DefaultMaximumWaitingData = 32 * 1024 * 1024,
ENetHost_DefaultMTU = 1400,
ENetHost_ReceiveBufferSize = 256 * 1024,
ENetHost_SendBufferSize = 256 * 1024,
ENetPeer_DefaultPacketThrottle = 32,
ENetPeer_DefaultRoundTripTime = 500,
ENetPeer_FreeReliableWindows = 8,
ENetPeer_FreeUnsequencedWindows = 32,
ENetPeer_PacketLossInterval = 10000,
ENetPeer_PacketLossScale = (1 << 16),
ENetPeer_PacketThrottleAcceleration = 2,
ENetPeer_PacketThrottleCounter = 7,
ENetPeer_PacketThrottleDeceleration = 2,
ENetPeer_PacketThrottleInterval = 5000,
ENetPeer_PacketThrottleScale = 32,
ENetPeer_PingInterval = 500,
ENetPeer_ReliableWindows = 16,
ENetPeer_ReliableWindowSize = 0x1000,
ENetPeer_TimeoutLimit = 32,
ENetPeer_TimeoutMaximum = 30000,
ENetPeer_TimeoutMinimum = 5000,
ENetPeer_UnsequencedWindows = 64,
ENetPeer_UnsequencedWindowSize = 1024,
ENetPeer_WindowSizeScale = 64 * 1024,
ENetProtocol_MaximumChannelCount = 255,
ENetProtocol_MaximumFragmentCount = 1024 * 1024,
ENetProtocol_MaximumMTU = 4096,
ENetProtocol_MaximumPacketCommands = 32,
ENetProtocol_MaximumPeerId = 0xFFF,
ENetProtocol_MaximumWindowSize = 65536,
ENetProtocol_MinimumChannelCount = 1,
ENetProtocol_MinimumMTU = 576,
ENetProtocol_MinimumWindowSize = 4096
};
enum class ENetPeerState
{
AcknowledgingConnect = 2,
AcknowledgingDisconnect = 8,
Connecting = 1,
ConnectionPending = 3,
ConnectionSucceeded = 4,
Connected = 5,
Disconnected = 0,
Disconnecting = 7,
DisconnectLater = 6,
Zombie = 9
};
enum ENetProtocolCommand
{
// Keeping the values is important for compatibility with the native ENet protocol
ENetProtocolCommand_Acknowledge = 1,
ENetProtocolCommand_BandwidthLimit = 10,
ENetProtocolCommand_Connect = 2,
ENetProtocolCommand_Disconnect = 4,
ENetProtocolCommand_None = 0,
ENetProtocolCommand_Ping = 5,
ENetProtocolCommand_SendFragment = 8,
ENetProtocolCommand_SendReliable = 6,
ENetProtocolCommand_SendUnreliable = 7,
ENetProtocolCommand_SendUnreliableFragment = 12,
ENetProtocolCommand_SendUnsequenced = 9,
ENetProtocolCommand_ThrottleConfigure = 11,
ENetProtocolCommand_VerifyConnect = 3,
ENetProtocolCommand_Count = 13,
ENetProtocolCommand_Mask = 0x0F
};
enum ENetProtocolFlag
{
ENetProtocolFlag_Acknowledge = (1 << 7),
ENetProtocolFlag_Unsequenced = (1 << 6),
ENetProtocolHeaderFlag_Compressed = (1 << 14),
ENetProtocolHeaderFlag_SentTime = (1 << 15),
ENetProtocolHeaderFlag_Mask = ENetProtocolHeaderFlag_Compressed | ENetProtocolHeaderFlag_SentTime,
ENetProtocolHeaderSessionMask = (3 << 12),
ENetProtocolHeaderSessionShift = 12
};
enum class ENetEventType
{
/** no event occurred within the specified time limit */
None,
/** a peer has disconnected. This event is generated on a successful
* completion of a disconnect initiated by enet_peer_disconnect, if
* a peer has timed out, or if a connection request initialized by
* enet_host_connect has timed out. The peer field contains the peer
* which disconnected. The data field contains user supplied data
* describing the disconnection, or 0, if none is available.
*/
Disconnect,
/** a connection request initiated by enet_host_connect from this host has completed.
* The peer field contains the peer which successfully connected.
*/
OutgoingConnect,
/** a connection request initiated by enet_host_connect from another host has completed.
* The peer field contains the peer which successfully connected.
*/
IncomingConnect,
/** a packet has been received from a peer. The peer field specifies the
* peer which sent the packet. The channelID field specifies the channel
* number upon which the packet was received. The packet field contains
* the packet that was received;
*/
Receive
};
struct ENetEvent
{
ENetEventType type;
ENetPeer* peer;
UInt8 channelId;
UInt32 data;
ENetPacketRef packet;
};
#ifdef _MSC_VER
#pragma pack(push, 1)
#define NAZARA_PACKED
#elif defined(__GNUC__) || defined(__clang__)
#define NAZARA_PACKED __attribute__ ((packed))
#else
#define NAZARA_PACKED
#endif
struct NAZARA_PACKED ENetProtocolHeader
{
UInt16 peerID;
UInt16 sentTime;
};
struct NAZARA_PACKED ENetProtocolCommandHeader
{
UInt8 command;
UInt8 channelID;
UInt16 reliableSequenceNumber;
};
struct NAZARA_PACKED ENetProtocolAcknowledge
{
ENetProtocolCommandHeader header;
UInt16 receivedReliableSequenceNumber;
UInt16 receivedSentTime;
};
struct NAZARA_PACKED ENetProtocolConnect
{
ENetProtocolCommandHeader header;
UInt16 outgoingPeerID;
UInt8 incomingSessionID;
UInt8 outgoingSessionID;
UInt32 mtu;
UInt32 windowSize;
UInt32 channelCount;
UInt32 incomingBandwidth;
UInt32 outgoingBandwidth;
UInt32 packetThrottleInterval;
UInt32 packetThrottleAcceleration;
UInt32 packetThrottleDeceleration;
UInt32 connectID;
UInt32 data;
};
struct NAZARA_PACKED ENetProtocolBandwidthLimit
{
ENetProtocolCommandHeader header;
UInt32 incomingBandwidth;
UInt32 outgoingBandwidth;
};
struct NAZARA_PACKED ENetProtocolDisconnect
{
ENetProtocolCommandHeader header;
UInt32 data;
};
struct NAZARA_PACKED ENetProtocolPing
{
ENetProtocolCommandHeader header;
};
struct NAZARA_PACKED ENetProtocolSendFragment
{
ENetProtocolCommandHeader header;
UInt16 startSequenceNumber;
UInt16 dataLength;
UInt32 fragmentCount;
UInt32 fragmentNumber;
UInt32 totalLength;
UInt32 fragmentOffset;
};
struct NAZARA_PACKED ENetProtocolSendReliable
{
ENetProtocolCommandHeader header;
UInt16 dataLength;
};
struct NAZARA_PACKED ENetProtocolSendUnreliable
{
ENetProtocolCommandHeader header;
UInt16 unreliableSequenceNumber;
UInt16 dataLength;
};
struct NAZARA_PACKED ENetProtocolSendUnsequenced
{
ENetProtocolCommandHeader header;
UInt16 unsequencedGroup;
UInt16 dataLength;
};
struct NAZARA_PACKED ENetProtocolThrottleConfigure
{
ENetProtocolCommandHeader header;
UInt32 packetThrottleInterval;
UInt32 packetThrottleAcceleration;
UInt32 packetThrottleDeceleration;
};
struct NAZARA_PACKED ENetProtocolVerifyConnect
{
ENetProtocolCommandHeader header;
UInt16 outgoingPeerID;
UInt8 incomingSessionID;
UInt8 outgoingSessionID;
UInt32 mtu;
UInt32 windowSize;
UInt32 channelCount;
UInt32 incomingBandwidth;
UInt32 outgoingBandwidth;
UInt32 packetThrottleInterval;
UInt32 packetThrottleAcceleration;
UInt32 packetThrottleDeceleration;
UInt32 connectID;
};
union NAZARA_PACKED ENetProtocol
{
ENetProtocol() = default;
ENetProtocol(UInt8 command, UInt8 channel)
{
header.command = command;
header.channelID = channel;
}
ENetProtocolCommandHeader header;
ENetProtocolAcknowledge acknowledge;
ENetProtocolBandwidthLimit bandwidthLimit;
ENetProtocolConnect connect;
ENetProtocolDisconnect disconnect;
ENetProtocolPing ping;
ENetProtocolSendReliable sendReliable;
ENetProtocolSendUnreliable sendUnreliable;
ENetProtocolSendUnsequenced sendUnsequenced;
ENetProtocolSendFragment sendFragment;
ENetProtocolThrottleConfigure throttleConfigure;
ENetProtocolVerifyConnect verifyConnect;
};
#ifdef _MSC_VER
#pragma pack(pop)
#endif
}
#include <Nazara/Network/ENetProtocol.inl>
#endif // NAZARA_ENETPROTOCOL_HPP

View File

@ -0,0 +1,34 @@
// 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/ENetProtocol.hpp>
#include <Nazara/Network/Debug.hpp>
namespace Nz
{
UInt32 ENetTimeDifference(UInt32 a, UInt32 b)
{
return (ENetTimeLess(a, b)) ? b - a : a - b;
}
bool ENetTimeLess(UInt32 a, UInt32 b)
{
return (a - b >= ENetTimeOverflow);
}
bool ENetTimeLessEqual(UInt32 a, UInt32 b)
{
return !ENetTimeGreater(a, b);
}
bool ENetTimeGreater(UInt32 a, UInt32 b)
{
return ENetTimeLess(b, a);
}
bool ENetTimeGreaterEqual(UInt32 a, UInt32 b)
{
return !ENetTimeLess(a, b);
}
}

View File

@ -155,7 +155,9 @@ namespace Nz
{
InitStream(HeaderSize + size, HeaderSize, OpenMode_ReadOnly);
m_buffer->Resize(HeaderSize + size);
std::memcpy(m_buffer->GetBuffer() + HeaderSize, ptr, size);
if (ptr)
std::memcpy(m_buffer->GetBuffer() + HeaderSize, ptr, size);
m_netCode = netCode;
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,24 @@
// 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/ENetPacket.hpp>
#include <Nazara/Core/MemoryPool.hpp>
#include <Nazara/Network/Debug.hpp>
namespace Nz
{
/// Temporary
void ENetPacketRef::Reset(ENetPacket* packet)
{
if (m_packet)
{
if (--m_packet->referenceCount == 0)
m_packet->owner->Delete(m_packet);
}
m_packet = packet;
if (m_packet)
m_packet->referenceCount++;
}
}

File diff suppressed because it is too large Load Diff