mirror of
https://github.com/tgstation/tgstation-server.git
synced 2026-08-24 21:46:52 +01:00
Hopefully fixes socket bind testing for the last time
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System.Net;
|
||||
using System;
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
|
||||
namespace Tgstation.Server.Host.Extensions
|
||||
@@ -15,24 +16,32 @@ namespace Tgstation.Server.Host.Extensions
|
||||
/// <param name="includeIPv6">If IPV6 should be tested as well.</param>
|
||||
public static void BindTest(ushort port, bool includeIPv6)
|
||||
{
|
||||
using var socket = new Socket(
|
||||
includeIPv6
|
||||
? AddressFamily.InterNetworkV6
|
||||
: AddressFamily.InterNetwork,
|
||||
SocketType.Stream,
|
||||
ProtocolType.Tcp);
|
||||
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, true);
|
||||
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, false);
|
||||
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);
|
||||
if (includeIPv6)
|
||||
socket.DualMode = true;
|
||||
|
||||
socket.Bind(
|
||||
new IPEndPoint(
|
||||
void BindSocket(bool forCleanup)
|
||||
{
|
||||
using var socket = new Socket(
|
||||
includeIPv6
|
||||
? IPAddress.IPv6Any
|
||||
: IPAddress.Any,
|
||||
port));
|
||||
? AddressFamily.InterNetworkV6
|
||||
: AddressFamily.InterNetwork,
|
||||
SocketType.Stream,
|
||||
ProtocolType.Tcp);
|
||||
|
||||
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, !forCleanup);
|
||||
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, forCleanup);
|
||||
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);
|
||||
if (includeIPv6)
|
||||
socket.DualMode = true;
|
||||
|
||||
socket.Bind(
|
||||
new IPEndPoint(
|
||||
includeIPv6
|
||||
? IPAddress.IPv6Any
|
||||
: IPAddress.Any,
|
||||
port));
|
||||
}
|
||||
|
||||
GC.Collect(Int32.MaxValue, GCCollectionMode.Forced, true, false);
|
||||
BindSocket(false);
|
||||
BindSocket(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user