diff --git a/Lidgren.Network/NetConnection.cs b/Lidgren.Network/NetConnection.cs index 268917a..a5c3c10 100644 --- a/Lidgren.Network/NetConnection.cs +++ b/Lidgren.Network/NetConnection.cs @@ -89,6 +89,15 @@ namespace Lidgren.Network m_currentMTU = m_peerConfiguration.MaximumTransmissionUnit; } + /// + /// Change the internal endpoint to this new one. Used when, during handshake, a switch in port is detected (due to NAT) + /// + internal void MutateEndpoint(IPEndPoint endpoint) + { + m_remoteEndpoint = endpoint; + + } + internal void SetStatus(NetConnectionStatus status, string reason) { // user or library thread diff --git a/Lidgren.Network/NetPeer.Internal.cs b/Lidgren.Network/NetPeer.Internal.cs index 97f3eda..aff472d 100644 --- a/Lidgren.Network/NetPeer.Internal.cs +++ b/Lidgren.Network/NetPeer.Internal.cs @@ -463,8 +463,23 @@ namespace Lidgren.Network { if (hs.Key.Address.Equals(senderEndpoint.Address)) { - LogWarning("Detected possible host port switch! TODO: Create new connection and continue handshake"); - return; + if (hs.Value.m_connectionInitiator) + { + // + // We are currently trying to connection to XX.XX.XX.XX:Y + // ... but we just received a ConnectResponse from XX.XX.XX.XX:Z + // Lets just assume the router decided to use this port instead + // + var hsconn = hs.Value; + m_connectionLookup.Remove(hsconn.RemoteEndpoint); + + LogDebug("Detected host port change; rerouting connection to " + senderEndpoint); + hsconn.MutateEndpoint(senderEndpoint); + m_connectionLookup.Add(senderEndpoint, hsconn); + + hsconn.ReceivedHandshake(now, tp, ptr, payloadByteLength); + return; + } } } }