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); } }
/// <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); }
/// <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(); } }
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); } }
/// <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); }
/// <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(); }
/// <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); } })); }
/// <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); } })); }
/// <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); }
/// <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); }
/// <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); } }
/// <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); } }
/// <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); } }
/// <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); } }
/// <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); } }
static extern StatusCode grpcsharp_batch_context_recv_status_on_client_status(BatchContextSafeHandleNotOwned ctx);
static extern IntPtr grpcsharp_batch_context_recv_status_on_client_details(BatchContextSafeHandleNotOwned ctx); // returns const char*
static extern IntPtr grpcsharp_batch_context_recv_status_on_client_details(BatchContextSafeHandleNotOwned ctx);
static extern int grpcsharp_batch_context_recv_close_on_server_cancelled(BatchContextSafeHandleNotOwned ctx);
static extern IntPtr grpcsharp_batch_context_server_rpc_new_method(BatchContextSafeHandleNotOwned ctx);
static extern CallSafeHandle grpcsharp_batch_context_server_rpc_new_call(BatchContextSafeHandleNotOwned ctx);
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); } }
static extern void grpcsharp_batch_context_recv_message_to_buffer(BatchContextSafeHandleNotOwned ctx, byte[] buffer, UIntPtr bufferLen);
static extern IntPtr grpcsharp_batch_context_recv_message_length(BatchContextSafeHandleNotOwned ctx);
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); } }
static extern IntPtr grpcsharp_batch_context_server_rpc_new_method(BatchContextSafeHandleNotOwned ctx); // returns const char*