Пример #1
0
        /// <summary>
        /// 处理响应转化
        /// </summary>
        /// <typeparam name="TRes"></typeparam>
        /// <typeparam name="TPara"></typeparam>
        /// <param name="res"></param>
        /// <param name="tPara"></param>
        /// <param name="func"></param>
        /// <returns></returns>
        public static ListResp <TRes> WithResp <TRes, TPara>(this ListResp <TRes> res, ListResp <TPara> tPara,
                                                             Func <TPara, TRes> func)
        {
            WithResp(res, tPara.sys_ret, tPara.ret, tPara.msg);

            res.data = tPara.data?.Select(func).ToList();

            return(res);
        }
        public override async Task <ListResp> List(ListReq request, ServerCallContext context)
        {
            var listPersistentSubscriptionsSource = new TaskCompletionSource <ListResp>();
            var correlationId = Guid.NewGuid();

            var user = context.GetHttpContext().User;

            if (!await _authorizationProvider.CheckAccessAsync(user,
                                                               GetInfoOperation, context.CancellationToken).ConfigureAwait(false))
            {
                throw RpcExceptions.AccessDenied();
            }

            var streamId = string.Empty;

            switch (request.Options.ListOptionCase)
            {
            case ListReq.Types.Options.ListOptionOneofCase.ListAllSubscriptions:
                _publisher.Publish(new MonitoringMessage.GetAllPersistentSubscriptionStats(
                                       new CallbackEnvelope(HandleListSubscriptionsCompleted)));
                break;

            case ListReq.Types.Options.ListOptionOneofCase.ListForStream:
                streamId = request.Options.ListForStream.StreamOptionCase switch {
                    ListReq.Types.StreamOption.StreamOptionOneofCase.All => "$all",
                    ListReq.Types.StreamOption.StreamOptionOneofCase.Stream => request.Options.ListForStream.Stream,
                    _ => throw new InvalidOperationException()
                };
                _publisher.Publish(new MonitoringMessage.GetStreamPersistentSubscriptionStats(
                                       new CallbackEnvelope(HandleListSubscriptionsCompleted),
                                       streamId
                                       ));
                break;

            default:
                throw new InvalidOperationException();
            }

            return(await listPersistentSubscriptionsSource.Task.ConfigureAwait(false));

            void HandleListSubscriptionsCompleted(Message message)
            {
                if (message is ClientMessage.NotHandled notHandled && RpcExceptions.TryHandleNotHandled(notHandled, out var ex))
                {
                    listPersistentSubscriptionsSource.TrySetException(ex);
                    return;
                }

                if (message is MonitoringMessage.GetPersistentSubscriptionStatsCompleted completed)
                {
                    switch (completed.Result)
                    {
                    case MonitoringMessage.GetPersistentSubscriptionStatsCompleted.OperationStatus.Success:
                        var listResp = new ListResp();
                        listResp.Subscriptions.AddRange(
                            completed.SubscriptionStats.Select(ParseSubscriptionInfo)
                            );
                        listPersistentSubscriptionsSource.TrySetResult(listResp);
                        return;

                    case MonitoringMessage.GetPersistentSubscriptionStatsCompleted.OperationStatus.NotFound:
                        listPersistentSubscriptionsSource.TrySetException(
                            RpcExceptions.PersistentSubscriptionDoesNotExist(streamId, ""));
                        return;

                    case MonitoringMessage.GetPersistentSubscriptionStatsCompleted.OperationStatus.NotReady:
                        listPersistentSubscriptionsSource.TrySetException(
                            RpcExceptions.ServerNotReady());
                        return;

                    case MonitoringMessage.GetPersistentSubscriptionStatsCompleted.OperationStatus.Fail:
                        listPersistentSubscriptionsSource.TrySetException(
                            RpcExceptions.PersistentSubscriptionFailed(streamId, "", completed.ErrorString));
                        return;

                    default:
                        listPersistentSubscriptionsSource.TrySetException(
                            RpcExceptions.UnknownError(completed.Result));
                        return;
                    }
                }
                listPersistentSubscriptionsSource.TrySetException(
                    RpcExceptions.UnknownMessage <MonitoringMessage.GetPersistentSubscriptionStatsCompleted>(
                        message));
            }
        }