tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
SocketExtensions.cs
Go to the documentation of this file.
1using System;
2using System.Net;
3using System.Net.Sockets;
4
6
8{
12 static class SocketExtensions
13 {
21 public static void BindTest(IPlatformIdentifier platformIdentifier, ushort port, bool includeIPv6, bool udp)
22 {
23 ArgumentNullException.ThrowIfNull(platformIdentifier);
25 {
26 using var socket = new Socket(
27 includeIPv6
28 ? AddressFamily.InterNetworkV6
29 : AddressFamily.InterNetwork,
30 udp
31 ? SocketType.Dgram
32 : SocketType.Stream,
33 udp
34 ? ProtocolType.Udp
35 : ProtocolType.Tcp);
36
37 socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ExclusiveAddressUse, true);
38 socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, false);
39 if (!udp && platformIdentifier.IsWindows)
40 socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true);
41
42 if (includeIPv6)
43 socket.DualMode = true;
44
45 socket.Bind(
46 new IPEndPoint(
47 includeIPv6
48 ? IPAddress.IPv6Any
49 : IPAddress.Any,
50 port));
51 });
52 }
53 }
54}
Extension methods for the Socket class.
static void BindTest(IPlatformIdentifier platformIdentifier, ushort port, bool includeIPv6, bool udp)
Attempt to exclusively bind to a given port .
static void WithProcessLaunchExclusivity(Action action)
Runs a given action making sure to not launch any processes while its running.
For identifying the current platform.
bool IsWindows
If the current platform is a Windows platform.