Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
TcpClient.cs
Go to the documentation of this file.
5
6namespace System.Net.Sockets;
7
8public class TcpClient : IDisposable
9{
11
13
15
16 private volatile int _disposed;
17
18 private bool _active;
19
20 private bool Disposed => _disposed != 0;
21
22 protected bool Active
23 {
24 get
25 {
26 return _active;
27 }
28 set
29 {
30 _active = value;
31 }
32 }
33
34 public int Available => Client?.Available ?? 0;
35
36 public Socket Client
37 {
38 get
39 {
40 if (!Disposed)
41 {
42 return _clientSocket;
43 }
44 return null;
45 }
46 set
47 {
50 }
51 }
52
53 public bool Connected => Client?.Connected ?? false;
54
56 {
57 get
58 {
59 return Client?.ExclusiveAddressUse ?? false;
60 }
61 set
62 {
63 if (_clientSocket != null)
64 {
65 _clientSocket.ExclusiveAddressUse = value;
66 }
67 }
68 }
69
71 {
72 get
73 {
74 return (int)Client.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer);
75 }
76 set
77 {
78 Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, value);
79 }
80 }
81
82 public int SendBufferSize
83 {
84 get
85 {
86 return (int)Client.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer);
87 }
88 set
89 {
90 Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, value);
91 }
92 }
93
94 public int ReceiveTimeout
95 {
96 get
97 {
98 return (int)Client.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout);
99 }
100 set
101 {
102 Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, value);
103 }
104 }
105
106 public int SendTimeout
107 {
108 get
109 {
110 return (int)Client.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout);
111 }
112 set
113 {
114 Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, value);
115 }
116 }
117
119 {
120 get
121 {
122 return Client.LingerState;
123 }
124 [param: DisallowNull]
125 set
126 {
127 Client.LingerState = value;
128 }
129 }
130
131 public bool NoDelay
132 {
133 get
134 {
135 return Client.NoDelay;
136 }
137 set
138 {
139 Client.NoDelay = value;
140 }
141 }
142
143 public TcpClient()
144 : this(AddressFamily.Unknown)
145 {
146 }
147
149 {
150 if (System.Net.NetEventSource.Log.IsEnabled())
151 {
152 System.Net.NetEventSource.Info(this, family, ".ctor");
153 }
154 if (family != AddressFamily.InterNetwork && family != AddressFamily.InterNetworkV6 && family != AddressFamily.Unknown)
155 {
157 }
158 _family = family;
160 }
161
162 public TcpClient(IPEndPoint localEP)
163 {
164 if (System.Net.NetEventSource.Log.IsEnabled())
165 {
166 System.Net.NetEventSource.Info(this, localEP, ".ctor");
167 }
168 if (localEP == null)
169 {
170 throw new ArgumentNullException("localEP");
171 }
172 _family = localEP.AddressFamily;
174 _clientSocket.Bind(localEP);
175 }
176
177 public TcpClient(string hostname, int port)
178 {
179 if (System.Net.NetEventSource.Log.IsEnabled())
180 {
181 System.Net.NetEventSource.Info(this, hostname, ".ctor");
182 }
183 if (hostname == null)
184 {
185 throw new ArgumentNullException("hostname");
186 }
188 {
189 throw new ArgumentOutOfRangeException("port");
190 }
191 _family = AddressFamily.Unknown;
192 try
193 {
194 Connect(hostname, port);
195 }
196 catch
197 {
199 throw;
200 }
201 }
202
203 internal TcpClient(Socket acceptedSocket)
204 {
205 _clientSocket = acceptedSocket;
206 _active = true;
207 }
208
209 public void Connect(string hostname, int port)
210 {
212 if (hostname == null)
213 {
214 throw new ArgumentNullException("hostname");
215 }
217 {
218 throw new ArgumentOutOfRangeException("port");
219 }
220 if (_active)
221 {
222 throw new SocketException(10056);
223 }
224 IPAddress[] hostAddresses = Dns.GetHostAddresses(hostname);
225 ExceptionDispatchInfo exceptionDispatchInfo = null;
226 try
227 {
228 IPAddress[] array = hostAddresses;
229 foreach (IPAddress iPAddress in array)
230 {
231 try
232 {
233 if (_clientSocket == null)
234 {
235 if ((iPAddress.AddressFamily == AddressFamily.InterNetwork && Socket.OSSupportsIPv4) || Socket.OSSupportsIPv6)
236 {
237 Socket socket = new Socket(iPAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
238 if (iPAddress.IsIPv4MappedToIPv6)
239 {
240 socket.DualMode = true;
241 }
243 if (Disposed)
244 {
245 socket.Dispose();
246 }
247 try
248 {
249 socket.Connect(iPAddress, port);
250 }
251 catch
252 {
253 _clientSocket = null;
254 throw;
255 }
256 }
257 _family = iPAddress.AddressFamily;
258 _active = true;
259 break;
260 }
261 if (iPAddress.AddressFamily == _family || _family == AddressFamily.Unknown)
262 {
263 Connect(new IPEndPoint(iPAddress, port));
264 _active = true;
265 break;
266 }
267 }
268 catch (Exception ex) when (!(ex is OutOfMemoryException))
269 {
270 exceptionDispatchInfo = ExceptionDispatchInfo.Capture(ex);
271 }
272 }
273 }
274 finally
275 {
276 if (!_active)
277 {
278 exceptionDispatchInfo?.Throw();
279 throw new SocketException(10057);
280 }
281 }
282 }
283
284 public void Connect(IPAddress address, int port)
285 {
287 if (address == null)
288 {
289 throw new ArgumentNullException("address");
290 }
292 {
293 throw new ArgumentOutOfRangeException("port");
294 }
295 IPEndPoint remoteEP = new IPEndPoint(address, port);
296 Connect(remoteEP);
297 }
298
299 public void Connect(IPEndPoint remoteEP)
300 {
302 if (remoteEP == null)
303 {
304 throw new ArgumentNullException("remoteEP");
305 }
306 Client.Connect(remoteEP);
307 _family = Client.AddressFamily;
308 _active = true;
309 }
310
311 public void Connect(IPAddress[] ipAddresses, int port)
312 {
313 Client.Connect(ipAddresses, port);
314 _family = Client.AddressFamily;
315 _active = true;
316 }
317
318 public Task ConnectAsync(IPAddress address, int port)
319 {
320 return CompleteConnectAsync(Client.ConnectAsync(address, port));
321 }
322
323 public Task ConnectAsync(string host, int port)
324 {
325 return CompleteConnectAsync(Client.ConnectAsync(host, port));
326 }
327
328 public Task ConnectAsync(IPAddress[] addresses, int port)
329 {
330 return CompleteConnectAsync(Client.ConnectAsync(addresses, port));
331 }
332
333 public Task ConnectAsync(IPEndPoint remoteEP)
334 {
335 return CompleteConnectAsync(Client.ConnectAsync(remoteEP));
336 }
337
339 {
340 await task.ConfigureAwait(continueOnCapturedContext: false);
341 _active = true;
342 }
343
345 {
346 return CompleteConnectAsync(Client.ConnectAsync(address, port, cancellationToken));
347 }
348
350 {
351 return CompleteConnectAsync(Client.ConnectAsync(host, port, cancellationToken));
352 }
353
355 {
356 return CompleteConnectAsync(Client.ConnectAsync(addresses, port, cancellationToken));
357 }
358
360 {
361 return CompleteConnectAsync(Client.ConnectAsync(remoteEP, cancellationToken));
362 }
363
365 {
366 await task.ConfigureAwait(continueOnCapturedContext: false);
367 _active = true;
368 }
369
370 public IAsyncResult BeginConnect(IPAddress address, int port, AsyncCallback? requestCallback, object? state)
371 {
372 return Client.BeginConnect(address, port, requestCallback, state);
373 }
374
375 public IAsyncResult BeginConnect(string host, int port, AsyncCallback? requestCallback, object? state)
376 {
377 return Client.BeginConnect(host, port, requestCallback, state);
378 }
379
380 public IAsyncResult BeginConnect(IPAddress[] addresses, int port, AsyncCallback? requestCallback, object? state)
381 {
382 return Client.BeginConnect(addresses, port, requestCallback, state);
383 }
384
390
392 {
394 if (!Connected)
395 {
397 }
398 if (_dataStream == null)
399 {
400 _dataStream = new NetworkStream(Client, ownsSocket: true);
401 }
402 return _dataStream;
403 }
404
405 public void Close()
406 {
407 Dispose();
408 }
409
410 protected virtual void Dispose(bool disposing)
411 {
412 if (Interlocked.CompareExchange(ref _disposed, 1, 0) != 0 || !disposing)
413 {
414 return;
415 }
416 IDisposable dataStream = _dataStream;
417 if (dataStream != null)
418 {
419 dataStream.Dispose();
420 }
421 else
422 {
423 Socket socket = Volatile.Read(ref _clientSocket);
424 if (socket != null)
425 {
426 try
427 {
428 socket.InternalShutdown(SocketShutdown.Both);
429 }
430 finally
431 {
432 socket.Close();
433 }
434 }
435 }
436 GC.SuppressFinalize(this);
437 }
438
439 public void Dispose()
440 {
441 Dispose(disposing: true);
442 }
443
445 {
446 Dispose(disposing: false);
447 }
448
450 {
451 if (_family == AddressFamily.Unknown)
452 {
453 _clientSocket = new Socket(SocketType.Stream, ProtocolType.Tcp);
454 if (_clientSocket.AddressFamily == AddressFamily.InterNetwork)
455 {
456 _family = AddressFamily.InterNetwork;
457 }
458 }
459 else
460 {
462 }
463 }
464
465 private void ThrowIfDisposed()
466 {
467 if (Disposed)
468 {
469 ThrowObjectDisposedException();
470 }
471 void ThrowObjectDisposedException()
472 {
473 throw new ObjectDisposedException(GetType().FullName);
474 }
475 }
476}
static void SuppressFinalize(object obj)
Definition GC.cs:202
Definition GC.cs:8
static IPAddress[] GetHostAddresses(string hostNameOrAddress)
Definition Dns.cs:170
AddressFamily AddressFamily
Definition IPAddress.cs:88
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
AddressFamily AddressFamily
Definition Socket.cs:748
virtual void Dispose(bool disposing)
Definition Socket.cs:3294
static bool OSSupportsIPv4
Definition Socket.cs:589
void Bind(EndPoint localEP)
Definition Socket.cs:1172
void Connect(EndPoint remoteEP)
Definition Socket.cs:1206
void InternalShutdown(SocketShutdown how)
Definition Socket.cs:3435
void EndConnect(IAsyncResult asyncResult)
Definition Socket.cs:2398
IAsyncResult BeginConnect(string host, int port, AsyncCallback? requestCallback, object? state)
Definition TcpClient.cs:375
async ValueTask CompleteConnectAsync(ValueTask task)
Definition TcpClient.cs:364
IAsyncResult BeginConnect(IPAddress address, int port, AsyncCallback? requestCallback, object? state)
Definition TcpClient.cs:370
ValueTask ConnectAsync(IPAddress[] addresses, int port, CancellationToken cancellationToken)
Definition TcpClient.cs:354
ValueTask ConnectAsync(IPAddress address, int port, CancellationToken cancellationToken)
Definition TcpClient.cs:344
async Task CompleteConnectAsync(Task task)
Definition TcpClient.cs:338
TcpClient(IPEndPoint localEP)
Definition TcpClient.cs:162
virtual void Dispose(bool disposing)
Definition TcpClient.cs:410
NetworkStream GetStream()
Definition TcpClient.cs:391
Task ConnectAsync(IPAddress[] addresses, int port)
Definition TcpClient.cs:328
ValueTask ConnectAsync(string host, int port, CancellationToken cancellationToken)
Definition TcpClient.cs:349
void Connect(IPEndPoint remoteEP)
Definition TcpClient.cs:299
TcpClient(string hostname, int port)
Definition TcpClient.cs:177
void Connect(IPAddress[] ipAddresses, int port)
Definition TcpClient.cs:311
void EndConnect(IAsyncResult asyncResult)
Definition TcpClient.cs:385
TcpClient(Socket acceptedSocket)
Definition TcpClient.cs:203
Task ConnectAsync(IPAddress address, int port)
Definition TcpClient.cs:318
Task ConnectAsync(IPEndPoint remoteEP)
Definition TcpClient.cs:333
Task ConnectAsync(string host, int port)
Definition TcpClient.cs:323
void Connect(IPAddress address, int port)
Definition TcpClient.cs:284
void Connect(string hostname, int port)
Definition TcpClient.cs:209
IAsyncResult BeginConnect(IPAddress[] addresses, int port, AsyncCallback? requestCallback, object? state)
Definition TcpClient.cs:380
ValueTask ConnectAsync(IPEndPoint remoteEP, CancellationToken cancellationToken)
Definition TcpClient.cs:359
TcpClient(AddressFamily family)
Definition TcpClient.cs:148
static bool ValidatePortNumber(int port)
static ExceptionDispatchInfo Capture(Exception source)
static string net_protocol_invalid_family
Definition SR.cs:44
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string net_notconnected
Definition SR.cs:24
Definition SR.cs:7
static int CompareExchange(ref int location1, int value, int comparand)
static int Exchange(ref int location1, int value)
new ConfiguredTaskAwaitable< TResult > ConfigureAwait(bool continueOnCapturedContext)
Definition Task.cs:226
static bool Read(ref bool location)
Definition Volatile.cs:67
ConfiguredValueTaskAwaitable ConfigureAwait(bool continueOnCapturedContext)
Definition ValueTask.cs:312