示例#1
0
        public virtual async Task ConnectAsync(string hostname, int port, bool ssl, bool validateCertificate)
        {
            try
            {
                Host = hostname;
                Port = port;
                Ssl = ssl;

#if WINDOWS_PHONE
                _Connection = new WindowsNetSockets(hostname, port, ssl);
#else
                _Connection = new SystemNetTransport(hostname, port, ssl, validateCertificate);
#endif
                _Stream = new BufferedReadStream(await _Connection.ConnectAsync());

                OnConnected(await GetResponseAsync());

                IsConnected = true;
            }
            catch (Exception)
            {
                IsConnected = false;
                Utilities.TryDispose(_Stream);
                throw;
            }
        }
示例#2
0
        private async Task ConnectAsync(CancellationToken token)
        {
            if (_transport.State == TransportState.Closed)
            {
                await m_mutex.WaitAsync().ConfigureAwait(false);

                try
                {
                    if (_transport.State == TransportState.Closed)
                    {
                        await _transport.ConnectAsync(token).ConfigureAwait(false);

                        if (_optionsAccessor.Password != null)
                        {
                            var response = await ExecuteAsync(new Request(CommandType.Auth, _optionsAccessor.Password), token).ConfigureAwait(false);

                            VerifyAuthentication(response);
                        }
                    }
                }
                finally
                {
                    m_mutex.Release();
                }
            }
        }
示例#3
0
        public virtual async Task ConnectAsync(string hostname, int port, bool ssl, bool validateCertificate)
        {
            try
            {
                Host = hostname;
                Port = port;
                Ssl  = ssl;

#if WINDOWS_PHONE
                _Connection = new WindowsNetSockets(hostname, port, ssl);
#else
                _Connection = new SystemNetTransport(hostname, port, ssl, validateCertificate);
#endif
                _Stream = new BufferedReadStream(await _Connection.ConnectAsync());

                OnConnected(await GetResponseAsync());

                IsConnected = true;
            }
            catch (Exception)
            {
                IsConnected = false;
                Utilities.TryDispose(_Stream);
                throw;
            }
        }
示例#4
0
        /// <summary>
        ///     Connect.
        /// </summary>
        public async Task <MTProtoConnectResult> Connect(CancellationToken cancellationToken)
        {
            var result = MTProtoConnectResult.Other;

            await Task.Run(async() =>
            {
                using (await _lock.LockAsync(cancellationToken))
                {
                    if (_state == MTProtoConnectionState.Connected)
                    {
                        result = MTProtoConnectResult.Success;
                        return;
                    }
                    Debug.Assert(_state == MTProtoConnectionState.Disconnected);
                    try
                    {
                        _state = MTProtoConnectionState.Connecting;
                        Log.Debug("Connecting...");

                        await _transport.ConnectAsync(cancellationToken).ToObservable().Timeout(DefaultConnectTimeout);

                        _connectionCts = new CancellationTokenSource();
                        _connectionCancellationToken = _connectionCts.Token;

                        Log.Debug("Connected.");
                        result = MTProtoConnectResult.Success;
                    }
                    catch (TimeoutException)
                    {
                        result = MTProtoConnectResult.Timeout;
                        Log.Debug(string.Format("Failed to connect due to timeout ({0}s).", DefaultConnectTimeout.TotalSeconds));
                    }
                    catch (Exception e)
                    {
                        result = MTProtoConnectResult.Other;
                        Log.Debug(e, "Failed to connect.");
                    }
                    finally
                    {
                        switch (result)
                        {
                        case MTProtoConnectResult.Success:
                            _state = MTProtoConnectionState.Connected;
                            break;

                        case MTProtoConnectResult.Timeout:
                        case MTProtoConnectResult.Other:
                            _state = MTProtoConnectionState.Disconnected;
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }
                }
            }, cancellationToken).ConfigureAwait(false);

            return(result);
        }
示例#5
0
文件: Client.cs 项目: chao1573/io
        public void Connect(Action <bool> callback)
        {
            m_transport.ConnectAsync($"{Host}:{Port}", (success) =>
            {
                if (success)
                {
                    m_heartbeatService = new HeartBeatService(3000, this);
                    m_heartbeatService.Start();
                }

                callback?.Invoke(success);
            });
        }