Modified lidgren to support IPv6 (code from https://github.com/lidgren/lidgren-network-gen3/pull/33). TODO: test
This commit is contained in:
@@ -10,7 +10,7 @@
|
|||||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||||
<RootNamespace>Lidgren.Network</RootNamespace>
|
<RootNamespace>Lidgren.Network</RootNamespace>
|
||||||
<AssemblyName>Lidgren.Network</AssemblyName>
|
<AssemblyName>Lidgren.Network</AssemblyName>
|
||||||
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
|
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||||
<FileAlignment>512</FileAlignment>
|
<FileAlignment>512</FileAlignment>
|
||||||
<PublishUrl>publish\</PublishUrl>
|
<PublishUrl>publish\</PublishUrl>
|
||||||
<Install>true</Install>
|
<Install>true</Install>
|
||||||
|
|||||||
@@ -124,17 +124,18 @@ namespace Lidgren.Network
|
|||||||
m_lastSocketBind = now;
|
m_lastSocketBind = now;
|
||||||
|
|
||||||
if (m_socket == null)
|
if (m_socket == null)
|
||||||
m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
|
m_socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
|
||||||
|
|
||||||
if (reBind)
|
if (reBind)
|
||||||
m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, (int)1);
|
m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, (int)1);
|
||||||
|
|
||||||
m_socket.ReceiveBufferSize = m_configuration.ReceiveBufferSize;
|
m_socket.ReceiveBufferSize = m_configuration.ReceiveBufferSize;
|
||||||
m_socket.SendBufferSize = m_configuration.SendBufferSize;
|
m_socket.SendBufferSize = m_configuration.SendBufferSize;
|
||||||
m_socket.Blocking = false;
|
m_socket.Blocking = false;
|
||||||
|
m_socket.DualMode = true;
|
||||||
|
|
||||||
var ep = (EndPoint)new NetEndPoint(m_configuration.LocalAddress, reBind ? m_listenPort : m_configuration.Port);
|
var ep = (EndPoint)new NetEndPoint(m_configuration.LocalAddress.MapToIPv6(), reBind ? m_listenPort : m_configuration.Port);
|
||||||
m_socket.Bind(ep);
|
m_socket.Bind(ep);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -135,7 +135,10 @@ namespace Lidgren.Network
|
|||||||
internal bool ActuallySendPacket(byte[] data, int numBytes, NetEndPoint target, out bool connectionReset)
|
internal bool ActuallySendPacket(byte[] data, int numBytes, NetEndPoint target, out bool connectionReset)
|
||||||
{
|
{
|
||||||
connectionReset = false;
|
connectionReset = false;
|
||||||
IPAddress ba = default(IPAddress);
|
|
||||||
|
target = NetUtility.MapToIPv6(target);
|
||||||
|
|
||||||
|
IPAddress ba = default(IPAddress);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ba = NetUtility.GetCachedBroadcastAddress();
|
ba = NetUtility.GetCachedBroadcastAddress();
|
||||||
|
|||||||
@@ -121,8 +121,8 @@ namespace Lidgren.Network
|
|||||||
m_connections = new List<NetConnection>();
|
m_connections = new List<NetConnection>();
|
||||||
m_connectionLookup = new Dictionary<NetEndPoint, NetConnection>();
|
m_connectionLookup = new Dictionary<NetEndPoint, NetConnection>();
|
||||||
m_handshakes = new Dictionary<NetEndPoint, NetConnection>();
|
m_handshakes = new Dictionary<NetEndPoint, NetConnection>();
|
||||||
m_senderRemote = (EndPoint)new NetEndPoint(IPAddress.Any, 0);
|
m_senderRemote = (EndPoint)new NetEndPoint(IPAddress.IPv6Any, 0);
|
||||||
m_status = NetPeerStatus.NotRunning;
|
m_status = NetPeerStatus.NotRunning;
|
||||||
m_receivedFragmentGroups = new Dictionary<NetConnection, Dictionary<int, ReceivedFragmentGroup>>();
|
m_receivedFragmentGroups = new Dictionary<NetConnection, Dictionary<int, ReceivedFragmentGroup>>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -293,7 +293,9 @@ namespace Lidgren.Network
|
|||||||
if (remoteEndPoint == null)
|
if (remoteEndPoint == null)
|
||||||
throw new ArgumentNullException("remoteEndPoint");
|
throw new ArgumentNullException("remoteEndPoint");
|
||||||
|
|
||||||
lock (m_connections)
|
remoteEndPoint = NetUtility.MapToIPv6(remoteEndPoint);
|
||||||
|
|
||||||
|
lock (m_connections)
|
||||||
{
|
{
|
||||||
if (m_status == NetPeerStatus.NotRunning)
|
if (m_status == NetPeerStatus.NotRunning)
|
||||||
throw new NetException("Must call Start() first");
|
throw new NetException("Must call Start() first");
|
||||||
|
|||||||
@@ -93,8 +93,8 @@ namespace Lidgren.Network
|
|||||||
//
|
//
|
||||||
m_disabledTypes = NetIncomingMessageType.ConnectionApproval | NetIncomingMessageType.UnconnectedData | NetIncomingMessageType.VerboseDebugMessage | NetIncomingMessageType.ConnectionLatencyUpdated | NetIncomingMessageType.NatIntroductionSuccess;
|
m_disabledTypes = NetIncomingMessageType.ConnectionApproval | NetIncomingMessageType.UnconnectedData | NetIncomingMessageType.VerboseDebugMessage | NetIncomingMessageType.ConnectionLatencyUpdated | NetIncomingMessageType.NatIntroductionSuccess;
|
||||||
m_networkThreadName = "Lidgren network thread";
|
m_networkThreadName = "Lidgren network thread";
|
||||||
m_localAddress = IPAddress.Any;
|
m_localAddress = IPAddress.IPv6Any;
|
||||||
m_broadcastAddress = IPAddress.Broadcast;
|
m_broadcastAddress = IPAddress.Broadcast;
|
||||||
var ip = NetUtility.GetBroadcastAddress();
|
var ip = NetUtility.GetBroadcastAddress();
|
||||||
if (ip != null)
|
if (ip != null)
|
||||||
{
|
{
|
||||||
@@ -326,11 +326,10 @@ namespace Lidgren.Network
|
|||||||
m_suppressUnreliableUnorderedAcks = value;
|
m_suppressUnreliableUnorderedAcks = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
/// <summary>
|
/// Gets or sets the local ip address to bind to. Defaults to <see cref="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>
|
||||||
/// </summary>
|
public IPAddress LocalAddress
|
||||||
public IPAddress LocalAddress
|
|
||||||
{
|
{
|
||||||
get { return m_localAddress; }
|
get { return m_localAddress; }
|
||||||
set
|
set
|
||||||
|
|||||||
@@ -98,8 +98,8 @@ namespace Lidgren.Network
|
|||||||
NetAddress ipAddress = null;
|
NetAddress ipAddress = null;
|
||||||
if (NetAddress.TryParse(ipOrHost, out ipAddress))
|
if (NetAddress.TryParse(ipOrHost, out ipAddress))
|
||||||
{
|
{
|
||||||
if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
|
if (ipAddress.AddressFamily == AddressFamily.InterNetwork || ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
|
||||||
{
|
{
|
||||||
callback(ipAddress);
|
callback(ipAddress);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -139,7 +139,7 @@ namespace Lidgren.Network
|
|||||||
// check each entry for a valid IP address
|
// check each entry for a valid IP address
|
||||||
foreach (var ipCurrent in entry.AddressList)
|
foreach (var ipCurrent in entry.AddressList)
|
||||||
{
|
{
|
||||||
if (ipCurrent.AddressFamily == AddressFamily.InterNetwork)
|
if (ipCurrent.AddressFamily == AddressFamily.InterNetwork || ipCurrent.AddressFamily == AddressFamily.InterNetworkV6)
|
||||||
{
|
{
|
||||||
callback(ipCurrent);
|
callback(ipCurrent);
|
||||||
return;
|
return;
|
||||||
@@ -176,22 +176,22 @@ namespace Lidgren.Network
|
|||||||
NetAddress ipAddress = null;
|
NetAddress ipAddress = null;
|
||||||
if (NetAddress.TryParse(ipOrHost, out ipAddress))
|
if (NetAddress.TryParse(ipOrHost, out ipAddress))
|
||||||
{
|
{
|
||||||
if (ipAddress.AddressFamily == AddressFamily.InterNetwork)
|
if (ipAddress.AddressFamily == AddressFamily.InterNetwork || ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
|
||||||
return ipAddress;
|
return ipAddress;
|
||||||
throw new ArgumentException("This method will not currently resolve other than ipv4 addresses");
|
throw new ArgumentException("This method will not currently resolve other than IPv4 or IPv6 addresses");
|
||||||
}
|
}
|
||||||
|
|
||||||
// ok must be a host name
|
// ok must be a host name
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var addresses = Dns.GetHostAddresses(ipOrHost);
|
var addresses = Dns.GetHostAddresses(ipOrHost);
|
||||||
if (addresses == null)
|
if (addresses == null)
|
||||||
return null;
|
return null;
|
||||||
foreach (var address in addresses)
|
foreach (var address in addresses)
|
||||||
{
|
{
|
||||||
if (address.AddressFamily == AddressFamily.InterNetwork)
|
if (address.AddressFamily == AddressFamily.InterNetwork || address.AddressFamily == AddressFamily.InterNetworkV6)
|
||||||
return address;
|
return address;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
catch (SocketException ex)
|
catch (SocketException ex)
|
||||||
@@ -453,5 +453,17 @@ namespace Lidgren.Network
|
|||||||
// this is defined in the platform specific files
|
// this is defined in the platform specific files
|
||||||
return ComputeSHAHash(bytes, 0, bytes.Length);
|
return ComputeSHAHash(bytes, 0, bytes.Length);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
/// <summary>
|
||||||
|
/// Maps the IPEndPoint object to an IPv6 address, if it is currently mapped to an IPv4 address.
|
||||||
|
/// </summary>
|
||||||
|
internal static IPEndPoint MapToIPv6(IPEndPoint endPoint)
|
||||||
|
{
|
||||||
|
if (endPoint.AddressFamily == AddressFamily.InterNetwork)
|
||||||
|
{
|
||||||
|
return new IPEndPoint(endPoint.Address.MapToIPv6(), endPoint.Port);
|
||||||
|
}
|
||||||
|
return endPoint;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user