// TODO: this method is not Async, so it shouldn't be in AsyncCall class, but // it is reusing fair amount of code in this class, so we are leaving it here. // TODO: for other calls, you need to call Initialize, this methods calls initialize // on its own, so there's a usage inconsistency. /// <summary> /// Blocking unary request - unary response call. /// </summary> public TResponse UnaryCall(Channel channel, string methodName, TRequest msg, Metadata headers) { using (CompletionQueueSafeHandle cq = CompletionQueueSafeHandle.Create()) { byte[] payload = UnsafeSerialize(msg); unaryResponseTcs = new TaskCompletionSource <TResponse>(); lock (myLock) { Initialize(channel, cq, methodName); started = true; halfcloseRequested = true; readingDone = true; } using (var metadataArray = MetadataArraySafeHandle.Create(headers)) { call.BlockingUnary(cq, payload, unaryResponseHandler, metadataArray); } try { // Once the blocking call returns, the result should be available synchronously. return(unaryResponseTcs.Task.Result); } catch (AggregateException ae) { throw ExceptionHelper.UnwrapRpcException(ae); } } }
public TRead UnaryCall(Channel channel, String methodName, TWrite msg) { using (CompletionQueueSafeHandle cq = CompletionQueueSafeHandle.Create()) { // TODO: handle serialization error... byte[] payload = serializer(msg); unaryResponseTcs = new TaskCompletionSource <TRead>(); lock (myLock) { Initialize(channel, cq, methodName); started = true; halfcloseRequested = true; readingDone = true; } call.BlockingUnary(cq, payload, unaryResponseHandler); try { // Once the blocking call returns, the result should be available synchronously. return(unaryResponseTcs.Task.Result); } catch (AggregateException ae) { throw ExceptionHelper.UnwrapRpcException(ae); } } }
private static ReadOnlyCollection <CompletionQueueSafeHandle> CreateCompletionQueueList(GrpcEnvironment environment, int completionQueueCount) { var list = new List <CompletionQueueSafeHandle>(); for (int i = 0; i < completionQueueCount; i++) { var completionRegistry = new CompletionRegistry(environment); list.Add(CompletionQueueSafeHandle.Create(completionRegistry)); } return(list.AsReadOnly()); }
// TODO: this method is not Async, so it shouldn't be in AsyncCall class, but // it is reusing fair amount of code in this class, so we are leaving it here. /// <summary> /// Blocking unary request - unary response call. /// </summary> public TResponse UnaryCall(TRequest msg) { var profiler = Profilers.ForCurrentThread(); using (profiler.NewScope("AsyncCall.UnaryCall")) using (CompletionQueueSafeHandle cq = CompletionQueueSafeHandle.Create()) { byte[] payload = UnsafeSerialize(msg); unaryResponseTcs = new TaskCompletionSource <TResponse>(); lock (myLock) { GrpcPreconditions.CheckState(!started); started = true; Initialize(cq); halfcloseRequested = true; readingDone = true; } using (var metadataArray = MetadataArraySafeHandle.Create(details.Options.Headers)) using (var ctx = BatchContextSafeHandle.Create()) { call.StartUnary(ctx, payload, metadataArray, GetWriteFlagsForCall()); var ev = cq.Pluck(ctx.Handle); bool success = (ev.success != 0); try { using (profiler.NewScope("AsyncCall.UnaryCall.HandleBatch")) { HandleUnaryResponse(success, ctx.GetReceivedStatusOnClient(), ctx.GetReceivedMessage(), ctx.GetReceivedInitialMetadata()); } } catch (Exception e) { Logger.Error(e, "Exception occured while invoking completion delegate."); } } // Once the blocking call returns, the result should be available synchronously. // Note that GetAwaiter().GetResult() doesn't wrap exceptions in AggregateException. return(unaryResponseTcs.Task.GetAwaiter().GetResult()); } }
public void Start() { lock (myLock) { if (cq != null) { throw new InvalidOperationException("Already started."); } cq = CompletionQueueSafeHandle.Create(); for (int i = 0; i < poolSize; i++) { threads.Add(CreateAndStartThread(i)); } } }
// TODO: this method is not Async, so it shouldn't be in AsyncCall class, but // it is reusing fair amount of code in this class, so we are leaving it here. // TODO: for other calls, you need to call Initialize, this methods calls initialize // on its own, so there's a usage inconsistency. /// <summary> /// Blocking unary request - unary response call. /// </summary> public TResponse UnaryCall(Channel channel, string methodName, TRequest msg, Metadata headers) { using (CompletionQueueSafeHandle cq = CompletionQueueSafeHandle.Create()) { byte[] payload = UnsafeSerialize(msg); unaryResponseTcs = new TaskCompletionSource <TResponse>(); lock (myLock) { Initialize(channel, cq, methodName); started = true; halfcloseRequested = true; readingDone = true; } using (var metadataArray = MetadataArraySafeHandle.Create(headers)) { using (var ctx = BatchContextSafeHandle.Create()) { call.StartUnary(payload, ctx, metadataArray); var ev = cq.Pluck(ctx.Handle); bool success = (ev.success != 0); try { HandleUnaryResponse(success, ctx); } catch (Exception e) { Console.WriteLine("Exception occured while invoking completion delegate: " + e); } } } try { // Once the blocking call returns, the result should be available synchronously. return(unaryResponseTcs.Task.Result); } catch (AggregateException ae) { throw ExceptionHelper.UnwrapRpcException(ae); } } }