public static VerifyLastActive ( IConnection connection ) : bool | ||
connection | IConnection | |
리턴 | bool |
// fire and forget private async void DoReconnect() { var reconnectUrl = UrlBuilder.BuildReconnect(_connection, Name, _connectionData); while (TransportHelper.VerifyLastActive(_connection) && _connection.EnsureReconnecting()) { try { await PerformConnect(reconnectUrl); break; } catch (OperationCanceledException) { break; } catch (Exception ex) { if (ExceptionHelper.IsRequestAborted(ex)) { break; } _connection.OnError(ex); } await Task.Delay(ReconnectDelay); } }
// internal for testing internal async Task Reconnect(IConnection connection, string connectionData) { var reconnectUrl = UrlBuilder.BuildReconnect(connection, Name, connectionData); while (TransportHelper.VerifyLastActive(connection) && connection.EnsureReconnecting() && !_disconnectToken.IsCancellationRequested) { try { await StartWebSocket(connection, reconnectUrl); if (connection.ChangeState(ConnectionState.Reconnecting, ConnectionState.Connected)) { connection.OnReconnected(); } break; } catch (OperationCanceledException) { break; } catch (Exception ex) { connection.OnError(ex); } await Task.Delay(ReconnectDelay); } }
private void Reconnect(IConnection connection, string data, CancellationToken disconnectToken) { // Need to verify before the task delay occurs because an application sleep could occur during the delayed duration. if (!TransportHelper.VerifyLastActive(connection)) { return; } // Wait for a bit before reconnecting TaskAsyncHelper.Delay(ReconnectDelay).Then(() => { if (!TransportHelper.VerifyLastActive(connection)) { return; } // FIX: Race if Connection is stopped and completely restarted between checking the token and calling // connection.EnsureReconnecting() if (!disconnectToken.IsCancellationRequested && connection.EnsureReconnecting()) { // Now attempt a reconnect OpenConnection(connection, data, disconnectToken, initializeCallback: null, errorCallback: null); } }); }
// internal virtual for testing internal virtual void OnError(IConnection connection, Exception exception) { // Prevent the reconnecting -> connected transition from happening if it hasn't already. _reconnectInvoker.Invoke(); if (TryFailStart(exception)) { return; } if (!TransportHelper.VerifyLastActive(connection)) { StopPolling(); } else { // Do the connected -> reconnecting transition if it hasn't already occurred. 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); } }
private async void DoReconnect() { while (TransportHelper.VerifyLastActive(_connectionInfo.Connection) && _connectionInfo.Connection.EnsureReconnecting()) { try { await PerformConnect(reconnecting : true); break; } catch (OperationCanceledException) { break; } catch (Exception ex) { if (ExceptionHelper.IsRequestAborted(ex)) { break; } _connectionInfo.Connection.OnError(ex); } await Task.Delay(ReconnectDelay); } }
// fire and forget private async void DoReconnect() { try { var reconnectUrl = UrlBuilder.BuildReconnect(_connection, Name, _connectionData); while (TransportHelper.VerifyLastActive(_connection) && _connection.EnsureReconnecting()) { try { await PerformConnect(reconnectUrl, _disconnectToken); break; } catch (OperationCanceledException) { return; } catch (Exception ex) { if (ExceptionHelper.IsRequestAborted(ex)) { return; } _connection.OnError(ex); } await Task.Delay(ReconnectDelay); } var linkedToken = CreateLinkedCancellationToken(); try { await _webSocketHandler.ProcessWebSocketRequestAsync(_webSocket, linkedToken); } catch { // Ignore any errors from ProcessWebSocketRequestAsync just as OnStart does after the init message is received. // Any errors other than one thrown from the final CloseAsync is reported via OnError(Exception). } } catch (Exception ex) { _connection.Trace(TraceLevels.Events, "WS DoReconnect() failed: {0}", ex); } }
public void VerifyLastActiveSetsLastErrorIfConnectionExpired() { var mockConnection = new Mock <IConnection>(); mockConnection.Setup(c => c.LastActiveAt).Returns(new DateTime(1)); mockConnection.Setup(c => c.ReconnectWindow).Returns(new TimeSpan(42)); var connection = mockConnection.Object; Assert.False(TransportHelper.VerifyLastActive(connection)); var expectedMessage = string.Format(CultureInfo.CurrentCulture, Resources.Error_ReconnectWindowTimeout, connection.LastActiveAt, connection.ReconnectWindow); mockConnection.Verify(c => c.Stop(It.Is <TimeoutException>(e => e.Message == expectedMessage))); }
// internal virtual for testing internal virtual void OnError(IConnection connection, Exception exception) { TransportFailed(exception); _reconnectInvoker.Invoke(); if (!TransportHelper.VerifyLastActive(connection)) { StopPolling(); } // 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); } }
private void PollingSetup(IConnection connection, string data, CancellationToken disconnectToken, PollingRequestHandler requestHandler, Action onInitialized) { // reconnectInvoker is created new on each poll var reconnectInvoker = new ThreadSafeInvoker(); var disconnectRegistration = disconnectToken.SafeRegister(state => { reconnectInvoker.Invoke(); requestHandler.Stop(); }, null); requestHandler.ResolveUrl = () => { var url = connection.Url; if (connection.MessageId == null) { url += "connect"; connection.Trace(TraceLevels.Events, "LP Connect: {0}", url); } else if (IsReconnecting(connection)) { url += "reconnect"; connection.Trace(TraceLevels.Events, "LP Reconnect: {0}", url); } else { url += "poll"; connection.Trace(TraceLevels.Events, "LP Poll: {0}", url); } 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, onInitialized); 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 (disconnectedReceived) { connection.Trace(TraceLevels.Messages, "Disconnect command received from server."); connection.Disconnect(); } }; requestHandler.OnError += exception => { reconnectInvoker.Invoke(); if (!TransportHelper.VerifyLastActive(connection)) { return; } // 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 { requestHandler.Stop(); } }; requestHandler.OnPolling += () => { // Capture the cleanup within a closure so it can persist through multiple requests TryDelayedReconnect(connection, reconnectInvoker); }; requestHandler.OnAfterPoll = exception => { if (AbortHandler.TryCompleteAbort()) { // Abort() was called, so don't reconnect requestHandler.Stop(); } else { reconnectInvoker = new ThreadSafeInvoker(); if (exception != null) { // Delay polling by the error delay return(TaskAsyncHelper.Delay(ErrorDelay)); } } return(TaskAsyncHelper.Empty); }; requestHandler.OnAbort += _ => { disconnectRegistration.Dispose(); // Complete any ongoing calls to Abort() // If someone calls Abort() later, have it no-op AbortHandler.CompleteAbort(); }; }