Beispiel #1
0
        private void Dispose(bool disposing)
        {
            if (_isDisposed || !disposing)
            {
                return;
            }

            Try.All(
                () =>
            {
                _readStream?.Dispose();
                _readStream = null;
            },
                () =>
            {
                _writeStream?.Dispose();
                _writeStream = null;
            },
                () =>
            {
                _socket?.Shutdown(SocketShutdown.Both);
                _socket?.Dispose();
                _socket = null;
            },
                () =>
            {
                _writeStreamSync?.Dispose();
                _writeStreamSync = null;
            });

            _reader = null;
            _writer = null;
        }
Beispiel #2
0
        internal NatsConnection(
            NatsServerInfo serverInfo,
            Socket socket,
            Stream stream,
            CancellationToken cancellationToken)
        {
            ServerInfo = serverInfo ?? throw new ArgumentNullException(nameof(serverInfo));

            _socket = socket ?? throw new ArgumentNullException(nameof(socket));
            if (!socket.Connected)
            {
                throw new ArgumentException("Socket is not connected.", nameof(socket));
            }

            _stream            = stream ?? throw new ArgumentNullException(nameof(stream));
            _writeStream       = new BufferedStream(_stream, socket.SendBufferSize);
            _readStream        = new BufferedStream(_stream, socket.ReceiveBufferSize);
            _cancellationToken = cancellationToken;
            _writeStreamSync   = new SemaphoreSlim(1, 1);
            _writer            = new NatsStreamWriter(_writeStream, _cancellationToken);
            _reader            = new NatsOpStreamReader(_readStream);

            _socketIsConnected = () => _socket?.Connected == true;
            _canRead           = () => _socket?.Connected == true && _stream != null && _stream.CanRead && !_cancellationToken.IsCancellationRequested;
        }
Beispiel #3
0
        internal NatsConnection(
            NatsServerInfo serverInfo,
            Socket socket,
            BufferedStream writeStream,
            BufferedStream readStream,
            NatsOpStreamReader reader,
            CancellationToken cancellationToken)
        {
            EnsureArg.IsNotNull(serverInfo, nameof(serverInfo));
            EnsureArg.IsNotNull(socket, nameof(socket));
            EnsureArg.IsNotNull(writeStream, nameof(writeStream));
            EnsureArg.IsNotNull(readStream, nameof(readStream));
            EnsureArg.IsNotNull(reader, nameof(reader));

            if (!socket.Connected)
            {
                throw new ArgumentException("Socket is not connected.", nameof(socket));
            }

            ServerInfo = serverInfo;

            _socket            = socket;
            _writeStreamSync   = new Locker();
            _writeStream       = writeStream;
            _readStream        = readStream;
            _reader            = reader;
            _cancellationToken = cancellationToken;

            _writer = new NatsStreamWriter(_writeStream, ServerInfo.MaxPayload, _cancellationToken);

            _socketIsConnected = () => _socket != null && _socket.Connected;
            _canRead           = () => _socketIsConnected() && _readStream != null && _readStream.CanRead && !_cancellationToken.IsCancellationRequested;
        }
Beispiel #4
0
        public void Dispose()
        {
            ThrowIfDisposed();

            _isDisposed = true;

            var exs = new List <Exception>();

            void TryDispose(IDisposable disposable)
            {
                try
                {
                    disposable.Dispose();
                }
                catch (Exception ex)
                {
                    exs.Add(ex);
                }
            }

            TryDispose(_writeStream);
            TryDispose(_readStream);
            TryDispose(_stream);
            try
            {
                _socket.Shutdown(SocketShutdown.Both);
            }
            catch (Exception ex)
            {
                exs.Add(ex);
            }
            TryDispose(_socket);
            TryDispose(_writeStreamSync);

            _writeStream     = null;
            _readStream      = null;
            _stream          = null;
            _socket          = null;
            _writeStreamSync = null;
            _reader          = null;
            _writer          = null;

            if (exs.Any())
            {
                throw new AggregateException("Failed while disposing connection. See inner exception(s) for more details.", exs);
            }
        }