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); }; }