diff options
Diffstat (limited to 'src/csharp/Grpc.Core/Server.cs')
-rw-r--r-- | src/csharp/Grpc.Core/Server.cs | 64 |
1 files changed, 24 insertions, 40 deletions
diff --git a/src/csharp/Grpc.Core/Server.cs b/src/csharp/Grpc.Core/Server.cs index 0f4d77eaf3..8e818885d1 100644 --- a/src/csharp/Grpc.Core/Server.cs +++ b/src/csharp/Grpc.Core/Server.cs @@ -52,11 +52,6 @@ namespace Grpc.Core /// </summary> public const int PickUnusedPort = 0; - // TODO(jtattermusch) : make sure the delegate doesn't get garbage collected while - // native callbacks are in the completion queue. - readonly CompletionCallbackDelegate serverShutdownHandler; - readonly CompletionCallbackDelegate newServerRpcHandler; - readonly ServerSafeHandle handle; readonly object myLock = new object(); @@ -66,11 +61,16 @@ namespace Grpc.Core bool startRequested; bool shutdownRequested; - public Server() + /// <summary> + /// Create a new server. + /// </summary> + /// <param name="options">Channel options.</param> + public Server(IEnumerable<ChannelOption> options = null) { - this.handle = ServerSafeHandle.NewServer(GetCompletionQueue(), IntPtr.Zero); - this.newServerRpcHandler = HandleNewServerRpc; - this.serverShutdownHandler = HandleServerShutdown; + using (var channelArgs = ChannelOptions.CreateChannelArgs(options)) + { + this.handle = ServerSafeHandle.NewServer(GetCompletionQueue(), channelArgs); + } } /// <summary> @@ -108,7 +108,7 @@ namespace Grpc.Core /// </summary> /// <returns>The port on which server will be listening.</returns> /// <param name="host">the host</param> - /// <param name="port">the port. If zero, , an unused port is chosen automatically.</param> + /// <param name="port">the port. If zero, an unused port is chosen automatically.</param> public int AddListeningPort(string host, int port, ServerCredentials credentials) { Preconditions.CheckNotNull(credentials); @@ -144,7 +144,7 @@ namespace Grpc.Core shutdownRequested = true; } - handle.ShutdownAndNotify(GetCompletionQueue(), serverShutdownHandler); + handle.ShutdownAndNotify(GetCompletionQueue(), HandleServerShutdown); await shutdownTcs.Task; handle.Dispose(); } @@ -173,7 +173,7 @@ namespace Grpc.Core shutdownRequested = true; } - handle.ShutdownAndNotify(GetCompletionQueue(), serverShutdownHandler); + handle.ShutdownAndNotify(GetCompletionQueue(), HandleServerShutdown); handle.CancelAllCalls(); await shutdownTcs.Task; handle.Dispose(); @@ -208,7 +208,7 @@ namespace Grpc.Core { if (!shutdownRequested) { - handle.RequestCall(GetCompletionQueue(), newServerRpcHandler); + handle.RequestCall(GetCompletionQueue(), HandleNewServerRpc); } } } @@ -236,44 +236,28 @@ namespace Grpc.Core /// <summary> /// Handles the native callback. /// </summary> - private void HandleNewServerRpc(bool success, IntPtr batchContextPtr) + private void HandleNewServerRpc(bool success, BatchContextSafeHandle ctx) { - try - { - var ctx = new BatchContextSafeHandleNotOwned(batchContextPtr); - - // TODO: handle error + // 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)); - } + CallSafeHandle call = ctx.GetServerRpcNewCall(); + string method = ctx.GetServerRpcNewMethod(); - AllowOneRpc(); - } - catch (Exception e) + // after server shutdown, the callback returns with null call + if (!call.IsInvalid) { - Console.WriteLine("Caught exception in a native handler: " + e); + Task.Run(async () => await InvokeCallHandler(call, method)); } + + AllowOneRpc(); } /// <summary> /// Handles native callback. /// </summary> - private void HandleServerShutdown(bool success, IntPtr batchContextPtr) + private void HandleServerShutdown(bool success, BatchContextSafeHandle ctx) { - try - { - shutdownTcs.SetResult(null); - } - catch (Exception e) - { - Console.WriteLine("Caught exception in a native handler: " + e); - } + shutdownTcs.SetResult(null); } private static CompletionQueueSafeHandle GetCompletionQueue() |