aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/csharp/Grpc.Core
diff options
context:
space:
mode:
Diffstat (limited to 'src/csharp/Grpc.Core')
-rw-r--r--src/csharp/Grpc.Core/Internal/ServerSafeHandle.cs21
-rw-r--r--src/csharp/Grpc.Core/Server.cs24
2 files changed, 27 insertions, 18 deletions
diff --git a/src/csharp/Grpc.Core/Internal/ServerSafeHandle.cs b/src/csharp/Grpc.Core/Internal/ServerSafeHandle.cs
index edd9c490ff..eda9afcb87 100644
--- a/src/csharp/Grpc.Core/Internal/ServerSafeHandle.cs
+++ b/src/csharp/Grpc.Core/Internal/ServerSafeHandle.cs
@@ -60,10 +60,10 @@ namespace Grpc.Core.Internal
static extern GRPCCallError grpcsharp_server_request_call(ServerSafeHandle server, CompletionQueueSafeHandle cq, BatchContextSafeHandle ctx);
[DllImport("grpc_csharp_ext.dll")]
- static extern void grpcsharp_server_shutdown(ServerSafeHandle server);
+ static extern void grpcsharp_server_cancel_all_calls(ServerSafeHandle server);
[DllImport("grpc_csharp_ext.dll")]
- static extern void grpcsharp_server_shutdown_and_notify_callback(ServerSafeHandle server, BatchContextSafeHandle ctx);
+ static extern void grpcsharp_server_shutdown_and_notify_callback(ServerSafeHandle server, CompletionQueueSafeHandle cq, BatchContextSafeHandle ctx);
[DllImport("grpc_csharp_ext.dll")]
static extern void grpcsharp_server_destroy(IntPtr server);
@@ -91,17 +91,12 @@ namespace Grpc.Core.Internal
{
grpcsharp_server_start(this);
}
-
- public void Shutdown()
- {
- grpcsharp_server_shutdown(this);
- }
-
- public void ShutdownAndNotify(BatchCompletionDelegate callback)
+
+ public void ShutdownAndNotify(CompletionQueueSafeHandle cq, BatchCompletionDelegate callback)
{
var ctx = BatchContextSafeHandle.Create();
GrpcEnvironment.CompletionRegistry.RegisterBatchCompletion(ctx, callback);
- grpcsharp_server_shutdown_and_notify_callback(this, ctx);
+ grpcsharp_server_shutdown_and_notify_callback(this, cq, ctx);
}
public void RequestCall(CompletionQueueSafeHandle cq, BatchCompletionDelegate callback)
@@ -116,5 +111,11 @@ namespace Grpc.Core.Internal
grpcsharp_server_destroy(handle);
return true;
}
+
+ // Only to be called after ShutdownAndNotify.
+ public void CancelAllCalls()
+ {
+ grpcsharp_server_cancel_all_calls(this);
+ }
}
}
diff --git a/src/csharp/Grpc.Core/Server.cs b/src/csharp/Grpc.Core/Server.cs
index da59fc7232..3352fc93a1 100644
--- a/src/csharp/Grpc.Core/Server.cs
+++ b/src/csharp/Grpc.Core/Server.cs
@@ -52,9 +52,6 @@ namespace Grpc.Core
/// </summary>
public const int PickUnusedPort = 0;
- //readonly OpCompletionDelegate serverShutdownHandler;
- //readonly OpCompletionDelegate newServerRpcHandler;
-
readonly ServerSafeHandle handle;
readonly object myLock = new object();
@@ -67,8 +64,6 @@ namespace Grpc.Core
public Server()
{
this.handle = ServerSafeHandle.NewServer(GetCompletionQueue(), IntPtr.Zero);
- //this.newServerRpcHandler = HandleNewServerRpc;
- //this.serverShutdownHandler = HandleServerShutdown;
}
/// <summary>
@@ -142,8 +137,7 @@ namespace Grpc.Core
shutdownRequested = true;
}
- var ctx = BatchContextSafeHandle.Create();
- handle.ShutdownAndNotify(HandleServerShutdown);
+ handle.ShutdownAndNotify(GetCompletionQueue(), HandleServerShutdown);
await shutdownTcs.Task;
handle.Dispose();
}
@@ -159,8 +153,22 @@ namespace Grpc.Core
}
}
- public void Kill()
+ /// <summary>
+ /// Requests server shutdown while cancelling all the in-progress calls.
+ /// The returned task finishes when shutdown procedure is complete.
+ /// </summary>
+ public async Task KillAsync()
{
+ lock (myLock)
+ {
+ Preconditions.CheckState(startRequested);
+ Preconditions.CheckState(!shutdownRequested);
+ shutdownRequested = true;
+ }
+
+ handle.ShutdownAndNotify(GetCompletionQueue(), HandleServerShutdown);
+ handle.CancelAllCalls();
+ await shutdownTcs.Task;
handle.Dispose();
}