コード例 #1
0
ファイル: MainHandler.cs プロジェクト: PixelBattles/Hub
        public async Task <IBattleHandlerSubscription> GetBattleHandlerAndSubscribeAsync(long battleId)
        {
            while (true)
            {
                var battleHandlerLazy = _battleHandlers.GetOrAdd(
                    key: battleId,
                    valueFactory: key => new AsyncLazy <BattleHandler>(
                        factory: () => _battleHandlerFactory.CreateBattleHandlerAsync(battleId),
                        flags: AsyncLazyFlags.RetryOnFailure));

                var battleHandler = await battleHandlerLazy;

                if (battleHandler == null)
                {
                    throw new InvalidOperationException($"Battle {battleId} is not found.");
                }

                battleHandler.IncrementSubscriptionCounter();
                if (battleHandler.IsClosing)
                {
                    battleHandler.DecrementSubscriptionCounter();
                    continue;
                }
                var subscription = new BattleHandlerSubscription(battleHandler);
                return(subscription);
            }
        }
コード例 #2
0
        public async override Task OnDisconnectedAsync(Exception exception)
        {
            foreach (var subscription in Subscriptions.Values)
            {
                subscription.Dispose();
            }
            Subscriptions.Clear();
            BattleHandlerSubscription.Dispose();

            await base.OnDisconnectedAsync(exception);
        }