Ejemplo n.º 1
0
 public void RegisterCompletionQueue(CompletionQueueSafeHandle cq)
 {
     using (cq.NewScope())
     {
         Native.grpcsharp_server_register_completion_queue(this, cq);
     }
 }
Ejemplo n.º 2
0
 public void ShutdownAndNotify(BatchCompletionDelegate callback, CompletionQueueSafeHandle completionQueue)
 {
     using (completionQueue.NewScope())
     {
         var ctx = BatchContextSafeHandle.Create();
         completionQueue.CompletionRegistry.RegisterBatchCompletion(ctx, callback);
         Native.grpcsharp_server_shutdown_and_notify_callback(this, completionQueue, ctx);
     }
 }
        private void RunBody()
        {
            var completionRegistry = new CompletionRegistry(Environment, () => Environment.BatchContextPool.Lease(), () => throw new NotImplementedException());
            var cq   = CompletionQueueSafeHandle.CreateAsync(completionRegistry);
            var call = CreateFakeCall(cq);

            var sendCompletionCallback = new NopSendCompletionCallback();
            var payload    = new byte[PayloadSize];
            var writeFlags = default(WriteFlags);

            for (int i = 0; i < Iterations; i++)
            {
                call.StartSendMessage(sendCompletionCallback, payload, writeFlags, false);
                var callback = completionRegistry.Extract(completionRegistry.LastRegisteredKey);
                callback.OnComplete(true);
            }
            cq.Dispose();
        }
Ejemplo n.º 4
0
        public void StartCall(string methodName, CallSafeHandle call, CompletionQueueSafeHandle cq)
        {
            var asyncCall = new AsyncCall <TResponse, TRequest>(
                method.ResponseMarshaller.Serializer,
                method.RequestMarshaller.Deserializer);

            asyncCall.InitializeServer(call);

            var finishedTask = asyncCall.ServerSideUnaryRequestCallAsync();

            var request = asyncCall.ReceiveMessageAsync().Result;

            var responseObserver = new ServerStreamingOutputObserver <TResponse, TRequest>(asyncCall);

            handler(request, responseObserver);

            finishedTask.Wait();
        }
