Example #1
0
        private async Task BeginConnect(string email, string password, string token = null)
        {
            try
            {
                using (await _connectionLock.LockAsync().ConfigureAwait(false))
                {
                    await Disconnect().ConfigureAwait(false);

                    _taskManager.ClearException();

                    Stopwatch stopwatch = null;
                    if (Config.LogLevel >= LogSeverity.Verbose)
                    {
                        _connectionStopwatch.Restart();
                        stopwatch = Stopwatch.StartNew();
                    }
                    State = ConnectionState.Connecting;
                    _disconnectedEvent.Reset();

                    var cancelSource = new CancellationTokenSource();
                    CancelToken           = cancelSource.Token;
                    ClientAPI.CancelToken = CancelToken;
                    StatusAPI.CancelToken = CancelToken;

                    await Login(email, password, token).ConfigureAwait(false);

                    await GatewaySocket.Connect(ClientAPI, CancelToken).ConfigureAwait(false);

                    var tasks = new[] { CancelToken.Wait() }
                    .Concat(MessageQueue.Run(CancelToken));

                    await _taskManager.Start(tasks, cancelSource).ConfigureAwait(false);

                    GatewaySocket.WaitForConnection(CancelToken);

                    if (Config.LogLevel >= LogSeverity.Verbose)
                    {
                        stopwatch.Stop();
                        double seconds = Math.Round(stopwatch.ElapsedTicks / (double)TimeSpan.TicksPerSecond, 2);
                        Logger.Verbose($"Handshake + Ready took {seconds} sec");
                    }
                }
            }
            catch (Exception ex)
            {
                await _taskManager.SignalError(ex).ConfigureAwait(false);

                throw;
            }
        }
Example #2
0
        private async Task BeginGatewayConnect()
        {
            try
            {
                using (await _connectionLock.LockAsync().ConfigureAwait(false))
                {
                    await Disconnect().ConfigureAwait(false);

                    _taskManager.ClearException();

                    ClientAPI.Token = Service.Client.ClientAPI.Token;

                    Stopwatch stopwatch = null;
                    if (_config.LogLevel >= LogSeverity.Verbose)
                    {
                        stopwatch = Stopwatch.StartNew();
                    }
                    _gatewayState = ConnectionState.Connecting;

                    var cancelSource = new CancellationTokenSource();
                    CancelToken           = cancelSource.Token;
                    ClientAPI.CancelToken = CancelToken;

                    await GatewaySocket.Connect(ClientAPI, CancelToken).ConfigureAwait(false);

                    await _taskManager.Start(new Task[0], cancelSource).ConfigureAwait(false);

                    GatewaySocket.WaitForConnection(CancelToken);

                    if (_config.LogLevel >= LogSeverity.Verbose)
                    {
                        stopwatch.Stop();
                        double seconds = Math.Round(stopwatch.ElapsedTicks / (double)TimeSpan.TicksPerSecond, 2);
                        Logger.Verbose($"Connection took {seconds} sec");
                    }
                }
            }
            catch (Exception ex)
            {
                await _taskManager.SignalError(ex).ConfigureAwait(false);

                throw;
            }
        }