Пример #1
0
 public async Task <IConnection> ConnectAsync(Uri uri, TimeSpan timeout)
 {
     return(new BufferedConnection(await _connectionInitiator.ConnectAsync(uri, timeout), _flushTimeout, _writeBufferSize));
 }
Пример #2
0
        public async Task <IConnection> EstablishConnectionAsync(TimeSpan timeout)
        {
            TimeoutHelper timeoutHelper             = new TimeoutHelper(timeout);
            IConnection   localRawConnection        = null;
            IConnection   localUpgradedConnection   = null;
            bool          localIsConnectionFromPool = true;

            // first try and use a connection from our pool (and use it if we successfully receive an ACK)
            while (localIsConnectionFromPool)
            {
                localRawConnection = this.TakeConnection(timeoutHelper.RemainingTime());
                if (localRawConnection == null)
                {
                    localIsConnectionFromPool = false;
                }
                else
                {
                    bool preambleSuccess = false;
                    try
                    {
                        localUpgradedConnection = await AcceptPooledConnectionAsync(localRawConnection, ref timeoutHelper);

                        preambleSuccess = true;
                        break;
                    }
                    catch (CommunicationException)
                    {
                        // CommunicationException is ok since it was a cached connection of unknown state
                    }
                    catch (TimeoutException)
                    {
                        // ditto for TimeoutException
                    }
                    finally
                    {
                        if (!preambleSuccess)
                        {
                            // This cannot throw TimeoutException since isConnectionStillGood is false (doesn't attempt a Close).
                            _connectionPool.ReturnConnection(_connectionKey, localRawConnection, false, TimeSpan.Zero);
                        }
                    }
                }
            }

            // if there isn't anything in the pool, we need to use a new connection
            if (!localIsConnectionFromPool)
            {
                bool     success        = false;
                TimeSpan connectTimeout = timeoutHelper.RemainingTime();
                try
                {
                    try
                    {
                        localRawConnection = await _connectionInitiator.ConnectAsync(_via, connectTimeout);
                    }
                    catch (TimeoutException e)
                    {
                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateNewConnectionTimeoutException(
                                                                                      connectTimeout, e));
                    }

                    _connectionInitiator    = null;
                    localUpgradedConnection = await AcceptPooledConnectionAsync(localRawConnection, ref timeoutHelper);

                    success = true;
                }
                finally
                {
                    if (!success)
                    {
                        _connectionKey = null;
                        if (localRawConnection != null)
                        {
                            localRawConnection.Abort();
                        }
                    }
                }
            }

            SnapshotConnection(localUpgradedConnection, localRawConnection, localIsConnectionFromPool);

            return(localUpgradedConnection);
        }