PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/SignalR/Transports/TransportDisconnectBase.cs

https://github.com/kpmrafeeq/SignalR
C# | 190 lines | 156 code | 30 blank | 4 comment | 7 complexity | e8dcc03495d58dea0cf47a055230e774 MD5 | raw file
Possible License(s): MIT
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. using SignalR.Infrastructure;
  8. namespace SignalR.Transports
  9. {
  10. public abstract class TransportDisconnectBase : ITrackingConnection
  11. {
  12. private readonly HostContext _context;
  13. private readonly ITransportHeartBeat _heartBeat;
  14. private readonly IJsonSerializer _jsonSerializer;
  15. protected int _isDisconnected;
  16. private readonly CancellationTokenSource _timeoutTokenSource;
  17. private readonly CancellationTokenSource _endTokenSource;
  18. private readonly CancellationToken _hostShutdownToken;
  19. private readonly CancellationTokenSource _connectionEndToken;
  20. public TransportDisconnectBase(HostContext context, IJsonSerializer jsonSerializer, ITransportHeartBeat heartBeat)
  21. {
  22. _context = context;
  23. _jsonSerializer = jsonSerializer;
  24. _heartBeat = heartBeat;
  25. _timeoutTokenSource = new CancellationTokenSource();
  26. _endTokenSource = new CancellationTokenSource();
  27. _hostShutdownToken = context.HostShutdownToken();
  28. Completed = new TaskCompletionSource<object>();
  29. // Create a token that represents the end of this connection's life
  30. _connectionEndToken = CancellationTokenSource.CreateLinkedTokenSource(_timeoutTokenSource.Token, _endTokenSource.Token, _hostShutdownToken);
  31. }
  32. public string ConnectionId
  33. {
  34. get
  35. {
  36. return _context.Request.QueryString["connectionId"];
  37. }
  38. }
  39. protected TaskCompletionSource<object> Completed
  40. {
  41. get;
  42. private set;
  43. }
  44. public IEnumerable<string> Groups
  45. {
  46. get
  47. {
  48. if (IsConnectRequest)
  49. {
  50. return Enumerable.Empty<string>();
  51. }
  52. string groupValue = Context.Request.QueryString["groups"];
  53. if (String.IsNullOrEmpty(groupValue))
  54. {
  55. return Enumerable.Empty<string>();
  56. }
  57. return _jsonSerializer.Parse<string[]>(groupValue);
  58. }
  59. }
  60. public Func<Task> Disconnected { get; set; }
  61. public virtual bool IsAlive
  62. {
  63. get { return _context.Response.IsClientConnected; }
  64. }
  65. protected CancellationToken ConnectionEndToken
  66. {
  67. get
  68. {
  69. return _connectionEndToken.Token;
  70. }
  71. }
  72. protected bool IsDisconnected
  73. {
  74. get
  75. {
  76. return _isDisconnected == 1;
  77. }
  78. }
  79. public bool IsTimedOut
  80. {
  81. get
  82. {
  83. return _timeoutTokenSource.IsCancellationRequested;
  84. }
  85. }
  86. public virtual bool SupportsKeepAlive
  87. {
  88. get
  89. {
  90. return true;
  91. }
  92. }
  93. public virtual TimeSpan DisconnectThreshold
  94. {
  95. get { return TimeSpan.FromSeconds(5); }
  96. }
  97. protected virtual bool IsConnectRequest
  98. {
  99. get
  100. {
  101. return Context.Request.Url.LocalPath.EndsWith("/connect", StringComparison.OrdinalIgnoreCase);
  102. }
  103. }
  104. protected bool IsAbortRequest
  105. {
  106. get
  107. {
  108. return Context.Request.Url.LocalPath.EndsWith("/abort", StringComparison.OrdinalIgnoreCase);
  109. }
  110. }
  111. public Task Disconnect()
  112. {
  113. return OnDisconnect().Then(() => Connection.Close());
  114. }
  115. public Task OnDisconnect()
  116. {
  117. // When a connection is aborted (graceful disconnect) we send a command to it
  118. // telling to to disconnect. At that moment, we raise the disconnect event and
  119. // remove this connection from the heartbeat so we don't end up raising it for the same connection.
  120. HeartBeat.RemoveConnection(this);
  121. if (Interlocked.Exchange(ref _isDisconnected, 1) == 0)
  122. {
  123. var disconnected = Disconnected; // copy before invoking event to avoid race
  124. if (disconnected != null)
  125. {
  126. return disconnected().Catch();
  127. }
  128. }
  129. return TaskAsyncHelper.Empty;
  130. }
  131. public void Timeout()
  132. {
  133. _timeoutTokenSource.Cancel();
  134. }
  135. public virtual void KeepAlive()
  136. {
  137. }
  138. public void End()
  139. {
  140. _endTokenSource.Cancel();
  141. }
  142. public void CompleteRequest()
  143. {
  144. Completed.TrySetResult(null);
  145. }
  146. protected ITransportConnection Connection { get; set; }
  147. protected HostContext Context
  148. {
  149. get { return _context; }
  150. }
  151. protected ITransportHeartBeat HeartBeat
  152. {
  153. get { return _heartBeat; }
  154. }
  155. public Uri Url
  156. {
  157. get { return _context.Request.Url; }
  158. }
  159. }
  160. }