コード例 #1
0
ファイル: ServerCallHandler.cs プロジェクト: dinara92/grpc
        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);
        }
コード例 #2
0
        public async Task HandleCall(ServerRpcNew newRpc, GrpcEnvironment environment)
        {
            var asyncCall = new AsyncCallServer <TRequest, TResponse>(
                method.ResponseMarshaller.Serializer,
                method.RequestMarshaller.Deserializer,
                environment, newRpc.Server);

            asyncCall.Initialize(newRpc.Call);
            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, asyncCall.Peer, 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;
                responseTuple = Tuple.Create(response, HandlerUtils.GetWriteFlags(context.WriteOptions));
            }
            catch (Exception e)
            {
                if (!(e is RpcException))
                {
                    Logger.Warning(e, "Exception occured in handler.");
                }
                status = HandlerUtils.StatusFromException(e);
            }
            try
            {
                await asyncCall.SendStatusFromServerAsync(status, context.ResponseTrailers, responseTuple).ConfigureAwait(false);
            }
            catch (Exception)
            {
                asyncCall.Cancel();
                throw;
            }
            await finishedTask.ConfigureAwait(false);
        }
コード例 #3
0
ファイル: ServerCallHandler.cs プロジェクト: txl0591/grpc1
        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);
        }
コード例 #4
0
        public async Task HandleCall(ServerRpcNew newRpc, GrpcEnvironment environment)
        {
            var asyncCall = new AsyncCallServer <TRequest, TResponse>(
                method.ResponseMarshaller.Serializer,
                method.RequestMarshaller.Deserializer,
                environment, newRpc.Server);

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

            Status status;
            var    context = HandlerUtils.NewContext(newRpc, asyncCall.Peer, responseStream, asyncCall.CancellationToken);

            try
            {
                await handler(requestStream, responseStream, context).ConfigureAwait(false);

                status = context.Status;
            }
            catch (Exception e)
            {
                if (!(e is RpcException))
                {
                    Logger.Warning(e, "Exception occured in handler.");
                }
                status = HandlerUtils.StatusFromException(e);
            }
            try
            {
                await asyncCall.SendStatusFromServerAsync(status, context.ResponseTrailers, null).ConfigureAwait(false);
            }
            catch (Exception)
            {
                asyncCall.Cancel();
                throw;
            }
            await finishedTask.ConfigureAwait(false);
        }
コード例 #5
0
        public async Task <bool> MoveNext(CancellationToken token)
        {
            var cancellationTokenRegistration = token.CanBeCanceled ? token.Register(() => call.Cancel()) : (IDisposable)null;

            using (cancellationTokenRegistration)
            {
                var result = await call.ReadMessageAsync().ConfigureAwait(false);

                this.current = result;
                return(result != null);
            }
        }