Skip to content

Commit

Permalink
Merge pull request #136 from space-wizards/20-02-15-ipv6-fix
Browse files Browse the repository at this point in the history
IPv6: Space Wizard Edition.
  • Loading branch information
lidgren authored Feb 15, 2020
2 parents 78df229 + f0e5bb4 commit 2df564e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 42 deletions.
21 changes: 13 additions & 8 deletions Lidgren.Network/NetPeer.Internal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,19 @@ private void BindSocket(bool reBind)
m_socket.SendBufferSize = m_configuration.SendBufferSize;
m_socket.Blocking = false;

if(m_configuration.DualStack && m_configuration.LocalAddress.AddressFamily == AddressFamily.InterNetworkV6)
m_socket.DualMode = true;

var localAddress = m_configuration.DualStack
? m_configuration.LocalAddress.MapToIPv6()
: m_configuration.LocalAddress;

var ep = (EndPoint)new NetEndPoint(localAddress, reBind ? m_listenPort : m_configuration.Port);
if (m_configuration.DualStack)
{
if (m_configuration.LocalAddress.AddressFamily != AddressFamily.InterNetworkV6)
{
LogWarning("Configuration specifies Dual Stack but does not use IPv6 local address; Dual stack will not work.");
}
else
{
m_socket.DualMode = true;
}
}

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

try
Expand Down
28 changes: 17 additions & 11 deletions Lidgren.Network/NetPeer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Threading;
using System.Collections.Generic;
using System.Net;

using System.Net.Sockets;
#if !__NOIPENDPOINT__
using NetEndPoint = System.Net.IPEndPoint;
#endif
Expand Down Expand Up @@ -32,8 +32,8 @@ public partial class NetPeer

/// <summary>
/// Signalling event which can be waited on to determine when a message is queued for reading.
/// Note that there is no guarantee that after the event is signaled the blocked thread will
/// find the message in the queue. Other user created threads could be preempted and dequeue
/// Note that there is no guarantee that after the event is signaled the blocked thread will
/// find the message in the queue. Other user created threads could be preempted and dequeue
/// the message before the waiting thread wakes up.
/// </summary>
public AutoResetEvent MessageReceivedEvent
Expand Down Expand Up @@ -121,10 +121,16 @@ public NetPeer(NetPeerConfiguration config)
m_connections = new List<NetConnection>();
m_connectionLookup = new Dictionary<NetEndPoint, NetConnection>();
m_handshakes = new Dictionary<NetEndPoint, NetConnection>();
var address = config.DualStack ? IPAddress.IPv6Any : IPAddress.Any;
m_senderRemote = (EndPoint)new NetEndPoint(address, 0);
if (m_configuration.LocalAddress.AddressFamily == AddressFamily.InterNetworkV6)
{
m_senderRemote = (EndPoint)new IPEndPoint(IPAddress.IPv6Any, 0);
}
else
{
m_senderRemote = (EndPoint)new IPEndPoint(IPAddress.Any, 0);
}
m_status = NetPeerStatus.NotRunning;
m_receivedFragmentGroups = new Dictionary<NetConnection, Dictionary<int, ReceivedFragmentGroup>>();
m_receivedFragmentGroups = new Dictionary<NetConnection, Dictionary<int, ReceivedFragmentGroup>>();
}

/// <summary>
Expand All @@ -149,7 +155,7 @@ public void Start()
}

InitializeNetwork();

// start network thread
m_networkThread = new Thread(new ThreadStart(NetworkLoop));
m_networkThread.Name = m_configuration.NetworkThreadName;
Expand Down Expand Up @@ -184,19 +190,19 @@ public NetConnection GetConnection(NetEndPoint ep)
public NetIncomingMessage WaitMessage(int maxMillis)
{
NetIncomingMessage msg = ReadMessage();

while (msg == null)
{
// This could return true...
if (!MessageReceivedEvent.WaitOne(maxMillis))
{
return null;
}

// ... while this will still returns null. That's why we need to cycle.
msg = ReadMessage();
}

return msg;
}

Expand All @@ -216,7 +222,7 @@ public NetIncomingMessage ReadMessage()
}
return retval;
}

/// <summary>
/// Reads a pending message from any connection, if any.
/// Returns true if message was read, otherwise false.
Expand Down
24 changes: 11 additions & 13 deletions Lidgren.Network/NetPeerConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ public sealed class NetPeerConfiguration
// -4 bytes to be on the safe side and align to 8-byte boundary
// Total 1408 bytes
// Note that lidgren headers (5 bytes) are not included here; since it's part of the "mtu payload"

/// <summary>
/// Default MTU value in bytes
/// </summary>
public const int kDefaultMTU = 1408;

private const string c_isLockedMessage = "You may not modify the NetPeerConfiguration after it has been used to initialize a NetPeer";

private bool m_isLocked;
Expand Down Expand Up @@ -344,23 +344,21 @@ public IPAddress LocalAddress
}

/// <summary>
/// Gets or sets a value indicating whether the library should use IPv6 dual stack mode
/// Gets or sets a value indicating whether the library should use IPv6 dual stack mode.
/// If you enable this you should make sure that the <see cref="LocalAddress"/> is an IPv6 address.
/// Cannot be changed once NetPeer is initialized.
/// </summary>
public bool DualStack
{
public bool DualStack
{
get { return m_dualStack; }
set
{
set
{
if (m_isLocked)
throw new NetException(c_isLockedMessage);
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;
}
}
}


/// <summary>
/// Gets or sets the local broadcast address to use when broadcasting
/// </summary>
Expand Down
20 changes: 10 additions & 10 deletions Lidgren.Network/NetUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public static void ResolveAsync(string ipOrHost, ResolveAddressCallback callback
}
}

/// <summary>
/// <summary>
/// Get IPv4 address from notation (xxx.xxx.xxx.xxx) or hostname
/// </summary>
public static NetAddress Resolve(string ipOrHost)
Expand Down Expand Up @@ -240,7 +240,7 @@ public static string ToHexString(byte[] data, int offset, int length)
}
return new string(c);
}

/// <summary>
/// Returns true if the endpoint supplied is on the same subnet as this host
/// </summary>
Expand Down Expand Up @@ -471,23 +471,23 @@ public static byte[] ComputeSHAHash(byte[] bytes)
/// </summary>
/// <param name="src">Source.</param>
/// <param name="dst">Destination.</param>
internal static void CopyEndpoint(IPEndPoint src, IPEndPoint dst)
{
internal static void CopyEndpoint(IPEndPoint src, IPEndPoint dst)
{
dst.Port = src.Port;
if (src.AddressFamily == AddressFamily.InterNetwork)
dst.Address = src.Address.MapToIPv6();
else
dst.Address = src.Address;
dst.Address = src.Address;
}

/// <summary>
/// Maps the IPEndPoint object to an IPv6 address. Has allocation
/// </summary>
internal static IPEndPoint MapToIPv6(IPEndPoint endPoint)
{
internal static IPEndPoint MapToIPv6(IPEndPoint endPoint)
{
if (endPoint.AddressFamily == AddressFamily.InterNetwork)
return new IPEndPoint(endPoint.Address.MapToIPv6(), endPoint.Port);
return endPoint;
}
return endPoint;
}
}
}
}

0 comments on commit 2df564e

Please sign in to comment.