Documentation for module: Network
Former-commit-id: d36042138d0883eb118cc9a70f94f3522214dd46
This commit is contained in:
@@ -10,6 +10,16 @@
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
/*!
|
||||
* \ingroup network
|
||||
* \class Nz::RUdpConnection
|
||||
* \brief Network class that represents a reliable UDP connection
|
||||
*/
|
||||
|
||||
/*!
|
||||
* \brief Constructs a RUdpConnection object by default
|
||||
*/
|
||||
|
||||
RUdpConnection::RUdpConnection() :
|
||||
m_peerIterator(0),
|
||||
m_forceAckSendTime(10'000), //< 10ms
|
||||
@@ -23,9 +33,20 @@ namespace Nz
|
||||
{
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Connects to the IpAddress
|
||||
* \return true
|
||||
*
|
||||
* \param remoteAddress Address to connect to
|
||||
*
|
||||
* \remark Produces a NazaraAssert if socket is not bound
|
||||
* \remark Produces a NazaraAssert if remote is invalid
|
||||
* \remark Produces a NazaraAssert if port is not specified
|
||||
*/
|
||||
|
||||
bool RUdpConnection::Connect(const IpAddress& remoteAddress)
|
||||
{
|
||||
NazaraAssert(m_socket.GetState() != SocketState_Bound, "Socket must be bound first");
|
||||
NazaraAssert(m_socket.GetState() == SocketState_Bound, "Socket must be bound first");
|
||||
NazaraAssert(remoteAddress.IsValid(), "Invalid remote address");
|
||||
NazaraAssert(remoteAddress.GetPort() != 0, "Remote address has no port");
|
||||
|
||||
@@ -39,6 +60,16 @@ namespace Nz
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Connects to the hostname
|
||||
* \return true If successful
|
||||
*
|
||||
* \param hostName Hostname of the remote
|
||||
* \param protocol Net protocol to use
|
||||
* \param service Specify the protocol used
|
||||
* \param error Optional argument to get the error
|
||||
*/
|
||||
|
||||
bool RUdpConnection::Connect(const String& hostName, NetProtocol protocol, const String& service, ResolveError* error)
|
||||
{
|
||||
std::vector<HostnameInfo> results = IpAddress::ResolveHostname(protocol, hostName, service, error);
|
||||
@@ -64,6 +95,13 @@ namespace Nz
|
||||
return Connect(hostnameAddress);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Listens to a socket
|
||||
* \return true If successfully bound
|
||||
*
|
||||
* \param remoteAddress Address to listen to
|
||||
*/
|
||||
|
||||
bool RUdpConnection::Listen(const IpAddress& address)
|
||||
{
|
||||
if (!InitSocket(address.GetProtocol()))
|
||||
@@ -72,8 +110,19 @@ namespace Nz
|
||||
return m_socket.Bind(address) == SocketState_Bound;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Polls the message
|
||||
* \return true If there is a message
|
||||
*
|
||||
* \param message Message to poll
|
||||
*
|
||||
* \remark Produces a NazaraAssert if message is invalid
|
||||
*/
|
||||
|
||||
bool RUdpConnection::PollMessage(RUdpMessage* message)
|
||||
{
|
||||
NazaraAssert(message, "Invalid message");
|
||||
|
||||
if (m_receivedMessages.empty())
|
||||
return false;
|
||||
|
||||
@@ -82,6 +131,16 @@ namespace Nz
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sends the packet to a peer
|
||||
* \return true If peer exists (false may result from disconnected client)
|
||||
*
|
||||
* \param peerIp IpAddress of the peer
|
||||
* \param priority Priority of the packet
|
||||
* \param reliability Policy of reliability of the packet
|
||||
* \param packet Packet to send
|
||||
*/
|
||||
|
||||
bool RUdpConnection::Send(const IpAddress& peerIp, PacketPriority priority, PacketReliability reliability, const NetPacket& packet)
|
||||
{
|
||||
auto it = m_peerByIP.find(peerIp);
|
||||
@@ -92,6 +151,10 @@ namespace Nz
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Updates the reliable connection
|
||||
*/
|
||||
|
||||
void RUdpConnection::Update()
|
||||
{
|
||||
m_currentTime = m_clock.GetMicroseconds();
|
||||
@@ -156,6 +219,14 @@ namespace Nz
|
||||
//m_activeClients.Reset();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Disconnects a peer
|
||||
*
|
||||
* \param peerIndex Index of the peer
|
||||
*
|
||||
* \remark Produces a NazaraNotice
|
||||
*/
|
||||
|
||||
void RUdpConnection::DisconnectPeer(std::size_t peerIndex)
|
||||
{
|
||||
PeerData& peer = m_peers[peerIndex];
|
||||
@@ -193,6 +264,15 @@ namespace Nz
|
||||
m_peers.pop_back();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Enqueues a packet in the sending list
|
||||
*
|
||||
* \param peer Data relative to the peer
|
||||
* \param priority Priority of the packet
|
||||
* \param reliability Policy of reliability of the packet
|
||||
* \param packet Packet to send
|
||||
*/
|
||||
|
||||
void RUdpConnection::EnqueuePacket(PeerData& peer, PacketPriority priority, PacketReliability reliability, const NetPacket& packet)
|
||||
{
|
||||
UInt16 protocolBegin = static_cast<UInt16>(m_protocol & 0xFFFF);
|
||||
@@ -208,6 +288,15 @@ namespace Nz
|
||||
EnqueuePacketInternal(peer, priority, reliability, std::move(data));
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Enqueues internally a packet in the sending list
|
||||
*
|
||||
* \param peer Data relative to the peer
|
||||
* \param priority Priority of the packet
|
||||
* \param reliability Policy of reliability of the packet
|
||||
* \param packet Packet to send
|
||||
*/
|
||||
|
||||
void RUdpConnection::EnqueuePacketInternal(PeerData& peer, PacketPriority priority, PacketReliability reliability, NetPacket&& data)
|
||||
{
|
||||
PendingPacket pendingPacket;
|
||||
@@ -219,6 +308,13 @@ namespace Nz
|
||||
m_activeClients.UnboundedSet(peer.index);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Inits the internal socket
|
||||
* \return true If successful
|
||||
*
|
||||
* \param protocol Net protocol to use
|
||||
*/
|
||||
|
||||
bool RUdpConnection::InitSocket(NetProtocol protocol)
|
||||
{
|
||||
CallOnExit updateLastError([this]
|
||||
@@ -233,6 +329,14 @@ namespace Nz
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Processes the acks
|
||||
*
|
||||
* \param peer Data relative to the peer
|
||||
* \param lastAck Last index of the ack
|
||||
* \param ackBits Bits for acking
|
||||
*/
|
||||
|
||||
void RUdpConnection::ProcessAcks(PeerData& peer, SequenceIndex lastAck, UInt32 ackBits)
|
||||
{
|
||||
auto it = peer.pendingAckQueue.begin();
|
||||
@@ -257,6 +361,14 @@ namespace Nz
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Registers a peer
|
||||
* \return Data relative to the peer
|
||||
*
|
||||
* \param address Address of the peer
|
||||
* \param state Status of the peer
|
||||
*/
|
||||
|
||||
RUdpConnection::PeerData& RUdpConnection::RegisterPeer(const IpAddress& address, PeerState state)
|
||||
{
|
||||
PeerData data;
|
||||
@@ -266,7 +378,7 @@ namespace Nz
|
||||
data.index = m_peers.size();
|
||||
data.lastPacketTime = m_currentTime;
|
||||
data.lastPingTime = m_currentTime;
|
||||
data.roundTripTime = 1000000; ///< Okay that's quite a lot
|
||||
data.roundTripTime = 1'000'000; ///< Okay that's quite a lot
|
||||
data.state = state;
|
||||
|
||||
m_activeClients.UnboundedSet(data.index);
|
||||
@@ -276,6 +388,14 @@ namespace Nz
|
||||
return m_peers.back();
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to do when client requests a connection
|
||||
*
|
||||
* \param address Address of the peer
|
||||
* \param sequenceId Sequence index for the ack
|
||||
* \param token Token for connection
|
||||
*/
|
||||
|
||||
void RUdpConnection::OnClientRequestingConnection(const IpAddress& address, SequenceIndex sequenceId, UInt64 token)
|
||||
{
|
||||
// Call hook to check if client should be accepted or not
|
||||
@@ -292,6 +412,13 @@ namespace Nz
|
||||
EnqueuePacket(client, PacketPriority_Immediate, PacketReliability_Reliable, connectionAcceptedPacket);
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to do when a packet is lost
|
||||
*
|
||||
* \param peer Data relative to the peer
|
||||
* \param packet Pending packet
|
||||
*/
|
||||
|
||||
void RUdpConnection::OnPacketLost(PeerData& peer, PendingAckPacket&& packet)
|
||||
{
|
||||
//NazaraNotice(m_socket.GetBoundAddress().ToString() + ": Lost packet " + String::Number(packet.sequenceId));
|
||||
@@ -300,6 +427,14 @@ namespace Nz
|
||||
EnqueuePacketInternal(peer, packet.priority, packet.reliability, std::move(packet.data));
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Operation to do when receiving a packet
|
||||
*
|
||||
* \param peerIndex Index of the peer
|
||||
*
|
||||
* \remark Produces a NazaraNotice
|
||||
*/
|
||||
|
||||
void RUdpConnection::OnPacketReceived(const IpAddress& peerIp, NetPacket&& packet)
|
||||
{
|
||||
UInt16 protocolBegin;
|
||||
@@ -436,6 +571,13 @@ namespace Nz
|
||||
}
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Sends a packet to a peer
|
||||
*
|
||||
* \param peer Data relative to the peer
|
||||
* \param packet Pending packet
|
||||
*/
|
||||
|
||||
void RUdpConnection::SendPacket(PeerData& peer, PendingPacket&& packet)
|
||||
{
|
||||
if (peer.state == PeerState_WillAck)
|
||||
@@ -448,7 +590,7 @@ namespace Nz
|
||||
{
|
||||
if (ack == remoteSequence)
|
||||
continue;
|
||||
|
||||
|
||||
unsigned int difference = ComputeSequenceDifference(remoteSequence, ack);
|
||||
if (difference <= 32U)
|
||||
previousAcks |= (1U << (difference - 1));
|
||||
@@ -473,6 +615,11 @@ namespace Nz
|
||||
peer.pendingAckQueue.emplace_back(std::move(pendingAckPacket));
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Initializes the RUdpConnection class
|
||||
* \return true
|
||||
*/
|
||||
|
||||
bool RUdpConnection::Initialize()
|
||||
{
|
||||
std::random_device device;
|
||||
@@ -481,6 +628,10 @@ namespace Nz
|
||||
return true;
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Uninitializes the RUdpConnection class
|
||||
*/
|
||||
|
||||
void RUdpConnection::Uninitialize()
|
||||
{
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user