Exemple #1
0
        public async void CancelCurrentAction()
        {
            await this.semaphore.WaitAsync();

            if (!this.currentTask.IsCompleted)
            {
                this.cancellationTokenSource.Cancel();
                Logger.Trace("Cancellation requested");
            }
            else
            {
                Logger.Trace($"No cancellation - operation [{this.actionCounter}] is finished");
            }

            this.semaphore.Release();
        }
        private async Task <bool> InternalAssertConnected(CancellationToken cancellationToken)
        {
            if (this.client != null &&
                this.wsClient != null &&
                this.wsClient.IsConnected &&
                !this.receiveMessagesCts.IsCancellationRequested)
            {
                Logger.Trace("Already connected");
                return(false);
            }

            var retryCount = 0;

            do
            {
                try
                {
                    this.receiveMessagesCts.Cancel();
                    await this.messageLoopTask;
                    Logger.Trace("Message loop task finished");
                    this.receiveMessagesCts = new CancellationTokenSource();

                    this.client   = new WebSocketClient(this.options);
                    this.wsClient = await this.client.ConnectAsync(this.Endpoint, cancellationToken);

                    this.messageLoopTask = Task.Run(() => ReceiveMessagesLoop(this.receiveMessagesCts.Token), cancellationToken);

                    Logger.Log("Connected to server");
                    return(true);
                }
                catch (OperationCanceledException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    Logger.Warn($"Connection failed, retry {++retryCount}/10");
                    Logger.Debug($"{ex}");
                    await Task.Delay(2000, cancellationToken);
                }
            } while (retryCount < 10);

            throw new WebsocketConnectionFailedException();
        }
 public void Debug(string message, Exception error = null)
 {
     Logger.Trace($"{message} {error}");
 }