Network/ENet: Add DisconnectTimeout event

This commit is contained in:
SirLynix 2022-09-09 12:56:50 +02:00
parent 3f38c52455
commit e063c7b45e
5 changed files with 19 additions and 11 deletions

View File

@ -95,7 +95,7 @@ namespace Nz
int ReceiveIncomingCommands(ENetEvent* event); int ReceiveIncomingCommands(ENetEvent* event);
void NotifyConnect(ENetPeer* peer, ENetEvent* event, bool incoming); void NotifyConnect(ENetPeer* peer, ENetEvent* event, bool incoming);
void NotifyDisconnect(ENetPeer*, ENetEvent* event); void NotifyDisconnect(ENetPeer*, ENetEvent* event, bool timeout);
void SendAcknowledgements(ENetPeer* peer); void SendAcknowledgements(ENetPeer* peer);
bool SendReliableOutgoingCommands(ENetPeer* peer); bool SendReliableOutgoingCommands(ENetPeer* peer);

View File

@ -246,6 +246,7 @@ namespace Nz
UInt64 m_totalByteReceived; UInt64 m_totalByteReceived;
UInt64 m_totalByteSent; UInt64 m_totalByteSent;
bool m_isSimulationEnabled; bool m_isSimulationEnabled;
bool m_timedOut;
}; };
} }

View File

@ -119,14 +119,19 @@ namespace Nz
None, None,
/** a peer has disconnected. This event is generated on a successful /** a peer has disconnected. This event is generated on a successful
* completion of a disconnect initiated by enet_peer_disconnect, if * completion of a disconnect initiated by enet_peer_disconnect.
* a peer has timed out, or if a connection request initialized by * The peer field contains the peer which disconnected.
* enet_host_connect has timed out. The peer field contains the peer * The data field contains user supplied data
* which disconnected. The data field contains user supplied data
* describing the disconnection, or 0, if none is available. * describing the disconnection, or 0, if none is available.
*/ */
Disconnect, Disconnect,
/** a peer has timed out. This event is generated if a connected peer timed out
* or if a enet_host_connected has timed out. The peer field contains the peer
* which disconnected.
*/
DisconnectTimeout,
/** a connection request initiated by enet_host_connect from this host has completed. /** a connection request initiated by enet_host_connect from this host has completed.
* The peer field contains the peer which successfully connected. * The peer field contains the peer which successfully connected.
*/ */

View File

@ -386,7 +386,7 @@ namespace Nz
case ENetPeerState::Zombie: case ENetPeerState::Zombie:
m_recalculateBandwidthLimits = true; m_recalculateBandwidthLimits = true;
event->type = ENetEventType::Disconnect; event->type = (peer.m_timedOut) ? ENetEventType::DisconnectTimeout : ENetEventType::Disconnect;
event->peer = &peer; event->peer = &peer;
event->data = peer.m_eventData; event->data = peer.m_eventData;
@ -787,7 +787,7 @@ namespace Nz
peer->DispatchState((peer->GetState() == ENetPeerState::Connecting) ? ENetPeerState::ConnectionSucceeded : ENetPeerState::ConnectionPending); peer->DispatchState((peer->GetState() == ENetPeerState::Connecting) ? ENetPeerState::ConnectionSucceeded : ENetPeerState::ConnectionPending);
} }
void ENetHost::NotifyDisconnect(ENetPeer* peer, ENetEvent* event) void ENetHost::NotifyDisconnect(ENetPeer* peer, ENetEvent* event, bool timeout)
{ {
if (peer->GetState() >= ENetPeerState::ConnectionPending) if (peer->GetState() >= ENetPeerState::ConnectionPending)
m_recalculateBandwidthLimits = true; m_recalculateBandwidthLimits = true;
@ -796,7 +796,7 @@ namespace Nz
peer->Reset(); peer->Reset();
else if (event) else if (event)
{ {
event->type = ENetEventType::Disconnect; event->type = (timeout) ? ENetEventType::DisconnectTimeout : ENetEventType::Disconnect;
event->peer = peer; event->peer = peer;
event->data = peer->m_eventData; event->data = peer->m_eventData;
@ -805,6 +805,7 @@ namespace Nz
else else
{ {
peer->m_eventData = 0; peer->m_eventData = 0;
peer->m_timedOut = timeout;
peer->DispatchState(ENetPeerState::Zombie); peer->DispatchState(ENetPeerState::Zombie);
} }
@ -1123,7 +1124,7 @@ namespace Nz
if (!currentPeer->IsConnected()) if (!currentPeer->IsConnected())
{ {
//< Network is down or unreachable (ex: IPv6 address when not supported), fails peer connection immediately //< Network is down or unreachable (ex: IPv6 address when not supported), fails peer connection immediately
NotifyDisconnect(currentPeer, event); NotifyDisconnect(currentPeer, event, true);
return 1; return 1;
} }

View File

@ -163,6 +163,7 @@ namespace Nz
m_incomingUnsequencedGroup = 0; m_incomingUnsequencedGroup = 0;
m_outgoingUnsequencedGroup = 0; m_outgoingUnsequencedGroup = 0;
m_eventData = 0; m_eventData = 0;
m_timedOut = false;
m_totalByteReceived = 0; m_totalByteReceived = 0;
m_totalByteSent = 0; m_totalByteSent = 0;
m_totalPacketReceived = 0; m_totalPacketReceived = 0;
@ -308,7 +309,7 @@ namespace Nz
if (m_earliestTimeout != 0 && (ENetTimeDifference(serviceTime, m_earliestTimeout) >= m_timeoutMaximum || if (m_earliestTimeout != 0 && (ENetTimeDifference(serviceTime, m_earliestTimeout) >= m_timeoutMaximum ||
(command.roundTripTimeout >= command.roundTripTimeoutLimit && ENetTimeDifference(serviceTime, m_earliestTimeout) >= m_timeoutMinimum))) (command.roundTripTimeout >= command.roundTripTimeoutLimit && ENetTimeDifference(serviceTime, m_earliestTimeout) >= m_timeoutMinimum)))
{ {
m_host->NotifyDisconnect(this, event); m_host->NotifyDisconnect(this, event, true);
return true; return true;
} }
@ -510,7 +511,7 @@ namespace Nz
if (commandNumber != ENetProtocolCommand_Disconnect) if (commandNumber != ENetProtocolCommand_Disconnect)
return false; return false;
m_host->NotifyDisconnect(this, event); m_host->NotifyDisconnect(this, event, false);
break; break;
case ENetPeerState::DisconnectLater: case ENetPeerState::DisconnectLater: