7 #ifndef NAZARA_RUDPSERVER_HPP 8 #define NAZARA_RUDPSERVER_HPP 10 #include <Nazara/Prerequesites.hpp> 11 #include <Nazara/Core/Bitset.hpp> 12 #include <Nazara/Core/Clock.hpp> 13 #include <Nazara/Network/IpAddress.hpp> 14 #include <Nazara/Network/NetPacket.hpp> 15 #include <Nazara/Network/RUdpMessage.hpp> 16 #include <Nazara/Network/UdpSocket.hpp> 21 #include <unordered_map> 30 using SequenceIndex = UInt16;
39 bool Connect(
const IpAddress& remoteAddress);
40 bool Connect(
const String& hostName, NetProtocol protocol = NetProtocol_Any,
const String& service =
"http", ResolveError* error =
nullptr);
44 inline UInt16 GetBoundPort()
const;
45 inline SocketError GetLastError()
const;
47 inline bool Listen(NetProtocol protocol, UInt16 port = 64266);
50 bool PollMessage(RUdpMessage* message);
52 bool Send(
const IpAddress& clientIp, PacketPriority priority, PacketReliability reliability,
const NetPacket& packet);
54 inline void SetProtocolId(UInt32 protocolId);
55 inline void SetTimeBeforeAck(UInt32 ms);
57 inline void SimulateNetwork(
double packetLoss);
64 static constexpr std::size_t MessageHeader =
sizeof(UInt16) + 2 *
sizeof(SequenceIndex) +
sizeof(UInt32);
65 static constexpr std::size_t MessageFooter =
sizeof(UInt16);
75 struct PendingAckPacket;
80 PeerState_Aknowledged,
86 void DisconnectPeer(std::size_t peerIndex);
87 void EnqueuePacket(PeerData& peer, PacketPriority priority, PacketReliability reliability,
const NetPacket& packet);
88 void EnqueuePacketInternal(PeerData& peer, PacketPriority priority, PacketReliability reliability,
NetPacket&& data);
89 bool InitSocket(NetProtocol protocol);
90 void ProcessAcks(PeerData& peer, SequenceIndex lastAck, UInt32 ackBits);
91 PeerData& RegisterPeer(
const IpAddress& address, PeerState state);
92 void OnClientRequestingConnection(
const IpAddress& address, SequenceIndex sequenceId, UInt64 token);
93 void OnPacketLost(PeerData& peer, PendingAckPacket&& packet);
95 void SendPacket(PeerData& peer, PendingPacket&& packet);
97 static inline unsigned int ComputeSequenceDifference(SequenceIndex sequence, SequenceIndex sequence2);
98 static inline bool HasPendingPackets(PeerData& peer);
99 static bool Initialize();
100 static inline bool IsAckMoreRecent(SequenceIndex ack, SequenceIndex ack2);
101 static inline bool IsReliable(PacketReliability reliability);
102 static void Uninitialize();
106 PacketPriority priority;
107 PacketReliability reliability;
111 struct PendingAckPacket
113 PacketPriority priority;
114 PacketReliability reliability;
116 SequenceIndex sequenceId;
122 PeerData() =
default;
123 PeerData(PeerData&& other) =
default;
124 PeerData& operator=(PeerData&& other) =
default;
126 std::array<std::vector<PendingPacket>, PacketPriority_Max + 1> pendingPackets;
127 std::deque<PendingAckPacket> pendingAckQueue;
128 std::set<UInt16> receivedQueue;
132 SequenceIndex localSequence;
133 SequenceIndex remoteSequence;
134 UInt32 roundTripTime;
135 UInt64 lastPacketTime;
140 std::bernoulli_distribution m_packetLossProbability;
141 std::queue<RUdpMessage> m_receivedMessages;
142 std::size_t m_peerIterator;
143 std::unordered_map<IpAddress, std::size_t> m_peerByIP;
144 std::vector<PeerData> m_peers;
147 SocketError m_lastError;
149 UInt32 m_forceAckSendTime;
150 UInt32 m_pingInterval;
152 UInt32 m_timeBeforePing;
153 UInt32 m_timeBeforeTimeOut;
154 UInt64 m_currentTime;
155 bool m_isSimulationEnabled;
156 bool m_shouldAcceptConnections;
158 static std::mt19937_64 s_randomGenerator;
162 #include <Nazara/Network/RUdpConnection.inl> 164 #endif // NAZARA_RUDPSERVER_HPP Utility class that measure the elapsed time.
Definition: Clock.hpp:20
TODO: Inherit SoundEmitter from Node.
Definition: Algorithm.hpp:12
Network class that represents an IP address.
Definition: IpAddress.hpp:21
Core class that represents a string.
Definition: String.hpp:22
Network class that represents a packet.
Definition: NetPacket.hpp:18
Network class that represents the module initializer of Network.
Definition: Network.hpp:15
Network class that represents a UDP socket, allowing for sending/receiving datagrams.
Definition: UdpSocket.hpp:19
Network class that represents a reliable UDP connection.
Definition: RUdpConnection.hpp:25