Ejemplo n.º 1
0
        private void CleanupServices()
        {
            _tokenSource.Cancel();
            _tokenSource            = new CancellationTokenSource();
            _throttlers.TokenSource = _tokenSource;

            if (!_stopServices)
            {
                return;
            }
            if (!(_networkTasks?.Length > 0))
            {
                return;
            }
            if (Task.WaitAll(_networkTasks, 15000))
            {
                return;
            }

            OnFatality?.Invoke(this,
                               new OnFatalErrorEventArgs
            {
                Reason = "Fatal network error. Network services fail to shut down."
            });
            _stopServices            = false;
            _throttlers.Reconnecting = false;
            _networkServicesRunning  = false;
        }
Ejemplo n.º 2
0
        private void _webSocket_OnFatality(string reason)
        {
            Task.Run(() => OnFatality?.Invoke(reason));

            if (_options.DebugMode)
            {
                WriteLine($"WebSocket Fatality, Reason: {reason}");
            }
        }
Ejemplo n.º 3
0
        private void _webSocket_OnFatality(string reason)
        {
            Task.Run(() => { OnFatality?.Invoke(reason); });

            if (Debug)
            {
                WriteLine($"WebSocket Fatality, Reason: {reason}");
            }
        }
Ejemplo n.º 4
0
 private void On_Fatality(string reason) => OnFatality?.Invoke(reason);
Ejemplo n.º 5
0
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
        private async Task DoReconnect()
        {
#pragma warning disable 4014
            Task.Run(async() =>
            {
                _tokenSource.Cancel();
                _reconnecting = true;
                if (!Task.WaitAll(new[] { _monitorTask, _listenerTask, _senderTask }, 15000))
                {
                    // exit everything as dead...
                    OnFatality?.Invoke("Fatal network error. Network services fail to shut down.");
                    _reconnecting     = false;
                    _disconnectCalled = true;
                    _tokenSource.Cancel();
                    return;
                }
                _ws.Dispose();

                OnStateChanged?.Invoke(WebSocketState.Connecting, WebSocketState.Aborted);

                _tokenSource = new CancellationTokenSource();

                var connected = false;
                while (!_disconnectCalled && !_disposedValue && !connected && !_tokenSource.IsCancellationRequested)
                {
                    try
                    {
                        _ws = new ClientWebSocket();
                        if (!_monitorRunning)
                        {
                            StartMonitor();
                        }
                        connected = _ws.ConnectAsync(new Uri(Url), _tokenSource.Token).Wait(15000);
                    }
                    catch
                    {
                        _ws.Dispose();
                        Thread.Sleep(_reconnectStrategy.GetReconnectInterval());
                        _reconnectStrategy.ProcessValues();
                        if (_reconnectStrategy.AreAttemptsComplete())
                        {
                            // exit everything as dead...
                            OnFatality?.Invoke("Fatal network error. Max reconnect attemps reached.");
                            _reconnecting     = false;
                            _disconnectCalled = true;
                            _tokenSource.Cancel();
                            return;
                        }
                    }
                }
                if (connected)
                {
                    _reconnecting = false;
                    if (!_monitorRunning)
                    {
                        StartMonitor();
                    }
                    if (!_listenerRunning)
                    {
                        StartListener();
                    }
                    if (!_senderRunning)
                    {
                        StartSender();
                    }
                }
            });
#pragma warning restore 4014
        }
Ejemplo n.º 6
0
 private void _scClient_OnFatality(string reason) => OnFatality?.Invoke(reason);
Ejemplo n.º 7
0
 private void Socket_OnFatality(object sender, string reason)
 {
     Log($"Fatality, reason {reason}.");
     OnFatality?.Invoke(this, reason);
 }
Ejemplo n.º 8
0
#pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously
        private async Task DoReconnect()
        {
#pragma warning disable 4014
            Log("Entered reconnect.");
            Task.Run(async() =>
            {
                _tokenSource.Cancel();
                _reconnecting = true;
                if (!Task.WaitAll(new[] { _monitorTask, _listenerTask, _senderTask }, 15000))
                {
                    Log("Reconnect fatality, tasks failed to stop before the timeout.");
                    // exit everything as dead...
                    OnFatality?.Invoke("Fatal network error. Network services fail to shut down.");
                    _reconnecting     = false;
                    _disconnectCalled = true;
                    _tokenSource.Cancel();
                    return;
                }
                Log("Disposing of current websocket.");
                _ws.Dispose();

                OnStateChanged?.Invoke(WebSocketState.Connecting, WebSocketState.Aborted);

                _tokenSource = new CancellationTokenSource();

                var connected = false;
                while (!_disconnectCalled && !_disposedValue && !connected && !_tokenSource.IsCancellationRequested)
                {
                    try
                    {
                        Log("Creating new websocket.");
                        InitializeClient();
                        if (!_monitorRunning)
                        {
                            Log("Starting monitor.");
                            StartMonitor();
                        }
                        Log("Attempting connect.");
                        connected = _ws.ConnectAsync(new Uri(Url), _tokenSource.Token).Wait(15000);
                        Log($"Connect result: {connected}");
                    }
                    catch (Exception ex)
                    {
                        Log($"Reconnect threw an error: {ex.Message}.");
                        Log("Disposing of current websocket.");
                        _ws.Dispose();
                        Log("Processing reconnect strategy.");
                        Thread.Sleep(_reconnectStrategy.GetReconnectInterval());
                        _reconnectStrategy.ProcessValues();
                        if (_reconnectStrategy.AreAttemptsComplete())
                        {
                            Log("Reconnect strategy has reached max connection attempts, going to fatality.");
                            // exit everything as dead...
                            OnFatality?.Invoke("Fatal network error. Max reconnect attemps reached.");
                            _reconnecting     = false;
                            _disconnectCalled = true;
                            _tokenSource.Cancel();
                            return;
                        }
                    }
                }
                if (connected)
                {
                    Log("Reconnect success, restarting tasks.");
                    _reconnecting = false;
                    if (!_monitorRunning)
                    {
                        StartMonitor();
                    }
                    if (!_listenerRunning)
                    {
                        StartListener();
                    }
                    if (!_senderRunning)
                    {
                        StartSender();
                    }
                }
                else
                {
                    Log("Reconnect failed.");
                }
            });
#pragma warning restore 4014
        }
 private void Socket_OnFatality(string reason)
 {
     Log($"Fatality, reason {reason}.");
     OnFatality?.Invoke(reason);
 }