Ejemplo n.º 1
0
        public async Task HandleCall(ServerRpcNew newRpc, CompletionQueueSafeHandle cq)
        {
            var asyncCall = new AsyncCallServer <TRequest, TResponse>(
                method.ResponseMarshaller.ContextualSerializer,
                method.RequestMarshaller.ContextualDeserializer,
                newRpc.Server);

            asyncCall.Initialize(newRpc.Call, cq);
            var finishedTask   = asyncCall.ServerSideCallAsync();
            var requestStream  = new ServerRequestStream <TRequest, TResponse>(asyncCall);
            var responseStream = new ServerResponseStream <TRequest, TResponse>(asyncCall);

            Status status;

            AsyncCallServer <TRequest, TResponse> .ResponseWithFlags?responseWithFlags = null;
            var context = HandlerUtils.NewContext(newRpc, responseStream, asyncCall.CancellationToken);

            try
            {
                GrpcPreconditions.CheckArgument(await requestStream.MoveNext().ConfigureAwait(false));
                var request  = requestStream.Current;
                var response = await handler(request, context).ConfigureAwait(false);

                status            = context.Status;
                responseWithFlags = new AsyncCallServer <TRequest, TResponse> .ResponseWithFlags(response, HandlerUtils.GetWriteFlags(context.WriteOptions));
            }
            catch (Exception e)
            {
                if (!(e is RpcException))
                {
                    Logger.Warning(e, "Exception occurred in the handler or an interceptor.");
                }
                status = HandlerUtils.GetStatusFromExceptionAndMergeTrailers(e, context.ResponseTrailers);
            }
            try
            {
                await asyncCall.SendStatusFromServerAsync(status, context.ResponseTrailers, responseWithFlags).ConfigureAwait(false);
            }
            catch (Exception)
            {
                asyncCall.Cancel();
                throw;
            }
            await finishedTask.ConfigureAwait(false);
        }
Ejemplo n.º 2
0
        public async Task HandleCall(ServerRpcNew newRpc, CompletionQueueSafeHandle cq)
        {
            var asyncCall = new AsyncCallServer <TRequest, TResponse>(
                method.ResponseMarshaller.Serializer,
                method.RequestMarshaller.Deserializer,
                newRpc.Server);

            asyncCall.Initialize(newRpc.Call, cq);
            var finishedTask   = asyncCall.ServerSideCallAsync();
            var requestStream  = new ServerRequestStream <TRequest, TResponse>(asyncCall);
            var responseStream = new ServerResponseStream <TRequest, TResponse>(asyncCall);

            Status status;
            Tuple <TResponse, WriteFlags> responseTuple = null;
            var context = HandlerUtils.NewContext(newRpc, responseStream, asyncCall.CancellationToken);

            try
            {
                var response = await handler(requestStream, context).ConfigureAwait(false);

                status        = context.Status;
                responseTuple = Tuple.Create(response, HandlerUtils.GetWriteFlags(context.WriteOptions));
            }
            catch (Exception e)
            {
                if (!(e is RpcException))
                {
                    Logger.Warning(e, "Exception occured in handler.");
                }
                status = HandlerUtils.GetStatusFromExceptionAndMergeTrailers(e, context.ResponseTrailers);
            }

            try
            {
                await asyncCall.SendStatusFromServerAsync(status, context.ResponseTrailers, responseTuple).ConfigureAwait(false);
            }
            catch (Exception)
            {
                asyncCall.Cancel();
                throw;
            }
            await finishedTask.ConfigureAwait(false);
        }