Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
TcpListener.cs
Go to the documentation of this file.
4
5namespace System.Net.Sockets;
6
7public class TcpListener
8{
9 private readonly IPEndPoint _serverSocketEP;
10
12
13 private bool _active;
14
16
17 private bool? _allowNatTraversal;
18
19 public Socket Server
20 {
21 get
22 {
24 return _serverSocket;
25 }
26 }
27
28 protected bool Active => _active;
29
31 {
32 get
33 {
34 if (!_active)
35 {
36 return _serverSocketEP;
37 }
39 }
40 }
41
43 {
44 get
45 {
46 if (_serverSocket == null)
47 {
49 }
51 }
52 set
53 {
54 if (_active)
55 {
57 }
58 if (_serverSocket != null)
59 {
60 _serverSocket.ExclusiveAddressUse = value;
61 }
63 }
64 }
65
66 public TcpListener(IPEndPoint localEP)
67 {
68 if (System.Net.NetEventSource.Log.IsEnabled())
69 {
70 System.Net.NetEventSource.Info(this, localEP, ".ctor");
71 }
72 if (localEP == null)
73 {
74 throw new ArgumentNullException("localEP");
75 }
76 _serverSocketEP = localEP;
78 }
79
80 public TcpListener(IPAddress localaddr, int port)
81 {
82 if (System.Net.NetEventSource.Log.IsEnabled())
83 {
84 System.Net.NetEventSource.Info(this, localaddr, ".ctor");
85 }
86 if (localaddr == null)
87 {
88 throw new ArgumentNullException("localaddr");
89 }
91 {
92 throw new ArgumentOutOfRangeException("port");
93 }
94 _serverSocketEP = new IPEndPoint(localaddr, port);
96 }
97
98 [Obsolete("This constructor has been deprecated. Use TcpListener(IPAddress localaddr, int port) instead.")]
99 public TcpListener(int port)
100 {
102 {
103 throw new ArgumentOutOfRangeException("port");
104 }
107 }
108
109 [SupportedOSPlatform("windows")]
110 public void AllowNatTraversal(bool allowed)
111 {
112 if (_active)
113 {
115 }
116 if (_serverSocket != null)
117 {
118 SetIPProtectionLevel(allowed);
119 }
120 else
121 {
122 _allowNatTraversal = allowed;
123 }
124 }
125
126 public void Start()
127 {
128 Start(int.MaxValue);
129 }
130
131 public void Start(int backlog)
132 {
133 if (backlog > int.MaxValue || backlog < 0)
134 {
135 throw new ArgumentOutOfRangeException("backlog");
136 }
137 if (!_active)
138 {
141 try
142 {
143 _serverSocket.Listen(backlog);
144 }
145 catch (SocketException)
146 {
147 Stop();
148 throw;
149 }
150 _active = true;
151 }
152 }
153
154 public void Stop()
155 {
157 _active = false;
158 _serverSocket = null;
159 }
160
161 public bool Pending()
162 {
163 if (!_active)
164 {
166 }
167 return _serverSocket.Poll(0, SelectMode.SelectRead);
168 }
169
171 {
172 if (!_active)
173 {
175 }
176 return _serverSocket.Accept();
177 }
178
180 {
181 if (!_active)
182 {
184 }
185 Socket acceptedSocket = _serverSocket.Accept();
186 return new TcpClient(acceptedSocket);
187 }
188
189 public IAsyncResult BeginAcceptSocket(AsyncCallback? callback, object? state)
190 {
192 }
193
195 {
196 return EndAcceptCore<Socket>(asyncResult);
197 }
198
199 public IAsyncResult BeginAcceptTcpClient(AsyncCallback? callback, object? state)
200 {
202 }
203
205 {
206 return EndAcceptCore<TcpClient>(asyncResult);
207 }
208
210 {
212 }
213
222
227
229 {
230 return WaitAndWrap(AcceptSocketAsync(cancellationToken));
231 static async ValueTask<TcpClient> WaitAndWrap(ValueTask<Socket> task)
232 {
233 return new TcpClient(await task.ConfigureAwait(continueOnCapturedContext: false));
234 }
235 }
236
237 public static TcpListener Create(int port)
238 {
240 {
241 throw new ArgumentOutOfRangeException("port");
242 }
243 TcpListener tcpListener;
245 {
246 tcpListener = new TcpListener(IPAddress.IPv6Any, port);
247 tcpListener.Server.DualMode = true;
248 }
249 else
250 {
251 tcpListener = new TcpListener(IPAddress.Any, port);
252 }
253 return tcpListener;
254 }
255
256 [SupportedOSPlatform("windows")]
257 private void SetIPProtectionLevel(bool allowed)
258 {
259 _serverSocket.SetIPProtectionLevel(allowed ? IPProtectionLevel.Unrestricted : IPProtectionLevel.EdgeRestricted);
260 }
261
263 {
264 if (_serverSocket == null)
265 {
267 }
269 {
270 _serverSocket.ExclusiveAddressUse = true;
271 }
272 if (_allowNatTraversal.HasValue)
273 {
274 SetIPProtectionLevel(_allowNatTraversal.GetValueOrDefault());
275 _allowNatTraversal = null;
276 }
277 }
278
280 {
281 try
282 {
284 }
285 catch (SocketException) when (!_active)
286 {
287 throw new ObjectDisposedException(typeof(Socket).FullName);
288 }
289 }
290}
static readonly IPAddress Any
Definition IPAddress.cs:19
static readonly IPAddress IPv6Any
Definition IPAddress.cs:27
override AddressFamily AddressFamily
Definition IPEndPoint.cs:17
static readonly System.Net.NetEventSource Log
static void Info(object thisOrContextObject, FormattableString formattableString=null, [CallerMemberName] string memberName=null)
static bool OSSupportsIPv6
Definition Socket.cs:591
virtual void Dispose(bool disposing)
Definition Socket.cs:3294
unsafe? EndPoint LocalEndPoint
Definition Socket.cs:615
void Bind(EndPoint localEP)
Definition Socket.cs:1172
void SetIPProtectionLevel(IPProtectionLevel level)
Definition Socket.cs:2318
bool Poll(int microSeconds, SelectMode mode)
Definition Socket.cs:2337
bool AcceptAsync(SocketAsyncEventArgs e)
Definition Socket.cs:2774
static TcpListener Create(int port)
TResult EndAcceptCore< TResult >(IAsyncResult asyncResult)
Task< Socket > AcceptSocketAsync()
IAsyncResult BeginAcceptTcpClient(AsyncCallback? callback, object? state)
readonly IPEndPoint _serverSocketEP
Definition TcpListener.cs:9
ValueTask< Socket > AcceptSocketAsync(CancellationToken cancellationToken)
void SetIPProtectionLevel(bool allowed)
TcpListener(IPAddress localaddr, int port)
TcpListener(IPEndPoint localEP)
void AllowNatTraversal(bool allowed)
Task< TcpClient > AcceptTcpClientAsync()
Socket EndAcceptSocket(IAsyncResult asyncResult)
TcpClient EndAcceptTcpClient(IAsyncResult asyncResult)
ValueTask< TcpClient > AcceptTcpClientAsync(CancellationToken cancellationToken)
IAsyncResult BeginAcceptSocket(AsyncCallback? callback, object? state)
static bool ValidatePortNumber(int port)
static string net_tcplistener_mustbestopped
Definition SR.cs:82
static string net_stopped
Definition SR.cs:28
Definition SR.cs:7
static IAsyncResult Begin(Task task, AsyncCallback callback, object state)
Definition TaskToApm.cs:43
static void End(IAsyncResult asyncResult)
Definition TaskToApm.cs:48