예제 #1
0
        internal async Task <bool> InternalDoConnectSocket(string hostname, int port, bool throwOnError)
        {
            var index = Interlocked.Increment(ref _counter);

            // read and write threads should not be running at this point
            if (_readThreadRunning)
            {
                throw new Exception("Read thread should not be running");
            }
            if (_writeThreadRunning)
            {
                throw new Exception("Write thread should not be running");
            }

            var result = await _socketHolder.Connect(hostname, port, index, throwOnError).ConfigureAwait(false);

            if (!result)
            {
                // Interlocked.Decrement(ref _counter);
                return(false);
            }

            if (LogAdapter.IsDebugEnabled)
            {
                LogAdapter.LogDebug(LogSource, "Connected socket to " + hostname + " port " + port);
            }

            DrainCommandsAndResetErrorState();

            if (_threadCancelSource != null)
            {
                if (LogAdapter.IsDebugEnabled)
                {
                    LogAdapter.LogDebug(LogSource, "Disposing existing cancellation token source");
                }

                _threadCancelSource.Dispose();
            }

            _threadCancelSource = new CancellationTokenSource();
            _threadCancelToken  = _threadCancelSource.Token;

            _socketHolder.WireStreams(_threadCancelToken, OnSocketClosed);

            _amqpWriter.Initialize(_socketHolder.Writer);
            _amqpReader.Initialize(_socketHolder.Reader);
            _frameReader.Initialize(_socketHolder.Reader, _amqpReader, this);

            ThreadFactory.BackgroundThread(WriteFramesLoop, WriteFrameThreadNamePrefix + index);
            ThreadFactory.BackgroundThread(ReadFramesLoop, ReadFrameThreadNamePrefix + index);

            return(true);
        }
예제 #2
0
        internal async Task <bool> InternalDoConnectSocket(string hostname, int port, bool throwOnError)
        {
            var index = Interlocked.Increment(ref _counter);

            var result = await _socketHolder.Connect(hostname, port, index, throwOnError).ConfigureAwait(false);

            if (!throwOnError && !result)
            {
                Interlocked.Decrement(ref _counter);
                return(false);
            }

            // Reset state
            _lastError = null;
            while (!_commandOutbox.IsEmpty)
            {
                CommandToSend cmd;
                _commandOutbox.TryDequeue(out cmd);
            }

            if (_threadCancelSource != null)
            {
                _threadCancelSource.Dispose();
            }

            _threadCancelSource = new CancellationTokenSource();
            _threadCancelToken  = _threadCancelSource.Token;

            _socketHolder.WireStreams(_threadCancelToken, OnSocketClosed);

            _amqpWriter.Initialize(_socketHolder.Writer);
            _amqpReader.Initialize(_socketHolder.Reader);
            _frameReader.Initialize(_socketHolder.Reader, _amqpReader, this);

            ThreadFactory.BackgroundThread(WriteFramesLoop, "WriteFramesLoop_" + index);
            ThreadFactory.BackgroundThread(ReadFramesLoop, "ReadFramesLoop_" + index);

            return(true);
        }