Skip to content

Commit

Permalink
Merge pull request #126 from Badartefact/master
Browse files Browse the repository at this point in the history
Fixed bugs after introducing IPv6 dual mode
  • Loading branch information
lidgren authored Jun 14, 2019
2 parents d577f87 + b9bfbb0 commit c18c8b9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
6 changes: 5 additions & 1 deletion Lidgren.Network/NetPeer.Internal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ private void BindSocket(bool reBind)
if(m_configuration.DualStack && m_configuration.LocalAddress.AddressFamily == AddressFamily.InterNetworkV6)
m_socket.DualMode = true;

var ep = (EndPoint)new NetEndPoint(m_configuration.LocalAddress.MapToIPv6(), reBind ? m_listenPort : m_configuration.Port);
var localAddress = m_configuration.DualStack
? m_configuration.LocalAddress.MapToIPv6()
: m_configuration.LocalAddress;

var ep = (EndPoint)new NetEndPoint(localAddress, reBind ? m_listenPort : m_configuration.Port);
m_socket.Bind(ep);

try
Expand Down
9 changes: 7 additions & 2 deletions Lidgren.Network/NetPeer.LatencySimulation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,13 @@ internal bool ActuallySendPacket(byte[] data, int numBytes, NetEndPoint target,
m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Broadcast, true);
}
else if(m_configuration.DualStack && m_configuration.LocalAddress.AddressFamily == AddressFamily.InterNetworkV6)
NetUtility.CopyEndpoint(target, targetCopy); //Maps to IPv6 for Dual Mode

NetUtility.CopyEndpoint(target, targetCopy); //Maps to IPv6 for Dual Mode
else
{
targetCopy.Port = target.Port;
targetCopy.Address = target.Address;
}

int bytesSent = m_socket.SendTo(data, 0, numBytes, SocketFlags.None, targetCopy);
if (numBytes != bytesSent)
LogWarning("Failed to send the full " + numBytes + "; only " + bytesSent + " bytes sent in packet!");
Expand Down
3 changes: 2 additions & 1 deletion Lidgren.Network/NetPeer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ public NetPeer(NetPeerConfiguration config)
m_connections = new List<NetConnection>();
m_connectionLookup = new Dictionary<NetEndPoint, NetConnection>();
m_handshakes = new Dictionary<NetEndPoint, NetConnection>();
m_senderRemote = (EndPoint)new NetEndPoint(IPAddress.IPv6Any, 0);
var address = config.DualStack ? IPAddress.IPv6Any : IPAddress.Any;
m_senderRemote = (EndPoint)new NetEndPoint(address, 0);
m_status = NetPeerStatus.NotRunning;
m_receivedFragmentGroups = new Dictionary<NetConnection, Dictionary<int, ReceivedFragmentGroup>>();
}
Expand Down
10 changes: 7 additions & 3 deletions Lidgren.Network/NetPeerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public NetPeerConfiguration(string appIdentifier)
//
m_disabledTypes = NetIncomingMessageType.ConnectionApproval | NetIncomingMessageType.UnconnectedData | NetIncomingMessageType.VerboseDebugMessage | NetIncomingMessageType.ConnectionLatencyUpdated | NetIncomingMessageType.NatIntroductionSuccess;
m_networkThreadName = "Lidgren network thread";
m_localAddress = IPAddress.IPv6Any;
m_localAddress = IPAddress.Any;
m_broadcastAddress = IPAddress.Broadcast;
var ip = NetUtility.GetBroadcastAddress();
if (ip != null)
Expand Down Expand Up @@ -330,7 +330,7 @@ public bool SuppressUnreliableUnorderedAcks
}

/// <summary>
/// Gets or sets the local ip address to bind to. Defaults to IPAddress.IPv6Any. Cannot be changed once NetPeer is initialized.
/// Gets or sets the local ip address to bind to. Defaults to IPAddress.Any. Cannot be changed once NetPeer is initialized.
/// </summary>
public IPAddress LocalAddress
{
Expand All @@ -353,7 +353,11 @@ public bool DualStack
{
if (m_isLocked)
throw new NetException(c_isLockedMessage);
m_dualStack = value;
m_dualStack = value;
if (m_dualStack && m_localAddress.Equals(IPAddress.Any))
m_localAddress = IPAddress.IPv6Any;
if (!m_dualStack && m_localAddress.Equals(IPAddress.IPv6Any))
m_localAddress = IPAddress.Any;
}
}

Expand Down
2 changes: 1 addition & 1 deletion Lidgren.Network/NetUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public static NetAddress Resolve(string ipOrHost)
return null;
foreach (var address in addresses)
{
if (address.AddressFamily == AddressFamily.InterNetwork || ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
if (address.AddressFamily == AddressFamily.InterNetwork || address.AddressFamily == AddressFamily.InterNetworkV6)
return address;
}
return null;
Expand Down

0 comments on commit c18c8b9

Please sign in to comment.