Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
WebSocket.cs
Go to the documentation of this file.
3using System.IO;
7
9
10public abstract class WebSocket : IDisposable
11{
12 public abstract WebSocketCloseStatus? CloseStatus { get; }
13
14 public abstract string? CloseStatusDescription { get; }
15
16 public abstract string? SubProtocol { get; }
17
18 public abstract WebSocketState State { get; }
19
21
22 public abstract void Abort();
23
24 public abstract Task CloseAsync(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken);
25
26 public abstract Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken);
27
28 public abstract void Dispose();
29
31
33
35 {
36 if (MemoryMarshal.TryGetArray((ReadOnlyMemory<byte>)buffer, out ArraySegment<byte> segment))
37 {
38 WebSocketReceiveResult webSocketReceiveResult = await ReceiveAsync(segment, cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
39 return new ValueWebSocketReceiveResult(webSocketReceiveResult.Count, webSocketReceiveResult.MessageType, webSocketReceiveResult.EndOfMessage);
40 }
41 byte[] array = ArrayPool<byte>.Shared.Rent(buffer.Length);
42 try
43 {
44 WebSocketReceiveResult webSocketReceiveResult2 = await ReceiveAsync(new ArraySegment<byte>(array, 0, buffer.Length), cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
45 new Span<byte>(array, 0, webSocketReceiveResult2.Count).CopyTo(buffer.Span);
46 return new ValueWebSocketReceiveResult(webSocketReceiveResult2.Count, webSocketReceiveResult2.MessageType, webSocketReceiveResult2.EndOfMessage);
47 }
48 finally
49 {
51 }
52 }
53
55 {
56 if (!MemoryMarshal.TryGetArray(buffer, out var segment))
57 {
58 return SendWithArrayPoolAsync(buffer, messageType, endOfMessage, cancellationToken);
59 }
60 return new ValueTask(SendAsync(segment, messageType, endOfMessage, cancellationToken));
61 }
62
64 {
65 return SendAsync(buffer, messageType, messageFlags.HasFlag(WebSocketMessageFlags.EndOfMessage), cancellationToken);
66 }
67
69 {
70 byte[] array = ArrayPool<byte>.Shared.Rent(buffer.Length);
71 try
72 {
73 buffer.Span.CopyTo(array);
74 await SendAsync(new ArraySegment<byte>(array, 0, buffer.Length), messageType, endOfMessage, cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
75 }
76 finally
77 {
79 }
80 }
81
82 protected static void ThrowOnInvalidState(WebSocketState state, params WebSocketState[] validStates)
83 {
84 string p = string.Empty;
85 if (validStates != null && validStates.Length != 0)
86 {
87 foreach (WebSocketState webSocketState in validStates)
88 {
89 if (state == webSocketState)
90 {
91 return;
92 }
93 }
94 p = string.Join(", ", validStates);
95 }
97 }
98
99 protected static bool IsStateTerminal(WebSocketState state)
100 {
101 if (state != WebSocketState.Closed)
102 {
103 return state == WebSocketState.Aborted;
104 }
105 return true;
106 }
107
108 public static ArraySegment<byte> CreateClientBuffer(int receiveBufferSize, int sendBufferSize)
109 {
110 if (receiveBufferSize <= 0)
111 {
112 throw new ArgumentOutOfRangeException("receiveBufferSize", receiveBufferSize, System.SR.Format(System.SR.net_WebSockets_ArgumentOutOfRange_TooSmall, 1));
113 }
114 if (sendBufferSize <= 0)
115 {
116 throw new ArgumentOutOfRangeException("sendBufferSize", sendBufferSize, System.SR.Format(System.SR.net_WebSockets_ArgumentOutOfRange_TooSmall, 1));
117 }
118 return new ArraySegment<byte>(new byte[Math.Max(receiveBufferSize, sendBufferSize)]);
119 }
120
121 public static ArraySegment<byte> CreateServerBuffer(int receiveBufferSize)
122 {
123 if (receiveBufferSize <= 0)
124 {
125 throw new ArgumentOutOfRangeException("receiveBufferSize", receiveBufferSize, System.SR.Format(System.SR.net_WebSockets_ArgumentOutOfRange_TooSmall, 1));
126 }
127 return new ArraySegment<byte>(new byte[receiveBufferSize]);
128 }
129
130 public static WebSocket CreateFromStream(Stream stream, bool isServer, string? subProtocol, TimeSpan keepAliveInterval)
131 {
132 if (stream == null)
133 {
134 throw new ArgumentNullException("stream");
135 }
136 if (!stream.CanRead || !stream.CanWrite)
137 {
139 }
140 if (subProtocol != null)
141 {
143 }
144 if (keepAliveInterval != Timeout.InfiniteTimeSpan && keepAliveInterval < TimeSpan.Zero)
145 {
146 throw new ArgumentOutOfRangeException("keepAliveInterval", keepAliveInterval, System.SR.Format(System.SR.net_WebSockets_ArgumentOutOfRange_TooSmall, 0));
147 }
148 return new ManagedWebSocket(stream, isServer, subProtocol, keepAliveInterval);
149 }
150
152 {
153 if (stream == null)
154 {
155 throw new ArgumentNullException("stream");
156 }
157 if (options == null)
158 {
159 throw new ArgumentNullException("options");
160 }
161 if (!stream.CanRead || !stream.CanWrite)
162 {
164 }
165 return new ManagedWebSocket(stream, options);
166 }
167
168 [EditorBrowsable(EditorBrowsableState.Never)]
169 [Obsolete("This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.")]
170 public static bool IsApplicationTargeting45()
171 {
172 return true;
173 }
174
175 [EditorBrowsable(EditorBrowsableState.Never)]
176 [Obsolete("This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.")]
177 public static void RegisterPrefixes()
178 {
180 }
181
182 [EditorBrowsable(EditorBrowsableState.Never)]
183 public static WebSocket CreateClientWebSocket(Stream innerStream, string? subProtocol, int receiveBufferSize, int sendBufferSize, TimeSpan keepAliveInterval, bool useZeroMaskingKey, ArraySegment<byte> internalBuffer)
184 {
185 if (innerStream == null)
186 {
187 throw new ArgumentNullException("innerStream");
188 }
189 if (!innerStream.CanRead || !innerStream.CanWrite)
190 {
191 throw new ArgumentException((!innerStream.CanRead) ? System.SR.NotReadableStream : System.SR.NotWriteableStream, "innerStream");
192 }
193 if (subProtocol != null)
194 {
196 }
197 if (keepAliveInterval != Timeout.InfiniteTimeSpan && keepAliveInterval < TimeSpan.Zero)
198 {
199 throw new ArgumentOutOfRangeException("keepAliveInterval", keepAliveInterval, System.SR.Format(System.SR.net_WebSockets_ArgumentOutOfRange_TooSmall, 0));
200 }
201 if (receiveBufferSize <= 0 || sendBufferSize <= 0)
202 {
203 throw new ArgumentOutOfRangeException((receiveBufferSize <= 0) ? "receiveBufferSize" : "sendBufferSize", (receiveBufferSize <= 0) ? receiveBufferSize : sendBufferSize, System.SR.Format(System.SR.net_WebSockets_ArgumentOutOfRange_TooSmall, 0));
204 }
205 return new ManagedWebSocket(innerStream, isServer: false, subProtocol, keepAliveInterval);
206 }
207}
static ArrayPool< T > Shared
Definition ArrayPool.cs:7
static byte Max(byte val1, byte val2)
Definition Math.cs:738
static void ValidateSubprotocol(string subProtocol)
static bool IsStateTerminal(WebSocketState state)
Definition WebSocket.cs:99
async ValueTask SendWithArrayPoolAsync(ReadOnlyMemory< byte > buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken)
Definition WebSocket.cs:68
static ArraySegment< byte > CreateClientBuffer(int receiveBufferSize, int sendBufferSize)
Definition WebSocket.cs:108
virtual ValueTask SendAsync(ReadOnlyMemory< byte > buffer, WebSocketMessageType messageType, WebSocketMessageFlags messageFlags, CancellationToken cancellationToken=default(CancellationToken))
Definition WebSocket.cs:63
static bool IsApplicationTargeting45()
Definition WebSocket.cs:170
static ArraySegment< byte > CreateServerBuffer(int receiveBufferSize)
Definition WebSocket.cs:121
virtual async ValueTask< ValueWebSocketReceiveResult > ReceiveAsync(Memory< byte > buffer, CancellationToken cancellationToken)
Definition WebSocket.cs:34
Task< WebSocketReceiveResult > ReceiveAsync(ArraySegment< byte > buffer, CancellationToken cancellationToken)
static WebSocket CreateClientWebSocket(Stream innerStream, string? subProtocol, int receiveBufferSize, int sendBufferSize, TimeSpan keepAliveInterval, bool useZeroMaskingKey, ArraySegment< byte > internalBuffer)
Definition WebSocket.cs:183
Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken)
virtual ValueTask SendAsync(ReadOnlyMemory< byte > buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken)
Definition WebSocket.cs:54
Task SendAsync(ArraySegment< byte > buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken)
WebSocketCloseStatus? CloseStatus
Definition WebSocket.cs:12
Task CloseAsync(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken)
static void ThrowOnInvalidState(WebSocketState state, params WebSocketState[] validStates)
Definition WebSocket.cs:82
static WebSocket CreateFromStream(Stream stream, bool isServer, string? subProtocol, TimeSpan keepAliveInterval)
Definition WebSocket.cs:130
static WebSocket CreateFromStream(Stream stream, WebSocketCreationOptions options)
Definition WebSocket.cs:151
static TimeSpan DefaultKeepAliveInterval
Definition WebSocket.cs:20
static string NotWriteableStream
Definition SR.cs:62
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string net_WebSockets_ArgumentOutOfRange_TooSmall
Definition SR.cs:146
static string net_WebSockets_InvalidState
Definition SR.cs:14
static string NotReadableStream
Definition SR.cs:60
Definition SR.cs:7
new ConfiguredTaskAwaitable< TResult > ConfigureAwait(bool continueOnCapturedContext)
Definition Task.cs:226
static readonly TimeSpan InfiniteTimeSpan
Definition Timeout.cs:5
void CopyTo(Span< T > destination)
Definition Span.cs:224
static TimeSpan FromSeconds(double value)
Definition TimeSpan.cs:247
static readonly TimeSpan Zero
Definition TimeSpan.cs:21