示例#1
0
文件: AsyncCall.cs 项目: varung/grpc
        /// <summary>
        /// Handler for unary response completion.
        /// </summary>
        private void HandleUnaryResponse(bool wasError, BatchContextSafeHandleNotOwned ctx)
        {
            lock (myLock)
            {
                finished   = true;
                halfclosed = true;

                ReleaseResourcesIfPossible();
            }

            if (wasError)
            {
                unaryResponseTcs.SetException(new RpcException(new Status(StatusCode.Internal, "Internal error occured.")));
                return;
            }

            var status = ctx.GetReceivedStatus();

            if (status.StatusCode != StatusCode.OK)
            {
                unaryResponseTcs.SetException(new RpcException(status));
                return;
            }

            // TODO: handle deserialization error
            TResponse msg;

            TryDeserialize(ctx.GetReceivedMessage(), out msg);

            unaryResponseTcs.SetResult(msg);
        }
示例#2
0
        /// <summary>
        /// Handles streaming read completion.
        /// </summary>
        private void HandleReadFinished(bool wasError, BatchContextSafeHandleNotOwned ctx)
        {
            var payload = ctx.GetReceivedMessage();

            lock (myLock)
            {
                readPending = false;
                if (payload == null)
                {
                    readingDone = true;
                }

                ReleaseResourcesIfPossible();
            }

            // TODO: handle the case when error occured...

            if (payload != null)
            {
                // TODO: handle deserialization error
                TRead msg;
                TryDeserialize(payload, out msg);

                FireReadObserverOnNext(msg);

                // Start a new read. The current one has already been delivered,
                // so correct ordering of reads is assured.
                StartReceiveMessage();
            }
            else
            {
                CompleteReadObserver();
            }
        }
示例#3
0
        /// <summary>
        /// Handler for unary response completion.
        /// </summary>
        private void HandleUnaryResponse(GRPCOpError error, IntPtr batchContextPtr)
        {
            try
            {
                TaskCompletionSource <TRead> tcs;
                lock (myLock)
                {
                    finished   = true;
                    halfclosed = true;
                    tcs        = unaryResponseTcs;

                    ReleaseResourcesIfPossible();
                }

                var ctx = new BatchContextSafeHandleNotOwned(batchContextPtr);

                if (error != GRPCOpError.GRPC_OP_OK)
                {
                    tcs.SetException(new RpcException(
                                         new Status(StatusCode.Internal, "Internal error occured.")
                                         ));
                    return;
                }

                var status = ctx.GetReceivedStatus();
                if (status.StatusCode != StatusCode.OK)
                {
                    tcs.SetException(new RpcException(status));
                    return;
                }

                // TODO: handle deserialize error...
                var msg = deserializer(ctx.GetReceivedMessage());
                tcs.SetResult(msg);
            }
            catch (Exception e)
            {
                Console.WriteLine("Caught exception in a native handler: " + e);
            }
        }
示例#4
0
        /// <summary>
        /// Handles streaming read completion.
        /// </summary>
        private void HandleReadFinished(bool success, BatchContextSafeHandleNotOwned ctx)
        {
            var payload = ctx.GetReceivedMessage();

            AsyncCompletionDelegate <TRead> origCompletionDelegate = null;

            lock (myLock)
            {
                origCompletionDelegate = readCompletionDelegate;
                if (payload != null)
                {
                    readCompletionDelegate = null;
                }
                else
                {
                    // This was the last read. Keeping the readCompletionDelegate
                    // to be either fired by this handler or by client-side finished
                    // handler.
                    readingDone = true;
                }

                ReleaseResourcesIfPossible();
            }

            // TODO: handle the case when error occured...

            if (payload != null)
            {
                // TODO: handle deserialization error
                TRead msg;
                TryDeserialize(payload, out msg);

                FireCompletion(origCompletionDelegate, msg, null);
            }
            else
            {
                ProcessLastRead(origCompletionDelegate);
            }
        }
示例#5
0
        private void HandleReadFinished(GRPCOpError error, IntPtr batchContextPtr)
        {
            try
            {
                var ctx     = new BatchContextSafeHandleNotOwned(batchContextPtr);
                var payload = ctx.GetReceivedMessage();

                TaskCompletionSource <TRead> oldTcs   = null;
                IObserver <TRead>            observer = null;

                Nullable <Status> status = null;

                lock (myLock)
                {
                    oldTcs  = readTcs;
                    readTcs = null;
                    if (payload == null)
                    {
                        readingDone = true;
                    }
                    observer = readObserver;
                    status   = finishedStatus;

                    ReleaseResourcesIfPossible();
                }

                // TODO: wrap deserialization...
                TRead msg = payload != null?deserializer(payload) : default(TRead);

                oldTcs.SetResult(msg);

                // TODO: make sure we deliver reads in the right order.

                if (observer != null)
                {
                    if (payload != null)
                    {
                        // TODO: wrap to handle exceptions
                        observer.OnNext(msg);

                        // start a new read
                        ReceiveMessageAsync();
                    }
                    else
                    {
                        if (!server)
                        {
                            if (status.HasValue)
                            {
                                CompleteStreamObserver(status.Value);
                            }
                        }
                        else
                        {
                            // TODO: wrap to handle exceptions..
                            observer.OnCompleted();
                        }
                        // TODO: completeStreamObserver serverside...
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Caught exception in a native handler: " + e);
            }
        }