server and channel adjustments

pull/13459/head
Jan Tattermusch 7 years ago
parent 2dc792de17
commit 8b451ed31d
  1. 28
      src/csharp/Grpc.Core/Channel.cs
  2. 4
      src/csharp/Grpc.Core/Internal/ChannelSafeHandle.cs
  3. 6
      src/csharp/Grpc.Core/Internal/ServerSafeHandle.cs
  4. 2
      src/csharp/Grpc.Core/Server.cs

@ -127,6 +127,20 @@ namespace Grpc.Core
} }
} }
// cached handler for watch connectivity state
static readonly BatchCompletionDelegate WatchConnectivityStateHandler = (success, ctx, state) =>
{
var tcs = (TaskCompletionSource<object>) state;
if (success)
{
tcs.SetResult(null);
}
else
{
tcs.SetCanceled();
}
};
/// <summary> /// <summary>
/// Returned tasks completes once channel state has become different from /// Returned tasks completes once channel state has become different from
/// given lastObservedState. /// given lastObservedState.
@ -138,18 +152,8 @@ namespace Grpc.Core
"Shutdown is a terminal state. No further state changes can occur."); "Shutdown is a terminal state. No further state changes can occur.");
var tcs = new TaskCompletionSource<object>(); var tcs = new TaskCompletionSource<object>();
var deadlineTimespec = deadline.HasValue ? Timespec.FromDateTime(deadline.Value) : Timespec.InfFuture; var deadlineTimespec = deadline.HasValue ? Timespec.FromDateTime(deadline.Value) : Timespec.InfFuture;
var handler = new BatchCompletionDelegate((success, ctx) => // pass "tcs" as "state" for WatchConnectivityStateHandler.
{ handle.WatchConnectivityState(lastObservedState, deadlineTimespec, completionQueue, WatchConnectivityStateHandler, tcs);
if (success)
{
tcs.SetResult(null);
}
else
{
tcs.SetCanceled();
}
});
handle.WatchConnectivityState(lastObservedState, deadlineTimespec, completionQueue, handler);
return tcs.Task; return tcs.Task;
} }

@ -64,10 +64,10 @@ namespace Grpc.Core.Internal
return Native.grpcsharp_channel_check_connectivity_state(this, tryToConnect ? 1 : 0); return Native.grpcsharp_channel_check_connectivity_state(this, tryToConnect ? 1 : 0);
} }
public void WatchConnectivityState(ChannelState lastObservedState, Timespec deadline, CompletionQueueSafeHandle cq, BatchCompletionDelegate callback) public void WatchConnectivityState(ChannelState lastObservedState, Timespec deadline, CompletionQueueSafeHandle cq, BatchCompletionDelegate callback, object callbackState)
{ {
var ctx = BatchContextSafeHandle.Create(); var ctx = BatchContextSafeHandle.Create();
cq.CompletionRegistry.RegisterBatchCompletion(ctx, callback); cq.CompletionRegistry.RegisterBatchCompletion(ctx, callback, callbackState);
Native.grpcsharp_channel_watch_connectivity_state(this, lastObservedState, deadline, cq, ctx); Native.grpcsharp_channel_watch_connectivity_state(this, lastObservedState, deadline, cq, ctx);
} }

@ -59,13 +59,15 @@ namespace Grpc.Core.Internal
{ {
Native.grpcsharp_server_start(this); Native.grpcsharp_server_start(this);
} }
public void ShutdownAndNotify(BatchCompletionDelegate callback, CompletionQueueSafeHandle completionQueue) public void ShutdownAndNotify(BatchCompletionDelegate callback, CompletionQueueSafeHandle completionQueue)
{ {
using (completionQueue.NewScope()) using (completionQueue.NewScope())
{ {
var ctx = BatchContextSafeHandle.Create(); var ctx = BatchContextSafeHandle.Create();
completionQueue.CompletionRegistry.RegisterBatchCompletion(ctx, callback); // TODO(jtattermusch): delegate allocation by caller can be avoided by utilizing the "state" object,
// but server shutdown isn't worth optimizing right now.
completionQueue.CompletionRegistry.RegisterBatchCompletion(ctx, callback, null);
Native.grpcsharp_server_shutdown_and_notify_callback(this, completionQueue, ctx); Native.grpcsharp_server_shutdown_and_notify_callback(this, completionQueue, ctx);
} }
} }

@ -387,7 +387,7 @@ namespace Grpc.Core
/// <summary> /// <summary>
/// Handles native callback. /// Handles native callback.
/// </summary> /// </summary>
private void HandleServerShutdown(bool success, BatchContextSafeHandle ctx) private void HandleServerShutdown(bool success, BatchContextSafeHandle ctx, object state)
{ {
shutdownTcs.SetResult(null); shutdownTcs.SetResult(null);
} }

Loading…
Cancel
Save