Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
NetworkStream.cs
Go to the documentation of this file.
1using System.IO;
5
6namespace System.Net.Sockets;
7
8public class NetworkStream : Stream
9{
10 private readonly Socket _streamSocket;
11
12 private readonly bool _ownsSocket;
13
14 private bool _readable;
15
16 private bool _writeable;
17
18 private int _disposed;
19
20 private int _closeTimeout = -1;
21
22 private int _currentReadTimeout = -1;
23
24 private int _currentWriteTimeout = -1;
25
27
28 protected bool Readable
29 {
30 get
31 {
32 return _readable;
33 }
34 set
35 {
37 }
38 }
39
40 protected bool Writeable
41 {
42 get
43 {
44 return _writeable;
45 }
46 set
47 {
49 }
50 }
51
52 public override bool CanRead => _readable;
53
54 public override bool CanSeek => false;
55
56 public override bool CanWrite => _writeable;
57
58 public override bool CanTimeout => true;
59
60 public override int ReadTimeout
61 {
62 get
63 {
64 int num = (int)_streamSocket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout);
65 if (num == 0)
66 {
67 return -1;
68 }
69 return num;
70 }
71 set
72 {
73 if (value <= 0 && value != -1)
74 {
76 }
77 SetSocketTimeoutOption(SocketShutdown.Receive, value, silent: false);
78 }
79 }
80
81 public override int WriteTimeout
82 {
83 get
84 {
85 int num = (int)_streamSocket.GetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout);
86 if (num == 0)
87 {
88 return -1;
89 }
90 return num;
91 }
92 set
93 {
94 if (value <= 0 && value != -1)
95 {
97 }
98 SetSocketTimeoutOption(SocketShutdown.Send, value, silent: false);
99 }
100 }
101
102 public virtual bool DataAvailable
103 {
104 get
105 {
107 return _streamSocket.Available != 0;
108 }
109 }
110
111 public override long Length
112 {
113 get
114 {
116 }
117 }
118
119 public override long Position
120 {
121 get
122 {
124 }
125 set
126 {
128 }
129 }
130
131 public NetworkStream(Socket socket)
132 : this(socket, FileAccess.ReadWrite, ownsSocket: false)
133 {
134 }
135
136 public NetworkStream(Socket socket, bool ownsSocket)
137 : this(socket, FileAccess.ReadWrite, ownsSocket)
138 {
139 }
140
141 public NetworkStream(Socket socket, FileAccess access)
142 : this(socket, access, ownsSocket: false)
143 {
144 }
145
146 public NetworkStream(Socket socket, FileAccess access, bool ownsSocket)
147 {
148 if (socket == null)
149 {
150 throw new ArgumentNullException("socket");
151 }
152 if (!socket.Blocking)
153 {
155 }
156 if (!socket.Connected)
157 {
159 }
160 if (socket.SocketType != SocketType.Stream)
161 {
163 }
164 _streamSocket = socket;
165 _ownsSocket = ownsSocket;
166 switch (access)
167 {
168 case FileAccess.Read:
169 _readable = true;
170 break;
171 case FileAccess.Write:
172 _writeable = true;
173 break;
174 default:
175 _readable = true;
176 _writeable = true;
177 break;
178 }
179 }
180
181 public override long Seek(long offset, SeekOrigin origin)
182 {
184 }
185
186 public override int Read(byte[] buffer, int offset, int count)
187 {
190 if (!CanRead)
191 {
193 }
194 try
195 {
197 }
198 catch (Exception ex) when (!(ex is OutOfMemoryException))
199 {
201 }
202 }
203
204 public override int Read(Span<byte> buffer)
205 {
206 if (GetType() != typeof(NetworkStream))
207 {
208 return base.Read(buffer);
209 }
211 if (!CanRead)
212 {
214 }
215 try
216 {
218 }
219 catch (Exception ex) when (!(ex is OutOfMemoryException))
220 {
222 }
223 }
224
225 public unsafe override int ReadByte()
226 {
227 Unsafe.SkipInit(out byte result);
228 if (Read(new Span<byte>(&result, 1)) != 0)
229 {
230 return result;
231 }
232 return -1;
233 }
234
235 public override void Write(byte[] buffer, int offset, int count)
236 {
239 if (!CanWrite)
240 {
242 }
243 try
244 {
246 }
247 catch (Exception ex) when (!(ex is OutOfMemoryException))
248 {
250 }
251 }
252
253 public override void Write(ReadOnlySpan<byte> buffer)
254 {
255 if (GetType() != typeof(NetworkStream))
256 {
257 base.Write(buffer);
258 return;
259 }
261 if (!CanWrite)
262 {
264 }
265 try
266 {
268 }
269 catch (Exception ex) when (!(ex is OutOfMemoryException))
270 {
272 }
273 }
274
275 public unsafe override void WriteByte(byte value)
276 {
278 }
279
280 public void Close(int timeout)
281 {
282 if (timeout < -1)
283 {
284 throw new ArgumentOutOfRangeException("timeout");
285 }
287 Dispose();
288 }
289
290 protected override void Dispose(bool disposing)
291 {
292 if (Interlocked.Exchange(ref _disposed, 1) != 0)
293 {
294 return;
295 }
296 if (disposing)
297 {
298 _readable = false;
299 _writeable = false;
300 if (_ownsSocket)
301 {
304 }
305 }
306 base.Dispose(disposing);
307 }
308
310 {
311 Dispose(disposing: false);
312 }
313
314 public override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
315 {
318 if (!CanRead)
319 {
321 }
322 try
323 {
324 return _streamSocket.BeginReceive(buffer, offset, count, SocketFlags.None, callback, state);
325 }
326 catch (Exception ex) when (!(ex is OutOfMemoryException))
327 {
329 }
330 }
331
332 public override int EndRead(IAsyncResult asyncResult)
333 {
335 if (asyncResult == null)
336 {
337 throw new ArgumentNullException("asyncResult");
338 }
339 try
340 {
342 }
343 catch (Exception ex) when (!(ex is OutOfMemoryException))
344 {
346 }
347 }
348
349 public override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
350 {
353 if (!CanWrite)
354 {
356 }
357 try
358 {
359 return _streamSocket.BeginSend(buffer, offset, count, SocketFlags.None, callback, state);
360 }
361 catch (Exception ex) when (!(ex is OutOfMemoryException))
362 {
364 }
365 }
366
367 public override void EndWrite(IAsyncResult asyncResult)
368 {
370 if (asyncResult == null)
371 {
372 throw new ArgumentNullException("asyncResult");
373 }
374 try
375 {
377 }
378 catch (Exception ex) when (!(ex is OutOfMemoryException))
379 {
381 }
382 }
383
385 {
388 if (!CanRead)
389 {
391 }
392 try
393 {
394 return _streamSocket.ReceiveAsync(new Memory<byte>(buffer, offset, count), SocketFlags.None, fromNetworkStream: true, cancellationToken).AsTask();
395 }
396 catch (Exception ex) when (!(ex is OutOfMemoryException))
397 {
399 }
400 }
401
403 {
404 bool canRead = CanRead;
406 if (!canRead)
407 {
409 }
410 try
411 {
412 return _streamSocket.ReceiveAsync(buffer, SocketFlags.None, fromNetworkStream: true, cancellationToken);
413 }
414 catch (Exception ex) when (!(ex is OutOfMemoryException))
415 {
417 }
418 }
419
437
439 {
440 bool canWrite = CanWrite;
442 if (!canWrite)
443 {
445 }
446 try
447 {
449 }
450 catch (Exception ex) when (!(ex is OutOfMemoryException))
451 {
453 }
454 }
455
456 public override void Flush()
457 {
458 }
459
461 {
462 return Task.CompletedTask;
463 }
464
465 public override void SetLength(long value)
466 {
468 }
469
470 internal void SetSocketTimeoutOption(SocketShutdown mode, int timeout, bool silent)
471 {
472 if (timeout < 0)
473 {
474 timeout = 0;
475 }
476 if ((mode == SocketShutdown.Send || mode == SocketShutdown.Both) && timeout != _currentWriteTimeout)
477 {
480 }
481 if ((mode == SocketShutdown.Receive || mode == SocketShutdown.Both) && timeout != _currentReadTimeout)
482 {
485 }
486 }
487
488 private void ThrowIfDisposed()
489 {
490 if (_disposed != 0)
491 {
492 ThrowObjectDisposedException();
493 }
494 void ThrowObjectDisposedException()
495 {
496 throw new ObjectDisposedException(GetType().FullName);
497 }
498 }
499
500 private static IOException WrapException(string resourceFormatString, Exception innerException)
501 {
502 return new IOException(System.SR.Format(resourceFormatString, innerException.Message), innerException);
503 }
504}
virtual string Message
Definition Exception.cs:100
static void ValidateBufferArguments(byte[] buffer, int offset, int count)
Definition Stream.cs:1044
void Dispose()
Definition Stream.cs:639
override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
void SetSocketTimeoutOption(SocketShutdown mode, int timeout, bool silent)
override int Read(Span< byte > buffer)
override void Write(byte[] buffer, int offset, int count)
NetworkStream(Socket socket, FileAccess access)
override ValueTask< int > ReadAsync(Memory< byte > buffer, CancellationToken cancellationToken)
override int EndRead(IAsyncResult asyncResult)
override Task< int > ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
override ValueTask WriteAsync(ReadOnlyMemory< byte > buffer, CancellationToken cancellationToken)
unsafe override int ReadByte()
unsafe override void WriteByte(byte value)
override void Write(ReadOnlySpan< byte > buffer)
override void SetLength(long value)
NetworkStream(Socket socket, bool ownsSocket)
override IAsyncResult BeginWrite(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
override long Seek(long offset, SeekOrigin origin)
override IAsyncResult BeginRead(byte[] buffer, int offset, int count, AsyncCallback? callback, object? state)
override void Dispose(bool disposing)
override void EndWrite(IAsyncResult asyncResult)
static IOException WrapException(string resourceFormatString, Exception innerException)
NetworkStream(Socket socket, FileAccess access, bool ownsSocket)
override int Read(byte[] buffer, int offset, int count)
override Task FlushAsync(CancellationToken cancellationToken)
int Send(byte[] buffer, int size, SocketFlags socketFlags)
Definition Socket.cs:1445
int EndReceive(IAsyncResult asyncResult)
Definition Socket.cs:2571
IAsyncResult BeginReceive(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback? callback, object? state)
Definition Socket.cs:2531
int EndSend(IAsyncResult asyncResult)
Definition Socket.cs:2473
int Receive(byte[] buffer, int size, SocketFlags socketFlags)
Definition Socket.cs:1720
bool ReceiveAsync(SocketAsyncEventArgs e)
Definition Socket.cs:3001
object? GetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName)
Definition Socket.cs:2235
ValueTask SendAsyncForNetworkStream(ReadOnlyMemory< byte > buffer, SocketFlags socketFlags, CancellationToken cancellationToken)
Definition Socket.cs:4145
void InternalShutdown(SocketShutdown how)
Definition Socket.cs:3435
IAsyncResult BeginSend(byte[] buffer, int offset, int size, SocketFlags socketFlags, AsyncCallback? callback, object? state)
Definition Socket.cs:2433
void SetSocketOption(SocketOptionLevel optionLevel, SocketOptionName optionName, int optionValue)
Definition Socket.cs:2143
static string net_sockets_blocking
Definition SR.cs:50
static string net_readonlystream
Definition SR.cs:152
static string net_io_readfailure
Definition SR.cs:158
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string net_io_timeout_use_gt_zero
Definition SR.cs:80
static string net_notconnected
Definition SR.cs:24
static string net_writeonlystream
Definition SR.cs:116
static string net_io_writefailure
Definition SR.cs:92
static string net_notstream
Definition SR.cs:26
static string net_noseek
Definition SR.cs:114
Definition SR.cs:7
static int Exchange(ref int location1, int value)
static Task CompletedTask
Definition Task.cs:1120