private Task ProcessMessages(ITransportConnection connection, Func <Task> initialize) { var disposer = new Disposer(); if (BeforeCancellationTokenCallbackRegistered != null) { BeforeCancellationTokenCallbackRegistered(); } var cancelContext = new ForeverTransportContext(this, disposer); // Ensure delegate continues to use the C# Compiler static delegate caching optimization. IDisposable registration = ConnectionEndToken.SafeRegister(state => Cancel(state), cancelContext); var messageContext = new MessageContext(this, _transportLifetime, registration); if (BeforeReceive != null) { BeforeReceive(); } try { // Ensure we enqueue the response initialization before any messages are received EnqueueOperation(state => InitializeResponse((ITransportConnection)state), connection) .Catch((ex, state) => OnError(ex, state), messageContext); // Ensure delegate continues to use the C# Compiler static delegate caching optimization. IDisposable subscription = connection.Receive(LastMessageId, (response, state) => OnMessageReceived(response, state), MaxMessages, messageContext); disposer.Set(subscription); if (AfterReceive != null) { AfterReceive(); } // Ensure delegate continues to use the C# Compiler static delegate caching optimization. initialize().Then(tcs => tcs.TrySetResult(null), InitializeTcs) .Catch((ex, state) => OnError(ex, state), messageContext); } catch (OperationCanceledException ex) { InitializeTcs.TrySetCanceled(); _transportLifetime.Complete(ex); } catch (Exception ex) { InitializeTcs.TrySetCanceled(); _transportLifetime.Complete(ex); } return(_requestLifeTime.Task); }
private Task ProcessMessages(ITransportConnection connection, Func <Task> initialize) { var disposer = new Disposer(); var cancelContext = new LongPollingTransportContext(this, disposer); // Ensure delegate continues to use the C# Compiler static delegate caching optimization. IDisposable registration = ConnectionEndToken.SafeRegister(state => Cancel(state), cancelContext); var lifeTime = new RequestLifetime(this, _requestLifeTime, registration); var messageContext = new MessageContext(this, lifeTime); try { // Ensure delegate continues to use the C# Compiler static delegate caching optimization. IDisposable subscription = connection.Receive(MessageId, (response, state) => OnMessageReceived(response, state), MaxMessages, messageContext); // Set the disposable disposer.Set(subscription); // Ensure delegate continues to use the C# Compiler static delegate caching optimization. initialize().Catch((ex, state) => OnError(ex, state), messageContext); } catch (Exception ex) { lifeTime.Complete(ex); } return(_requestLifeTime.Task); }
public void SetDisposesIfShouldDispose() { bool disposed = false; var disposable = new DisposableAction(() => { disposed = true; }); var disposer = new Disposer(); Assert.False(disposed); disposer.Dispose(); disposer.Set(disposable); Assert.True(disposed); }
public Task <IResponse> Post(string url, Action <IRequest> prepareRequest, IDictionary <string, string> postData, bool isLongRunning) { if (prepareRequest == null) { throw new ArgumentNullException("prepareRequest"); } var responseDisposer = new Disposer(); var cts = new CancellationTokenSource(); var requestMessage = new HttpRequestMessage(HttpMethod.Post, new Uri(url)); if (postData == null) { requestMessage.Content = new StringContent(String.Empty); } else { requestMessage.Content = new ByteArrayContent(HttpHelper.ProcessPostData(postData)); } var request = new HttpRequestMessageWrapper(requestMessage, () => { cts.Cancel(); responseDisposer.Dispose(); }); prepareRequest(request); var httpClient = GetHttpClient(isLongRunning); return(httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, cts.Token) .Then(responseMessage => { if (responseMessage.IsSuccessStatusCode) { responseDisposer.Set(responseMessage); } else { // Dispose the response (https://github.com/SignalR/SignalR/issues/4092) var message = responseMessage.ToString(); responseMessage.RequestMessage.Dispose(); responseMessage.Dispose(); throw new HttpClientException(message); } return (IResponse) new HttpResponseMessageWrapper(responseMessage); })); }
public Task <IResponse> Post(string url, Action <IRequest> prepareRequest, IDictionary <string, string> postData, bool isLongRunning) { if (prepareRequest == null) { throw new ArgumentNullException("prepareRequest"); } var responseDisposer = new Disposer(); var cts = new CancellationTokenSource(); var requestMessage = new HttpRequestMessage(HttpMethod.Post, new Uri(url)); if (postData == null) { requestMessage.Content = new StringContent(String.Empty); } else { requestMessage.Content = new FormUrlEncodedContent(postData); } var request = new HttpRequestMessageWrapper(requestMessage, () => { cts.Cancel(); responseDisposer.Dispose(); }); prepareRequest(request); HttpClient httpClient = GetHttpClient(isLongRunning); return(httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, cts.Token) .Then(responseMessage => { if (responseMessage.IsSuccessStatusCode) { responseDisposer.Set(responseMessage); } else { throw new HttpClientException(responseMessage); } return (IResponse) new HttpResponseMessageWrapper(responseMessage); })); }
public Task <IResponse> Get(string url, Action <IRequest> prepareRequest, bool isLongRunning) { if (prepareRequest == null) { throw new ArgumentNullException("prepareRequest"); } var responseDisposer = new Disposer(); var cts = new CancellationTokenSource(); var requestMessage = new HttpRequestMessage(HttpMethod.Get, new Uri(url)); var request = new HttpRequestMessageWrapper(requestMessage, () => { cts.Cancel(); responseDisposer.Dispose(); }); prepareRequest(request); var httpClient = GetHttpClient(isLongRunning); return(httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseHeadersRead, cts.Token) .Then(responseMessage => { if (responseMessage.IsSuccessStatusCode) { responseDisposer.Set(responseMessage); } else { // Dispose the response (https://github.com/SignalR/SignalR/issues/4092) responseMessage.RequestMessage.Dispose(); responseMessage.Dispose(); // None of the getters on HttpResponseMessage throw ODE, so it should be safe to give the catcher of the exception // access to the response. They may get an ODE if they try to read the body, but that's OK. throw new HttpClientException(responseMessage); } return (IResponse) new HttpResponseMessageWrapper(responseMessage); })); }
private void OpenConnection(IConnection connection, string data, CancellationToken disconnectToken, Action initializeCallback, Action <Exception> errorCallback) { // If we're reconnecting add /connect to the url bool reconnecting = initializeCallback == null; var callbackInvoker = new ThreadSafeInvoker(); var requestDisposer = new Disposer(); Action initializeInvoke = () => { callbackInvoker.Invoke(initializeCallback); }; var url = connection.Url + (reconnecting ? "reconnect" : "connect") + GetReceiveQueryString(connection, data); connection.Trace(TraceLevels.Events, "SSE: GET {0}", url); HttpClient.Get(url, req => { _request = req; _request.Accept = "text/event-stream"; connection.PrepareRequest(_request); }, isLongRunning: true).ContinueWith(task => { if (task.IsFaulted || task.IsCanceled) { Exception exception; if (task.IsCanceled) { exception = new OperationCanceledException(Resources.Error_TaskCancelledException); } else { exception = task.Exception.Unwrap(); } if (errorCallback != null) { callbackInvoker.Invoke((cb, ex) => cb(ex), errorCallback, exception); } else if (!_stop && reconnecting) { // Only raise the error event if we failed to reconnect connection.OnError(exception); Reconnect(connection, data, disconnectToken); } requestDisposer.Dispose(); } else { // If the disconnect token is canceled the response to the task doesn't matter. if (disconnectToken.IsCancellationRequested) { return; } var response = task.Result; Stream stream = response.GetStream(); var eventSource = new EventSourceStreamReader(connection, stream); var esCancellationRegistration = disconnectToken.SafeRegister(state => { _stop = true; ((IRequest)state).Abort(); }, _request); eventSource.Opened = () => { // This will noop if we're not in the reconnecting state if (connection.ChangeState(ConnectionState.Reconnecting, ConnectionState.Connected)) { // Raise the reconnect event if the connection comes back up connection.OnReconnected(); } }; eventSource.Message = sseEvent => { if (sseEvent.EventType == EventType.Data) { if (sseEvent.Data.Equals("initialized", StringComparison.OrdinalIgnoreCase)) { return; } bool shouldReconnect; bool disconnected; TransportHelper.ProcessResponse(connection, sseEvent.Data, out shouldReconnect, out disconnected, initializeInvoke); if (disconnected) { _stop = true; connection.Disconnect(); } } }; eventSource.Closed = exception => { if (exception != null) { // Check if the request is aborted bool isRequestAborted = ExceptionHelper.IsRequestAborted(exception); if (!isRequestAborted) { // Don't raise exceptions if the request was aborted (connection was stopped). connection.OnError(exception); } } requestDisposer.Dispose(); esCancellationRegistration.Dispose(); response.Dispose(); if (_stop) { AbortHandler.CompleteAbort(); } else if (AbortHandler.TryCompleteAbort()) { // Abort() was called, so don't reconnect } else { Reconnect(connection, data, disconnectToken); } }; eventSource.Start(); } }); var requestCancellationRegistration = disconnectToken.SafeRegister(state => { if (state != null) { // This will no-op if the request is already finished. ((IRequest)state).Abort(); } if (errorCallback != null) { callbackInvoker.Invoke((cb, token) => { #if !NET35 cb(new OperationCanceledException(Resources.Error_ConnectionCancelled, token)); #else cb(new OperationCanceledException(Resources.Error_ConnectionCancelled)); #endif }, errorCallback, disconnectToken); } }, _request); requestDisposer.Set(requestCancellationRegistration); }
public void Set() { _disposer.Set(_supscription); }
private void OpenConnection(IConnection connection, string data, CancellationToken disconnectToken, Action initializeCallback, Action <Exception> errorCallback) { // If we're reconnecting add /connect to the url bool reconnecting = initializeCallback == null; var callbackInvoker = new ThreadSafeInvoker(); var requestDisposer = new Disposer(); var url = (reconnecting ? connection.Url : connection.Url + "connect") + GetReceiveQueryString(connection, data); connection.Trace(TraceLevels.Events, "SSE: GET {0}", url); HttpClient.Get(url, req => { _request = req; connection.PrepareRequest(_request); _request.Accept = "text/event-stream"; }).ContinueWith(task => { if (task.IsFaulted) { Exception exception = task.Exception.Unwrap(); if (!ExceptionHelper.IsRequestAborted(exception)) { if (errorCallback != null) { callbackInvoker.Invoke((cb, ex) => cb(ex), errorCallback, exception); } else if (reconnecting) { // Only raise the error event if we failed to reconnect connection.OnError(exception); Reconnect(connection, data, disconnectToken); } } requestDisposer.Dispose(); } else { var response = task.Result; Stream stream = response.GetStream(); var eventSource = new EventSourceStreamReader(connection, stream); bool retry = true; var esCancellationRegistration = disconnectToken.SafeRegister(state => { retry = false; ((IRequest)state).Abort(); }, _request); eventSource.Opened = () => { // If we're not reconnecting, then we're starting the transport for the first time. Trigger callback only on first start. if (!reconnecting) { callbackInvoker.Invoke(initializeCallback); } else if (connection.ChangeState(ConnectionState.Reconnecting, ConnectionState.Connected)) { // Raise the reconnect event if the connection comes back up connection.OnReconnected(); } }; eventSource.Message = sseEvent => { if (sseEvent.EventType == EventType.Data) { if (sseEvent.Data.Equals("initialized", StringComparison.OrdinalIgnoreCase)) { return; } bool timedOut; bool disconnected; TransportHelper.ProcessResponse(connection, sseEvent.Data, out timedOut, out disconnected); if (disconnected) { retry = false; connection.Disconnect(); } } }; eventSource.Closed = exception => { if (exception != null) { // Check if the request is aborted bool isRequestAborted = ExceptionHelper.IsRequestAborted(exception); if (!isRequestAborted) { // Don't raise exceptions if the request was aborted (connection was stopped). connection.OnError(exception); } } requestDisposer.Dispose(); esCancellationRegistration.Dispose(); response.Dispose(); if (AbortResetEvent != null) { AbortResetEvent.Set(); } else if (retry) { Reconnect(connection, data, disconnectToken); } }; eventSource.Start(); } }); var requestCancellationRegistration = disconnectToken.SafeRegister(state => { if (state != null) { // This will no-op if the request is already finished. ((IRequest)state).Abort(); } if (errorCallback != null) { callbackInvoker.Invoke((cb, token) => { #if NET35 || WINDOWS_PHONE cb(new OperationCanceledException(Resources.Error_ConnectionCancelled)); #else cb(new OperationCanceledException(Resources.Error_ConnectionCancelled, token)); #endif }, errorCallback, disconnectToken); } }, _request); requestDisposer.Set(requestCancellationRegistration); if (errorCallback != null) { TaskAsyncHelper.Delay(ConnectionTimeout).Then(() => { callbackInvoker.Invoke((conn, cb) => { // Abort the request before cancelling _request.Abort(); // Connection timeout occurred cb(new TimeoutException()); }, connection, errorCallback); }); } }
private void PollingSetup(IConnection connection, string data, CancellationToken disconnectToken, PollingRequestHandler requestHandler) { // These are created new on each poll var reconnectInvoker = new ThreadSafeInvoker(); var requestDisposer = new Disposer(); requestHandler.ResolveUrl = () => { var url = connection.Url; if (connection.MessageId == null) { url += "connect"; } else if (IsReconnecting(connection)) { url += "reconnect"; } else { url += "poll"; } url += GetReceiveQueryString(connection, data); return(url); }; requestHandler.PrepareRequest += req => { connection.PrepareRequest(req); }; requestHandler.OnMessage += message => { var shouldReconnect = false; var disconnectedReceived = false; connection.Trace(TraceLevels.Messages, "LP: OnMessage({0})", message); TransportHelper.ProcessResponse(connection, message, out shouldReconnect, out disconnectedReceived); if (IsReconnecting(connection)) { // If the timeout for the reconnect hasn't fired as yet just fire the // event here before any incoming messages are processed TryReconnect(connection, reconnectInvoker); } if (shouldReconnect) { // Transition into reconnecting state connection.EnsureReconnecting(); } if (AbortResetEvent != null) { AbortResetEvent.Set(); } else if (disconnectedReceived) { connection.Disconnect(); } }; requestHandler.OnError += exception => { reconnectInvoker.Invoke(); // Transition into reconnecting state connection.EnsureReconnecting(); // Sometimes a connection might have been closed by the server before we get to write anything // so just try again and raise OnError. if (!ExceptionHelper.IsRequestAborted(exception) && !(exception is IOException)) { connection.OnError(exception); } else { // If we aborted purposely then we need to stop the request handler requestHandler.Stop(); } }; requestHandler.OnPolling += () => { // Capture the cleanup within a closure so it can persist through multiple requests TryDelayedReconnect(connection, reconnectInvoker); requestDisposer.Set(disconnectToken.SafeRegister(state => { reconnectInvoker.Invoke(); requestHandler.Stop(); }, null)); }; requestHandler.OnAfterPoll = exception => { requestDisposer.Dispose(); requestDisposer = new Disposer(); reconnectInvoker = new ThreadSafeInvoker(); if (exception != null) { // Delay polling by the error delay return(TaskAsyncHelper.Delay(ErrorDelay)); } return(TaskAsyncHelper.Empty); }; }
private void PollingLoop(IConnection connection, string data, CancellationToken disconnectToken, Action initializeCallback, Action <Exception> errorCallback, bool raiseReconnect = false) { string url = connection.Url; IRequest request = null; var reconnectInvoker = new ThreadSafeInvoker(); var callbackInvoker = new ThreadSafeInvoker(); var requestDisposer = new Disposer(); if (connection.MessageId == null) { url += "connect"; } else if (raiseReconnect) { url += "reconnect"; // FIX: Race if the connection is stopped and completely restarted between checking the token and calling // connection.EnsureReconnecting() if (disconnectToken.IsCancellationRequested || !connection.EnsureReconnecting()) { return; } } url += GetReceiveQueryString(connection, data); #if NET35 Debug.WriteLine(String.Format(CultureInfo.InvariantCulture, "LP: {0}", (object)url)); #else Debug.WriteLine("LP: {0}", (object)url); #endif HttpClient.PostAsync(url, req => { request = req; connection.PrepareRequest(request); }).ContinueWith(task => { bool shouldRaiseReconnect = false; bool disconnectedReceived = false; try { if (!task.IsFaulted) { if (raiseReconnect) { // If the timeout for the reconnect hasn't fired as yet just fire the // event here before any incoming messages are processed reconnectInvoker.Invoke((conn) => FireReconnected(conn), connection); } if (initializeCallback != null) { // If the timeout for connect hasn't fired as yet then just fire // the event before any incoming messages are processed callbackInvoker.Invoke(initializeCallback); } // Get the response var raw = task.Result.ReadAsString(); #if NET35 Debug.WriteLine(String.Format(CultureInfo.InvariantCulture, "LP Receive: {0}", (object)raw)); #else Debug.WriteLine("LP Receive: {0}", (object)raw); #endif ProcessResponse(connection, raw, out shouldRaiseReconnect, out disconnectedReceived); } } finally { if (disconnectedReceived) { connection.Disconnect(); } else { bool requestAborted = false; if (task.IsFaulted) { reconnectInvoker.Invoke(); // Raise the reconnect event if we successfully reconnect after failing shouldRaiseReconnect = true; // Get the underlying exception Exception exception = task.Exception.Unwrap(); // If the error callback isn't null then raise it and don't continue polling if (errorCallback != null) { callbackInvoker.Invoke((cb, ex) => cb(ex), errorCallback, exception); } else { // Figure out if the request was aborted requestAborted = ExceptionHelper.IsRequestAborted(exception); // Sometimes a connection might have been closed by the server before we get to write anything // so just try again and don't raise OnError. if (!requestAborted && !(exception is IOException)) { // Raise on error connection.OnError(exception); // If the connection is still active after raising the error event wait for 2 seconds // before polling again so we aren't hammering the server TaskAsyncHelper.Delay(ErrorDelay).Then(() => { if (!disconnectToken.IsCancellationRequested) { PollingLoop(connection, data, disconnectToken, initializeCallback: null, errorCallback: null, raiseReconnect: shouldRaiseReconnect); } }); } } } else { if (!disconnectToken.IsCancellationRequested) { // Continue polling if there was no error PollingLoop(connection, data, disconnectToken, initializeCallback: null, errorCallback: null, raiseReconnect: shouldRaiseReconnect); } } } requestDisposer.Dispose(); } }); var requestCancellationRegistration = disconnectToken.SafeRegister(req => { if (req != null) { // This will no-op if the request is already finished. req.Abort(); } // Prevent the connection state from switching to the reconnected state. reconnectInvoker.Invoke(); if (errorCallback != null) { callbackInvoker.Invoke((cb, token) => { #if NET35 || WINDOWS_PHONE cb(new OperationCanceledException(Resources.Error_ConnectionCancelled)); #else cb(new OperationCanceledException(Resources.Error_ConnectionCancelled, token)); #endif }, errorCallback, disconnectToken); } }, request); requestDisposer.Set(requestCancellationRegistration); if (initializeCallback != null) { TaskAsyncHelper.Delay(ConnectDelay).Then(() => { callbackInvoker.Invoke(initializeCallback); }); } if (raiseReconnect) { TaskAsyncHelper.Delay(ReconnectDelay).Then(() => { // Fire the reconnect event after the delay. This gives the reconnectInvoker.Invoke((conn) => FireReconnected(conn), connection); }); } }
private void OpenConnection(IConnection connection, string data, CancellationToken disconnectToken, Action initializeCallback, Action <Exception> errorCallback) { // If we're reconnecting add /connect to the url bool reconnecting = initializeCallback == null; var callbackInvoker = new ThreadSafeInvoker(); var requestDisposer = new Disposer(); var url = (reconnecting ? connection.Url : connection.Url + "connect") + GetReceiveQueryString(connection, data); IRequest request = null; #if NET35 Debug.WriteLine(String.Format(CultureInfo.InvariantCulture, "SSE: GET {0}", (object)url)); #else Debug.WriteLine("SSE: GET {0}", (object)url); #endif HttpClient.GetAsync(url, req => { request = req; connection.PrepareRequest(request); request.Accept = "text/event-stream"; }).ContinueWith(task => { if (task.IsFaulted) { Exception exception = task.Exception.Unwrap(); if (!ExceptionHelper.IsRequestAborted(exception)) { if (errorCallback != null) { callbackInvoker.Invoke((cb, ex) => cb(ex), errorCallback, exception); } else if (reconnecting) { // Only raise the error event if we failed to reconnect connection.OnError(exception); Reconnect(connection, data, disconnectToken); } } requestDisposer.Dispose(); } else { IResponse response = task.Result; Stream stream = response.GetResponseStream(); var eventSource = new EventSourceStreamReader(stream); bool retry = true; var esCancellationRegistration = disconnectToken.SafeRegister(es => { retry = false; es.Close(); }, eventSource); eventSource.Opened = () => { if (!reconnecting) { callbackInvoker.Invoke(initializeCallback); } else if (connection.ChangeState(ConnectionState.Reconnecting, ConnectionState.Connected)) { // Raise the reconnect event if the connection comes back up connection.OnReconnected(); } }; eventSource.Message = sseEvent => { if (sseEvent.EventType == EventType.Data) { if (sseEvent.Data.Equals("initialized", StringComparison.OrdinalIgnoreCase)) { return; } bool timedOut; bool disconnected; ProcessResponse(connection, sseEvent.Data, out timedOut, out disconnected); if (disconnected) { retry = false; connection.Disconnect(); } } }; eventSource.Closed = exception => { bool isRequestAborted = false; if (exception != null) { // Check if the request is aborted isRequestAborted = ExceptionHelper.IsRequestAborted(exception); if (!isRequestAborted) { // Don't raise exceptions if the request was aborted (connection was stopped). connection.OnError(exception); } } // Skip reconnect attempt for aborted requests if (!isRequestAborted && retry) { Reconnect(connection, data, disconnectToken); } }; // See http://msdn.microsoft.com/en-us/library/system.net.httpwebresponse.close.aspx eventSource.Disabled = () => { requestDisposer.Dispose(); esCancellationRegistration.Dispose(); response.Close(); }; eventSource.Start(); } }); var requestCancellationRegistration = disconnectToken.SafeRegister(req => { if (req != null) { // This will no-op if the request is already finished. req.Abort(); } if (errorCallback != null) { callbackInvoker.Invoke((cb, token) => { #if NET35 || WINDOWS_PHONE cb(new OperationCanceledException(Resources.Error_ConnectionCancelled)); #else cb(new OperationCanceledException(Resources.Error_ConnectionCancelled, token)); #endif }, errorCallback, disconnectToken); } }, request); requestDisposer.Set(requestCancellationRegistration); if (errorCallback != null) { TaskAsyncHelper.Delay(ConnectionTimeout).Then(() => { callbackInvoker.Invoke((conn, cb) => { connection.Disconnect(); // Connection timeout occurred cb(new TimeoutException()); }, connection, errorCallback); }); } }