Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SocketAddress.cs
Go to the documentation of this file.
3
5
6internal 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
48 public SocketAddress(AddressFamily family, int size)
49 {
50 if (size < 2)
51 {
52 throw new ArgumentOutOfRangeException("size");
53 }
54 InternalSize = size;
55 Buffer = new byte[(size / IntPtr.Size + 2) * IntPtr.Size];
57 }
58
59 internal SocketAddress(IPAddress ipAddress)
61 {
63 if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
64 {
65 Span<byte> span = stackalloc byte[16];
66 ipAddress.TryWriteBytes(span, out var _);
68 }
69 else
70 {
71 uint address = (uint)ipAddress.Address;
73 }
74 }
75
76 internal SocketAddress(IPAddress ipaddress, int port)
77 : this(ipaddress)
78 {
80 }
81
83 {
84 Buffer = buffer.ToArray();
85 InternalSize = Buffer.Length;
86 }
87
89 {
90 if (Family == AddressFamily.InterNetworkV6)
91 {
92 Span<byte> span = stackalloc byte[16];
93 System.Net.SocketAddressPal.GetIPv6Address(Buffer, span, out var scope);
94 return new IPAddress(span, scope);
95 }
96 if (Family == AddressFamily.InterNetwork)
97 {
98 long newAddress = (long)System.Net.SocketAddressPal.GetIPv4Address(Buffer) & 0xFFFFFFFFL;
99 return new IPAddress(newAddress);
100 }
101 throw new SocketException(10047);
102 }
103
105 {
106 IPAddress iPAddress = GetIPAddress();
108 return new IPEndPoint(iPAddress, port);
109 }
110
112 {
113 Buffer[Buffer.Length - IntPtr.Size] = (byte)InternalSize;
114 Buffer[Buffer.Length - IntPtr.Size + 1] = (byte)(InternalSize >> 8);
115 Buffer[Buffer.Length - IntPtr.Size + 2] = (byte)(InternalSize >> 16);
116 Buffer[Buffer.Length - IntPtr.Size + 3] = (byte)(InternalSize >> 24);
117 }
118
119 internal int GetAddressSizeOffset()
120 {
121 return Buffer.Length - IntPtr.Size;
122 }
123
124 public override bool Equals(object comparand)
125 {
126 if (!(comparand is SocketAddress socketAddress) || Size != socketAddress.Size)
127 {
128 return false;
129 }
130 for (int i = 0; i < Size; i++)
131 {
132 if (this[i] != socketAddress[i])
133 {
134 return false;
135 }
136 }
137 return true;
138 }
139
140 public override int GetHashCode()
141 {
142 if (_changed)
143 {
144 _changed = false;
145 _hash = 0;
146 int num = Size & -4;
147 int i;
148 for (i = 0; i < num; i += 4)
149 {
151 }
152 if (((uint)Size & 3u) != 0)
153 {
154 int num2 = 0;
155 int num3 = 0;
156 for (; i < Size; i++)
157 {
158 num2 |= Buffer[i] << num3;
159 num3 += 8;
160 }
161 _hash ^= num2;
162 }
163 }
164 return _hash;
165 }
166
167 public override string ToString()
168 {
169 string text = Family.ToString();
170 int num = text.Length + 1 + 10 + 2 + (Size - 2) * 4 + 1;
171 Span<char> span = ((num > 256) ? ((Span<char>)new char[num]) : stackalloc char[256]);
172 Span<char> destination = span;
174 int length = text.Length;
175 destination[length++] = ':';
176 bool flag = Size.TryFormat(destination.Slice(length), out var charsWritten);
177 length += charsWritten;
178 destination[length++] = ':';
179 destination[length++] = '{';
180 byte[] buffer = Buffer;
181 for (int i = 2; i < Size; i++)
182 {
183 if (i > 2)
184 {
185 destination[length++] = ',';
186 }
187 flag = buffer[i].TryFormat(destination.Slice(length), out charsWritten);
188 length += charsWritten;
189 }
190 destination[length++] = '}';
191 return destination.Slice(0, length).ToString();
192 }
193}
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 readonly int IPv4AddressSize
override bool Equals(object comparand)
SocketAddress(AddressFamily addressFamily, ReadOnlySpan< byte > buffer)
SocketAddress(AddressFamily family, int size)
SocketAddress(IPAddress ipAddress)
static readonly int IPv6AddressSize
SocketAddress(IPAddress ipaddress, int port)
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 int Size
Definition IntPtr.cs:21
void CopyTo(Span< T > destination)
Definition Span.cs:224