Exemplo n.º 1
0
        private void HandleFinishedServerside(GRPCOpError error, IntPtr batchContextPtr)
        {
            try
            {
                var ctx = new BatchContextSafeHandleNotOwned(batchContextPtr);

                lock (myLock)
                {
                    finished = true;

                    // TODO: because of the way server calls are implemented, we need to set
                    // reading done to true here. Should be fixed in the future.
                    readingDone = true;

                    ReleaseResourcesIfPossible();
                }
                // TODO: handle error ...

                finishedServersideTcs.SetResult(null);
            }
            catch (Exception e)
            {
                Console.WriteLine("Caught exception in a native handler: " + e);
            }
        }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 3
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();
            }
        }
Exemplo n.º 4
0
        private void HandleFinished(GRPCOpError error, IntPtr batchContextPtr)
        {
            try
            {
                var ctx    = new BatchContextSafeHandleNotOwned(batchContextPtr);
                var status = ctx.GetReceivedStatus();

                bool wasReadingDone;

                lock (myLock)
                {
                    finished       = true;
                    finishedStatus = status;

                    wasReadingDone = readingDone;

                    ReleaseResourcesIfPossible();
                }

                if (wasReadingDone)
                {
                    CompleteStreamObserver(status);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Caught exception in a native handler: " + e);
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Handles the server side close completion.
        /// </summary>
        private void HandleFinishedServerside(bool wasError, BatchContextSafeHandleNotOwned ctx)
        {
            lock (myLock)
            {
                finished = true;

                ReleaseResourcesIfPossible();
            }
            // TODO: handle error ...

            finishedServersideTcs.SetResult(null);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Handles receive status completion for calls with streaming response.
        /// </summary>
        private void HandleFinished(bool wasError, BatchContextSafeHandleNotOwned ctx)
        {
            var status = ctx.GetReceivedStatus();

            lock (myLock)
            {
                finished       = true;
                finishedStatus = status;

                ReleaseResourcesIfPossible();
            }

            CompleteReadObserver();
        }
Exemplo n.º 7
0
 /// <summary>
 /// Creates completion callback delegate that wraps the batch completion handler in a try catch block to
 /// prevent propagating exceptions accross managed/unmanaged boundary.
 /// </summary>
 protected CompletionCallbackDelegate CreateBatchCompletionCallback(Action <bool, BatchContextSafeHandleNotOwned> handler)
 {
     return(new CompletionCallbackDelegate((success, batchContextPtr) =>
     {
         try
         {
             var ctx = new BatchContextSafeHandleNotOwned(batchContextPtr);
             handler(success, ctx);
         }
         catch (Exception e)
         {
             Console.WriteLine("Caught exception in a native handler: " + e);
         }
     }));
 }
Exemplo n.º 8
0
 /// <summary>
 /// Creates completion callback delegate that wraps the batch completion handler in a try catch block to
 /// prevent propagating exceptions accross managed/unmanaged boundary.
 /// </summary>
 protected CompletionCallbackDelegate CreateBatchCompletionCallback(Action <bool, BatchContextSafeHandleNotOwned> handler)
 {
     return(new CompletionCallbackDelegate((error, batchContextPtr) =>
     {
         try
         {
             var ctx = new BatchContextSafeHandleNotOwned(batchContextPtr);
             bool wasError = (error != GRPCOpError.GRPC_OP_OK);
             handler(wasError, ctx);
         }
         catch (Exception e)
         {
             Console.WriteLine("Caught exception in a native handler: " + e);
         }
     }));
 }
Exemplo n.º 9
0
        /// <summary>
        /// Handles receive status completion for calls with streaming response.
        /// </summary>
        private void HandleFinished(bool wasError, BatchContextSafeHandleNotOwned ctx)
        {
            var status = ctx.GetReceivedStatus();

            AsyncCompletionDelegate <TResponse> origReadCompletionDelegate = null;

            lock (myLock)
            {
                finished       = true;
                finishedStatus = status;

                origReadCompletionDelegate = readCompletionDelegate;

                ReleaseResourcesIfPossible();
            }

            ProcessLastRead(origReadCompletionDelegate);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Handles the server side close completion.
        /// </summary>
        private void HandleFinishedServerside(bool success, BatchContextSafeHandleNotOwned ctx)
        {
            bool cancelled = ctx.GetReceivedCloseOnServerCancelled();

            lock (myLock)
            {
                finished = true;

                if (cancelled)
                {
                    // Once we cancel, we don't have to care that much
                    // about reads and writes.
                    Cancel();
                }

                ReleaseResourcesIfPossible();
            }
            // TODO(jtattermusch): handle error

            finishedServersideTcs.SetResult(null);
        }
Exemplo n.º 11
0
        /// <summary>
        /// Handles send completion.
        /// </summary>
        private void HandleSendFinished(bool success, BatchContextSafeHandleNotOwned ctx)
        {
            AsyncCompletionDelegate <object> origCompletionDelegate = null;

            lock (myLock)
            {
                origCompletionDelegate = sendCompletionDelegate;
                sendCompletionDelegate = null;

                ReleaseResourcesIfPossible();
            }

            if (!success)
            {
                FireCompletion(origCompletionDelegate, null, new OperationFailedException("Send failed"));
            }
            else
            {
                FireCompletion(origCompletionDelegate, null, null);
            }
        }
Exemplo n.º 12
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);
            }
        }
Exemplo n.º 13
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);
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Handles halfclose completion.
        /// </summary>
        private void HandleHalfclosed(bool wasError, BatchContextSafeHandleNotOwned ctx)
        {
            AsyncCompletionDelegate origCompletionDelegate = null;

            lock (myLock)
            {
                halfclosed             = true;
                origCompletionDelegate = sendCompletionDelegate;
                sendCompletionDelegate = null;

                ReleaseResourcesIfPossible();
            }

            if (wasError)
            {
                FireCompletion(origCompletionDelegate, new OperationFailedException("Halfclose failed"));
            }
            else
            {
                FireCompletion(origCompletionDelegate, null);
            }
        }
Exemplo n.º 15
0
        /// <summary>
        /// Handles the native callback.
        /// </summary>
        private void HandleNewServerRpc(bool success, IntPtr batchContextPtr)
        {
            try
            {
                var ctx = new BatchContextSafeHandleNotOwned(batchContextPtr);

                // TODO: handle error

                CallSafeHandle call = ctx.GetServerRpcNewCall();
                string method = ctx.GetServerRpcNewMethod();

                // after server shutdown, the callback returns with null call
                if (!call.IsInvalid)
                {
                    Task.Run(async () => await InvokeCallHandler(call, method));
                }

                AllowOneRpc();
            }
            catch (Exception e)
            {
                Console.WriteLine("Caught exception in a native handler: " + e);
            }
        }
Exemplo n.º 16
0
 static extern StatusCode grpcsharp_batch_context_recv_status_on_client_status(BatchContextSafeHandleNotOwned ctx);
Exemplo n.º 17
0
 static extern IntPtr grpcsharp_batch_context_recv_status_on_client_details(BatchContextSafeHandleNotOwned ctx);  // returns const char*
Exemplo n.º 18
0
 static extern StatusCode grpcsharp_batch_context_recv_status_on_client_status(BatchContextSafeHandleNotOwned ctx);
Exemplo n.º 19
0
 static extern IntPtr grpcsharp_batch_context_recv_status_on_client_details(BatchContextSafeHandleNotOwned ctx);
Exemplo n.º 20
0
 static extern int grpcsharp_batch_context_recv_close_on_server_cancelled(BatchContextSafeHandleNotOwned ctx);
Exemplo n.º 21
0
 static extern IntPtr grpcsharp_batch_context_server_rpc_new_method(BatchContextSafeHandleNotOwned ctx);
Exemplo n.º 22
0
 static extern CallSafeHandle grpcsharp_batch_context_server_rpc_new_call(BatchContextSafeHandleNotOwned ctx);
Exemplo n.º 23
0
 static extern CallSafeHandle grpcsharp_batch_context_server_rpc_new_call(BatchContextSafeHandleNotOwned ctx);
Exemplo n.º 24
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);
            }
        }
