Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
UnixDomainSocketEndPoint.cs
Go to the documentation of this file.
1using System.IO;
2using System.Text;
3
4namespace System.Net.Sockets;
5
6public sealed class UnixDomainSocketEndPoint : EndPoint
7{
8 private readonly string _path;
9
10 private readonly byte[] _encodedPath;
11
12 private static readonly int s_nativePathOffset = 2;
13
14 private static readonly int s_nativePathLength = 108;
15
17
18 internal string? BoundFileName { get; }
19
20 internal static int MaxAddressSize => s_nativeAddressSize;
21
22 public override AddressFamily AddressFamily => AddressFamily.Unix;
23
24 public UnixDomainSocketEndPoint(string path)
25 : this(path, null)
26 {
27 }
28
29 private UnixDomainSocketEndPoint(string path, string boundFileName)
30 {
31 if (path == null)
32 {
33 throw new ArgumentNullException("path");
34 }
35 BoundFileName = boundFileName;
36 bool flag = IsAbstract(path);
37 int num = Encoding.UTF8.GetByteCount(path);
38 if (!flag)
39 {
40 num++;
41 }
42 if (path.Length == 0 || num > s_nativePathLength)
43 {
45 }
46 _path = path;
47 _encodedPath = new byte[num];
48 int bytes = Encoding.UTF8.GetBytes(path, 0, path.Length, _encodedPath, 0);
50 {
52 }
53 }
54
56 {
57 if (socketAddress == null)
58 {
59 throw new ArgumentNullException("socketAddress");
60 }
61 if (socketAddress.Family != AddressFamily.Unix || socketAddress.Size > s_nativeAddressSize)
62 {
63 throw new ArgumentOutOfRangeException("socketAddress");
64 }
65 if (socketAddress.Size > s_nativePathOffset)
66 {
67 _encodedPath = new byte[socketAddress.Size - s_nativePathOffset];
68 for (int i = 0; i < _encodedPath.Length; i++)
69 {
70 _encodedPath[i] = socketAddress[s_nativePathOffset + i];
71 }
72 int num = _encodedPath.Length;
74 {
75 while (_encodedPath[num - 1] == 0)
76 {
77 num--;
78 }
79 }
80 _path = Encoding.UTF8.GetString(_encodedPath, 0, num);
81 }
82 else
83 {
84 _encodedPath = Array.Empty<byte>();
85 _path = string.Empty;
86 }
87 }
88
89 public override SocketAddress Serialize()
90 {
92 for (int i = 0; i < _encodedPath.Length; i++)
93 {
94 socketAddress[s_nativePathOffset + i] = _encodedPath[i];
95 }
96 return socketAddress;
97 }
98
99 public override EndPoint Create(SocketAddress socketAddress)
100 {
101 return new UnixDomainSocketEndPoint(socketAddress);
102 }
103
104 public override string ToString()
105 {
106 if (IsAbstract(_path))
107 {
108 return "@" + _path.AsSpan(1);
109 }
110 return _path;
111 }
112
114 {
115 if (IsAbstract(_path))
116 {
117 return this;
118 }
120 }
121
123 {
124 if (IsAbstract(_path) || BoundFileName == null)
125 {
126 return this;
127 }
128 return new UnixDomainSocketEndPoint(_path, null);
129 }
130
131 private static bool IsAbstract(string path)
132 {
133 if (path.Length > 0)
134 {
135 return path[0] == '\0';
136 }
137 return false;
138 }
139
140 private static bool IsAbstract(byte[] encodedPath)
141 {
142 if (encodedPath.Length != 0)
143 {
144 return encodedPath[0] == 0;
145 }
146 return false;
147 }
148
153}
static string GetFullPath(string path)
Definition Path.cs:881
static bool OSSupportsUnixDomainSockets
Definition Socket.cs:593
UnixDomainSocketEndPoint(string path, string boundFileName)
override EndPoint Create(SocketAddress socketAddress)
static string ArgumentOutOfRange_PathLengthInvalid
Definition SR.cs:100
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
Definition SR.cs:7
static Encoding UTF8
Definition Encoding.cs:526