Commit progression

This commit is contained in:
Lynix
2017-01-27 14:48:31 +01:00
parent 12b4073033
commit 8a59dc88b8
11 changed files with 865 additions and 811 deletions

View File

@@ -1,4 +1,4 @@
// Copyright (C) 2017 Jérôme Leclercq
// 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
@@ -14,6 +14,7 @@
#include <Nazara/Network/ENetHost.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>
@@ -40,8 +41,10 @@ namespace Nz
void Broadcast(UInt8 channelId, ENetPacketFlags flags, NetPacket&& packet);
bool Connect(const IpAddress& remoteAddress, std::size_t channelCount = 0, UInt32 data = 0);
bool Connect(const String& hostName, NetProtocol protocol = NetProtocol_Any, const String& service = "http", ResolveError* error = nullptr, std::size_t channelCount = 0, UInt32 data = 0);
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);
@@ -58,8 +61,10 @@ namespace Nz
private:
bool InitSocket(const IpAddress& address);
inline void AddToDispatchQueue(ENetPeer* peer);
inline void RemoveFromDispatchQueue(ENetPeer* peer);
void AddToDispatchQueue(ENetPeer* peer);
void RemoveFromDispatchQueue(ENetPeer* peer);
bool CheckTimeouts(ENetPeer* peer, ENetEvent* event);
bool DispatchIncomingCommands(ENetEvent* event);
@@ -69,7 +74,11 @@ namespace Nz
bool HandleDisconnect(ENetPeer* peer, const ENetProtocol* command);
bool HandleIncomingCommands(ENetEvent* event);
bool HandlePing(ENetPeer* peer, const ENetProtocol* command);
bool HandleSendFragment(ENetPeer* peer, const ENetProtocol* command, UInt8** currentData);
bool HandleSendReliable(ENetPeer* peer, const ENetProtocol* command, UInt8** currentData);
bool HandleSendUnreliable(ENetPeer* peer, const ENetProtocol* command, UInt8** currentData);
bool HandleSendUnreliableFragment(ENetPeer* peer, const ENetProtocol* command, UInt8** currentData);
bool HandleSendUnsequenced(ENetPeer* peer, const ENetProtocol* command, UInt8** currentData);
bool HandleThrottleConfigure(ENetPeer* peer, const ENetProtocol* command);
bool HandleVerifyConnect(ENetEvent* event, ENetPeer* peer, ENetProtocol* command);
@@ -78,12 +87,19 @@ namespace Nz
void NotifyConnect(ENetPeer* peer, ENetEvent* event);
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();
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;
@@ -93,6 +109,8 @@ namespace Nz
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::vector<ENetPeer> m_peers;
Bitset<UInt64> m_dispatchQueue;
@@ -101,6 +119,7 @@ namespace Nz
IpAddress m_receivedAddress;
SocketPoller m_poller;
UdpSocket m_socket;
UInt16 m_headerFlags;
UInt32 m_bandwidthThrottleEpoch;
UInt32 m_connectedPeers;
UInt32 m_mtu;
@@ -113,6 +132,7 @@ namespace Nz
UInt32 m_totalReceivedData;
UInt32 m_totalReceivedPackets;
UInt8* m_receivedData;
bool m_continueSending;
bool m_isSimulationEnabled;
bool m_shouldAcceptConnections;
bool m_recalculateBandwidthLimits;

View File

@@ -1,4 +1,4 @@
// Copyright (C) 2017 Jérôme Leclercq
// 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
@@ -50,16 +50,6 @@ namespace Nz
m_peers.clear();
m_socket.Close();
}
inline void ENetHost::AddToDispatchQueue(ENetPeer* peer)
{
m_dispatchQueue.UnboundedSet(peer->m_incomingPeerID);
}
inline void ENetHost::RemoveFromDispatchQueue(ENetPeer* peer)
{
m_dispatchQueue.UnboundedReset(peer->m_incomingPeerID);
}
}
#include <Nazara/Network/DebugOff.hpp>

View File

