/
TransportDisconnectBase.cs
336 lines (275 loc) · 10.1 KB
/
TransportDisconnectBase.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.md in the project root for license information.
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR.Hosting;
using Microsoft.AspNet.SignalR.Infrastructure;
using Microsoft.AspNet.SignalR.Tracing;
namespace Microsoft.AspNet.SignalR.Transports
{
[SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable", Justification = "Disposable fields are disposed from a different method")]
public abstract class TransportDisconnectBase : ITrackingConnection
{
private readonly HostContext _context;
private readonly ITransportHeartbeat _heartbeat;
private TextWriter _outputWriter;
private TraceSource _trace;
private int _timedOut;
private readonly IPerformanceCounterManager _counters;
private int _ended;
private TransportConnectionStates _state;
internal static readonly Func<Task> _emptyTaskFunc = () => TaskAsyncHelper.Empty;
// Token that represents the end of the connection based on a combination of
// conditions (timeout, disconnect, connection forcibly ended, host shutdown)
private CancellationToken _connectionEndToken;
private SafeCancellationTokenSource _connectionEndTokenSource;
// Token that represents the host shutting down
private CancellationToken _hostShutdownToken;
private IDisposable _hostRegistration;
private IDisposable _connectionEndRegistration;
internal HttpRequestLifeTime _requestLifeTime;
protected TransportDisconnectBase(HostContext context, ITransportHeartbeat heartbeat, IPerformanceCounterManager performanceCounterManager, ITraceManager traceManager)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (heartbeat == null)
{
throw new ArgumentNullException("heartbeat");
}
if (performanceCounterManager == null)
{
throw new ArgumentNullException("performanceCounterManager");
}
if (traceManager == null)
{
throw new ArgumentNullException("traceManager");
}
_context = context;
_heartbeat = heartbeat;
_counters = performanceCounterManager;
// Queue to protect against overlapping writes to the underlying response stream
WriteQueue = new TaskQueue();
_trace = traceManager["SignalR.Transports." + GetType().Name];
}
protected TraceSource Trace
{
get
{
return _trace;
}
}
public string ConnectionId
{
get;
set;
}
public virtual TextWriter OutputWriter
{
get
{
if (_outputWriter == null)
{
_outputWriter = CreateResponseWriter();
_outputWriter.NewLine = "\n";
}
return _outputWriter;
}
}
internal TaskQueue WriteQueue
{
get;
set;
}
public Func<Task> Disconnected { get; set; }
public virtual CancellationToken CancellationToken
{
get { return _context.Response.CancellationToken; }
}
public virtual bool IsAlive
{
get
{
// If the CTS is tripped or the request has ended then the connection isn't alive
return !(CancellationToken.IsCancellationRequested || (_requestLifeTime != null && _requestLifeTime.Task.IsCompleted));
}
}
protected CancellationToken ConnectionEndToken
{
get
{
return _connectionEndToken;
}
}
public bool IsTimedOut
{
get
{
return _timedOut == 1;
}
}
public virtual bool SupportsKeepAlive
{
get
{
return true;
}
}
public virtual TimeSpan DisconnectThreshold
{
get { return TimeSpan.FromSeconds(5); }
}
public virtual bool IsConnectRequest
{
get
{
return Context.Request.Url.LocalPath.EndsWith("/connect", StringComparison.OrdinalIgnoreCase);
}
}
protected bool IsAbortRequest
{
get
{
return Context.Request.Url.LocalPath.EndsWith("/abort", StringComparison.OrdinalIgnoreCase);
}
}
protected ITransportConnection Connection { get; set; }
protected HostContext Context
{
get { return _context; }
}
protected ITransportHeartbeat Heartbeat
{
get { return _heartbeat; }
}
public Uri Url
{
get { return _context.Request.Url; }
}
protected virtual TextWriter CreateResponseWriter()
{
return new BinaryTextWriter(Context.Response);
}
protected void IncrementErrors()
{
_counters.ErrorsTransportTotal.Increment();
_counters.ErrorsTransportPerSec.Increment();
_counters.ErrorsAllTotal.Increment();
_counters.ErrorsAllPerSec.Increment();
}
public Task Disconnect()
{
// Ensure delegate continues to use the C# Compiler static delegate caching optimization.
return Abort(clean: false).Then(transport => transport.Connection.Close(transport.ConnectionId), this);
}
public Task Abort()
{
return Abort(clean: true);
}
public Task Abort(bool clean)
{
if (clean)
{
ApplyState(TransportConnectionStates.Aborted);
}
else
{
ApplyState(TransportConnectionStates.Disconnected);
}
Trace.TraceInformation("Abort(" + ConnectionId + ")");
// When a connection is aborted (graceful disconnect) we send a command to it
// telling to to disconnect. At that moment, we raise the disconnect event and
// remove this connection from the heartbeat so we don't end up raising it for the same connection.
Heartbeat.RemoveConnection(this);
// End the connection
End();
var disconnected = Disconnected ?? _emptyTaskFunc;
// Ensure delegate continues to use the C# Compiler static delegate caching optimization.
return disconnected().Catch((ex, state) => OnDisconnectError(ex, state), Trace)
.Then(counters => counters.ConnectionsDisconnected.Increment(), _counters);
}
public void ApplyState(TransportConnectionStates states)
{
_state |= states;
}
public void Timeout()
{
if (Interlocked.Exchange(ref _timedOut, 1) == 0)
{
Trace.TraceInformation("Timeout(" + ConnectionId + ")");
End();
}
}
public virtual Task KeepAlive()
{
return TaskAsyncHelper.Empty;
}
public void End()
{
if (Interlocked.Exchange(ref _ended, 1) == 0)
{
Trace.TraceInformation("End(" + ConnectionId + ")");
if (_connectionEndTokenSource != null)
{
_connectionEndTokenSource.Cancel();
}
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_connectionEndTokenSource.Dispose();
_connectionEndRegistration.Dispose();
_hostRegistration.Dispose();
ApplyState(TransportConnectionStates.Disposed);
}
}
protected virtual internal Task EnqueueOperation(Func<Task> writeAsync)
{
return EnqueueOperation(state => ((Func<Task>)state).Invoke(), writeAsync);
}
protected virtual internal Task EnqueueOperation(Func<object, Task> writeAsync, object state)
{
if (!IsAlive)
{
return TaskAsyncHelper.Empty;
}
// Only enqueue new writes if the connection is alive
return WriteQueue.Enqueue(writeAsync, state);
}
protected virtual void InitializePersistentState()
{
_hostShutdownToken = _context.HostShutdownToken();
_requestLifeTime = new HttpRequestLifeTime(this, WriteQueue, Trace, ConnectionId);
// Create a token that represents the end of this connection's life
_connectionEndTokenSource = new SafeCancellationTokenSource();
_connectionEndToken = _connectionEndTokenSource.Token;
// Handle the shutdown token's callback so we can end our token if it trips
_hostRegistration = _hostShutdownToken.SafeRegister(state =>
{
((SafeCancellationTokenSource)state).Cancel();
},
_connectionEndTokenSource);
// When the connection ends release the request
_connectionEndRegistration = CancellationToken.SafeRegister(state =>
{
((HttpRequestLifeTime)state).Complete();
},
_requestLifeTime);
}
private static void OnDisconnectError(AggregateException ex, object state)
{
((TraceSource)state).TraceEvent(TraceEventType.Error, 0, "Failed to raise disconnect: " + ex.GetBaseException());
}
}
}