You've already forked lidgren-network-gen3
mirror of
https://github.com/lidgren/lidgren-network-gen3.git
synced 2026-05-16 23:26:32 +09:00
Handshakes are now resent if lost. Documentation updated.
This commit is contained in:
Binary file not shown.
@@ -11,12 +11,15 @@ namespace Lidgren.Network
|
|||||||
internal bool m_connectionInitiator;
|
internal bool m_connectionInitiator;
|
||||||
internal string m_disconnectMessage;
|
internal string m_disconnectMessage;
|
||||||
internal NetIncomingMessage m_remoteHailMessage;
|
internal NetIncomingMessage m_remoteHailMessage;
|
||||||
|
internal float m_lastHandshakeSendTime;
|
||||||
|
internal int m_handshakeAttempts;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The message that the remote part specified via Connect() or Approve() - can be null.
|
/// The message that the remote part specified via Connect() or Approve() - can be null.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public NetIncomingMessage RemoteHailMessage { get { return m_remoteHailMessage; } }
|
public NetIncomingMessage RemoteHailMessage { get { return m_remoteHailMessage; } }
|
||||||
|
|
||||||
|
// heartbeat called when connection still is in m_handshakes of NetPeer
|
||||||
internal void UnconnectedHeartbeat(float now)
|
internal void UnconnectedHeartbeat(float now)
|
||||||
{
|
{
|
||||||
m_peer.VerifyNetworkThread();
|
m_peer.VerifyNetworkThread();
|
||||||
@@ -33,25 +36,50 @@ namespace Lidgren.Network
|
|||||||
// reconnect
|
// reconnect
|
||||||
ExecuteDisconnect("Reconnecting", true);
|
ExecuteDisconnect("Reconnecting", true);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case NetConnectionStatus.InitiatedConnect:
|
case NetConnectionStatus.InitiatedConnect:
|
||||||
// send another connect attempt
|
// send another connect attempt
|
||||||
SendConnect();
|
SendConnect(now);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case NetConnectionStatus.Disconnected:
|
case NetConnectionStatus.Disconnected:
|
||||||
throw new NetException("This connection is Disconnected; spent. A new one should have been created");
|
throw new NetException("This connection is Disconnected; spent. A new one should have been created");
|
||||||
|
|
||||||
case NetConnectionStatus.Disconnecting:
|
case NetConnectionStatus.Disconnecting:
|
||||||
// let disconnect finish first
|
// let disconnect finish first
|
||||||
return;
|
break;
|
||||||
|
|
||||||
case NetConnectionStatus.None:
|
case NetConnectionStatus.None:
|
||||||
default:
|
default:
|
||||||
SendConnect();
|
SendConnect(now);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: handle dangling connections
|
if (now - m_lastHandshakeSendTime > m_peerConfiguration.m_resendHandshakeInterval)
|
||||||
|
{
|
||||||
|
if (m_handshakeAttempts > m_peerConfiguration.m_maximumHandshakeAttempts)
|
||||||
|
{
|
||||||
|
// failed to connect
|
||||||
|
ExecuteDisconnect("Failed to establish connection - no response from remote host", true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// resend handshake
|
||||||
|
switch (m_status)
|
||||||
|
{
|
||||||
|
case NetConnectionStatus.InitiatedConnect:
|
||||||
|
SendConnect(now);
|
||||||
|
break;
|
||||||
|
case NetConnectionStatus.RespondedConnect:
|
||||||
|
SendConnectResponse(now, true);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
m_peer.LogWarning("Time to resend handshake, but status is " + m_status);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void ExecuteDisconnect(string reason, bool sendByeMessage)
|
internal void ExecuteDisconnect(string reason, bool sendByeMessage)
|
||||||
@@ -72,33 +100,48 @@ namespace Lidgren.Network
|
|||||||
SendDisconnect(reason, true);
|
SendDisconnect(reason, true);
|
||||||
|
|
||||||
SetStatus(NetConnectionStatus.Disconnected, reason);
|
SetStatus(NetConnectionStatus.Disconnected, reason);
|
||||||
|
|
||||||
|
// in case we're still in handshake
|
||||||
|
m_peer.m_handshakes.Remove(m_remoteEndpoint);
|
||||||
|
|
||||||
m_disconnectRequested = false;
|
m_disconnectRequested = false;
|
||||||
m_connectRequested = false;
|
m_connectRequested = false;
|
||||||
|
m_handshakeAttempts = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SendConnect()
|
internal void SendConnect(float now)
|
||||||
{
|
{
|
||||||
|
m_peer.VerifyNetworkThread();
|
||||||
|
|
||||||
NetOutgoingMessage om = m_peer.CreateMessage(m_peerConfiguration.AppIdentifier.Length + 1 + 4);
|
NetOutgoingMessage om = m_peer.CreateMessage(m_peerConfiguration.AppIdentifier.Length + 1 + 4);
|
||||||
om.m_messageType = NetMessageType.Connect;
|
om.m_messageType = NetMessageType.Connect;
|
||||||
om.Write(m_peerConfiguration.AppIdentifier);
|
om.Write(m_peerConfiguration.AppIdentifier);
|
||||||
om.Write(m_peer.m_uniqueIdentifier);
|
om.Write(m_peer.m_uniqueIdentifier);
|
||||||
om.Write((float)NetTime.Now);
|
om.Write(now);
|
||||||
|
|
||||||
WriteLocalHail(om);
|
WriteLocalHail(om);
|
||||||
|
|
||||||
m_peer.SendLibrary(om, m_remoteEndpoint);
|
m_peer.SendLibrary(om, m_remoteEndpoint);
|
||||||
|
|
||||||
SetStatus(NetConnectionStatus.InitiatedConnect, "Locally requested connect");
|
|
||||||
m_connectRequested = false;
|
m_connectRequested = false;
|
||||||
|
m_lastHandshakeSendTime = now;
|
||||||
|
m_handshakeAttempts++;
|
||||||
|
|
||||||
|
if (m_handshakeAttempts > 1)
|
||||||
|
m_peer.LogDebug("Resending Connect...");
|
||||||
|
SetStatus(NetConnectionStatus.InitiatedConnect, "Locally requested connect");
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SendConnectResponse(bool onLibraryThread)
|
internal void SendConnectResponse(float now, bool onLibraryThread)
|
||||||
{
|
{
|
||||||
|
if (onLibraryThread)
|
||||||
|
m_peer.VerifyNetworkThread();
|
||||||
|
|
||||||
NetOutgoingMessage om = m_peer.CreateMessage(m_peerConfiguration.AppIdentifier.Length + 1 + 4);
|
NetOutgoingMessage om = m_peer.CreateMessage(m_peerConfiguration.AppIdentifier.Length + 1 + 4);
|
||||||
om.m_messageType = NetMessageType.ConnectResponse;
|
om.m_messageType = NetMessageType.ConnectResponse;
|
||||||
om.Write(m_peerConfiguration.AppIdentifier);
|
om.Write(m_peerConfiguration.AppIdentifier);
|
||||||
om.Write(m_peer.m_uniqueIdentifier);
|
om.Write(m_peer.m_uniqueIdentifier);
|
||||||
om.Write((float)NetTime.Now);
|
om.Write(now);
|
||||||
|
|
||||||
WriteLocalHail(om);
|
WriteLocalHail(om);
|
||||||
|
|
||||||
@@ -107,11 +150,20 @@ namespace Lidgren.Network
|
|||||||
else
|
else
|
||||||
m_peer.m_unsentUnconnectedMessages.Enqueue(new NetTuple<System.Net.IPEndPoint, NetOutgoingMessage>(m_remoteEndpoint, om));
|
m_peer.m_unsentUnconnectedMessages.Enqueue(new NetTuple<System.Net.IPEndPoint, NetOutgoingMessage>(m_remoteEndpoint, om));
|
||||||
|
|
||||||
|
m_lastHandshakeSendTime = now;
|
||||||
|
m_handshakeAttempts++;
|
||||||
|
|
||||||
|
if (m_handshakeAttempts > 1)
|
||||||
|
m_peer.LogDebug("Resending ConnectResponse...");
|
||||||
|
|
||||||
SetStatus(NetConnectionStatus.RespondedConnect, "Remotely requested connect");
|
SetStatus(NetConnectionStatus.RespondedConnect, "Remotely requested connect");
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void SendDisconnect(string reason, bool onLibraryThread)
|
internal void SendDisconnect(string reason, bool onLibraryThread)
|
||||||
{
|
{
|
||||||
|
if (onLibraryThread)
|
||||||
|
m_peer.VerifyNetworkThread();
|
||||||
|
|
||||||
NetOutgoingMessage om = m_peer.CreateMessage(reason);
|
NetOutgoingMessage om = m_peer.CreateMessage(reason);
|
||||||
om.m_messageType = NetMessageType.Disconnect;
|
om.m_messageType = NetMessageType.Disconnect;
|
||||||
if (onLibraryThread)
|
if (onLibraryThread)
|
||||||
@@ -141,6 +193,8 @@ namespace Lidgren.Network
|
|||||||
om.Write((float)NetTime.Now);
|
om.Write((float)NetTime.Now);
|
||||||
m_peer.SendLibrary(om, m_remoteEndpoint);
|
m_peer.SendLibrary(om, m_remoteEndpoint);
|
||||||
|
|
||||||
|
m_handshakeAttempts = 0;
|
||||||
|
|
||||||
InitializePing();
|
InitializePing();
|
||||||
if (m_status != NetConnectionStatus.Connected)
|
if (m_status != NetConnectionStatus.Connected)
|
||||||
SetStatus(NetConnectionStatus.Connected, "Connected to " + NetUtility.ToHexString(m_remoteUniqueIdentifier));
|
SetStatus(NetConnectionStatus.Connected, "Connected to " + NetUtility.ToHexString(m_remoteUniqueIdentifier));
|
||||||
@@ -152,7 +206,8 @@ namespace Lidgren.Network
|
|||||||
public void Approve()
|
public void Approve()
|
||||||
{
|
{
|
||||||
m_localHailMessage = null;
|
m_localHailMessage = null;
|
||||||
SendConnectResponse(false);
|
m_handshakeAttempts = 0;
|
||||||
|
SendConnectResponse((float)NetTime.Now, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -162,7 +217,8 @@ namespace Lidgren.Network
|
|||||||
public void Approve(NetOutgoingMessage localHail)
|
public void Approve(NetOutgoingMessage localHail)
|
||||||
{
|
{
|
||||||
m_localHailMessage = localHail;
|
m_localHailMessage = localHail;
|
||||||
SendConnectResponse(false);
|
m_handshakeAttempts = 0;
|
||||||
|
SendConnectResponse((float)NetTime.Now, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -223,14 +279,14 @@ namespace Lidgren.Network
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
SendConnectResponse(true);
|
SendConnectResponse((float)now, true);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (m_status == NetConnectionStatus.RespondedConnect)
|
if (m_status == NetConnectionStatus.RespondedConnect)
|
||||||
{
|
{
|
||||||
// our ConnectResponse must have been lost
|
// our ConnectResponse must have been lost
|
||||||
SendConnectResponse(true);
|
SendConnectResponse((float)now, true);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
m_peer.LogDebug("Unhandled Connect: " + tp + ", status is " + m_status + " length: " + payloadLength);
|
m_peer.LogDebug("Unhandled Connect: " + tp + ", status is " + m_status + " length: " + payloadLength);
|
||||||
|
|||||||
@@ -90,10 +90,14 @@ namespace Lidgren.Network
|
|||||||
|
|
||||||
if (status == m_status)
|
if (status == m_status)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
m_status = status;
|
m_status = status;
|
||||||
if (reason == null)
|
if (reason == null)
|
||||||
reason = string.Empty;
|
reason = string.Empty;
|
||||||
|
|
||||||
|
// new status equals potentially new handshake attempts
|
||||||
|
m_handshakeAttempts = 0;
|
||||||
|
|
||||||
if (m_status == NetConnectionStatus.Connected)
|
if (m_status == NetConnectionStatus.Connected)
|
||||||
{
|
{
|
||||||
m_timeoutDeadline = (float)NetTime.Now + m_peerConfiguration.m_connectionTimeout;
|
m_timeoutDeadline = (float)NetTime.Now + m_peerConfiguration.m_connectionTimeout;
|
||||||
|
|||||||
@@ -229,7 +229,7 @@ namespace Lidgren.Network
|
|||||||
break;
|
break;
|
||||||
case NetConnectionStatus.RespondedConnect:
|
case NetConnectionStatus.RespondedConnect:
|
||||||
// send another response
|
// send another response
|
||||||
hs.SendConnectResponse(false);
|
hs.SendConnectResponse((float)NetTime.Now, false);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// weird
|
// weird
|
||||||
|
|||||||
@@ -44,6 +44,8 @@ namespace Lidgren.Network
|
|||||||
internal int m_port;
|
internal int m_port;
|
||||||
internal int m_receiveBufferSize;
|
internal int m_receiveBufferSize;
|
||||||
internal int m_sendBufferSize;
|
internal int m_sendBufferSize;
|
||||||
|
internal float m_resendHandshakeInterval;
|
||||||
|
internal int m_maximumHandshakeAttempts;
|
||||||
|
|
||||||
// bad network simulation
|
// bad network simulation
|
||||||
internal float m_loss;
|
internal float m_loss;
|
||||||
@@ -79,6 +81,8 @@ namespace Lidgren.Network
|
|||||||
m_pingInterval = 4.0f;
|
m_pingInterval = 4.0f;
|
||||||
m_connectionTimeout = 25.0f;
|
m_connectionTimeout = 25.0f;
|
||||||
m_useMessageRecycling = true;
|
m_useMessageRecycling = true;
|
||||||
|
m_resendHandshakeInterval = 3.0f;
|
||||||
|
m_maximumHandshakeAttempts = 5;
|
||||||
|
|
||||||
// Maximum transmission unit
|
// Maximum transmission unit
|
||||||
// Ethernet can take 1500 bytes of payload, so lets stay below that.
|
// Ethernet can take 1500 bytes of payload, so lets stay below that.
|
||||||
@@ -302,6 +306,24 @@ namespace Lidgren.Network
|
|||||||
set { m_acceptIncomingConnections = value; }
|
set { m_acceptIncomingConnections = value; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the number of seconds between handshake attempts
|
||||||
|
/// </summary>
|
||||||
|
public float ResendHandshakeInterval
|
||||||
|
{
|
||||||
|
get { return m_resendHandshakeInterval; }
|
||||||
|
set { m_resendHandshakeInterval = value; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the maximum number of handshake attempts before failing to connect
|
||||||
|
/// </summary>
|
||||||
|
public int MaximumHandshakeAttempts
|
||||||
|
{
|
||||||
|
get { return m_maximumHandshakeAttempts; }
|
||||||
|
set { m_maximumHandshakeAttempts = value; }
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets if the NetPeer should send large messages to try to expand the maximum transmission unit size
|
/// Gets or sets if the NetPeer should send large messages to try to expand the maximum transmission unit size
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
Reference in New Issue
Block a user