@@ -1,4 +1,4 @@
// Copyright (C) 2017 Jérôme Leclercq
// 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
@@ -40,7 +40,7 @@ namespace Nz
std::size_t referenceCount = 0;
};
struct ENetPacketRef
struct NAZARA_NETWORK_API ENetPacketRef
{
ENetPacketRef() = default;
@@ -49,6 +49,18 @@ namespace Nz
Reset(packet);
}
ENetPacketRef(const ENetPacketRef& packet) :
ENetPacketRef()
{
Reset(packet);
}
ENetPacketRef(ENetPacketRef&& packet) :
m_packet(packet.m_packet)
{
packet.m_packet = nullptr;
}
~ENetPacketRef()
{
Reset();
@@ -69,6 +81,23 @@ namespace Nz
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;

View File

@@ -1,4 +1,4 @@
// Copyright (C) 2017 Jérôme Leclercq
// 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
@@ -32,6 +32,7 @@ namespace Nz
friend struct PacketRef;
public:
ENetPeer(ENetHost* host, UInt16 peerId);
ENetPeer(const ENetPeer&) = delete;
ENetPeer(ENetPeer&&) = default;
~ENetPeer() = default;
@@ -40,6 +41,8 @@ namespace Nz
void DisconnectLater(UInt32 data);
void DisconnectNow(UInt32 data);
inline const IpAddress& GetAddress() const;
void Ping();
bool Receive(ENetPacketRef* packet, UInt8* channelId);
@@ -54,8 +57,6 @@ namespace Nz
ENetPeer& operator=(ENetPeer&&) = default;
private:
ENetPeer(ENetHost* host, UInt16 peerId);
void InitIncoming(std::size_t channelCount, const IpAddress& address, ENetProtocolConnect& incomingCommand);
void InitOutgoing(std::size_t channelCount, const IpAddress& address, UInt32 connectId, UInt32 windowSize);
@@ -66,7 +67,7 @@ namespace Nz
// Protocol functions
inline void ChangeState(ENetPeerState state);
inline void DispatchState(ENetPeerState state);
void DispatchState(ENetPeerState state);
void DispatchIncomingReliableCommands(Channel& channel);
void DispatchIncomingUnreliableCommands(Channel& channel);
@@ -80,7 +81,7 @@ namespace Nz
void ResetQueues();
bool QueueAcknowledgement(ENetProtocol* command, UInt16 sentTime);
IncomingCommmand* QueueIncomingCommand(ENetProtocol& command, const void* data, std::size_t dataLength, UInt32 flags, UInt32 fragmentCount);
IncomingCommmand* QueueIncomingCommand(const ENetProtocol& command, const void* data, std::size_t dataLength, UInt32 flags, UInt32 fragmentCount);
void QueueOutgoingCommand(ENetProtocol& command, ENetPacketRef packet, UInt32 offset, UInt16 length);
void SetupOutgoingCommand(OutgoingCommand& outgoingCommand);
@@ -139,64 +140,66 @@ namespace Nz
UInt32 sentTime;
};
ENetHost* m_host;
IpAddress m_address; /**< Internet address of the peer */
std::vector<Channel> m_channels;
std::list<Acknowledgement> m_acknowledgements;
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;
MemoryPool m_packetPool;
//ENetListNode m_dispatchList;
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_unsequencedWindow[ENetPeer_ReliableWindowSize / 32];
UInt32 m_windowSize;
std::size_t m_totalWaitingData;
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::list<Acknowledgement> m_acknowledgements;
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::vector<Channel> m_channels;
MemoryPool m_packetPool;
//ENetListNode m_dispatchList;
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;
};
}

View File

@@ -1,4 +1,4 @@
// Copyright (C) 2017 Jérôme Leclercq
// 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
@@ -8,6 +8,11 @@
namespace Nz
{
inline const IpAddress& ENetPeer::GetAddress() const
{
return m_address;
}
inline void ENetPeer::ChangeState(ENetPeerState state)
{
if (state == ENetPeerState::Connected || state == ENetPeerState::DisconnectLater)
@@ -17,13 +22,6 @@ namespace Nz
m_state = state;
}
inline void ENetPeer::DispatchState(ENetPeerState state)
{
ChangeState(state);
m_host->AddToDispatchQueue(this);
}
}
#include <Nazara/Network/DebugOff.hpp>

