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); }
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); }
internal async Task InternalDoConnectSocket(string hostname, int port) { var index = Interlocked.Increment(ref _counter); await _socketHolder.Connect(hostname, port, OnSocketClosed, index); _amqpWriter = new AmqpPrimitivesWriter(_socketHolder.Writer, null, null); _amqpReader = new AmqpPrimitivesReader(_socketHolder.Reader); _frameReader = new FrameReader(_socketHolder.Reader, _amqpReader, this); var t1 = new Thread(WriteFramesLoop) { IsBackground = true, Name = "WriteFramesLoop_" + index }; t1.Start(); var t2 = new Thread(ReadFramesLoop) { IsBackground = true, Name = "ReadFramesLoop_" + index }; t2.Start(); }