Ejemplo n.º 5
0
        public void Setup()
        {
            var native = NativeMethods.Get();

            // nop the native-call via reflection
            NativeMethods.Delegates.grpcsharp_call_send_status_from_server_delegate nop = (CallSafeHandle call, BatchContextSafeHandle ctx, StatusCode statusCode, byte[] statusMessage, UIntPtr statusMessageLen, MetadataArraySafeHandle metadataArray, int sendEmptyInitialMetadata, byte[] optionalSendBuffer, UIntPtr optionalSendBufferLen, WriteFlags writeFlags) => {
                completionRegistry.Extract(ctx.Handle).OnComplete(true); // drain the dictionary as we go
                return(CallError.OK);
            };
            native.GetType().GetField(nameof(native.grpcsharp_call_send_status_from_server)).SetValue(native, nop);

            environment        = GrpcEnvironment.AddRef();
            metadata           = MetadataArraySafeHandle.Create(Metadata.Empty);
            completionRegistry = new CompletionRegistry(environment, () => environment.BatchContextPool.Lease(), () => throw new NotImplementedException());
            var cq = CompletionQueueSafeHandle.CreateAsync(completionRegistry);

            call = CreateFakeCall(cq);
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Selects corresponding handler for given call and handles the call.
 /// </summary>
 private async Task HandleCallAsync(ServerRpcNew newRpc, CompletionQueueSafeHandle cq, Action <Server, CompletionQueueSafeHandle> continuation)
 {
     try
     {
         IServerCallHandler callHandler;
         if (!callHandlers.TryGetValue(newRpc.Method, out callHandler))
         {
             callHandler = UnimplementedMethodCallHandler.Instance;
         }
         await callHandler.HandleCall(newRpc, cq).ConfigureAwait(false);
     }
     catch (Exception e)
     {
         Logger.Warning(e, "Exception while handling RPC.");
     }
     finally
     {
         continuation(this, cq);
     }
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Creates a channel that connects to a specific host.
        /// Port will default to 80 for an unsecure channel or to 443 for a secure channel.
        /// </summary>
        /// <param name="target">Target of the channel.</param>
        /// <param name="credentials">Credentials to secure the channel.</param>
        /// <param name="options">Channel options.</param>
        public Channel(string target, ChannelCredentials credentials, IEnumerable <ChannelOption> options) : base(target)
        {
            this.options = CreateOptionsDictionary(options);
            EnsureUserAgentChannelOption(this.options);
            this.environment = GrpcEnvironment.AddRef();

            this.completionQueue = this.environment.PickCompletionQueue();
            using (var nativeChannelArgs = ChannelOptions.CreateChannelArgs(this.options.Values))
            {
                var nativeCredentials = credentials.ToNativeCredentials();
                if (nativeCredentials != null)
                {
                    this.handle = ChannelSafeHandle.CreateSecure(nativeCredentials, target, nativeChannelArgs);
                }
                else
                {
                    this.handle = ChannelSafeHandle.CreateInsecure(target, nativeChannelArgs);
                }
            }
            GrpcEnvironment.RegisterChannel(this);
        }
Ejemplo n.º 8
0
        private void ThreadBody(int iterations, int payloadSize)
        {
            var completionRegistry = new CompletionRegistry(environment, () => environment.BatchContextPool.Lease());
            var cq   = CompletionQueueSafeHandle.CreateAsync(completionRegistry);
            var call = CreateFakeCall(cq);

            var sendCompletionCallback = new NopSendCompletionCallback();
            var payload    = new byte[payloadSize];
            var writeFlags = default(WriteFlags);

            var stopwatch = Stopwatch.StartNew();

            for (int i = 0; i < iterations; i++)
            {
                call.StartSendMessage(sendCompletionCallback, payload, writeFlags, false);
                var callback = completionRegistry.Extract(completionRegistry.LastRegisteredKey);
                callback.OnComplete(true);
            }
            stopwatch.Stop();
            Console.WriteLine("Elapsed millis: " + stopwatch.ElapsedMilliseconds);

            cq.Dispose();
        }
Ejemplo n.º 9
0
        private void RunBody()
        {
            var completionRegistry = new CompletionRegistry(Environment, () => Environment.BatchContextPool.Lease(), () => throw new NotImplementedException());
            var cq   = CompletionQueueSafeHandle.CreateAsync(completionRegistry);
            var call = CreateFakeCall(cq);

            var sendCompletionCallback = new NopSendCompletionCallback();
            var sliceBuffer            = SliceBufferSafeHandle.Create();
            var writeFlags             = default(WriteFlags);

            for (int i = 0; i < Iterations; i++)
            {
                // SendMessage steals the slices from the slice buffer, so we need to repopulate in each iteration.
                sliceBuffer.Reset();
                sliceBuffer.GetSpan(PayloadSize);
                sliceBuffer.Advance(PayloadSize);

                call.StartSendMessage(sendCompletionCallback, sliceBuffer, writeFlags, false);
                var callback = completionRegistry.Extract(completionRegistry.LastRegisteredKey);
                callback.OnComplete(true);
            }
            sliceBuffer.Dispose();
            cq.Dispose();
        }
Ejemplo n.º 10
0
 public void RequestCall(RequestCallCompletionDelegate callback, CompletionQueueSafeHandle completionQueue)
 {
     using (completionQueue.NewScope())
     {
         var ctx = RequestCallContextSafeHandle.Create();
         completionQueue.CompletionRegistry.RegisterRequestCallCompletion(ctx, callback);
         Native.grpcsharp_server_request_call(this, completionQueue, ctx).CheckOk();
     }
 }
Ejemplo n.º 11
0
        /// <summary>
        /// Handles the native callback.
        /// </summary>
        private void HandleNewServerRpc(bool success, RequestCallContextSafeHandle ctx, CompletionQueueSafeHandle cq)
        {
            bool nextRpcRequested = false;

            if (success)
            {
                var newRpc = ctx.GetServerRpcNew(this);

                // after server shutdown, the callback returns with null call
                if (!newRpc.Call.IsInvalid)
                {
                    nextRpcRequested = true;

                    // Start asynchronous handler for the call.
                    // Don't await, the continuations will run on gRPC thread pool once triggered
                    // by cq.Next().
                    #pragma warning disable 4014
                    HandleCallAsync(newRpc, cq, (server, state) => server.AllowOneRpc(state));
                    #pragma warning restore 4014
                }
            }

            if (!nextRpcRequested)
            {
                AllowOneRpc(cq);
            }
        }
Ejemplo n.º 12
0
 public void RegisterCompletionQueue(CompletionQueueSafeHandle cq)
 {
     Native.grpcsharp_server_register_completion_queue(this, cq);
 }
        public void CreateAndDestroy()
        {
            var cq = CompletionQueueSafeHandle.Create();

            cq.Dispose();
        }