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
82 public override bool Equals(object comparand)
83 {
84 if (!(comparand is SocketAddress socketAddress) || Size != socketAddress.Size)
85 {
86 return false;
87 }
88 for (int i = 0; i < Size; i++)
89 {
90 if (this[i] != socketAddress[i])
91 {
92 return false;
93 }
94 }
95 return true;
96 }
97
98 public override int GetHashCode()
99 {
100 if (_changed)
101 {
102 _changed = false;
103 _hash = 0;
104 int num = Size & -4;
105 int i;
106 for (i = 0; i < num; i += 4)
107 {
109 }
110 if (((uint)Size & 3u) != 0)
111 {
112 int num2 = 0;
113 int num3 = 0;
114 for (; i < Size; i++)
115 {
116 num2 |= Buffer[i] << num3;
117 num3 += 8;
118 }
119 _hash ^= num2;
120 }
121 }
122 return _hash;
123 }
124
125 public override string ToString()
126 {
127 string text = Family.ToString();
128 int num = text.Length + 1 + 10 + 2 + (Size - 2) * 4 + 1;
129 Span<char> span = ((num > 256) ? ((Span<char>)new char[num]) : stackalloc char[256]);
130 Span<char> destination = span;
132 int length = text.Length;
133 destination[length++] = ':';
134 bool flag = Size.TryFormat(destination.Slice(length), out var charsWritten);
135 length += charsWritten;
136 destination[length++] = ':';
137 destination[length++] = '{';
138 byte[] buffer = Buffer;
139 for (int i = 2; i < Size; i++)
140 {
141 if (i > 2)
142 {
143 destination[length++] = ',';
144 }
145 flag = buffer[i].TryFormat(destination.Slice(length), out charsWritten);
146 length += charsWritten;
147 }
148 destination[length++] = '}';
149 return destination.Slice(0, length).ToString();
150 }
151}
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
SocketAddress(IPAddress ipaddress, int port)
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 AddressFamily GetAddressFamily(ReadOnlySpan< byte > buffer)
static int Size
Definition IntPtr.cs:21
void CopyTo(Span< T > destination)
Definition Span.cs:224