mscorlib(4.0.0.0) API with additions
WebSocket.cs
2 using System.IO;
4 using System.Threading;
6 
7 namespace System.Net.WebSockets
8 {
10  public abstract class WebSocket : IDisposable
11  {
12  private static TimeSpan? defaultKeepAliveInterval;
13 
16  public abstract WebSocketCloseStatus? CloseStatus
17  {
18  get;
19  }
20 
23  public abstract string CloseStatusDescription
24  {
25  get;
26  }
27 
30  public abstract string SubProtocol
31  {
32  get;
33  }
34 
37  public abstract WebSocketState State
38  {
39  get;
40  }
41 
45  {
46  get
47  {
48  if (!defaultKeepAliveInterval.HasValue)
49  {
50  if (WebSocketProtocolComponent.IsSupported)
51  {
52  defaultKeepAliveInterval = WebSocketProtocolComponent.WebSocketGetDefaultKeepAliveInterval();
53  }
54  else
55  {
56  defaultKeepAliveInterval = Timeout.InfiniteTimeSpan;
57  }
58  }
59  return defaultKeepAliveInterval.Value;
60  }
61  }
62 
67  public static ArraySegment<byte> CreateClientBuffer(int receiveBufferSize, int sendBufferSize)
68  {
69  WebSocketHelpers.ValidateBufferSizes(receiveBufferSize, sendBufferSize);
70  return WebSocketBuffer.CreateInternalBufferArraySegment(receiveBufferSize, sendBufferSize, isServerBuffer: false);
71  }
72 
76  public static ArraySegment<byte> CreateServerBuffer(int receiveBufferSize)
77  {
78  WebSocketHelpers.ValidateBufferSizes(receiveBufferSize, 16);
79  return WebSocketBuffer.CreateInternalBufferArraySegment(receiveBufferSize, 16, isServerBuffer: true);
80  }
81 
91  [EditorBrowsable(EditorBrowsableState.Never)]
92  public static WebSocket CreateClientWebSocket(Stream innerStream, string subProtocol, int receiveBufferSize, int sendBufferSize, TimeSpan keepAliveInterval, bool useZeroMaskingKey, ArraySegment<byte> internalBuffer)
93  {
94  if (!WebSocketProtocolComponent.IsSupported)
95  {
96  WebSocketHelpers.ThrowPlatformNotSupportedException_WSPC();
97  }
98  WebSocketHelpers.ValidateInnerStream(innerStream);
99  WebSocketHelpers.ValidateOptions(subProtocol, receiveBufferSize, sendBufferSize, keepAliveInterval);
100  WebSocketHelpers.ValidateArraySegment(internalBuffer, "internalBuffer");
101  WebSocketBuffer.Validate(internalBuffer.Count, receiveBufferSize, sendBufferSize, isServerBuffer: false);
102  return new InternalClientWebSocket(innerStream, subProtocol, receiveBufferSize, sendBufferSize, keepAliveInterval, useZeroMaskingKey, internalBuffer);
103  }
104 
105  internal static WebSocket CreateServerWebSocket(Stream innerStream, string subProtocol, int receiveBufferSize, TimeSpan keepAliveInterval, ArraySegment<byte> internalBuffer)
106  {
107  if (!WebSocketProtocolComponent.IsSupported)
108  {
109  WebSocketHelpers.ThrowPlatformNotSupportedException_WSPC();
110  }
111  WebSocketHelpers.ValidateInnerStream(innerStream);
112  WebSocketHelpers.ValidateOptions(subProtocol, receiveBufferSize, 16, keepAliveInterval);
113  WebSocketHelpers.ValidateArraySegment(internalBuffer, "internalBuffer");
114  WebSocketBuffer.Validate(internalBuffer.Count, receiveBufferSize, 16, isServerBuffer: true);
115  return new ServerWebSocket(innerStream, subProtocol, receiveBufferSize, keepAliveInterval, internalBuffer);
116  }
117 
119  [EditorBrowsable(EditorBrowsableState.Never)]
120  public static void RegisterPrefixes()
121  {
122  WebRequest.RegisterPrefix(Uri.UriSchemeWs + ":", new WebSocketHttpRequestCreator(usingHttps: false));
123  WebRequest.RegisterPrefix(Uri.UriSchemeWss + ":", new WebSocketHttpRequestCreator(usingHttps: true));
124  }
125 
129  [EditorBrowsable(EditorBrowsableState.Never)]
130  [Obsolete("This API supports the .NET Framework infrastructure and is not intended to be used directly from your code.")]
131  public static bool IsApplicationTargeting45()
132  {
133  return BinaryCompatibility.TargetsAtLeast_Desktop_V4_5;
134  }
135 
137  public abstract void Abort();
138 
144  public abstract Task CloseAsync(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken);
145 
151  public abstract Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken);
152 
154  public abstract void Dispose();
155 
160  public abstract Task<WebSocketReceiveResult> ReceiveAsync(ArraySegment<byte> buffer, CancellationToken cancellationToken);
161 
168  public abstract Task SendAsync(ArraySegment<byte> buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken);
169 
173  protected static void ThrowOnInvalidState(WebSocketState state, params WebSocketState[] validStates)
174  {
175  string text = string.Empty;
176  if (validStates != null && validStates.Length != 0)
177  {
178  foreach (WebSocketState webSocketState in validStates)
179  {
180  if (state == webSocketState)
181  {
182  return;
183  }
184  }
185  text = string.Join(", ", validStates);
186  }
187  throw new WebSocketException(SR.GetString("net_WebSockets_InvalidState", state, text));
188  }
189 
194  protected static bool IsStateTerminal(WebSocketState state)
195  {
196  if (state != WebSocketState.Closed)
197  {
198  return state == WebSocketState.Aborted;
199  }
200  return true;
201  }
202  }
203 }
WebSocketCloseStatus
Represents well known WebSocket close codes as defined in section 11.7 of the WebSocket protocol spec...
Propagates notification that operations should be canceled.
static TimeSpan DefaultKeepAliveInterval
Gets the default WebSocket protocol keep-alive interval in milliseconds.
Definition: WebSocket.cs:45
abstract Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken)
Initiates or completes the close handshake defined in the WebSocket protocol specification section 7.
abstract string CloseStatusDescription
Allows the remote endpoint to describe the reason why the connection was closed.
Definition: WebSocket.cs:24
WebSocketState
Defines the different states a WebSockets instance can be in.
EditorBrowsableState
Specifies the browsable state of a property or method from within an editor.
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
Definition: IDisposable.cs:8
Makes a request to a Uniform Resource Identifier (URI). This is an abstract class.
Definition: WebRequest.cs:21
WebSocketMessageType
Indicates the message type.
Definition: __Canon.cs:3
abstract WebSocketState State
Returns the current state of the WebSocket connection.
Definition: WebSocket.cs:38
abstract void Abort()
Aborts the WebSocket connection and cancels any pending IO operations.
static ArraySegment< byte > CreateClientBuffer(int receiveBufferSize, int sendBufferSize)
Create client buffers to use with this T:System.Net.WebSockets.WebSocket instance.
Definition: WebSocket.cs:67
static readonly TimeSpan InfiniteTimeSpan
A constant used to specify an infinite waiting period, for methods that accept a T:System....
Definition: Timeout.cs:13
static ArraySegment< byte > CreateServerBuffer(int receiveBufferSize)
Creates a WebSocket server buffer.
Definition: WebSocket.cs:76
static void ThrowOnInvalidState(WebSocketState state, params WebSocketState[] validStates)
Verifies that the connection is in an expected state.
Definition: WebSocket.cs:173
The WebSocket class allows applications to send and receive data after the WebSocket upgrade has comp...
Definition: WebSocket.cs:10
static bool IsStateTerminal(WebSocketState state)
Returns a value that indicates if the state of the WebSocket instance is closed or aborted.
Definition: WebSocket.cs:194
static bool RegisterPrefix(string prefix, IWebRequestCreate creator)
Registers a T:System.Net.WebRequest descendant for the specified URI.
Definition: WebRequest.cs:624
abstract Task SendAsync(ArraySegment< byte > buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken)
Sends data over the T:System.Net.WebSockets.WebSocket connection asynchronously.
abstract Task CloseAsync(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken)
Closes the WebSocket connection as an asynchronous operation using the close handshake defined in the...
abstract Task< WebSocketReceiveResult > ReceiveAsync(ArraySegment< byte > buffer, CancellationToken cancellationToken)
Receives data from the T:System.Net.WebSockets.WebSocket connection asynchronously.
Contains constants that specify infinite time-out intervals. This class cannot be inherited.
Definition: Timeout.cs:8
static WebSocket CreateClientWebSocket(Stream innerStream, string subProtocol, int receiveBufferSize, int sendBufferSize, TimeSpan keepAliveInterval, bool useZeroMaskingKey, ArraySegment< byte > internalBuffer)
This API supports the .NET Framework infrastructure and is not intended to be used directly from your...
Definition: WebSocket.cs:92
Represents an exception that occurred when performing an operation on a WebSocket connection.
int Count
Gets the number of elements in the range delimited by the array segment.
abstract ? WebSocketCloseStatus CloseStatus
Indicates the reason why the remote endpoint initiated the close handshake.
Definition: WebSocket.cs:17
Represents a time interval.To browse the .NET Framework source code for this type,...
Definition: TimeSpan.cs:12
abstract string SubProtocol
The subprotocol that was negotiated during the opening handshake.
Definition: WebSocket.cs:31
abstract void Dispose()
Used to clean up unmanaged resources for ASP.NET and self-hosted implementations.
Provides an object representation of a uniform resource identifier (URI) and easy access to the parts...
Definition: Uri.cs:19
static void RegisterPrefixes()
This API supports the .NET Framework infrastructure and is not intended to be used directly from your...
Definition: WebSocket.cs:120
static bool IsApplicationTargeting45()
Returns a value that indicates if the WebSocket instance is targeting .NET Framework 4....
Definition: WebSocket.cs:131
Represents an asynchronous operation that can return a value.
Definition: Task.cs:18
Provides a generic view of a sequence of bytes. This is an abstract class.To browse the ....
Definition: Stream.cs:16