Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SocketAddress.cs
Go to the documentation of this file.
3
4namespace System.Net;
5
6public class SocketAddress
7{
8 internal static readonly int IPv6AddressSize = 28;
9
10 internal static readonly int IPv4AddressSize = 16;
11
12 internal int InternalSize;
13
14 internal byte[] Buffer;
15
16 private bool _changed = true;
17
18 private int _hash;
19
21
22 public int Size => InternalSize;
23
24 public byte this[int offset]
25 {
26 get
27 {
28 if (offset < 0 || offset >= Size)
29 {
30 throw new IndexOutOfRangeException();
31 }
32 return Buffer[offset];
33 }
34 set
35 {
36 if (offset < 0 || offset >= Size)
37 {
38 throw new IndexOutOfRangeException();
39 }
40 if (Buffer[offset] != value)
41 {
42 _changed = true;
43 }
45 }
46 }
47
49 : this(family, 32)
50 {
51 }
52
53 public SocketAddress(AddressFamily family, int size)
54 {
55 if (size < 2)
56 {
57 throw new ArgumentOutOfRangeException("size");
58 }
59 InternalSize = size;
60 Buffer = new byte[(size / IntPtr.Size + 2) * IntPtr.Size];
62 }
63
64 internal SocketAddress(IPAddress ipAddress)
66 {
68 if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
69 {
70 Span<byte> span = stackalloc byte[16];
71 ipAddress.TryWriteBytes(span, out var _);
72 SocketAddressPal.SetIPv6Address(Buffer, span, (uint)ipAddress.ScopeId);
73 }
74 else
75 {
76 uint address = (uint)ipAddress.Address;
78 }
79 }
80
81 internal SocketAddress(IPAddress ipaddress, int port)
82 : this(ipaddress)
83 {
84 SocketAddressPal.SetPort(Buffer, (ushort)port);
85 }
86
88 {
89 if (Family == AddressFamily.InterNetworkV6)
90 {
91 Span<byte> span = stackalloc byte[16];
92 SocketAddressPal.GetIPv6Address(Buffer, span, out var scope);
93 return new IPAddress(span, scope);
94 }
95 if (Family == AddressFamily.InterNetwork)
96 {
97 long newAddress = (long)SocketAddressPal.GetIPv4Address(Buffer) & 0xFFFFFFFFL;
98 return new IPAddress(newAddress);
99 }
100 throw new SocketException(SocketError.AddressFamilyNotSupported);
101 }
102
104 {
105 IPAddress iPAddress = GetIPAddress();
106 int port = SocketAddressPal.GetPort(Buffer);
107 return new IPEndPoint(iPAddress, port);
108 }
109
110 public override bool Equals(object? comparand)
111 {
112 if (!(comparand is SocketAddress socketAddress) || Size != socketAddress.Size)
113 {
114 return false;
115 }
116 for (int i = 0; i < Size; i++)
117 {
118 if (this[i] != socketAddress[i])
119 {
120 return false;
121 }
122 }
123 return true;
124 }
125
126 public override int GetHashCode()
127 {
128 if (_changed)
129 {
130 _changed = false;
131 _hash = 0;
132 int num = Size & -4;
133 int i;
134 for (i = 0; i < num; i += 4)
135 {
137 }
138 if (((uint)Size & 3u) != 0)
139 {
140 int num2 = 0;
141 int num3 = 0;
142 for (; i < Size; i++)
143 {
144 num2 |= Buffer[i] << num3;
145 num3 += 8;
146 }
147 _hash ^= num2;
148 }
149 }
150 return _hash;
151 }
152
153 public override string ToString()
154 {
155 string text = Family.ToString();
156 int num = text.Length + 1 + 10 + 2 + (Size - 2) * 4 + 1;
157 Span<char> span = ((num > 256) ? ((Span<char>)new char[num]) : stackalloc char[256]);
158 Span<char> destination = span;
160 int length = text.Length;
161 destination[length++] = ':';
162 bool flag = Size.TryFormat(destination.Slice(length), out var charsWritten);
163 length += charsWritten;
164 destination[length++] = ':';
165 destination[length++] = '{';
166 byte[] buffer = Buffer;
167 for (int i = 2; i < Size; i++)
168 {
169 if (i > 2)
170 {
171 destination[length++] = ',';
172 }
173 flag = buffer[i].TryFormat(destination.Slice(length), out charsWritten);
174 length += charsWritten;
175 }
176 destination[length++] = '}';
177 return destination.Slice(0, length).ToString();
178 }
179}
static int ReadInt32LittleEndian(ReadOnlySpan< byte > source)
bool TryWriteBytes(Span< byte > destination, out int bytesWritten)
Definition IPAddress.cs:334
AddressFamily AddressFamily
Definition IPAddress.cs:88
static uint GetIPv4Address(ReadOnlySpan< byte > buffer)
static void SetIPv6Address(byte[] buffer, Span< byte > address, uint scope)
static void SetAddressFamily(byte[] buffer, AddressFamily family)
static void SetPort(byte[] buffer, ushort port)
static ushort GetPort(ReadOnlySpan< byte > buffer)
static void SetIPv4Address(byte[] buffer, uint address)
static void GetIPv6Address(ReadOnlySpan< byte > buffer, Span< byte > address, out uint scope)
static AddressFamily GetAddressFamily(ReadOnlySpan< byte > buffer)
static readonly int IPv4AddressSize
static readonly int IPv6AddressSize
SocketAddress(AddressFamily family)
SocketAddress(IPAddress ipAddress)
SocketAddress(IPAddress ipaddress, int port)
override bool Equals(object? comparand)
SocketAddress(AddressFamily family, int size)
override string ToString()
static int Size
Definition IntPtr.cs:21
void CopyTo(Span< T > destination)
Definition Span.cs:224