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 }
35
36 public SocketAddress(AddressFamily family, int size)
37 {
38 if (size < 2)
39 {
40 throw new ArgumentOutOfRangeException("size");
41 }
42 InternalSize = size;
43 Buffer = new byte[(size / IntPtr.Size + 2) * IntPtr.Size];
45 }
46
47 internal SocketAddress(IPAddress ipAddress)
49 {
51 if (ipAddress.AddressFamily == AddressFamily.InterNetworkV6)
52 {
53 Span<byte> span = stackalloc byte[16];
54 ipAddress.TryWriteBytes(span, out var _);
56 }
57 else
58 {
59 uint address = (uint)ipAddress.Address;
61 }
62 }
63
65 {
66 if (Family == AddressFamily.InterNetworkV6)
67 {
68 Span<byte> span = stackalloc byte[16];
69 System.Net.SocketAddressPal.GetIPv6Address(Buffer, span, out var scope);
70 return new IPAddress(span, scope);
71 }
72 if (Family == AddressFamily.InterNetwork)
73 {
74 long newAddress = (long)System.Net.SocketAddressPal.GetIPv4Address(Buffer) & 0xFFFFFFFFL;
75 return new IPAddress(newAddress);
76 }
77 throw new SocketException(10047);
78 }
79
80 public override bool Equals(object comparand)
81 {
82 if (!(comparand is System.Net.Internals.SocketAddress socketAddress) || Size != socketAddress.Size)
83 {
84 return false;
85 }
86 for (int i = 0; i < Size; i++)
87 {
88 if (this[i] != socketAddress[i])
89 {
90 return false;
91 }
92 }
93 return true;
94 }
95
96 public override int GetHashCode()
97 {
98 if (_changed)
99 {
100 _changed = false;
101 _hash = 0;
102 int num = Size & -4;
103 int i;
104 for (i = 0; i < num; i += 4)
105 {
107 }
108 if (((uint)Size & 3u) != 0)
109 {
110 int num2 = 0;
111 int num3 = 0;
112 for (; i < Size; i++)
113 {
114 num2 |= Buffer[i] << num3;
115 num3 += 8;
116 }
117 _hash ^= num2;
118 }
119 }
120 return _hash;
121 }
122
123 public override string ToString()
124 {
125 string text = Family.ToString();
126 int num = text.Length + 1 + 10 + 2 + (Size - 2) * 4 + 1;
127 Span<char> span = ((num > 256) ? ((Span<char>)new char[num]) : stackalloc char[256]);
128 Span<char> destination = span;
130 int length = text.Length;
131 destination[length++] = ':';
132 bool flag = Size.TryFormat(destination.Slice(length), out var charsWritten);
133 length += charsWritten;
134 destination[length++] = ':';
135 destination[length++] = '{';
136 byte[] buffer = Buffer;
137 for (int i = 2; i < Size; i++)
138 {
139 if (i > 2)
140 {
141 destination[length++] = ',';
142 }
143 flag = buffer[i].TryFormat(destination.Slice(length), out charsWritten);
144 length += charsWritten;
145 }
146 destination[length++] = '}';
147 return destination.Slice(0, length).ToString();
148 }
149}
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 family, int size)
SocketAddress(IPAddress ipAddress)
static readonly int IPv6AddressSize
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 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