mscorlib(4.0.0.0) API with additions
ClientWebSocketOptions.cs
3 using System.Threading;
4 
5 namespace System.Net.WebSockets
6 {
8  public sealed class ClientWebSocketOptions
9  {
10  private bool isReadOnly;
11 
12  private readonly IList<string> requestedSubProtocols;
13 
14  private readonly WebHeaderCollection requestHeaders;
15 
16  private TimeSpan keepAliveInterval;
17 
18  private int receiveBufferSize;
19 
20  private int sendBufferSize;
21 
22  private ArraySegment<byte>? buffer;
23 
24  private bool useDefaultCredentials;
25 
26  private ICredentials credentials;
27 
28  private IWebProxy proxy;
29 
30  private X509CertificateCollection clientCertificates;
31 
32  private CookieContainer cookies;
33 
34  internal WebHeaderCollection RequestHeaders => requestHeaders;
35 
39  public bool UseDefaultCredentials
40  {
41  get
42  {
43  return useDefaultCredentials;
44  }
45  set
46  {
47  ThrowIfReadOnly();
48  useDefaultCredentials = value;
49  }
50  }
51 
55  {
56  get
57  {
58  return credentials;
59  }
60  set
61  {
62  ThrowIfReadOnly();
63  credentials = value;
64  }
65  }
66 
69  public IWebProxy Proxy
70  {
71  get
72  {
73  return proxy;
74  }
75  set
76  {
77  ThrowIfReadOnly();
78  proxy = value;
79  }
80  }
81 
85  {
86  get
87  {
88  if (clientCertificates == null)
89  {
90  clientCertificates = new X509CertificateCollection();
91  }
92  return clientCertificates;
93  }
94  set
95  {
96  ThrowIfReadOnly();
97  if (value == null)
98  {
99  throw new ArgumentNullException("value");
100  }
101  clientCertificates = value;
102  }
103  }
104 
105  internal X509CertificateCollection InternalClientCertificates => clientCertificates;
106 
109  public CookieContainer Cookies
110  {
111  get
112  {
113  return cookies;
114  }
115  set
116  {
117  ThrowIfReadOnly();
118  cookies = value;
119  }
120  }
121 
122  internal int ReceiveBufferSize => receiveBufferSize;
123 
124  internal int SendBufferSize => sendBufferSize;
125 
126  internal IList<string> RequestedSubProtocols => requestedSubProtocols;
127 
131  {
132  get
133  {
134  return keepAliveInterval;
135  }
136  set
137  {
138  ThrowIfReadOnly();
139  if (value < Timeout.InfiniteTimeSpan)
140  {
141  throw new ArgumentOutOfRangeException("value", value, SR.GetString("net_WebSockets_ArgumentOutOfRange_TooSmall", Timeout.InfiniteTimeSpan.ToString()));
142  }
143  keepAliveInterval = value;
144  }
145  }
146 
147  internal ClientWebSocketOptions()
148  {
149  requestedSubProtocols = new List<string>();
150  requestHeaders = new WebHeaderCollection(WebHeaderCollectionType.HttpWebRequest);
152  receiveBufferSize = 16384;
153  sendBufferSize = 16384;
154  keepAliveInterval = WebSocket.DefaultKeepAliveInterval;
155  }
156 
160  public void SetRequestHeader(string headerName, string headerValue)
161  {
162  ThrowIfReadOnly();
163  requestHeaders.Set(headerName, headerValue);
164  }
165 
169  public void SetBuffer(int receiveBufferSize, int sendBufferSize)
170  {
171  ThrowIfReadOnly();
172  WebSocketHelpers.ValidateBufferSizes(receiveBufferSize, sendBufferSize);
173  buffer = null;
174  this.receiveBufferSize = receiveBufferSize;
175  this.sendBufferSize = sendBufferSize;
176  }
177 
182  public void SetBuffer(int receiveBufferSize, int sendBufferSize, ArraySegment<byte> buffer)
183  {
184  ThrowIfReadOnly();
185  WebSocketHelpers.ValidateBufferSizes(receiveBufferSize, sendBufferSize);
186  WebSocketHelpers.ValidateArraySegment(buffer, "buffer");
187  WebSocketBuffer.Validate(buffer.Count, receiveBufferSize, sendBufferSize, isServerBuffer: false);
188  this.receiveBufferSize = receiveBufferSize;
189  this.sendBufferSize = sendBufferSize;
191  {
192  this.buffer = buffer;
193  }
194  else
195  {
196  this.buffer = null;
197  }
198  }
199 
200  internal ArraySegment<byte> GetOrCreateBuffer()
201  {
202  if (!buffer.HasValue)
203  {
204  buffer = WebSocket.CreateClientBuffer(receiveBufferSize, sendBufferSize);
205  }
206  return buffer.Value;
207  }
208 
211  public void AddSubProtocol(string subProtocol)
212  {
213  ThrowIfReadOnly();
214  WebSocketHelpers.ValidateSubprotocol(subProtocol);
215  foreach (string requestedSubProtocol in requestedSubProtocols)
216  {
217  if (string.Equals(requestedSubProtocol, subProtocol, StringComparison.OrdinalIgnoreCase))
218  {
219  throw new ArgumentException(SR.GetString("net_WebSockets_NoDuplicateProtocol", subProtocol), "subProtocol");
220  }
221  }
222  requestedSubProtocols.Add(subProtocol);
223  }
224 
225  internal void SetToReadOnly()
226  {
227  isReadOnly = true;
228  }
229 
230  private void ThrowIfReadOnly()
231  {
232  if (isReadOnly)
233  {
234  throw new InvalidOperationException(SR.GetString("net_WebSockets_AlreadyStarted"));
235  }
236  }
237  }
238 }
void SetRequestHeader(string headerName, string headerValue)
Creates a HTTP request header and its value.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Options to use with a T:System.Net.WebSockets.ClientWebSocket object.
static TimeSpan DefaultKeepAliveInterval
Gets the default WebSocket protocol keep-alive interval in milliseconds.
Definition: WebSocket.cs:45
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
Defines a collection that stores T:System.Security.Cryptography.X509Certificates.X509Certificate obje...
bool UseDefaultCredentials
Gets or sets a T:System.Boolean value that indicates if default credentials should be used during Web...
Makes a request to a Uniform Resource Identifier (URI). This is an abstract class.
Definition: WebRequest.cs:21
ICredentials Credentials
Gets or sets the credential information for the client.
Definition: __Canon.cs:3
The exception that is thrown when the value of an argument is outside the allowable range of values a...
void SetBuffer(int receiveBufferSize, int sendBufferSize)
Sets the client buffer parameters.
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 AppDomain CurrentDomain
Gets the current application domain for the current T:System.Threading.Thread.
Definition: AppDomain.cs:274
void AddSubProtocol(string subProtocol)
Adds a sub-protocol to be negotiated during the WebSocket connection handshake.
Represents an application domain, which is an isolated environment where applications execute....
Definition: AppDomain.cs:33
Contains protocol headers associated with a request or response.
static readonly TimeSpan InfiniteTimeSpan
A constant used to specify an infinite waiting period, for methods that accept a T:System....
Definition: Timeout.cs:13
TimeSpan KeepAliveInterval
Gets or sets the WebSocket protocol keep-alive interval in milliseconds.
The WebSocket class allows applications to send and receive data after the WebSocket upgrade has comp...
Definition: WebSocket.cs:10
Provides the base authentication interface for retrieving credentials for Web client authentication.
Definition: ICredentials.cs:5
bool??? IsFullyTrusted
Gets a value that indicates whether assemblies that are loaded into the current application domain ex...
Definition: AppDomain.cs:454
IWebProxy Proxy
Gets or sets the proxy for WebSocket requests.
Contains constants that specify infinite time-out intervals. This class cannot be inherited.
Definition: Timeout.cs:8
The exception that is thrown when one of the arguments provided to a method is not valid.
CookieContainer Cookies
Gets or sets the cookies associated with the request.
int Count
Gets the number of elements in the range delimited by the array segment.
override string ToString()
Converts the value of the current T:System.TimeSpan object to its equivalent string representation.
Definition: TimeSpan.cs:749
Represents a time interval.To browse the .NET Framework source code for this type,...
Definition: TimeSpan.cs:12
void SetBuffer(int receiveBufferSize, int sendBufferSize, ArraySegment< byte > buffer)
Sets client buffer parameters.
X509CertificateCollection ClientCertificates
Gets or sets a collection of client side certificates.
The exception that is thrown when a method call is invalid for the object's current state.
Provides the base interface for implementation of proxy access for the T:System.Net....
Definition: IWebProxy.cs:5
static IWebProxy DefaultWebProxy
Gets or sets the global HTTP proxy.
Definition: WebRequest.cs:463