Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ ConnectAsync()

async Task System.Net.WebSockets.WebSocketHandle.ConnectAsync ( Uri uri,
CancellationToken cancellationToken,
ClientWebSocketOptions options )
inline

Definition at line 76 of file WebSocketHandle.cs.

77 {
79 SocketsHttpHandler handler = null;
80 bool disposeHandler = true;
81 try
82 {
84 WebHeaderCollection requestHeaders = options._requestHeaders;
85 if (requestHeaders != null && requestHeaders.Count > 0)
86 {
87 foreach (string requestHeader in options.RequestHeaders)
88 {
89 httpRequestMessage.Headers.TryAddWithoutValidation(requestHeader, options.RequestHeaders[requestHeader]);
90 }
91 }
94 if (options.Credentials == null && !options.UseDefaultCredentials && options.Proxy == null && options.Cookies == null && options.RemoteCertificateValidationCallback == null)
95 {
98 {
99 disposeHandler = false;
100 handler = s_defaultHandler;
101 if (handler == null)
102 {
103 handler = new SocketsHttpHandler
104 {
105 PooledConnectionLifetime = TimeSpan.Zero,
106 UseProxy = false,
107 UseCookies = false
108 };
109 if (Interlocked.CompareExchange(ref s_defaultHandler, handler, null) != null)
110 {
111 handler.Dispose();
112 handler = s_defaultHandler;
113 }
114 }
115 goto IL_02ee;
116 }
117 }
118 handler = new SocketsHttpHandler
119 {
120 PooledConnectionLifetime = TimeSpan.Zero,
121 CookieContainer = options.Cookies,
122 UseCookies = (options.Cookies != null),
123 SslOptions =
124 {
125 RemoteCertificateValidationCallback = options.RemoteCertificateValidationCallback
126 }
127 };
128 if (options.UseDefaultCredentials)
129 {
130 handler.Credentials = CredentialCache.DefaultCredentials;
131 }
132 else
133 {
134 handler.Credentials = options.Credentials;
135 }
136 if (options.Proxy == null)
137 {
138 handler.UseProxy = false;
139 }
140 else if (options.Proxy != DefaultWebProxy.Instance)
141 {
142 handler.Proxy = options.Proxy;
143 }
146 {
147 handler.SslOptions.ClientCertificates = new X509Certificate2Collection();
148 handler.SslOptions.ClientCertificates.AddRange(options.ClientCertificates);
149 }
150 goto IL_02ee;
151 IL_02ee:
154 if (cancellationToken.CanBeCanceled)
155 {
159 }
160 else
161 {
164 }
166 {
168 externalAndAbortCancellation.Token.ThrowIfCancellationRequested();
169 }
170 if (response.StatusCode != HttpStatusCode.SwitchingProtocols)
171 {
172 throw new WebSocketException(WebSocketError.NotAWebSocket, System.SR.Format(System.SR.net_WebSockets_Connect101Expected, (int)response.StatusCode));
173 }
174 ValidateHeader(response.Headers, "Connection", "Upgrade");
175 ValidateHeader(response.Headers, "Upgrade", "websocket");
176 ValidateHeader(response.Headers, "Sec-WebSocket-Accept", secKeyAndSecWebSocketAccept.Value);
177 string text = null;
178 if (response.Headers.TryGetValues("Sec-WebSocket-Protocol", out IEnumerable<string> values))
179 {
180 string[] array = (string[])values;
181 if (array.Length != 0 && !string.IsNullOrEmpty(array[0]))
182 {
183 if (options._requestedSubProtocols != null)
184 {
185 foreach (string requestedSubProtocol in options._requestedSubProtocols)
186 {
187 if (requestedSubProtocol.Equals(array[0], StringComparison.OrdinalIgnoreCase))
188 {
190 break;
191 }
192 }
193 }
194 if (text == null)
195 {
196 throw new WebSocketException(WebSocketError.UnsupportedProtocol, System.SR.Format(System.SR.net_WebSockets_AcceptUnsupportedProtocol, string.Join(", ", options.RequestedSubProtocols), string.Join(", ", array)));
197 }
198 }
199 }
200 WebSocketDeflateOptions webSocketDeflateOptions = null;
201 if (options.DangerousDeflateOptions != null && response.Headers.TryGetValues("Sec-WebSocket-Extensions", out IEnumerable<string> values2))
202 {
203 foreach (string item in values2)
204 {
206 if (readOnlySpan.TrimStart().StartsWith("permessage-deflate"))
207 {
209 break;
210 }
211 }
212 }
213 if (response.Content == null)
214 {
215 throw new WebSocketException(WebSocketError.ConnectionClosedPrematurely);
216 }
217 Stream stream = response.Content.ReadAsStream();
218 WebSocket = WebSocket.CreateFromStream(stream, new WebSocketCreationOptions
219 {
220 IsServer = false,
221 SubProtocol = text,
222 KeepAliveInterval = options.KeepAliveInterval,
223 DangerousDeflateOptions = webSocketDeflateOptions
224 });
226 }
227 catch (Exception ex)
228 {
229 if (_state < WebSocketState.Closed)
230 {
231 _state = WebSocketState.Closed;
232 }
233 Abort();
234 response?.Dispose();
235 if (ex is WebSocketException || (ex is OperationCanceledException && cancellationToken.IsCancellationRequested))
236 {
237 throw;
238 }
239 throw new WebSocketException(WebSocketError.Faulted, System.SR.net_webstatus_ConnectFailure, ex);
240 }
241 finally
242 {
243 if (disposeHandler)
244 {
245 handler?.Dispose();
246 }
247 }
248 }
virtual Task< HttpResponseMessage > SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
static HttpMethod Get
Definition HttpMethod.cs:35
SslClientAuthenticationOptions SslOptions
override void Dispose(bool disposing)
static void AddWebSocketHeaders(HttpRequestMessage request, string secKey, ClientWebSocketOptions options)
WebSocketDeflateOptions _negotiatedDeflateOptions
static KeyValuePair< string, string > CreateSecKeyAndSecWebSocketAccept()
static WebSocketDeflateOptions ParseDeflateOptions(ReadOnlySpan< char > extension, WebSocketDeflateOptions original)
static SocketsHttpHandler s_defaultHandler
static void ValidateHeader(HttpHeaders headers, string name, string expectedValue)
readonly CancellationTokenSource _abortSource
static WebSocket CreateFromStream(Stream stream, bool isServer, string? subProtocol, TimeSpan keepAliveInterval)
Definition WebSocket.cs:130
static string net_WebSockets_AcceptUnsupportedProtocol
Definition SR.cs:128
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string net_webstatus_ConnectFailure
Definition SR.cs:110
static string net_WebSockets_Connect101Expected
Definition SR.cs:22
Definition SR.cs:7
static CancellationTokenSource CreateLinkedTokenSource(CancellationToken token1, CancellationToken token2)
static int CompareExchange(ref int location1, int value, int comparand)

