Build 0.21.1.0

This commit is contained in:
Markus Isberg
2023-01-13 18:10:35 +02:00
parent 2f7205fb4b
commit 697ec52120
155 changed files with 2423 additions and 1237 deletions

View File

@@ -124,7 +124,24 @@ namespace Lidgren.Network
m_lastSocketBind = now;
if (m_socket == null)
m_socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
{
try
{
m_socket = new Socket(AddressFamily.InterNetworkV6, SocketType.Dgram, ProtocolType.Udp);
}
catch (SocketException socketException)
{
if (socketException.SocketErrorCode == SocketError.AddressFamilyNotSupported)
{
// Fall back to IPv4 if IPv6 is unsupported
m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
}
else
{
throw;
}
}
}
if (reBind)
m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, (int)1);
@@ -132,9 +149,9 @@ namespace Lidgren.Network
m_socket.ReceiveBufferSize = m_configuration.ReceiveBufferSize;
m_socket.SendBufferSize = m_configuration.SendBufferSize;
m_socket.Blocking = false;
m_socket.DualMode = m_configuration.UseDualModeSockets;
if (m_socket.AddressFamily == AddressFamily.InterNetworkV6) { m_socket.DualMode = m_configuration.UseDualModeSockets; }
var ep = (EndPoint)new NetEndPoint(m_configuration.LocalAddress.MapToIPv6(), reBind ? m_listenPort : m_configuration.Port);
var ep = (EndPoint)new NetEndPoint(m_configuration.LocalAddress.MapToFamily(m_socket.AddressFamily), reBind ? m_listenPort : m_configuration.Port);
m_socket.Bind(ep);
try
@@ -413,6 +430,10 @@ namespace Lidgren.Network
int bytesReceived = 0;
try
{
if (m_senderRemote is IPEndPoint ipEndpoint && ipEndpoint.AddressFamily != m_socket.AddressFamily)
{
m_senderRemote = ipEndpoint.MapToFamily(m_socket.AddressFamily);
}
bytesReceived = m_socket.ReceiveFrom(m_receiveBuffer, 0, m_receiveBuffer.Length, SocketFlags.None, ref m_senderRemote);
}
catch (SocketException sx)

View File

@@ -136,7 +136,7 @@ namespace Lidgren.Network
{
connectionReset = false;
target = NetUtility.MapToIPv6(target);
target = target.MapToFamily(m_socket.AddressFamily);
IPAddress ba = default(IPAddress);
try

View File

@@ -293,7 +293,7 @@ namespace Lidgren.Network
if (remoteEndPoint == null)
throw new ArgumentNullException("remoteEndPoint");
remoteEndPoint = NetUtility.MapToIPv6(remoteEndPoint);
remoteEndPoint = remoteEndPoint.MapToFamily(m_socket.AddressFamily);
lock (m_connections)
{

View File

@@ -454,16 +454,24 @@ namespace Lidgren.Network
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;
}
internal static IPAddress MapToFamily(this IPAddress address, AddressFamily family)
{
switch (family)
{
case AddressFamily.InterNetworkV6:
return address.MapToIPv6();
case AddressFamily.InterNetwork:
return address.MapToIPv4();
default:
throw new Exception($"Unsupported address family: {family}");
}
}
internal static IPEndPoint MapToFamily(this IPEndPoint endpoint, AddressFamily family)
{
return endpoint.Address.AddressFamily == family
? endpoint
: new IPEndPoint(endpoint.Address.MapToFamily(family), endpoint.Port);
}
}
}