Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
IOControlKeepAlive.cs
Go to the documentation of this file.
2
3namespace System.Net.Sockets;
4
5internal sealed class IOControlKeepAlive
6{
8
10
11 [ThreadStatic]
12 private static byte[] s_keepAliveValuesBuffer;
13
14 private uint _timeMs = 7200000u;
15
16 private uint _intervalMs = 1000u;
17
19
20 public static SocketError Get(SafeSocketHandle handle, SocketOptionName optionName, byte[] optionValueSeconds, ref int optionLength)
21 {
22 if (optionValueSeconds == null || !BitConverter.TryWriteBytes(optionValueSeconds.AsSpan(), Get(handle, optionName)))
23 {
24 return SocketError.Fault;
25 }
26 optionLength = optionValueSeconds.Length;
27 return SocketError.Success;
28 }
29
30 public static int Get(SafeSocketHandle handle, SocketOptionName optionName)
31 {
32 if (s_socketKeepAliveTable.TryGetValue(handle, out var value))
33 {
34 if (optionName != SocketOptionName.TypeOfService)
35 {
36 return MillisecondsToSeconds(value._intervalMs);
37 }
38 return MillisecondsToSeconds(value._timeMs);
39 }
40 if (optionName != SocketOptionName.TypeOfService)
41 {
42 return MillisecondsToSeconds(1000u);
43 }
44 return MillisecondsToSeconds(7200000u);
45 }
46
47 public static SocketError Set(SafeSocketHandle handle, SocketOptionName optionName, byte[] optionValueSeconds)
48 {
49 if (optionValueSeconds == null || optionValueSeconds.Length < 4)
50 {
51 return SocketError.Fault;
52 }
53 return Set(handle, optionName, BitConverter.ToInt32(optionValueSeconds, 0));
54 }
55
56 public static SocketError Set(SafeSocketHandle handle, SocketOptionName optionName, int optionValueSeconds)
57 {
59 if (optionName == SocketOptionName.TypeOfService)
60 {
61 value._timeMs = SecondsToMilliseconds(optionValueSeconds);
62 }
63 else
64 {
65 value._intervalMs = SecondsToMilliseconds(optionValueSeconds);
66 }
67 byte[] array = s_keepAliveValuesBuffer ?? (s_keepAliveValuesBuffer = new byte[12]);
68 value.Fill(array);
69 int optionLength = 0;
70 return SocketPal.WindowsIoctl(handle, -1744830460, array, null, out optionLength);
71 }
72
74 {
75 AddressFamily addressFamily = (Socket.OSSupportsIPv4 ? AddressFamily.InterNetwork : AddressFamily.InterNetworkV6);
76 using Socket socket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp);
77 int optionValue = MillisecondsToSeconds(7200000u);
78 SocketError socketError = global::Interop.Winsock.setsockopt(socket.SafeHandle, SocketOptionLevel.Tcp, SocketOptionName.TypeOfService, ref optionValue, 4);
79 int optionValue2 = MillisecondsToSeconds(1000u);
80 SocketError socketError2 = global::Interop.Winsock.setsockopt(socket.SafeHandle, SocketOptionLevel.Tcp, SocketOptionName.BlockSource, ref optionValue2, 4);
81 return socketError == SocketError.Success && socketError2 == SocketError.Success;
82 }
83
84 private static int MillisecondsToSeconds(uint milliseconds)
85 {
86 return (int)(milliseconds / 1000);
87 }
88
89 private static uint SecondsToMilliseconds(int seconds)
90 {
91 return (uint)(seconds * 1000);
92 }
93
94 private void Fill(byte[] buffer)
95 {
97 }
98}
static bool TryWriteBytes(Span< byte > destination, bool value)
static int ToInt32(byte[] value, int startIndex)
static SocketError Set(SafeSocketHandle handle, SocketOptionName optionName, int optionValueSeconds)
static readonly ConditionalWeakTable< SafeSocketHandle, IOControlKeepAlive > s_socketKeepAliveTable
static readonly bool s_supportsKeepAliveViaSocketOption
static int MillisecondsToSeconds(uint milliseconds)
static SocketError Set(SafeSocketHandle handle, SocketOptionName optionName, byte[] optionValueSeconds)
static uint SecondsToMilliseconds(int seconds)
static SocketError Get(SafeSocketHandle handle, SocketOptionName optionName, byte[] optionValueSeconds, ref int optionLength)
static int Get(SafeSocketHandle handle, SocketOptionName optionName)
static SocketError WindowsIoctl(SafeSocketHandle handle, int ioControlCode, byte[] optionInValue, byte[] optionOutValue, out int optionLength)
Definition SocketPal.cs:468