Exemplo n.º 25
0
 static extern int grpcsharp_batch_context_recv_close_on_server_cancelled(BatchContextSafeHandleNotOwned ctx);
Exemplo n.º 26
0
 static extern void grpcsharp_batch_context_recv_message_to_buffer(BatchContextSafeHandleNotOwned ctx, byte[] buffer, UIntPtr bufferLen);
Exemplo n.º 27
0
 static extern IntPtr grpcsharp_batch_context_recv_message_length(BatchContextSafeHandleNotOwned ctx);
Exemplo n.º 28
0
 static extern IntPtr grpcsharp_batch_context_recv_message_length(BatchContextSafeHandleNotOwned ctx);
Exemplo n.º 29
0
 static extern void grpcsharp_batch_context_recv_message_to_buffer(BatchContextSafeHandleNotOwned ctx, byte[] buffer, UIntPtr bufferLen);
Exemplo n.º 30
0
        private void HandleNewServerRpc(GRPCOpError error, IntPtr batchContextPtr)
        {
            try
            {
                var ctx = new BatchContextSafeHandleNotOwned(batchContextPtr);

                if (error != GRPCOpError.GRPC_OP_OK)
                {
                    // TODO: handle error
                }

                var rpcInfo = new NewRpcInfo(ctx.GetServerRpcNewCall(), ctx.GetServerRpcNewMethod());

                // after server shutdown, the callback returns with null call
                if (!rpcInfo.Call.IsInvalid)
                {
                    newRpcQueue.Add(rpcInfo);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Caught exception in a native handler: " + e);
            }
        }
Exemplo n.º 31
0
 static extern IntPtr grpcsharp_batch_context_server_rpc_new_method(BatchContextSafeHandleNotOwned ctx);  // returns const char*