View File

@@ -1,4 +1,4 @@
// Copyright (C) 2017 Jérôme Leclercq
// 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
@@ -13,6 +13,8 @@
namespace Nz
{
class ENetPeer;
// Constants for the ENet implementation and protocol
enum ENetConstants
{
@@ -140,27 +142,38 @@ namespace Nz
ENetPacketRef packet;
};
struct ENetProtocolHeader
#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 ENetProtocolCommandHeader
struct NAZARA_PACKED ENetProtocolCommandHeader
{
UInt8 command;
UInt8 channelID;
UInt16 reliableSequenceNumber;
};
struct ENetProtocolAcknowledge
struct NAZARA_PACKED ENetProtocolAcknowledge
{
ENetProtocolCommandHeader header;
UInt16 receivedReliableSequenceNumber;
UInt16 receivedSentTime;
};
struct ENetProtocolConnect
struct NAZARA_PACKED ENetProtocolConnect
{
ENetProtocolCommandHeader header;
UInt16 outgoingPeerID;
@@ -178,25 +191,25 @@ namespace Nz
UInt32 data;
};
struct ENetProtocolBandwidthLimit
struct NAZARA_PACKED ENetProtocolBandwidthLimit
{
ENetProtocolCommandHeader header;
UInt32 incomingBandwidth;
UInt32 outgoingBandwidth;
};
struct ENetProtocolDisconnect
struct NAZARA_PACKED ENetProtocolDisconnect
{
ENetProtocolCommandHeader header;
UInt32 data;
};
struct ENetProtocolPing
struct NAZARA_PACKED ENetProtocolPing
{
ENetProtocolCommandHeader header;
};
struct ENetProtocolSendFragment
struct NAZARA_PACKED ENetProtocolSendFragment
{
ENetProtocolCommandHeader header;
UInt16 startSequenceNumber;
@@ -207,27 +220,27 @@ namespace Nz
UInt32 fragmentOffset;
};
struct ENetProtocolSendReliable
struct NAZARA_PACKED ENetProtocolSendReliable
{
ENetProtocolCommandHeader header;
UInt16 dataLength;
};
struct ENetProtocolSendUnreliable
struct NAZARA_PACKED ENetProtocolSendUnreliable
{
ENetProtocolCommandHeader header;
UInt16 unreliableSequenceNumber;
UInt16 dataLength;
};
struct ENetProtocolSendUnsequenced
struct NAZARA_PACKED ENetProtocolSendUnsequenced
{
ENetProtocolCommandHeader header;
UInt16 unsequencedGroup;
UInt16 dataLength;
};
struct ENetProtocolThrottleConfigure
struct NAZARA_PACKED ENetProtocolThrottleConfigure
{
ENetProtocolCommandHeader header;
UInt32 packetThrottleInterval;
@@ -235,7 +248,7 @@ namespace Nz
UInt32 packetThrottleDeceleration;
};
struct ENetProtocolVerifyConnect
struct NAZARA_PACKED ENetProtocolVerifyConnect
{
ENetProtocolCommandHeader header;
UInt16 outgoingPeerID;
@@ -252,7 +265,7 @@ namespace Nz
UInt32 connectID;
};
union ENetProtocol
union NAZARA_PACKED ENetProtocol
{
ENetProtocolCommandHeader header;
ENetProtocolAcknowledge acknowledge;
@@ -267,6 +280,10 @@ namespace Nz
ENetProtocolThrottleConfigure throttleConfigure;
ENetProtocolVerifyConnect verifyConnect;
};
#ifdef _MSC_VER
#pragma pack(pop)
#endif
}
#endif // NAZARA_ENETPROTOCOL_HPP

View File

@@ -7,6 +7,8 @@
#ifndef NAZARA_ENUMS_NETWORK_HPP
#define NAZARA_ENUMS_NETWORK_HPP
#include <Nazara/Prerequesites.hpp>
namespace Nz
{
enum NetCode : UInt16