Пример #1
0
        public async Task Connect()
        {
            try
            {
                using (await _connectionLock.LockAsync().ConfigureAwait(false))
                {
                    await Disconnect().ConfigureAwait(false);

                    _taskManager.ClearException();
                    _disconnectedEvent.Reset();

                    var cancelSource = new CancellationTokenSource();
                    CancelToken = cancelSource.Token;
                    var tasks = new Task[] { CancelToken.Wait(), TwitchClientMessageQueue.RunSendQueue(CancelToken), TwitchWhisperMessageQueue.RunSendQueue(CancelToken) };
                    await _taskManager.Start(tasks, cancelSource).ConfigureAwait(false);
                    await ConnectClient().ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                await _taskManager.SignalError(ex).ConfigureAwait(false);

                throw;
            }
        }
Пример #2
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;
            }
        }
Пример #3
0
        //Closes the UDP socket when _disconnectToken is triggered, since UDPClient doesn't allow passing a canceltoken
        private async Task WatcherAsync()
        {
            await CancelToken.Wait().ConfigureAwait(false);

            _udp.Close();
        }
Пример #4
0
        //Closes the UDP socket when _disconnectToken is triggered, since UDPClient doesn't allow passing a canceltoken
        private async Task WatcherAsync()
        {
            await CancelToken.Wait();

            _udp.Close();
        }
Пример #5
0
 //Closes the UDP socket when _disconnectToken is triggered, since UDPClient doesn't allow passing a canceltoken
 private Task WatcherAsync()
 => CancelToken.Wait().ContinueWith(_ => _udp.Close());