Exemple #1
0
        public void Stop()
        {
            if (!_isRunning)
            {
                return;
            }

            _isRunning = false;

            try
            {
                foreach (var conn in _connections.Values.ToArray())
                {
                    DisconnectInternal(conn);
                }

                if (_isListening)
                {
                    _isListening = false;
                    _listener.Stop();
                }

                _tokenSource.Cancel();

                _connectionPool.Reset();
                _connections.Clear();
            }
            catch (Exception e)
            {
                Log.Exception(e);
            }
        }
Exemple #2
0
        async void ReceiveLoop()
        {
            while (true)
            {
                try
                {
                    var msg     = _receiveBuffer.Next();
                    var success = await msg.ReadFromStream(_stream, _token).ConfigureAwait(false);

                    if (!success)
                    {
                        break;
                    }

                    Log.Verbose?.Invoke($"received {msg.Size} bytes.");

                    _eventQueue.Enqueue(new ClientEvent
                    {
                        type = EventType.DataReceived,
                        data = msg.Payload
                    });
                }
                catch (ObjectDisposedException)
                {
                    break;
                }
                catch (TaskCanceledException)
                {
                    break;
                }
                catch (OperationCanceledException)
                {
                    break;
                }
                catch (Exception e)
                {
                    Log.Exception(e);
                    break;
                }
            }

            DisconnectInternal();
        }