Network: Add RUdpConnection class (experimental)

Currently missing an external RUdpClient class, ping handling, some
messages but should basically works


Former-commit-id: 6ebd181a4804094c62aedb8e3ba7876a7b06acdc
This commit is contained in:
Lynix
2016-03-09 13:54:04 +01:00
parent 8e3c542049
commit 3f9a4170f1
6 changed files with 824 additions and 0 deletions

View File

@@ -11,6 +11,12 @@ namespace Nz
{
enum NetCode : UInt16
{
NetCode_Acknowledge = 0x9A4E,
NetCode_AcknowledgeConnection = 0xC108,
NetCode_Ping = 0x96AC,
NetCode_Pong = 0x974C,
NetCode_RequestConnection = 0xF27D,
NetCode_Invalid = 0x0000
};
@@ -24,6 +30,27 @@ namespace Nz
NetProtocol_Max = NetProtocol_Unknown
};
enum PacketPriority
{
PacketPriority_High = 1, //< High-priority packet, will be sent quickly
PacketPriority_Immediate = 0, //< Immediate priority, will be sent immediately
PacketPriority_Medium = 2, //< Medium-priority packet, will be sent as regular
PacketPriority_Low = 3, //< Low-priority packet, may take some time to be sent
PacketPriority_Lowest = PacketPriority_Low,
PacketPriority_Highest = PacketPriority_Immediate,
PacketPriority_Max = PacketPriority_Low
};
enum PacketReliability
{
PacketReliability_Reliable, //< Packet will be resent if lost
PacketReliability_ReliableOrdered, //< Packet will be resent if lost and will only arrive in order
PacketReliability_Unreliable, //< Packet won't be resent if lost
PacketReliability_Max = PacketReliability_Unreliable
};
enum ResolveError
{
ResolveError_NoError,

View File

@@ -0,0 +1,158 @@
// Copyright (C) 2015 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_RUDPSERVER_HPP
#define NAZARA_RUDPSERVER_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Core/Bitset.hpp>
#include <Nazara/Core/Clock.hpp>
#include <Nazara/Network/IpAddress.hpp>
#include <Nazara/Network/NetPacket.hpp>
#include <Nazara/Network/RUdpMessage.hpp>
#include <Nazara/Network/UdpSocket.hpp>
#include <deque>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
namespace Nz
{
class RUdpClient;
class NAZARA_NETWORK_API RUdpConnection
{
friend class Network;
public:
using SequenceIndex = UInt16;
RUdpConnection();
RUdpConnection(const RUdpConnection&) = delete;
RUdpConnection(RUdpConnection&&) = default;
~RUdpConnection() = default;
inline void Close();
bool Connect(const IpAddress& remoteAddress);
bool Connect(const String& hostName, NetProtocol protocol = NetProtocol_Any, const String& service = "http", ResolveError* error = nullptr);
inline void Disconnect();
inline IpAddress GetBoundAddress() const;
inline UInt16 GetBoundPort() const;
inline SocketError GetLastError() const;
inline bool Listen(NetProtocol protocol, UInt16 port = 64266, unsigned int queueSize = 10);
bool Listen(const IpAddress& address, unsigned int queueSize = 10);
bool PollMessage(RUdpMessage* message);
bool Send(const IpAddress& clientIp, PacketPriority priority, PacketReliability reliability, const NetPacket& packet);
inline void SetProtocolId(UInt32 protocolId);
inline void SetTimeBeforeAck(UInt32 ms);
void Update();
RUdpConnection& operator=(const RUdpConnection&) = delete;
RUdpConnection& operator=(RUdpConnection&&) = default;
static constexpr std::size_t MessageHeader = sizeof(UInt16) + 2 * sizeof(SequenceIndex) + sizeof(UInt32); //< Protocol ID (begin) + Sequence ID + Remote Sequence ID + Ack bitfield
static constexpr std::size_t MessageFooter = sizeof(UInt16); //< Protocol ID (end)
// Signals:
NazaraSignal(OnConnectedToPeer, RUdpConnection* /*connection*/);
NazaraSignal(OnPeerAcknowledged, RUdpConnection* /*connection*/, const IpAddress& /*adress*/);
NazaraSignal(OnPeerConnection, RUdpConnection* /*connection*/, const IpAddress& /*adress*/);
NazaraSignal(OnPeerDisconnected, RUdpConnection* /*connection*/, const IpAddress& /*adress*/);
private:
struct PeerData;
struct PendingAckPacket;
struct PendingPacket;
enum PeerState
{
PeerState_Aknowledged, //< A connection request from this peer has been received, we're waiting for another packet to validate
PeerState_Connected, //< Connection is working in both-ways
PeerState_Connecting, //< A connection request has been made
PeerState_WillAck //< Connected, received one or more packets and has no packets to send, waiting before sending an empty ack packet
};
void DisconnectPeer(std::size_t peerIndex);
void EnqueuePacket(PeerData& peer, PacketPriority priority, PacketReliability reliability, const NetPacket& packet);
void EnqueuePacketInternal(PeerData& peer, PacketPriority priority, PacketReliability reliability, NetPacket&& data);
bool InitSocket(NetProtocol protocol);
void ProcessAcks(PeerData& peer, SequenceIndex lastAck, UInt32 ackBits);
PeerData& RegisterPeer(const IpAddress& address, PeerState state);
void OnClientRequestingConnection(const IpAddress& address, SequenceIndex sequenceId, UInt64 token);
void OnPacketLost(PeerData& peer, PendingAckPacket&& packet);
void OnPacketReceived(const IpAddress& peerIp, NetPacket&& packet);
void SendPacket(PeerData& peer, PendingPacket&& packet);
static inline unsigned int ComputeSequenceDifference(SequenceIndex sequence, SequenceIndex sequence2);
static inline bool HasPendingPackets(PeerData& peer);
static bool Initialize();
static inline bool IsAckMoreRecent(SequenceIndex ack, SequenceIndex ack2);
static inline bool IsReliable(PacketReliability reliability);
static void Uninitialize();
struct PendingPacket
{
PacketPriority priority;
PacketReliability reliability;
NetPacket data;
};
struct PendingAckPacket
{
PacketPriority priority;
PacketReliability reliability;
NetPacket data;
SequenceIndex sequenceId;
UInt64 timeSent;
};
struct PeerData //TODO: Move this to RUdpClient
{
std::array<std::vector<PendingPacket>, PacketPriority_Max + 1> pendingPackets;
std::deque<PendingAckPacket> pendingAckQueue;
std::set<UInt16> receivedQueue;
std::size_t index;
PeerState state;
IpAddress address;
SequenceIndex localSequence;
SequenceIndex remoteSequence;
UInt32 roundTripTime;
UInt64 lastPacketTime;
UInt64 lastPingTime;
UInt64 stateData1;
};
std::unordered_map<IpAddress, std::size_t> m_peerByIP;
std::queue<RUdpMessage> m_receivedMessages;
std::size_t m_peerIterator;
std::vector<PeerData> m_peers;
Bitset<UInt64> m_activeClients;
Clock m_clock;
SocketError m_lastError;
UdpSocket m_socket;
UInt32 m_forceAckSendTime;
UInt32 m_pingInterval;
UInt32 m_protocol;
UInt32 m_timeBeforePing;
UInt32 m_timeBeforeTimeOut;
UInt64 m_currentTime;
bool m_shouldAcceptConnections;
static std::mt19937_64 s_randomGenerator;
};
}
#include <Nazara/Network/RudpConnection.inl>
#endif // NAZARA_RUDPSERVER_HPP

View File

@@ -0,0 +1,126 @@
// Copyright (C) 2015 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/RUdpConnection.hpp>
#include <utility>
#include <Nazara/Network/Debug.hpp>
namespace Nz
{
inline void RUdpConnection::Close()
{
m_socket.Close();
}
inline void RUdpConnection::Disconnect()
{
Close();
}
inline IpAddress RUdpConnection::GetBoundAddress() const
{
return m_socket.GetBoundAddress();
}
inline UInt16 RUdpConnection::GetBoundPort() const
{
return m_socket.GetBoundPort();
}
inline SocketError RUdpConnection::GetLastError() const
{
return m_lastError;
}
inline bool RUdpConnection::Listen(NetProtocol protocol, UInt16 port, unsigned int queueSize)
{
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 Listen(any, queueSize);
}
inline void RUdpConnection::SetProtocolId(UInt32 protocolId)
{
m_protocol = protocolId;
}
inline void RUdpConnection::SetTimeBeforeAck(UInt32 ms)
{
m_forceAckSendTime = ms * 1000; //< Store in microseconds for easier handling
}
inline unsigned int RUdpConnection::ComputeSequenceDifference(SequenceIndex sequence, SequenceIndex sequence2)
{
unsigned int difference;
if (sequence2 > sequence)
difference = std::numeric_limits<SequenceIndex>::max() - sequence2 + sequence;
else
difference = sequence - sequence2;
return 0;
}
inline bool RUdpConnection::HasPendingPackets(PeerData& peer)
{
for (unsigned int priority = PacketPriority_Highest; priority <= PacketPriority_Lowest; ++priority)
{
std::vector<PendingPacket>& pendingPackets = peer.pendingPackets[priority];
if (!pendingPackets.empty())
return true;
pendingPackets.clear();
}
return false;
}
inline bool RUdpConnection::IsAckMoreRecent(SequenceIndex ack, SequenceIndex ack2)
{
constexpr SequenceIndex maxDifference = std::numeric_limits<SequenceIndex>::max() / 2;
if (ack > ack2)
return ack - ack2 <= maxDifference;
else if (ack2 > ack)
return ack2 - ack > maxDifference;
else
return false; ///< Same ack
}
inline bool RUdpConnection::IsReliable(PacketReliability reliability)
{
switch (reliability)
{
case PacketReliability_Reliable:
case PacketReliability_ReliableOrdered:
return true;
case PacketReliability_Unreliable:
return false;
}
NazaraError("PacketReliability not handled (0x" + String::Number(reliability, 16) + ')');
return false;
}
}
#include <Nazara/Network/DebugOff.hpp>

View File

@@ -0,0 +1,23 @@
// Copyright (C) 2015 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_RUDMESSAGE_HPP
#define NAZARA_RUDMESSAGE_HPP
#include <Nazara/Prerequesites.hpp>
#include <Nazara/Network/IpAddress.hpp>
#include <Nazara/Network/NetPacket.hpp>
namespace Nz
{
struct RUdpMessage
{
IpAddress from;
NetPacket data;
};
}
#endif // NAZARA_RUDMESSAGE_HPP