aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Craig Tiller <ctiller@google.com>2015-05-06 13:14:06 -0700
committerGravatar Craig Tiller <ctiller@google.com>2015-05-06 13:14:06 -0700
commit12f809fd898d4c8696b7b4d22c3de3e430d2fb2e (patch)
tree6639e7f6ef2d012add60fa9f77831214ec6e1642 /src
parent9eb47ada08d3c5cb35b7fe05b2bf9cf18f61e928 (diff)
parent096528d604402f1324f6ef1aeb99f59836e6d8d9 (diff)
Merge github.com:grpc/grpc into bye-bye-completion-queue-pie
Diffstat (limited to 'src')
-rw-r--r--src/core/channel/context.h44
-rw-r--r--src/core/profiling/basic_timers.c14
-rw-r--r--src/core/profiling/stap_probes.d1
-rw-r--r--src/core/profiling/stap_timers.c5
-rw-r--r--src/core/profiling/timers.h12
-rw-r--r--src/core/support/slice_buffer.c33
-rw-r--r--src/core/surface/call.c34
-rw-r--r--src/core/surface/call.h8
-rw-r--r--src/core/transport/stream_op.c31
-rw-r--r--src/core/transport/transport.h3
-rw-r--r--src/csharp/.gitignore1
-rw-r--r--src/csharp/Grpc.Core.Tests/ClientServerTest.cs201
-rw-r--r--src/csharp/Grpc.Core/AsyncClientStreamingCall.cs (renamed from src/csharp/Grpc.Core/Internal/ServerStreamingOutputObserver.cs)72
-rw-r--r--src/csharp/Grpc.Core/AsyncDuplexStreamingCall.cs101
-rw-r--r--src/csharp/Grpc.Core/AsyncServerStreamingCall.cs (renamed from src/csharp/Grpc.Core/Utils/RecordingQueue.cs)47
-rw-r--r--src/csharp/Grpc.Core/Call.cs3
-rw-r--r--src/csharp/Grpc.Core/Calls.cs47
-rw-r--r--src/csharp/Grpc.Core/Channel.cs16
-rw-r--r--src/csharp/Grpc.Core/Credentials.cs2
-rw-r--r--src/csharp/Grpc.Core/Grpc.Core.csproj24
-rw-r--r--src/csharp/Grpc.Core/GrpcEnvironment.cs16
-rw-r--r--src/csharp/Grpc.Core/IAsyncStreamReader.cs54
-rw-r--r--src/csharp/Grpc.Core/IAsyncStreamWriter.cs54
-rw-r--r--src/csharp/Grpc.Core/IClientStreamWriter.cs53
-rw-r--r--src/csharp/Grpc.Core/IServerStreamWriter.cs48
-rw-r--r--src/csharp/Grpc.Core/Internal/AsyncCall.cs50
-rw-r--r--src/csharp/Grpc.Core/Internal/AsyncCallBase.cs135
-rw-r--r--src/csharp/Grpc.Core/Internal/AsyncCallServer.cs39
-rw-r--r--src/csharp/Grpc.Core/Internal/AsyncCompletion.cs18
-rw-r--r--src/csharp/Grpc.Core/Internal/AtomicCounter.cs (renamed from src/csharp/Grpc.Core/ClientStreamingAsyncResult.cs)28
-rw-r--r--src/csharp/Grpc.Core/Internal/BatchContextSafeHandleNotOwned.cs8
-rw-r--r--src/csharp/Grpc.Core/Internal/ClientRequestStream.cs (renamed from src/csharp/Grpc.Core/Internal/ClientStreamingInputObserver.cs)33
-rw-r--r--src/csharp/Grpc.Core/Internal/ClientResponseStream.cs (renamed from src/csharp/Grpc.Core/Utils/RecordingObserver.cs)27
-rw-r--r--src/csharp/Grpc.Core/Internal/DebugStats.cs45
-rw-r--r--src/csharp/Grpc.Core/Internal/ServerCallHandler.cs204
-rw-r--r--src/csharp/Grpc.Core/Internal/ServerCalls.cs63
-rw-r--r--src/csharp/Grpc.Core/Internal/ServerRequestStream.cs56
-rw-r--r--src/csharp/Grpc.Core/Internal/ServerResponseStream.cs (renamed from src/csharp/Grpc.Core/ServerCalls.cs)33
-rw-r--r--src/csharp/Grpc.Core/Method.cs11
-rw-r--r--src/csharp/Grpc.Core/Server.cs6
-rw-r--r--src/csharp/Grpc.Core/ServerMethods.cs61
-rw-r--r--src/csharp/Grpc.Core/ServerServiceDefinition.cs24
-rw-r--r--src/csharp/Grpc.Core/Status.cs10
-rw-r--r--src/csharp/Grpc.Core/Utils/AsyncStreamExtensions.cs111
-rw-r--r--src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs68
-rw-r--r--src/csharp/Grpc.Examples/MathExamples.cs26
-rw-r--r--src/csharp/Grpc.Examples/MathGrpc.cs24
-rw-r--r--src/csharp/Grpc.Examples/MathServiceImpl.cs67
-rw-r--r--src/csharp/Grpc.IntegrationTesting/InteropClient.cs186
-rw-r--r--src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs14
-rw-r--r--src/csharp/Grpc.IntegrationTesting/TestServiceGrpc.cs34
-rw-r--r--src/csharp/Grpc.IntegrationTesting/TestServiceImpl.cs77
-rw-r--r--src/csharp/ext/grpc_csharp_ext.c6
-rwxr-xr-xsrc/php/tests/interop/interop_client.php24
-rwxr-xr-xsrc/ruby/grpc.gemspec2
55 files changed, 1794 insertions, 620 deletions
diff --git a/src/core/channel/context.h b/src/core/channel/context.h
new file mode 100644
index 0000000000..e2e5e80513
--- /dev/null
+++ b/src/core/channel/context.h
@@ -0,0 +1,44 @@
+/*
+ *
+ * Copyright 2015, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef GRPC_INTERNAL_CORE_CHANNEL_CONTEXT_H
+#define GRPC_INTERNAL_CORE_CHANNEL_CONTEXT_H
+
+/* Call object context pointers */
+typedef enum {
+ GRPC_CONTEXT_SECURITY = 0,
+ GRPC_CONTEXT_TRACING,
+ GRPC_CONTEXT_COUNT
+} grpc_context_index;
+
+#endif
diff --git a/src/core/profiling/basic_timers.c b/src/core/profiling/basic_timers.c
index 866833f225..124a8d6621 100644
--- a/src/core/profiling/basic_timers.c
+++ b/src/core/profiling/basic_timers.c
@@ -45,7 +45,12 @@
#include <grpc/support/thd.h>
#include <stdio.h>
-typedef enum { BEGIN = '{', END = '}', MARK = '.' } marker_type;
+typedef enum {
+ BEGIN = '{',
+ END = '}',
+ MARK = '.',
+ IMPORTANT = '!'
+} marker_type;
typedef struct grpc_timer_entry {
grpc_precise_clock tm;
@@ -101,6 +106,13 @@ void grpc_timer_add_mark(int tag, void* id, const char* file, int line) {
}
}
+void grpc_timer_add_important_mark(int tag, void* id, const char* file,
+ int line) {
+ if (tag < GRPC_PTAG_IGNORE_THRESHOLD) {
+ grpc_timers_log_add(tag, IMPORTANT, id, file, line);
+ }
+}
+
void grpc_timer_begin(int tag, void* id, const char* file, int line) {
if (tag < GRPC_PTAG_IGNORE_THRESHOLD) {
grpc_timers_log_add(tag, BEGIN, id, file, line);
diff --git a/src/core/profiling/stap_probes.d b/src/core/profiling/stap_probes.d
index 374eeedd6c..153de91752 100644
--- a/src/core/profiling/stap_probes.d
+++ b/src/core/profiling/stap_probes.d
@@ -1,5 +1,6 @@
provider _stap {
probe add_mark(int tag);
+ probe add_important_mark(int tag);
probe timing_ns_begin(int tag);
probe timing_ns_end(int tag);
};
diff --git a/src/core/profiling/stap_timers.c b/src/core/profiling/stap_timers.c
index 6e3a965dae..064c86e794 100644
--- a/src/core/profiling/stap_timers.c
+++ b/src/core/profiling/stap_timers.c
@@ -46,6 +46,11 @@ void grpc_timer_add_mark(int tag, void* id, const char* file, int line) {
_STAP_ADD_MARK(tag);
}
+void grpc_timer_add_important_mark(int tag, void* id, const char* file,
+ int line) {
+ _STAP_ADD_IMPORTANT_MARK(tag);
+}
+
void grpc_timer_begin(int tag, void* id, const char* file, int line) {
_STAP_TIMING_NS_BEGIN(tag);
}
diff --git a/src/core/profiling/timers.h b/src/core/profiling/timers.h
index 0b0f7152e7..4fb465c237 100644
--- a/src/core/profiling/timers.h
+++ b/src/core/profiling/timers.h
@@ -42,6 +42,8 @@ void grpc_timers_global_init(void);
void grpc_timers_global_destroy(void);
void grpc_timer_add_mark(int tag, void *id, const char *file, int line);
+void grpc_timer_add_important_mark(int tag, void *id, const char *file,
+ int line);
void grpc_timer_begin(int tag, void *id, const char *file, int line);
void grpc_timer_end(int tag, void *id, const char *file, int line);
@@ -82,6 +84,10 @@ enum grpc_profiling_tags {
do { \
} while (0)
+#define GRPC_TIMER_IMPORTANT_MARK(tag, id) \
+ do { \
+ } while (0)
+
#define GRPC_TIMER_BEGIN(tag, id) \
do { \
} while (0)
@@ -102,6 +108,12 @@ enum grpc_profiling_tags {
grpc_timer_add_mark(tag, ((void *)(gpr_intptr)(id)), __FILE__, __LINE__); \
}
+#define GRPC_TIMER_IMPORTANT_MARK(tag, id) \
+ if (tag < GRPC_PTAG_IGNORE_THRESHOLD) { \
+ grpc_timer_add_important_mark(tag, ((void *)(gpr_intptr)(id)), __FILE__, \
+ __LINE__); \
+ }
+
#define GRPC_TIMER_BEGIN(tag, id) \
if (tag < GRPC_PTAG_IGNORE_THRESHOLD) { \
grpc_timer_begin(tag, ((void *)(gpr_intptr)(id)), __FILE__, __LINE__); \
diff --git a/src/core/support/slice_buffer.c b/src/core/support/slice_buffer.c
index 3b1daa07c5..91b5d8c98b 100644
--- a/src/core/support/slice_buffer.c
+++ b/src/core/support/slice_buffer.c
@@ -37,6 +37,7 @@
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
+#include <grpc/support/useful.h>
/* grow a buffer; requires GRPC_SLICE_BUFFER_INLINE_ELEMENTS > 1 */
#define GROW(x) (3 * (x) / 2)
@@ -162,14 +163,30 @@ void gpr_slice_buffer_reset_and_unref(gpr_slice_buffer *sb) {
}
void gpr_slice_buffer_swap(gpr_slice_buffer *a, gpr_slice_buffer *b) {
- gpr_slice_buffer temp = *a;
- *a = *b;
- *b = temp;
-
- if (a->slices == b->inlined) {
+ GPR_SWAP(size_t, a->count, b->count);
+ GPR_SWAP(size_t, a->capacity, b->capacity);
+ GPR_SWAP(size_t, a->length, b->length);
+
+ if (a->slices == a->inlined) {
+ if (b->slices == b->inlined) {
+ /* swap contents of inlined buffer */
+ gpr_slice temp[GRPC_SLICE_BUFFER_INLINE_ELEMENTS];
+ memcpy(temp, a->slices, b->count * sizeof(gpr_slice));
+ memcpy(a->slices, b->slices, a->count * sizeof(gpr_slice));
+ memcpy(b->slices, temp, b->count * sizeof(gpr_slice));
+ } else {
+ /* a is inlined, b is not - copy a inlined into b, fix pointers */
+ a->slices = b->slices;
+ b->slices = b->inlined;
+ memcpy(b->slices, a->inlined, b->count * sizeof(gpr_slice));
+ }
+ } else if (b->slices == b->inlined) {
+ /* b is inlined, a is not - copy b inlined int a, fix pointers */
+ b->slices = a->slices;
a->slices = a->inlined;
- }
- if (b->slices == a->inlined) {
- b->slices = b->inlined;
+ memcpy(a->slices, b->inlined, a->count * sizeof(gpr_slice));
+ } else {
+ /* no inlining: easy swap */
+ GPR_SWAP(gpr_slice *, a->slices, b->slices);
}
}
diff --git a/src/core/surface/call.c b/src/core/surface/call.c
index 5fdb5bf7e4..bb2c20af32 100644
--- a/src/core/surface/call.c
+++ b/src/core/surface/call.c
@@ -205,6 +205,9 @@ struct grpc_call {
/* Received call statuses from various sources */
received_status status[STATUS_SOURCE_COUNT];
+ void *context[GRPC_CONTEXT_COUNT];
+ void (*destroy_context[GRPC_CONTEXT_COUNT])(void *);
+
/* Deadline alarm - if have_alarm is non-zero */
grpc_alarm alarm;
@@ -232,13 +235,6 @@ struct grpc_call {
#define CALL_FROM_TOP_ELEM(top_elem) \
CALL_FROM_CALL_STACK(grpc_call_stack_from_top_element(top_elem))
-#define SWAP(type, x, y) \
- do { \
- type temp = x; \
- x = y; \
- y = temp; \
- } while (0)
-
static void set_deadline_alarm(grpc_call *call, gpr_timespec deadline);
static void call_on_done_recv(void *call, int success);
static void call_on_done_send(void *call, int success);
@@ -294,6 +290,7 @@ grpc_call *grpc_call_create(grpc_channel *channel, grpc_completion_queue *cq,
initial_op.recv_state = &call->recv_state;
initial_op.on_done_recv = call_on_done_recv;
initial_op.recv_user_data = call;
+ initial_op.context = call->context;
call->receiving = 1;
GRPC_CALL_INTERNAL_REF(call, "receiving");
initial_op_ptr = &initial_op;
@@ -346,6 +343,11 @@ static void destroy_call(void *call, int ignored_success) {
for (i = 0; i < c->send_initial_metadata_count; i++) {
grpc_mdelem_unref(c->send_initial_metadata[i].md);
}
+ for (i = 0; i < GRPC_CONTEXT_COUNT; i++) {
+ if (c->destroy_context[i]) {
+ c->destroy_context[i](c->context[i]);
+ }
+ }
grpc_sopb_destroy(&c->send_ops);
grpc_sopb_destroy(&c->recv_ops);
grpc_bbq_destroy(&c->incoming_queue);
@@ -562,12 +564,12 @@ static void finish_live_ioreq_op(grpc_call *call, grpc_ioreq_op op,
call->request_data[GRPC_IOREQ_RECV_STATUS_DETAILS]);
break;
case GRPC_IOREQ_RECV_INITIAL_METADATA:
- SWAP(grpc_metadata_array, call->buffered_metadata[0],
+ GPR_SWAP(grpc_metadata_array, call->buffered_metadata[0],
*call->request_data[GRPC_IOREQ_RECV_INITIAL_METADATA]
.recv_metadata);
break;
case GRPC_IOREQ_RECV_TRAILING_METADATA:
- SWAP(grpc_metadata_array, call->buffered_metadata[1],
+ GPR_SWAP(grpc_metadata_array, call->buffered_metadata[1],
*call->request_data[GRPC_IOREQ_RECV_TRAILING_METADATA]
.recv_metadata);
break;
@@ -1029,6 +1031,7 @@ static grpc_call_error cancel_with_status(
static void execute_op(grpc_call *call, grpc_transport_op *op) {
grpc_call_element *elem;
elem = CALL_ELEM_FROM_CALL(call, 0);
+ op->context = call->context;
elem->filter->start_transport_op(elem, op);
}
@@ -1266,3 +1269,16 @@ grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops,
return grpc_call_start_ioreq_and_call_back(call, reqs, out, finish_batch,
tag);
}
+
+void grpc_call_context_set(grpc_call *call, grpc_context_index elem, void *value,
+ void (*destroy)(void *value)) {
+ if (call->destroy_context[elem]) {
+ call->destroy_context[elem](value);
+ }
+ call->context[elem] = value;
+ call->destroy_context[elem] = destroy;
+}
+
+void *grpc_call_context_get(grpc_call *call, grpc_context_index elem) {
+ return call->context[elem];
+}
diff --git a/src/core/surface/call.h b/src/core/surface/call.h
index 30d9c868a3..7d3e661bf3 100644
--- a/src/core/surface/call.h
+++ b/src/core/surface/call.h
@@ -35,6 +35,7 @@
#define GRPC_INTERNAL_CORE_SURFACE_CALL_H
#include "src/core/channel/channel_stack.h"
+#include "src/core/channel/context.h"
#include <grpc/grpc.h>
/* Primitive operation types - grpc_op's get rewritten into these */
@@ -119,6 +120,13 @@ void grpc_call_log_batch(char *file, int line, gpr_log_severity severity,
grpc_call *call, const grpc_op *ops, size_t nops,
void *tag);
+/* Set a context pointer.
+ No thread safety guarantees are made wrt this value. */
+void grpc_call_context_set(grpc_call *call, grpc_context_index elem, void *value,
+ void (*destroy)(void *value));
+/* Get a context pointer. */
+void *grpc_call_context_get(grpc_call *call, grpc_context_index elem);
+
#define GRPC_CALL_LOG_BATCH(sev, call, ops, nops, tag) \
if (grpc_trace_batch) grpc_call_log_batch(sev, call, ops, nops, tag)
diff --git a/src/core/transport/stream_op.c b/src/core/transport/stream_op.c
index e1a75adcb6..8996ecac35 100644
--- a/src/core/transport/stream_op.c
+++ b/src/core/transport/stream_op.c
@@ -59,15 +59,30 @@ void grpc_sopb_reset(grpc_stream_op_buffer *sopb) {
}
void grpc_sopb_swap(grpc_stream_op_buffer *a, grpc_stream_op_buffer *b) {
- grpc_stream_op_buffer temp = *a;
- *a = *b;
- *b = temp;
-
- if (a->ops == b->inlined_ops) {
+ GPR_SWAP(size_t, a->nops, b->nops);
+ GPR_SWAP(size_t, a->capacity, b->capacity);
+
+ if (a->ops == a->inlined_ops) {
+ if (b->ops == b->inlined_ops) {
+ /* swap contents of inlined buffer */
+ gpr_slice temp[GRPC_SOPB_INLINE_ELEMENTS];
+ memcpy(temp, a->ops, b->nops * sizeof(grpc_stream_op));
+ memcpy(a->ops, b->ops, a->nops * sizeof(grpc_stream_op));
+ memcpy(b->ops, temp, b->nops * sizeof(grpc_stream_op));
+ } else {
+ /* a is inlined, b is not - copy a inlined into b, fix pointers */
+ a->ops = b->ops;
+ b->ops = b->inlined_ops;
+ memcpy(b->ops, a->inlined_ops, b->nops * sizeof(grpc_stream_op));
+ }
+ } else if (b->ops == b->inlined_ops) {
+ /* b is inlined, a is not - copy b inlined int a, fix pointers */
+ b->ops = a->ops;
a->ops = a->inlined_ops;
- }
- if (b->ops == a->inlined_ops) {
- b->ops = b->inlined_ops;
+ memcpy(a->ops, b->inlined_ops, a->nops * sizeof(grpc_stream_op));
+ } else {
+ /* no inlining: easy swap */
+ GPR_SWAP(grpc_stream_op *, a->ops, b->ops);
}
}
diff --git a/src/core/transport/transport.h b/src/core/transport/transport.h
index cdea0b9a0b..7a389ea393 100644
--- a/src/core/transport/transport.h
+++ b/src/core/transport/transport.h
@@ -76,6 +76,9 @@ typedef struct grpc_transport_op {
grpc_status_code cancel_with_status;
grpc_mdstr *cancel_message;
+
+ /* Indexes correspond to grpc_context_index enum values */
+ void *const *context;
} grpc_transport_op;
/* Callbacks made from the transport to the upper layers of grpc. */
diff --git a/src/csharp/.gitignore b/src/csharp/.gitignore
index dbaf60de0c..c064ff1fa6 100644
--- a/src/csharp/.gitignore
+++ b/src/csharp/.gitignore
@@ -1,4 +1,5 @@
*.userprefs
+*.csproj.user
StyleCop.Cache
test-results
packages
diff --git a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs
index 3da9e33e53..caa6220f2c 100644
--- a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs
+++ b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs
@@ -44,104 +44,205 @@ namespace Grpc.Core.Tests
{
public class ClientServerTest
{
- string host = "localhost";
+ const string Host = "localhost";
+ const string ServiceName = "/tests.Test";
- string serviceName = "/tests.Test";
+ static readonly Method<string, string> EchoMethod = new Method<string, string>(
+ MethodType.Unary,
+ "/tests.Test/Echo",
+ Marshallers.StringMarshaller,
+ Marshallers.StringMarshaller);
+
+ static readonly Method<string, string> ConcatAndEchoMethod = new Method<string, string>(
+ MethodType.ClientStreaming,
+ "/tests.Test/ConcatAndEcho",
+ Marshallers.StringMarshaller,
+ Marshallers.StringMarshaller);
- Method<string, string> unaryEchoStringMethod = new Method<string, string>(
+ static readonly Method<string, string> NonexistentMethod = new Method<string, string>(
MethodType.Unary,
- "/tests.Test/UnaryEchoString",
+ "/tests.Test/NonexistentMethod",
Marshallers.StringMarshaller,
Marshallers.StringMarshaller);
+ static readonly ServerServiceDefinition ServiceDefinition = ServerServiceDefinition.CreateBuilder(ServiceName)
+ .AddMethod(EchoMethod, EchoHandler)
+ .AddMethod(ConcatAndEchoMethod, ConcatAndEchoHandler)
+ .Build();
+
+ Server server;
+ Channel channel;
+
[TestFixtureSetUp]
- public void Init()
+ public void InitClass()
{
GrpcEnvironment.Initialize();
}
- [TestFixtureTearDown]
+ [SetUp]
+ public void Init()
+ {
+ server = new Server();
+ server.AddServiceDefinition(ServiceDefinition);
+ int port = server.AddListeningPort(Host + ":0");
+ server.Start();
+ channel = new Channel(Host + ":" + port);
+ }
+
+ [TearDown]
public void Cleanup()
{
+ channel.Dispose();
+ server.ShutdownAsync().Wait();
+ }
+
+ [TestFixtureTearDown]
+ public void CleanupClass()
+ {
GrpcEnvironment.Shutdown();
}
[Test]
public void UnaryCall()
{
- Server server = new Server();
- server.AddServiceDefinition(
- ServerServiceDefinition.CreateBuilder(serviceName)
- .AddMethod(unaryEchoStringMethod, HandleUnaryEchoString).Build());
-
- int port = server.AddListeningPort(host + ":0");
- server.Start();
+ var call = new Call<string, string>(ServiceName, EchoMethod, channel, Metadata.Empty);
+ Assert.AreEqual("ABC", Calls.BlockingUnaryCall(call, "ABC", CancellationToken.None));
+ }
- using (Channel channel = new Channel(host + ":" + port))
+ [Test]
+ public void UnaryCall_ServerHandlerThrows()
+ {
+ var call = new Call<string, string>(ServiceName, EchoMethod, channel, Metadata.Empty);
+ try
{
- var call = new Call<string, string>(serviceName, unaryEchoStringMethod, channel, Metadata.Empty);
-
- Assert.AreEqual("ABC", Calls.BlockingUnaryCall(call, "ABC", default(CancellationToken)));
-
- Assert.AreEqual("abcdef", Calls.BlockingUnaryCall(call, "abcdef", default(CancellationToken)));
+ Calls.BlockingUnaryCall(call, "THROW", CancellationToken.None);
+ Assert.Fail();
+ }
+ catch (RpcException e)
+ {
+ Assert.AreEqual(StatusCode.Unknown, e.Status.StatusCode);
}
-
- server.ShutdownAsync().Wait();
}
[Test]
- public void UnaryCallPerformance()
+ public void AsyncUnaryCall()
{
- Server server = new Server();
- server.AddServiceDefinition(
- ServerServiceDefinition.CreateBuilder(serviceName)
- .AddMethod(unaryEchoStringMethod, HandleUnaryEchoString).Build());
+ var call = new Call<string, string>(ServiceName, EchoMethod, channel, Metadata.Empty);
+ var result = Calls.AsyncUnaryCall(call, "ABC", CancellationToken.None).Result;
+ Assert.AreEqual("ABC", result);
+ }
- int port = server.AddListeningPort(host + ":0");
- server.Start();
+ [Test]
+ public void AsyncUnaryCall_ServerHandlerThrows()
+ {
+ Task.Run(async () =>
+ {
+ var call = new Call<string, string>(ServiceName, EchoMethod, channel, Metadata.Empty);
+ try
+ {
+ await Calls.AsyncUnaryCall(call, "THROW", CancellationToken.None);
+ Assert.Fail();
+ }
+ catch (RpcException e)
+ {
+ Assert.AreEqual(StatusCode.Unknown, e.Status.StatusCode);
+ }
+ }).Wait();
+ }
- using (Channel channel = new Channel(host + ":" + port))
+ [Test]
+ public void ClientStreamingCall()
+ {
+ Task.Run(async () =>
{
- var call = new Call<string, string>(serviceName, unaryEchoStringMethod, channel, Metadata.Empty);
- BenchmarkUtil.RunBenchmark(100, 1000,
- () => { Calls.BlockingUnaryCall(call, "ABC", default(CancellationToken)); });
- }
+ var call = new Call<string, string>(ServiceName, ConcatAndEchoMethod, channel, Metadata.Empty);
+ var callResult = Calls.AsyncClientStreamingCall(call, CancellationToken.None);
- server.ShutdownAsync().Wait();
+ await callResult.RequestStream.WriteAll(new string[] { "A", "B", "C" });
+ Assert.AreEqual("ABC", await callResult.Result);
+ }).Wait();
}
[Test]
- public void UnknownMethodHandler()
+ public void ClientStreamingCall_CancelAfterBegin()
{
- Server server = new Server();
- server.AddServiceDefinition(
- ServerServiceDefinition.CreateBuilder(serviceName).Build());
+ Task.Run(async () =>
+ {
+ var call = new Call<string, string>(ServiceName, ConcatAndEchoMethod, channel, Metadata.Empty);
- int port = server.AddListeningPort(host + ":0");
- server.Start();
+ var cts = new CancellationTokenSource();
+ var callResult = Calls.AsyncClientStreamingCall(call, cts.Token);
- using (Channel channel = new Channel(host + ":" + port))
- {
- var call = new Call<string, string>(serviceName, unaryEchoStringMethod, channel, Metadata.Empty);
+ // TODO(jtattermusch): we need this to ensure call has been initiated once we cancel it.
+ await Task.Delay(1000);
+ cts.Cancel();
try
{
- Calls.BlockingUnaryCall(call, "ABC", default(CancellationToken));
- Assert.Fail();
+ await callResult.Result;
}
catch (RpcException e)
{
- Assert.AreEqual(StatusCode.Unimplemented, e.Status.StatusCode);
+ Assert.AreEqual(StatusCode.Cancelled, e.Status.StatusCode);
}
+ }).Wait();
+ }
+
+ [Test]
+ public void UnaryCall_DisposedChannel()
+ {
+ channel.Dispose();
+
+ var call = new Call<string, string>(ServiceName, EchoMethod, channel, Metadata.Empty);
+ Assert.Throws(typeof(ObjectDisposedException), () => Calls.BlockingUnaryCall(call, "ABC", CancellationToken.None));
+ }
+
+ [Test]
+ public void UnaryCallPerformance()
+ {
+ var call = new Call<string, string>(ServiceName, EchoMethod, channel, Metadata.Empty);
+ BenchmarkUtil.RunBenchmark(100, 100,
+ () => { Calls.BlockingUnaryCall(call, "ABC", default(CancellationToken)); });
+ }
+
+ [Test]
+ public void UnknownMethodHandler()
+ {
+ var call = new Call<string, string>(ServiceName, NonexistentMethod, channel, Metadata.Empty);
+ try
+ {
+ Calls.BlockingUnaryCall(call, "ABC", default(CancellationToken));
+ Assert.Fail();
}
+ catch (RpcException e)
+ {
+ Assert.AreEqual(StatusCode.Unimplemented, e.Status.StatusCode);
+ }
+ }
- server.ShutdownAsync().Wait();
+ private static async Task<string> EchoHandler(string request)
+ {
+ if (request == "THROW")
+ {
+ throw new Exception("This was thrown on purpose by a test");
+ }
+ return request;
}
- private void HandleUnaryEchoString(string request, IObserver<string> responseObserver)
+ private static async Task<string> ConcatAndEchoHandler(IAsyncStreamReader<string> requestStream)
{
- responseObserver.OnNext(request);
- responseObserver.OnCompleted();
+ string result = "";
+ await requestStream.ForEach(async (request) =>
+ {
+ if (request == "THROW")
+ {
+ throw new Exception("This was thrown on purpose by a test");
+ }
+ result += request;
+ });
+ // simulate processing takes some time.
+ await Task.Delay(250);
+ return result;
}
}
}
diff --git a/src/csharp/Grpc.Core/Internal/ServerStreamingOutputObserver.cs b/src/csharp/Grpc.Core/AsyncClientStreamingCall.cs
index 97b62d0569..e81ce01ebb 100644
--- a/src/csharp/Grpc.Core/Internal/ServerStreamingOutputObserver.cs
+++ b/src/csharp/Grpc.Core/AsyncClientStreamingCall.cs
@@ -1,4 +1,5 @@
#region Copyright notice and license
+
// Copyright 2015, Google Inc.
// All rights reserved.
//
@@ -27,45 +28,74 @@
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
#endregion
+
using System;
-using Grpc.Core.Internal;
+using System.Runtime.CompilerServices;
+using System.Threading.Tasks;
-namespace Grpc.Core.Internal
+namespace Grpc.Core
{
/// <summary>
- /// Observer that writes all arriving messages to a call abstraction (in blocking fashion)
- /// and then halfcloses the call. Used for server-side call handling.
+ /// Return type for client streaming calls.
/// </summary>
- internal class ServerStreamingOutputObserver<TRequest, TResponse> : IObserver<TResponse>
+ public struct AsyncClientStreamingCall<TRequest, TResponse>
{
- readonly AsyncCallServer<TRequest, TResponse> call;
+ readonly IClientStreamWriter<TRequest> requestStream;
+ readonly Task<TResponse> result;
+
+ public AsyncClientStreamingCall(IClientStreamWriter<TRequest> requestStream, Task<TResponse> result)
+ {
+ this.requestStream = requestStream;
+ this.result = result;
+ }
+
+ /// <summary>
+ /// Writes a request to RequestStream.
+ /// </summary>
+ public Task Write(TRequest message)
+ {
+ return requestStream.Write(message);
+ }
- public ServerStreamingOutputObserver(AsyncCallServer<TRequest, TResponse> call)
+ /// <summary>
+ /// Closes the RequestStream.
+ /// </summary>
+ public Task Close()
{
- this.call = call;
+ return requestStream.Close();
}
- public void OnCompleted()
+ /// <summary>
+ /// Asynchronous call result.
+ /// </summary>
+ public Task<TResponse> Result
{
- var taskSource = new AsyncCompletionTaskSource();
- call.StartSendStatusFromServer(new Status(StatusCode.OK, ""), taskSource.CompletionDelegate);
- // TODO: how bad is the Wait here?
- taskSource.Task.Wait();
+ get
+ {
+ return this.result;
+ }
}
- public void OnError(Exception error)
+ /// <summary>
+ /// Async stream to send streaming requests.
+ /// </summary>
+ public IClientStreamWriter<TRequest> RequestStream
{
- // TODO: implement this...
- throw new InvalidOperationException("This should never be called.");
+ get
+ {
+ return requestStream;
+ }
}
- public void OnNext(TResponse value)
+ /// <summary>
+ /// Allows awaiting this object directly.
+ /// </summary>
+ /// <returns></returns>
+ public TaskAwaiter<TResponse> GetAwaiter()
{
- var taskSource = new AsyncCompletionTaskSource();
- call.StartSendMessage(value, taskSource.CompletionDelegate);
- // TODO: how bad is the Wait here?
- taskSource.Task.Wait();
+ return result.GetAwaiter();
}
}
}
diff --git a/src/csharp/Grpc.Core/AsyncDuplexStreamingCall.cs b/src/csharp/Grpc.Core/AsyncDuplexStreamingCall.cs
new file mode 100644
index 0000000000..1cb30f4779
--- /dev/null
+++ b/src/csharp/Grpc.Core/AsyncDuplexStreamingCall.cs
@@ -0,0 +1,101 @@
+#region Copyright notice and license
+
+// Copyright 2015, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#endregion
+
+using System;
+using System.Runtime.CompilerServices;
+using System.Threading.Tasks;
+
+namespace Grpc.Core
+{
+ /// <summary>
+ /// Return type for bidirectional streaming calls.
+ /// </summary>
+ public struct AsyncDuplexStreamingCall<TRequest, TResponse>
+ {
+ readonly IClientStreamWriter<TRequest> requestStream;
+ readonly IAsyncStreamReader<TResponse> responseStream;
+
+ public AsyncDuplexStreamingCall(IClientStreamWriter<TRequest> requestStream, IAsyncStreamReader<TResponse> responseStream)
+ {
+ this.requestStream = requestStream;
+ this.responseStream = responseStream;
+ }
+
+ /// <summary>
+ /// Writes a request to RequestStream.
+ /// </summary>
+ public Task Write(TRequest message)
+ {
+ return requestStream.Write(message);
+ }
+
+ /// <summary>
+ /// Closes the RequestStream.
+ /// </summary>
+ public Task Close()
+ {
+ return requestStream.Close();
+ }
+
+ /// <summary>
+ /// Reads a response from ResponseStream.
+ /// </summary>
+ /// <returns></returns>
+ public Task<TResponse> ReadNext()
+ {
+ return responseStream.ReadNext();
+ }
+
+ /// <summary>
+ /// Async stream to read streaming responses.
+ /// </summary>
+ public IAsyncStreamReader<TResponse> ResponseStream
+ {
+ get
+ {
+ return responseStream;
+ }
+ }
+
+ /// <summary>
+ /// Async stream to send streaming requests.
+ /// </summary>
+ public IClientStreamWriter<TRequest> RequestStream
+ {
+ get
+ {
+ return requestStream;
+ }
+ }
+ }
+}
diff --git a/src/csharp/Grpc.Core/Utils/RecordingQueue.cs b/src/csharp/Grpc.Core/AsyncServerStreamingCall.cs
index 9749168af0..d614916fb7 100644
--- a/src/csharp/Grpc.Core/Utils/RecordingQueue.cs
+++ b/src/csharp/Grpc.Core/AsyncServerStreamingCall.cs
@@ -32,51 +32,40 @@
#endregion
using System;
-using System.Collections.Concurrent;
-using System.Collections.Generic;
+using System.Runtime.CompilerServices;
using System.Threading.Tasks;
-namespace Grpc.Core.Utils
+namespace Grpc.Core
{
- // TODO: replace this by something that implements IAsyncEnumerator.
/// <summary>
- /// Observer that allows us to await incoming messages one-by-one.
- /// The implementation is not ideal and class will be probably replaced
- /// by something more versatile in the future.
+ /// Return type for server streaming calls.
/// </summary>
- public class RecordingQueue<T> : IObserver<T>
+ public struct AsyncServerStreamingCall<TResponse>
{
- readonly BlockingCollection<T> queue = new BlockingCollection<T>();
- TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
+ readonly IAsyncStreamReader<TResponse> responseStream;
- public void OnCompleted()
+ public AsyncServerStreamingCall(IAsyncStreamReader<TResponse> responseStream)
{
- tcs.SetResult(null);
+ this.responseStream = responseStream;
}
- public void OnError(Exception error)
+ /// <summary>
+ /// Reads the next response from ResponseStream
+ /// </summary>
+ /// <returns></returns>
+ public Task<TResponse> ReadNext()
{
- tcs.SetException(error);
+ return responseStream.ReadNext();
}
- public void OnNext(T value)
- {
- queue.Add(value);
- }
-
- public BlockingCollection<T> Queue
- {
- get
- {
- return queue;
- }
- }
-
- public Task Finished
+ /// <summary>
+ /// Async stream to read streaming responses.
+ /// </summary>
+ public IAsyncStreamReader<TResponse> ResponseStream
{
get
{
- return tcs.Task;
+ return responseStream;
}
}
}
diff --git a/src/csharp/Grpc.Core/Call.cs b/src/csharp/Grpc.Core/Call.cs
index fe5f40f5e9..070dfb569d 100644
--- a/src/csharp/Grpc.Core/Call.cs
+++ b/src/csharp/Grpc.Core/Call.cs
@@ -37,6 +37,9 @@ using Grpc.Core.Utils;
namespace Grpc.Core
{
+ /// <summary>
+ /// Abstraction of a call to be invoked on a client.
+ /// </summary>
public class Call<TRequest, TResponse>
{
readonly string name;
diff --git a/src/csharp/Grpc.Core/Calls.cs b/src/csharp/Grpc.Core/Calls.cs
index 280387b323..a8d2b9498e 100644
--- a/src/csharp/Grpc.Core/Calls.cs
+++ b/src/csharp/Grpc.Core/Calls.cs
@@ -39,13 +39,15 @@ using Grpc.Core.Internal;
namespace Grpc.Core
{
/// <summary>
- /// Helper methods for generated stubs to make RPC calls.
+ /// Helper methods for generated client stubs to make RPC calls.
/// </summary>
public static class Calls
{
public static TResponse BlockingUnaryCall<TRequest, TResponse>(Call<TRequest, TResponse> call, TRequest req, CancellationToken token)
{
var asyncCall = new AsyncCall<TRequest, TResponse>(call.RequestMarshaller.Serializer, call.ResponseMarshaller.Deserializer);
+ // TODO(jtattermusch): this gives a race that cancellation can be requested before the call even starts.
+ RegisterCancellationCallback(asyncCall, token);
return asyncCall.UnaryCall(call.Channel, call.Name, req, call.Headers);
}
@@ -53,38 +55,53 @@ namespace Grpc.Core
{
var asyncCall = new AsyncCall<TRequest, TResponse>(call.RequestMarshaller.Serializer, call.ResponseMarshaller.Deserializer);
asyncCall.Initialize(call.Channel, GetCompletionQueue(), call.Name);
- return await asyncCall.UnaryCallAsync(req, call.Headers);
+ var asyncResult = asyncCall.UnaryCallAsync(req, call.Headers);
+ RegisterCancellationCallback(asyncCall, token);
+ return await asyncResult;
}
- public static void AsyncServerStreamingCall<TRequest, TResponse>(Call<TRequest, TResponse> call, TRequest req, IObserver<TResponse> outputs, CancellationToken token)
+ public static AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>(Call<TRequest, TResponse> call, TRequest req, CancellationToken token)
{
var asyncCall = new AsyncCall<TRequest, TResponse>(call.RequestMarshaller.Serializer, call.ResponseMarshaller.Deserializer);
asyncCall.Initialize(call.Channel, GetCompletionQueue(), call.Name);
- asyncCall.StartServerStreamingCall(req, outputs, call.Headers);
+ asyncCall.StartServerStreamingCall(req, call.Headers);
+ RegisterCancellationCallback(asyncCall, token);
+ var responseStream = new ClientResponseStream<TRequest, TResponse>(asyncCall);
+ return new AsyncServerStreamingCall<TResponse>(responseStream);
}
- public static ClientStreamingAsyncResult<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(Call<TRequest, TResponse> call, CancellationToken token)
+ public static AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(Call<TRequest, TResponse> call, CancellationToken token)
{
var asyncCall = new AsyncCall<TRequest, TResponse>(call.RequestMarshaller.Serializer, call.ResponseMarshaller.Deserializer);
asyncCall.Initialize(call.Channel, GetCompletionQueue(), call.Name);
- var task = asyncCall.ClientStreamingCallAsync(call.Headers);
- var inputs = new ClientStreamingInputObserver<TRequest, TResponse>(asyncCall);
- return new ClientStreamingAsyncResult<TRequest, TResponse>(task, inputs);
+ var resultTask = asyncCall.ClientStreamingCallAsync(call.Headers);
+ RegisterCancellationCallback(asyncCall, token);
+ var requestStream = new ClientRequestStream<TRequest, TResponse>(asyncCall);
+ return new AsyncClientStreamingCall<TRequest, TResponse>(requestStream, resultTask);
}
- public static TResponse BlockingClientStreamingCall<TRequest, TResponse>(Call<TRequest, TResponse> call, IObservable<TRequest> inputs, CancellationToken token)
+ public static AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>(Call<TRequest, TResponse> call, CancellationToken token)
{
- throw new NotImplementedException();
+ var asyncCall = new AsyncCall<TRequest, TResponse>(call.RequestMarshaller.Serializer, call.ResponseMarshaller.Deserializer);
+ asyncCall.Initialize(call.Channel, GetCompletionQueue(), call.Name);
+ asyncCall.StartDuplexStreamingCall(call.Headers);
+ RegisterCancellationCallback(asyncCall, token);
+ var requestStream = new ClientRequestStream<TRequest, TResponse>(asyncCall);
+ var responseStream = new ClientResponseStream<TRequest, TResponse>(asyncCall);
+ return new AsyncDuplexStreamingCall<TRequest, TResponse>(requestStream, responseStream);
}
- public static IObserver<TRequest> DuplexStreamingCall<TRequest, TResponse>(Call<TRequest, TResponse> call, IObserver<TResponse> outputs, CancellationToken token)
+ private static void RegisterCancellationCallback<TRequest, TResponse>(AsyncCall<TRequest, TResponse> asyncCall, CancellationToken token)
{
- var asyncCall = new AsyncCall<TRequest, TResponse>(call.RequestMarshaller.Serializer, call.ResponseMarshaller.Deserializer);
- asyncCall.Initialize(call.Channel, GetCompletionQueue(), call.Name);
- asyncCall.StartDuplexStreamingCall(outputs, call.Headers);
- return new ClientStreamingInputObserver<TRequest, TResponse>(asyncCall);
+ if (token.CanBeCanceled)
+ {
+ token.Register(() => asyncCall.Cancel());
+ }
}
+ /// <summary>
+ /// Gets shared completion queue used for async calls.
+ /// </summary>
private static CompletionQueueSafeHandle GetCompletionQueue()
{
return GrpcEnvironment.ThreadPool.CompletionQueue;
diff --git a/src/csharp/Grpc.Core/Channel.cs b/src/csharp/Grpc.Core/Channel.cs
index 3a42dac1d7..b47d810672 100644
--- a/src/csharp/Grpc.Core/Channel.cs
+++ b/src/csharp/Grpc.Core/Channel.cs
@@ -66,14 +66,6 @@ namespace Grpc.Core
this.target = GetOverridenTarget(target, channelArgs);
}
- internal ChannelSafeHandle Handle
- {
- get
- {
- return this.handle;
- }
- }
-
public string Target
{
get
@@ -88,6 +80,14 @@ namespace Grpc.Core
GC.SuppressFinalize(this);
}
+ internal ChannelSafeHandle Handle
+ {
+ get
+ {
+ return this.handle;
+ }
+ }
+
protected virtual void Dispose(bool disposing)
{
if (handle != null && !handle.IsInvalid)
diff --git a/src/csharp/Grpc.Core/Credentials.cs b/src/csharp/Grpc.Core/Credentials.cs
index 15dd3ef321..e64c1e3dc1 100644
--- a/src/csharp/Grpc.Core/Credentials.cs
+++ b/src/csharp/Grpc.Core/Credentials.cs
@@ -37,7 +37,7 @@ using Grpc.Core.Internal;
namespace Grpc.Core
{
/// <summary>
- /// Client-side credentials.
+ /// Client-side credentials. Used for creation of a secure channel.
/// </summary>
public abstract class Credentials
{
diff --git a/src/csharp/Grpc.Core/Grpc.Core.csproj b/src/csharp/Grpc.Core/Grpc.Core.csproj
index 0b85392e15..9c91541d90 100644
--- a/src/csharp/Grpc.Core/Grpc.Core.csproj
+++ b/src/csharp/Grpc.Core/Grpc.Core.csproj
@@ -5,7 +5,7 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
- <ProductVersion>10.0.0</ProductVersion>
+ <ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{CCC4440E-49F7-4790-B0AF-FEABB0837AE7}</ProjectGuid>
<OutputType>Library</OutputType>
@@ -39,12 +39,18 @@
</Reference>
</ItemGroup>
<ItemGroup>
+ <Compile Include="AsyncDuplexStreamingCall.cs" />
+ <Compile Include="AsyncServerStreamingCall.cs" />
+ <Compile Include="IClientStreamWriter.cs" />
+ <Compile Include="IServerStreamWriter.cs" />
+ <Compile Include="IAsyncStreamWriter.cs" />
+ <Compile Include="IAsyncStreamReader.cs" />
<Compile Include="Internal\GrpcLog.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RpcException.cs" />
<Compile Include="Calls.cs" />
<Compile Include="Call.cs" />
- <Compile Include="ClientStreamingAsyncResult.cs" />
+ <Compile Include="AsyncClientStreamingCall.cs" />
<Compile Include="GrpcEnvironment.cs" />
<Compile Include="Status.cs" />
<Compile Include="StatusCode.cs" />
@@ -59,14 +65,10 @@
<Compile Include="Internal\GrpcThreadPool.cs" />
<Compile Include="Internal\ServerSafeHandle.cs" />
<Compile Include="Method.cs" />
- <Compile Include="ServerCalls.cs" />
<Compile Include="Internal\ServerCallHandler.cs" />
<Compile Include="Marshaller.cs" />
<Compile Include="ServerServiceDefinition.cs" />
- <Compile Include="Utils\RecordingObserver.cs" />
- <Compile Include="Utils\RecordingQueue.cs" />
- <Compile Include="Internal\ClientStreamingInputObserver.cs" />
- <Compile Include="Internal\ServerStreamingOutputObserver.cs" />
+ <Compile Include="Utils\AsyncStreamExtensions.cs" />
<Compile Include="Internal\BatchContextSafeHandleNotOwned.cs" />
<Compile Include="Utils\BenchmarkUtil.cs" />
<Compile Include="Utils\ExceptionHelper.cs" />
@@ -86,6 +88,14 @@
<Compile Include="Internal\MetadataArraySafeHandle.cs" />
<Compile Include="Stub\AbstractStub.cs" />
<Compile Include="Stub\StubConfiguration.cs" />
+ <Compile Include="Internal\ServerCalls.cs" />
+ <Compile Include="ServerMethods.cs" />
+ <Compile Include="Internal\ClientRequestStream.cs" />
+ <Compile Include="Internal\ClientResponseStream.cs" />
+ <Compile Include="Internal\ServerRequestStream.cs" />
+ <Compile Include="Internal\ServerResponseStream.cs" />
+ <Compile Include="Internal\AtomicCounter.cs" />
+ <Compile Include="Internal\DebugStats.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
diff --git a/src/csharp/Grpc.Core/GrpcEnvironment.cs b/src/csharp/Grpc.Core/GrpcEnvironment.cs
index 9c10a42e23..2e9e5a2ef6 100644
--- a/src/csharp/Grpc.Core/GrpcEnvironment.cs
+++ b/src/csharp/Grpc.Core/GrpcEnvironment.cs
@@ -86,6 +86,8 @@ namespace Grpc.Core
{
instance.Close();
instance = null;
+
+ CheckDebugStats();
}
}
}
@@ -132,5 +134,19 @@ namespace Grpc.Core
// TODO: use proper logging here
Console.WriteLine("GRPC shutdown.");
}
+
+ private static void CheckDebugStats()
+ {
+ var remainingClientCalls = DebugStats.ActiveClientCalls.Count;
+ if (remainingClientCalls != 0)
+ {
+ Console.WriteLine("Warning: Detected {0} client calls that weren't disposed properly.", remainingClientCalls);
+ }
+ var remainingServerCalls = DebugStats.ActiveServerCalls.Count;
+ if (remainingServerCalls != 0)
+ {
+ Console.WriteLine("Warning: Detected {0} server calls that weren't disposed properly.", remainingServerCalls);
+ }
+ }
}
}
diff --git a/src/csharp/Grpc.Core/IAsyncStreamReader.cs b/src/csharp/Grpc.Core/IAsyncStreamReader.cs
new file mode 100644
index 0000000000..61cf57f7e0
--- /dev/null
+++ b/src/csharp/Grpc.Core/IAsyncStreamReader.cs
@@ -0,0 +1,54 @@
+#region Copyright notice and license
+
+// Copyright 2015, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#endregion
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Grpc.Core
+{
+ /// <summary>
+ /// A stream of messages to be read.
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ public interface IAsyncStreamReader<T>
+ {
+ /// <summary>
+ /// Reads a single message. Returns default(T) if the last message was already read.
+ /// A following read can only be started when the previous one finishes.
+ /// </summary>
+ Task<T> ReadNext();
+ }
+}
diff --git a/src/csharp/Grpc.Core/IAsyncStreamWriter.cs b/src/csharp/Grpc.Core/IAsyncStreamWriter.cs
new file mode 100644
index 0000000000..724bae8f31
--- /dev/null
+++ b/src/csharp/Grpc.Core/IAsyncStreamWriter.cs
@@ -0,0 +1,54 @@
+#region Copyright notice and license
+
+// Copyright 2015, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#endregion
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Grpc.Core
+{
+ /// <summary>
+ /// A writable stream of messages.
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ public interface IAsyncStreamWriter<T>
+ {
+ /// <summary>
+ /// Writes a single message. Only one write can be pending at a time.
+ /// </summary>
+ /// <param name="message">the message to be written. Cannot be null.</param>
+ Task Write(T message);
+ }
+}
diff --git a/src/csharp/Grpc.Core/IClientStreamWriter.cs b/src/csharp/Grpc.Core/IClientStreamWriter.cs
new file mode 100644
index 0000000000..6da42e9ccc
--- /dev/null
+++ b/src/csharp/Grpc.Core/IClientStreamWriter.cs
@@ -0,0 +1,53 @@
+#region Copyright notice and license
+
+// Copyright 2015, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#endregion
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Grpc.Core
+{
+ /// <summary>
+ /// Client-side writable stream of messages with Close capability.
+ /// </summary>
+ /// <typeparam name="T"></typeparam>
+ public interface IClientStreamWriter<T> : IAsyncStreamWriter<T>
+ {
+ /// <summary>
+ /// Closes the stream. Can only be called once there is no pending write. No writes should follow calling this.
+ /// </summary>
+ Task Close();
+ }
+}
diff --git a/src/csharp/Grpc.Core/IServerStreamWriter.cs b/src/csharp/Grpc.Core/IServerStreamWriter.cs
new file mode 100644
index 0000000000..e76397d8a0
--- /dev/null
+++ b/src/csharp/Grpc.Core/IServerStreamWriter.cs
@@ -0,0 +1,48 @@
+#region Copyright notice and license
+
+// Copyright 2015, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#endregion
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Grpc.Core
+{
+ /// <summary>
+ /// A writable stream of messages that is used in server-side handlers.
+ /// </summary>
+ public interface IServerStreamWriter<T> : IAsyncStreamWriter<T>
+ {
+ }
+}
diff --git a/src/csharp/Grpc.Core/Internal/AsyncCall.cs b/src/csharp/Grpc.Core/Internal/AsyncCall.cs
index b8f4ae0a02..9bb918d53d 100644
--- a/src/csharp/Grpc.Core/Internal/AsyncCall.cs
+++ b/src/csharp/Grpc.Core/Internal/AsyncCall.cs
@@ -43,7 +43,7 @@ using Grpc.Core.Utils;
namespace Grpc.Core.Internal
{
/// <summary>
- /// Handles client side native call lifecycle.
+ /// Manages client side native call lifecycle.
/// </summary>
internal class AsyncCall<TRequest, TResponse> : AsyncCallBase<TRequest, TResponse>
{
@@ -67,6 +67,7 @@ namespace Grpc.Core.Internal
public void Initialize(Channel channel, CompletionQueueSafeHandle cq, string methodName)
{
var call = CallSafeHandle.Create(channel.Handle, cq, methodName, channel.Target, Timespec.InfFuture);
+ DebugStats.ActiveClientCalls.Increment();
InitializeInternal(call);
}
@@ -160,7 +161,7 @@ namespace Grpc.Core.Internal
/// <summary>
/// Starts a unary request - streamed response call.
/// </summary>
- public void StartServerStreamingCall(TRequest msg, IObserver<TResponse> readObserver, Metadata headers)
+ public void StartServerStreamingCall(TRequest msg, Metadata headers)
{
lock (myLock)
{
@@ -169,17 +170,13 @@ namespace Grpc.Core.Internal
started = true;
halfcloseRequested = true;
halfclosed = true; // halfclose not confirmed yet, but it will be once finishedHandler is called.
-
- this.readObserver = readObserver;
byte[] payload = UnsafeSerialize(msg);
-
+
using (var metadataArray = MetadataArraySafeHandle.Create(headers))
{
call.StartServerStreaming(payload, finishedHandler, metadataArray);
}
-
- StartReceiveMessage();
}
}
@@ -187,7 +184,7 @@ namespace Grpc.Core.Internal
/// Starts a streaming request - streaming response call.
/// Use StartSendMessage and StartSendCloseFromClient to stream requests.
/// </summary>
- public void StartDuplexStreamingCall(IObserver<TResponse> readObserver, Metadata headers)
+ public void StartDuplexStreamingCall(Metadata headers)
{
lock (myLock)
{
@@ -195,14 +192,10 @@ namespace Grpc.Core.Internal
started = true;
- this.readObserver = readObserver;
-
using (var metadataArray = MetadataArraySafeHandle.Create(headers))
{
call.StartDuplexStreaming(finishedHandler, metadataArray);
}
-
- StartReceiveMessage();
}
}
@@ -210,17 +203,26 @@ namespace Grpc.Core.Internal
/// Sends a streaming request. Only one pending send action is allowed at any given time.
/// completionDelegate is called when the operation finishes.
/// </summary>
- public void StartSendMessage(TRequest msg, AsyncCompletionDelegate completionDelegate)
+ public void StartSendMessage(TRequest msg, AsyncCompletionDelegate<object> completionDelegate)
{
StartSendMessageInternal(msg, completionDelegate);
}
/// <summary>
+ /// Receives a streaming response. Only one pending read action is allowed at any given time.
+ /// completionDelegate is called when the operation finishes.
+ /// </summary>
+ public void StartReadMessage(AsyncCompletionDelegate<TResponse> completionDelegate)
+ {
+ StartReadMessageInternal(completionDelegate);
+ }
+
+ /// <summary>
/// Sends halfclose, indicating client is done with streaming requests.
/// Only one pending send action is allowed at any given time.
/// completionDelegate is called when the operation finishes.
/// </summary>
- public void StartSendCloseFromClient(AsyncCompletionDelegate completionDelegate)
+ public void StartSendCloseFromClient(AsyncCompletionDelegate<object> completionDelegate)
{
lock (myLock)
{
@@ -235,12 +237,12 @@ namespace Grpc.Core.Internal
}
/// <summary>
- /// On client-side, we only fire readObserver.OnCompleted once all messages have been read
+ /// On client-side, we only fire readCompletionDelegate once all messages have been read
/// and status has been received.
/// </summary>
- protected override void CompleteReadObserver()
+ protected override void ProcessLastRead(AsyncCompletionDelegate<TResponse> completionDelegate)
{
- if (readingDone && finishedStatus.HasValue)
+ if (completionDelegate != null && readingDone && finishedStatus.HasValue)
{
bool shouldComplete;
lock (myLock)
@@ -254,16 +256,21 @@ namespace Grpc.Core.Internal
var status = finishedStatus.Value;
if (status.StatusCode != StatusCode.OK)
{
- FireReadObserverOnError(new RpcException(status));
+ FireCompletion(completionDelegate, default(TResponse), new RpcException(status));
}
else
{
- FireReadObserverOnCompleted();
+ FireCompletion(completionDelegate, default(TResponse), null);
}
}
}
}
+ protected override void OnReleaseResources()
+ {
+ DebugStats.ActiveClientCalls.Decrement();
+ }
+
/// <summary>
/// Handler for unary response completion.
/// </summary>
@@ -304,15 +311,18 @@ namespace Grpc.Core.Internal
{
var status = ctx.GetReceivedStatus();
+ AsyncCompletionDelegate<TResponse> origReadCompletionDelegate = null;
lock (myLock)
{
finished = true;
finishedStatus = status;
+ origReadCompletionDelegate = readCompletionDelegate;
+
ReleaseResourcesIfPossible();
}
- CompleteReadObserver();
+ ProcessLastRead(origReadCompletionDelegate);
}
}
} \ No newline at end of file
diff --git a/src/csharp/Grpc.Core/Internal/AsyncCallBase.cs b/src/csharp/Grpc.Core/Internal/AsyncCallBase.cs
index eb7b8b1ffc..b4f4edb17a 100644
--- a/src/csharp/Grpc.Core/Internal/AsyncCallBase.cs
+++ b/src/csharp/Grpc.Core/Internal/AsyncCallBase.cs
@@ -44,7 +44,7 @@ namespace Grpc.Core.Internal
{
/// <summary>
/// Base for handling both client side and server side calls.
- /// Handles native call lifecycle and provides convenience methods.
+ /// Manages native call lifecycle and provides convenience methods.
/// </summary>
internal abstract class AsyncCallBase<TWrite, TRead>
{
@@ -65,16 +65,14 @@ namespace Grpc.Core.Internal
protected bool errorOccured;
protected bool cancelRequested;
- protected AsyncCompletionDelegate sendCompletionDelegate; // Completion of a pending send or sendclose if not null.
- protected bool readPending; // True if there is a read in progress.
+ protected AsyncCompletionDelegate<object> sendCompletionDelegate; // Completion of a pending send or sendclose if not null.
+ protected AsyncCompletionDelegate<TRead> readCompletionDelegate; // Completion of a pending send or sendclose if not null.
+
protected bool readingDone;
protected bool halfcloseRequested;
protected bool halfclosed;
protected bool finished; // True if close has been received from the peer.
- // Streaming reads will be delivered to this observer. For a call that only does unary read it may remain null.
- protected IObserver<TRead> readObserver;
-
public AsyncCallBase(Func<TWrite, byte[]> serializer, Func<byte[], TRead> deserializer)
{
this.serializer = Preconditions.CheckNotNull(serializer);
@@ -131,10 +129,10 @@ namespace Grpc.Core.Internal
}
/// <summary>
- /// Initiates sending a message. Only once send operation can be active at a time.
+ /// Initiates sending a message. Only one send operation can be active at a time.
/// completionDelegate is invoked upon completion.
/// </summary>
- protected void StartSendMessageInternal(TWrite msg, AsyncCompletionDelegate completionDelegate)
+ protected void StartSendMessageInternal(TWrite msg, AsyncCompletionDelegate<object> completionDelegate)
{
byte[] payload = UnsafeSerialize(msg);
@@ -149,31 +147,29 @@ namespace Grpc.Core.Internal
}
/// <summary>
- /// Requests receiving a next message.
+ /// Initiates reading a message. Only one read operation can be active at a time.
+ /// completionDelegate is invoked upon completion.
/// </summary>
- protected void StartReceiveMessage()
+ protected void StartReadMessageInternal(AsyncCompletionDelegate<TRead> completionDelegate)
{
lock (myLock)
{
- Preconditions.CheckState(started);
- Preconditions.CheckState(!disposed);
- Preconditions.CheckState(!errorOccured);
-
- Preconditions.CheckState(!readingDone);
- Preconditions.CheckState(!readPending);
+ Preconditions.CheckNotNull(completionDelegate, "Completion delegate cannot be null");
+ CheckReadingAllowed();
call.StartReceiveMessage(readFinishedHandler);
- readPending = true;
+ readCompletionDelegate = completionDelegate;
}
}
+ // TODO(jtattermusch): find more fitting name for this method.
/// <summary>
/// Default behavior just completes the read observer, but more sofisticated behavior might be required
/// by subclasses.
/// </summary>
- protected virtual void CompleteReadObserver()
+ protected virtual void ProcessLastRead(AsyncCompletionDelegate<TRead> completionDelegate)
{
- FireReadObserverOnCompleted();
+ FireCompletion(completionDelegate, default(TRead), null);
}
/// <summary>
@@ -184,7 +180,8 @@ namespace Grpc.Core.Internal
{
if (!disposed && call != null)
{
- if (halfclosed && readingDone && finished)
+ bool noMoreSendCompletions = halfclosed || (cancelRequested && sendCompletionDelegate == null);
+ if (noMoreSendCompletions && readingDone && finished)
{
ReleaseResources();
return true;
@@ -195,6 +192,7 @@ namespace Grpc.Core.Internal
private void ReleaseResources()
{
+ OnReleaseResources();
if (call != null)
{
call.Dispose();
@@ -203,16 +201,39 @@ namespace Grpc.Core.Internal
disposed = true;
}
+ protected virtual void OnReleaseResources()
+ {
+ }
+
protected void CheckSendingAllowed()
{
Preconditions.CheckState(started);
- Preconditions.CheckState(!disposed);
Preconditions.CheckState(!errorOccured);
+ CheckNotCancelled();
+ Preconditions.CheckState(!disposed);
Preconditions.CheckState(!halfcloseRequested, "Already halfclosed.");
Preconditions.CheckState(sendCompletionDelegate == null, "Only one write can be pending at a time");
}
+ protected void CheckReadingAllowed()
+ {
+ Preconditions.CheckState(started);
+ Preconditions.CheckState(!disposed);
+ Preconditions.CheckState(!errorOccured);
+
+ Preconditions.CheckState(!readingDone, "Stream has already been closed.");
+ Preconditions.CheckState(readCompletionDelegate == null, "Only one read can be pending at a time");
+ }
+
+ protected void CheckNotCancelled()
+ {
+ if (cancelRequested)
+ {
+ throw new OperationCanceledException("Remote call has been cancelled.");
+ }
+ }
+
protected byte[] UnsafeSerialize(TWrite msg)
{
return serializer(msg);
@@ -248,47 +269,11 @@ namespace Grpc.Core.Internal
}
}
- protected void FireReadObserverOnNext(TRead value)
+ protected void FireCompletion<T>(AsyncCompletionDelegate<T> completionDelegate, T value, Exception error)
{
try
{
- readObserver.OnNext(value);
- }
- catch (Exception e)
- {
- Console.WriteLine("Exception occured while invoking readObserver.OnNext: " + e);
- }
- }
-
- protected void FireReadObserverOnCompleted()
- {
- try
- {
- readObserver.OnCompleted();
- }
- catch (Exception e)
- {
- Console.WriteLine("Exception occured while invoking readObserver.OnCompleted: " + e);
- }
- }
-
- protected void FireReadObserverOnError(Exception error)
- {
- try
- {
- readObserver.OnError(error);
- }
- catch (Exception e)
- {
- Console.WriteLine("Exception occured while invoking readObserver.OnError: " + e);
- }
- }
-
- protected void FireCompletion(AsyncCompletionDelegate completionDelegate, Exception error)
- {
- try
- {
- completionDelegate(error);
+ completionDelegate(value, error);
}
catch (Exception e)
{
@@ -321,7 +306,7 @@ namespace Grpc.Core.Internal
/// </summary>
private void HandleSendFinished(bool success, BatchContextSafeHandleNotOwned ctx)
{
- AsyncCompletionDelegate origCompletionDelegate = null;
+ AsyncCompletionDelegate<object> origCompletionDelegate = null;
lock (myLock)
{
origCompletionDelegate = sendCompletionDelegate;
@@ -332,11 +317,11 @@ namespace Grpc.Core.Internal
if (!success)
{
- FireCompletion(origCompletionDelegate, new OperationFailedException("Send failed"));
+ FireCompletion(origCompletionDelegate, null, new OperationFailedException("Send failed"));
}
else
{
- FireCompletion(origCompletionDelegate, null);
+ FireCompletion(origCompletionDelegate, null, null);
}
}
@@ -345,7 +330,7 @@ namespace Grpc.Core.Internal
/// </summary>
private void HandleHalfclosed(bool success, BatchContextSafeHandleNotOwned ctx)
{
- AsyncCompletionDelegate origCompletionDelegate = null;
+ AsyncCompletionDelegate<object> origCompletionDelegate = null;
lock (myLock)
{
halfclosed = true;
@@ -357,11 +342,11 @@ namespace Grpc.Core.Internal
if (!success)
{
- FireCompletion(origCompletionDelegate, new OperationFailedException("Halfclose failed"));
+ FireCompletion(origCompletionDelegate, null, new OperationFailedException("Halfclose failed"));
}
else
{
- FireCompletion(origCompletionDelegate, null);
+ FireCompletion(origCompletionDelegate, null, null);
}
}
@@ -372,11 +357,19 @@ namespace Grpc.Core.Internal
{
var payload = ctx.GetReceivedMessage();
+ AsyncCompletionDelegate<TRead> origCompletionDelegate = null;
lock (myLock)
{
- readPending = false;
- if (payload == null)
+ origCompletionDelegate = readCompletionDelegate;
+ if (payload != null)
{
+ readCompletionDelegate = null;
+ }
+ else
+ {
+ // This was the last read. Keeping the readCompletionDelegate
+ // to be either fired by this handler or by client-side finished
+ // handler.
readingDone = true;
}
@@ -391,15 +384,11 @@ namespace Grpc.Core.Internal
TRead msg;
TryDeserialize(payload, out msg);
- FireReadObserverOnNext(msg);
-
- // Start a new read. The current one has already been delivered,
- // so correct ordering of reads is assured.
- StartReceiveMessage();
+ FireCompletion(origCompletionDelegate, msg, null);
}
else
{
- CompleteReadObserver();
+ ProcessLastRead(origCompletionDelegate);
}
}
}
diff --git a/src/csharp/Grpc.Core/Internal/AsyncCallServer.cs b/src/csharp/Grpc.Core/Internal/AsyncCallServer.cs
index 5de270ca95..91106cd407 100644
--- a/src/csharp/Grpc.Core/Internal/AsyncCallServer.cs
+++ b/src/csharp/Grpc.Core/Internal/AsyncCallServer.cs
@@ -43,7 +43,7 @@ using Grpc.Core.Utils;
namespace Grpc.Core.Internal
{
/// <summary>
- /// Handles server side native call lifecycle.
+ /// Manages server side native call lifecycle.
/// </summary>
internal class AsyncCallServer<TRequest, TResponse> : AsyncCallBase<TResponse, TRequest>
{
@@ -57,24 +57,22 @@ namespace Grpc.Core.Internal
public void Initialize(CallSafeHandle call)
{
+ DebugStats.ActiveServerCalls.Increment();
InitializeInternal(call);
}
/// <summary>
- /// Starts a server side call. Currently, all server side calls are implemented as duplex
- /// streaming call and they are adapted to the appropriate streaming arity.
+ /// Starts a server side call.
/// </summary>
- public Task ServerSideCallAsync(IObserver<TRequest> readObserver)
+ public Task ServerSideCallAsync()
{
lock (myLock)
{
Preconditions.CheckNotNull(call);
started = true;
- this.readObserver = readObserver;
call.StartServerSide(finishedServersideHandler);
- StartReceiveMessage();
return finishedServersideTcs.Task;
}
}
@@ -83,17 +81,26 @@ namespace Grpc.Core.Internal
/// Sends a streaming response. Only one pending send action is allowed at any given time.
/// completionDelegate is called when the operation finishes.
/// </summary>
- public void StartSendMessage(TResponse msg, AsyncCompletionDelegate completionDelegate)
+ public void StartSendMessage(TResponse msg, AsyncCompletionDelegate<object> completionDelegate)
{
StartSendMessageInternal(msg, completionDelegate);
}
/// <summary>
+ /// Receives a streaming request. Only one pending read action is allowed at any given time.
+ /// completionDelegate is called when the operation finishes.
+ /// </summary>
+ public void StartReadMessage(AsyncCompletionDelegate<TRequest> completionDelegate)
+ {
+ StartReadMessageInternal(completionDelegate);
+ }
+
+ /// <summary>
/// Sends call result status, also indicating server is done with streaming responses.
/// Only one pending send action is allowed at any given time.
/// completionDelegate is called when the operation finishes.
/// </summary>
- public void StartSendStatusFromServer(Status status, AsyncCompletionDelegate completionDelegate)
+ public void StartSendStatusFromServer(Status status, AsyncCompletionDelegate<object> completionDelegate)
{
lock (myLock)
{
@@ -106,17 +113,33 @@ namespace Grpc.Core.Internal
}
}
+ protected override void OnReleaseResources()
+ {
+ DebugStats.ActiveServerCalls.Decrement();
+ }
+
/// <summary>
/// Handles the server side close completion.
/// </summary>
private void HandleFinishedServerside(bool success, BatchContextSafeHandleNotOwned ctx)
{
+ bool cancelled = ctx.GetReceivedCloseOnServerCancelled();
+
lock (myLock)
{
finished = true;
+ if (cancelled)
+ {
+ // Once we cancel, we don't have to care that much
+ // about reads and writes.
+ Cancel();
+ }
+
ReleaseResourcesIfPossible();
}
+ // TODO(jtattermusch): check if call was cancelled.
+
// TODO: handle error ...
finishedServersideTcs.SetResult(null);
diff --git a/src/csharp/Grpc.Core/Internal/AsyncCompletion.cs b/src/csharp/Grpc.Core/Internal/AsyncCompletion.cs
index 673b527fb2..c88cae98fe 100644
--- a/src/csharp/Grpc.Core/Internal/AsyncCompletion.cs
+++ b/src/csharp/Grpc.Core/Internal/AsyncCompletion.cs
@@ -45,22 +45,22 @@ namespace Grpc.Core.Internal
/// <summary>
/// If error != null, there's been an error or operation has been cancelled.
/// </summary>
- internal delegate void AsyncCompletionDelegate(Exception error);
+ internal delegate void AsyncCompletionDelegate<T>(T result, Exception error);
/// <summary>
/// Helper for transforming AsyncCompletionDelegate into full-fledged Task.
/// </summary>
- internal class AsyncCompletionTaskSource
+ internal class AsyncCompletionTaskSource<T>
{
- readonly TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
- readonly AsyncCompletionDelegate completionDelegate;
+ readonly TaskCompletionSource<T> tcs = new TaskCompletionSource<T>();
+ readonly AsyncCompletionDelegate<T> completionDelegate;
public AsyncCompletionTaskSource()
{
- completionDelegate = new AsyncCompletionDelegate(HandleCompletion);
+ completionDelegate = new AsyncCompletionDelegate<T>(HandleCompletion);
}
- public Task Task
+ public Task<T> Task
{
get
{
@@ -68,7 +68,7 @@ namespace Grpc.Core.Internal
}
}
- public AsyncCompletionDelegate CompletionDelegate
+ public AsyncCompletionDelegate<T> CompletionDelegate
{
get
{
@@ -76,11 +76,11 @@ namespace Grpc.Core.Internal
}
}
- private void HandleCompletion(Exception error)
+ private void HandleCompletion(T value, Exception error)
{
if (error == null)
{
- tcs.SetResult(null);
+ tcs.SetResult(value);
return;
}
if (error is OperationCanceledException)
diff --git a/src/csharp/Grpc.Core/ClientStreamingAsyncResult.cs b/src/csharp/Grpc.Core/Internal/AtomicCounter.cs
index 65bedb0a33..7ccda225dc 100644
--- a/src/csharp/Grpc.Core/ClientStreamingAsyncResult.cs
+++ b/src/csharp/Grpc.Core/Internal/AtomicCounter.cs
@@ -32,37 +32,29 @@
#endregion
using System;
-using System.Threading.Tasks;
+using System.Threading;
-namespace Grpc.Core
+namespace Grpc.Core.Internal
{
- /// <summary>
- /// Return type for client streaming async method.
- /// </summary>
- public struct ClientStreamingAsyncResult<TRequest, TResponse>
+ internal class AtomicCounter
{
- readonly Task<TResponse> task;
- readonly IObserver<TRequest> inputs;
+ long counter = 0;
- public ClientStreamingAsyncResult(Task<TResponse> task, IObserver<TRequest> inputs)
+ public void Increment()
{
- this.task = task;
- this.inputs = inputs;
+ Interlocked.Increment(ref counter);
}
- public Task<TResponse> Task
+ public void Decrement()
{
- get
- {
- return this.task;
- }
+ Interlocked.Decrement(ref counter);
}
- public IObserver<TRequest> Inputs
+ public long Count
{
get
{
- return this.inputs;
+ return counter;
}
}
}
diff --git a/src/csharp/Grpc.Core/Internal/BatchContextSafeHandleNotOwned.cs b/src/csharp/Grpc.Core/Internal/BatchContextSafeHandleNotOwned.cs
index 3c54753756..b562abaa7a 100644
--- a/src/csharp/Grpc.Core/Internal/BatchContextSafeHandleNotOwned.cs
+++ b/src/csharp/Grpc.Core/Internal/BatchContextSafeHandleNotOwned.cs
@@ -61,6 +61,9 @@ namespace Grpc.Core.Internal
[DllImport("grpc_csharp_ext.dll")]
static extern IntPtr grpcsharp_batch_context_server_rpc_new_method(BatchContextSafeHandleNotOwned ctx); // returns const char*
+ [DllImport("grpc_csharp_ext.dll")]
+ static extern int grpcsharp_batch_context_recv_close_on_server_cancelled(BatchContextSafeHandleNotOwned ctx);
+
public BatchContextSafeHandleNotOwned(IntPtr handle) : base(false)
{
SetHandle(handle);
@@ -94,5 +97,10 @@ namespace Grpc.Core.Internal
{
return Marshal.PtrToStringAnsi(grpcsharp_batch_context_server_rpc_new_method(this));
}
+
+ public bool GetReceivedCloseOnServerCancelled()
+ {
+ return grpcsharp_batch_context_recv_close_on_server_cancelled(this) != 0;
+ }
}
} \ No newline at end of file
diff --git a/src/csharp/Grpc.Core/Internal/ClientStreamingInputObserver.cs b/src/csharp/Grpc.Core/Internal/ClientRequestStream.cs
index 286c54f2c4..6854922a6f 100644
--- a/src/csharp/Grpc.Core/Internal/ClientStreamingInputObserver.cs
+++ b/src/csharp/Grpc.Core/Internal/ClientRequestStream.cs
@@ -29,38 +29,35 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
using System;
+using System.Threading.Tasks;
using Grpc.Core.Internal;
namespace Grpc.Core.Internal
{
- internal class ClientStreamingInputObserver<TWrite, TRead> : IObserver<TWrite>
+ /// <summary>
+ /// Writes requests asynchronously to an underlying AsyncCall object.
+ /// </summary>
+ internal class ClientRequestStream<TRequest, TResponse> : IClientStreamWriter<TRequest>
{
- readonly AsyncCall<TWrite, TRead> call;
+ readonly AsyncCall<TRequest, TResponse> call;
- public ClientStreamingInputObserver(AsyncCall<TWrite, TRead> call)
+ public ClientRequestStream(AsyncCall<TRequest, TResponse> call)
{
this.call = call;
}
- public void OnCompleted()
+ public Task Write(TRequest message)
{
- var taskSource = new AsyncCompletionTaskSource();
- call.StartSendCloseFromClient(taskSource.CompletionDelegate);
- // TODO: how bad is the Wait here?
- taskSource.Task.Wait();
+ var taskSource = new AsyncCompletionTaskSource<object>();
+ call.StartSendMessage(message, taskSource.CompletionDelegate);
+ return taskSource.Task;
}
- public void OnError(Exception error)
+ public Task Close()
{
- throw new InvalidOperationException("This should never be called.");
- }
-
- public void OnNext(TWrite value)
- {
- var taskSource = new AsyncCompletionTaskSource();
- call.StartSendMessage(value, taskSource.CompletionDelegate);
- // TODO: how bad is the Wait here?
- taskSource.Task.Wait();
+ var taskSource = new AsyncCompletionTaskSource<object>();
+ call.StartSendCloseFromClient(taskSource.CompletionDelegate);
+ return taskSource.Task;
}
}
}
diff --git a/src/csharp/Grpc.Core/Utils/RecordingObserver.cs b/src/csharp/Grpc.Core/Internal/ClientResponseStream.cs
index 7b43ab8ad5..7fa511faa8 100644
--- a/src/csharp/Grpc.Core/Utils/RecordingObserver.cs
+++ b/src/csharp/Grpc.Core/Internal/ClientResponseStream.cs
@@ -35,31 +35,22 @@ using System;
using System.Collections.Generic;
using System.Threading.Tasks;
-namespace Grpc.Core.Utils
+namespace Grpc.Core.Internal
{
- public class RecordingObserver<T> : IObserver<T>
+ internal class ClientResponseStream<TRequest, TResponse> : IAsyncStreamReader<TResponse>
{
- TaskCompletionSource<List<T>> tcs = new TaskCompletionSource<List<T>>();
- List<T> data = new List<T>();
+ readonly AsyncCall<TRequest, TResponse> call;
- public void OnCompleted()
+ public ClientResponseStream(AsyncCall<TRequest, TResponse> call)
{
- tcs.SetResult(data);
+ this.call = call;
}
- public void OnError(Exception error)
+ public Task<TResponse> ReadNext()
{
- tcs.SetException(error);
- }
-
- public void OnNext(T value)
- {
- data.Add(value);
- }
-
- public Task<List<T>> ToList()
- {
- return tcs.Task;
+ var taskSource = new AsyncCompletionTaskSource<TResponse>();
+ call.StartReadMessage(taskSource.CompletionDelegate);
+ return taskSource.Task;
}
}
}
diff --git a/src/csharp/Grpc.Core/Internal/DebugStats.cs b/src/csharp/Grpc.Core/Internal/DebugStats.cs
new file mode 100644
index 0000000000..476914f751
--- /dev/null
+++ b/src/csharp/Grpc.Core/Internal/DebugStats.cs
@@ -0,0 +1,45 @@
+#region Copyright notice and license
+
+// Copyright 2015, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#endregion
+
+using System;
+using System.Threading;
+
+namespace Grpc.Core.Internal
+{
+ internal static class DebugStats
+ {
+ public static readonly AtomicCounter ActiveClientCalls = new AtomicCounter();
+
+ public static readonly AtomicCounter ActiveServerCalls = new AtomicCounter();
+ }
+}
diff --git a/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs b/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs
index 25fd4fab8f..01b2a11369 100644
--- a/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs
+++ b/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs
@@ -33,6 +33,7 @@
using System;
using System.Linq;
+using System.Threading.Tasks;
using Grpc.Core.Internal;
using Grpc.Core.Utils;
@@ -40,96 +41,229 @@ namespace Grpc.Core.Internal
{
internal interface IServerCallHandler
{
- void StartCall(string methodName, CallSafeHandle call, CompletionQueueSafeHandle cq);
+ Task HandleCall(string methodName, CallSafeHandle call, CompletionQueueSafeHandle cq);
}
- internal class UnaryRequestServerCallHandler<TRequest, TResponse> : IServerCallHandler
+ internal class UnaryServerCallHandler<TRequest, TResponse> : IServerCallHandler
{
readonly Method<TRequest, TResponse> method;
- readonly UnaryRequestServerMethod<TRequest, TResponse> handler;
+ readonly UnaryServerMethod<TRequest, TResponse> handler;
- public UnaryRequestServerCallHandler(Method<TRequest, TResponse> method, UnaryRequestServerMethod<TRequest, TResponse> handler)
+ public UnaryServerCallHandler(Method<TRequest, TResponse> method, UnaryServerMethod<TRequest, TResponse> handler)
{
this.method = method;
this.handler = handler;
}
- public void StartCall(string methodName, CallSafeHandle call, CompletionQueueSafeHandle cq)
+ public async Task HandleCall(string methodName, CallSafeHandle call, CompletionQueueSafeHandle cq)
{
var asyncCall = new AsyncCallServer<TRequest, TResponse>(
method.ResponseMarshaller.Serializer,
method.RequestMarshaller.Deserializer);
asyncCall.Initialize(call);
-
- var requestObserver = new RecordingObserver<TRequest>();
- var finishedTask = asyncCall.ServerSideCallAsync(requestObserver);
+ var finishedTask = asyncCall.ServerSideCallAsync();
+ var requestStream = new ServerRequestStream<TRequest, TResponse>(asyncCall);
+ var responseStream = new ServerResponseStream<TRequest, TResponse>(asyncCall);
- var request = requestObserver.ToList().Result.Single();
- var responseObserver = new ServerStreamingOutputObserver<TRequest, TResponse>(asyncCall);
- handler(request, responseObserver);
-
- finishedTask.Wait();
+ Status status = Status.DefaultSuccess;
+ try
+ {
+ var request = await requestStream.ReadNext();
+ // TODO(jtattermusch): we need to read the full stream so that native callhandle gets deallocated.
+ Preconditions.CheckArgument(await requestStream.ReadNext() == null);
+ var result = await handler(request);
+ await responseStream.Write(result);
+ }
+ catch (Exception e)
+ {
+ Console.WriteLine("Exception occured in handler: " + e);
+ status = HandlerUtils.StatusFromException(e);
+ }
+ try
+ {
+ await responseStream.WriteStatus(status);
+ }
+ catch (OperationCanceledException)
+ {
+ // Call has been already cancelled.
+ }
+ await finishedTask;
}
}
- internal class StreamingRequestServerCallHandler<TRequest, TResponse> : IServerCallHandler
+ internal class ServerStreamingServerCallHandler<TRequest, TResponse> : IServerCallHandler
{
readonly Method<TRequest, TResponse> method;
- readonly StreamingRequestServerMethod<TRequest, TResponse> handler;
+ readonly ServerStreamingServerMethod<TRequest, TResponse> handler;
- public StreamingRequestServerCallHandler(Method<TRequest, TResponse> method, StreamingRequestServerMethod<TRequest, TResponse> handler)
+ public ServerStreamingServerCallHandler(Method<TRequest, TResponse> method, ServerStreamingServerMethod<TRequest, TResponse> handler)
{
this.method = method;
this.handler = handler;
}
- public void StartCall(string methodName, CallSafeHandle call, CompletionQueueSafeHandle cq)
+ public async Task HandleCall(string methodName, CallSafeHandle call, CompletionQueueSafeHandle cq)
{
var asyncCall = new AsyncCallServer<TRequest, TResponse>(
method.ResponseMarshaller.Serializer,
method.RequestMarshaller.Deserializer);
asyncCall.Initialize(call);
+ var finishedTask = asyncCall.ServerSideCallAsync();
+ var requestStream = new ServerRequestStream<TRequest, TResponse>(asyncCall);
+ var responseStream = new ServerResponseStream<TRequest, TResponse>(asyncCall);
+
+ Status status = Status.DefaultSuccess;
+ try
+ {
+ var request = await requestStream.ReadNext();
+ // TODO(jtattermusch): we need to read the full stream so that native callhandle gets deallocated.
+ Preconditions.CheckArgument(await requestStream.ReadNext() == null);
+
+ await handler(request, responseStream);
+ }
+ catch (Exception e)
+ {
+ Console.WriteLine("Exception occured in handler: " + e);
+ status = HandlerUtils.StatusFromException(e);
+ }
- var responseObserver = new ServerStreamingOutputObserver<TRequest, TResponse>(asyncCall);
- var requestObserver = handler(responseObserver);
- var finishedTask = asyncCall.ServerSideCallAsync(requestObserver);
- finishedTask.Wait();
+ try
+ {
+ await responseStream.WriteStatus(status);
+ }
+ catch (OperationCanceledException)
+ {
+ // Call has been already cancelled.
+ }
+ await finishedTask;
}
}
- internal class NoSuchMethodCallHandler : IServerCallHandler
+ internal class ClientStreamingServerCallHandler<TRequest, TResponse> : IServerCallHandler
{
- public void StartCall(string methodName, CallSafeHandle call, CompletionQueueSafeHandle cq)
+ readonly Method<TRequest, TResponse> method;
+ readonly ClientStreamingServerMethod<TRequest, TResponse> handler;
+
+ public ClientStreamingServerCallHandler(Method<TRequest, TResponse> method, ClientStreamingServerMethod<TRequest, TResponse> handler)
{
- // We don't care about the payload type here.
- var asyncCall = new AsyncCallServer<byte[], byte[]>(
- (payload) => payload, (payload) => payload);
+ this.method = method;
+ this.handler = handler;
+ }
- asyncCall.Initialize(call);
+ public async Task HandleCall(string methodName, CallSafeHandle call, CompletionQueueSafeHandle cq)
+ {
+ var asyncCall = new AsyncCallServer<TRequest, TResponse>(
+ method.ResponseMarshaller.Serializer,
+ method.RequestMarshaller.Deserializer);
- var finishedTask = asyncCall.ServerSideCallAsync(new NullObserver<byte[]>());
+ asyncCall.Initialize(call);
+ var finishedTask = asyncCall.ServerSideCallAsync();
+ var requestStream = new ServerRequestStream<TRequest, TResponse>(asyncCall);
+ var responseStream = new ServerResponseStream<TRequest, TResponse>(asyncCall);
- // TODO: check result of the completion status.
- asyncCall.StartSendStatusFromServer(new Status(StatusCode.Unimplemented, "No such method."), new AsyncCompletionDelegate((error) => { }));
+ Status status = Status.DefaultSuccess;
+ try
+ {
+ var result = await handler(requestStream);
+ try
+ {
+ await responseStream.Write(result);
+ }
+ catch (OperationCanceledException)
+ {
+ status = Status.DefaultCancelled;
+ }
+ }
+ catch (Exception e)
+ {
+ Console.WriteLine("Exception occured in handler: " + e);
+ status = HandlerUtils.StatusFromException(e);
+ }
- finishedTask.Wait();
+ try
+ {
+ await responseStream.WriteStatus(status);
+ }
+ catch (OperationCanceledException)
+ {
+ // Call has been already cancelled.
+ }
+ await finishedTask;
}
}
- internal class NullObserver<T> : IObserver<T>
+ internal class DuplexStreamingServerCallHandler<TRequest, TResponse> : IServerCallHandler
{
- public void OnCompleted()
+ readonly Method<TRequest, TResponse> method;
+ readonly DuplexStreamingServerMethod<TRequest, TResponse> handler;
+
+ public DuplexStreamingServerCallHandler(Method<TRequest, TResponse> method, DuplexStreamingServerMethod<TRequest, TResponse> handler)
{
+ this.method = method;
+ this.handler = handler;
+ }
+
+ public async Task HandleCall(string methodName, CallSafeHandle call, CompletionQueueSafeHandle cq)
+ {
+ var asyncCall = new AsyncCallServer<TRequest, TResponse>(
+ method.ResponseMarshaller.Serializer,
+ method.RequestMarshaller.Deserializer);
+
+ asyncCall.Initialize(call);
+ var finishedTask = asyncCall.ServerSideCallAsync();
+ var requestStream = new ServerRequestStream<TRequest, TResponse>(asyncCall);
+ var responseStream = new ServerResponseStream<TRequest, TResponse>(asyncCall);
+
+ Status status = Status.DefaultSuccess;
+ try
+ {
+ await handler(requestStream, responseStream);
+ }
+ catch (Exception e)
+ {
+ Console.WriteLine("Exception occured in handler: " + e);
+ status = HandlerUtils.StatusFromException(e);
+ }
+ try
+ {
+ await responseStream.WriteStatus(status);
+ }
+ catch (OperationCanceledException)
+ {
+ // Call has been already cancelled.
+ }
+ await finishedTask;
}
+ }
- public void OnError(Exception error)
+ internal class NoSuchMethodCallHandler : IServerCallHandler
+ {
+ public async Task HandleCall(string methodName, CallSafeHandle call, CompletionQueueSafeHandle cq)
{
+ // We don't care about the payload type here.
+ var asyncCall = new AsyncCallServer<byte[], byte[]>(
+ (payload) => payload, (payload) => payload);
+
+ asyncCall.Initialize(call);
+ var finishedTask = asyncCall.ServerSideCallAsync();
+ var requestStream = new ServerRequestStream<byte[], byte[]>(asyncCall);
+ var responseStream = new ServerResponseStream<byte[], byte[]>(asyncCall);
+
+ await responseStream.WriteStatus(new Status(StatusCode.Unimplemented, "No such method."));
+ // TODO(jtattermusch): if we don't read what client has sent, the server call never gets disposed.
+ await requestStream.ToList();
+ await finishedTask;
}
+ }
- public void OnNext(T value)
+ internal static class HandlerUtils
+ {
+ public static Status StatusFromException(Exception e)
{
+ // TODO(jtattermusch): what is the right status code here?
+ return new Status(StatusCode.Unknown, "Exception was thrown by handler.");
}
}
}
diff --git a/src/csharp/Grpc.Core/Internal/ServerCalls.cs b/src/csharp/Grpc.Core/Internal/ServerCalls.cs
new file mode 100644
index 0000000000..5c6b335c7f
--- /dev/null
+++ b/src/csharp/Grpc.Core/Internal/ServerCalls.cs
@@ -0,0 +1,63 @@
+#region Copyright notice and license
+
+// Copyright 2015, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#endregion
+
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+using Grpc.Core;
+
+namespace Grpc.Core.Internal
+{
+ internal static class ServerCalls
+ {
+ public static IServerCallHandler UnaryCall<TRequest, TResponse>(Method<TRequest, TResponse> method, UnaryServerMethod<TRequest, TResponse> handler)
+ {
+ return new UnaryServerCallHandler<TRequest, TResponse>(method, handler);
+ }
+
+ public static IServerCallHandler ClientStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, ClientStreamingServerMethod<TRequest, TResponse> handler)
+ {
+ return new ClientStreamingServerCallHandler<TRequest, TResponse>(method, handler);
+ }
+
+ public static IServerCallHandler ServerStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, ServerStreamingServerMethod<TRequest, TResponse> handler)
+ {
+ return new ServerStreamingServerCallHandler<TRequest, TResponse>(method, handler);
+ }
+
+ public static IServerCallHandler DuplexStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, DuplexStreamingServerMethod<TRequest, TResponse> handler)
+ {
+ return new DuplexStreamingServerCallHandler<TRequest, TResponse>(method, handler);
+ }
+ }
+}
diff --git a/src/csharp/Grpc.Core/Internal/ServerRequestStream.cs b/src/csharp/Grpc.Core/Internal/ServerRequestStream.cs
new file mode 100644
index 0000000000..aa311059c3
--- /dev/null
+++ b/src/csharp/Grpc.Core/Internal/ServerRequestStream.cs
@@ -0,0 +1,56 @@
+#region Copyright notice and license
+
+// Copyright 2015, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#endregion
+
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+
+namespace Grpc.Core.Internal
+{
+ internal class ServerRequestStream<TRequest, TResponse> : IAsyncStreamReader<TRequest>
+ {
+ readonly AsyncCallServer<TRequest, TResponse> call;
+
+ public ServerRequestStream(AsyncCallServer<TRequest, TResponse> call)
+ {
+ this.call = call;
+ }
+
+ public Task<TRequest> ReadNext()
+ {
+ var taskSource = new AsyncCompletionTaskSource<TRequest>();
+ call.StartReadMessage(taskSource.CompletionDelegate);
+ return taskSource.Task;
+ }
+ }
+}
diff --git a/src/csharp/Grpc.Core/ServerCalls.cs b/src/csharp/Grpc.Core/Internal/ServerResponseStream.cs
index dcae99446f..686017c048 100644
--- a/src/csharp/Grpc.Core/ServerCalls.cs
+++ b/src/csharp/Grpc.Core/Internal/ServerResponseStream.cs
@@ -1,5 +1,4 @@
#region Copyright notice and license
-
// Copyright 2015, Google Inc.
// All rights reserved.
//
@@ -28,30 +27,38 @@
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-
#endregion
using System;
+using System.Threading.Tasks;
using Grpc.Core.Internal;
-namespace Grpc.Core
+namespace Grpc.Core.Internal
{
- // TODO: perhaps add also serverSideStreaming and clientSideStreaming
-
- public delegate void UnaryRequestServerMethod<TRequest, TResponse>(TRequest request, IObserver<TResponse> responseObserver);
+ /// <summary>
+ /// Writes responses asynchronously to an underlying AsyncCallServer object.
+ /// </summary>
+ internal class ServerResponseStream<TRequest, TResponse> : IServerStreamWriter<TResponse>
+ {
+ readonly AsyncCallServer<TRequest, TResponse> call;
- public delegate IObserver<TRequest> StreamingRequestServerMethod<TRequest, TResponse>(IObserver<TResponse> responseObserver);
+ public ServerResponseStream(AsyncCallServer<TRequest, TResponse> call)
+ {
+ this.call = call;
+ }
- internal static class ServerCalls
- {
- public static IServerCallHandler UnaryRequestCall<TRequest, TResponse>(Method<TRequest, TResponse> method, UnaryRequestServerMethod<TRequest, TResponse> handler)
+ public Task Write(TResponse message)
{
- return new UnaryRequestServerCallHandler<TRequest, TResponse>(method, handler);
+ var taskSource = new AsyncCompletionTaskSource<object>();
+ call.StartSendMessage(message, taskSource.CompletionDelegate);
+ return taskSource.Task;
}
- public static IServerCallHandler StreamingRequestCall<TRequest, TResponse>(Method<TRequest, TResponse> method, StreamingRequestServerMethod<TRequest, TResponse> handler)
+ public Task WriteStatus(Status status)
{
- return new StreamingRequestServerCallHandler<TRequest, TResponse>(method, handler);
+ var taskSource = new AsyncCompletionTaskSource<object>();
+ call.StartSendStatusFromServer(status, taskSource.CompletionDelegate);
+ return taskSource.Task;
}
}
}
diff --git a/src/csharp/Grpc.Core/Method.cs b/src/csharp/Grpc.Core/Method.cs
index 4f97eeef37..156e780c7d 100644
--- a/src/csharp/Grpc.Core/Method.cs
+++ b/src/csharp/Grpc.Core/Method.cs
@@ -35,12 +35,15 @@ using System;
namespace Grpc.Core
{
+ /// <summary>
+ /// Method types supported by gRPC.
+ /// </summary>
public enum MethodType
{
- Unary,
- ClientStreaming,
- ServerStreaming,
- DuplexStreaming
+ Unary, // Unary request, unary response.
+ ClientStreaming, // Streaming request, unary response.
+ ServerStreaming, // Unary request, streaming response.
+ DuplexStreaming // Streaming request, streaming response.
}
/// <summary>
diff --git a/src/csharp/Grpc.Core/Server.cs b/src/csharp/Grpc.Core/Server.cs
index 4f559590f0..1561aec07a 100644
--- a/src/csharp/Grpc.Core/Server.cs
+++ b/src/csharp/Grpc.Core/Server.cs
@@ -181,7 +181,7 @@ namespace Grpc.Core
/// <summary>
/// Selects corresponding handler for given call and handles the call.
/// </summary>
- private void InvokeCallHandler(CallSafeHandle call, string method)
+ private async Task InvokeCallHandler(CallSafeHandle call, string method)
{
try
{
@@ -190,7 +190,7 @@ namespace Grpc.Core
{
callHandler = new NoSuchMethodCallHandler();
}
- callHandler.StartCall(method, call, GetCompletionQueue());
+ await callHandler.HandleCall(method, call, GetCompletionQueue());
}
catch (Exception e)
{
@@ -215,7 +215,7 @@ namespace Grpc.Core
// after server shutdown, the callback returns with null call
if (!call.IsInvalid)
{
- Task.Run(() => InvokeCallHandler(call, method));
+ Task.Run(async () => await InvokeCallHandler(call, method));
}
AllowOneRpc();
diff --git a/src/csharp/Grpc.Core/ServerMethods.cs b/src/csharp/Grpc.Core/ServerMethods.cs
new file mode 100644
index 0000000000..6646bb5a89
--- /dev/null
+++ b/src/csharp/Grpc.Core/ServerMethods.cs
@@ -0,0 +1,61 @@
+#region Copyright notice and license
+
+// Copyright 2015, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#endregion
+
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+
+using Grpc.Core.Internal;
+
+namespace Grpc.Core
+{
+ /// <summary>
+ /// Server-side handler for unary call.
+ /// </summary>
+ public delegate Task<TResponse> UnaryServerMethod<TRequest, TResponse>(TRequest request);
+
+ /// <summary>
+ /// Server-side handler for client streaming call.
+ /// </summary>
+ public delegate Task<TResponse> ClientStreamingServerMethod<TRequest, TResponse>(IAsyncStreamReader<TRequest> requestStream);
+
+ /// <summary>
+ /// Server-side handler for server streaming call.
+ /// </summary>
+ public delegate Task ServerStreamingServerMethod<TRequest, TResponse>(TRequest request, IServerStreamWriter<TResponse> responseStream);
+
+ /// <summary>
+ /// Server-side handler for bidi streaming call.
+ /// </summary>
+ public delegate Task DuplexStreamingServerMethod<TRequest, TResponse>(IAsyncStreamReader<TRequest> requestStream, IServerStreamWriter<TResponse> responseStream);
+}
diff --git a/src/csharp/Grpc.Core/ServerServiceDefinition.cs b/src/csharp/Grpc.Core/ServerServiceDefinition.cs
index f08c7d88f3..01b1dc8f7b 100644
--- a/src/csharp/Grpc.Core/ServerServiceDefinition.cs
+++ b/src/csharp/Grpc.Core/ServerServiceDefinition.cs
@@ -75,17 +75,33 @@ namespace Grpc.Core
public Builder AddMethod<TRequest, TResponse>(
Method<TRequest, TResponse> method,
- UnaryRequestServerMethod<TRequest, TResponse> handler)
+ UnaryServerMethod<TRequest, TResponse> handler)
{
- callHandlers.Add(GetFullMethodName(serviceName, method.Name), ServerCalls.UnaryRequestCall(method, handler));
+ callHandlers.Add(GetFullMethodName(serviceName, method.Name), ServerCalls.UnaryCall(method, handler));
return this;
}
public Builder AddMethod<TRequest, TResponse>(
Method<TRequest, TResponse> method,
- StreamingRequestServerMethod<TRequest, TResponse> handler)
+ ClientStreamingServerMethod<TRequest, TResponse> handler)
{
- callHandlers.Add(GetFullMethodName(serviceName, method.Name), ServerCalls.StreamingRequestCall(method, handler));
+ callHandlers.Add(GetFullMethodName(serviceName, method.Name), ServerCalls.ClientStreamingCall(method, handler));
+ return this;
+ }
+
+ public Builder AddMethod<TRequest, TResponse>(
+ Method<TRequest, TResponse> method,
+ ServerStreamingServerMethod<TRequest, TResponse> handler)
+ {
+ callHandlers.Add(GetFullMethodName(serviceName, method.Name), ServerCalls.ServerStreamingCall(method, handler));
+ return this;
+ }
+
+ public Builder AddMethod<TRequest, TResponse>(
+ Method<TRequest, TResponse> method,
+ DuplexStreamingServerMethod<TRequest, TResponse> handler)
+ {
+ callHandlers.Add(GetFullMethodName(serviceName, method.Name), ServerCalls.DuplexStreamingCall(method, handler));
return this;
}
diff --git a/src/csharp/Grpc.Core/Status.cs b/src/csharp/Grpc.Core/Status.cs
index 7d76aec4d1..754f6cb3ca 100644
--- a/src/csharp/Grpc.Core/Status.cs
+++ b/src/csharp/Grpc.Core/Status.cs
@@ -39,6 +39,16 @@ namespace Grpc.Core
/// </summary>
public struct Status
{
+ /// <summary>
+ /// Default result of a successful RPC. StatusCode=OK, empty details message.
+ /// </summary>
+ public static readonly Status DefaultSuccess = new Status(StatusCode.OK, "");
+
+ /// <summary>
+ /// Default result of a cancelled RPC. StatusCode=Cancelled, empty details message.
+ /// </summary>
+ public static readonly Status DefaultCancelled = new Status(StatusCode.Cancelled, "");
+
readonly StatusCode statusCode;
readonly string detail;
diff --git a/src/csharp/Grpc.Core/Utils/AsyncStreamExtensions.cs b/src/csharp/Grpc.Core/Utils/AsyncStreamExtensions.cs
new file mode 100644
index 0000000000..f915155f8a
--- /dev/null
+++ b/src/csharp/Grpc.Core/Utils/AsyncStreamExtensions.cs
@@ -0,0 +1,111 @@
+#region Copyright notice and license
+
+// Copyright 2015, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#endregion
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace Grpc.Core.Utils
+{
+ /// <summary>
+ /// Extension methods that simplify work with gRPC streaming calls.
+ /// </summary>
+ public static class AsyncStreamExtensions
+ {
+ /// <summary>
+ /// Reads the entire stream and executes an async action for each element.
+ /// </summary>
+ public static async Task ForEach<T>(this IAsyncStreamReader<T> streamReader, Func<T, Task> asyncAction)
+ where T : class
+ {
+ while (true)
+ {
+ var elem = await streamReader.ReadNext();
+ if (elem == null)
+ {
+ break;
+ }
+ await asyncAction(elem);
+ }
+ }
+
+ /// <summary>
+ /// Reads the entire stream and creates a list containing all the elements read.
+ /// </summary>
+ public static async Task<List<T>> ToList<T>(this IAsyncStreamReader<T> streamReader)
+ where T : class
+ {
+ var result = new List<T>();
+ while (true)
+ {
+ var elem = await streamReader.ReadNext();
+ if (elem == null)
+ {
+ break;
+ }
+ result.Add(elem);
+ }
+ return result;
+ }
+
+ /// <summary>
+ /// Writes all elements from given enumerable to the stream.
+ /// Closes the stream afterwards unless close = false.
+ /// </summary>
+ public static async Task WriteAll<T>(this IClientStreamWriter<T> streamWriter, IEnumerable<T> elements, bool close = true)
+ where T : class
+ {
+ foreach (var element in elements)
+ {
+ await streamWriter.Write(element);
+ }
+ if (close)
+ {
+ await streamWriter.Close();
+ }
+ }
+
+ /// <summary>
+ /// Writes all elements from given enumerable to the stream.
+ /// </summary>
+ public static async Task WriteAll<T>(this IServerStreamWriter<T> streamWriter, IEnumerable<T> elements)
+ where T : class
+ {
+ foreach (var element in elements)
+ {
+ await streamWriter.Write(element);
+ }
+ }
+ }
+}
diff --git a/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs b/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs
index fa5d6688a6..332795e0e5 100644
--- a/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs
+++ b/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs
@@ -63,7 +63,7 @@ namespace math.Tests
server.Start();
channel = new Channel(host + ":" + port);
- // TODO: get rid of the custom header here once we have dedicated tests
+ // TODO(jtattermusch): get rid of the custom header here once we have dedicated tests
// for header support.
var stubConfig = new StubConfiguration((headerBuilder) =>
{
@@ -97,55 +97,67 @@ namespace math.Tests
Assert.AreEqual(0, response.Remainder);
}
- // TODO: test division by zero
+ // TODO(jtattermusch): test division by zero
[Test]
public void DivAsync()
{
- DivReply response = client.DivAsync(new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build()).Result;
- Assert.AreEqual(3, response.Quotient);
- Assert.AreEqual(1, response.Remainder);
+ Task.Run(async () =>
+ {
+ DivReply response = await client.DivAsync(new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build());
+ Assert.AreEqual(3, response.Quotient);
+ Assert.AreEqual(1, response.Remainder);
+ }).Wait();
}
[Test]
public void Fib()
{
- var recorder = new RecordingObserver<Num>();
- client.Fib(new FibArgs.Builder { Limit = 6 }.Build(), recorder);
+ Task.Run(async () =>
+ {
+ var call = client.Fib(new FibArgs.Builder { Limit = 6 }.Build());
- CollectionAssert.AreEqual(new List<long> { 1, 1, 2, 3, 5, 8 },
- recorder.ToList().Result.ConvertAll((n) => n.Num_));
+ var responses = await call.ResponseStream.ToList();
+ CollectionAssert.AreEqual(new List<long> { 1, 1, 2, 3, 5, 8 },
+ responses.ConvertAll((n) => n.Num_));
+ }).Wait();
}
// TODO: test Fib with limit=0 and cancellation
[Test]
public void Sum()
{
- var clientStreamingResult = client.Sum();
- var numList = new List<long> { 10, 20, 30 }.ConvertAll(
- n => Num.CreateBuilder().SetNum_(n).Build());
- numList.Subscribe(clientStreamingResult.Inputs);
-
- Assert.AreEqual(60, clientStreamingResult.Task.Result.Num_);
+ Task.Run(async () =>
+ {
+ var call = client.Sum();
+ var numbers = new List<long> { 10, 20, 30 }.ConvertAll(
+ n => Num.CreateBuilder().SetNum_(n).Build());
+
+ await call.RequestStream.WriteAll(numbers);
+ var result = await call.Result;
+ Assert.AreEqual(60, result.Num_);
+ }).Wait();
}
[Test]
public void DivMany()
{
- List<DivArgs> divArgsList = new List<DivArgs>
+ Task.Run(async () =>
{
- new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build(),
- new DivArgs.Builder { Dividend = 100, Divisor = 21 }.Build(),
- new DivArgs.Builder { Dividend = 7, Divisor = 2 }.Build()
- };
-
- var recorder = new RecordingObserver<DivReply>();
- var requestObserver = client.DivMany(recorder);
- divArgsList.Subscribe(requestObserver);
- var result = recorder.ToList().Result;
-
- CollectionAssert.AreEqual(new long[] { 3, 4, 3 }, result.ConvertAll((divReply) => divReply.Quotient));
- CollectionAssert.AreEqual(new long[] { 1, 16, 1 }, result.ConvertAll((divReply) => divReply.Remainder));
+ var divArgsList = new List<DivArgs>
+ {
+ new DivArgs.Builder { Dividend = 10, Divisor = 3 }.Build(),
+ new DivArgs.Builder { Dividend = 100, Divisor = 21 }.Build(),
+ new DivArgs.Builder { Dividend = 7, Divisor = 2 }.Build()
+ };
+
+ var call = client.DivMany();
+ await call.RequestStream.WriteAll(divArgsList);
+ var result = await call.ResponseStream.ToList();
+
+ CollectionAssert.AreEqual(new long[] { 3, 4, 3 }, result.ConvertAll((divReply) => divReply.Quotient));
+ CollectionAssert.AreEqual(new long[] { 1, 16, 1 }, result.ConvertAll((divReply) => divReply.Remainder));
+ }).Wait();
}
}
}
diff --git a/src/csharp/Grpc.Examples/MathExamples.cs b/src/csharp/Grpc.Examples/MathExamples.cs
index 032372b2a1..dba5a7736c 100644
--- a/src/csharp/Grpc.Examples/MathExamples.cs
+++ b/src/csharp/Grpc.Examples/MathExamples.cs
@@ -61,9 +61,8 @@ namespace math
public static async Task FibExample(MathGrpc.IMathServiceClient stub)
{
- var recorder = new RecordingObserver<Num>();
- stub.Fib(new FibArgs.Builder { Limit = 5 }.Build(), recorder);
- List<Num> result = await recorder.ToList();
+ var call = stub.Fib(new FibArgs.Builder { Limit = 5 }.Build());
+ List<Num> result = await call.ResponseStream.ToList();
Console.WriteLine("Fib Result: " + string.Join("|", result));
}
@@ -76,9 +75,9 @@ namespace math
new Num.Builder { Num_ = 3 }.Build()
};
- var clientStreamingResult = stub.Sum();
- numbers.Subscribe(clientStreamingResult.Inputs);
- Console.WriteLine("Sum Result: " + await clientStreamingResult.Task);
+ var call = stub.Sum();
+ await call.RequestStream.WriteAll(numbers);
+ Console.WriteLine("Sum Result: " + await call.Result);
}
public static async Task DivManyExample(MathGrpc.IMathServiceClient stub)
@@ -89,12 +88,9 @@ namespace math
new DivArgs.Builder { Dividend = 100, Divisor = 21 }.Build(),
new DivArgs.Builder { Dividend = 7, Divisor = 2 }.Build()
};
-
- var recorder = new RecordingObserver<DivReply>();
- var inputs = stub.DivMany(recorder);
- divArgsList.Subscribe(inputs);
- var result = await recorder.ToList();
- Console.WriteLine("DivMany Result: " + string.Join("|", result));
+ var call = stub.DivMany();
+ await call.RequestStream.WriteAll(divArgsList);
+ Console.WriteLine("DivMany Result: " + string.Join("|", await call.ResponseStream.ToList()));
}
public static async Task DependendRequestsExample(MathGrpc.IMathServiceClient stub)
@@ -106,9 +102,9 @@ namespace math
new Num.Builder { Num_ = 3 }.Build()
};
- var clientStreamingResult = stub.Sum();
- numbers.Subscribe(clientStreamingResult.Inputs);
- Num sum = await clientStreamingResult.Task;
+ var sumCall = stub.Sum();
+ await sumCall.RequestStream.WriteAll(numbers);
+ Num sum = await sumCall.Result;
DivReply result = await stub.DivAsync(new DivArgs.Builder { Dividend = sum.Num_, Divisor = numbers.Count }.Build());
Console.WriteLine("Avg Result: " + result);
diff --git a/src/csharp/Grpc.Examples/MathGrpc.cs b/src/csharp/Grpc.Examples/MathGrpc.cs
index 24e6a1de8e..60408b9018 100644
--- a/src/csharp/Grpc.Examples/MathGrpc.cs
+++ b/src/csharp/Grpc.Examples/MathGrpc.cs
@@ -82,11 +82,11 @@ namespace math
Task<DivReply> DivAsync(DivArgs request, CancellationToken token = default(CancellationToken));
- void Fib(FibArgs request, IObserver<Num> responseObserver, CancellationToken token = default(CancellationToken));
+ AsyncServerStreamingCall<Num> Fib(FibArgs request, CancellationToken token = default(CancellationToken));
- ClientStreamingAsyncResult<Num, Num> Sum(CancellationToken token = default(CancellationToken));
+ AsyncClientStreamingCall<Num, Num> Sum(CancellationToken token = default(CancellationToken));
- IObserver<DivArgs> DivMany(IObserver<DivReply> responseObserver, CancellationToken token = default(CancellationToken));
+ AsyncDuplexStreamingCall<DivArgs, DivReply> DivMany(CancellationToken token = default(CancellationToken));
}
public class MathServiceClientStub : AbstractStub<MathServiceClientStub, StubConfiguration>, IMathServiceClient
@@ -111,35 +111,35 @@ namespace math
return Calls.AsyncUnaryCall(call, request, token);
}
- public void Fib(FibArgs request, IObserver<Num> responseObserver, CancellationToken token = default(CancellationToken))
+ public AsyncServerStreamingCall<Num> Fib(FibArgs request, CancellationToken token = default(CancellationToken))
{
var call = CreateCall(ServiceName, FibMethod);
- Calls.AsyncServerStreamingCall(call, request, responseObserver, token);
+ return Calls.AsyncServerStreamingCall(call, request, token);
}
- public ClientStreamingAsyncResult<Num, Num> Sum(CancellationToken token = default(CancellationToken))
+ public AsyncClientStreamingCall<Num, Num> Sum(CancellationToken token = default(CancellationToken))
{
var call = CreateCall(ServiceName, SumMethod);
return Calls.AsyncClientStreamingCall(call, token);
}
- public IObserver<DivArgs> DivMany(IObserver<DivReply> responseObserver, CancellationToken token = default(CancellationToken))
+ public AsyncDuplexStreamingCall<DivArgs, DivReply> DivMany(CancellationToken token = default(CancellationToken))
{
var call = CreateCall(ServiceName, DivManyMethod);
- return Calls.DuplexStreamingCall(call, responseObserver, token);
+ return Calls.AsyncDuplexStreamingCall(call, token);
}
}
// server-side interface
public interface IMathService
{
- void Div(DivArgs request, IObserver<DivReply> responseObserver);
+ Task<DivReply> Div(DivArgs request);
- void Fib(FibArgs request, IObserver<Num> responseObserver);
+ Task Fib(FibArgs request, IServerStreamWriter<Num> responseStream);
- IObserver<Num> Sum(IObserver<Num> responseObserver);
+ Task<Num> Sum(IAsyncStreamReader<Num> requestStream);
- IObserver<DivArgs> DivMany(IObserver<DivReply> responseObserver);
+ Task DivMany(IAsyncStreamReader<DivArgs> requestStream, IServerStreamWriter<DivReply> responseStream);
}
public static ServerServiceDefinition BindService(IMathService serviceImpl)
diff --git a/src/csharp/Grpc.Examples/MathServiceImpl.cs b/src/csharp/Grpc.Examples/MathServiceImpl.cs
index 0b2357e0fa..83ec2a8c3d 100644
--- a/src/csharp/Grpc.Examples/MathServiceImpl.cs
+++ b/src/csharp/Grpc.Examples/MathServiceImpl.cs
@@ -36,6 +36,7 @@ using System.Collections.Generic;
using System.Reactive.Linq;
using System.Threading;
using System.Threading.Tasks;
+using Grpc.Core;
using Grpc.Core.Utils;
namespace math
@@ -45,18 +46,16 @@ namespace math
/// </summary>
public class MathServiceImpl : MathGrpc.IMathService
{
- public void Div(DivArgs request, IObserver<DivReply> responseObserver)
+ public Task<DivReply> Div(DivArgs request)
{
- var response = DivInternal(request);
- responseObserver.OnNext(response);
- responseObserver.OnCompleted();
+ return Task.FromResult(DivInternal(request));
}
- public void Fib(FibArgs request, IObserver<Num> responseObserver)
+ public async Task Fib(FibArgs request, IServerStreamWriter<Num> responseStream)
{
if (request.Limit <= 0)
{
- // TODO: support cancellation....
+ // TODO(jtattermusch): support cancellation
throw new NotImplementedException("Not implemented yet");
}
@@ -64,34 +63,27 @@ namespace math
{
foreach (var num in FibInternal(request.Limit))
{
- responseObserver.OnNext(num);
+ await responseStream.Write(num);
}
- responseObserver.OnCompleted();
}
}
- public IObserver<Num> Sum(IObserver<Num> responseObserver)
+ public async Task<Num> Sum(IAsyncStreamReader<Num> requestStream)
{
- var recorder = new RecordingObserver<Num>();
- Task.Factory.StartNew(() =>
+ long sum = 0;
+ await requestStream.ForEach(async num =>
{
- List<Num> inputs = recorder.ToList().Result;
-
- long sum = 0;
- foreach (Num num in inputs)
- {
- sum += num.Num_;
- }
-
- responseObserver.OnNext(Num.CreateBuilder().SetNum_(sum).Build());
- responseObserver.OnCompleted();
+ sum += num.Num_;
});
- return recorder;
+ return Num.CreateBuilder().SetNum_(sum).Build();
}
- public IObserver<DivArgs> DivMany(IObserver<DivReply> responseObserver)
+ public async Task DivMany(IAsyncStreamReader<DivArgs> requestStream, IServerStreamWriter<DivReply> responseStream)
{
- return new DivObserver(responseObserver);
+ await requestStream.ForEach(async divArgs =>
+ {
+ await responseStream.Write(DivInternal(divArgs));
+ });
}
static DivReply DivInternal(DivArgs args)
@@ -114,31 +106,6 @@ namespace math
b = temp + b;
yield return new Num.Builder { Num_ = a }.Build();
}
- }
-
- private class DivObserver : IObserver<DivArgs>
- {
- readonly IObserver<DivReply> responseObserver;
-
- public DivObserver(IObserver<DivReply> responseObserver)
- {
- this.responseObserver = responseObserver;
- }
-
- public void OnCompleted()
- {
- responseObserver.OnCompleted();
- }
-
- public void OnError(Exception error)
- {
- throw new NotImplementedException();
- }
-
- public void OnNext(DivArgs value)
- {
- responseObserver.OnNext(DivInternal(value));
- }
- }
+ }
}
}
diff --git a/src/csharp/Grpc.IntegrationTesting/InteropClient.cs b/src/csharp/Grpc.IntegrationTesting/InteropClient.cs
index 1fbae374b1..a433659a08 100644
--- a/src/csharp/Grpc.IntegrationTesting/InteropClient.cs
+++ b/src/csharp/Grpc.IntegrationTesting/InteropClient.cs
@@ -34,6 +34,8 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
+using System.Threading;
+using System.Threading.Tasks;
using Google.ProtocolBuffers;
using grpc.testing;
@@ -165,6 +167,12 @@ namespace Grpc.IntegrationTesting
case "compute_engine_creds":
RunComputeEngineCreds(client);
break;
+ case "cancel_after_begin":
+ RunCancelAfterBegin(client);
+ break;
+ case "cancel_after_first_response":
+ RunCancelAfterFirstResponse(client);
+ break;
case "benchmark_empty_unary":
RunBenchmarkEmptyUnary(client);
break;
@@ -199,113 +207,115 @@ namespace Grpc.IntegrationTesting
public static void RunClientStreaming(TestServiceGrpc.ITestServiceClient client)
{
- Console.WriteLine("running client_streaming");
+ Task.Run(async () =>
+ {
+ Console.WriteLine("running client_streaming");
- var bodySizes = new List<int> { 27182, 8, 1828, 45904 };
+ var bodySizes = new List<int> { 27182, 8, 1828, 45904 }.ConvertAll((size) => StreamingInputCallRequest.CreateBuilder().SetPayload(CreateZerosPayload(size)).Build());
- var context = client.StreamingInputCall();
- foreach (var size in bodySizes)
- {
- context.Inputs.OnNext(
- StreamingInputCallRequest.CreateBuilder().SetPayload(CreateZerosPayload(size)).Build());
- }
- context.Inputs.OnCompleted();
+ var call = client.StreamingInputCall();
+ await call.RequestStream.WriteAll(bodySizes);
- var response = context.Task.Result;
- Assert.AreEqual(74922, response.AggregatedPayloadSize);
- Console.WriteLine("Passed!");
+ var response = await call.Result;
+ Assert.AreEqual(74922, response.AggregatedPayloadSize);
+ Console.WriteLine("Passed!");
+ }).Wait();
}
public static void RunServerStreaming(TestServiceGrpc.ITestServiceClient client)
{
- Console.WriteLine("running server_streaming");
+ Task.Run(async () =>
+ {
+ Console.WriteLine("running server_streaming");
- var bodySizes = new List<int> { 31415, 9, 2653, 58979 };
+ var bodySizes = new List<int> { 31415, 9, 2653, 58979 };
- var request = StreamingOutputCallRequest.CreateBuilder()
+ var request = StreamingOutputCallRequest.CreateBuilder()
.SetResponseType(PayloadType.COMPRESSABLE)
.AddRangeResponseParameters(bodySizes.ConvertAll(
- (size) => ResponseParameters.CreateBuilder().SetSize(size).Build()))
+ (size) => ResponseParameters.CreateBuilder().SetSize(size).Build()))
.Build();
- var recorder = new RecordingObserver<StreamingOutputCallResponse>();
- client.StreamingOutputCall(request, recorder);
-
- var responseList = recorder.ToList().Result;
+ var call = client.StreamingOutputCall(request);
- foreach (var res in responseList)
- {
- Assert.AreEqual(PayloadType.COMPRESSABLE, res.Payload.Type);
- }
- CollectionAssert.AreEqual(bodySizes, responseList.ConvertAll((item) => item.Payload.Body.Length));
- Console.WriteLine("Passed!");
+ var responseList = await call.ResponseStream.ToList();
+ foreach (var res in responseList)
+ {
+ Assert.AreEqual(PayloadType.COMPRESSABLE, res.Payload.Type);
+ }
+ CollectionAssert.AreEqual(bodySizes, responseList.ConvertAll((item) => item.Payload.Body.Length));
+ Console.WriteLine("Passed!");
+ }).Wait();
}
public static void RunPingPong(TestServiceGrpc.ITestServiceClient client)
{
- Console.WriteLine("running ping_pong");
+ Task.Run(async () =>
+ {
+ Console.WriteLine("running ping_pong");
- var recorder = new RecordingQueue<StreamingOutputCallResponse>();
- var inputs = client.FullDuplexCall(recorder);
+ var call = client.FullDuplexCall();
- StreamingOutputCallResponse response;
+ StreamingOutputCallResponse response;
- inputs.OnNext(StreamingOutputCallRequest.CreateBuilder()
+ await call.RequestStream.Write(StreamingOutputCallRequest.CreateBuilder()
.SetResponseType(PayloadType.COMPRESSABLE)
.AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(31415))
.SetPayload(CreateZerosPayload(27182)).Build());
- response = recorder.Queue.Take();
- Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
- Assert.AreEqual(31415, response.Payload.Body.Length);
+ response = await call.ResponseStream.ReadNext();
+ Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
+ Assert.AreEqual(31415, response.Payload.Body.Length);
- inputs.OnNext(StreamingOutputCallRequest.CreateBuilder()
+ await call.RequestStream.Write(StreamingOutputCallRequest.CreateBuilder()
.SetResponseType(PayloadType.COMPRESSABLE)
.AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(9))
.SetPayload(CreateZerosPayload(8)).Build());
- response = recorder.Queue.Take();
- Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
- Assert.AreEqual(9, response.Payload.Body.Length);
+ response = await call.ResponseStream.ReadNext();
+ Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
+ Assert.AreEqual(9, response.Payload.Body.Length);
- inputs.OnNext(StreamingOutputCallRequest.CreateBuilder()
+ await call.RequestStream.Write(StreamingOutputCallRequest.CreateBuilder()
.SetResponseType(PayloadType.COMPRESSABLE)
.AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(2653))
.SetPayload(CreateZerosPayload(1828)).Build());
- response = recorder.Queue.Take();
- Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
- Assert.AreEqual(2653, response.Payload.Body.Length);
+ response = await call.ResponseStream.ReadNext();
+ Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
+ Assert.AreEqual(2653, response.Payload.Body.Length);
- inputs.OnNext(StreamingOutputCallRequest.CreateBuilder()
+ await call.RequestStream.Write(StreamingOutputCallRequest.CreateBuilder()
.SetResponseType(PayloadType.COMPRESSABLE)
.AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(58979))
.SetPayload(CreateZerosPayload(45904)).Build());
- response = recorder.Queue.Take();
- Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
- Assert.AreEqual(58979, response.Payload.Body.Length);
+ response = await call.ResponseStream.ReadNext();
+ Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
+ Assert.AreEqual(58979, response.Payload.Body.Length);
- inputs.OnCompleted();
+ await call.RequestStream.Close();
- recorder.Finished.Wait();
- Assert.AreEqual(0, recorder.Queue.Count);
+ response = await call.ResponseStream.ReadNext();
+ Assert.AreEqual(null, response);
- Console.WriteLine("Passed!");
+ Console.WriteLine("Passed!");
+ }).Wait();
}
public static void RunEmptyStream(TestServiceGrpc.ITestServiceClient client)
{
- Console.WriteLine("running empty_stream");
-
- var recorder = new RecordingObserver<StreamingOutputCallResponse>();
- var inputs = client.FullDuplexCall(recorder);
- inputs.OnCompleted();
+ Task.Run(async () =>
+ {
+ Console.WriteLine("running empty_stream");
+ var call = client.FullDuplexCall();
+ await call.Close();
- var responseList = recorder.ToList().Result;
- Assert.AreEqual(0, responseList.Count);
+ var responseList = await call.ResponseStream.ToList();
+ Assert.AreEqual(0, responseList.Count);
- Console.WriteLine("Passed!");
+ Console.WriteLine("Passed!");
+ }).Wait();
}
public static void RunServiceAccountCreds(TestServiceGrpc.ITestServiceClient client)
@@ -348,6 +358,66 @@ namespace Grpc.IntegrationTesting
Console.WriteLine("Passed!");
}
+ public static void RunCancelAfterBegin(TestServiceGrpc.ITestServiceClient client)
+ {
+ Task.Run(async () =>
+ {
+ Console.WriteLine("running cancel_after_begin");
+
+ var cts = new CancellationTokenSource();
+ var call = client.StreamingInputCall(cts.Token);
+ // TODO(jtattermusch): we need this to ensure call has been initiated once we cancel it.
+ await Task.Delay(1000);
+ cts.Cancel();
+
+ try
+ {
+ var response = await call.Result;
+ Assert.Fail();
+ }
+ catch (RpcException e)
+ {
+ Assert.AreEqual(StatusCode.Cancelled, e.Status.StatusCode);
+ }
+ Console.WriteLine("Passed!");
+ }).Wait();
+ }
+
+ public static void RunCancelAfterFirstResponse(TestServiceGrpc.ITestServiceClient client)
+ {
+ Task.Run(async () =>
+ {
+ Console.WriteLine("running cancel_after_first_response");
+
+ var cts = new CancellationTokenSource();
+ var call = client.FullDuplexCall(cts.Token);
+
+ StreamingOutputCallResponse response;
+
+ await call.RequestStream.Write(StreamingOutputCallRequest.CreateBuilder()
+ .SetResponseType(PayloadType.COMPRESSABLE)
+ .AddResponseParameters(ResponseParameters.CreateBuilder().SetSize(31415))
+ .SetPayload(CreateZerosPayload(27182)).Build());
+
+ response = await call.ResponseStream.ReadNext();
+ Assert.AreEqual(PayloadType.COMPRESSABLE, response.Payload.Type);
+ Assert.AreEqual(31415, response.Payload.Body.Length);
+
+ cts.Cancel();
+
+ try
+ {
+ response = await call.ResponseStream.ReadNext();
+ Assert.Fail();
+ }
+ catch (RpcException e)
+ {
+ Assert.AreEqual(StatusCode.Cancelled, e.Status.StatusCode);
+ }
+ Console.WriteLine("Passed!");
+ }).Wait();
+ }
+
// This is not an official interop test, but it's useful.
public static void RunBenchmarkEmptyUnary(TestServiceGrpc.ITestServiceClient client)
{
diff --git a/src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs b/src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs
index 1e76d3df21..45380227c2 100644
--- a/src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs
+++ b/src/csharp/Grpc.IntegrationTesting/InteropClientServerTest.cs
@@ -87,7 +87,7 @@ namespace Grpc.IntegrationTesting
[Test]
public void LargeUnary()
{
- InteropClient.RunEmptyUnary(client);
+ InteropClient.RunLargeUnary(client);
}
[Test]
@@ -114,8 +114,16 @@ namespace Grpc.IntegrationTesting
InteropClient.RunEmptyStream(client);
}
- // TODO: add cancel_after_begin
+ [Test]
+ public void CancelAfterBegin()
+ {
+ InteropClient.RunCancelAfterBegin(client);
+ }
- // TODO: add cancel_after_first_response
+ [Test]
+ public void CancelAfterFirstResponse()
+ {
+ InteropClient.RunCancelAfterFirstResponse(client);
+ }
}
}
diff --git a/src/csharp/Grpc.IntegrationTesting/TestServiceGrpc.cs b/src/csharp/Grpc.IntegrationTesting/TestServiceGrpc.cs
index f63e0361a4..d1f8aa12c7 100644
--- a/src/csharp/Grpc.IntegrationTesting/TestServiceGrpc.cs
+++ b/src/csharp/Grpc.IntegrationTesting/TestServiceGrpc.cs
@@ -100,13 +100,13 @@ namespace grpc.testing
Task<SimpleResponse> UnaryCallAsync(SimpleRequest request, CancellationToken token = default(CancellationToken));
- void StreamingOutputCall(StreamingOutputCallRequest request, IObserver<StreamingOutputCallResponse> responseObserver, CancellationToken token = default(CancellationToken));
+ AsyncServerStreamingCall<StreamingOutputCallResponse> StreamingOutputCall(StreamingOutputCallRequest request, CancellationToken token = default(CancellationToken));
- ClientStreamingAsyncResult<StreamingInputCallRequest, StreamingInputCallResponse> StreamingInputCall(CancellationToken token = default(CancellationToken));
+ AsyncClientStreamingCall<StreamingInputCallRequest, StreamingInputCallResponse> StreamingInputCall(CancellationToken token = default(CancellationToken));
- IObserver<StreamingOutputCallRequest> FullDuplexCall(IObserver<StreamingOutputCallResponse> responseObserver, CancellationToken token = default(CancellationToken));
+ AsyncDuplexStreamingCall<StreamingOutputCallRequest, StreamingOutputCallResponse> FullDuplexCall(CancellationToken token = default(CancellationToken));
- IObserver<StreamingOutputCallRequest> HalfDuplexCall(IObserver<StreamingOutputCallResponse> responseObserver, CancellationToken token = default(CancellationToken));
+ AsyncDuplexStreamingCall<StreamingOutputCallRequest, StreamingOutputCallResponse> HalfDuplexCall(CancellationToken token = default(CancellationToken));
}
public class TestServiceClientStub : AbstractStub<TestServiceClientStub, StubConfiguration>, ITestServiceClient
@@ -143,45 +143,45 @@ namespace grpc.testing
return Calls.AsyncUnaryCall(call, request, token);
}
- public void StreamingOutputCall(StreamingOutputCallRequest request, IObserver<StreamingOutputCallResponse> responseObserver, CancellationToken token = default(CancellationToken))
+ public AsyncServerStreamingCall<StreamingOutputCallResponse> StreamingOutputCall(StreamingOutputCallRequest request, CancellationToken token = default(CancellationToken))
{
var call = CreateCall(ServiceName, StreamingOutputCallMethod);
- Calls.AsyncServerStreamingCall(call, request, responseObserver, token);
+ return Calls.AsyncServerStreamingCall(call, request, token);
}
- public ClientStreamingAsyncResult<StreamingInputCallRequest, StreamingInputCallResponse> StreamingInputCall(CancellationToken token = default(CancellationToken))
+ public AsyncClientStreamingCall<StreamingInputCallRequest, StreamingInputCallResponse> StreamingInputCall(CancellationToken token = default(CancellationToken))
{
var call = CreateCall(ServiceName, StreamingInputCallMethod);
return Calls.AsyncClientStreamingCall(call, token);
}
- public IObserver<StreamingOutputCallRequest> FullDuplexCall(IObserver<StreamingOutputCallResponse> responseObserver, CancellationToken token = default(CancellationToken))
+ public AsyncDuplexStreamingCall<StreamingOutputCallRequest, StreamingOutputCallResponse> FullDuplexCall(CancellationToken token = default(CancellationToken))
{
var call = CreateCall(ServiceName, FullDuplexCallMethod);
- return Calls.DuplexStreamingCall(call, responseObserver, token);
+ return Calls.AsyncDuplexStreamingCall(call, token);
}
- public IObserver<StreamingOutputCallRequest> HalfDuplexCall(IObserver<StreamingOutputCallResponse> responseObserver, CancellationToken token = default(CancellationToken))
+ public AsyncDuplexStreamingCall<StreamingOutputCallRequest, StreamingOutputCallResponse> HalfDuplexCall(CancellationToken token = default(CancellationToken))
{
var call = CreateCall(ServiceName, HalfDuplexCallMethod);
- return Calls.DuplexStreamingCall(call, responseObserver, token);
+ return Calls.AsyncDuplexStreamingCall(call, token);
}
}
// server-side interface
public interface ITestService
{
- void EmptyCall(Empty request, IObserver<Empty> responseObserver);
+ Task<Empty> EmptyCall(Empty request);
- void UnaryCall(SimpleRequest request, IObserver<SimpleResponse> responseObserver);
+ Task<SimpleResponse> UnaryCall(SimpleRequest request);
- void StreamingOutputCall(StreamingOutputCallRequest request, IObserver<StreamingOutputCallResponse> responseObserver);
+ Task StreamingOutputCall(StreamingOutputCallRequest request, IServerStreamWriter<StreamingOutputCallResponse> responseStream);
- IObserver<StreamingInputCallRequest> StreamingInputCall(IObserver<StreamingInputCallResponse> responseObserver);
+ Task<StreamingInputCallResponse> StreamingInputCall(IAsyncStreamReader<StreamingInputCallRequest> requestStream);
- IObserver<StreamingOutputCallRequest> FullDuplexCall(IObserver<StreamingOutputCallResponse> responseObserver);
+ Task FullDuplexCall(IAsyncStreamReader<StreamingOutputCallRequest> requestStream, IServerStreamWriter<StreamingOutputCallResponse> responseStream);
- IObserver<StreamingOutputCallRequest> HalfDuplexCall(IObserver<StreamingOutputCallResponse> responseObserver);
+ Task HalfDuplexCall(IAsyncStreamReader<StreamingOutputCallRequest> requestStream, IServerStreamWriter<StreamingOutputCallResponse> responseStream);
}
public static ServerServiceDefinition BindService(ITestService serviceImpl)
diff --git a/src/csharp/Grpc.IntegrationTesting/TestServiceImpl.cs b/src/csharp/Grpc.IntegrationTesting/TestServiceImpl.cs
index 661b31b0ee..8b0cf3a2d0 100644
--- a/src/csharp/Grpc.IntegrationTesting/TestServiceImpl.cs
+++ b/src/csharp/Grpc.IntegrationTesting/TestServiceImpl.cs
@@ -36,6 +36,7 @@ using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Google.ProtocolBuffers;
+using Grpc.Core;
using Grpc.Core.Utils;
namespace grpc.testing
@@ -45,88 +46,54 @@ namespace grpc.testing
/// </summary>
public class TestServiceImpl : TestServiceGrpc.ITestService
{
- public void EmptyCall(Empty request, IObserver<Empty> responseObserver)
+ public Task<Empty> EmptyCall(Empty request)
{
- responseObserver.OnNext(Empty.DefaultInstance);
- responseObserver.OnCompleted();
+ return Task.FromResult(Empty.DefaultInstance);
}
- public void UnaryCall(SimpleRequest request, IObserver<SimpleResponse> responseObserver)
+ public Task<SimpleResponse> UnaryCall(SimpleRequest request)
{
var response = SimpleResponse.CreateBuilder()
.SetPayload(CreateZerosPayload(request.ResponseSize)).Build();
- // TODO: check we support ReponseType
- responseObserver.OnNext(response);
- responseObserver.OnCompleted();
+ return Task.FromResult(response);
}
- public void StreamingOutputCall(StreamingOutputCallRequest request, IObserver<StreamingOutputCallResponse> responseObserver)
+ public async Task StreamingOutputCall(StreamingOutputCallRequest request, IServerStreamWriter<StreamingOutputCallResponse> responseStream)
{
foreach (var responseParam in request.ResponseParametersList)
{
var response = StreamingOutputCallResponse.CreateBuilder()
.SetPayload(CreateZerosPayload(responseParam.Size)).Build();
- responseObserver.OnNext(response);
+ await responseStream.Write(response);
}
- responseObserver.OnCompleted();
}
- public IObserver<StreamingInputCallRequest> StreamingInputCall(IObserver<StreamingInputCallResponse> responseObserver)
+ public async Task<StreamingInputCallResponse> StreamingInputCall(IAsyncStreamReader<StreamingInputCallRequest> requestStream)
{
- var recorder = new RecordingObserver<StreamingInputCallRequest>();
- Task.Run(() =>
+ int sum = 0;
+ await requestStream.ForEach(async request =>
{
- int sum = 0;
- foreach (var req in recorder.ToList().Result)
- {
- sum += req.Payload.Body.Length;
- }
- var response = StreamingInputCallResponse.CreateBuilder()
- .SetAggregatedPayloadSize(sum).Build();
- responseObserver.OnNext(response);
- responseObserver.OnCompleted();
+ sum += request.Payload.Body.Length;
});
- return recorder;
+ return StreamingInputCallResponse.CreateBuilder().SetAggregatedPayloadSize(sum).Build();
}
- public IObserver<StreamingOutputCallRequest> FullDuplexCall(IObserver<StreamingOutputCallResponse> responseObserver)
+ public async Task FullDuplexCall(IAsyncStreamReader<StreamingOutputCallRequest> requestStream, IServerStreamWriter<StreamingOutputCallResponse> responseStream)
{
- return new FullDuplexObserver(responseObserver);
- }
-
- public IObserver<StreamingOutputCallRequest> HalfDuplexCall(IObserver<StreamingOutputCallResponse> responseObserver)
- {
- throw new NotImplementedException();
- }
-
- private class FullDuplexObserver : IObserver<StreamingOutputCallRequest>
- {
- readonly IObserver<StreamingOutputCallResponse> responseObserver;
-
- public FullDuplexObserver(IObserver<StreamingOutputCallResponse> responseObserver)
- {
- this.responseObserver = responseObserver;
- }
-
- public void OnCompleted()
+ await requestStream.ForEach(async request =>
{
- responseObserver.OnCompleted();
- }
-
- public void OnError(Exception error)
- {
- throw new NotImplementedException();
- }
-
- public void OnNext(StreamingOutputCallRequest value)
- {
- foreach (var responseParam in value.ResponseParametersList)
+ foreach (var responseParam in request.ResponseParametersList)
{
var response = StreamingOutputCallResponse.CreateBuilder()
.SetPayload(CreateZerosPayload(responseParam.Size)).Build();
- responseObserver.OnNext(response);
+ await responseStream.Write(response);
}
- }
+ });
+ }
+
+ public async Task HalfDuplexCall(IAsyncStreamReader<StreamingOutputCallRequest> requestStream, IServerStreamWriter<StreamingOutputCallResponse> responseStream)
+ {
+ throw new NotImplementedException();
}
private static Payload CreateZerosPayload(int size)
diff --git a/src/csharp/ext/grpc_csharp_ext.c b/src/csharp/ext/grpc_csharp_ext.c
index 57cf750c7f..817b172095 100644
--- a/src/csharp/ext/grpc_csharp_ext.c
+++ b/src/csharp/ext/grpc_csharp_ext.c
@@ -276,6 +276,12 @@ grpcsharp_batch_context_server_rpc_new_method(
return ctx->server_rpc_new.call_details.method;
}
+GPR_EXPORT gpr_int32 GPR_CALLTYPE
+grpcsharp_batch_context_recv_close_on_server_cancelled(
+ const grpcsharp_batch_context *ctx) {
+ return (gpr_int32) ctx->recv_close_on_server_cancelled;
+}
+
/* Init & shutdown */
GPR_EXPORT void GPR_CALLTYPE grpcsharp_init(void) { grpc_init(); }
diff --git a/src/php/tests/interop/interop_client.php b/src/php/tests/interop/interop_client.php
index 22f85aa322..98a367a905 100755
--- a/src/php/tests/interop/interop_client.php
+++ b/src/php/tests/interop/interop_client.php
@@ -126,6 +126,24 @@ function serviceAccountCreds($stub, $args) {
}
/**
+ * Run the compute engine credentials auth test.
+ * Has not been run from gcloud as of 2015-05-05
+ * @param $stub Stub object that has service methods
+ * @param $args array command line args
+ */
+function computeEngineCreds($stub, $args) {
+ if (!array_key_exists('oauth_scope', $args)) {
+ throw new Exception('Missing oauth scope');
+ }
+ if (!array_key_exists('default_service_account', $args)) {
+ throw new Exception('Missing default_service_account');
+ }
+ $result = performLargeUnary($stub, $fillUsername=true, $fillOauthScope=true);
+ hardAssert($args['default_service_account'] == $result->getUsername(),
+ 'invalid email returned');
+}
+
+/**
* Run the client_streaming test.
* Passes when run against the Node server as of 2015-04-30
* @param $stub Stub object that has service methods
@@ -240,7 +258,8 @@ function cancelAfterFirstResponse($stub) {
}
$args = getopt('', array('server_host:', 'server_port:', 'test_case:',
- 'server_host_override:', 'oauth_scope:'));
+ 'server_host_override:', 'oauth_scope:',
+ 'default_service_account:'));
if (!array_key_exists('server_host', $args) ||
!array_key_exists('server_port', $args) ||
!array_key_exists('test_case', $args)) {
@@ -301,6 +320,9 @@ switch ($args['test_case']) {
case 'service_account_creds':
serviceAccountCreds($stub, $args);
break;
+ case 'compute_engine_creds':
+ computeEngineCreds($stub, $args);
+ break;
default:
exit(1);
}
diff --git a/src/ruby/grpc.gemspec b/src/ruby/grpc.gemspec
index 19b3e21cb6..3ddb7fac18 100755
--- a/src/ruby/grpc.gemspec
+++ b/src/ruby/grpc.gemspec
@@ -34,7 +34,7 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rake', '~> 10.4'
s.add_development_dependency 'rake-compiler', '~> 0.9'
s.add_development_dependency 'rspec', '~> 3.2'
- s.add_development_dependency 'rubocop', '~> 0.30'
+ s.add_development_dependency 'rubocop', '~> 0.30.0'
s.extensions = %w(ext/grpc/extconf.rb)
end