コード例 #1
0
        public async Task ConnectAsync()
        {
            ThrowIfDisposed();

            await _sync.WaitAsync().ConfigureAwait(false);

            IList <IOp> opsReceivedWhileConnecting;

            try
            {
                if (IsConnected)
                {
                    return;
                }

                DoSafeRelease();

                _cancellation = new CancellationTokenSource();

                (_connection, opsReceivedWhileConnecting) =
                    await _connectionManager.OpenConnectionAsync(_connectionInfo, _cancellation.Token).ConfigureAwait(false);

                _consumer = Task.Factory
                            .StartNew(
                    Worker,
                    _cancellation.Token,
                    TaskCreationOptions.LongRunning,
                    TaskScheduler.Default)
                            .ContinueWith(async t =>
                {
                    if (_isDisposed)
                    {
                        return;
                    }

                    await _sync.WaitAsync().ConfigureAwait(false);

                    try
                    {
                        if (_isDisposed)
                        {
                            return;
                        }

                        if (!t.IsFaulted)
                        {
                            return;
                        }

                        DoSafeRelease();

                        _eventMediator.Emit(new ClientDisconnected(this, DisconnectReason.DueToFailure));

                        var ex = t.Exception?.GetBaseException() ?? t.Exception;
                        if (ex == null)
                        {
                            return;
                        }

                        _logger.Error("Internal client worker exception.", ex);

                        _eventMediator.Emit(new ClientWorkerFailed(this, ex));
                    }
                    catch (Exception ex)
                    {
                        _logger.Error("Unhandled exception while ending worker.", ex);
                    }
                    finally
                    {
                        _sync?.Release();
                    }
                });

                _eventMediator.Emit(new ClientConnected(this));

                if (opsReceivedWhileConnecting.Any())
                {
                    _opMediator.Emit(opsReceivedWhileConnecting);
                }

                await DoSubAsync(_subscriptions.Values.Select(i => i.SubscriptionInfo).ToArray()).ConfigureAwait(false);
            }
            finally
            {
                _sync.Release();
            }
        }
コード例 #2
0
        public void Connect()
        {
            ThrowIfDisposed();

            if (IsConnected)
            {
                return;
            }

            using (_sync.Lock())
            {
                if (IsConnected)
                {
                    return;
                }

                DoSafeRelease();

                _cancellation = new CancellationTokenSource();

                var connectionResult = _connectionManager.OpenConnection(
                    _connectionInfo,
                    _cancellation.Token);

                _connection = connectionResult.Item1;

                var opsReceivedWhileConnecting = connectionResult.Item2;

                _eventMediator.Emit(new ClientConnected(this));

                foreach (var op in opsReceivedWhileConnecting)
                {
                    _opMediator.Emit(op);
                }

                foreach (var subscription in _subscriptions.Values)
                {
                    if (!IsConnected)
                    {
                        break;
                    }

                    DoSub(subscription.SubscriptionInfo);
                }

                _consumer = Task.Factory
                            .StartNew(
                    Worker,
                    _cancellation.Token,
                    TaskCreationOptions.LongRunning,
                    TaskScheduler.Default)
                            .ContinueWith(t =>
                {
                    if (!t.IsFaulted)
                    {
                        return;
                    }

                    DoSafeRelease();

                    _eventMediator.Emit(new ClientDisconnected(this, DisconnectReason.DueToFailure));

                    var ex = t.Exception?.GetBaseException() ?? t.Exception;
                    if (ex == null)
                    {
                        return;
                    }

                    _logger.Error("Internal client worker exception.", ex);

                    _eventMediator.Emit(new ClientWorkerFailed(this, ex));
                });
            }
        }