aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/csharp/Grpc.Core
diff options
context:
space:
mode:
authorGravatar Jan Tattermusch <jtattermusch@google.com>2016-04-01 14:38:32 -0700
committerGravatar Jan Tattermusch <jtattermusch@google.com>2016-04-01 14:38:32 -0700
commit153c32a1eb696643095c60a4a98cd8507989fcf6 (patch)
treefab8cf051d9a2c13805a078b6cb5bf11c05dc4a4 /src/csharp/Grpc.Core
parent00eef19acd8a6c3a8ae41929571c4cc040f73132 (diff)
allow halfclose after close on clients
Diffstat (limited to 'src/csharp/Grpc.Core')
-rw-r--r--src/csharp/Grpc.Core/Internal/AsyncCall.cs14
-rw-r--r--src/csharp/Grpc.Core/Internal/AsyncCallBase.cs38
-rw-r--r--src/csharp/Grpc.Core/Internal/AsyncCallServer.cs6
3 files changed, 46 insertions, 12 deletions
diff --git a/src/csharp/Grpc.Core/Internal/AsyncCall.cs b/src/csharp/Grpc.Core/Internal/AsyncCall.cs
index cc37267161..016e1b8587 100644
--- a/src/csharp/Grpc.Core/Internal/AsyncCall.cs
+++ b/src/csharp/Grpc.Core/Internal/AsyncCall.cs
@@ -258,9 +258,19 @@ namespace Grpc.Core.Internal
lock (myLock)
{
GrpcPreconditions.CheckNotNull(completionDelegate, "Completion delegate cannot be null");
- CheckSendingAllowed();
+ CheckSendingAllowed(allowFinished: true);
- call.StartSendCloseFromClient(HandleHalfclosed);
+ if (!disposed && !finished)
+ {
+ call.StartSendCloseFromClient(HandleSendCloseFromClientFinished);
+ }
+ else
+ {
+ // In case the call has already been finished by the serverside,
+ // the halfclose has already been done implicitly, so we only
+ // emit the notification for the completion delegate.
+ Task.Run(() => HandleSendCloseFromClientFinished(true));
+ }
halfcloseRequested = true;
sendCompletionDelegate = completionDelegate;
diff --git a/src/csharp/Grpc.Core/Internal/AsyncCallBase.cs b/src/csharp/Grpc.Core/Internal/AsyncCallBase.cs
index aa5765be6f..ccd047f469 100644
--- a/src/csharp/Grpc.Core/Internal/AsyncCallBase.cs
+++ b/src/csharp/Grpc.Core/Internal/AsyncCallBase.cs
@@ -136,7 +136,7 @@ namespace Grpc.Core.Internal
lock (myLock)
{
GrpcPreconditions.CheckNotNull(completionDelegate, "Completion delegate cannot be null");
- CheckSendingAllowed();
+ CheckSendingAllowed(allowFinished: false);
call.StartSendMessage(HandleSendFinished, payload, writeFlags, !initialMetadataSent);
@@ -202,14 +202,14 @@ namespace Grpc.Core.Internal
{
}
- protected void CheckSendingAllowed()
+ protected void CheckSendingAllowed(bool allowFinished)
{
GrpcPreconditions.CheckState(started);
CheckNotCancelled();
- GrpcPreconditions.CheckState(!disposed);
+ GrpcPreconditions.CheckState(!disposed || allowFinished);
GrpcPreconditions.CheckState(!halfcloseRequested, "Already halfclosed.");
- GrpcPreconditions.CheckState(!finished, "Already finished.");
+ GrpcPreconditions.CheckState(!finished || allowFinished, "Already finished.");
GrpcPreconditions.CheckState(sendCompletionDelegate == null, "Only one write can be pending at a time");
}
@@ -294,9 +294,33 @@ namespace Grpc.Core.Internal
}
/// <summary>
- /// Handles halfclose completion.
+ /// Handles halfclose (send close from client) completion.
+ /// </summary>
+ protected void HandleSendCloseFromClientFinished(bool success)
+ {
+ AsyncCompletionDelegate<object> origCompletionDelegate = null;
+ lock (myLock)
+ {
+ origCompletionDelegate = sendCompletionDelegate;
+ sendCompletionDelegate = null;
+
+ ReleaseResourcesIfPossible();
+ }
+
+ if (!success)
+ {
+ FireCompletion(origCompletionDelegate, null, new InvalidOperationException("Sending close from client has failed."));
+ }
+ else
+ {
+ FireCompletion(origCompletionDelegate, null, null);
+ }
+ }
+
+ /// <summary>
+ /// Handles send status from server completion.
/// </summary>
- protected void HandleHalfclosed(bool success)
+ protected void HandleSendStatusFromServerFinished(bool success)
{
AsyncCompletionDelegate<object> origCompletionDelegate = null;
lock (myLock)
@@ -309,7 +333,7 @@ namespace Grpc.Core.Internal
if (!success)
{
- FireCompletion(origCompletionDelegate, null, new InvalidOperationException("Halfclose failed"));
+ FireCompletion(origCompletionDelegate, null, new InvalidOperationException("Error sending status from server."));
}
else
{
diff --git a/src/csharp/Grpc.Core/Internal/AsyncCallServer.cs b/src/csharp/Grpc.Core/Internal/AsyncCallServer.cs
index e749448565..bea2b3660c 100644
--- a/src/csharp/Grpc.Core/Internal/AsyncCallServer.cs
+++ b/src/csharp/Grpc.Core/Internal/AsyncCallServer.cs
@@ -113,7 +113,7 @@ namespace Grpc.Core.Internal
GrpcPreconditions.CheckState(!initialMetadataSent, "Response headers can only be sent once per call.");
GrpcPreconditions.CheckState(streamingWritesCounter == 0, "Response headers can only be sent before the first write starts.");
- CheckSendingAllowed();
+ CheckSendingAllowed(allowFinished: false);
GrpcPreconditions.CheckNotNull(completionDelegate, "Completion delegate cannot be null");
@@ -137,11 +137,11 @@ namespace Grpc.Core.Internal
lock (myLock)
{
GrpcPreconditions.CheckNotNull(completionDelegate, "Completion delegate cannot be null");
- CheckSendingAllowed();
+ CheckSendingAllowed(allowFinished: false);
using (var metadataArray = MetadataArraySafeHandle.Create(trailers))
{
- call.StartSendStatusFromServer(HandleHalfclosed, status, metadataArray, !initialMetadataSent);
+ call.StartSendStatusFromServer(HandleSendStatusFromServerFinished, status, metadataArray, !initialMetadataSent);
}
halfcloseRequested = true;
readingDone = true;