Beispiel #1
0
        /// <summary>
        /// Handles the native callback.
        /// </summary>
        private void HandleNewServerRpc(GRPCOpError error, IntPtr batchContextPtr)
        {
            try
            {
                var ctx = new BatchContextSafeHandleNotOwned(batchContextPtr);

                if (error != GRPCOpError.GRPC_OP_OK)
                {
                    // 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);
            }
        }
Beispiel #2
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);
            }
        }
Beispiel #3
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);
            }
        }
Beispiel #4
0
        private void HandleHalfclosed(GRPCOpError error, IntPtr batchContextPtr)
        {
            try
            {
                lock (myLock)
                {
                    halfclosed = true;

                    ReleaseResourcesIfPossible();
                }

                if (error != GRPCOpError.GRPC_OP_OK)
                {
                    halfcloseTcs.SetException(new Exception("Halfclose failed"));
                }
                else
                {
                    halfcloseTcs.SetResult(null);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Caught exception in a native handler: " + e);
            }
        }
Beispiel #5
0
        private void HandleWriteFinished(GRPCOpError error, IntPtr batchContextPtr)
        {
            try
            {
                TaskCompletionSource <object> oldTcs = null;
                lock (myLock)
                {
                    oldTcs   = writeTcs;
                    writeTcs = null;
                }

                if (errorOccured)
                {
                    // TODO: use the right type of exception...
                    oldTcs.SetException(new Exception("Write failed"));
                }
                else
                {
                    // TODO: where does the continuation run?
                    oldTcs.SetResult(null);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Caught exception in a native handler: " + e);
            }
        }
Beispiel #6
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);
            }
        }
Beispiel #7
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);
            }
        }
Beispiel #8
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);
            }
        }
Beispiel #9
0
 private void Handler(GRPCOpError op, IntPtr ptr)
 {
     counter++;
 }
Beispiel #10
0
 private void Handler(GRPCOpError op, IntPtr ptr)
 {
     counter++;
 }
Beispiel #11
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);
            }
        }