References System.Net.WebSockets.WebSocketHandle._abortSource, System.Net.WebSockets.WebSocketHandle._negotiatedDeflateOptions, System.Net.WebSockets.WebSocketHandle._state, System.Net.WebSockets.WebSocketHandle.Abort(), System.Net.WebSockets.WebSocketHandle.AddWebSocketHeaders(), System.array, System.cancellationToken, System.Threading.Interlocked.CompareExchange(), System.Collections.Generic.Dictionary< TKey, TValue >.Count, System.Net.WebSockets.WebSocket.CreateFromStream(), System.Threading.CancellationTokenSource.CreateLinkedTokenSource(), System.Net.WebSockets.WebSocketHandle.CreateSecKeyAndSecWebSocketAccept(), System.Net.CredentialCache.DefaultCredentials, System.Net.Http.SocketsHttpHandler.Dispose(), System.SR.Format(), System.Net.Http.HttpMethod.Get, System.Net.WebSockets.WebSocketHandle.DefaultWebProxy.Instance, System.item, System.Net.WebSockets.WebSocketCreationOptions.KeepAliveInterval, System.SR.net_WebSockets_AcceptUnsupportedProtocol, System.SR.net_WebSockets_Connect101Expected, System.SR.net_webstatus_ConnectFailure, System.options, System.Net.WebSockets.WebSocketHandle.ParseDeflateOptions(), System.Net.WebSockets.WebSocketHandle.s_defaultHandler, System.Net.Http.HttpMessageInvoker.SendAsync(), System.Net.Http.SocketsHttpHandler.SslOptions, System.stream, System.text, System.Threading.CancellationTokenSource.Token, System.Net.UseProxy, System.Net.WebSockets.WebSocketHandle.ValidateHeader(), System.values, and System.TimeSpan.Zero.

Referenced by System.Net.WebSockets.ClientWebSocket.ConnectAsyncCore().