-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
OwinWebSocketHandler.cs
207 lines (177 loc) · 7.67 KB
/
OwinWebSocketHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Net.WebSockets;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Hosting;
using Microsoft.AspNet.SignalR.WebSockets;
namespace Microsoft.AspNet.SignalR.Owin
{
using WebSocketCloseAsync =
Func
<
int /* closeStatus */,
string /* closeDescription */,
CancellationToken /* cancel */,
Task
>;
using WebSocketReceiveAsync =
Func
<
ArraySegment<byte> /* data */,
CancellationToken /* cancel */,
Task
<
Tuple
<
int /* messageType */,
bool /* endOfMessage */,
int /* count */
>
>
>;
using WebSocketSendAsync =
Func
<
ArraySegment<byte> /* data */,
int /* messageType */,
bool /* endOfMessage */,
CancellationToken /* cancel */,
Task
>;
internal class OwinWebSocketHandler
{
private readonly Func<IWebSocket, Task> _callback;
private readonly int? _maxIncomingMessageSize;
public OwinWebSocketHandler(Func<IWebSocket, Task> callback, int? maxIncomingMessageSize)
{
_callback = callback;
_maxIncomingMessageSize = maxIncomingMessageSize;
}
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "The websocket handler disposes the socket when the receive loop is over.")]
public Task ProcessRequest(IDictionary<string, object> environment)
{
object value;
WebSocket webSocket;
// Try to get the websocket context from the environment
if (!environment.TryGetValue(typeof(WebSocketContext).FullName, out value))
{
webSocket = new OwinWebSocket(environment);
}
else
{
webSocket = ((WebSocketContext)value).WebSocket;
}
var cts = new CancellationTokenSource();
var webSocketHandler = new DefaultWebSocketHandler(_maxIncomingMessageSize);
var task = webSocketHandler.ProcessWebSocketRequestAsync(webSocket, cts.Token);
RunWebSocketHandler(webSocketHandler, cts);
return task;
}
private void RunWebSocketHandler(DefaultWebSocketHandler handler, CancellationTokenSource cts)
{
// async void methods are not supported in ASP.NET and they throw a InvalidOperationException.
Task.Run(async () =>
{
try
{
await _callback(handler).PreserveCulture();
}
catch
{
// This error was already handled by other layers
// we can no-op here so we don't cause an unobserved exception
}
// Always try to close async, if the websocket already closed
// then this will no-op
await handler.CloseAsync().PreserveCulture();
// Cancel the token
cts.Cancel();
});
}
private class OwinWebSocket : WebSocket
{
private readonly WebSocketSendAsync _sendAsync;
private readonly WebSocketReceiveAsync _receiveAsync;
private readonly WebSocketCloseAsync _closeAsync;
public OwinWebSocket(IDictionary<string, object> env)
{
_sendAsync = (WebSocketSendAsync)env[WebSocketConstants.WebSocketSendAsyncKey];
_receiveAsync = (WebSocketReceiveAsync)env[WebSocketConstants.WebSocketReceiveAyncKey];
_closeAsync = (WebSocketCloseAsync)env[WebSocketConstants.WebSocketCloseAsyncKey];
}
public override void Abort()
{
}
public override Task CloseAsync(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken)
{
return _closeAsync((int)closeStatus, statusDescription, cancellationToken);
}
public override Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string statusDescription, CancellationToken cancellationToken)
{
return CloseAsync(closeStatus, statusDescription, cancellationToken);
}
public override WebSocketCloseStatus? CloseStatus
{
get { throw new NotImplementedException(); }
}
public override string CloseStatusDescription
{
get { throw new NotImplementedException(); }
}
public override void Dispose()
{
}
public override async Task<WebSocketReceiveResult> ReceiveAsync(ArraySegment<byte> buffer, CancellationToken cancellationToken)
{
var tuple = await _receiveAsync(buffer, cancellationToken).PreserveCulture();
int messageType = tuple.Item1;
bool endOfMessage = tuple.Item2;
int count = tuple.Item3;
return new WebSocketReceiveResult(count, OpCodeToEnum(messageType), endOfMessage);
}
public override Task SendAsync(ArraySegment<byte> buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken)
{
return _sendAsync(buffer, EnumToOpCode(messageType), endOfMessage, cancellationToken);
}
public override WebSocketState State
{
get { throw new NotImplementedException(); }
}
public override string SubProtocol
{
get { throw new NotImplementedException(); }
}
private static WebSocketMessageType OpCodeToEnum(int messageType)
{
switch (messageType)
{
case 0x1:
return WebSocketMessageType.Text;
case 0x2:
return WebSocketMessageType.Binary;
case 0x8:
return WebSocketMessageType.Close;
default:
throw new ArgumentOutOfRangeException("messageType", messageType, String.Empty);
}
}
private static int EnumToOpCode(WebSocketMessageType webSocketMessageType)
{
switch (webSocketMessageType)
{
case WebSocketMessageType.Text:
return 0x1;
case WebSocketMessageType.Binary:
return 0x2;
case WebSocketMessageType.Close:
return 0x8;
default:
throw new ArgumentOutOfRangeException("webSocketMessageType", webSocketMessageType, String.Empty);
}
}
}
}
}