Пример #1
0
        protected async Task BeginConnect(CancellationToken parentCancelToken)
        {
            try
            {
                using (await _lock.LockAsync().ConfigureAwait(false))
                {
                    _parentCancelToken = parentCancelToken;

                    await _taskManager.Stop().ConfigureAwait(false);

                    _taskManager.ClearException();
                    State = ConnectionState.Connecting;

                    _cancelSource  = new CancellationTokenSource();
                    CancelToken    = CancellationTokenSource.CreateLinkedTokenSource(_cancelSource.Token, parentCancelToken).Token;
                    _lastHeartbeat = DateTime.UtcNow;

                    await _engine.Connect(Host, CancelToken).ConfigureAwait(false);
                    await Run().ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                //TODO: Should this be inside the lock?
                await _taskManager.SignalError(ex).ConfigureAwait(false);

                throw;
            }
        }
Пример #2
0
        protected virtual async Task Start()
        {
            try
            {
                if (_state != (int)WebSocketState.Connecting)
                {
                    throw new InvalidOperationException("Socket is in the wrong state.");
                }

                _lastHeartbeat = DateTime.UtcNow;
                await _engine.Connect(Host, _cancelToken).ConfigureAwait(false);

                _runTask = RunTasks();
            }
            catch (Exception ex)
            {
                await DisconnectInternal(ex, isUnexpected : false).ConfigureAwait(false);

                throw;
            }
        }