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
            {
                var result = await handler(requestStream, context).ConfigureAwait(false);

                status = context.Status;
                try
                {
                    await responseStream.WriteAsync(result).ConfigureAwait(false);
                }
                catch (OperationCanceledException)
                {
                    status = Status.DefaultCancelled;
                }
            }
            catch (Exception e)
            {
                Logger.Error(e, "Exception occured in handler.");
                status = HandlerUtils.StatusFromException(e);
            }

            try
            {
                await responseStream.WriteStatusAsync(status, context.ResponseTrailers).ConfigureAwait(false);
            }
            catch (OperationCanceledException)
            {
                // Call has been already cancelled.
            }
            await finishedTask.ConfigureAwait(false);
        }
Exemple #2
0
        public async Task HandleCall(string methodName, CallSafeHandle call, GrpcEnvironment environment)
        {
            var asyncCall = new AsyncCallServer <TRequest, TResponse>(
                method.ResponseMarshaller.Serializer,
                method.RequestMarshaller.Deserializer,
                environment);

            asyncCall.Initialize(call);
            var finishedTask   = asyncCall.ServerSideCallAsync();
            var requestStream  = new ServerRequestStream <TRequest, TResponse>(asyncCall);
            var responseStream = new ServerResponseStream <TRequest, TResponse>(asyncCall);
            var context        = new ServerCallContext(); // TODO(jtattermusch): initialize the context

            Status status = Status.DefaultSuccess;

            try
            {
                var result = await handler(context, requestStream);

                try
                {
                    await responseStream.WriteAsync(result);
                }
                catch (OperationCanceledException)
                {
                    status = Status.DefaultCancelled;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception occured in handler: " + e);
                status = HandlerUtils.StatusFromException(e);
            }

            try
            {
                await responseStream.WriteStatusAsync(status);
            }
            catch (OperationCanceledException)
            {
                // Call has been already cancelled.
            }
            await finishedTask;
        }
        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
            {
                Preconditions.CheckArgument(await requestStream.MoveNext().ConfigureAwait(false));
                var request = requestStream.Current;
                // TODO(jtattermusch): we need to read the full stream so that native callhandle gets deallocated.
                Preconditions.CheckArgument(!await requestStream.MoveNext().ConfigureAwait(false));
                var result = await handler(request, context).ConfigureAwait(false);

                status = context.Status;
                await responseStream.WriteAsync(result).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                Logger.Error(e, "Exception occured in handler.");
                status = HandlerUtils.StatusFromException(e);
            }
            try
            {
                await responseStream.WriteStatusAsync(status, context.ResponseTrailers).ConfigureAwait(false);
            }
            catch (OperationCanceledException)
            {
                // Call has been already cancelled.
            }
            await finishedTask.ConfigureAwait(false);
        }
        public async Task HandleCall(string methodName, CallSafeHandle call, CompletionQueueSafeHandle cq)
        {
            var asyncCall = new AsyncCallServer <TRequest, TResponse>(
                method.ResponseMarshaller.Serializer,
                method.RequestMarshaller.Deserializer);

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

            Status status = Status.DefaultSuccess;

            try
            {
                Preconditions.CheckArgument(await requestStream.MoveNext());
                var request = requestStream.Current;
                // TODO(jtattermusch): we need to read the full stream so that native callhandle gets deallocated.
                Preconditions.CheckArgument(!await requestStream.MoveNext());
                var context = new ServerCallContext();  // TODO(jtattermusch): initialize the context
                var result  = await handler(context, request);

                await responseStream.WriteAsync(result);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception occured in handler: " + e);
                status = HandlerUtils.StatusFromException(e);
            }
            try
            {
                await responseStream.WriteStatusAsync(status);
            }
            catch (OperationCanceledException)
            {
                // Call has been already cancelled.
            }
            await finishedTask;
        }