Esempio n. 1
0
        public async Task Open(CancellationToken ct)
        {
            if (_client != null)
            {
                throw new InvalidOperationException();
            }
            _client = _tcpClientFactory.Create(_address, false);
            var isConnected = false;

            try
            {
                _client.ReadWriteTimeout = ReadWriteTimeout;
                _client.Timeout          = Timeout;

                // Connect
                await _client.Open(ct);

                isConnected = true;

                // Authenticate with proxy server
                await Authenticate(ct);

                // Open connection to destination address through proxy server
                await Connect(_destinationAddress, ct);

                // Do we need SSL?
                _networkStream = _client.GetStream();
                if (_useSsl)
                {
                    _networkStream = await _tcpClientFactory.CreateSslStream(_networkStream, _destinationAddress.Host);
                }
            }
            catch
            {
                if (isConnected)
                {
                    _client.Close();
                }
                _client = null;
                throw;
            }
        }
        public async Task Open(CancellationToken cancellationToken)
        {
            _tcpClient = new TcpClient();
            if (ReadWriteTimeout.HasValue)
            {
                _tcpClient.ReceiveTimeout = ReadWriteTimeout.Value;
                _tcpClient.SendTimeout    = ReadWriteTimeout.Value;
            }
            var connectTask = _tcpClient.ConnectAsync(_destinationAddress.Host, _destinationAddress.Port);

            if (Timeout.HasValue)
            {
                connectTask = connectTask.HandleCancellation(new CancellationTokenSource(Timeout.Value).Token);
            }
            await connectTask;

            _networkStream = _tcpClient.GetStream();
            if (_useSsl)
            {
                _networkStream = await _factory.CreateSslStream(_networkStream, _destinationAddress.Host);
            }
        }