From 4d1589ace07d0080abfc2f40132f4f111f5484d7 Mon Sep 17 00:00:00 2001 From: Alistair Veitch Date: Fri, 17 Jul 2015 15:13:04 -0700 Subject: add record_stat API --- build.json | 6 ++++-- include/grpc/census.h | 3 +++ src/core/census/record_stat.c | 38 ++++++++++++++++++++++++++++++++++ src/core/census/resource_id.h | 48 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 src/core/census/record_stat.c create mode 100644 src/core/census/resource_id.h diff --git a/build.json b/build.json index 121637204e..a1104c6d88 100644 --- a/build.json +++ b/build.json @@ -18,11 +18,13 @@ "include/grpc/census.h" ], "headers": [ - "src/core/census/context.h" + "src/core/census/context.h", + "src/core/census/resource_id.h" ], "src": [ "src/core/census/context.c", - "src/core/census/initialize.c" + "src/core/census/initialize.c", + "src/core/census/record_stat.c" ] }, { diff --git a/include/grpc/census.h b/include/grpc/census.h index 3fc07affc8..9271d4f6a7 100644 --- a/include/grpc/census.h +++ b/include/grpc/census.h @@ -100,6 +100,9 @@ int census_context_deserialize(const char *buffer, census_context **context); * future census calls will result in undefined behavior. */ void census_context_destroy(census_context *context); +/* Record a new value against the given stats ID and context. */ +void census_record_stat(census_context *context, int resource_id, double value); + #ifdef __cplusplus } #endif diff --git a/src/core/census/record_stat.c b/src/core/census/record_stat.c new file mode 100644 index 0000000000..7d5a350c49 --- /dev/null +++ b/src/core/census/record_stat.c @@ -0,0 +1,38 @@ +/* + * + * 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. + * + */ + +#include +#include "src/core/census/resource_id.h" + +void census_record_stat(census_context *context, int resource_id, + double value) {} diff --git a/src/core/census/resource_id.h b/src/core/census/resource_id.h new file mode 100644 index 0000000000..89c31df311 --- /dev/null +++ b/src/core/census/resource_id.h @@ -0,0 +1,48 @@ +/* + * + * 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 CENSUS_RESOURCE_ID_H +#define CENSUS_RESOURCE_ID_H + +/* Resource ID's used for census measurements. */ +#define RESOURCE_INVALID 0 /* Make default be invalid. */ +#define RESOURCE_RPC_CLIENT_REQUESTS 1 /* Count of client requests sent. */ +#define RESOURCE_RPC_SERVER_REQUESTS 2 /* Count of server requests sent. */ +#define RESOURCE_RPC_CLIENT_ERRORS 3 /* Client error counts. */ +#define RESOURCE_RPC_SERVER_ERRORS 4 /* Server error counts. */ +#define RESOURCE_RPC_CLIENT_LATENCY 5 /* Client side request latency. */ +#define RESOURCE_RPC_SERVER_LATENCY 6 /* Server side request latency. */ +#define RESOURCE_RPC_CLIENT_CPU 7 /* Client CPU processing time. */ +#define RESOURCE_RPC_SERVER_CPU 8 /* Server CPU processing time. */ + +#endif /* CENSUS_RESOURCE_ID_H */ -- cgit v1.2.3 From 635899d0624e5e304d79bf7f02027f9d040cbfaa Mon Sep 17 00:00:00 2001 From: Alistair Veitch Date: Fri, 17 Jul 2015 16:02:24 -0700 Subject: rebuild makefile --- BUILD | 6 ++++++ Makefile | 2 ++ gRPC.podspec | 7 +++++-- tools/doxygen/Doxyfile.core.internal | 2 ++ tools/run_tests/sources_and_headers.json | 6 ++++++ vsprojects/grpc/grpc.vcxproj | 3 +++ vsprojects/grpc/grpc.vcxproj.filters | 6 ++++++ vsprojects/grpc_unsecure/grpc_unsecure.vcxproj | 3 +++ vsprojects/grpc_unsecure/grpc_unsecure.vcxproj.filters | 6 ++++++ 9 files changed, 39 insertions(+), 2 deletions(-) diff --git a/BUILD b/BUILD index d75bd4205f..bdcef1e20b 100644 --- a/BUILD +++ b/BUILD @@ -243,6 +243,7 @@ cc_library( "src/core/transport/transport.h", "src/core/transport/transport_impl.h", "src/core/census/context.h", + "src/core/census/resource_id.h", "src/core/httpcli/format_request.c", "src/core/httpcli/httpcli.c", "src/core/httpcli/httpcli_security_connector.c", @@ -377,6 +378,7 @@ cc_library( "src/core/transport/transport_op_string.c", "src/core/census/context.c", "src/core/census/initialize.c", + "src/core/census/record_stat.c", ], hdrs = [ "include/grpc/grpc_security.h", @@ -497,6 +499,7 @@ cc_library( "src/core/transport/transport.h", "src/core/transport/transport_impl.h", "src/core/census/context.h", + "src/core/census/resource_id.h", "src/core/surface/init_unsecure.c", "src/core/census/grpc_context.c", "src/core/channel/channel_args.c", @@ -608,6 +611,7 @@ cc_library( "src/core/transport/transport_op_string.c", "src/core/census/context.c", "src/core/census/initialize.c", + "src/core/census/record_stat.c", ], hdrs = [ "include/grpc/byte_buffer.h", @@ -1080,6 +1084,7 @@ objc_library( "src/core/transport/transport_op_string.c", "src/core/census/context.c", "src/core/census/initialize.c", + "src/core/census/record_stat.c", ], hdrs = [ "include/grpc/grpc_security.h", @@ -1202,6 +1207,7 @@ objc_library( "src/core/transport/transport.h", "src/core/transport/transport_impl.h", "src/core/census/context.h", + "src/core/census/resource_id.h", ], includes = [ "include", diff --git a/Makefile b/Makefile index 57ab6fed15..c7afefd07d 100644 --- a/Makefile +++ b/Makefile @@ -3375,6 +3375,7 @@ LIBGRPC_SRC = \ src/core/transport/transport_op_string.c \ src/core/census/context.c \ src/core/census/initialize.c \ + src/core/census/record_stat.c \ PUBLIC_HEADERS_C += \ include/grpc/grpc_security.h \ @@ -3636,6 +3637,7 @@ LIBGRPC_UNSECURE_SRC = \ src/core/transport/transport_op_string.c \ src/core/census/context.c \ src/core/census/initialize.c \ + src/core/census/record_stat.c \ PUBLIC_HEADERS_C += \ include/grpc/byte_buffer.h \ diff --git a/gRPC.podspec b/gRPC.podspec index f678819b96..5d2459b289 100644 --- a/gRPC.podspec +++ b/gRPC.podspec @@ -245,6 +245,7 @@ Pod::Spec.new do |s| 'src/core/transport/transport.h', 'src/core/transport/transport_impl.h', 'src/core/census/context.h', + 'src/core/census/resource_id.h', 'grpc/grpc_security.h', 'grpc/byte_buffer.h', 'grpc/byte_buffer_reader.h', @@ -385,7 +386,8 @@ Pod::Spec.new do |s| 'src/core/transport/transport.c', 'src/core/transport/transport_op_string.c', 'src/core/census/context.c', - 'src/core/census/initialize.c' + 'src/core/census/initialize.c', + 'src/core/census/record_stat.c' ss.private_header_files = 'src/core/support/env.h', 'src/core/support/file.h', @@ -505,7 +507,8 @@ Pod::Spec.new do |s| 'src/core/transport/stream_op.h', 'src/core/transport/transport.h', 'src/core/transport/transport_impl.h', - 'src/core/census/context.h' + 'src/core/census/context.h', + 'src/core/census/resource_id.h' ss.header_mappings_dir = '.' diff --git a/tools/doxygen/Doxyfile.core.internal b/tools/doxygen/Doxyfile.core.internal index f11df21388..dd65c6cdbd 100644 --- a/tools/doxygen/Doxyfile.core.internal +++ b/tools/doxygen/Doxyfile.core.internal @@ -880,6 +880,7 @@ src/core/transport/stream_op.h \ src/core/transport/transport.h \ src/core/transport/transport_impl.h \ src/core/census/context.h \ +src/core/census/resource_id.h \ src/core/httpcli/format_request.c \ src/core/httpcli/httpcli.c \ src/core/httpcli/httpcli_security_connector.c \ @@ -1014,6 +1015,7 @@ src/core/transport/transport.c \ src/core/transport/transport_op_string.c \ src/core/census/context.c \ src/core/census/initialize.c \ +src/core/census/record_stat.c \ include/grpc/support/alloc.h \ include/grpc/support/atm.h \ include/grpc/support/atm_gcc_atomic.h \ diff --git a/tools/run_tests/sources_and_headers.json b/tools/run_tests/sources_and_headers.json index 0493e06f58..07fb85d9d0 100644 --- a/tools/run_tests/sources_and_headers.json +++ b/tools/run_tests/sources_and_headers.json @@ -8716,6 +8716,7 @@ "include/grpc/status.h", "src/core/census/context.h", "src/core/census/grpc_context.h", + "src/core/census/resource_id.h", "src/core/channel/census_filter.h", "src/core/channel/channel_args.h", "src/core/channel/channel_stack.h", @@ -8843,6 +8844,8 @@ "src/core/census/grpc_context.c", "src/core/census/grpc_context.h", "src/core/census/initialize.c", + "src/core/census/record_stat.c", + "src/core/census/resource_id.h", "src/core/channel/census_filter.h", "src/core/channel/channel_args.c", "src/core/channel/channel_args.h", @@ -9169,6 +9172,7 @@ "include/grpc/status.h", "src/core/census/context.h", "src/core/census/grpc_context.h", + "src/core/census/resource_id.h", "src/core/channel/census_filter.h", "src/core/channel/channel_args.h", "src/core/channel/channel_stack.h", @@ -9278,6 +9282,8 @@ "src/core/census/grpc_context.c", "src/core/census/grpc_context.h", "src/core/census/initialize.c", + "src/core/census/record_stat.c", + "src/core/census/resource_id.h", "src/core/channel/census_filter.h", "src/core/channel/channel_args.c", "src/core/channel/channel_args.h", diff --git a/vsprojects/grpc/grpc.vcxproj b/vsprojects/grpc/grpc.vcxproj index 16744b181b..679bc33754 100644 --- a/vsprojects/grpc/grpc.vcxproj +++ b/vsprojects/grpc/grpc.vcxproj @@ -269,6 +269,7 @@ + @@ -539,6 +540,8 @@ + + diff --git a/vsprojects/grpc/grpc.vcxproj.filters b/vsprojects/grpc/grpc.vcxproj.filters index de9f20521c..d139a2e330 100644 --- a/vsprojects/grpc/grpc.vcxproj.filters +++ b/vsprojects/grpc/grpc.vcxproj.filters @@ -403,6 +403,9 @@ src\core\census + + src\core\census + @@ -767,6 +770,9 @@ src\core\census + + src\core\census + diff --git a/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj b/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj index 02c791f995..4cea37e7aa 100644 --- a/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj +++ b/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj @@ -250,6 +250,7 @@ + @@ -474,6 +475,8 @@ + + diff --git a/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj.filters b/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj.filters index 333a71f564..e51f0e7c50 100644 --- a/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj.filters +++ b/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj.filters @@ -334,6 +334,9 @@ src\core\census + + src\core\census + @@ -644,6 +647,9 @@ src\core\census + + src\core\census + -- cgit v1.2.3 From 851032a7ae03796eda80f0fe63bdafd8a20f38bb Mon Sep 17 00:00:00 2001 From: Alistair Veitch Date: Mon, 20 Jul 2015 11:59:13 -0700 Subject: address comments --- build.json | 2 +- include/grpc/census.h | 12 +++++++++-- src/core/census/record_stat.c | 6 +++--- src/core/census/resource_id.h | 48 ------------------------------------------- src/core/census/rpc_stat_id.h | 46 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 54 deletions(-) delete mode 100644 src/core/census/resource_id.h create mode 100644 src/core/census/rpc_stat_id.h diff --git a/build.json b/build.json index a1104c6d88..9bb27d7579 100644 --- a/build.json +++ b/build.json @@ -19,7 +19,7 @@ ], "headers": [ "src/core/census/context.h", - "src/core/census/resource_id.h" + "src/core/census/rpc_stat_id.h" ], "src": [ "src/core/census/context.c", diff --git a/include/grpc/census.h b/include/grpc/census.h index 9271d4f6a7..379783905a 100644 --- a/include/grpc/census.h +++ b/include/grpc/census.h @@ -100,8 +100,16 @@ int census_context_deserialize(const char *buffer, census_context **context); * future census calls will result in undefined behavior. */ void census_context_destroy(census_context *context); -/* Record a new value against the given stats ID and context. */ -void census_record_stat(census_context *context, int resource_id, double value); +/* A census statistic to be recorded comprises two parts: an ID for the + * particular statistic and the value to be recorded against it. */ +typedef struct { + int id; + double value; +} census_stat; + +/* Record new stats against the given context. */ +void census_record_stat(census_context *context, census_stat *stats, + size_t nstats); #ifdef __cplusplus } diff --git a/src/core/census/record_stat.c b/src/core/census/record_stat.c index 7d5a350c49..3dd918618b 100644 --- a/src/core/census/record_stat.c +++ b/src/core/census/record_stat.c @@ -32,7 +32,7 @@ */ #include -#include "src/core/census/resource_id.h" +#include "src/core/census/rpc_stat_id.h" -void census_record_stat(census_context *context, int resource_id, - double value) {} +void census_record_stat(census_context *context, census_stat *stats, + size_t nstats) {} diff --git a/src/core/census/resource_id.h b/src/core/census/resource_id.h deleted file mode 100644 index 89c31df311..0000000000 --- a/src/core/census/resource_id.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * - * 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 CENSUS_RESOURCE_ID_H -#define CENSUS_RESOURCE_ID_H - -/* Resource ID's used for census measurements. */ -#define RESOURCE_INVALID 0 /* Make default be invalid. */ -#define RESOURCE_RPC_CLIENT_REQUESTS 1 /* Count of client requests sent. */ -#define RESOURCE_RPC_SERVER_REQUESTS 2 /* Count of server requests sent. */ -#define RESOURCE_RPC_CLIENT_ERRORS 3 /* Client error counts. */ -#define RESOURCE_RPC_SERVER_ERRORS 4 /* Server error counts. */ -#define RESOURCE_RPC_CLIENT_LATENCY 5 /* Client side request latency. */ -#define RESOURCE_RPC_SERVER_LATENCY 6 /* Server side request latency. */ -#define RESOURCE_RPC_CLIENT_CPU 7 /* Client CPU processing time. */ -#define RESOURCE_RPC_SERVER_CPU 8 /* Server CPU processing time. */ - -#endif /* CENSUS_RESOURCE_ID_H */ diff --git a/src/core/census/rpc_stat_id.h b/src/core/census/rpc_stat_id.h new file mode 100644 index 0000000000..fc0aa6f43f --- /dev/null +++ b/src/core/census/rpc_stat_id.h @@ -0,0 +1,46 @@ +/* + * + * 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 CENSUS_RPC_STAT_ID_H +#define CENSUS_RPC_STAT_ID_H + +/* Stats ID's used for RPC measurements. */ +#define CENSUS_INVALID_STAT_ID 0 /* ID 0 is always invalid */ +#define CENSUS_RPC_CLIENT_REQUESTS 1 /* Count of client requests sent. */ +#define CENSUS_RPC_SERVER_REQUESTS 2 /* Count of server requests sent. */ +#define CENSUS_RPC_CLIENT_ERRORS 3 /* Client error counts. */ +#define CENSUS_RPC_SERVER_ERRORS 4 /* Server error counts. */ +#define CENSUS_RPC_CLIENT_LATENCY 5 /* Client side request latency. */ +#define CENSUS_RPC_SERVER_LATENCY 6 /* Server side request latency. */ + +#endif /* CENSUS_RPC_STAT_ID_H */ -- cgit v1.2.3 From 698d00c60e91ebf8acf993cf6602d74c0032b5dc Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Mon, 20 Jul 2015 12:32:58 -0700 Subject: Add ipv4:, ipv6: schemes --- BUILD | 12 +- Makefile | 4 +- build.json | 4 +- gRPC.podspec | 6 +- include/grpc/support/host_port.h | 6 +- src/core/client_config/README.md | 4 + .../client_config/resolvers/sockaddr_resolver.c | 297 +++++++++++++++++++++ .../client_config/resolvers/sockaddr_resolver.h | 50 ++++ .../client_config/resolvers/unix_resolver_posix.c | 195 -------------- .../client_config/resolvers/unix_resolver_posix.h | 44 --- src/core/support/host_port.c | 10 +- src/core/surface/init.c | 7 +- test/core/end2end/dualstack_socket_test.c | 18 +- tools/doxygen/Doxyfile.core.internal | 4 +- tools/run_tests/sources_and_headers.json | 12 +- vsprojects/grpc/grpc.vcxproj | 4 +- vsprojects/grpc/grpc.vcxproj.filters | 4 +- vsprojects/grpc_unsecure/grpc_unsecure.vcxproj | 4 +- .../grpc_unsecure/grpc_unsecure.vcxproj.filters | 4 +- 19 files changed, 410 insertions(+), 279 deletions(-) create mode 100644 src/core/client_config/resolvers/sockaddr_resolver.c create mode 100644 src/core/client_config/resolvers/sockaddr_resolver.h delete mode 100644 src/core/client_config/resolvers/unix_resolver_posix.c delete mode 100644 src/core/client_config/resolvers/unix_resolver_posix.h diff --git a/BUILD b/BUILD index e116d4584b..fefb1d9544 100644 --- a/BUILD +++ b/BUILD @@ -168,7 +168,7 @@ cc_library( "src/core/client_config/resolver_factory.h", "src/core/client_config/resolver_registry.h", "src/core/client_config/resolvers/dns_resolver.h", - "src/core/client_config/resolvers/unix_resolver_posix.h", + "src/core/client_config/resolvers/sockaddr_resolver.h", "src/core/client_config/subchannel.h", "src/core/client_config/subchannel_factory.h", "src/core/client_config/uri_parser.h", @@ -287,7 +287,7 @@ cc_library( "src/core/client_config/resolver_factory.c", "src/core/client_config/resolver_registry.c", "src/core/client_config/resolvers/dns_resolver.c", - "src/core/client_config/resolvers/unix_resolver_posix.c", + "src/core/client_config/resolvers/sockaddr_resolver.c", "src/core/client_config/subchannel.c", "src/core/client_config/subchannel_factory.c", "src/core/client_config/uri_parser.c", @@ -424,7 +424,7 @@ cc_library( "src/core/client_config/resolver_factory.h", "src/core/client_config/resolver_registry.h", "src/core/client_config/resolvers/dns_resolver.h", - "src/core/client_config/resolvers/unix_resolver_posix.h", + "src/core/client_config/resolvers/sockaddr_resolver.h", "src/core/client_config/subchannel.h", "src/core/client_config/subchannel_factory.h", "src/core/client_config/uri_parser.h", @@ -520,7 +520,7 @@ cc_library( "src/core/client_config/resolver_factory.c", "src/core/client_config/resolver_registry.c", "src/core/client_config/resolvers/dns_resolver.c", - "src/core/client_config/resolvers/unix_resolver_posix.c", + "src/core/client_config/resolvers/sockaddr_resolver.c", "src/core/client_config/subchannel.c", "src/core/client_config/subchannel_factory.c", "src/core/client_config/uri_parser.c", @@ -998,7 +998,7 @@ objc_library( "src/core/client_config/resolver_factory.c", "src/core/client_config/resolver_registry.c", "src/core/client_config/resolvers/dns_resolver.c", - "src/core/client_config/resolvers/unix_resolver_posix.c", + "src/core/client_config/resolvers/sockaddr_resolver.c", "src/core/client_config/subchannel.c", "src/core/client_config/subchannel_factory.c", "src/core/client_config/uri_parser.c", @@ -1137,7 +1137,7 @@ objc_library( "src/core/client_config/resolver_factory.h", "src/core/client_config/resolver_registry.h", "src/core/client_config/resolvers/dns_resolver.h", - "src/core/client_config/resolvers/unix_resolver_posix.h", + "src/core/client_config/resolvers/sockaddr_resolver.h", "src/core/client_config/subchannel.h", "src/core/client_config/subchannel_factory.h", "src/core/client_config/uri_parser.h", diff --git a/Makefile b/Makefile index 4756b75fdf..5117cbbc31 100644 --- a/Makefile +++ b/Makefile @@ -3517,7 +3517,7 @@ LIBGRPC_SRC = \ src/core/client_config/resolver_factory.c \ src/core/client_config/resolver_registry.c \ src/core/client_config/resolvers/dns_resolver.c \ - src/core/client_config/resolvers/unix_resolver_posix.c \ + src/core/client_config/resolvers/sockaddr_resolver.c \ src/core/client_config/subchannel.c \ src/core/client_config/subchannel_factory.c \ src/core/client_config/uri_parser.c \ @@ -3781,7 +3781,7 @@ LIBGRPC_UNSECURE_SRC = \ src/core/client_config/resolver_factory.c \ src/core/client_config/resolver_registry.c \ src/core/client_config/resolvers/dns_resolver.c \ - src/core/client_config/resolvers/unix_resolver_posix.c \ + src/core/client_config/resolvers/sockaddr_resolver.c \ src/core/client_config/subchannel.c \ src/core/client_config/subchannel_factory.c \ src/core/client_config/uri_parser.c \ diff --git a/build.json b/build.json index 2755703e1c..783fa800e9 100644 --- a/build.json +++ b/build.json @@ -129,7 +129,7 @@ "src/core/client_config/resolver_factory.h", "src/core/client_config/resolver_registry.h", "src/core/client_config/resolvers/dns_resolver.h", - "src/core/client_config/resolvers/unix_resolver_posix.h", + "src/core/client_config/resolvers/sockaddr_resolver.h", "src/core/client_config/subchannel.h", "src/core/client_config/subchannel_factory.h", "src/core/client_config/uri_parser.h", @@ -225,7 +225,7 @@ "src/core/client_config/resolver_factory.c", "src/core/client_config/resolver_registry.c", "src/core/client_config/resolvers/dns_resolver.c", - "src/core/client_config/resolvers/unix_resolver_posix.c", + "src/core/client_config/resolvers/sockaddr_resolver.c", "src/core/client_config/subchannel.c", "src/core/client_config/subchannel_factory.c", "src/core/client_config/uri_parser.c", diff --git a/gRPC.podspec b/gRPC.podspec index 28d9ac4c53..56e6628831 100644 --- a/gRPC.podspec +++ b/gRPC.podspec @@ -170,7 +170,7 @@ Pod::Spec.new do |s| 'src/core/client_config/resolver_factory.h', 'src/core/client_config/resolver_registry.h', 'src/core/client_config/resolvers/dns_resolver.h', - 'src/core/client_config/resolvers/unix_resolver_posix.h', + 'src/core/client_config/resolvers/sockaddr_resolver.h', 'src/core/client_config/subchannel.h', 'src/core/client_config/subchannel_factory.h', 'src/core/client_config/uri_parser.h', @@ -296,7 +296,7 @@ Pod::Spec.new do |s| 'src/core/client_config/resolver_factory.c', 'src/core/client_config/resolver_registry.c', 'src/core/client_config/resolvers/dns_resolver.c', - 'src/core/client_config/resolvers/unix_resolver_posix.c', + 'src/core/client_config/resolvers/sockaddr_resolver.c', 'src/core/client_config/subchannel.c', 'src/core/client_config/subchannel_factory.c', 'src/core/client_config/uri_parser.c', @@ -434,7 +434,7 @@ Pod::Spec.new do |s| 'src/core/client_config/resolver_factory.h', 'src/core/client_config/resolver_registry.h', 'src/core/client_config/resolvers/dns_resolver.h', - 'src/core/client_config/resolvers/unix_resolver_posix.h', + 'src/core/client_config/resolvers/sockaddr_resolver.h', 'src/core/client_config/subchannel.h', 'src/core/client_config/subchannel_factory.h', 'src/core/client_config/uri_parser.h', diff --git a/include/grpc/support/host_port.h b/include/grpc/support/host_port.h index 3cc2f498e8..30267ab1df 100644 --- a/include/grpc/support/host_port.h +++ b/include/grpc/support/host_port.h @@ -52,8 +52,10 @@ int gpr_join_host_port(char **out, const char *host, int port); /* Given a name in the form "host:port" or "[ho:st]:port", split into hostname and port number, into newly allocated strings, which must later be - destroyed using gpr_free(). */ -void gpr_split_host_port(const char *name, char **host, char **port); + destroyed using gpr_free(). + Return 1 on success, 0 on failure. Guarantees *host and *port == NULL on + failure. */ +int gpr_split_host_port(const char *name, char **host, char **port); #ifdef __cplusplus } diff --git a/src/core/client_config/README.md b/src/core/client_config/README.md index d0700cfb13..fff7a5af5b 100644 --- a/src/core/client_config/README.md +++ b/src/core/client_config/README.md @@ -60,3 +60,7 @@ unix:path - the unix scheme is used to create and connect to unix domain sockets - the authority must be empty, and the path represents the absolute or relative path to the desired socket + +ipv4:host:port - a pre-resolved ipv4 dotted decimal address/port combination + +ipv6:[host]:port - a pre-resolved ipv6 address/port combination diff --git a/src/core/client_config/resolvers/sockaddr_resolver.c b/src/core/client_config/resolvers/sockaddr_resolver.c new file mode 100644 index 0000000000..d42f8b1798 --- /dev/null +++ b/src/core/client_config/resolvers/sockaddr_resolver.c @@ -0,0 +1,297 @@ +/* + * + * 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. + * + */ + +#include + +#include "src/core/client_config/resolvers/sockaddr_resolver.h" + +#include +#include +#include + +#include +#include +#include + +#include "src/core/client_config/lb_policies/pick_first.h" +#include "src/core/iomgr/resolve_address.h" +#include "src/core/support/string.h" + +typedef struct { + /** base class: must be first */ + grpc_resolver base; + /** refcount */ + gpr_refcount refs; + /** subchannel factory */ + grpc_subchannel_factory *subchannel_factory; + /** load balancing policy factory */ + grpc_lb_policy *(*lb_policy_factory)(grpc_subchannel **subchannels, + size_t num_subchannels); + + /** the address that we've 'resolved' */ + struct sockaddr_storage addr; + int addr_len; + + /** mutex guarding the rest of the state */ + gpr_mu mu; + /** have we published? */ + int published; + /** pending next completion, or NULL */ + grpc_iomgr_closure *next_completion; + /** target config address for next completion */ + grpc_client_config **target_config; +} sockaddr_resolver; + +static void sockaddr_destroy(grpc_resolver *r); + +static void sockaddr_maybe_finish_next_locked(sockaddr_resolver *r); + +static void sockaddr_shutdown(grpc_resolver *r); +static void sockaddr_channel_saw_error(grpc_resolver *r, + struct sockaddr *failing_address, + int failing_address_len); +static void sockaddr_next(grpc_resolver *r, grpc_client_config **target_config, + grpc_iomgr_closure *on_complete); + +static const grpc_resolver_vtable sockaddr_resolver_vtable = { + sockaddr_destroy, sockaddr_shutdown, sockaddr_channel_saw_error, + sockaddr_next}; + +static void sockaddr_shutdown(grpc_resolver *resolver) { + sockaddr_resolver *r = (sockaddr_resolver *)resolver; + gpr_mu_lock(&r->mu); + if (r->next_completion != NULL) { + *r->target_config = NULL; + /* TODO(ctiller): add delayed callback */ + grpc_iomgr_add_callback(r->next_completion); + r->next_completion = NULL; + } + gpr_mu_unlock(&r->mu); +} + +static void sockaddr_channel_saw_error(grpc_resolver *resolver, + struct sockaddr *sa, int len) {} + +static void sockaddr_next(grpc_resolver *resolver, + grpc_client_config **target_config, + grpc_iomgr_closure *on_complete) { + sockaddr_resolver *r = (sockaddr_resolver *)resolver; + gpr_mu_lock(&r->mu); + GPR_ASSERT(!r->next_completion); + r->next_completion = on_complete; + r->target_config = target_config; + sockaddr_maybe_finish_next_locked(r); + gpr_mu_unlock(&r->mu); +} + +static void sockaddr_maybe_finish_next_locked(sockaddr_resolver *r) { + grpc_client_config *cfg; + grpc_lb_policy *lb_policy; + grpc_subchannel *subchannel; + grpc_subchannel_args args; + + if (r->next_completion != NULL && !r->published) { + cfg = grpc_client_config_create(); + memset(&args, 0, sizeof(args)); + args.addr = (struct sockaddr *)&r->addr; + args.addr_len = r->addr_len; + subchannel = + grpc_subchannel_factory_create_subchannel(r->subchannel_factory, &args); + lb_policy = r->lb_policy_factory(&subchannel, 1); + grpc_client_config_set_lb_policy(cfg, lb_policy); + GRPC_LB_POLICY_UNREF(lb_policy, "unix"); + r->published = 1; + *r->target_config = cfg; + grpc_iomgr_add_callback(r->next_completion); + r->next_completion = NULL; + } +} + +static void sockaddr_destroy(grpc_resolver *gr) { + sockaddr_resolver *r = (sockaddr_resolver *)gr; + gpr_mu_destroy(&r->mu); + grpc_subchannel_factory_unref(r->subchannel_factory); + gpr_free(r); +} + +#ifdef GPR_POSIX_SOCKET +static int parse_unix(grpc_uri *uri, struct sockaddr_storage *addr, int *len) { + struct sockaddr_un *un = (struct sockaddr_un *)addr; + + un->sun_family = AF_UNIX; + strcpy(un->sun_path, uri->path); + *len = strlen(un->sun_path) + sizeof(un->sun_family) + 1; + + return 1; +} +#endif + +static int parse_ipv4(grpc_uri *uri, struct sockaddr_storage *addr, int *len) { + const char *host_port = uri->path; + char *host; + char *port; + int port_num; + int result = 0; + struct sockaddr_in *in = (struct sockaddr_in *)addr; + + if (*host_port == '/') ++host_port; + if (!gpr_split_host_port(host_port, &host, &port)) { + return 0; + } + + memset(in, 0, sizeof(*in)); + *len = sizeof(*in); + in->sin_family = AF_INET; + if (inet_aton(host, &in->sin_addr) == 0) { + gpr_log(GPR_ERROR, "invalid ipv4 address: '%s'", host); + goto done; + } + + if (port != NULL) { + if (sscanf(port, "%d", &port_num) != 1 || port_num < 0 || + port_num > 65535) { + gpr_log(GPR_ERROR, "invalid ipv4 port: '%s'", port); + goto done; + } + in->sin_port = htons(port_num); + } else { + gpr_log(GPR_ERROR, "no port given for ipv4 scheme"); + goto done; + } + + result = 1; +done: + gpr_free(host); + gpr_free(port); + return result; +} + +static int parse_ipv6(grpc_uri *uri, struct sockaddr_storage *addr, int *len) { + const char *host_port = uri->path; + char *host; + char *port; + int port_num; + int result = 0; + struct sockaddr_in6 *in6 = (struct sockaddr_in6 *)addr; + + if (*host_port == '/') ++host_port; + if (!gpr_split_host_port(host_port, &host, &port)) { + return 0; + } + + memset(in6, 0, sizeof(*in6)); + *len = sizeof(*in6); + in6->sin6_family = AF_INET6; + if (inet_pton(AF_INET6, host, &in6->sin6_addr) == 0) { + gpr_log(GPR_ERROR, "invalid ipv6 address: '%s'", host); + goto done; + } + + if (port != NULL) { + if (sscanf(port, "%d", &port_num) != 1 || port_num < 0 || + port_num > 65535) { + gpr_log(GPR_ERROR, "invalid ipv6 port: '%s'", port); + goto done; + } + in6->sin6_port = htons(port_num); + } else { + gpr_log(GPR_ERROR, "no port given for ipv6 scheme"); + goto done; + } + + result = 1; +done: + gpr_free(host); + gpr_free(port); + return result; +} + +static grpc_resolver *sockaddr_create( + grpc_uri *uri, + grpc_lb_policy *(*lb_policy_factory)(grpc_subchannel **subchannels, + size_t num_subchannels), + grpc_subchannel_factory *subchannel_factory, + int parse(grpc_uri *uri, struct sockaddr_storage *dst, int *len)) { + sockaddr_resolver *r; + + if (0 != strcmp(uri->authority, "")) { + gpr_log(GPR_ERROR, "authority based uri's not supported"); + return NULL; + } + + r = gpr_malloc(sizeof(sockaddr_resolver)); + memset(r, 0, sizeof(*r)); + if (!parse(uri, &r->addr, &r->addr_len)) { + gpr_free(r); + return NULL; + } + + gpr_ref_init(&r->refs, 1); + gpr_mu_init(&r->mu); + grpc_resolver_init(&r->base, &sockaddr_resolver_vtable); + r->subchannel_factory = subchannel_factory; + r->lb_policy_factory = lb_policy_factory; + + grpc_subchannel_factory_ref(subchannel_factory); + return &r->base; +} + +/* + * FACTORY + */ + +static void sockaddr_factory_ref(grpc_resolver_factory *factory) {} + +static void sockaddr_factory_unref(grpc_resolver_factory *factory) {} + +#define DECL_FACTORY(name) \ + static grpc_resolver *name##_factory_create_resolver( \ + grpc_resolver_factory *factory, grpc_uri *uri, \ + grpc_subchannel_factory *subchannel_factory) { \ + return sockaddr_create(uri, grpc_create_pick_first_lb_policy, \ + subchannel_factory, parse_##name); \ + } \ + static const grpc_resolver_factory_vtable name##_factory_vtable = { \ + sockaddr_factory_ref, sockaddr_factory_unref, \ + name##_factory_create_resolver}; \ + static grpc_resolver_factory name##_resolver_factory = { \ + &name##_factory_vtable}; \ + grpc_resolver_factory *grpc_##name##_resolver_factory_create() { \ + return &name##_resolver_factory; \ + } + +#ifdef GPR_POSIX_SOCKET +DECL_FACTORY(unix) +#endif +DECL_FACTORY(ipv4) +DECL_FACTORY(ipv6) diff --git a/src/core/client_config/resolvers/sockaddr_resolver.h b/src/core/client_config/resolvers/sockaddr_resolver.h new file mode 100644 index 0000000000..1b7a18f9c2 --- /dev/null +++ b/src/core/client_config/resolvers/sockaddr_resolver.h @@ -0,0 +1,50 @@ +/* + * + * 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_CLIENT_CONFIG_RESOLVERS_UNIX_RESOLVER_H +#define GRPC_INTERNAL_CORE_CLIENT_CONFIG_RESOLVERS_UNIX_RESOLVER_H + +#include + +#include "src/core/client_config/resolver_factory.h" + +grpc_resolver_factory *grpc_ipv4_resolver_factory_create(void); + +grpc_resolver_factory *grpc_ipv6_resolver_factory_create(void); + +#ifdef GPR_POSIX_SOCKET +/** Create a unix resolver factory */ +grpc_resolver_factory *grpc_unix_resolver_factory_create(void); +#endif + +#endif /* GRPC_INTERNAL_CORE_CLIENT_CONFIG_RESOLVERS_UNIX_RESOLVER_H */ diff --git a/src/core/client_config/resolvers/unix_resolver_posix.c b/src/core/client_config/resolvers/unix_resolver_posix.c deleted file mode 100644 index be515d2689..0000000000 --- a/src/core/client_config/resolvers/unix_resolver_posix.c +++ /dev/null @@ -1,195 +0,0 @@ -/* - * - * 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. - * - */ - -#include -#ifdef GPR_POSIX_SOCKET - -#include "src/core/client_config/resolvers/unix_resolver_posix.h" - -#include -#include - -#include -#include - -#include "src/core/client_config/lb_policies/pick_first.h" -#include "src/core/iomgr/resolve_address.h" -#include "src/core/support/string.h" - -typedef struct { - /** base class: must be first */ - grpc_resolver base; - /** refcount */ - gpr_refcount refs; - /** subchannel factory */ - grpc_subchannel_factory *subchannel_factory; - /** load balancing policy factory */ - grpc_lb_policy *(*lb_policy_factory)(grpc_subchannel **subchannels, - size_t num_subchannels); - - /** the address that we've 'resolved' */ - struct sockaddr_un addr; - int addr_len; - - /** mutex guarding the rest of the state */ - gpr_mu mu; - /** have we published? */ - int published; - /** pending next completion, or NULL */ - grpc_iomgr_closure *next_completion; - /** target config address for next completion */ - grpc_client_config **target_config; -} unix_resolver; - -static void unix_destroy(grpc_resolver *r); - -static void unix_maybe_finish_next_locked(unix_resolver *r); - -static void unix_shutdown(grpc_resolver *r); -static void unix_channel_saw_error(grpc_resolver *r, - struct sockaddr *failing_address, - int failing_address_len); -static void unix_next(grpc_resolver *r, grpc_client_config **target_config, - grpc_iomgr_closure *on_complete); - -static const grpc_resolver_vtable unix_resolver_vtable = { - unix_destroy, unix_shutdown, unix_channel_saw_error, unix_next}; - -static void unix_shutdown(grpc_resolver *resolver) { - unix_resolver *r = (unix_resolver *)resolver; - gpr_mu_lock(&r->mu); - if (r->next_completion != NULL) { - *r->target_config = NULL; - /* TODO(ctiller): add delayed callback */ - grpc_iomgr_add_callback(r->next_completion); - r->next_completion = NULL; - } - gpr_mu_unlock(&r->mu); -} - -static void unix_channel_saw_error(grpc_resolver *resolver, struct sockaddr *sa, - int len) {} - -static void unix_next(grpc_resolver *resolver, - grpc_client_config **target_config, - grpc_iomgr_closure *on_complete) { - unix_resolver *r = (unix_resolver *)resolver; - gpr_mu_lock(&r->mu); - GPR_ASSERT(!r->next_completion); - r->next_completion = on_complete; - r->target_config = target_config; - unix_maybe_finish_next_locked(r); - gpr_mu_unlock(&r->mu); -} - -static void unix_maybe_finish_next_locked(unix_resolver *r) { - grpc_client_config *cfg; - grpc_lb_policy *lb_policy; - grpc_subchannel *subchannel; - grpc_subchannel_args args; - - if (r->next_completion != NULL && !r->published) { - cfg = grpc_client_config_create(); - memset(&args, 0, sizeof(args)); - args.addr = (struct sockaddr *)&r->addr; - args.addr_len = r->addr_len; - subchannel = - grpc_subchannel_factory_create_subchannel(r->subchannel_factory, &args); - lb_policy = r->lb_policy_factory(&subchannel, 1); - grpc_client_config_set_lb_policy(cfg, lb_policy); - GRPC_LB_POLICY_UNREF(lb_policy, "unix"); - r->published = 1; - *r->target_config = cfg; - grpc_iomgr_add_callback(r->next_completion); - r->next_completion = NULL; - } -} - -static void unix_destroy(grpc_resolver *gr) { - unix_resolver *r = (unix_resolver *)gr; - gpr_mu_destroy(&r->mu); - grpc_subchannel_factory_unref(r->subchannel_factory); - gpr_free(r); -} - -static grpc_resolver *unix_create( - grpc_uri *uri, - grpc_lb_policy *(*lb_policy_factory)(grpc_subchannel **subchannels, - size_t num_subchannels), - grpc_subchannel_factory *subchannel_factory) { - unix_resolver *r; - - if (0 != strcmp(uri->authority, "")) { - gpr_log(GPR_ERROR, "authority based uri's not supported"); - return NULL; - } - - r = gpr_malloc(sizeof(unix_resolver)); - memset(r, 0, sizeof(*r)); - gpr_ref_init(&r->refs, 1); - gpr_mu_init(&r->mu); - grpc_resolver_init(&r->base, &unix_resolver_vtable); - r->subchannel_factory = subchannel_factory; - r->lb_policy_factory = lb_policy_factory; - - r->addr.sun_family = AF_UNIX; - strcpy(r->addr.sun_path, uri->path); - r->addr_len = strlen(r->addr.sun_path) + sizeof(r->addr.sun_family) + 1; - - grpc_subchannel_factory_ref(subchannel_factory); - return &r->base; -} - -/* - * FACTORY - */ - -static void unix_factory_ref(grpc_resolver_factory *factory) {} - -static void unix_factory_unref(grpc_resolver_factory *factory) {} - -static grpc_resolver *unix_factory_create_resolver( - grpc_resolver_factory *factory, grpc_uri *uri, - grpc_subchannel_factory *subchannel_factory) { - return unix_create(uri, grpc_create_pick_first_lb_policy, subchannel_factory); -} - -static const grpc_resolver_factory_vtable unix_factory_vtable = { - unix_factory_ref, unix_factory_unref, unix_factory_create_resolver}; -static grpc_resolver_factory unix_resolver_factory = {&unix_factory_vtable}; - -grpc_resolver_factory *grpc_unix_resolver_factory_create() { - return &unix_resolver_factory; -} - -#endif diff --git a/src/core/client_config/resolvers/unix_resolver_posix.h b/src/core/client_config/resolvers/unix_resolver_posix.h deleted file mode 100644 index 57ace59e21..0000000000 --- a/src/core/client_config/resolvers/unix_resolver_posix.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * - * 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_CLIENT_CONFIG_RESOLVERS_UNIX_RESOLVER_H -#define GRPC_INTERNAL_CORE_CLIENT_CONFIG_RESOLVERS_UNIX_RESOLVER_H - -#include - -#include "src/core/client_config/resolver_factory.h" - -/** Create a unix resolver factory */ -grpc_resolver_factory *grpc_unix_resolver_factory_create(void); - -#endif /* GRPC_INTERNAL_CORE_CLIENT_CONFIG_RESOLVERS_UNIX_RESOLVER_H */ diff --git a/src/core/support/host_port.c b/src/core/support/host_port.c index 0906ebc2a3..a28f04df9c 100644 --- a/src/core/support/host_port.c +++ b/src/core/support/host_port.c @@ -50,7 +50,7 @@ int gpr_join_host_port(char **out, const char *host, int port) { } } -void gpr_split_host_port(const char *name, char **host, char **port) { +int gpr_split_host_port(const char *name, char **host, char **port) { const char *host_start; size_t host_len; const char *port_start; @@ -63,7 +63,7 @@ void gpr_split_host_port(const char *name, char **host, char **port) { const char *rbracket = strchr(name, ']'); if (rbracket == NULL) { /* Unmatched [ */ - return; + return 0; } if (rbracket[1] == '\0') { /* ] */ @@ -73,14 +73,14 @@ void gpr_split_host_port(const char *name, char **host, char **port) { port_start = rbracket + 2; } else { /* ] */ - return; + return 0; } host_start = name + 1; host_len = (size_t)(rbracket - host_start); if (memchr(host_start, ':', host_len) == NULL) { /* Require all bracketed hosts to contain a colon, because a hostname or IPv4 address should never use brackets. */ - return; + return 0; } } else { const char *colon = strchr(name, ':'); @@ -105,4 +105,6 @@ void gpr_split_host_port(const char *name, char **host, char **port) { if (port_start != NULL) { *port = gpr_strdup(port_start); } + + return 1; } diff --git a/src/core/surface/init.c b/src/core/surface/init.c index 04e27d30ac..5cba479317 100644 --- a/src/core/surface/init.c +++ b/src/core/surface/init.c @@ -39,6 +39,7 @@ #include "src/core/channel/channel_stack.h" #include "src/core/client_config/resolver_registry.h" #include "src/core/client_config/resolvers/dns_resolver.h" +#include "src/core/client_config/resolvers/sockaddr_resolver.h" #include "src/core/debug/trace.h" #include "src/core/iomgr/iomgr.h" #include "src/core/profiling/timers.h" @@ -47,10 +48,6 @@ #include "src/core/surface/surface_trace.h" #include "src/core/transport/chttp2_transport.h" -#ifdef GPR_POSIX_SOCKET -#include "src/core/client_config/resolvers/unix_resolver_posix.h" -#endif - static gpr_once g_basic_init = GPR_ONCE_INIT; static gpr_mu g_init_mu; static int g_initializations; @@ -68,6 +65,8 @@ void grpc_init(void) { gpr_time_init(); grpc_resolver_registry_init("dns:///"); grpc_register_resolver_type("dns", grpc_dns_resolver_factory_create()); + grpc_register_resolver_type("ipv4", grpc_ipv4_resolver_factory_create()); + grpc_register_resolver_type("ipv6", grpc_ipv6_resolver_factory_create()); #ifdef GPR_POSIX_SOCKET grpc_register_resolver_type("unix", grpc_unix_resolver_factory_create()); #endif diff --git a/test/core/end2end/dualstack_socket_test.c b/test/core/end2end/dualstack_socket_test.c index 7d3568c22e..790758918f 100644 --- a/test/core/end2end/dualstack_socket_test.c +++ b/test/core/end2end/dualstack_socket_test.c @@ -37,6 +37,7 @@ #include #include #include +#include #include "test/core/end2end/cq_verifier.h" #include "test/core/util/port.h" #include "test/core/util/test_config.h" @@ -105,7 +106,12 @@ void test_connect(const char *server_host, const char *client_host, int port, cqv = cq_verifier_create(cq); /* Create client. */ - gpr_join_host_port(&client_hostport, client_host, port); + if (client_host[0] == 'i') { + /* for ipv4:/ipv6: addresses, just concatenate the port */ + gpr_asprintf(&client_hostport, "%s:%d", client_host, port); + } else { + gpr_join_host_port(&client_hostport, client_host, port); + } client = grpc_channel_create(client_hostport, NULL); gpr_log(GPR_INFO, "Testing with server=%s client=%s (expecting %s)", @@ -233,21 +239,31 @@ int main(int argc, char **argv) { /* :: and 0.0.0.0 are handled identically. */ test_connect("::", "127.0.0.1", 0, 1); test_connect("::", "::ffff:127.0.0.1", 0, 1); + test_connect("::", "ipv4:127.0.0.1", 0, 1); + test_connect("::", "ipv6:[::ffff:127.0.0.1]", 0, 1); test_connect("::", "localhost", 0, 1); test_connect("0.0.0.0", "127.0.0.1", 0, 1); test_connect("0.0.0.0", "::ffff:127.0.0.1", 0, 1); + test_connect("0.0.0.0", "ipv4:127.0.0.1", 0, 1); + test_connect("0.0.0.0", "ipv6:[::ffff:127.0.0.1]", 0, 1); test_connect("0.0.0.0", "localhost", 0, 1); if (do_ipv6) { test_connect("::", "::1", 0, 1); test_connect("0.0.0.0", "::1", 0, 1); + test_connect("::", "ipv6:[::1]", 0, 1); + test_connect("0.0.0.0", "ipv6:[::1]", 0, 1); } /* These only work when the families agree. */ test_connect("127.0.0.1", "127.0.0.1", 0, 1); + test_connect("127.0.0.1", "ipv4:127.0.0.1", 0, 1); if (do_ipv6) { test_connect("::1", "::1", 0, 1); test_connect("::1", "127.0.0.1", 0, 0); test_connect("127.0.0.1", "::1", 0, 0); + test_connect("::1", "ipv6:[::1]", 0, 1); + test_connect("::1", "ipv4:127.0.0.1", 0, 0); + test_connect("127.0.0.1", "ipv6:[::1]", 0, 0); } } diff --git a/tools/doxygen/Doxyfile.core.internal b/tools/doxygen/Doxyfile.core.internal index 31ad56a97c..bcbb8522ac 100644 --- a/tools/doxygen/Doxyfile.core.internal +++ b/tools/doxygen/Doxyfile.core.internal @@ -803,7 +803,7 @@ src/core/client_config/resolver.h \ src/core/client_config/resolver_factory.h \ src/core/client_config/resolver_registry.h \ src/core/client_config/resolvers/dns_resolver.h \ -src/core/client_config/resolvers/unix_resolver_posix.h \ +src/core/client_config/resolvers/sockaddr_resolver.h \ src/core/client_config/subchannel.h \ src/core/client_config/subchannel_factory.h \ src/core/client_config/uri_parser.h \ @@ -922,7 +922,7 @@ src/core/client_config/resolver.c \ src/core/client_config/resolver_factory.c \ src/core/client_config/resolver_registry.c \ src/core/client_config/resolvers/dns_resolver.c \ -src/core/client_config/resolvers/unix_resolver_posix.c \ +src/core/client_config/resolvers/sockaddr_resolver.c \ src/core/client_config/subchannel.c \ src/core/client_config/subchannel_factory.c \ src/core/client_config/uri_parser.c \ diff --git a/tools/run_tests/sources_and_headers.json b/tools/run_tests/sources_and_headers.json index abddaab699..1d02f8bea5 100644 --- a/tools/run_tests/sources_and_headers.json +++ b/tools/run_tests/sources_and_headers.json @@ -9867,7 +9867,7 @@ "src/core/client_config/resolver_factory.h", "src/core/client_config/resolver_registry.h", "src/core/client_config/resolvers/dns_resolver.h", - "src/core/client_config/resolvers/unix_resolver_posix.h", + "src/core/client_config/resolvers/sockaddr_resolver.h", "src/core/client_config/subchannel.h", "src/core/client_config/subchannel_factory.h", "src/core/client_config/uri_parser.h", @@ -10011,8 +10011,8 @@ "src/core/client_config/resolver_registry.h", "src/core/client_config/resolvers/dns_resolver.c", "src/core/client_config/resolvers/dns_resolver.h", - "src/core/client_config/resolvers/unix_resolver_posix.c", - "src/core/client_config/resolvers/unix_resolver_posix.h", + "src/core/client_config/resolvers/sockaddr_resolver.c", + "src/core/client_config/resolvers/sockaddr_resolver.h", "src/core/client_config/subchannel.c", "src/core/client_config/subchannel.h", "src/core/client_config/subchannel_factory.c", @@ -10329,7 +10329,7 @@ "src/core/client_config/resolver_factory.h", "src/core/client_config/resolver_registry.h", "src/core/client_config/resolvers/dns_resolver.h", - "src/core/client_config/resolvers/unix_resolver_posix.h", + "src/core/client_config/resolvers/sockaddr_resolver.h", "src/core/client_config/subchannel.h", "src/core/client_config/subchannel_factory.h", "src/core/client_config/uri_parser.h", @@ -10455,8 +10455,8 @@ "src/core/client_config/resolver_registry.h", "src/core/client_config/resolvers/dns_resolver.c", "src/core/client_config/resolvers/dns_resolver.h", - "src/core/client_config/resolvers/unix_resolver_posix.c", - "src/core/client_config/resolvers/unix_resolver_posix.h", + "src/core/client_config/resolvers/sockaddr_resolver.c", + "src/core/client_config/resolvers/sockaddr_resolver.h", "src/core/client_config/subchannel.c", "src/core/client_config/subchannel.h", "src/core/client_config/subchannel_factory.c", diff --git a/vsprojects/grpc/grpc.vcxproj b/vsprojects/grpc/grpc.vcxproj index 6f929a72c7..c92e69d699 100644 --- a/vsprojects/grpc/grpc.vcxproj +++ b/vsprojects/grpc/grpc.vcxproj @@ -192,7 +192,7 @@ - + @@ -354,7 +354,7 @@ - + diff --git a/vsprojects/grpc/grpc.vcxproj.filters b/vsprojects/grpc/grpc.vcxproj.filters index 0c388222c2..b5dbf3df9d 100644 --- a/vsprojects/grpc/grpc.vcxproj.filters +++ b/vsprojects/grpc/grpc.vcxproj.filters @@ -124,7 +124,7 @@ src\core\client_config\resolvers - + src\core\client_config\resolvers @@ -539,7 +539,7 @@ src\core\client_config\resolvers - + src\core\client_config\resolvers diff --git a/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj b/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj index 753f3424b3..0039ddd034 100644 --- a/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj +++ b/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj @@ -173,7 +173,7 @@ - + @@ -289,7 +289,7 @@ - + diff --git a/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj.filters b/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj.filters index 1b312cca51..cf04cb9ac7 100644 --- a/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj.filters +++ b/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj.filters @@ -55,7 +55,7 @@ src\core\client_config\resolvers - + src\core\client_config\resolvers @@ -416,7 +416,7 @@ src\core\client_config\resolvers - + src\core\client_config\resolvers -- cgit v1.2.3 From b46f3f400e4966f89737c376f7eb39b9511e375b Mon Sep 17 00:00:00 2001 From: Alistair Veitch Date: Mon, 20 Jul 2015 13:34:10 -0700 Subject: fix build --- BUILD | 6 +++--- gRPC.podspec | 4 ++-- tools/doxygen/Doxyfile.core.internal | 2 +- tools/run_tests/sources_and_headers.json | 8 ++++---- vsprojects/grpc/grpc.vcxproj | 2 +- vsprojects/grpc/grpc.vcxproj.filters | 2 +- vsprojects/grpc_unsecure/grpc_unsecure.vcxproj | 2 +- vsprojects/grpc_unsecure/grpc_unsecure.vcxproj.filters | 2 +- 8 files changed, 14 insertions(+), 14 deletions(-) diff --git a/BUILD b/BUILD index bdcef1e20b..336074a9d8 100644 --- a/BUILD +++ b/BUILD @@ -243,7 +243,7 @@ cc_library( "src/core/transport/transport.h", "src/core/transport/transport_impl.h", "src/core/census/context.h", - "src/core/census/resource_id.h", + "src/core/census/rpc_stat_id.h", "src/core/httpcli/format_request.c", "src/core/httpcli/httpcli.c", "src/core/httpcli/httpcli_security_connector.c", @@ -499,7 +499,7 @@ cc_library( "src/core/transport/transport.h", "src/core/transport/transport_impl.h", "src/core/census/context.h", - "src/core/census/resource_id.h", + "src/core/census/rpc_stat_id.h", "src/core/surface/init_unsecure.c", "src/core/census/grpc_context.c", "src/core/channel/channel_args.c", @@ -1207,7 +1207,7 @@ objc_library( "src/core/transport/transport.h", "src/core/transport/transport_impl.h", "src/core/census/context.h", - "src/core/census/resource_id.h", + "src/core/census/rpc_stat_id.h", ], includes = [ "include", diff --git a/gRPC.podspec b/gRPC.podspec index 5d2459b289..4f9364d92b 100644 --- a/gRPC.podspec +++ b/gRPC.podspec @@ -245,7 +245,7 @@ Pod::Spec.new do |s| 'src/core/transport/transport.h', 'src/core/transport/transport_impl.h', 'src/core/census/context.h', - 'src/core/census/resource_id.h', + 'src/core/census/rpc_stat_id.h', 'grpc/grpc_security.h', 'grpc/byte_buffer.h', 'grpc/byte_buffer_reader.h', @@ -508,7 +508,7 @@ Pod::Spec.new do |s| 'src/core/transport/transport.h', 'src/core/transport/transport_impl.h', 'src/core/census/context.h', - 'src/core/census/resource_id.h' + 'src/core/census/rpc_stat_id.h' ss.header_mappings_dir = '.' diff --git a/tools/doxygen/Doxyfile.core.internal b/tools/doxygen/Doxyfile.core.internal index dd65c6cdbd..b126fe28e6 100644 --- a/tools/doxygen/Doxyfile.core.internal +++ b/tools/doxygen/Doxyfile.core.internal @@ -880,7 +880,7 @@ src/core/transport/stream_op.h \ src/core/transport/transport.h \ src/core/transport/transport_impl.h \ src/core/census/context.h \ -src/core/census/resource_id.h \ +src/core/census/rpc_stat_id.h \ src/core/httpcli/format_request.c \ src/core/httpcli/httpcli.c \ src/core/httpcli/httpcli_security_connector.c \ diff --git a/tools/run_tests/sources_and_headers.json b/tools/run_tests/sources_and_headers.json index 07fb85d9d0..c06ff14202 100644 --- a/tools/run_tests/sources_and_headers.json +++ b/tools/run_tests/sources_and_headers.json @@ -8716,7 +8716,7 @@ "include/grpc/status.h", "src/core/census/context.h", "src/core/census/grpc_context.h", - "src/core/census/resource_id.h", + "src/core/census/rpc_stat_id.h", "src/core/channel/census_filter.h", "src/core/channel/channel_args.h", "src/core/channel/channel_stack.h", @@ -8845,7 +8845,7 @@ "src/core/census/grpc_context.h", "src/core/census/initialize.c", "src/core/census/record_stat.c", - "src/core/census/resource_id.h", + "src/core/census/rpc_stat_id.h", "src/core/channel/census_filter.h", "src/core/channel/channel_args.c", "src/core/channel/channel_args.h", @@ -9172,7 +9172,7 @@ "include/grpc/status.h", "src/core/census/context.h", "src/core/census/grpc_context.h", - "src/core/census/resource_id.h", + "src/core/census/rpc_stat_id.h", "src/core/channel/census_filter.h", "src/core/channel/channel_args.h", "src/core/channel/channel_stack.h", @@ -9283,7 +9283,7 @@ "src/core/census/grpc_context.h", "src/core/census/initialize.c", "src/core/census/record_stat.c", - "src/core/census/resource_id.h", + "src/core/census/rpc_stat_id.h", "src/core/channel/census_filter.h", "src/core/channel/channel_args.c", "src/core/channel/channel_args.h", diff --git a/vsprojects/grpc/grpc.vcxproj b/vsprojects/grpc/grpc.vcxproj index 679bc33754..7998acd606 100644 --- a/vsprojects/grpc/grpc.vcxproj +++ b/vsprojects/grpc/grpc.vcxproj @@ -269,7 +269,7 @@ - + diff --git a/vsprojects/grpc/grpc.vcxproj.filters b/vsprojects/grpc/grpc.vcxproj.filters index d139a2e330..2b96dcc5d3 100644 --- a/vsprojects/grpc/grpc.vcxproj.filters +++ b/vsprojects/grpc/grpc.vcxproj.filters @@ -770,7 +770,7 @@ src\core\census - + src\core\census diff --git a/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj b/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj index 4cea37e7aa..86413fd407 100644 --- a/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj +++ b/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj @@ -250,7 +250,7 @@ - + diff --git a/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj.filters b/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj.filters index e51f0e7c50..d24e0772df 100644 --- a/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj.filters +++ b/vsprojects/grpc_unsecure/grpc_unsecure.vcxproj.filters @@ -647,7 +647,7 @@ src\core\census - + src\core\census -- cgit v1.2.3 From 1b22b9db94784bc589d372e2b30eb939d009c3d9 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Mon, 20 Jul 2015 13:42:22 -0700 Subject: Add grpc_call_get_peer --- include/grpc/grpc.h | 12 +++++++- src/core/channel/channel_stack.c | 5 ++++ src/core/channel/channel_stack.h | 5 ++++ src/core/channel/client_channel.c | 21 ++++++++++++++ src/core/channel/compress_filter.c | 1 + src/core/channel/connected_channel.c | 6 ++++ src/core/channel/http_client_filter.c | 3 +- src/core/channel/http_server_filter.c | 3 +- src/core/channel/noop_filter.c | 1 + src/core/client_config/subchannel.c | 6 ++++ src/core/client_config/subchannel.h | 3 ++ src/core/iomgr/endpoint.c | 4 +++ src/core/iomgr/endpoint.h | 3 ++ src/core/iomgr/endpoint_pair_posix.c | 8 +++--- src/core/iomgr/sockaddr_utils.c | 33 +++++++++++++++++++++- src/core/iomgr/sockaddr_utils.h | 2 ++ src/core/iomgr/tcp_client_posix.c | 12 ++++++-- src/core/iomgr/tcp_posix.c | 24 ++++++++++++---- src/core/iomgr/tcp_posix.h | 3 +- src/core/iomgr/tcp_server_posix.c | 7 +++-- src/core/security/client_auth_filter.c | 8 ++++-- src/core/security/secure_endpoint.c | 7 ++++- src/core/security/server_auth_filter.c | 8 ++++-- src/core/surface/call.c | 5 ++++ src/core/surface/channel.c | 15 ++++++++-- src/core/surface/channel.h | 2 +- src/core/surface/channel_create.c | 3 +- src/core/surface/lame_client.c | 18 +++++++++--- src/core/surface/secure_channel_create.c | 7 +++-- src/core/surface/server.c | 5 ++-- src/core/transport/chttp2/internal.h | 1 + src/core/transport/chttp2_transport.c | 16 +++++++++-- src/core/transport/transport.c | 4 +++ src/core/transport/transport.h | 3 ++ src/core/transport/transport_impl.h | 3 ++ test/core/channel/channel_stack_test.c | 13 ++++++--- test/core/end2end/dualstack_socket_test.c | 5 ++++ test/core/end2end/fixtures/chttp2_socket_pair.c | 2 +- .../chttp2_socket_pair_one_byte_at_a_time.c | 2 +- .../fixtures/chttp2_socket_pair_with_grpc_trace.c | 2 +- test/core/end2end/tests/simple_request.c | 15 ++++++++++ test/core/iomgr/tcp_posix_test.c | 17 +++++------ test/core/surface/lame_client_test.c | 2 +- 43 files changed, 264 insertions(+), 61 deletions(-) diff --git a/include/grpc/grpc.h b/include/grpc/grpc.h index 3c72c1db27..c194138e4e 100644 --- a/include/grpc/grpc.h +++ b/include/grpc/grpc.h @@ -423,6 +423,12 @@ grpc_call *grpc_channel_create_registered_call( grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops, size_t nops, void *tag); +/* Returns a newly allocated string representing the endpoint to which this + call is communicating with. The string is in the uri format accepted by + grpc_channel_create. + The returned string should be disposed of with gpr_free(). */ +char *grpc_call_get_peer(grpc_call *call); + /* Create a client channel to 'target'. Additional channel level configuration MAY be provided by grpc_channel_args, though the expectation is that most clients will want to simply pass NULL. See grpc_channel_args definition for @@ -431,8 +437,12 @@ grpc_call_error grpc_call_start_batch(grpc_call *call, const grpc_op *ops, grpc_channel *grpc_channel_create(const char *target, const grpc_channel_args *args); +/* Return a newly allocated string representing the target a channel was + created for. */ +char *grpc_channel_get_target(grpc_channel *channel); + /* Create a lame client: this client fails every operation attempted on it. */ -grpc_channel *grpc_lame_client_channel_create(void); +grpc_channel *grpc_lame_client_channel_create(const char *target); /* Close and destroy a grpc channel */ void grpc_channel_destroy(grpc_channel *channel); diff --git a/src/core/channel/channel_stack.c b/src/core/channel/channel_stack.c index e38dcb58b7..cd7c182ef2 100644 --- a/src/core/channel/channel_stack.c +++ b/src/core/channel/channel_stack.c @@ -191,6 +191,11 @@ void grpc_call_next_op(grpc_call_element *elem, grpc_transport_stream_op *op) { next_elem->filter->start_transport_stream_op(next_elem, op); } +char *grpc_call_next_get_peer(grpc_call_element *elem) { + grpc_call_element *next_elem = elem + 1; + return next_elem->filter->get_peer(next_elem); +} + void grpc_channel_next_op(grpc_channel_element *elem, grpc_transport_op *op) { grpc_channel_element *next_elem = elem + 1; next_elem->filter->start_transport_op(next_elem, op); diff --git a/src/core/channel/channel_stack.h b/src/core/channel/channel_stack.h index 785be8925b..4a608b956e 100644 --- a/src/core/channel/channel_stack.h +++ b/src/core/channel/channel_stack.h @@ -104,6 +104,9 @@ typedef struct { The filter does not need to do any chaining */ void (*destroy_channel_elem)(grpc_channel_element *elem); + /* Implement grpc_call_get_peer() */ + char *(*get_peer)(grpc_call_element *elem); + /* The name of this filter */ const char *name; } grpc_channel_filter; @@ -173,6 +176,8 @@ void grpc_call_next_op(grpc_call_element *elem, grpc_transport_stream_op *op); /* Call the next operation (depending on call directionality) in a channel stack */ void grpc_channel_next_op(grpc_channel_element *elem, grpc_transport_op *op); +/* Pass through a request to get_peer to the next child element */ +char *grpc_call_next_get_peer(grpc_call_element *elem); /* Given the top element of a channel stack, get the channel stack itself */ grpc_channel_stack *grpc_channel_stack_from_top_element( diff --git a/src/core/channel/client_channel.c b/src/core/channel/client_channel.c index 10e01ebbb4..8eb95ca822 100644 --- a/src/core/channel/client_channel.c +++ b/src/core/channel/client_channel.c @@ -280,6 +280,26 @@ static grpc_iomgr_closure *merge_into_waiting_op( return consumed_op; } +static char *cc_get_peer(grpc_call_element *elem) { + call_data *calld = elem->call_data; + channel_data *chand = elem->channel_data; + grpc_subchannel_call *subchannel_call; + char *result; + + gpr_mu_lock(&calld->mu_state); + if (calld->state == CALL_ACTIVE) { + subchannel_call = calld->subchannel_call; + GRPC_SUBCHANNEL_CALL_REF(subchannel_call, "get_peer"); + gpr_mu_unlock(&calld->mu_state); + result = grpc_subchannel_call_get_peer(subchannel_call); + GRPC_SUBCHANNEL_CALL_UNREF(subchannel_call, "get_peer"); + return result; + } else { + gpr_mu_unlock(&calld->mu_state); + return grpc_channel_get_target(chand->master); + } +} + static void perform_transport_stream_op(grpc_call_element *elem, grpc_transport_stream_op *op, int continuation) { @@ -594,6 +614,7 @@ const grpc_channel_filter grpc_client_channel_filter = { sizeof(channel_data), init_channel_elem, destroy_channel_elem, + cc_get_peer, "client-channel", }; diff --git a/src/core/channel/compress_filter.c b/src/core/channel/compress_filter.c index 14cb3da62d..d07d96f3f9 100644 --- a/src/core/channel/compress_filter.c +++ b/src/core/channel/compress_filter.c @@ -322,4 +322,5 @@ const grpc_channel_filter grpc_compress_filter = { sizeof(channel_data), init_channel_elem, destroy_channel_elem, + grpc_call_next_get_peer, "compress"}; diff --git a/src/core/channel/connected_channel.c b/src/core/channel/connected_channel.c index 34d07de519..b95ed06f2b 100644 --- a/src/core/channel/connected_channel.c +++ b/src/core/channel/connected_channel.c @@ -119,6 +119,11 @@ static void destroy_channel_elem(grpc_channel_element *elem) { grpc_transport_destroy(cd->transport); } +static char *con_get_peer(grpc_call_element *elem) { + channel_data *chand = elem->channel_data; + return grpc_transport_get_peer(chand->transport); +} + const grpc_channel_filter grpc_connected_channel_filter = { con_start_transport_stream_op, con_start_transport_op, @@ -128,6 +133,7 @@ const grpc_channel_filter grpc_connected_channel_filter = { sizeof(channel_data), init_channel_elem, destroy_channel_elem, + con_get_peer, "connected", }; diff --git a/src/core/channel/http_client_filter.c b/src/core/channel/http_client_filter.c index 63e4912397..6e8c287e3d 100644 --- a/src/core/channel/http_client_filter.c +++ b/src/core/channel/http_client_filter.c @@ -206,4 +206,5 @@ static void destroy_channel_elem(grpc_channel_element *elem) { const grpc_channel_filter grpc_http_client_filter = { hc_start_transport_op, grpc_channel_next_op, sizeof(call_data), init_call_elem, destroy_call_elem, sizeof(channel_data), - init_channel_elem, destroy_channel_elem, "http-client"}; + init_channel_elem, destroy_channel_elem, grpc_call_next_get_peer, + "http-client"}; diff --git a/src/core/channel/http_server_filter.c b/src/core/channel/http_server_filter.c index a6cbb5a7f4..7c798d2fb4 100644 --- a/src/core/channel/http_server_filter.c +++ b/src/core/channel/http_server_filter.c @@ -280,4 +280,5 @@ static void destroy_channel_elem(grpc_channel_element *elem) { const grpc_channel_filter grpc_http_server_filter = { hs_start_transport_op, grpc_channel_next_op, sizeof(call_data), init_call_elem, destroy_call_elem, sizeof(channel_data), - init_channel_elem, destroy_channel_elem, "http-server"}; + init_channel_elem, destroy_channel_elem, grpc_call_next_get_peer, + "http-server"}; diff --git a/src/core/channel/noop_filter.c b/src/core/channel/noop_filter.c index 5117723617..d631885aaf 100644 --- a/src/core/channel/noop_filter.c +++ b/src/core/channel/noop_filter.c @@ -127,4 +127,5 @@ const grpc_channel_filter grpc_no_op_filter = {noop_start_transport_stream_op, sizeof(channel_data), init_channel_elem, destroy_channel_elem, + grpc_call_next_get_peer, "no-op"}; diff --git a/src/core/client_config/subchannel.c b/src/core/client_config/subchannel.c index 35f172683a..2a12fbc86d 100644 --- a/src/core/client_config/subchannel.c +++ b/src/core/client_config/subchannel.c @@ -640,6 +640,12 @@ void grpc_subchannel_call_unref( } } +char *grpc_subchannel_call_get_peer(grpc_subchannel_call *call) { + grpc_call_stack *call_stack = SUBCHANNEL_CALL_TO_CALL_STACK(call); + grpc_call_element *top_elem = grpc_call_stack_element(call_stack, 0); + return top_elem->filter->get_peer(top_elem); +} + void grpc_subchannel_call_process_op(grpc_subchannel_call *call, grpc_transport_stream_op *op) { grpc_call_stack *call_stack = SUBCHANNEL_CALL_TO_CALL_STACK(call); diff --git a/src/core/client_config/subchannel.h b/src/core/client_config/subchannel.h index a23a623277..d1cd33b2af 100644 --- a/src/core/client_config/subchannel.h +++ b/src/core/client_config/subchannel.h @@ -100,6 +100,9 @@ void grpc_subchannel_del_interested_party(grpc_subchannel *channel, void grpc_subchannel_call_process_op(grpc_subchannel_call *subchannel_call, grpc_transport_stream_op *op); +/** continue querying for peer */ +char *grpc_subchannel_call_get_peer(grpc_subchannel_call *subchannel_call); + struct grpc_subchannel_args { /** Channel filters for this channel - wrapped factories will likely want to mutate this */ diff --git a/src/core/iomgr/endpoint.c b/src/core/iomgr/endpoint.c index 96487958a7..f2b44aad03 100644 --- a/src/core/iomgr/endpoint.c +++ b/src/core/iomgr/endpoint.c @@ -53,3 +53,7 @@ void grpc_endpoint_add_to_pollset(grpc_endpoint *ep, grpc_pollset *pollset) { void grpc_endpoint_shutdown(grpc_endpoint *ep) { ep->vtable->shutdown(ep); } void grpc_endpoint_destroy(grpc_endpoint *ep) { ep->vtable->destroy(ep); } + +char *grpc_endpoint_get_peer(grpc_endpoint *ep) { + return ep->vtable->get_peer(ep); +} diff --git a/src/core/iomgr/endpoint.h b/src/core/iomgr/endpoint.h index 881e851800..ee0becf3d6 100644 --- a/src/core/iomgr/endpoint.h +++ b/src/core/iomgr/endpoint.h @@ -72,12 +72,15 @@ struct grpc_endpoint_vtable { void (*add_to_pollset)(grpc_endpoint *ep, grpc_pollset *pollset); void (*shutdown)(grpc_endpoint *ep); void (*destroy)(grpc_endpoint *ep); + char *(*get_peer)(grpc_endpoint *ep); }; /* When data is available on the connection, calls the callback with slices. */ void grpc_endpoint_notify_on_read(grpc_endpoint *ep, grpc_endpoint_read_cb cb, void *user_data); +char *grpc_endpoint_get_peer(grpc_endpoint *ep); + /* Write slices out to the socket. If the connection is ready for more data after the end of the call, it diff --git a/src/core/iomgr/endpoint_pair_posix.c b/src/core/iomgr/endpoint_pair_posix.c index fa2d2555d6..deae9c6875 100644 --- a/src/core/iomgr/endpoint_pair_posix.c +++ b/src/core/iomgr/endpoint_pair_posix.c @@ -66,12 +66,12 @@ grpc_endpoint_pair grpc_iomgr_create_endpoint_pair(const char *name, create_sockets(sv); gpr_asprintf(&final_name, "%s:client", name); - p.client = - grpc_tcp_create(grpc_fd_create(sv[1], final_name), read_slice_size); + p.client = grpc_tcp_create(grpc_fd_create(sv[1], final_name), read_slice_size, + "socketpair-server"); gpr_free(final_name); gpr_asprintf(&final_name, "%s:server", name); - p.server = - grpc_tcp_create(grpc_fd_create(sv[0], final_name), read_slice_size); + p.server = grpc_tcp_create(grpc_fd_create(sv[0], final_name), read_slice_size, + "socketpair-client"); gpr_free(final_name); return p; } diff --git a/src/core/iomgr/sockaddr_utils.c b/src/core/iomgr/sockaddr_utils.c index e91b94f8c8..71ac12e87b 100644 --- a/src/core/iomgr/sockaddr_utils.c +++ b/src/core/iomgr/sockaddr_utils.c @@ -36,12 +36,18 @@ #include #include -#include "src/core/support/string.h" +#ifdef GPR_POSIX_SOCKET +#include +#endif + +#include #include #include #include #include +#include "src/core/support/string.h" + static const gpr_uint8 kV4MappedPrefix[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}; @@ -161,6 +167,31 @@ int grpc_sockaddr_to_string(char **out, const struct sockaddr *addr, return ret; } +char *grpc_sockaddr_to_uri(const struct sockaddr *addr) { + char *temp; + char *result; + + switch (addr->sa_family) { + case AF_INET: + grpc_sockaddr_to_string(&temp, addr, 0); + gpr_asprintf(&result, "ipv4:%s", temp); + gpr_free(temp); + return result; + case AF_INET6: + grpc_sockaddr_to_string(&temp, addr, 0); + gpr_asprintf(&result, "ipv6:%s", temp); + gpr_free(temp); + return result; +#ifdef GPR_POSIX_SOCKET + case AF_UNIX: + gpr_asprintf(&result, "unix:%s", ((struct sockaddr_un *)addr)->sun_path); + return result; +#endif + } + + return NULL; +} + int grpc_sockaddr_get_port(const struct sockaddr *addr) { switch (addr->sa_family) { case AF_INET: diff --git a/src/core/iomgr/sockaddr_utils.h b/src/core/iomgr/sockaddr_utils.h index bdfb83479b..99f1ed54da 100644 --- a/src/core/iomgr/sockaddr_utils.h +++ b/src/core/iomgr/sockaddr_utils.h @@ -84,4 +84,6 @@ int grpc_sockaddr_set_port(const struct sockaddr *addr, int port); int grpc_sockaddr_to_string(char **out, const struct sockaddr *addr, int normalize); +char *grpc_sockaddr_to_uri(const struct sockaddr *addr); + #endif /* GRPC_INTERNAL_CORE_IOMGR_SOCKADDR_UTILS_H */ diff --git a/src/core/iomgr/tcp_client_posix.c b/src/core/iomgr/tcp_client_posix.c index dc0489e64f..427cd86c4e 100644 --- a/src/core/iomgr/tcp_client_posix.c +++ b/src/core/iomgr/tcp_client_posix.c @@ -64,6 +64,7 @@ typedef struct { int refs; grpc_iomgr_closure write_closure; grpc_pollset_set *interested_parties; + char *addr_str; } async_connect; static int prepare_socket(const struct sockaddr *addr, int fd) { @@ -99,6 +100,7 @@ static void on_alarm(void *acp, int success) { gpr_mu_unlock(&ac->mu); if (done) { gpr_mu_destroy(&ac->mu); + gpr_free(ac->addr_str); gpr_free(ac); } } @@ -156,7 +158,8 @@ static void on_writable(void *acp, int success) { } } else { grpc_pollset_set_del_fd(ac->interested_parties, ac->fd); - ep = grpc_tcp_create(ac->fd, GRPC_TCP_DEFAULT_READ_SLICE_SIZE); + ep = grpc_tcp_create(ac->fd, GRPC_TCP_DEFAULT_READ_SLICE_SIZE, + ac->addr_str); goto finish; } } else { @@ -177,6 +180,7 @@ finish: gpr_mu_unlock(&ac->mu); if (done) { gpr_mu_destroy(&ac->mu); + gpr_free(ac->addr_str); gpr_free(ac); } else { grpc_alarm_cancel(&ac->alarm); @@ -223,13 +227,13 @@ void grpc_tcp_client_connect(void (*cb)(void *arg, grpc_endpoint *ep), err = connect(fd, addr, addr_len); } while (err < 0 && errno == EINTR); - grpc_sockaddr_to_string(&addr_str, addr, 1); + addr_str = grpc_sockaddr_to_uri(addr); gpr_asprintf(&name, "tcp-client:%s", addr_str); fdobj = grpc_fd_create(fd, name); if (err >= 0) { - cb(arg, grpc_tcp_create(fdobj, GRPC_TCP_DEFAULT_READ_SLICE_SIZE)); + cb(arg, grpc_tcp_create(fdobj, GRPC_TCP_DEFAULT_READ_SLICE_SIZE, addr_str)); goto done; } @@ -247,6 +251,8 @@ void grpc_tcp_client_connect(void (*cb)(void *arg, grpc_endpoint *ep), ac->cb_arg = arg; ac->fd = fdobj; ac->interested_parties = interested_parties; + ac->addr_str = addr_str; + addr_str = NULL; gpr_mu_init(&ac->mu); ac->refs = 2; ac->write_closure.cb = on_writable; diff --git a/src/core/iomgr/tcp_posix.c b/src/core/iomgr/tcp_posix.c index b6d6efc9fb..1e8432d463 100644 --- a/src/core/iomgr/tcp_posix.c +++ b/src/core/iomgr/tcp_posix.c @@ -44,15 +44,17 @@ #include #include -#include "src/core/support/string.h" -#include "src/core/debug/trace.h" -#include "src/core/profiling/timers.h" #include #include #include +#include #include #include +#include "src/core/support/string.h" +#include "src/core/debug/trace.h" +#include "src/core/profiling/timers.h" + #ifdef GPR_HAVE_MSG_NOSIGNAL #define SENDMSG_FLAGS MSG_NOSIGNAL #else @@ -282,6 +284,8 @@ typedef struct { grpc_iomgr_closure write_closure; grpc_iomgr_closure handle_read_closure; + + char *peer_string; } grpc_tcp; static void grpc_tcp_handle_read(void *arg /* grpc_tcp */, int success); @@ -296,6 +300,7 @@ static void grpc_tcp_unref(grpc_tcp *tcp) { int refcount_zero = gpr_unref(&tcp->refcount); if (refcount_zero) { grpc_fd_orphan(tcp->em_fd, NULL, "tcp_unref_orphan"); + gpr_free(tcp->peer_string); gpr_free(tcp); } } @@ -567,13 +572,20 @@ static void grpc_tcp_add_to_pollset(grpc_endpoint *ep, grpc_pollset *pollset) { grpc_pollset_add_fd(pollset, tcp->em_fd); } +static char *grpc_tcp_get_peer(grpc_endpoint *ep) { + grpc_tcp *tcp = (grpc_tcp *)ep; + return gpr_strdup(tcp->peer_string); +} + static const grpc_endpoint_vtable vtable = { - grpc_tcp_notify_on_read, grpc_tcp_write, grpc_tcp_add_to_pollset, - grpc_tcp_shutdown, grpc_tcp_destroy}; + grpc_tcp_notify_on_read, grpc_tcp_write, grpc_tcp_add_to_pollset, + grpc_tcp_shutdown, grpc_tcp_destroy, grpc_tcp_get_peer}; -grpc_endpoint *grpc_tcp_create(grpc_fd *em_fd, size_t slice_size) { +grpc_endpoint *grpc_tcp_create(grpc_fd *em_fd, size_t slice_size, + const char *peer_string) { grpc_tcp *tcp = (grpc_tcp *)gpr_malloc(sizeof(grpc_tcp)); tcp->base.vtable = &vtable; + tcp->peer_string = gpr_strdup(peer_string); tcp->fd = em_fd->fd; tcp->read_cb = NULL; tcp->write_cb = NULL; diff --git a/src/core/iomgr/tcp_posix.h b/src/core/iomgr/tcp_posix.h index 44279d5a26..d752feaeea 100644 --- a/src/core/iomgr/tcp_posix.h +++ b/src/core/iomgr/tcp_posix.h @@ -53,6 +53,7 @@ extern int grpc_tcp_trace; /* Create a tcp endpoint given a file desciptor and a read slice size. Takes ownership of fd. */ -grpc_endpoint *grpc_tcp_create(grpc_fd *fd, size_t read_slice_size); +grpc_endpoint *grpc_tcp_create(grpc_fd *fd, size_t read_slice_size, + const char *peer_string); #endif /* GRPC_INTERNAL_CORE_IOMGR_TCP_POSIX_H */ diff --git a/src/core/iomgr/tcp_server_posix.c b/src/core/iomgr/tcp_server_posix.c index 5854031c9b..8538600112 100644 --- a/src/core/iomgr/tcp_server_posix.c +++ b/src/core/iomgr/tcp_server_posix.c @@ -332,7 +332,7 @@ static void on_read(void *arg, int success) { grpc_set_socket_no_sigpipe_if_possible(fd); - grpc_sockaddr_to_string(&addr_str, (struct sockaddr *)&addr, 1); + addr_str = grpc_sockaddr_to_uri((struct sockaddr *)&addr); gpr_asprintf(&name, "tcp-server-connection:%s", addr_str); fdobj = grpc_fd_create(fd, name); @@ -342,8 +342,9 @@ static void on_read(void *arg, int success) { for (i = 0; i < sp->server->pollset_count; i++) { grpc_pollset_add_fd(sp->server->pollsets[i], fdobj); } - sp->server->cb(sp->server->cb_arg, - grpc_tcp_create(fdobj, GRPC_TCP_DEFAULT_READ_SLICE_SIZE)); + sp->server->cb( + sp->server->cb_arg, + grpc_tcp_create(fdobj, GRPC_TCP_DEFAULT_READ_SLICE_SIZE, addr_str)); gpr_free(name); gpr_free(addr_str); diff --git a/src/core/security/client_auth_filter.c b/src/core/security/client_auth_filter.c index 9e49a807f1..9a69f53a5a 100644 --- a/src/core/security/client_auth_filter.c +++ b/src/core/security/client_auth_filter.c @@ -344,6 +344,8 @@ static void destroy_channel_elem(grpc_channel_element *elem) { } const grpc_channel_filter grpc_client_auth_filter = { - auth_start_transport_op, grpc_channel_next_op, sizeof(call_data), - init_call_elem, destroy_call_elem, sizeof(channel_data), - init_channel_elem, destroy_channel_elem, "client-auth"}; + auth_start_transport_op, grpc_channel_next_op, + sizeof(call_data), init_call_elem, + destroy_call_elem, sizeof(channel_data), + init_channel_elem, destroy_channel_elem, + grpc_call_next_get_peer, "client-auth"}; diff --git a/src/core/security/secure_endpoint.c b/src/core/security/secure_endpoint.c index 3548198046..e189380ec5 100644 --- a/src/core/security/secure_endpoint.c +++ b/src/core/security/secure_endpoint.c @@ -331,9 +331,14 @@ static void endpoint_add_to_pollset(grpc_endpoint *secure_ep, grpc_endpoint_add_to_pollset(ep->wrapped_ep, pollset); } +static char *endpoint_get_peer(grpc_endpoint *secure_ep) { + secure_endpoint *ep = (secure_endpoint *)secure_ep; + return grpc_endpoint_get_peer(ep->wrapped_ep); +} + static const grpc_endpoint_vtable vtable = { endpoint_notify_on_read, endpoint_write, endpoint_add_to_pollset, - endpoint_shutdown, endpoint_unref}; + endpoint_shutdown, endpoint_unref, endpoint_get_peer}; grpc_endpoint *grpc_secure_endpoint_create( struct tsi_frame_protector *protector, grpc_endpoint *transport, diff --git a/src/core/security/server_auth_filter.c b/src/core/security/server_auth_filter.c index 10eef6d237..69789c2f0d 100644 --- a/src/core/security/server_auth_filter.c +++ b/src/core/security/server_auth_filter.c @@ -120,6 +120,8 @@ static void destroy_channel_elem(grpc_channel_element *elem) { } const grpc_channel_filter grpc_server_auth_filter = { - auth_start_transport_op, grpc_channel_next_op, sizeof(call_data), - init_call_elem, destroy_call_elem, sizeof(channel_data), - init_channel_elem, destroy_channel_elem, "server-auth"}; + auth_start_transport_op, grpc_channel_next_op, + sizeof(call_data), init_call_elem, + destroy_call_elem, sizeof(channel_data), + init_channel_elem, destroy_channel_elem, + grpc_call_next_get_peer, "server-auth"}; diff --git a/src/core/surface/call.c b/src/core/surface/call.c index 6e643b591c..2ba851da3d 100644 --- a/src/core/surface/call.c +++ b/src/core/surface/call.c @@ -1253,6 +1253,11 @@ static void execute_op(grpc_call *call, grpc_transport_stream_op *op) { elem->filter->start_transport_stream_op(elem, op); } +char *grpc_call_get_peer(grpc_call *call) { + grpc_call_element *elem = CALL_ELEM_FROM_CALL(call, 0); + return elem->filter->get_peer(elem); +} + grpc_call *grpc_call_from_top_element(grpc_call_element *elem) { return CALL_FROM_TOP_ELEM(elem); } diff --git a/src/core/surface/channel.c b/src/core/surface/channel.c index a6438ff512..4052c65cc6 100644 --- a/src/core/surface/channel.c +++ b/src/core/surface/channel.c @@ -36,12 +36,14 @@ #include #include +#include +#include +#include + #include "src/core/iomgr/iomgr.h" #include "src/core/support/string.h" #include "src/core/surface/call.h" #include "src/core/surface/init.h" -#include -#include /** Cache grpc-status: X mdelems for X = 0..NUM_CACHED_STATUS_ELEMS. * Avoids needing to take a metadata context lock for sending status @@ -73,6 +75,7 @@ struct grpc_channel { gpr_mu registered_call_mu; registered_call *registered_calls; grpc_iomgr_closure destroy_closure; + char *target; }; #define CHANNEL_STACK_FROM_CHANNEL(c) ((grpc_channel_stack *)((c) + 1)) @@ -85,13 +88,14 @@ struct grpc_channel { #define DEFAULT_MAX_MESSAGE_LENGTH (100 * 1024 * 1024) grpc_channel *grpc_channel_create_from_filters( - const grpc_channel_filter **filters, size_t num_filters, + const char *target, const grpc_channel_filter **filters, size_t num_filters, const grpc_channel_args *args, grpc_mdctx *mdctx, int is_client) { size_t i; size_t size = sizeof(grpc_channel) + grpc_channel_stack_size(filters, num_filters); grpc_channel *channel = gpr_malloc(size); memset(channel, 0, sizeof(*channel)); + channel->target = gpr_strdup(target); GPR_ASSERT(grpc_is_initialized() && "call grpc_init()"); channel->is_client = is_client; /* decremented by grpc_channel_destroy */ @@ -137,6 +141,10 @@ grpc_channel *grpc_channel_create_from_filters( return channel; } +char *grpc_channel_get_target(grpc_channel *channel) { + return gpr_strdup(channel->target); +} + static grpc_call *grpc_channel_create_call_internal( grpc_channel *channel, grpc_completion_queue *cq, grpc_mdelem *path_mdelem, grpc_mdelem *authority_mdelem, gpr_timespec deadline) { @@ -222,6 +230,7 @@ static void destroy_channel(void *p, int ok) { } grpc_mdctx_unref(channel->metadata_context); gpr_mu_destroy(&channel->registered_call_mu); + gpr_free(channel->target); gpr_free(channel); } diff --git a/src/core/surface/channel.h b/src/core/surface/channel.h index 4e03eb4411..9e0646efaa 100644 --- a/src/core/surface/channel.h +++ b/src/core/surface/channel.h @@ -38,7 +38,7 @@ #include "src/core/client_config/subchannel_factory.h" grpc_channel *grpc_channel_create_from_filters( - const grpc_channel_filter **filters, size_t count, + const char *target, const grpc_channel_filter **filters, size_t count, const grpc_channel_args *args, grpc_mdctx *mdctx, int is_client); /** Get a (borrowed) pointer to this channels underlying channel stack */ diff --git a/src/core/surface/channel_create.c b/src/core/surface/channel_create.c index 91c7b35550..778f7108fd 100644 --- a/src/core/surface/channel_create.c +++ b/src/core/surface/channel_create.c @@ -179,7 +179,8 @@ grpc_channel *grpc_channel_create(const char *target, return NULL; } - channel = grpc_channel_create_from_filters(filters, n, args, mdctx, 1); + channel = + grpc_channel_create_from_filters(target, filters, n, args, mdctx, 1); grpc_client_channel_set_resolver(grpc_channel_get_channel_stack(channel), resolver); GRPC_RESOLVER_UNREF(resolver, "create"); diff --git a/src/core/surface/lame_client.c b/src/core/surface/lame_client.c index 3f2bb5c8a9..c4215a2cfb 100644 --- a/src/core/surface/lame_client.c +++ b/src/core/surface/lame_client.c @@ -47,7 +47,10 @@ typedef struct { grpc_linked_mdelem details; } call_data; -typedef struct { grpc_mdctx *mdctx; } channel_data; +typedef struct { + grpc_mdctx *mdctx; + grpc_channel *master; +} channel_data; static void lame_start_transport_stream_op(grpc_call_element *elem, grpc_transport_stream_op *op) { @@ -82,6 +85,11 @@ static void lame_start_transport_stream_op(grpc_call_element *elem, } } +static char *lame_get_peer(grpc_call_element *elem) { + channel_data *chand = elem->channel_data; + return grpc_channel_get_target(chand->master); +} + static void lame_start_transport_op(grpc_channel_element *elem, grpc_transport_op *op) { if (op->on_connectivity_state_change) { @@ -112,6 +120,7 @@ static void init_channel_elem(grpc_channel_element *elem, grpc_channel *master, GPR_ASSERT(is_first); GPR_ASSERT(is_last); chand->mdctx = mdctx; + chand->master = master; } static void destroy_channel_elem(grpc_channel_element *elem) {} @@ -125,11 +134,12 @@ static const grpc_channel_filter lame_filter = { sizeof(channel_data), init_channel_elem, destroy_channel_elem, + lame_get_peer, "lame-client", }; -grpc_channel *grpc_lame_client_channel_create(void) { +grpc_channel *grpc_lame_client_channel_create(const char *target) { static const grpc_channel_filter *filters[] = {&lame_filter}; - return grpc_channel_create_from_filters(filters, 1, NULL, grpc_mdctx_create(), - 1); + return grpc_channel_create_from_filters(target, filters, 1, NULL, + grpc_mdctx_create(), 1); } diff --git a/src/core/surface/secure_channel_create.c b/src/core/surface/secure_channel_create.c index d87ec97b53..a280311ba0 100644 --- a/src/core/surface/secure_channel_create.c +++ b/src/core/surface/secure_channel_create.c @@ -196,13 +196,13 @@ grpc_channel *grpc_secure_channel_create(grpc_credentials *creds, if (grpc_find_security_connector_in_args(args) != NULL) { gpr_log(GPR_ERROR, "Cannot set security context in channel args."); - return grpc_lame_client_channel_create(); + return grpc_lame_client_channel_create(target); } if (grpc_credentials_create_security_connector( creds, target, args, NULL, &connector, &new_args_from_connector) != GRPC_SECURITY_OK) { - return grpc_lame_client_channel_create(); + return grpc_lame_client_channel_create(target); } mdctx = grpc_mdctx_create(); @@ -231,7 +231,8 @@ grpc_channel *grpc_secure_channel_create(grpc_credentials *creds, return NULL; } - channel = grpc_channel_create_from_filters(filters, n, args_copy, mdctx, 1); + channel = + grpc_channel_create_from_filters(target, filters, n, args_copy, mdctx, 1); grpc_client_channel_set_resolver(grpc_channel_get_channel_stack(channel), resolver); GRPC_RESOLVER_UNREF(resolver, "create"); diff --git a/src/core/surface/server.c b/src/core/surface/server.c index f2d6b11bc7..5ba6f513c0 100644 --- a/src/core/surface/server.c +++ b/src/core/surface/server.c @@ -722,6 +722,7 @@ static const grpc_channel_filter server_surface_filter = { sizeof(channel_data), init_channel_elem, destroy_channel_elem, + grpc_call_next_get_peer, "server", }; @@ -878,8 +879,8 @@ void grpc_server_setup_transport(grpc_server *s, grpc_transport *transport, grpc_transport_perform_op(transport, &op); } - channel = - grpc_channel_create_from_filters(filters, num_filters, args, mdctx, 0); + channel = grpc_channel_create_from_filters(NULL, filters, num_filters, args, + mdctx, 0); chand = (channel_data *)grpc_channel_stack_element( grpc_channel_get_channel_stack(channel), 0) ->channel_data; diff --git a/src/core/transport/chttp2/internal.h b/src/core/transport/chttp2/internal.h index e5e6f445b7..e7901da510 100644 --- a/src/core/transport/chttp2/internal.h +++ b/src/core/transport/chttp2/internal.h @@ -286,6 +286,7 @@ struct grpc_chttp2_transport { grpc_endpoint *ep; grpc_mdctx *metadata_context; gpr_refcount refs; + char *peer_string; gpr_mu mu; diff --git a/src/core/transport/chttp2_transport.c b/src/core/transport/chttp2_transport.c index c923d5e42f..eb435a2ee8 100644 --- a/src/core/transport/chttp2_transport.c +++ b/src/core/transport/chttp2_transport.c @@ -168,6 +168,7 @@ static void destruct_transport(grpc_chttp2_transport *t) { grpc_mdctx_unref(t->metadata_context); + gpr_free(t->peer_string); gpr_free(t); } @@ -217,6 +218,7 @@ static void init_transport(grpc_chttp2_transport *t, gpr_ref_init(&t->refs, 2); gpr_mu_init(&t->mu); grpc_mdctx_ref(mdctx); + t->peer_string = grpc_endpoint_get_peer(ep); t->metadata_context = mdctx; t->endpoint_reading = 1; t->global.next_stream_id = is_client ? 1 : 2; @@ -1069,9 +1071,17 @@ void grpc_chttp2_flowctl_trace(const char *file, int line, const char *reason, * INTEGRATION GLUE */ -static const grpc_transport_vtable vtable = { - sizeof(grpc_chttp2_stream), init_stream, perform_stream_op, - perform_transport_op, destroy_stream, destroy_transport}; +static char *chttp2_get_peer(grpc_transport *t) { + return gpr_strdup(((grpc_chttp2_transport *)t)->peer_string); +} + +static const grpc_transport_vtable vtable = {sizeof(grpc_chttp2_stream), + init_stream, + perform_stream_op, + perform_transport_op, + destroy_stream, + destroy_transport, + chttp2_get_peer}; grpc_transport *grpc_create_chttp2_transport( const grpc_channel_args *channel_args, grpc_endpoint *ep, grpc_mdctx *mdctx, diff --git a/src/core/transport/transport.c b/src/core/transport/transport.c index 2689e3028a..69c00b6a4f 100644 --- a/src/core/transport/transport.c +++ b/src/core/transport/transport.c @@ -65,6 +65,10 @@ void grpc_transport_destroy_stream(grpc_transport *transport, transport->vtable->destroy_stream(transport, stream); } +char *grpc_transport_get_peer(grpc_transport *transport) { + return transport->vtable->get_peer(transport); +} + void grpc_transport_stream_op_finish_with_failure( grpc_transport_stream_op *op) { if (op->send_ops) { diff --git a/src/core/transport/transport.h b/src/core/transport/transport.h index 64503604ee..553779602a 100644 --- a/src/core/transport/transport.h +++ b/src/core/transport/transport.h @@ -182,4 +182,7 @@ void grpc_transport_close(grpc_transport *transport); /* Destroy the transport */ void grpc_transport_destroy(grpc_transport *transport); +/* Get the transports peer */ +char *grpc_transport_get_peer(grpc_transport *transport); + #endif /* GRPC_INTERNAL_CORE_TRANSPORT_TRANSPORT_H */ diff --git a/src/core/transport/transport_impl.h b/src/core/transport/transport_impl.h index 515721dfb6..d3bbdf6c27 100644 --- a/src/core/transport/transport_impl.h +++ b/src/core/transport/transport_impl.h @@ -58,6 +58,9 @@ typedef struct grpc_transport_vtable { /* implementation of grpc_transport_destroy */ void (*destroy)(grpc_transport *self); + + /* implementation of grpc_transport_get_peer */ + char *(*get_peer)(grpc_transport *self); } grpc_transport_vtable; /* an instance of a grpc transport */ diff --git a/test/core/channel/channel_stack_test.c b/test/core/channel/channel_stack_test.c index eca2a40c97..60129afc43 100644 --- a/test/core/channel/channel_stack_test.c +++ b/test/core/channel/channel_stack_test.c @@ -37,6 +37,8 @@ #include #include +#include + #include "test/core/util/test_config.h" static void channel_init_func(grpc_channel_element *elem, grpc_channel *master, @@ -73,11 +75,14 @@ static void channel_func(grpc_channel_element *elem, grpc_transport_op *op) { ++*(int *)(elem->channel_data); } +static char *get_peer(grpc_call_element *elem) { return gpr_strdup("peer"); } + static void test_create_channel_stack(void) { - const grpc_channel_filter filter = { - call_func, channel_func, sizeof(int), - call_init_func, call_destroy_func, sizeof(int), - channel_init_func, channel_destroy_func, "some_test_filter"}; + const grpc_channel_filter filter = {call_func, channel_func, + sizeof(int), call_init_func, + call_destroy_func, sizeof(int), + channel_init_func, channel_destroy_func, + get_peer, "some_test_filter"}; const grpc_channel_filter *filters = &filter; grpc_channel_stack *channel_stack; grpc_call_stack *call_stack; diff --git a/test/core/end2end/dualstack_socket_test.c b/test/core/end2end/dualstack_socket_test.c index 790758918f..e7c113b36b 100644 --- a/test/core/end2end/dualstack_socket_test.c +++ b/test/core/end2end/dualstack_socket_test.c @@ -79,6 +79,7 @@ void test_connect(const char *server_host, const char *client_host, int port, size_t details_capacity = 0; int was_cancelled = 2; grpc_call_details call_details; + char *peer; if (port == 0) { port = grpc_pick_unused_port_or_die(); @@ -185,6 +186,10 @@ void test_connect(const char *server_host, const char *client_host, int port, cq_expect_completion(cqv, tag(1), 1); cq_verify(cqv); + peer = grpc_call_get_peer(c); + gpr_log(GPR_DEBUG, "got peer: '%s'", peer); + gpr_free(peer); + GPR_ASSERT(status == GRPC_STATUS_UNIMPLEMENTED); GPR_ASSERT(0 == strcmp(details, "xyz")); GPR_ASSERT(0 == strcmp(call_details.method, "/foo")); diff --git a/test/core/end2end/fixtures/chttp2_socket_pair.c b/test/core/end2end/fixtures/chttp2_socket_pair.c index 37b61cf37f..807fc8e7bc 100644 --- a/test/core/end2end/fixtures/chttp2_socket_pair.c +++ b/test/core/end2end/fixtures/chttp2_socket_pair.c @@ -80,7 +80,7 @@ static void client_setup_transport(void *ts, grpc_transport *transport, &grpc_connected_channel_filter}; size_t nfilters = sizeof(filters) / sizeof(*filters); grpc_channel *channel = grpc_channel_create_from_filters( - filters, nfilters, cs->client_args, mdctx, 1); + "socketpair-target", filters, nfilters, cs->client_args, mdctx, 1); cs->f->client = channel; diff --git a/test/core/end2end/fixtures/chttp2_socket_pair_one_byte_at_a_time.c b/test/core/end2end/fixtures/chttp2_socket_pair_one_byte_at_a_time.c index 2ec2697288..21d4404237 100644 --- a/test/core/end2end/fixtures/chttp2_socket_pair_one_byte_at_a_time.c +++ b/test/core/end2end/fixtures/chttp2_socket_pair_one_byte_at_a_time.c @@ -80,7 +80,7 @@ static void client_setup_transport(void *ts, grpc_transport *transport, &grpc_connected_channel_filter}; size_t nfilters = sizeof(filters) / sizeof(*filters); grpc_channel *channel = grpc_channel_create_from_filters( - filters, nfilters, cs->client_args, mdctx, 1); + "socketpair-target", filters, nfilters, cs->client_args, mdctx, 1); cs->f->client = channel; diff --git a/test/core/end2end/fixtures/chttp2_socket_pair_with_grpc_trace.c b/test/core/end2end/fixtures/chttp2_socket_pair_with_grpc_trace.c index 3aa364c5e0..c59628b959 100644 --- a/test/core/end2end/fixtures/chttp2_socket_pair_with_grpc_trace.c +++ b/test/core/end2end/fixtures/chttp2_socket_pair_with_grpc_trace.c @@ -81,7 +81,7 @@ static void client_setup_transport(void *ts, grpc_transport *transport, &grpc_connected_channel_filter}; size_t nfilters = sizeof(filters) / sizeof(*filters); grpc_channel *channel = grpc_channel_create_from_filters( - filters, nfilters, cs->client_args, mdctx, 1); + "socketpair-target", filters, nfilters, cs->client_args, mdctx, 1); cs->f->client = channel; diff --git a/test/core/end2end/tests/simple_request.c b/test/core/end2end/tests/simple_request.c index 6194b841d8..ca783afccd 100644 --- a/test/core/end2end/tests/simple_request.c +++ b/test/core/end2end/tests/simple_request.c @@ -114,11 +114,17 @@ static void simple_request_body(grpc_end2end_test_fixture f) { char *details = NULL; size_t details_capacity = 0; int was_cancelled = 2; + char *peer; c = grpc_channel_create_call(f.client, f.cq, "/foo", "foo.test.google.fr:1234", deadline); GPR_ASSERT(c); + peer = grpc_call_get_peer(c); + GPR_ASSERT(peer != NULL); + gpr_log(GPR_DEBUG, "client_peer_before_call=%s", peer); + gpr_free(peer); + grpc_metadata_array_init(&initial_metadata_recv); grpc_metadata_array_init(&trailing_metadata_recv); grpc_metadata_array_init(&request_metadata_recv); @@ -151,6 +157,15 @@ static void simple_request_body(grpc_end2end_test_fixture f) { cq_expect_completion(cqv, tag(101), 1); cq_verify(cqv); + peer = grpc_call_get_peer(s); + GPR_ASSERT(peer != NULL); + gpr_log(GPR_DEBUG, "server_peer=%s", peer); + gpr_free(peer); + peer = grpc_call_get_peer(c); + GPR_ASSERT(peer != NULL); + gpr_log(GPR_DEBUG, "client_peer=%s", peer); + gpr_free(peer); + op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; op->data.send_initial_metadata.count = 0; diff --git a/test/core/iomgr/tcp_posix_test.c b/test/core/iomgr/tcp_posix_test.c index a23c64928e..4e7fb446a7 100644 --- a/test/core/iomgr/tcp_posix_test.c +++ b/test/core/iomgr/tcp_posix_test.c @@ -172,7 +172,7 @@ static void read_test(ssize_t num_bytes, ssize_t slice_size) { create_sockets(sv); - ep = grpc_tcp_create(grpc_fd_create(sv[1], "read_test"), slice_size); + ep = grpc_tcp_create(grpc_fd_create(sv[1], "read_test"), slice_size, "test"); grpc_endpoint_add_to_pollset(ep, &g_pollset); written_bytes = fill_socket_partial(sv[0], num_bytes); @@ -207,7 +207,8 @@ static void large_read_test(ssize_t slice_size) { create_sockets(sv); - ep = grpc_tcp_create(grpc_fd_create(sv[1], "large_read_test"), slice_size); + ep = grpc_tcp_create(grpc_fd_create(sv[1], "large_read_test"), slice_size, + "test"); grpc_endpoint_add_to_pollset(ep, &g_pollset); written_bytes = fill_socket(sv[0]); @@ -340,7 +341,7 @@ static void write_test(ssize_t num_bytes, ssize_t slice_size) { create_sockets(sv); ep = grpc_tcp_create(grpc_fd_create(sv[1], "write_test"), - GRPC_TCP_DEFAULT_READ_SLICE_SIZE); + GRPC_TCP_DEFAULT_READ_SLICE_SIZE, "test"); grpc_endpoint_add_to_pollset(ep, &g_pollset); state.ep = ep; @@ -394,7 +395,7 @@ static void write_error_test(ssize_t num_bytes, ssize_t slice_size) { create_sockets(sv); ep = grpc_tcp_create(grpc_fd_create(sv[1], "write_error_test"), - GRPC_TCP_DEFAULT_READ_SLICE_SIZE); + GRPC_TCP_DEFAULT_READ_SLICE_SIZE, "test"); grpc_endpoint_add_to_pollset(ep, &g_pollset); close(sv[0]); @@ -459,10 +460,10 @@ static grpc_endpoint_test_fixture create_fixture_tcp_socketpair( grpc_endpoint_test_fixture f; create_sockets(sv); - f.client_ep = - grpc_tcp_create(grpc_fd_create(sv[0], "fixture:client"), slice_size); - f.server_ep = - grpc_tcp_create(grpc_fd_create(sv[1], "fixture:server"), slice_size); + f.client_ep = grpc_tcp_create(grpc_fd_create(sv[0], "fixture:client"), + slice_size, "test"); + f.server_ep = grpc_tcp_create(grpc_fd_create(sv[1], "fixture:server"), + slice_size, "test"); grpc_endpoint_add_to_pollset(f.client_ep, &g_pollset); grpc_endpoint_add_to_pollset(f.server_ep, &g_pollset); diff --git a/test/core/surface/lame_client_test.c b/test/core/surface/lame_client_test.c index b2facd33b1..3a7a3a36ee 100644 --- a/test/core/surface/lame_client_test.c +++ b/test/core/surface/lame_client_test.c @@ -57,7 +57,7 @@ int main(int argc, char **argv) { grpc_metadata_array_init(&trailing_metadata_recv); - chan = grpc_lame_client_channel_create(); + chan = grpc_lame_client_channel_create("lampoon:national"); GPR_ASSERT(chan); cq = grpc_completion_queue_create(); call = grpc_channel_create_call(chan, cq, "/Foo", "anywhere", -- cgit v1.2.3 From 81bcc4caba6fb137c306b818ca0acfe3692c31ff Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Mon, 20 Jul 2015 14:04:18 -0700 Subject: Make endpoint peer API work on Windows --- src/core/client_config/resolvers/sockaddr_resolver.c | 4 +++- src/core/iomgr/endpoint_pair_windows.c | 4 ++-- src/core/iomgr/tcp_client_windows.c | 5 ++++- src/core/iomgr/tcp_server_windows.c | 12 ++++++++++-- src/core/iomgr/tcp_windows.c | 13 +++++++++++-- src/core/iomgr/tcp_windows.h | 2 +- src/cpp/client/create_channel.cc | 2 +- 7 files changed, 32 insertions(+), 10 deletions(-) diff --git a/src/core/client_config/resolvers/sockaddr_resolver.c b/src/core/client_config/resolvers/sockaddr_resolver.c index d42f8b1798..74584e7e2c 100644 --- a/src/core/client_config/resolvers/sockaddr_resolver.c +++ b/src/core/client_config/resolvers/sockaddr_resolver.c @@ -37,7 +37,9 @@ #include #include +#ifdef GPR_POSIX_SOCKET #include +#endif #include #include @@ -172,7 +174,7 @@ static int parse_ipv4(grpc_uri *uri, struct sockaddr_storage *addr, int *len) { memset(in, 0, sizeof(*in)); *len = sizeof(*in); in->sin_family = AF_INET; - if (inet_aton(host, &in->sin_addr) == 0) { + if (inet_pton(AF_INET, host, &in->sin_addr) == 0) { gpr_log(GPR_ERROR, "invalid ipv4 address: '%s'", host); goto done; } diff --git a/src/core/iomgr/endpoint_pair_windows.c b/src/core/iomgr/endpoint_pair_windows.c index c6790b2937..7d81470a78 100644 --- a/src/core/iomgr/endpoint_pair_windows.c +++ b/src/core/iomgr/endpoint_pair_windows.c @@ -81,8 +81,8 @@ grpc_endpoint_pair grpc_iomgr_create_endpoint_pair(const char *name, size_t read SOCKET sv[2]; grpc_endpoint_pair p; create_sockets(sv); - p.client = grpc_tcp_create(grpc_winsocket_create(sv[1], "endpoint:client")); - p.server = grpc_tcp_create(grpc_winsocket_create(sv[0], "endpoint:server")); + p.client = grpc_tcp_create(grpc_winsocket_create(sv[1], "endpoint:client"), "endpoint:server"); + p.server = grpc_tcp_create(grpc_winsocket_create(sv[0], "endpoint:server"), "endpoint:client"); return p; } diff --git a/src/core/iomgr/tcp_client_windows.c b/src/core/iomgr/tcp_client_windows.c index 16741452b9..32dd1ec11d 100644 --- a/src/core/iomgr/tcp_client_windows.c +++ b/src/core/iomgr/tcp_client_windows.c @@ -58,6 +58,7 @@ typedef struct { grpc_winsocket *socket; gpr_timespec deadline; grpc_alarm alarm; + char *addr_name; int refs; int aborted; } async_connect; @@ -67,6 +68,7 @@ static void async_connect_cleanup(async_connect *ac) { gpr_mu_unlock(&ac->mu); if (done) { gpr_mu_destroy(&ac->mu); + gpr_free(ac->addr_name); gpr_free(ac); } } @@ -107,7 +109,7 @@ static void on_connect(void *acp, int from_iocp) { gpr_log(GPR_ERROR, "on_connect error: %s", utf8_message); gpr_free(utf8_message); } else if (!aborted) { - ep = grpc_tcp_create(ac->socket); + ep = grpc_tcp_create(ac->socket, ac->addr_name); } } else { gpr_log(GPR_ERROR, "on_connect is shutting down"); @@ -213,6 +215,7 @@ void grpc_tcp_client_connect(void (*cb)(void *arg, grpc_endpoint *tcp), ac->socket = socket; gpr_mu_init(&ac->mu); ac->refs = 2; + ac->addr_name = grpc_sockaddr_to_uri(addr); ac->aborted = 0; grpc_alarm_init(&ac->alarm, deadline, on_alarm, ac, diff --git a/src/core/iomgr/tcp_server_windows.c b/src/core/iomgr/tcp_server_windows.c index 187009b2c8..8f634fcd7a 100644 --- a/src/core/iomgr/tcp_server_windows.c +++ b/src/core/iomgr/tcp_server_windows.c @@ -243,6 +243,10 @@ static void on_accept(void *arg, int from_iocp) { SOCKET sock = sp->new_socket; grpc_winsocket_callback_info *info = &sp->socket->read_info; grpc_endpoint *ep = NULL; + struct sockaddr_storage peer_name; + char *peer_name_string; + char *fd_name; + int peer_name_len = sizeof(peer_name); DWORD transfered_bytes; DWORD flags; BOOL wsa_success; @@ -277,8 +281,12 @@ static void on_accept(void *arg, int from_iocp) { } } else { if (!sp->shutting_down) { - /* TODO(ctiller): add sockaddr address to label */ - ep = grpc_tcp_create(grpc_winsocket_create(sock, "server")); + getpeername(sock, (struct sockaddr*)&peer_name, &peer_name_len); + peer_name_string = grpc_sockaddr_to_uri((struct sockaddr*)&peer_name); + gpr_asprintf(&fd_name, "tcp_server:%s", peer_name_string); + ep = grpc_tcp_create(grpc_winsocket_create(sock, fd_name), peer_name_string); + gpr_free(fd_name); + gpr_free(peer_name_string); } } diff --git a/src/core/iomgr/tcp_windows.c b/src/core/iomgr/tcp_windows.c index 1bf81a73e0..d68e6aee79 100644 --- a/src/core/iomgr/tcp_windows.c +++ b/src/core/iomgr/tcp_windows.c @@ -96,6 +96,8 @@ typedef struct grpc_tcp { to protect ourselves when requesting a shutdown. */ gpr_mu mu; int shutting_down; + + char *peer_string; } grpc_tcp; static void tcp_ref(grpc_tcp *tcp) { @@ -107,6 +109,7 @@ static void tcp_unref(grpc_tcp *tcp) { gpr_slice_buffer_destroy(&tcp->write_slices); grpc_winsocket_orphan(tcp->socket); gpr_mu_destroy(&tcp->mu); + gpr_free(tcp->peer_string); gpr_free(tcp); } } @@ -393,11 +396,16 @@ static void win_destroy(grpc_endpoint *ep) { tcp_unref(tcp); } +static char *win_get_peer(grpc_endpoint *ep) { + grpc_tcp *tcp = (grpc_tcp *)ep; + return gpr_strdup(tcp->peer_string); +} + static grpc_endpoint_vtable vtable = { - win_notify_on_read, win_write, win_add_to_pollset, win_shutdown, win_destroy + win_notify_on_read, win_write, win_add_to_pollset, win_shutdown, win_destroy, win_get_peer }; -grpc_endpoint *grpc_tcp_create(grpc_winsocket *socket) { +grpc_endpoint *grpc_tcp_create(grpc_winsocket *socket, char *peer_string) { grpc_tcp *tcp = (grpc_tcp *) gpr_malloc(sizeof(grpc_tcp)); memset(tcp, 0, sizeof(grpc_tcp)); tcp->base.vtable = &vtable; @@ -405,6 +413,7 @@ grpc_endpoint *grpc_tcp_create(grpc_winsocket *socket) { gpr_mu_init(&tcp->mu); gpr_slice_buffer_init(&tcp->write_slices); gpr_ref_init(&tcp->refcount, 1); + tcp->peer_string = gpr_strdup(peer_string); return &tcp->base; } diff --git a/src/core/iomgr/tcp_windows.h b/src/core/iomgr/tcp_windows.h index 4cbc12c53a..7e301db250 100644 --- a/src/core/iomgr/tcp_windows.h +++ b/src/core/iomgr/tcp_windows.h @@ -50,7 +50,7 @@ /* Create a tcp endpoint given a winsock handle. * Takes ownership of the handle. */ -grpc_endpoint *grpc_tcp_create(grpc_winsocket *socket); +grpc_endpoint *grpc_tcp_create(grpc_winsocket *socket, char *peer_string); int grpc_tcp_prepare_socket(SOCKET sock); diff --git a/src/cpp/client/create_channel.cc b/src/cpp/client/create_channel.cc index 510af2bb00..d85daabd20 100644 --- a/src/cpp/client/create_channel.cc +++ b/src/cpp/client/create_channel.cc @@ -45,6 +45,6 @@ std::shared_ptr CreateChannel( const ChannelArguments& args) { return creds ? creds->CreateChannel(target, args) : std::shared_ptr( - new Channel(target, grpc_lame_client_channel_create())); + new Channel(target, grpc_lame_client_channel_create(NULL))); } } // namespace grpc -- cgit v1.2.3 From ec50f281be54eb4f1017f01ef15faad5e6270454 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Mon, 20 Jul 2015 19:44:29 -0700 Subject: reading of metadata --- .../Grpc.Core/Internal/BatchContextSafeHandle.cs | 28 +++++++++++- .../Grpc.Core/Internal/MetadataArraySafeHandle.cs | 53 +++++++++++++++++++++- src/csharp/ext/grpc_csharp_ext.c | 29 ++++++++++++ 3 files changed, 108 insertions(+), 2 deletions(-) diff --git a/src/csharp/Grpc.Core/Internal/BatchContextSafeHandle.cs b/src/csharp/Grpc.Core/Internal/BatchContextSafeHandle.cs index 861cbbe4c6..bfd88a0940 100644 --- a/src/csharp/Grpc.Core/Internal/BatchContextSafeHandle.cs +++ b/src/csharp/Grpc.Core/Internal/BatchContextSafeHandle.cs @@ -46,6 +46,9 @@ namespace Grpc.Core.Internal [DllImport("grpc_csharp_ext.dll")] static extern BatchContextSafeHandle grpcsharp_batch_context_create(); + [DllImport("grpc_csharp_ext.dll")] + static extern IntPtr grpcsharp_batch_context_receive_initial_metadata(BatchContextSafeHandle ctx); + [DllImport("grpc_csharp_ext.dll")] static extern IntPtr grpcsharp_batch_context_recv_message_length(BatchContextSafeHandle ctx); @@ -58,12 +61,18 @@ namespace Grpc.Core.Internal [DllImport("grpc_csharp_ext.dll")] static extern IntPtr grpcsharp_batch_context_recv_status_on_client_details(BatchContextSafeHandle ctx); // returns const char* + [DllImport("grpc_csharp_ext.dll")] + static extern IntPtr grpcsharp_batch_context_recv_status_on_client_trailing_metadata(BatchContextSafeHandle ctx); + [DllImport("grpc_csharp_ext.dll")] static extern CallSafeHandle grpcsharp_batch_context_server_rpc_new_call(BatchContextSafeHandle ctx); [DllImport("grpc_csharp_ext.dll")] static extern IntPtr grpcsharp_batch_context_server_rpc_new_method(BatchContextSafeHandle ctx); // returns const char* + [DllImport("grpc_csharp_ext.dll")] + static extern IntPtr grpcsharp_batch_context_server_rpc_new_request_metadata(BatchContextSafeHandle ctx); + [DllImport("grpc_csharp_ext.dll")] static extern int grpcsharp_batch_context_recv_close_on_server_cancelled(BatchContextSafeHandle ctx); @@ -87,13 +96,24 @@ namespace Grpc.Core.Internal } } + public Metadata GetReceivedInitialMetadata() + { + IntPtr metadataArrayPtr = grpcsharp_batch_context_receive_initial_metadata(this); + return MetadataArraySafeHandle.ReadMetadataFromPtrUnsafe(metadataArrayPtr); + } + public Status GetReceivedStatus() { - // TODO: can the native method return string directly? string details = Marshal.PtrToStringAnsi(grpcsharp_batch_context_recv_status_on_client_details(this)); return new Status(grpcsharp_batch_context_recv_status_on_client_status(this), details); } + public Metadata GetReceivedStatusTrailingMetadata() + { + IntPtr metadataArrayPtr = grpcsharp_batch_context_recv_status_on_client_trailing_metadata(this); + return MetadataArraySafeHandle.ReadMetadataFromPtrUnsafe(metadataArrayPtr); + } + public byte[] GetReceivedMessage() { IntPtr len = grpcsharp_batch_context_recv_message_length(this); @@ -116,6 +136,12 @@ namespace Grpc.Core.Internal return Marshal.PtrToStringAnsi(grpcsharp_batch_context_server_rpc_new_method(this)); } + public Metadata GetServerRpcNewRequestMetadata() + { + IntPtr metadataArrayPtr = grpcsharp_batch_context_server_rpc_new_request_metadata(this); + return MetadataArraySafeHandle.ReadMetadataFromPtrUnsafe(metadataArrayPtr); + } + public bool GetReceivedCloseOnServerCancelled() { return grpcsharp_batch_context_recv_close_on_server_cancelled(this) != 0; diff --git a/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs b/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs index 80aa7f5603..e17eb89abc 100644 --- a/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs +++ b/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs @@ -34,6 +34,8 @@ using System.Threading.Tasks; namespace Grpc.Core.Internal { + + /// /// grpc_metadata_array from /// @@ -45,13 +47,19 @@ namespace Grpc.Core.Internal [DllImport("grpc_csharp_ext.dll", CharSet = CharSet.Ansi)] static extern void grpcsharp_metadata_array_add(MetadataArraySafeHandle array, string key, byte[] value, UIntPtr valueLength); + [DllImport("grpc_csharp_ext.dll")] + static extern UIntPtr grpcsharp_metadata_array_count(IntPtr metadataArray); + + [DllImport("grpc_csharp_ext.dll")] + static extern MetadataEntryStruct grpcsharp_metadata_array_get(IntPtr metadataArray, UIntPtr index); + [DllImport("grpc_csharp_ext.dll")] static extern void grpcsharp_metadata_array_destroy_full(IntPtr array); private MetadataArraySafeHandle() { } - + public static MetadataArraySafeHandle Create(Metadata metadata) { // TODO(jtattermusch): we might wanna check that the metadata is readonly @@ -63,10 +71,53 @@ namespace Grpc.Core.Internal return metadataArray; } + /// + /// Reads metadata from pointer to grpc_metadata_array + /// + public static Metadata ReadMetadataFromPtrUnsafe(IntPtr metadataArray) + { + if (metadataArray == IntPtr.Zero) + { + return null; + } + + ulong count = grpcsharp_metadata_array_count(metadataArray).ToUInt64(); + + var metadata = new Metadata(); + for (ulong index = 0; index < count; index ++) + { + var rawEntry = grpcsharp_metadata_array_get(metadataArray, new UIntPtr(index)); + string key = Marshal.PtrToStringAnsi(rawEntry.key); + var bytes = new byte[rawEntry.valueLength.ToUInt64()]; + Marshal.Copy(rawEntry.value, bytes, 0, bytes.Length); + metadata.Add(new Metadata.Entry(key, bytes)); + } + return metadata; + } + + internal IntPtr Handle + { + get + { + return handle; + } + } + protected override bool ReleaseHandle() { grpcsharp_metadata_array_destroy_full(handle); return true; } + + /// + /// gprc_metadata from grpc/grpc.h + /// + [StructLayout(LayoutKind.Sequential)] + private struct MetadataEntryStruct + { + public IntPtr key; // const char* + public IntPtr value; // const char* + public UIntPtr valueLength; + } } } diff --git a/src/csharp/ext/grpc_csharp_ext.c b/src/csharp/ext/grpc_csharp_ext.c index 7dd1959a5f..d8996ae7a9 100644 --- a/src/csharp/ext/grpc_csharp_ext.c +++ b/src/csharp/ext/grpc_csharp_ext.c @@ -167,6 +167,17 @@ grpcsharp_metadata_array_add(grpc_metadata_array *array, const char *key, array->count++; } +GPR_EXPORT gpr_intptr GPR_CALLTYPE +grpcsharp_metadata_array_count(grpc_metadata_array *array) { + return (gpr_intptr) array->count; +} + +GPR_EXPORT const grpc_metadata *GPR_CALLTYPE +grpcsharp_metadata_array_get(grpc_metadata_array *array, size_t index) { + GPR_ASSERT(index < array->count); + return array->metadata[index]; +} + /* Move contents of metadata array */ void grpcsharp_metadata_array_move(grpc_metadata_array *dest, grpc_metadata_array *src) { @@ -218,6 +229,12 @@ GPR_EXPORT void GPR_CALLTYPE grpcsharp_batch_context_destroy(grpcsharp_batch_con gpr_free(ctx); } +GPR_EXPORT const grpc_metadata_array *GPR_CALLTYPE +grpcsharp_batch_context_receive_initial_metadata( + const grpcsharp_batch_context *ctx) { + return &(ctx->recv_initial_metadata); +} + GPR_EXPORT gpr_intptr GPR_CALLTYPE grpcsharp_batch_context_recv_message_length( const grpcsharp_batch_context *ctx) { if (!ctx->recv_message) { @@ -260,6 +277,12 @@ grpcsharp_batch_context_recv_status_on_client_details( return ctx->recv_status_on_client.status_details; } +GPR_EXPORT const grpc_metadata_array *GPR_CALLTYPE +grpcsharp_batch_context_recv_status_on_client_trailing_metadata( + const grpcsharp_batch_context *ctx) { + return &(ctx->recv_status_on_client.trailing_metadata); +} + GPR_EXPORT grpc_call *GPR_CALLTYPE grpcsharp_batch_context_server_rpc_new_call( const grpcsharp_batch_context *ctx) { return ctx->server_rpc_new.call; @@ -271,6 +294,12 @@ grpcsharp_batch_context_server_rpc_new_method( return ctx->server_rpc_new.call_details.method; } +GPR_EXPORT const grpc_metadata_array *GPR_CALLTYPE +grpcsharp_batch_context_server_rpc_new_request_metadata( + const grpcsharp_batch_context *ctx) { + return &(ctx->server_rpc_new.request_metadata); +} + GPR_EXPORT gpr_int32 GPR_CALLTYPE grpcsharp_batch_context_recv_close_on_server_cancelled( const grpcsharp_batch_context *ctx) { -- cgit v1.2.3 From 77415b63bbad2d8a6cbcf610c07da958d34f87f0 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Mon, 20 Jul 2015 20:18:26 -0700 Subject: some cleanup and better metadata support --- src/csharp/Grpc.Core/Internal/AsyncCall.cs | 7 +- .../Grpc.Core/Internal/BatchContextSafeHandle.cs | 140 ++++++++++++++++++--- .../Grpc.Core/Internal/MetadataArraySafeHandle.cs | 2 - src/csharp/Grpc.Core/Server.cs | 7 +- src/csharp/ext/grpc_csharp_ext.c | 16 ++- 5 files changed, 142 insertions(+), 30 deletions(-) diff --git a/src/csharp/Grpc.Core/Internal/AsyncCall.cs b/src/csharp/Grpc.Core/Internal/AsyncCall.cs index 24b75d1668..660ad1c32a 100644 --- a/src/csharp/Grpc.Core/Internal/AsyncCall.cs +++ b/src/csharp/Grpc.Core/Internal/AsyncCall.cs @@ -302,7 +302,9 @@ namespace Grpc.Core.Internal return; } - var status = ctx.GetReceivedStatus(); + var fullStatus = ctx.GetReceivedStatusOnClient(); + var status = fullStatus.Status; + if (status.StatusCode != StatusCode.OK) { unaryResponseTcs.SetException(new RpcException(status)); @@ -321,7 +323,8 @@ namespace Grpc.Core.Internal /// private void HandleFinished(bool success, BatchContextSafeHandle ctx) { - var status = ctx.GetReceivedStatus(); + var fullStatus = ctx.GetReceivedStatusOnClient(); + var status = fullStatus.Status; AsyncCompletionDelegate origReadCompletionDelegate = null; lock (myLock) diff --git a/src/csharp/Grpc.Core/Internal/BatchContextSafeHandle.cs b/src/csharp/Grpc.Core/Internal/BatchContextSafeHandle.cs index bfd88a0940..6a2add54db 100644 --- a/src/csharp/Grpc.Core/Internal/BatchContextSafeHandle.cs +++ b/src/csharp/Grpc.Core/Internal/BatchContextSafeHandle.cs @@ -38,7 +38,6 @@ using Grpc.Core; namespace Grpc.Core.Internal { /// - /// Not owned version of /// grpcsharp_batch_context /// internal class BatchContextSafeHandle : SafeHandleZeroIsInvalid @@ -47,7 +46,7 @@ namespace Grpc.Core.Internal static extern BatchContextSafeHandle grpcsharp_batch_context_create(); [DllImport("grpc_csharp_ext.dll")] - static extern IntPtr grpcsharp_batch_context_receive_initial_metadata(BatchContextSafeHandle ctx); + static extern IntPtr grpcsharp_batch_context_recv_initial_metadata(BatchContextSafeHandle ctx); [DllImport("grpc_csharp_ext.dll")] static extern IntPtr grpcsharp_batch_context_recv_message_length(BatchContextSafeHandle ctx); @@ -70,6 +69,12 @@ namespace Grpc.Core.Internal [DllImport("grpc_csharp_ext.dll")] static extern IntPtr grpcsharp_batch_context_server_rpc_new_method(BatchContextSafeHandle ctx); // returns const char* + [DllImport("grpc_csharp_ext.dll")] + static extern IntPtr grpcsharp_batch_context_server_rpc_new_host(BatchContextSafeHandle ctx); // returns const char* + + [DllImport("grpc_csharp_ext.dll")] + static extern Timespec grpcsharp_batch_context_server_rpc_new_deadline(BatchContextSafeHandle ctx); + [DllImport("grpc_csharp_ext.dll")] static extern IntPtr grpcsharp_batch_context_server_rpc_new_request_metadata(BatchContextSafeHandle ctx); @@ -96,24 +101,26 @@ namespace Grpc.Core.Internal } } + // Gets data of recv_initial_metadata completion. public Metadata GetReceivedInitialMetadata() { - IntPtr metadataArrayPtr = grpcsharp_batch_context_receive_initial_metadata(this); + IntPtr metadataArrayPtr = grpcsharp_batch_context_recv_initial_metadata(this); return MetadataArraySafeHandle.ReadMetadataFromPtrUnsafe(metadataArrayPtr); } - - public Status GetReceivedStatus() + + // Gets data of recv_status_on_client completion. + public ClientSideStatus GetReceivedStatusOnClient() { string details = Marshal.PtrToStringAnsi(grpcsharp_batch_context_recv_status_on_client_details(this)); - return new Status(grpcsharp_batch_context_recv_status_on_client_status(this), details); - } + var status = new Status(grpcsharp_batch_context_recv_status_on_client_status(this), details); - public Metadata GetReceivedStatusTrailingMetadata() - { IntPtr metadataArrayPtr = grpcsharp_batch_context_recv_status_on_client_trailing_metadata(this); - return MetadataArraySafeHandle.ReadMetadataFromPtrUnsafe(metadataArrayPtr); + var metadata = MetadataArraySafeHandle.ReadMetadataFromPtrUnsafe(metadataArrayPtr); + + return new ClientSideStatus(status, metadata); } + // Gets data of recv_message completion. public byte[] GetReceivedMessage() { IntPtr len = grpcsharp_batch_context_recv_message_length(this); @@ -126,22 +133,22 @@ namespace Grpc.Core.Internal return data; } - public CallSafeHandle GetServerRpcNewCall() + // Gets data of server_rpc_new completion. + public ServerRpcNew GetServerRpcNew() { - return grpcsharp_batch_context_server_rpc_new_call(this); - } + var call = grpcsharp_batch_context_server_rpc_new_call(this); - public string GetServerRpcNewMethod() - { - return Marshal.PtrToStringAnsi(grpcsharp_batch_context_server_rpc_new_method(this)); - } + var method = Marshal.PtrToStringAnsi(grpcsharp_batch_context_server_rpc_new_method(this)); + var host = Marshal.PtrToStringAnsi(grpcsharp_batch_context_server_rpc_new_host(this)); + var deadline = grpcsharp_batch_context_server_rpc_new_deadline(this); - public Metadata GetServerRpcNewRequestMetadata() - { IntPtr metadataArrayPtr = grpcsharp_batch_context_server_rpc_new_request_metadata(this); - return MetadataArraySafeHandle.ReadMetadataFromPtrUnsafe(metadataArrayPtr); + var metadata = MetadataArraySafeHandle.ReadMetadataFromPtrUnsafe(metadataArrayPtr); + + return new ServerRpcNew(call, method, host, deadline, metadata); } + // Gets data of receive_close_on_server completion. public bool GetReceivedCloseOnServerCancelled() { return grpcsharp_batch_context_recv_close_on_server_cancelled(this) != 0; @@ -153,4 +160,97 @@ namespace Grpc.Core.Internal return true; } } + + /// + /// Status + metadata received on client side when call finishes. + /// (when receive_status_on_client operation finishes). + /// + internal struct ClientSideStatus + { + readonly Status status; + readonly Metadata trailers; + + public ClientSideStatus(Status status, Metadata trailers) + { + this.status = status; + this.trailers = trailers; + } + + public Status Status + { + get + { + return this.status; + } + } + + public Metadata Trailers + { + get + { + return this.trailers; + } + } + } + + /// + /// Details of a newly received RPC. + /// + internal struct ServerRpcNew + { + readonly CallSafeHandle call; + readonly string method; + readonly string host; + readonly Timespec deadline; + readonly Metadata requestMetadata; + + public ServerRpcNew(CallSafeHandle call, string method, string host, Timespec deadline, Metadata requestMetadata) + { + this.call = call; + this.method = method; + this.host = host; + this.deadline = deadline; + this.requestMetadata = requestMetadata; + } + + public CallSafeHandle Call + { + get + { + return this.call; + } + } + + public string Method + { + get + { + return this.method; + } + } + + public string Host + { + get + { + return this.host; + } + } + + public Timespec Deadline + { + get + { + return this.deadline; + } + } + + public Metadata RequestMetadata + { + get + { + return this.requestMetadata; + } + } + } } \ No newline at end of file diff --git a/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs b/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs index e17eb89abc..ede85fb7f2 100644 --- a/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs +++ b/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs @@ -34,8 +34,6 @@ using System.Threading.Tasks; namespace Grpc.Core.Internal { - - /// /// grpc_metadata_array from /// diff --git a/src/csharp/Grpc.Core/Server.cs b/src/csharp/Grpc.Core/Server.cs index cbf77196cf..22f0e8973a 100644 --- a/src/csharp/Grpc.Core/Server.cs +++ b/src/csharp/Grpc.Core/Server.cs @@ -242,13 +242,12 @@ namespace Grpc.Core { // TODO: handle error - CallSafeHandle call = ctx.GetServerRpcNewCall(); - string method = ctx.GetServerRpcNewMethod(); + ServerRpcNew newRpc = ctx.GetServerRpcNew(); // after server shutdown, the callback returns with null call - if (!call.IsInvalid) + if (!newRpc.Call.IsInvalid) { - Task.Run(async () => await InvokeCallHandler(call, method)); + Task.Run(async () => await InvokeCallHandler(newRpc.Call, newRpc.Method)); } AllowOneRpc(); diff --git a/src/csharp/ext/grpc_csharp_ext.c b/src/csharp/ext/grpc_csharp_ext.c index d8996ae7a9..cfd96d15f1 100644 --- a/src/csharp/ext/grpc_csharp_ext.c +++ b/src/csharp/ext/grpc_csharp_ext.c @@ -175,7 +175,7 @@ grpcsharp_metadata_array_count(grpc_metadata_array *array) { GPR_EXPORT const grpc_metadata *GPR_CALLTYPE grpcsharp_metadata_array_get(grpc_metadata_array *array, size_t index) { GPR_ASSERT(index < array->count); - return array->metadata[index]; + return &(array->metadata[index]); } /* Move contents of metadata array */ @@ -230,7 +230,7 @@ GPR_EXPORT void GPR_CALLTYPE grpcsharp_batch_context_destroy(grpcsharp_batch_con } GPR_EXPORT const grpc_metadata_array *GPR_CALLTYPE -grpcsharp_batch_context_receive_initial_metadata( +grpcsharp_batch_context_recv_initial_metadata( const grpcsharp_batch_context *ctx) { return &(ctx->recv_initial_metadata); } @@ -294,6 +294,18 @@ grpcsharp_batch_context_server_rpc_new_method( return ctx->server_rpc_new.call_details.method; } +GPR_EXPORT const char *GPR_CALLTYPE +grpcsharp_batch_context_server_rpc_new_host( + const grpcsharp_batch_context *ctx) { + return ctx->server_rpc_new.call_details.host; +} + +GPR_EXPORT gpr_timespec GPR_CALLTYPE +grpcsharp_batch_context_server_rpc_new_deadline( + const grpcsharp_batch_context *ctx) { + return ctx->server_rpc_new.call_details.deadline; +} + GPR_EXPORT const grpc_metadata_array *GPR_CALLTYPE grpcsharp_batch_context_server_rpc_new_request_metadata( const grpcsharp_batch_context *ctx) { -- cgit v1.2.3 From 7f23a75422aea6181c35857dd6f7936d1523fcd8 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Mon, 20 Jul 2015 20:28:28 -0700 Subject: fix bug --- .../Internal/MetadataArraySafeHandleTest.cs | 21 +++++++++++++++++++++ src/csharp/ext/grpc_csharp_ext.c | 4 ++-- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/csharp/Grpc.Core.Tests/Internal/MetadataArraySafeHandleTest.cs b/src/csharp/Grpc.Core.Tests/Internal/MetadataArraySafeHandleTest.cs index e03e20c4f7..46469113c5 100644 --- a/src/csharp/Grpc.Core.Tests/Internal/MetadataArraySafeHandleTest.cs +++ b/src/csharp/Grpc.Core.Tests/Internal/MetadataArraySafeHandleTest.cs @@ -59,5 +59,26 @@ namespace Grpc.Core.Internal.Tests var nativeMetadata = MetadataArraySafeHandle.Create(metadata); nativeMetadata.Dispose(); } + + [Test] + public void ReadMetadataFromPtrUnsafe() + { + var metadata = new Metadata + { + new Metadata.Entry("host", "somehost"), + new Metadata.Entry("header2", "header value"), + }; + var nativeMetadata = MetadataArraySafeHandle.Create(metadata); + + var copy = MetadataArraySafeHandle.ReadMetadataFromPtrUnsafe(nativeMetadata.Handle); + Assert.AreEqual(2, copy.Count); + + Assert.AreEqual("host", copy[0].Key); + Assert.AreEqual("somehost", copy[0].Value); + Assert.AreEqual("header2", copy[1].Key); + Assert.AreEqual("header value", copy[1].Value); + + nativeMetadata.Dispose(); + } } } diff --git a/src/csharp/ext/grpc_csharp_ext.c b/src/csharp/ext/grpc_csharp_ext.c index cfd96d15f1..6856d89ff1 100644 --- a/src/csharp/ext/grpc_csharp_ext.c +++ b/src/csharp/ext/grpc_csharp_ext.c @@ -172,10 +172,10 @@ grpcsharp_metadata_array_count(grpc_metadata_array *array) { return (gpr_intptr) array->count; } -GPR_EXPORT const grpc_metadata *GPR_CALLTYPE +GPR_EXPORT grpc_metadata GPR_CALLTYPE grpcsharp_metadata_array_get(grpc_metadata_array *array, size_t index) { GPR_ASSERT(index < array->count); - return &(array->metadata[index]); + return array->metadata[index]; } /* Move contents of metadata array */ -- cgit v1.2.3 From 5bbd8186cc1d4b22a8021bdacb5d243c5c621ef3 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Mon, 20 Jul 2015 20:48:40 -0700 Subject: refactor server call handlers --- src/csharp/Grpc.Core/Internal/ServerCallHandler.cs | 24 ++++++++++++---------- src/csharp/Grpc.Core/Server.cs | 23 +++++++++++---------- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs b/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs index 594e46b159..880005ea40 100644 --- a/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs +++ b/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs @@ -42,7 +42,7 @@ namespace Grpc.Core.Internal { internal interface IServerCallHandler { - Task HandleCall(string methodName, CallSafeHandle call, GrpcEnvironment environment); + Task HandleCall(ServerRpcNew newRpc, GrpcEnvironment environment); } internal class UnaryServerCallHandler : IServerCallHandler @@ -58,14 +58,14 @@ namespace Grpc.Core.Internal this.handler = handler; } - public async Task HandleCall(string methodName, CallSafeHandle call, GrpcEnvironment environment) + public async Task HandleCall(ServerRpcNew newRpc, GrpcEnvironment environment) { var asyncCall = new AsyncCallServer( method.ResponseMarshaller.Serializer, method.RequestMarshaller.Deserializer, environment); - asyncCall.Initialize(call); + asyncCall.Initialize(newRpc.Call); var finishedTask = asyncCall.ServerSideCallAsync(); var requestStream = new ServerRequestStream(asyncCall); var responseStream = new ServerResponseStream(asyncCall); @@ -111,14 +111,14 @@ namespace Grpc.Core.Internal this.handler = handler; } - public async Task HandleCall(string methodName, CallSafeHandle call, GrpcEnvironment environment) + public async Task HandleCall(ServerRpcNew newRpc, GrpcEnvironment environment) { var asyncCall = new AsyncCallServer( method.ResponseMarshaller.Serializer, method.RequestMarshaller.Deserializer, environment); - asyncCall.Initialize(call); + asyncCall.Initialize(newRpc.Call); var finishedTask = asyncCall.ServerSideCallAsync(); var requestStream = new ServerRequestStream(asyncCall); var responseStream = new ServerResponseStream(asyncCall); @@ -165,14 +165,14 @@ namespace Grpc.Core.Internal this.handler = handler; } - public async Task HandleCall(string methodName, CallSafeHandle call, GrpcEnvironment environment) + public async Task HandleCall(ServerRpcNew newRpc, GrpcEnvironment environment) { var asyncCall = new AsyncCallServer( method.ResponseMarshaller.Serializer, method.RequestMarshaller.Deserializer, environment); - asyncCall.Initialize(call); + asyncCall.Initialize(newRpc.Call); var finishedTask = asyncCall.ServerSideCallAsync(); var requestStream = new ServerRequestStream(asyncCall); var responseStream = new ServerResponseStream(asyncCall); @@ -222,14 +222,14 @@ namespace Grpc.Core.Internal this.handler = handler; } - public async Task HandleCall(string methodName, CallSafeHandle call, GrpcEnvironment environment) + public async Task HandleCall(ServerRpcNew newRpc, GrpcEnvironment environment) { var asyncCall = new AsyncCallServer( method.ResponseMarshaller.Serializer, method.RequestMarshaller.Deserializer, environment); - asyncCall.Initialize(call); + asyncCall.Initialize(newRpc.Call); var finishedTask = asyncCall.ServerSideCallAsync(); var requestStream = new ServerRequestStream(asyncCall); var responseStream = new ServerResponseStream(asyncCall); @@ -259,13 +259,15 @@ namespace Grpc.Core.Internal internal class NoSuchMethodCallHandler : IServerCallHandler { - public async Task HandleCall(string methodName, CallSafeHandle call, GrpcEnvironment environment) + public static readonly NoSuchMethodCallHandler Instance = new NoSuchMethodCallHandler(); + + public async Task HandleCall(ServerRpcNew newRpc, GrpcEnvironment environment) { // We don't care about the payload type here. var asyncCall = new AsyncCallServer( (payload) => payload, (payload) => payload, environment); - asyncCall.Initialize(call); + asyncCall.Initialize(newRpc.Call); var finishedTask = asyncCall.ServerSideCallAsync(); var requestStream = new ServerRequestStream(asyncCall); var responseStream = new ServerResponseStream(asyncCall); diff --git a/src/csharp/Grpc.Core/Server.cs b/src/csharp/Grpc.Core/Server.cs index 22f0e8973a..7f9ec41486 100644 --- a/src/csharp/Grpc.Core/Server.cs +++ b/src/csharp/Grpc.Core/Server.cs @@ -218,16 +218,16 @@ namespace Grpc.Core /// /// Selects corresponding handler for given call and handles the call. /// - private async Task InvokeCallHandler(CallSafeHandle call, string method) + private async Task HandleCallAsync(ServerRpcNew newRpc) { try { IServerCallHandler callHandler; - if (!callHandlers.TryGetValue(method, out callHandler)) + if (!callHandlers.TryGetValue(newRpc.Method, out callHandler)) { - callHandler = new NoSuchMethodCallHandler(); + callHandler = NoSuchMethodCallHandler.Instance; } - await callHandler.HandleCall(method, call, environment); + await callHandler.HandleCall(newRpc, environment); } catch (Exception e) { @@ -240,14 +240,15 @@ namespace Grpc.Core /// private void HandleNewServerRpc(bool success, BatchContextSafeHandle ctx) { - // TODO: handle error - - ServerRpcNew newRpc = ctx.GetServerRpcNew(); - - // after server shutdown, the callback returns with null call - if (!newRpc.Call.IsInvalid) + if (success) { - Task.Run(async () => await InvokeCallHandler(newRpc.Call, newRpc.Method)); + ServerRpcNew newRpc = ctx.GetServerRpcNew(); + + // after server shutdown, the callback returns with null call + if (!newRpc.Call.IsInvalid) + { + Task.Run(async () => await HandleCallAsync(newRpc)); + } } AllowOneRpc(); -- cgit v1.2.3 From 998eb9bcaf8990a9c7ec2709550fb70c72c430dc Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Mon, 20 Jul 2015 22:12:53 -0700 Subject: populate server context --- src/csharp/Grpc.Core.Tests/ClientServerTest.cs | 20 +++++ src/csharp/Grpc.Core.Tests/TimespecTest.cs | 13 ++++ src/csharp/Grpc.Core/Internal/ServerCallHandler.cs | 28 +++++-- src/csharp/Grpc.Core/Internal/Timespec.cs | 14 ++++ src/csharp/Grpc.Core/Metadata.cs | 6 ++ src/csharp/Grpc.Core/ServerCallContext.cs | 91 ++++++++++++++++++++-- 6 files changed, 159 insertions(+), 13 deletions(-) diff --git a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs index e797dd82f2..05e33f1589 100644 --- a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs +++ b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs @@ -182,6 +182,19 @@ namespace Grpc.Core.Tests }).Wait(); } + [Test] + public void AsyncUnaryCall_EchoMetadata() + { + var metadata = new Metadata + { + new Metadata.Entry("asciiHeader", "abcdefg"), + new Metadata.Entry("binaryHeader-bin", new byte[] { 1, 2, 3, 0, 0xff } ), + }; + var call = new Call(ServiceName, EchoMethod, channel, metadata); + var result = Calls.AsyncUnaryCall(call, "ABC", CancellationToken.None).Result; + Assert.AreEqual("ABC", result); + } + [Test] public void UnaryCall_DisposedChannel() { @@ -216,10 +229,17 @@ namespace Grpc.Core.Tests private static async Task EchoHandler(ServerCallContext context, string request) { + foreach (Metadata.Entry metadataEntry in context.RequestHeaders) + { + Console.WriteLine("Echoing header " + metadataEntry.Key + " as trailer"); + context.ResponseTrailers.Add(metadataEntry); + } + if (request == "THROW") { throw new Exception("This was thrown on purpose by a test"); } + return request; } diff --git a/src/csharp/Grpc.Core.Tests/TimespecTest.cs b/src/csharp/Grpc.Core.Tests/TimespecTest.cs index 5831121add..a34b407a01 100644 --- a/src/csharp/Grpc.Core.Tests/TimespecTest.cs +++ b/src/csharp/Grpc.Core.Tests/TimespecTest.cs @@ -58,6 +58,19 @@ namespace Grpc.Core.Internal.Tests Assert.AreEqual(Timespec.NativeSize, Marshal.SizeOf(typeof(Timespec))); } + [Test] + public void ToDateTime() + { + Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc), + new Timespec(IntPtr.Zero, 0).ToDateTime()); + + Assert.AreEqual(new DateTime(1970, 1, 1, 0, 0, 10, DateTimeKind.Utc).AddTicks(50), + new Timespec(new IntPtr(10), 5000).ToDateTime()); + + Assert.AreEqual(new DateTime(2015, 7, 21, 4, 21, 48, DateTimeKind.Utc), + new Timespec(new IntPtr(1437452508), 0).ToDateTime()); + } + [Test] public void Add() { diff --git a/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs b/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs index 880005ea40..f3d3c629bc 100644 --- a/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs +++ b/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs @@ -34,6 +34,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using System.Threading.Tasks; using Grpc.Core.Internal; using Grpc.Core.Utils; @@ -70,15 +71,16 @@ namespace Grpc.Core.Internal var requestStream = new ServerRequestStream(asyncCall); var responseStream = new ServerResponseStream(asyncCall); - Status status = Status.DefaultSuccess; + Status status; try { Preconditions.CheckArgument(await requestStream.MoveNext()); var request = requestStream.Current; // TODO(jtattermusch): we need to read the full stream so that native callhandle gets deallocated. Preconditions.CheckArgument(!await requestStream.MoveNext()); - var context = new ServerCallContext(); // TODO(jtattermusch): initialize the context + var context = HandlerUtils.NewContext(newRpc); var result = await handler(context, request); + status = context.Status; await responseStream.WriteAsync(result); } catch (Exception e) @@ -123,7 +125,7 @@ namespace Grpc.Core.Internal var requestStream = new ServerRequestStream(asyncCall); var responseStream = new ServerResponseStream(asyncCall); - Status status = Status.DefaultSuccess; + Status status; try { Preconditions.CheckArgument(await requestStream.MoveNext()); @@ -131,8 +133,9 @@ namespace Grpc.Core.Internal // TODO(jtattermusch): we need to read the full stream so that native callhandle gets deallocated. Preconditions.CheckArgument(!await requestStream.MoveNext()); - var context = new ServerCallContext(); // TODO(jtattermusch): initialize the context + var context = HandlerUtils.NewContext(newRpc); await handler(context, request, responseStream); + status = context.Status; } catch (Exception e) { @@ -176,12 +179,13 @@ namespace Grpc.Core.Internal var finishedTask = asyncCall.ServerSideCallAsync(); var requestStream = new ServerRequestStream(asyncCall); var responseStream = new ServerResponseStream(asyncCall); - var context = new ServerCallContext(); // TODO(jtattermusch): initialize the context + var context = HandlerUtils.NewContext(newRpc); - Status status = Status.DefaultSuccess; + Status status; try { var result = await handler(context, requestStream); + status = context.Status; try { await responseStream.WriteAsync(result); @@ -233,12 +237,13 @@ namespace Grpc.Core.Internal var finishedTask = asyncCall.ServerSideCallAsync(); var requestStream = new ServerRequestStream(asyncCall); var responseStream = new ServerResponseStream(asyncCall); - var context = new ServerCallContext(); // TODO(jtattermusch): initialize the context + var context = HandlerUtils.NewContext(newRpc); - Status status = Status.DefaultSuccess; + Status status; try { await handler(context, requestStream, responseStream); + status = context.Status; } catch (Exception e) { @@ -284,5 +289,12 @@ namespace Grpc.Core.Internal // TODO(jtattermusch): what is the right status code here? return new Status(StatusCode.Unknown, "Exception was thrown by handler."); } + + public static ServerCallContext NewContext(ServerRpcNew newRpc) + { + return new ServerCallContext( + newRpc.Method, newRpc.Host, newRpc.Deadline.ToDateTime(), + newRpc.RequestMetadata, CancellationToken.None); + } } } diff --git a/src/csharp/Grpc.Core/Internal/Timespec.cs b/src/csharp/Grpc.Core/Internal/Timespec.cs index de783f5a4b..da2819f14d 100644 --- a/src/csharp/Grpc.Core/Internal/Timespec.cs +++ b/src/csharp/Grpc.Core/Internal/Timespec.cs @@ -43,6 +43,8 @@ namespace Grpc.Core.Internal const int NanosPerSecond = 1000 * 1000 * 1000; const int NanosPerTick = 100; + static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); + [DllImport("grpc_csharp_ext.dll")] static extern Timespec gprsharp_now(); @@ -52,6 +54,13 @@ namespace Grpc.Core.Internal [DllImport("grpc_csharp_ext.dll")] static extern int gprsharp_sizeof_timespec(); + public Timespec(IntPtr tv_sec, int tv_nsec) + { + this.tv_sec = tv_sec; + this.tv_nsec = tv_nsec; + this.clock_type = GPRClockType.Realtime; + } + // NOTE: on linux 64bit sizeof(gpr_timespec) = 16, on windows 32bit sizeof(gpr_timespec) = 8 // so IntPtr seems to have the right size to work on both. public System.IntPtr tv_sec; @@ -76,6 +85,11 @@ namespace Grpc.Core.Internal return gprsharp_now(); } } + + public DateTime ToDateTime() + { + return UnixEpoch.AddTicks(tv_sec.ToInt64() * (NanosPerSecond / NanosPerTick) + tv_nsec / NanosPerTick); + } internal static int NativeSize { diff --git a/src/csharp/Grpc.Core/Metadata.cs b/src/csharp/Grpc.Core/Metadata.cs index 4552d39d88..0c6fcbc0f8 100644 --- a/src/csharp/Grpc.Core/Metadata.cs +++ b/src/csharp/Grpc.Core/Metadata.cs @@ -220,6 +220,12 @@ namespace Grpc.Core return value; } } + + public override string ToString() + { + return string.Format("[Entry: key={0}, value={1}]", Key, Value); + } + } } } diff --git a/src/csharp/Grpc.Core/ServerCallContext.cs b/src/csharp/Grpc.Core/ServerCallContext.cs index bc9a499c51..4fec3dc676 100644 --- a/src/csharp/Grpc.Core/ServerCallContext.cs +++ b/src/csharp/Grpc.Core/ServerCallContext.cs @@ -33,6 +33,7 @@ using System; using System.Runtime.CompilerServices; +using System.Threading; using System.Threading.Tasks; namespace Grpc.Core @@ -42,14 +43,94 @@ namespace Grpc.Core /// public sealed class ServerCallContext { - // TODO(jtattermusch): add cancellationToken + // TODO(jtattermusch): expose method to send initial metadata back to client - // TODO(jtattermusch): add deadline info + // TODO(jtattermusch): allow setting status and trailing metadata to send after handler completes. - // TODO(jtattermusch): expose initial metadata sent by client for reading + private readonly string method; + private readonly string host; + private readonly DateTime deadline; + private readonly Metadata requestHeaders; + private readonly CancellationToken cancellationToken; - // TODO(jtattermusch): expose method to send initial metadata back to client + private Status status = Status.DefaultSuccess; + private readonly Metadata responseTrailers = new Metadata(); - // TODO(jtattermusch): allow setting status and trailing metadata to send after handler completes. + public ServerCallContext(string method, string host, DateTime deadline, Metadata requestHeaders, CancellationToken cancellationToken) + { + this.method = method; + this.host = host; + this.deadline = deadline; + this.requestHeaders = requestHeaders; + this.cancellationToken = cancellationToken; + } + + /// Name of method called in this RPC. + public string Method + { + get + { + return this.method; + } + } + + /// Name of host called in this RPC. + public string Host + { + get + { + return this.host; + } + } + + /// Deadline for this RPC. + public DateTime Deadline + { + get + { + return this.deadline; + } + } + + /// Initial metadata sent by client. + public Metadata RequestHeaders + { + get + { + return this.requestHeaders; + } + } + + // TODO(jtattermusch): support signalling cancellation. + /// Cancellation token signals when call is cancelled. + public CancellationToken CancellationToken + { + get + { + return this.cancellationToken; + } + } + + /// Trailers to send back to client after RPC finishes. + public Metadata ResponseTrailers + { + get + { + return this.responseTrailers; + } + } + + /// Status to send back to client after RPC finishes. + public Status Status + { + get + { + return this.status; + } + set + { + status = value; + } + } } } -- cgit v1.2.3 From a0bb06511e139e413f2d7dfde11644f81c29a5c1 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Mon, 20 Jul 2015 22:34:19 -0700 Subject: allow sending trailers from server handler --- src/csharp/Grpc.Core/Internal/AsyncCallServer.cs | 7 +++++-- src/csharp/Grpc.Core/Internal/CallSafeHandle.cs | 6 +++--- src/csharp/Grpc.Core/Internal/ServerCallHandler.cs | 20 ++++++++++---------- .../Grpc.Core/Internal/ServerResponseStream.cs | 4 ++-- src/csharp/ext/grpc_csharp_ext.c | 11 ++++++++--- 5 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/csharp/Grpc.Core/Internal/AsyncCallServer.cs b/src/csharp/Grpc.Core/Internal/AsyncCallServer.cs index 309067ea9d..f809f4a84c 100644 --- a/src/csharp/Grpc.Core/Internal/AsyncCallServer.cs +++ b/src/csharp/Grpc.Core/Internal/AsyncCallServer.cs @@ -101,14 +101,17 @@ namespace Grpc.Core.Internal /// Only one pending send action is allowed at any given time. /// completionDelegate is called when the operation finishes. /// - public void StartSendStatusFromServer(Status status, AsyncCompletionDelegate completionDelegate) + public void StartSendStatusFromServer(Status status, Metadata trailers, AsyncCompletionDelegate completionDelegate) { lock (myLock) { Preconditions.CheckNotNull(completionDelegate, "Completion delegate cannot be null"); CheckSendingAllowed(); - call.StartSendStatusFromServer(status, HandleHalfclosed); + using (var metadataArray = MetadataArraySafeHandle.Create(trailers)) + { + call.StartSendStatusFromServer(status, HandleHalfclosed, metadataArray); + } halfcloseRequested = true; readingDone = true; sendCompletionDelegate = completionDelegate; diff --git a/src/csharp/Grpc.Core/Internal/CallSafeHandle.cs b/src/csharp/Grpc.Core/Internal/CallSafeHandle.cs index 3b246ac01b..19dbb83f24 100644 --- a/src/csharp/Grpc.Core/Internal/CallSafeHandle.cs +++ b/src/csharp/Grpc.Core/Internal/CallSafeHandle.cs @@ -81,7 +81,7 @@ namespace Grpc.Core.Internal [DllImport("grpc_csharp_ext.dll")] static extern GRPCCallError grpcsharp_call_send_status_from_server(CallSafeHandle call, - BatchContextSafeHandle ctx, StatusCode statusCode, string statusMessage); + BatchContextSafeHandle ctx, StatusCode statusCode, string statusMessage, MetadataArraySafeHandle metadataArray); [DllImport("grpc_csharp_ext.dll")] static extern GRPCCallError grpcsharp_call_recv_message(CallSafeHandle call, @@ -159,11 +159,11 @@ namespace Grpc.Core.Internal grpcsharp_call_send_close_from_client(this, ctx).CheckOk(); } - public void StartSendStatusFromServer(Status status, BatchCompletionDelegate callback) + public void StartSendStatusFromServer(Status status, BatchCompletionDelegate callback, MetadataArraySafeHandle metadataArray) { var ctx = BatchContextSafeHandle.Create(); completionRegistry.RegisterBatchCompletion(ctx, callback); - grpcsharp_call_send_status_from_server(this, ctx, status.StatusCode, status.Detail).CheckOk(); + grpcsharp_call_send_status_from_server(this, ctx, status.StatusCode, status.Detail, metadataArray).CheckOk(); } public void StartReceiveMessage(BatchCompletionDelegate callback) diff --git a/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs b/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs index f3d3c629bc..ddd2187b3e 100644 --- a/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs +++ b/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs @@ -72,13 +72,13 @@ namespace Grpc.Core.Internal var responseStream = new ServerResponseStream(asyncCall); Status status; + var context = HandlerUtils.NewContext(newRpc); try { Preconditions.CheckArgument(await requestStream.MoveNext()); var request = requestStream.Current; // TODO(jtattermusch): we need to read the full stream so that native callhandle gets deallocated. Preconditions.CheckArgument(!await requestStream.MoveNext()); - var context = HandlerUtils.NewContext(newRpc); var result = await handler(context, request); status = context.Status; await responseStream.WriteAsync(result); @@ -90,7 +90,7 @@ namespace Grpc.Core.Internal } try { - await responseStream.WriteStatusAsync(status); + await responseStream.WriteStatusAsync(status, context.ResponseTrailers); } catch (OperationCanceledException) { @@ -126,14 +126,13 @@ namespace Grpc.Core.Internal var responseStream = new ServerResponseStream(asyncCall); Status status; + var context = HandlerUtils.NewContext(newRpc); try { Preconditions.CheckArgument(await requestStream.MoveNext()); var request = requestStream.Current; // TODO(jtattermusch): we need to read the full stream so that native callhandle gets deallocated. Preconditions.CheckArgument(!await requestStream.MoveNext()); - - var context = HandlerUtils.NewContext(newRpc); await handler(context, request, responseStream); status = context.Status; } @@ -145,7 +144,7 @@ namespace Grpc.Core.Internal try { - await responseStream.WriteStatusAsync(status); + await responseStream.WriteStatusAsync(status, context.ResponseTrailers); } catch (OperationCanceledException) { @@ -179,9 +178,10 @@ namespace Grpc.Core.Internal var finishedTask = asyncCall.ServerSideCallAsync(); var requestStream = new ServerRequestStream(asyncCall); var responseStream = new ServerResponseStream(asyncCall); - var context = HandlerUtils.NewContext(newRpc); + Status status; + var context = HandlerUtils.NewContext(newRpc); try { var result = await handler(context, requestStream); @@ -203,7 +203,7 @@ namespace Grpc.Core.Internal try { - await responseStream.WriteStatusAsync(status); + await responseStream.WriteStatusAsync(status, context.ResponseTrailers); } catch (OperationCanceledException) { @@ -237,9 +237,9 @@ namespace Grpc.Core.Internal var finishedTask = asyncCall.ServerSideCallAsync(); var requestStream = new ServerRequestStream(asyncCall); var responseStream = new ServerResponseStream(asyncCall); - var context = HandlerUtils.NewContext(newRpc); Status status; + var context = HandlerUtils.NewContext(newRpc); try { await handler(context, requestStream, responseStream); @@ -252,7 +252,7 @@ namespace Grpc.Core.Internal } try { - await responseStream.WriteStatusAsync(status); + await responseStream.WriteStatusAsync(status, context.ResponseTrailers); } catch (OperationCanceledException) { @@ -277,7 +277,7 @@ namespace Grpc.Core.Internal var requestStream = new ServerRequestStream(asyncCall); var responseStream = new ServerResponseStream(asyncCall); - await responseStream.WriteStatusAsync(new Status(StatusCode.Unimplemented, "No such method.")); + await responseStream.WriteStatusAsync(new Status(StatusCode.Unimplemented, "No such method."), Metadata.Empty); await finishedTask; } } diff --git a/src/csharp/Grpc.Core/Internal/ServerResponseStream.cs b/src/csharp/Grpc.Core/Internal/ServerResponseStream.cs index a2d77dd5b7..756dcee87f 100644 --- a/src/csharp/Grpc.Core/Internal/ServerResponseStream.cs +++ b/src/csharp/Grpc.Core/Internal/ServerResponseStream.cs @@ -56,10 +56,10 @@ namespace Grpc.Core.Internal return taskSource.Task; } - public Task WriteStatusAsync(Status status) + public Task WriteStatusAsync(Status status, Metadata trailers) { var taskSource = new AsyncCompletionTaskSource(); - call.StartSendStatusFromServer(status, taskSource.CompletionDelegate); + call.StartSendStatusFromServer(status, trailers, taskSource.CompletionDelegate); return taskSource.Task; } } diff --git a/src/csharp/ext/grpc_csharp_ext.c b/src/csharp/ext/grpc_csharp_ext.c index 6856d89ff1..bd0a259593 100644 --- a/src/csharp/ext/grpc_csharp_ext.c +++ b/src/csharp/ext/grpc_csharp_ext.c @@ -630,15 +630,20 @@ GPR_EXPORT grpc_call_error GPR_CALLTYPE grpcsharp_call_send_status_from_server(grpc_call *call, grpcsharp_batch_context *ctx, grpc_status_code status_code, - const char *status_details) { + const char *status_details, + grpc_metadata_array *trailing_metadata) { /* TODO: don't use magic number */ grpc_op ops[1]; ops[0].op = GRPC_OP_SEND_STATUS_FROM_SERVER; ops[0].data.send_status_from_server.status = status_code; ops[0].data.send_status_from_server.status_details = gpr_strdup(status_details); - ops[0].data.send_status_from_server.trailing_metadata = NULL; - ops[0].data.send_status_from_server.trailing_metadata_count = 0; + grpcsharp_metadata_array_move(&(ctx->send_status_from_server.trailing_metadata), + trailing_metadata); + ops[0].data.send_status_from_server.trailing_metadata_count = + ctx->send_status_from_server.trailing_metadata.count; + ops[0].data.send_status_from_server.trailing_metadata = + ctx->send_status_from_server.trailing_metadata.metadata; ops[0].flags = 0; return grpc_call_start_batch(call, ops, sizeof(ops) / sizeof(ops[0]), ctx); -- cgit v1.2.3 From 8271f5d093b82acabf35979537e0c05a69a2d460 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Mon, 20 Jul 2015 22:48:15 -0700 Subject: propagate statuscode from server handler --- src/csharp/Grpc.Core/Internal/ServerCallHandler.cs | 8 +++++++- src/csharp/Grpc.HealthCheck.Tests/HealthClientServerTest.cs | 4 +--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs b/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs index ddd2187b3e..03062d1434 100644 --- a/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs +++ b/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs @@ -274,7 +274,6 @@ namespace Grpc.Core.Internal asyncCall.Initialize(newRpc.Call); var finishedTask = asyncCall.ServerSideCallAsync(); - var requestStream = new ServerRequestStream(asyncCall); var responseStream = new ServerResponseStream(asyncCall); await responseStream.WriteStatusAsync(new Status(StatusCode.Unimplemented, "No such method."), Metadata.Empty); @@ -286,6 +285,13 @@ namespace Grpc.Core.Internal { public static Status StatusFromException(Exception e) { + var rpcException = e as RpcException; + if (rpcException != null) + { + // use the status thrown by handler. + return rpcException.Status; + } + // 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.HealthCheck.Tests/HealthClientServerTest.cs b/src/csharp/Grpc.HealthCheck.Tests/HealthClientServerTest.cs index 73ff0e74b5..bc14a0a62f 100644 --- a/src/csharp/Grpc.HealthCheck.Tests/HealthClientServerTest.cs +++ b/src/csharp/Grpc.HealthCheck.Tests/HealthClientServerTest.cs @@ -87,9 +87,7 @@ namespace Grpc.HealthCheck.Tests [Test] public void ServiceDoesntExist() { - // TODO(jtattermusch): currently, this returns wrong status code, because we don't enable sending arbitrary status code from - // server handlers yet. - Assert.Throws(typeof(RpcException), () => client.Check(HealthCheckRequest.CreateBuilder().SetHost("").SetService("nonexistent.service").Build())); + Assert.Throws(Is.TypeOf(typeof(RpcException)).And.Property("Status").Property("StatusCode").EqualTo(StatusCode.NotFound), () => client.Check(HealthCheckRequest.CreateBuilder().SetHost("").SetService("nonexistent.service").Build())); } // TODO(jtattermusch): add test with timeout once timeouts are supported -- cgit v1.2.3 From 1cf8d429e3aad6ca7da41de5d62ab2498be5bd10 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Tue, 21 Jul 2015 10:37:55 -0700 Subject: added some tests --- src/csharp/Grpc.Core.Tests/ClientServerTest.cs | 43 ++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs index 05e33f1589..cd8af3dbc0 100644 --- a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs +++ b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs @@ -118,6 +118,36 @@ namespace Grpc.Core.Tests } } + [Test] + public void UnaryCall_ServerHandlerThrowsRpcException() + { + var call = new Call(ServiceName, EchoMethod, channel, Metadata.Empty); + try + { + Calls.BlockingUnaryCall(call, "THROW_UNAUTHENTICATED", CancellationToken.None); + Assert.Fail(); + } + catch (RpcException e) + { + Assert.AreEqual(StatusCode.Unauthenticated, e.Status.StatusCode); + } + } + + [Test] + public void UnaryCall_ServerHandlerSetsStatus() + { + var call = new Call(ServiceName, EchoMethod, channel, Metadata.Empty); + try + { + Calls.BlockingUnaryCall(call, "SET_UNAUTHENTICATED", CancellationToken.None); + Assert.Fail(); + } + catch (RpcException e) + { + Assert.AreEqual(StatusCode.Unauthenticated, e.Status.StatusCode); + } + } + [Test] public void AsyncUnaryCall() { @@ -193,6 +223,9 @@ namespace Grpc.Core.Tests var call = new Call(ServiceName, EchoMethod, channel, metadata); var result = Calls.AsyncUnaryCall(call, "ABC", CancellationToken.None).Result; Assert.AreEqual("ABC", result); + + // TODO: implement assertion... + Assert.Fail(); } [Test] @@ -240,6 +273,16 @@ namespace Grpc.Core.Tests throw new Exception("This was thrown on purpose by a test"); } + if (request == "THROW_UNAUTHENTICATED") + { + throw new RpcException(new Status(StatusCode.Unauthenticated, "")); + } + + if (request == "SET_UNAUTHENTICATED") + { + context.Status = new Status(StatusCode.Unauthenticated, ""); + } + return request; } -- cgit v1.2.3 From c4af2249705842f66b665c717e2ccbc83b9b1ffb Mon Sep 17 00:00:00 2001 From: Vijay Pai Date: Tue, 21 Jul 2015 18:43:28 +0000 Subject: Remove one range-based for and do appropriate static casts on nullptr --- test/cpp/client/credentials_test.cc | 2 +- test/cpp/common/secure_auth_context_test.cc | 16 ++++++++-------- test/cpp/end2end/end2end_test.cc | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/test/cpp/client/credentials_test.cc b/test/cpp/client/credentials_test.cc index ee94f455a4..bbf7705f0a 100644 --- a/test/cpp/client/credentials_test.cc +++ b/test/cpp/client/credentials_test.cc @@ -47,7 +47,7 @@ class CredentialsTest : public ::testing::Test { TEST_F(CredentialsTest, InvalidServiceAccountCreds) { std::shared_ptr bad1 = ServiceAccountCredentials("", "", 1); - EXPECT_EQ(nullptr, bad1.get()); + EXPECT_EQ(static_cast(nullptr), bad1.get()); } } // namespace testing diff --git a/test/cpp/common/secure_auth_context_test.cc b/test/cpp/common/secure_auth_context_test.cc index f18a04178e..fc8aa8f681 100644 --- a/test/cpp/common/secure_auth_context_test.cc +++ b/test/cpp/common/secure_auth_context_test.cc @@ -92,21 +92,21 @@ TEST_F(SecureAuthContextTest, Iterators) { EXPECT_EQ("bar", p2.second); ++iter; EXPECT_EQ(context.end(), iter); - // Range-based for loop test. + int i = 0; - for (auto p : context) { + for (auto p = context.begin(); p != context.end(); p++) { switch (i++) { case 0: - EXPECT_EQ("name", p.first); - EXPECT_EQ("chapi", p.second); + EXPECT_EQ("name", (*p).first); + EXPECT_EQ("chapi", (*p).second); break; case 1: - EXPECT_EQ("name", p.first); - EXPECT_EQ("chapo", p.second); + EXPECT_EQ("name", (*p).first); + EXPECT_EQ("chapo", (*p).second); break; case 2: - EXPECT_EQ("foo", p.first); - EXPECT_EQ("bar", p.second); + EXPECT_EQ("foo", (*p).first); + EXPECT_EQ("bar", (*p).second); break; default: EXPECT_TRUE(0); diff --git a/test/cpp/end2end/end2end_test.cc b/test/cpp/end2end/end2end_test.cc index 8b4424c735..ca0324e161 100644 --- a/test/cpp/end2end/end2end_test.cc +++ b/test/cpp/end2end/end2end_test.cc @@ -508,7 +508,7 @@ TEST_F(End2endTest, DiffPackageServices) { // rpc and stream should fail on bad credentials. TEST_F(End2endTest, BadCredentials) { std::shared_ptr bad_creds = ServiceAccountCredentials("", "", 1); - EXPECT_EQ(nullptr, bad_creds.get()); + EXPECT_EQ(static_cast(nullptr), bad_creds.get()); std::shared_ptr channel = CreateChannel(server_address_.str(), bad_creds, ChannelArguments()); std::unique_ptr stub( -- cgit v1.2.3 From ed4b7a7c29843fe2d87e9cbbc21bf482d3c4f342 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Tue, 21 Jul 2015 11:46:43 -0700 Subject: modify client call interface to allow reading status and trailers --- src/csharp/Grpc.Core/AsyncClientStreamingCall.cs | 8 +- src/csharp/Grpc.Core/AsyncDuplexStreamingCall.cs | 25 +++++- src/csharp/Grpc.Core/AsyncServerStreamingCall.cs | 24 ++++- src/csharp/Grpc.Core/AsyncUnaryCall.cs | 106 +++++++++++++++++++++++ src/csharp/Grpc.Core/Calls.cs | 6 +- src/csharp/Grpc.Core/Grpc.Core.csproj | 8 +- src/csharp/Grpc.Core/Internal/AsyncCall.cs | 40 +++++++-- 7 files changed, 200 insertions(+), 17 deletions(-) create mode 100644 src/csharp/Grpc.Core/AsyncUnaryCall.cs diff --git a/src/csharp/Grpc.Core/AsyncClientStreamingCall.cs b/src/csharp/Grpc.Core/AsyncClientStreamingCall.cs index d66b0d4974..98ebeea318 100644 --- a/src/csharp/Grpc.Core/AsyncClientStreamingCall.cs +++ b/src/csharp/Grpc.Core/AsyncClientStreamingCall.cs @@ -44,12 +44,16 @@ namespace Grpc.Core { readonly IClientStreamWriter requestStream; readonly Task result; + readonly Func getStatusFunc; + readonly Func getTrailersFunc; readonly Action disposeAction; - public AsyncClientStreamingCall(IClientStreamWriter requestStream, Task result, Action disposeAction) + public AsyncClientStreamingCall(IClientStreamWriter requestStream, Task result, Func getStatusFunc, Func getTrailersFunc, Action disposeAction) { this.requestStream = requestStream; this.result = result; + this.getStatusFunc = getStatusFunc; + this.getTrailersFunc = getTrailersFunc; this.disposeAction = disposeAction; } @@ -85,7 +89,7 @@ namespace Grpc.Core } /// - /// Provides means to provide after the call. + /// Provides means to cleanup after the call. /// If the call has already finished normally (request stream has been completed and call result has been received), doesn't do anything. /// Otherwise, requests cancellation of the call which should terminate all pending async operations associated with the call. /// As a result, all resources being used by the call should be released eventually. diff --git a/src/csharp/Grpc.Core/AsyncDuplexStreamingCall.cs b/src/csharp/Grpc.Core/AsyncDuplexStreamingCall.cs index 4c0d5936ac..d76272c59b 100644 --- a/src/csharp/Grpc.Core/AsyncDuplexStreamingCall.cs +++ b/src/csharp/Grpc.Core/AsyncDuplexStreamingCall.cs @@ -44,14 +44,19 @@ namespace Grpc.Core { readonly IClientStreamWriter requestStream; readonly IAsyncStreamReader responseStream; + readonly Func getStatusFunc; + readonly Func getTrailersFunc; readonly Action disposeAction; - public AsyncDuplexStreamingCall(IClientStreamWriter requestStream, IAsyncStreamReader responseStream, Action disposeAction) + public AsyncDuplexStreamingCall(IClientStreamWriter requestStream, IAsyncStreamReader responseStream, Func getStatusFunc, Func getTrailersFunc, Action disposeAction) { this.requestStream = requestStream; this.responseStream = responseStream; + this.getStatusFunc = getStatusFunc; + this.getTrailersFunc = getTrailersFunc; this.disposeAction = disposeAction; } + /// /// Async stream to read streaming responses. @@ -75,6 +80,24 @@ namespace Grpc.Core } } + /// + /// Gets the call status if the call has already finished. + /// Throws InvalidOperationException otherwise. + /// + public Status GetStatus() + { + return getStatusFunc(); + } + + /// + /// Gets the call trailing metadata if the call has already finished. + /// Throws InvalidOperationException otherwise. + /// + public Metadata GetTrailers() + { + return getTrailersFunc(); + } + /// /// Provides means to cleanup after the call. /// If the call has already finished normally (request stream has been completed and response stream has been fully read), doesn't do anything. diff --git a/src/csharp/Grpc.Core/AsyncServerStreamingCall.cs b/src/csharp/Grpc.Core/AsyncServerStreamingCall.cs index 7a479b9a23..380efcdb0e 100644 --- a/src/csharp/Grpc.Core/AsyncServerStreamingCall.cs +++ b/src/csharp/Grpc.Core/AsyncServerStreamingCall.cs @@ -43,11 +43,15 @@ namespace Grpc.Core public sealed class AsyncServerStreamingCall : IDisposable { readonly IAsyncStreamReader responseStream; + readonly Func getStatusFunc; + readonly Func getTrailersFunc; readonly Action disposeAction; - public AsyncServerStreamingCall(IAsyncStreamReader responseStream, Action disposeAction) + public AsyncServerStreamingCall(IAsyncStreamReader responseStream, Func getStatusFunc, Func getTrailersFunc, Action disposeAction) { this.responseStream = responseStream; + this.getStatusFunc = getStatusFunc; + this.getTrailersFunc = getTrailersFunc; this.disposeAction = disposeAction; } @@ -62,6 +66,24 @@ namespace Grpc.Core } } + /// + /// Gets the call status if the call has already finished. + /// Throws InvalidOperationException otherwise. + /// + public Status GetStatus() + { + return getStatusFunc(); + } + + /// + /// Gets the call trailing metadata if the call has already finished. + /// Throws InvalidOperationException otherwise. + /// + public Metadata GetTrailers() + { + return getTrailersFunc(); + } + /// /// Provides means to cleanup after the call. /// If the call has already finished normally (response stream has been fully read), doesn't do anything. diff --git a/src/csharp/Grpc.Core/AsyncUnaryCall.cs b/src/csharp/Grpc.Core/AsyncUnaryCall.cs new file mode 100644 index 0000000000..c644d477ef --- /dev/null +++ b/src/csharp/Grpc.Core/AsyncUnaryCall.cs @@ -0,0 +1,106 @@ +#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 +{ + /// + /// Return type for single request - single response call. + /// + public sealed class AsyncUnaryCall : IDisposable + { + readonly Task result; + readonly Func getStatusFunc; + readonly Func getTrailersFunc; + readonly Action disposeAction; + + public AsyncUnaryCall(Task result, Func getStatusFunc, Func getTrailersFunc, Action disposeAction) + { + this.result = result; + this.getStatusFunc = getStatusFunc; + this.getTrailersFunc = getTrailersFunc; + this.disposeAction = disposeAction; + } + + /// + /// Asynchronous call result. + /// + public Task Result + { + get + { + return this.result; + } + } + + /// + /// Allows awaiting this object directly. + /// + public TaskAwaiter GetAwaiter() + { + return result.GetAwaiter(); + } + + /// + /// Gets the call status if the call has already finished. + /// Throws InvalidOperationException otherwise. + /// + public Status GetStatus() + { + return getStatusFunc(); + } + + /// + /// Gets the call trailing metadata if the call has already finished. + /// Throws InvalidOperationException otherwise. + /// + public Metadata GetTrailers() + { + return getTrailersFunc(); + } + + /// + /// Provides means to cleanup after the call. + /// If the call has already finished normally (request stream has been completed and call result has been received), doesn't do anything. + /// Otherwise, requests cancellation of the call which should terminate all pending async operations associated with the call. + /// As a result, all resources being used by the call should be released eventually. + /// + public void Dispose() + { + disposeAction.Invoke(); + } + } +} diff --git a/src/csharp/Grpc.Core/Calls.cs b/src/csharp/Grpc.Core/Calls.cs index 9e95182c72..61231ba612 100644 --- a/src/csharp/Grpc.Core/Calls.cs +++ b/src/csharp/Grpc.Core/Calls.cs @@ -73,7 +73,7 @@ namespace Grpc.Core asyncCall.StartServerStreamingCall(req, call.Headers); RegisterCancellationCallback(asyncCall, token); var responseStream = new ClientResponseStream(asyncCall); - return new AsyncServerStreamingCall(responseStream, asyncCall.Cancel); + return new AsyncServerStreamingCall(responseStream, asyncCall.GetStatus, asyncCall.GetTrailers, asyncCall.Cancel); } public static AsyncClientStreamingCall AsyncClientStreamingCall(Call call, CancellationToken token) @@ -85,7 +85,7 @@ namespace Grpc.Core var resultTask = asyncCall.ClientStreamingCallAsync(call.Headers); RegisterCancellationCallback(asyncCall, token); var requestStream = new ClientRequestStream(asyncCall); - return new AsyncClientStreamingCall(requestStream, resultTask, asyncCall.Cancel); + return new AsyncClientStreamingCall(requestStream, resultTask, asyncCall.GetStatus, asyncCall.GetTrailers, asyncCall.Cancel); } public static AsyncDuplexStreamingCall AsyncDuplexStreamingCall(Call call, CancellationToken token) @@ -98,7 +98,7 @@ namespace Grpc.Core RegisterCancellationCallback(asyncCall, token); var requestStream = new ClientRequestStream(asyncCall); var responseStream = new ClientResponseStream(asyncCall); - return new AsyncDuplexStreamingCall(requestStream, responseStream, asyncCall.Cancel); + return new AsyncDuplexStreamingCall(requestStream, responseStream, asyncCall.GetStatus, asyncCall.GetTrailers, asyncCall.Cancel); } private static void RegisterCancellationCallback(AsyncCall asyncCall, CancellationToken token) diff --git a/src/csharp/Grpc.Core/Grpc.Core.csproj b/src/csharp/Grpc.Core/Grpc.Core.csproj index a227fe5477..3b9b3b6f7e 100644 --- a/src/csharp/Grpc.Core/Grpc.Core.csproj +++ b/src/csharp/Grpc.Core/Grpc.Core.csproj @@ -33,13 +33,12 @@ - - False - ..\packages\System.Collections.Immutable.1.1.36\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll - ..\packages\Ix-Async.1.2.3\lib\net45\System.Interactive.Async.dll + + ..\packages\System.Collections.Immutable.1.1.36\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll + @@ -102,6 +101,7 @@ + diff --git a/src/csharp/Grpc.Core/Internal/AsyncCall.cs b/src/csharp/Grpc.Core/Internal/AsyncCall.cs index 660ad1c32a..f983dbb759 100644 --- a/src/csharp/Grpc.Core/Internal/AsyncCall.cs +++ b/src/csharp/Grpc.Core/Internal/AsyncCall.cs @@ -52,8 +52,8 @@ namespace Grpc.Core.Internal // Completion of a pending unary response if not null. TaskCompletionSource unaryResponseTcs; - // Set after status is received. Only used for streaming response calls. - Status? finishedStatus; + // Set after status is received. Used for both unary and streaming response calls. + ClientSideStatus? finishedStatus; bool readObserverCompleted; // True if readObserver has already been completed. @@ -248,6 +248,32 @@ namespace Grpc.Core.Internal } } + /// + /// Gets the resulting status if the call has already finished. + /// Throws InvalidOperationException otherwise. + /// + public Status GetStatus() + { + lock (myLock) + { + Preconditions.CheckState(finishedStatus.HasValue, "Status can only be accessed once the call has finished."); + return finishedStatus.Value.Status; + } + } + + /// + /// Gets the trailing metadata if the call has already finished. + /// Throws InvalidOperationException otherwise. + /// + public Metadata GetTrailers() + { + lock (myLock) + { + Preconditions.CheckState(finishedStatus.HasValue, "Trailers can only be accessed once the call has finished."); + return finishedStatus.Value.Trailers; + } + } + /// /// On client-side, we only fire readCompletionDelegate once all messages have been read /// and status has been received. @@ -265,7 +291,7 @@ namespace Grpc.Core.Internal if (shouldComplete) { - var status = finishedStatus.Value; + var status = finishedStatus.Value.Status; if (status.StatusCode != StatusCode.OK) { FireCompletion(completionDelegate, default(TResponse), new RpcException(status)); @@ -288,9 +314,13 @@ namespace Grpc.Core.Internal /// private void HandleUnaryResponse(bool success, BatchContextSafeHandle ctx) { + var fullStatus = ctx.GetReceivedStatusOnClient(); + lock (myLock) { finished = true; + finishedStatus = fullStatus; + halfclosed = true; ReleaseResourcesIfPossible(); @@ -302,7 +332,6 @@ namespace Grpc.Core.Internal return; } - var fullStatus = ctx.GetReceivedStatusOnClient(); var status = fullStatus.Status; if (status.StatusCode != StatusCode.OK) @@ -324,13 +353,12 @@ namespace Grpc.Core.Internal private void HandleFinished(bool success, BatchContextSafeHandle ctx) { var fullStatus = ctx.GetReceivedStatusOnClient(); - var status = fullStatus.Status; AsyncCompletionDelegate origReadCompletionDelegate = null; lock (myLock) { finished = true; - finishedStatus = status; + finishedStatus = fullStatus; origReadCompletionDelegate = readCompletionDelegate; -- cgit v1.2.3 From 5269d16dd945db697a5c0128d4911b6d27ee6fb1 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Tue, 21 Jul 2015 11:56:42 -0700 Subject: codegen and API changes --- src/compiler/csharp_generator.cc | 4 ++-- src/csharp/Grpc.Core/Calls.cs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/compiler/csharp_generator.cc b/src/compiler/csharp_generator.cc index 1910e9bd2d..64371047e0 100644 --- a/src/compiler/csharp_generator.cc +++ b/src/compiler/csharp_generator.cc @@ -149,7 +149,7 @@ std::string GetMethodRequestParamMaybe(const MethodDescriptor *method) { std::string GetMethodReturnTypeClient(const MethodDescriptor *method) { switch (GetMethodType(method)) { case METHODTYPE_NO_STREAMING: - return "Task<" + GetClassName(method->output_type()) + ">"; + return "AsyncUnaryCall<" + GetClassName(method->output_type()) + ">"; case METHODTYPE_CLIENT_STREAMING: return "AsyncClientStreamingCall<" + GetClassName(method->input_type()) + ", " + GetClassName(method->output_type()) + ">"; @@ -298,7 +298,7 @@ void GenerateServerInterface(Printer* out, const ServiceDescriptor *service) { out->Indent(); for (int i = 0; i < service->method_count(); i++) { const MethodDescriptor *method = service->method(i); - out->Print("$returntype$ $methodname$(ServerCallContext context, $request$$response_stream_maybe$);\n", + out->Print("$returntype$ $methodname$($request$$response_stream_maybe$, ServerCallContext context);\n", "methodname", method->name(), "returntype", GetMethodReturnTypeServer(method), "request", GetMethodRequestParamServer(method), "response_stream_maybe", diff --git a/src/csharp/Grpc.Core/Calls.cs b/src/csharp/Grpc.Core/Calls.cs index 61231ba612..359fe53741 100644 --- a/src/csharp/Grpc.Core/Calls.cs +++ b/src/csharp/Grpc.Core/Calls.cs @@ -53,7 +53,7 @@ namespace Grpc.Core return asyncCall.UnaryCall(call.Channel, call.Name, req, call.Headers); } - public static async Task AsyncUnaryCall(Call call, TRequest req, CancellationToken token) + public static AsyncUnaryCall AsyncUnaryCall(Call call, TRequest req, CancellationToken token) where TRequest : class where TResponse : class { @@ -61,7 +61,7 @@ namespace Grpc.Core asyncCall.Initialize(call.Channel, call.Channel.CompletionQueue, call.Name); var asyncResult = asyncCall.UnaryCallAsync(req, call.Headers); RegisterCancellationCallback(asyncCall, token); - return await asyncResult; + return new AsyncUnaryCall(asyncResult, asyncCall.GetStatus, asyncCall.GetTrailers, asyncCall.Cancel); } public static AsyncServerStreamingCall AsyncServerStreamingCall(Call call, TRequest req, CancellationToken token) -- cgit v1.2.3 From 25bb2ef8b84651bff7175ac221448da152f03dad Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Tue, 21 Jul 2015 12:15:53 -0700 Subject: regenerated code and fixed inconsistencies --- src/csharp/Grpc.Core.Tests/ClientServerTest.cs | 13 +++++++------ src/csharp/Grpc.Core/Internal/ServerCallHandler.cs | 8 ++++---- src/csharp/Grpc.Core/ServerMethods.cs | 8 ++++---- src/csharp/Grpc.Examples/MathExamples.cs | 3 +-- src/csharp/Grpc.Examples/MathGrpc.cs | 12 ++++++------ src/csharp/Grpc.Examples/MathServiceImpl.cs | 8 ++++---- .../Grpc.HealthCheck.Tests/HealthServiceImplTest.cs | 2 +- src/csharp/Grpc.HealthCheck/HealthGrpc.cs | 6 +++--- src/csharp/Grpc.HealthCheck/HealthServiceImpl.cs | 2 +- src/csharp/Grpc.IntegrationTesting/TestGrpc.cs | 20 ++++++++++---------- .../Grpc.IntegrationTesting/TestServiceImpl.cs | 12 ++++++------ 11 files changed, 47 insertions(+), 47 deletions(-) diff --git a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs index cd8af3dbc0..a10529a614 100644 --- a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs +++ b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs @@ -152,7 +152,7 @@ namespace Grpc.Core.Tests public void AsyncUnaryCall() { var call = new Call(ServiceName, EchoMethod, channel, Metadata.Empty); - var result = Calls.AsyncUnaryCall(call, "ABC", CancellationToken.None).Result; + var result = Calls.AsyncUnaryCall(call, "ABC", CancellationToken.None).Result.Result; Assert.AreEqual("ABC", result); } @@ -221,11 +221,12 @@ namespace Grpc.Core.Tests new Metadata.Entry("binaryHeader-bin", new byte[] { 1, 2, 3, 0, 0xff } ), }; var call = new Call(ServiceName, EchoMethod, channel, metadata); - var result = Calls.AsyncUnaryCall(call, "ABC", CancellationToken.None).Result; - Assert.AreEqual("ABC", result); + var callResult = Calls.AsyncUnaryCall(call, "ABC", CancellationToken.None); + + Assert.AreEqual("ABC", callResult.Result.Result); // TODO: implement assertion... - Assert.Fail(); + //Assert.Fail(); } [Test] @@ -260,7 +261,7 @@ namespace Grpc.Core.Tests } } - private static async Task EchoHandler(ServerCallContext context, string request) + private static async Task EchoHandler(string request, ServerCallContext context) { foreach (Metadata.Entry metadataEntry in context.RequestHeaders) { @@ -286,7 +287,7 @@ namespace Grpc.Core.Tests return request; } - private static async Task ConcatAndEchoHandler(ServerCallContext context, IAsyncStreamReader requestStream) + private static async Task ConcatAndEchoHandler(IAsyncStreamReader requestStream, ServerCallContext context) { string result = ""; await requestStream.ForEach(async (request) => diff --git a/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs b/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs index 03062d1434..bcd438f969 100644 --- a/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs +++ b/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs @@ -79,7 +79,7 @@ namespace Grpc.Core.Internal var request = requestStream.Current; // TODO(jtattermusch): we need to read the full stream so that native callhandle gets deallocated. Preconditions.CheckArgument(!await requestStream.MoveNext()); - var result = await handler(context, request); + var result = await handler(request, context); status = context.Status; await responseStream.WriteAsync(result); } @@ -133,7 +133,7 @@ namespace Grpc.Core.Internal var request = requestStream.Current; // TODO(jtattermusch): we need to read the full stream so that native callhandle gets deallocated. Preconditions.CheckArgument(!await requestStream.MoveNext()); - await handler(context, request, responseStream); + await handler(request, responseStream, context); status = context.Status; } catch (Exception e) @@ -184,7 +184,7 @@ namespace Grpc.Core.Internal var context = HandlerUtils.NewContext(newRpc); try { - var result = await handler(context, requestStream); + var result = await handler(requestStream, context); status = context.Status; try { @@ -242,7 +242,7 @@ namespace Grpc.Core.Internal var context = HandlerUtils.NewContext(newRpc); try { - await handler(context, requestStream, responseStream); + await handler(requestStream, responseStream, context); status = context.Status; } catch (Exception e) diff --git a/src/csharp/Grpc.Core/ServerMethods.cs b/src/csharp/Grpc.Core/ServerMethods.cs index 377b78eb30..d457770203 100644 --- a/src/csharp/Grpc.Core/ServerMethods.cs +++ b/src/csharp/Grpc.Core/ServerMethods.cs @@ -42,28 +42,28 @@ namespace Grpc.Core /// /// Server-side handler for unary call. /// - public delegate Task UnaryServerMethod(ServerCallContext context, TRequest request) + public delegate Task UnaryServerMethod(TRequest request, ServerCallContext context) where TRequest : class where TResponse : class; /// /// Server-side handler for client streaming call. /// - public delegate Task ClientStreamingServerMethod(ServerCallContext context, IAsyncStreamReader requestStream) + public delegate Task ClientStreamingServerMethod(IAsyncStreamReader requestStream, ServerCallContext context) where TRequest : class where TResponse : class; /// /// Server-side handler for server streaming call. /// - public delegate Task ServerStreamingServerMethod(ServerCallContext context, TRequest request, IServerStreamWriter responseStream) + public delegate Task ServerStreamingServerMethod(TRequest request, IServerStreamWriter responseStream, ServerCallContext context) where TRequest : class where TResponse : class; /// /// Server-side handler for bidi streaming call. /// - public delegate Task DuplexStreamingServerMethod(ServerCallContext context, IAsyncStreamReader requestStream, IServerStreamWriter responseStream) + public delegate Task DuplexStreamingServerMethod(IAsyncStreamReader requestStream, IServerStreamWriter responseStream, ServerCallContext context) where TRequest : class where TResponse : class; } diff --git a/src/csharp/Grpc.Examples/MathExamples.cs b/src/csharp/Grpc.Examples/MathExamples.cs index 7deb651689..90956f65a2 100644 --- a/src/csharp/Grpc.Examples/MathExamples.cs +++ b/src/csharp/Grpc.Examples/MathExamples.cs @@ -46,8 +46,7 @@ namespace math public static async Task DivAsyncExample(Math.IMathClient client) { - Task resultTask = client.DivAsync(new DivArgs.Builder { Dividend = 4, Divisor = 5 }.Build()); - DivReply result = await resultTask; + DivReply result = await client.DivAsync(new DivArgs.Builder { Dividend = 4, Divisor = 5 }.Build()); Console.WriteLine("DivAsync Result: " + result); } diff --git a/src/csharp/Grpc.Examples/MathGrpc.cs b/src/csharp/Grpc.Examples/MathGrpc.cs index 1805972ce3..ef787cf1d8 100644 --- a/src/csharp/Grpc.Examples/MathGrpc.cs +++ b/src/csharp/Grpc.Examples/MathGrpc.cs @@ -45,7 +45,7 @@ namespace math { public interface IMathClient { global::math.DivReply Div(global::math.DivArgs request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)); - Task DivAsync(global::math.DivArgs request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)); + AsyncUnaryCall DivAsync(global::math.DivArgs request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)); AsyncDuplexStreamingCall DivMany(Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)); AsyncServerStreamingCall Fib(global::math.FibArgs request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)); AsyncClientStreamingCall Sum(Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)); @@ -54,10 +54,10 @@ namespace math { // server-side interface public interface IMath { - Task Div(ServerCallContext context, global::math.DivArgs request); - Task DivMany(ServerCallContext context, IAsyncStreamReader requestStream, IServerStreamWriter responseStream); - Task Fib(ServerCallContext context, global::math.FibArgs request, IServerStreamWriter responseStream); - Task Sum(ServerCallContext context, IAsyncStreamReader requestStream); + Task Div(global::math.DivArgs request, ServerCallContext context); + Task DivMany(IAsyncStreamReader requestStream, IServerStreamWriter responseStream, ServerCallContext context); + Task Fib(global::math.FibArgs request, IServerStreamWriter responseStream, ServerCallContext context); + Task Sum(IAsyncStreamReader requestStream, ServerCallContext context); } // client stub @@ -71,7 +71,7 @@ namespace math { var call = CreateCall(__ServiceName, __Method_Div, headers); return Calls.BlockingUnaryCall(call, request, cancellationToken); } - public Task DivAsync(global::math.DivArgs request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)) + public AsyncUnaryCall DivAsync(global::math.DivArgs request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)) { var call = CreateCall(__ServiceName, __Method_Div, headers); return Calls.AsyncUnaryCall(call, request, cancellationToken); diff --git a/src/csharp/Grpc.Examples/MathServiceImpl.cs b/src/csharp/Grpc.Examples/MathServiceImpl.cs index e247ac9d73..3dd0f53a0d 100644 --- a/src/csharp/Grpc.Examples/MathServiceImpl.cs +++ b/src/csharp/Grpc.Examples/MathServiceImpl.cs @@ -45,12 +45,12 @@ namespace math /// public class MathServiceImpl : Math.IMath { - public Task Div(ServerCallContext context, DivArgs request) + public Task Div(DivArgs request, ServerCallContext context) { return Task.FromResult(DivInternal(request)); } - public async Task Fib(ServerCallContext context, FibArgs request, IServerStreamWriter responseStream) + public async Task Fib(FibArgs request, IServerStreamWriter responseStream, ServerCallContext context) { if (request.Limit <= 0) { @@ -67,7 +67,7 @@ namespace math } } - public async Task Sum(ServerCallContext context, IAsyncStreamReader requestStream) + public async Task Sum(IAsyncStreamReader requestStream, ServerCallContext context) { long sum = 0; await requestStream.ForEach(async num => @@ -77,7 +77,7 @@ namespace math return Num.CreateBuilder().SetNum_(sum).Build(); } - public async Task DivMany(ServerCallContext context, IAsyncStreamReader requestStream, IServerStreamWriter responseStream) + public async Task DivMany(IAsyncStreamReader requestStream, IServerStreamWriter responseStream, ServerCallContext context) { await requestStream.ForEach(async divArgs => { diff --git a/src/csharp/Grpc.HealthCheck.Tests/HealthServiceImplTest.cs b/src/csharp/Grpc.HealthCheck.Tests/HealthServiceImplTest.cs index 9b7c4f2140..7184415655 100644 --- a/src/csharp/Grpc.HealthCheck.Tests/HealthServiceImplTest.cs +++ b/src/csharp/Grpc.HealthCheck.Tests/HealthServiceImplTest.cs @@ -101,7 +101,7 @@ namespace Grpc.HealthCheck.Tests private static HealthCheckResponse.Types.ServingStatus GetStatusHelper(HealthServiceImpl impl, string host, string service) { - return impl.Check(null, HealthCheckRequest.CreateBuilder().SetHost(host).SetService(service).Build()).Result.Status; + return impl.Check(HealthCheckRequest.CreateBuilder().SetHost(host).SetService(service).Build(), null).Result.Status; } } } diff --git a/src/csharp/Grpc.HealthCheck/HealthGrpc.cs b/src/csharp/Grpc.HealthCheck/HealthGrpc.cs index 3aebdcb557..217127eca7 100644 --- a/src/csharp/Grpc.HealthCheck/HealthGrpc.cs +++ b/src/csharp/Grpc.HealthCheck/HealthGrpc.cs @@ -25,13 +25,13 @@ namespace Grpc.Health.V1Alpha { public interface IHealthClient { global::Grpc.Health.V1Alpha.HealthCheckResponse Check(global::Grpc.Health.V1Alpha.HealthCheckRequest request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)); - Task CheckAsync(global::Grpc.Health.V1Alpha.HealthCheckRequest request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)); + AsyncUnaryCall CheckAsync(global::Grpc.Health.V1Alpha.HealthCheckRequest request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)); } // server-side interface public interface IHealth { - Task Check(ServerCallContext context, global::Grpc.Health.V1Alpha.HealthCheckRequest request); + Task Check(global::Grpc.Health.V1Alpha.HealthCheckRequest request, ServerCallContext context); } // client stub @@ -45,7 +45,7 @@ namespace Grpc.Health.V1Alpha { var call = CreateCall(__ServiceName, __Method_Check, headers); return Calls.BlockingUnaryCall(call, request, cancellationToken); } - public Task CheckAsync(global::Grpc.Health.V1Alpha.HealthCheckRequest request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)) + public AsyncUnaryCall CheckAsync(global::Grpc.Health.V1Alpha.HealthCheckRequest request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)) { var call = CreateCall(__ServiceName, __Method_Check, headers); return Calls.AsyncUnaryCall(call, request, cancellationToken); diff --git a/src/csharp/Grpc.HealthCheck/HealthServiceImpl.cs b/src/csharp/Grpc.HealthCheck/HealthServiceImpl.cs index db3a2a0942..3c3b9c35f1 100644 --- a/src/csharp/Grpc.HealthCheck/HealthServiceImpl.cs +++ b/src/csharp/Grpc.HealthCheck/HealthServiceImpl.cs @@ -95,7 +95,7 @@ namespace Grpc.HealthCheck } } - public Task Check(ServerCallContext context, HealthCheckRequest request) + public Task Check(HealthCheckRequest request, ServerCallContext context) { lock (myLock) { diff --git a/src/csharp/Grpc.IntegrationTesting/TestGrpc.cs b/src/csharp/Grpc.IntegrationTesting/TestGrpc.cs index 96d9b23717..de2fa07441 100644 --- a/src/csharp/Grpc.IntegrationTesting/TestGrpc.cs +++ b/src/csharp/Grpc.IntegrationTesting/TestGrpc.cs @@ -60,9 +60,9 @@ namespace grpc.testing { public interface ITestServiceClient { global::grpc.testing.Empty EmptyCall(global::grpc.testing.Empty request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)); - Task EmptyCallAsync(global::grpc.testing.Empty request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)); + AsyncUnaryCall EmptyCallAsync(global::grpc.testing.Empty request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)); global::grpc.testing.SimpleResponse UnaryCall(global::grpc.testing.SimpleRequest request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)); - Task UnaryCallAsync(global::grpc.testing.SimpleRequest request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)); + AsyncUnaryCall UnaryCallAsync(global::grpc.testing.SimpleRequest request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)); AsyncServerStreamingCall StreamingOutputCall(global::grpc.testing.StreamingOutputCallRequest request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)); AsyncClientStreamingCall StreamingInputCall(Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)); AsyncDuplexStreamingCall FullDuplexCall(Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)); @@ -72,12 +72,12 @@ namespace grpc.testing { // server-side interface public interface ITestService { - Task EmptyCall(ServerCallContext context, global::grpc.testing.Empty request); - Task UnaryCall(ServerCallContext context, global::grpc.testing.SimpleRequest request); - Task StreamingOutputCall(ServerCallContext context, global::grpc.testing.StreamingOutputCallRequest request, IServerStreamWriter responseStream); - Task StreamingInputCall(ServerCallContext context, IAsyncStreamReader requestStream); - Task FullDuplexCall(ServerCallContext context, IAsyncStreamReader requestStream, IServerStreamWriter responseStream); - Task HalfDuplexCall(ServerCallContext context, IAsyncStreamReader requestStream, IServerStreamWriter responseStream); + Task EmptyCall(global::grpc.testing.Empty request, ServerCallContext context); + Task UnaryCall(global::grpc.testing.SimpleRequest request, ServerCallContext context); + Task StreamingOutputCall(global::grpc.testing.StreamingOutputCallRequest request, IServerStreamWriter responseStream, ServerCallContext context); + Task StreamingInputCall(IAsyncStreamReader requestStream, ServerCallContext context); + Task FullDuplexCall(IAsyncStreamReader requestStream, IServerStreamWriter responseStream, ServerCallContext context); + Task HalfDuplexCall(IAsyncStreamReader requestStream, IServerStreamWriter responseStream, ServerCallContext context); } // client stub @@ -91,7 +91,7 @@ namespace grpc.testing { var call = CreateCall(__ServiceName, __Method_EmptyCall, headers); return Calls.BlockingUnaryCall(call, request, cancellationToken); } - public Task EmptyCallAsync(global::grpc.testing.Empty request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)) + public AsyncUnaryCall EmptyCallAsync(global::grpc.testing.Empty request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)) { var call = CreateCall(__ServiceName, __Method_EmptyCall, headers); return Calls.AsyncUnaryCall(call, request, cancellationToken); @@ -101,7 +101,7 @@ namespace grpc.testing { var call = CreateCall(__ServiceName, __Method_UnaryCall, headers); return Calls.BlockingUnaryCall(call, request, cancellationToken); } - public Task UnaryCallAsync(global::grpc.testing.SimpleRequest request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)) + public AsyncUnaryCall UnaryCallAsync(global::grpc.testing.SimpleRequest request, Metadata headers = null, CancellationToken cancellationToken = default(CancellationToken)) { var call = CreateCall(__ServiceName, __Method_UnaryCall, headers); return Calls.AsyncUnaryCall(call, request, cancellationToken); diff --git a/src/csharp/Grpc.IntegrationTesting/TestServiceImpl.cs b/src/csharp/Grpc.IntegrationTesting/TestServiceImpl.cs index 6bd997d1f4..ccf9fe6ced 100644 --- a/src/csharp/Grpc.IntegrationTesting/TestServiceImpl.cs +++ b/src/csharp/Grpc.IntegrationTesting/TestServiceImpl.cs @@ -46,19 +46,19 @@ namespace grpc.testing /// public class TestServiceImpl : TestService.ITestService { - public Task EmptyCall(ServerCallContext context, Empty request) + public Task EmptyCall(Empty request, ServerCallContext context) { return Task.FromResult(Empty.DefaultInstance); } - public Task UnaryCall(ServerCallContext context, SimpleRequest request) + public Task UnaryCall(SimpleRequest request, ServerCallContext context) { var response = SimpleResponse.CreateBuilder() .SetPayload(CreateZerosPayload(request.ResponseSize)).Build(); return Task.FromResult(response); } - public async Task StreamingOutputCall(ServerCallContext context, StreamingOutputCallRequest request, IServerStreamWriter responseStream) + public async Task StreamingOutputCall(StreamingOutputCallRequest request, IServerStreamWriter responseStream, ServerCallContext context) { foreach (var responseParam in request.ResponseParametersList) { @@ -68,7 +68,7 @@ namespace grpc.testing } } - public async Task StreamingInputCall(ServerCallContext context, IAsyncStreamReader requestStream) + public async Task StreamingInputCall(IAsyncStreamReader requestStream, ServerCallContext context) { int sum = 0; await requestStream.ForEach(async request => @@ -78,7 +78,7 @@ namespace grpc.testing return StreamingInputCallResponse.CreateBuilder().SetAggregatedPayloadSize(sum).Build(); } - public async Task FullDuplexCall(ServerCallContext context, IAsyncStreamReader requestStream, IServerStreamWriter responseStream) + public async Task FullDuplexCall(IAsyncStreamReader requestStream, IServerStreamWriter responseStream, ServerCallContext context) { await requestStream.ForEach(async request => { @@ -91,7 +91,7 @@ namespace grpc.testing }); } - public async Task HalfDuplexCall(ServerCallContext context, IAsyncStreamReader requestStream, IServerStreamWriter responseStream) + public async Task HalfDuplexCall(IAsyncStreamReader requestStream, IServerStreamWriter responseStream, ServerCallContext context) { throw new NotImplementedException(); } -- cgit v1.2.3 From 7d219cfe4afe61b96c235dffb049bb856a62124e Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Tue, 21 Jul 2015 12:23:31 -0700 Subject: fix echo metadata test --- src/csharp/Grpc.Core.Tests/ClientServerTest.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs index a10529a614..87055b1adc 100644 --- a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs +++ b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs @@ -129,7 +129,7 @@ namespace Grpc.Core.Tests } catch (RpcException e) { - Assert.AreEqual(StatusCode.Unauthenticated, e.Status.StatusCode); + Assert.AreEqual(StatusCode.Unauthenticated, e.Status.StatusCode); } } @@ -215,18 +215,25 @@ namespace Grpc.Core.Tests [Test] public void AsyncUnaryCall_EchoMetadata() { - var metadata = new Metadata + var headers = new Metadata { new Metadata.Entry("asciiHeader", "abcdefg"), new Metadata.Entry("binaryHeader-bin", new byte[] { 1, 2, 3, 0, 0xff } ), }; - var call = new Call(ServiceName, EchoMethod, channel, metadata); + var call = new Call(ServiceName, EchoMethod, channel, headers); var callResult = Calls.AsyncUnaryCall(call, "ABC", CancellationToken.None); Assert.AreEqual("ABC", callResult.Result.Result); - // TODO: implement assertion... - //Assert.Fail(); + Assert.AreEqual(StatusCode.OK, callResult.GetStatus().StatusCode); + + var trailers = callResult.GetTrailers(); + Assert.AreEqual(2, trailers.Count); + Assert.AreEqual(headers[0].Key, trailers[0].Key); + Assert.AreEqual(headers[0].Value, trailers[0].Value); + + Assert.AreEqual(headers[1].Key, trailers[1].Key); + CollectionAssert.AreEqual(headers[1].ValueBytes, trailers[1].ValueBytes); } [Test] -- cgit v1.2.3 From a236ff205ba3211dc547e20f6b0689df4b542858 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Tue, 21 Jul 2015 12:33:31 -0700 Subject: rename Result to ResponseAsync --- src/csharp/Grpc.Core.Tests/ClientServerTest.cs | 8 ++++---- src/csharp/Grpc.Core/AsyncClientStreamingCall.cs | 12 ++++++------ src/csharp/Grpc.Core/AsyncUnaryCall.cs | 12 ++++++------ src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs | 2 +- src/csharp/Grpc.Examples/MathExamples.cs | 4 ++-- src/csharp/Grpc.IntegrationTesting/InteropClient.cs | 4 ++-- 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs index 87055b1adc..98fc6b4f10 100644 --- a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs +++ b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs @@ -152,7 +152,7 @@ namespace Grpc.Core.Tests public void AsyncUnaryCall() { var call = new Call(ServiceName, EchoMethod, channel, Metadata.Empty); - var result = Calls.AsyncUnaryCall(call, "ABC", CancellationToken.None).Result.Result; + var result = Calls.AsyncUnaryCall(call, "ABC", CancellationToken.None).ResponseAsync.Result; Assert.AreEqual("ABC", result); } @@ -183,7 +183,7 @@ namespace Grpc.Core.Tests var callResult = Calls.AsyncClientStreamingCall(call, CancellationToken.None); await callResult.RequestStream.WriteAll(new string[] { "A", "B", "C" }); - Assert.AreEqual("ABC", await callResult.Result); + Assert.AreEqual("ABC", await callResult.ResponseAsync); }).Wait(); } @@ -203,7 +203,7 @@ namespace Grpc.Core.Tests try { - await callResult.Result; + await callResult.ResponseAsync; } catch (RpcException e) { @@ -223,7 +223,7 @@ namespace Grpc.Core.Tests var call = new Call(ServiceName, EchoMethod, channel, headers); var callResult = Calls.AsyncUnaryCall(call, "ABC", CancellationToken.None); - Assert.AreEqual("ABC", callResult.Result.Result); + Assert.AreEqual("ABC", callResult.ResponseAsync.Result); Assert.AreEqual(StatusCode.OK, callResult.GetStatus().StatusCode); diff --git a/src/csharp/Grpc.Core/AsyncClientStreamingCall.cs b/src/csharp/Grpc.Core/AsyncClientStreamingCall.cs index 98ebeea318..bf020cd627 100644 --- a/src/csharp/Grpc.Core/AsyncClientStreamingCall.cs +++ b/src/csharp/Grpc.Core/AsyncClientStreamingCall.cs @@ -43,15 +43,15 @@ namespace Grpc.Core public sealed class AsyncClientStreamingCall : IDisposable { readonly IClientStreamWriter requestStream; - readonly Task result; + readonly Task responseAsync; readonly Func getStatusFunc; readonly Func getTrailersFunc; readonly Action disposeAction; - public AsyncClientStreamingCall(IClientStreamWriter requestStream, Task result, Func getStatusFunc, Func getTrailersFunc, Action disposeAction) + public AsyncClientStreamingCall(IClientStreamWriter requestStream, Task responseAsync, Func getStatusFunc, Func getTrailersFunc, Action disposeAction) { this.requestStream = requestStream; - this.result = result; + this.responseAsync = responseAsync; this.getStatusFunc = getStatusFunc; this.getTrailersFunc = getTrailersFunc; this.disposeAction = disposeAction; @@ -60,11 +60,11 @@ namespace Grpc.Core /// /// Asynchronous call result. /// - public Task Result + public Task ResponseAsync { get { - return this.result; + return this.responseAsync; } } @@ -85,7 +85,7 @@ namespace Grpc.Core /// public TaskAwaiter GetAwaiter() { - return result.GetAwaiter(); + return responseAsync.GetAwaiter(); } /// diff --git a/src/csharp/Grpc.Core/AsyncUnaryCall.cs b/src/csharp/Grpc.Core/AsyncUnaryCall.cs index c644d477ef..224e343916 100644 --- a/src/csharp/Grpc.Core/AsyncUnaryCall.cs +++ b/src/csharp/Grpc.Core/AsyncUnaryCall.cs @@ -42,14 +42,14 @@ namespace Grpc.Core /// public sealed class AsyncUnaryCall : IDisposable { - readonly Task result; + readonly Task responseAsync; readonly Func getStatusFunc; readonly Func getTrailersFunc; readonly Action disposeAction; - public AsyncUnaryCall(Task result, Func getStatusFunc, Func getTrailersFunc, Action disposeAction) + public AsyncUnaryCall(Task responseAsync, Func getStatusFunc, Func getTrailersFunc, Action disposeAction) { - this.result = result; + this.responseAsync = responseAsync; this.getStatusFunc = getStatusFunc; this.getTrailersFunc = getTrailersFunc; this.disposeAction = disposeAction; @@ -58,11 +58,11 @@ namespace Grpc.Core /// /// Asynchronous call result. /// - public Task Result + public Task ResponseAsync { get { - return this.result; + return this.responseAsync; } } @@ -71,7 +71,7 @@ namespace Grpc.Core /// public TaskAwaiter GetAwaiter() { - return result.GetAwaiter(); + return responseAsync.GetAwaiter(); } /// diff --git a/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs b/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs index e7c4b33120..7a957c5b6f 100644 --- a/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs +++ b/src/csharp/Grpc.Examples.Tests/MathClientServerTests.cs @@ -144,7 +144,7 @@ namespace math.Tests n => Num.CreateBuilder().SetNum_(n).Build()); await call.RequestStream.WriteAll(numbers); - var result = await call.Result; + var result = await call.ResponseAsync; Assert.AreEqual(60, result.Num_); } }).Wait(); diff --git a/src/csharp/Grpc.Examples/MathExamples.cs b/src/csharp/Grpc.Examples/MathExamples.cs index 90956f65a2..06d81a4d83 100644 --- a/src/csharp/Grpc.Examples/MathExamples.cs +++ b/src/csharp/Grpc.Examples/MathExamples.cs @@ -71,7 +71,7 @@ namespace math using (var call = client.Sum()) { await call.RequestStream.WriteAll(numbers); - Console.WriteLine("Sum Result: " + await call.Result); + Console.WriteLine("Sum Result: " + await call.ResponseAsync); } } @@ -103,7 +103,7 @@ namespace math using (var sumCall = client.Sum()) { await sumCall.RequestStream.WriteAll(numbers); - sum = await sumCall.Result; + sum = await sumCall.ResponseAsync; } DivReply result = await client.DivAsync(new DivArgs.Builder { Dividend = sum.Num_, Divisor = numbers.Count }.Build()); diff --git a/src/csharp/Grpc.IntegrationTesting/InteropClient.cs b/src/csharp/Grpc.IntegrationTesting/InteropClient.cs index ea83aaf2c1..2746dc945e 100644 --- a/src/csharp/Grpc.IntegrationTesting/InteropClient.cs +++ b/src/csharp/Grpc.IntegrationTesting/InteropClient.cs @@ -219,7 +219,7 @@ namespace Grpc.IntegrationTesting { await call.RequestStream.WriteAll(bodySizes); - var response = await call.Result; + var response = await call.ResponseAsync; Assert.AreEqual(74922, response.AggregatedPayloadSize); } Console.WriteLine("Passed!"); @@ -421,7 +421,7 @@ namespace Grpc.IntegrationTesting try { - var response = await call.Result; + var response = await call.ResponseAsync; Assert.Fail(); } catch (RpcException e) -- cgit v1.2.3 From e7e1c82d5e5f9bcad91390bd9b7c73c51c45f8cb Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Tue, 21 Jul 2015 12:38:07 -0700 Subject: improving test readability --- src/csharp/Grpc.Core.Tests/ClientServerTest.cs | 60 +++++++++++++------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs index 98fc6b4f10..d82a985f0c 100644 --- a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs +++ b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs @@ -99,17 +99,17 @@ namespace Grpc.Core.Tests [Test] public void UnaryCall() { - var call = new Call(ServiceName, EchoMethod, channel, Metadata.Empty); - Assert.AreEqual("ABC", Calls.BlockingUnaryCall(call, "ABC", CancellationToken.None)); + var internalCall = new Call(ServiceName, EchoMethod, channel, Metadata.Empty); + Assert.AreEqual("ABC", Calls.BlockingUnaryCall(internalCall, "ABC", CancellationToken.None)); } [Test] public void UnaryCall_ServerHandlerThrows() { - var call = new Call(ServiceName, EchoMethod, channel, Metadata.Empty); + var internalCall = new Call(ServiceName, EchoMethod, channel, Metadata.Empty); try { - Calls.BlockingUnaryCall(call, "THROW", CancellationToken.None); + Calls.BlockingUnaryCall(internalCall, "THROW", CancellationToken.None); Assert.Fail(); } catch (RpcException e) @@ -121,10 +121,10 @@ namespace Grpc.Core.Tests [Test] public void UnaryCall_ServerHandlerThrowsRpcException() { - var call = new Call(ServiceName, EchoMethod, channel, Metadata.Empty); + var internalCall = new Call(ServiceName, EchoMethod, channel, Metadata.Empty); try { - Calls.BlockingUnaryCall(call, "THROW_UNAUTHENTICATED", CancellationToken.None); + Calls.BlockingUnaryCall(internalCall, "THROW_UNAUTHENTICATED", CancellationToken.None); Assert.Fail(); } catch (RpcException e) @@ -136,10 +136,10 @@ namespace Grpc.Core.Tests [Test] public void UnaryCall_ServerHandlerSetsStatus() { - var call = new Call(ServiceName, EchoMethod, channel, Metadata.Empty); + var internalCall = new Call(ServiceName, EchoMethod, channel, Metadata.Empty); try { - Calls.BlockingUnaryCall(call, "SET_UNAUTHENTICATED", CancellationToken.None); + Calls.BlockingUnaryCall(internalCall, "SET_UNAUTHENTICATED", CancellationToken.None); Assert.Fail(); } catch (RpcException e) @@ -151,8 +151,8 @@ namespace Grpc.Core.Tests [Test] public void AsyncUnaryCall() { - var call = new Call(ServiceName, EchoMethod, channel, Metadata.Empty); - var result = Calls.AsyncUnaryCall(call, "ABC", CancellationToken.None).ResponseAsync.Result; + var internalCall = new Call(ServiceName, EchoMethod, channel, Metadata.Empty); + var result = Calls.AsyncUnaryCall(internalCall, "ABC", CancellationToken.None).ResponseAsync.Result; Assert.AreEqual("ABC", result); } @@ -161,10 +161,10 @@ namespace Grpc.Core.Tests { Task.Run(async () => { - var call = new Call(ServiceName, EchoMethod, channel, Metadata.Empty); + var internalCall = new Call(ServiceName, EchoMethod, channel, Metadata.Empty); try { - await Calls.AsyncUnaryCall(call, "THROW", CancellationToken.None); + await Calls.AsyncUnaryCall(internalCall, "THROW", CancellationToken.None); Assert.Fail(); } catch (RpcException e) @@ -179,11 +179,11 @@ namespace Grpc.Core.Tests { Task.Run(async () => { - var call = new Call(ServiceName, ConcatAndEchoMethod, channel, Metadata.Empty); - var callResult = Calls.AsyncClientStreamingCall(call, CancellationToken.None); + var internalCall = new Call(ServiceName, ConcatAndEchoMethod, channel, Metadata.Empty); + var call = Calls.AsyncClientStreamingCall(internalCall, CancellationToken.None); - await callResult.RequestStream.WriteAll(new string[] { "A", "B", "C" }); - Assert.AreEqual("ABC", await callResult.ResponseAsync); + await call.RequestStream.WriteAll(new string[] { "A", "B", "C" }); + Assert.AreEqual("ABC", await call.ResponseAsync); }).Wait(); } @@ -192,10 +192,10 @@ namespace Grpc.Core.Tests { Task.Run(async () => { - var call = new Call(ServiceName, ConcatAndEchoMethod, channel, Metadata.Empty); + var internalCall = new Call(ServiceName, ConcatAndEchoMethod, channel, Metadata.Empty); var cts = new CancellationTokenSource(); - var callResult = Calls.AsyncClientStreamingCall(call, cts.Token); + var call = Calls.AsyncClientStreamingCall(internalCall, cts.Token); // TODO(jtattermusch): we need this to ensure call has been initiated once we cancel it. await Task.Delay(1000); @@ -203,7 +203,7 @@ namespace Grpc.Core.Tests try { - await callResult.ResponseAsync; + await call.ResponseAsync; } catch (RpcException e) { @@ -220,14 +220,14 @@ namespace Grpc.Core.Tests new Metadata.Entry("asciiHeader", "abcdefg"), new Metadata.Entry("binaryHeader-bin", new byte[] { 1, 2, 3, 0, 0xff } ), }; - var call = new Call(ServiceName, EchoMethod, channel, headers); - var callResult = Calls.AsyncUnaryCall(call, "ABC", CancellationToken.None); + var internalCall = new Call(ServiceName, EchoMethod, channel, headers); + var call = Calls.AsyncUnaryCall(internalCall, "ABC", CancellationToken.None); - Assert.AreEqual("ABC", callResult.ResponseAsync.Result); + Assert.AreEqual("ABC", call.ResponseAsync.Result); - Assert.AreEqual(StatusCode.OK, callResult.GetStatus().StatusCode); + Assert.AreEqual(StatusCode.OK, call.GetStatus().StatusCode); - var trailers = callResult.GetTrailers(); + var trailers = call.GetTrailers(); Assert.AreEqual(2, trailers.Count); Assert.AreEqual(headers[0].Key, trailers[0].Key); Assert.AreEqual(headers[0].Value, trailers[0].Value); @@ -241,25 +241,25 @@ namespace Grpc.Core.Tests { channel.Dispose(); - var call = new Call(ServiceName, EchoMethod, channel, Metadata.Empty); - Assert.Throws(typeof(ObjectDisposedException), () => Calls.BlockingUnaryCall(call, "ABC", CancellationToken.None)); + var internalCall = new Call(ServiceName, EchoMethod, channel, Metadata.Empty); + Assert.Throws(typeof(ObjectDisposedException), () => Calls.BlockingUnaryCall(internalCall, "ABC", CancellationToken.None)); } [Test] public void UnaryCallPerformance() { - var call = new Call(ServiceName, EchoMethod, channel, Metadata.Empty); + var internalCall = new Call(ServiceName, EchoMethod, channel, Metadata.Empty); BenchmarkUtil.RunBenchmark(100, 100, - () => { Calls.BlockingUnaryCall(call, "ABC", default(CancellationToken)); }); + () => { Calls.BlockingUnaryCall(internalCall, "ABC", default(CancellationToken)); }); } [Test] public void UnknownMethodHandler() { - var call = new Call(ServiceName, NonexistentMethod, channel, Metadata.Empty); + var internalCall = new Call(ServiceName, NonexistentMethod, channel, Metadata.Empty); try { - Calls.BlockingUnaryCall(call, "ABC", default(CancellationToken)); + Calls.BlockingUnaryCall(internalCall, "ABC", default(CancellationToken)); Assert.Fail(); } catch (RpcException e) -- cgit v1.2.3 From e66165dead7d4b26afa416143b32daad4bef9e36 Mon Sep 17 00:00:00 2001 From: Vijay Pai Date: Tue, 21 Jul 2015 21:11:38 +0000 Subject: Remove iterator-based test altogether --- test/cpp/common/secure_auth_context_test.cc | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/test/cpp/common/secure_auth_context_test.cc b/test/cpp/common/secure_auth_context_test.cc index fc8aa8f681..d0243a5432 100644 --- a/test/cpp/common/secure_auth_context_test.cc +++ b/test/cpp/common/secure_auth_context_test.cc @@ -92,26 +92,6 @@ TEST_F(SecureAuthContextTest, Iterators) { EXPECT_EQ("bar", p2.second); ++iter; EXPECT_EQ(context.end(), iter); - - int i = 0; - for (auto p = context.begin(); p != context.end(); p++) { - switch (i++) { - case 0: - EXPECT_EQ("name", (*p).first); - EXPECT_EQ("chapi", (*p).second); - break; - case 1: - EXPECT_EQ("name", (*p).first); - EXPECT_EQ("chapo", (*p).second); - break; - case 2: - EXPECT_EQ("foo", (*p).first); - EXPECT_EQ("bar", (*p).second); - break; - default: - EXPECT_TRUE(0); - } - } } } // namespace -- cgit v1.2.3 From 198a1ad966cb38ccc1697961914fa5b8b854df2f Mon Sep 17 00:00:00 2001 From: murgatroid99 Date: Tue, 21 Jul 2015 14:27:56 -0700 Subject: Added user-agent setting code, and a test for it --- src/node/src/client.js | 6 +++++- src/node/test/surface_test.js | 10 ++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/node/src/client.js b/src/node/src/client.js index b7bad949d4..06a0f3637f 100644 --- a/src/node/src/client.js +++ b/src/node/src/client.js @@ -47,6 +47,7 @@ var Readable = stream.Readable; var Writable = stream.Writable; var Duplex = stream.Duplex; var util = require('util'); +var version = require('../package.json').version; util.inherits(ClientWritableStream, Writable); @@ -517,7 +518,10 @@ function makeClientConstructor(methods, serviceName) { callback(null, metadata); }; } - + if (!options) { + options = {}; + } + options.GRPC_ARG_PRIMARY_USER_AGENT_STRING = 'grpc-node/' + version; this.server_address = address.replace(/\/$/, ''); this.channel = new grpc.Channel(address, options); this.auth_uri = this.server_address + '/' + serviceName; diff --git a/src/node/test/surface_test.js b/src/node/test/surface_test.js index 18178e49e4..3cb68f8cd8 100644 --- a/src/node/test/surface_test.js +++ b/src/node/test/surface_test.js @@ -258,6 +258,16 @@ describe('Echo metadata', function() { }); call.end(); }); + it('shows the correct user-agent string', function(done) { + var version = require('../package.json').version; + var call = client.unary({}, function(err, data) { + assert.ifError(err); + }, {key: ['value']}); + call.on('metadata', function(metadata) { + assert(_.startsWith(metadata['user-agent'], 'grpc-node/' + version)); + done(); + }); + }); }); describe('Other conditions', function() { var client; -- cgit v1.2.3 From 7717202c2b2f451bfc92cd475b85b106cc07374a Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Tue, 21 Jul 2015 18:28:16 -0700 Subject: fix crash caused by wrong size of MetadataEntryStruct --- .../Grpc.Core/Internal/MetadataArraySafeHandle.cs | 29 +++++++++------------- src/csharp/ext/grpc_csharp_ext.c | 18 +++++++++++--- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs b/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs index ede85fb7f2..5dcc9f06fa 100644 --- a/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs +++ b/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs @@ -49,7 +49,13 @@ namespace Grpc.Core.Internal static extern UIntPtr grpcsharp_metadata_array_count(IntPtr metadataArray); [DllImport("grpc_csharp_ext.dll")] - static extern MetadataEntryStruct grpcsharp_metadata_array_get(IntPtr metadataArray, UIntPtr index); + static extern IntPtr grpcsharp_metadata_array_get_key(IntPtr metadataArray, UIntPtr index); + + [DllImport("grpc_csharp_ext.dll")] + static extern IntPtr grpcsharp_metadata_array_get_value(IntPtr metadataArray, UIntPtr index); + + [DllImport("grpc_csharp_ext.dll")] + static extern UIntPtr grpcsharp_metadata_array_get_value_length(IntPtr metadataArray, UIntPtr index); [DllImport("grpc_csharp_ext.dll")] static extern void grpcsharp_metadata_array_destroy_full(IntPtr array); @@ -82,12 +88,12 @@ namespace Grpc.Core.Internal ulong count = grpcsharp_metadata_array_count(metadataArray).ToUInt64(); var metadata = new Metadata(); - for (ulong index = 0; index < count; index ++) + for (ulong i = 0; i < count; i ++) { - var rawEntry = grpcsharp_metadata_array_get(metadataArray, new UIntPtr(index)); - string key = Marshal.PtrToStringAnsi(rawEntry.key); - var bytes = new byte[rawEntry.valueLength.ToUInt64()]; - Marshal.Copy(rawEntry.value, bytes, 0, bytes.Length); + var index = new UIntPtr(i); + string key = Marshal.PtrToStringAnsi(grpcsharp_metadata_array_get_key(metadataArray, index)); + var bytes = new byte[grpcsharp_metadata_array_get_value_length(metadataArray, index).ToUInt64()]; + Marshal.Copy(grpcsharp_metadata_array_get_value(metadataArray, index), bytes, 0, bytes.Length); metadata.Add(new Metadata.Entry(key, bytes)); } return metadata; @@ -106,16 +112,5 @@ namespace Grpc.Core.Internal grpcsharp_metadata_array_destroy_full(handle); return true; } - - /// - /// gprc_metadata from grpc/grpc.h - /// - [StructLayout(LayoutKind.Sequential)] - private struct MetadataEntryStruct - { - public IntPtr key; // const char* - public IntPtr value; // const char* - public UIntPtr valueLength; - } } } diff --git a/src/csharp/ext/grpc_csharp_ext.c b/src/csharp/ext/grpc_csharp_ext.c index bd0a259593..682521446f 100644 --- a/src/csharp/ext/grpc_csharp_ext.c +++ b/src/csharp/ext/grpc_csharp_ext.c @@ -172,10 +172,22 @@ grpcsharp_metadata_array_count(grpc_metadata_array *array) { return (gpr_intptr) array->count; } -GPR_EXPORT grpc_metadata GPR_CALLTYPE -grpcsharp_metadata_array_get(grpc_metadata_array *array, size_t index) { +GPR_EXPORT const char *GPR_CALLTYPE +grpcsharp_metadata_array_get_key(grpc_metadata_array *array, size_t index) { + GPR_ASSERT(index < array->count); + return array->metadata[index].key; +} + +GPR_EXPORT const char *GPR_CALLTYPE +grpcsharp_metadata_array_get_value(grpc_metadata_array *array, size_t index) { + GPR_ASSERT(index < array->count); + return array->metadata[index].value; +} + +GPR_EXPORT gpr_intptr GPR_CALLTYPE +grpcsharp_metadata_array_get_value_length(grpc_metadata_array *array, size_t index) { GPR_ASSERT(index < array->count); - return array->metadata[index]; + return (gpr_intptr) array->metadata[index].value_length; } /* Move contents of metadata array */ -- cgit v1.2.3 From d601ff5e9d6a2f74d9ead98c54ea6844cb087765 Mon Sep 17 00:00:00 2001 From: murgatroid99 Date: Wed, 22 Jul 2015 09:58:38 -0700 Subject: Fixed setting user-agent string --- src/node/src/client.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/node/src/client.js b/src/node/src/client.js index 06a0f3637f..da6327b432 100644 --- a/src/node/src/client.js +++ b/src/node/src/client.js @@ -521,9 +521,9 @@ function makeClientConstructor(methods, serviceName) { if (!options) { options = {}; } - options.GRPC_ARG_PRIMARY_USER_AGENT_STRING = 'grpc-node/' + version; - this.server_address = address.replace(/\/$/, ''); + options['grpc.primary_user_agent'] = 'grpc-node/' + version; this.channel = new grpc.Channel(address, options); + this.server_address = address.replace(/\/$/, ''); this.auth_uri = this.server_address + '/' + serviceName; this.updateMetadata = updateMetadata; } -- cgit v1.2.3 From 766d72b1c0c030ed56ac6453e3cf9b412ffc72ba Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Tue, 21 Jul 2015 20:09:25 -0700 Subject: set primary user agent by C# channel --- src/csharp/Grpc.Core.Tests/ClientServerTest.cs | 20 +++++++++++++-- src/csharp/Grpc.Core/Channel.cs | 25 ++++++++++++++++-- src/csharp/Grpc.Core/ChannelOptions.cs | 30 ++++++++++++++-------- src/csharp/Grpc.Core/Grpc.Core.csproj | 1 + src/csharp/Grpc.Core/Server.cs | 4 ++- src/csharp/Grpc.Core/Version.cs | 2 +- src/csharp/Grpc.Core/VersionInfo.cs | 15 +++++++++++ .../Grpc.IntegrationTesting.Client.csproj | 6 +++-- .../Grpc.IntegrationTesting.Server.csproj | 6 +++-- 9 files changed, 88 insertions(+), 21 deletions(-) create mode 100644 src/csharp/Grpc.Core/VersionInfo.cs diff --git a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs index d82a985f0c..8775c446f7 100644 --- a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs +++ b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs @@ -33,6 +33,7 @@ using System; using System.Diagnostics; +using System.Linq; using System.Threading; using System.Threading.Tasks; using Grpc.Core; @@ -268,12 +269,27 @@ namespace Grpc.Core.Tests } } + [Test] + public void UserAgentStringPresent() + { + var internalCall = new Call(ServiceName, EchoMethod, channel, Metadata.Empty); + string userAgent = Calls.BlockingUnaryCall(internalCall, "RETURN-USER-AGENT", CancellationToken.None); + Assert.IsTrue(userAgent.StartsWith("grpc-csharp/")); + } + private static async Task EchoHandler(string request, ServerCallContext context) { foreach (Metadata.Entry metadataEntry in context.RequestHeaders) { - Console.WriteLine("Echoing header " + metadataEntry.Key + " as trailer"); - context.ResponseTrailers.Add(metadataEntry); + if (metadataEntry.Key != "user-agent") + { + context.ResponseTrailers.Add(metadataEntry); + } + } + + if (request == "RETURN-USER-AGENT") + { + return context.RequestHeaders.Where(entry => entry.Key == "user-agent").Single().Value; } if (request == "THROW") diff --git a/src/csharp/Grpc.Core/Channel.cs b/src/csharp/Grpc.Core/Channel.cs index 5baf260003..e5c6abd2cb 100644 --- a/src/csharp/Grpc.Core/Channel.cs +++ b/src/csharp/Grpc.Core/Channel.cs @@ -28,11 +28,14 @@ // (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.Runtime.InteropServices; using System.Threading; using System.Threading.Tasks; + using Grpc.Core.Internal; namespace Grpc.Core @@ -44,6 +47,7 @@ namespace Grpc.Core { readonly GrpcEnvironment environment; readonly ChannelSafeHandle handle; + readonly List options; readonly string target; bool disposed; @@ -57,7 +61,10 @@ namespace Grpc.Core public Channel(string host, Credentials credentials = null, IEnumerable options = null) { this.environment = GrpcEnvironment.GetInstance(); - using (ChannelArgsSafeHandle nativeChannelArgs = ChannelOptions.CreateChannelArgs(options)) + this.options = options != null ? new List(options) : new List(); + + EnsureUserAgentChannelOption(this.options); + using (ChannelArgsSafeHandle nativeChannelArgs = ChannelOptions.CreateChannelArgs(this.options)) { if (credentials != null) { @@ -71,7 +78,7 @@ namespace Grpc.Core this.handle = ChannelSafeHandle.Create(host, nativeChannelArgs); } } - this.target = GetOverridenTarget(host, options); + this.target = GetOverridenTarget(host, this.options); } /// @@ -141,6 +148,20 @@ namespace Grpc.Core } } + private static void EnsureUserAgentChannelOption(List options) + { + if (!options.Any((option) => option.Name == ChannelOptions.PrimaryUserAgentString)) + { + options.Add(new ChannelOption(ChannelOptions.PrimaryUserAgentString, GetUserAgentString())); + } + } + + private static string GetUserAgentString() + { + // TODO(jtattermusch): it would be useful to also provide .NET/mono version. + return string.Format("grpc-csharp/{0}", VersionInfo.CurrentVersion); + } + /// /// Look for SslTargetNameOverride option and return its value instead of originalTarget /// if found. diff --git a/src/csharp/Grpc.Core/ChannelOptions.cs b/src/csharp/Grpc.Core/ChannelOptions.cs index bc23bb59b1..9fe03d2805 100644 --- a/src/csharp/Grpc.Core/ChannelOptions.cs +++ b/src/csharp/Grpc.Core/ChannelOptions.cs @@ -115,41 +115,49 @@ namespace Grpc.Core } } + /// + /// Defines names of supported channel options. + /// public static class ChannelOptions { - // Override SSL target check. Only to be used for testing. + /// Override SSL target check. Only to be used for testing. public const string SslTargetNameOverride = "grpc.ssl_target_name_override"; - // Enable census for tracing and stats collection + /// Enable census for tracing and stats collection public const string Census = "grpc.census"; - // Maximum number of concurrent incoming streams to allow on a http2 connection + /// Maximum number of concurrent incoming streams to allow on a http2 connection public const string MaxConcurrentStreams = "grpc.max_concurrent_streams"; - // Maximum message length that the channel can receive + /// Maximum message length that the channel can receive public const string MaxMessageLength = "grpc.max_message_length"; - // Initial sequence number for http2 transports + /// Initial sequence number for http2 transports public const string Http2InitialSequenceNumber = "grpc.http2.initial_sequence_number"; + /// Primary user agent: goes at the start of the user-agent metadata + public const string PrimaryUserAgentString = "grpc.primary_user_agent"; + + /// Secondary user agent: goes at the end of the user-agent metadata + public const string SecondaryUserAgentString = "grpc.secondary_user_agent"; + /// /// Creates native object for a collection of channel options. /// /// The native channel arguments. - internal static ChannelArgsSafeHandle CreateChannelArgs(IEnumerable options) + internal static ChannelArgsSafeHandle CreateChannelArgs(List options) { - if (options == null) + if (options == null || options.Count == 0) { return ChannelArgsSafeHandle.CreateNull(); } - var optionList = new List(options); // It's better to do defensive copy ChannelArgsSafeHandle nativeArgs = null; try { - nativeArgs = ChannelArgsSafeHandle.Create(optionList.Count); - for (int i = 0; i < optionList.Count; i++) + nativeArgs = ChannelArgsSafeHandle.Create(options.Count); + for (int i = 0; i < options.Count; i++) { - var option = optionList[i]; + var option = options[i]; if (option.Type == ChannelOption.OptionType.Integer) { nativeArgs.SetInteger(i, option.Name, option.IntValue); diff --git a/src/csharp/Grpc.Core/Grpc.Core.csproj b/src/csharp/Grpc.Core/Grpc.Core.csproj index 3b9b3b6f7e..fd68b91851 100644 --- a/src/csharp/Grpc.Core/Grpc.Core.csproj +++ b/src/csharp/Grpc.Core/Grpc.Core.csproj @@ -102,6 +102,7 @@ + diff --git a/src/csharp/Grpc.Core/Server.cs b/src/csharp/Grpc.Core/Server.cs index 7f9ec41486..fd30735359 100644 --- a/src/csharp/Grpc.Core/Server.cs +++ b/src/csharp/Grpc.Core/Server.cs @@ -53,6 +53,7 @@ namespace Grpc.Core public const int PickUnusedPort = 0; readonly GrpcEnvironment environment; + readonly List options; readonly ServerSafeHandle handle; readonly object myLock = new object(); @@ -69,7 +70,8 @@ namespace Grpc.Core public Server(IEnumerable options = null) { this.environment = GrpcEnvironment.GetInstance(); - using (var channelArgs = ChannelOptions.CreateChannelArgs(options)) + this.options = options != null ? new List(options) : new List(); + using (var channelArgs = ChannelOptions.CreateChannelArgs(this.options)) { this.handle = ServerSafeHandle.NewServer(environment.CompletionQueue, channelArgs); } diff --git a/src/csharp/Grpc.Core/Version.cs b/src/csharp/Grpc.Core/Version.cs index f1db1f6157..b5cb652945 100644 --- a/src/csharp/Grpc.Core/Version.cs +++ b/src/csharp/Grpc.Core/Version.cs @@ -2,4 +2,4 @@ using System.Reflection; using System.Runtime.CompilerServices; // The current version of gRPC C#. -[assembly: AssemblyVersion("0.6.0.*")] +[assembly: AssemblyVersion(Grpc.Core.VersionInfo.CurrentVersion + ".*")] diff --git a/src/csharp/Grpc.Core/VersionInfo.cs b/src/csharp/Grpc.Core/VersionInfo.cs new file mode 100644 index 0000000000..396cdb27fd --- /dev/null +++ b/src/csharp/Grpc.Core/VersionInfo.cs @@ -0,0 +1,15 @@ +using System.Reflection; +using System.Runtime.CompilerServices; + +namespace Grpc.Core +{ + public static class VersionInfo + { + /// + /// Current version of gRPC + /// + public const string CurrentVersion = "0.6.0"; + } +} + + diff --git a/src/csharp/Grpc.IntegrationTesting.Client/Grpc.IntegrationTesting.Client.csproj b/src/csharp/Grpc.IntegrationTesting.Client/Grpc.IntegrationTesting.Client.csproj index 328acb5b47..dc1d0a44c0 100644 --- a/src/csharp/Grpc.IntegrationTesting.Client/Grpc.IntegrationTesting.Client.csproj +++ b/src/csharp/Grpc.IntegrationTesting.Client/Grpc.IntegrationTesting.Client.csproj @@ -3,8 +3,6 @@ Debug x86 - 10.0.0 - 2.0 {3D166931-BA2D-416E-95A3-D36E8F6E90B9} Exe Grpc.IntegrationTesting.Client @@ -48,6 +46,10 @@ {C61154BA-DD4A-4838-8420-0162A28925E0} Grpc.IntegrationTesting + + {CCC4440E-49F7-4790-B0AF-FEABB0837AE7} + Grpc.Core + diff --git a/src/csharp/Grpc.IntegrationTesting.Server/Grpc.IntegrationTesting.Server.csproj b/src/csharp/Grpc.IntegrationTesting.Server/Grpc.IntegrationTesting.Server.csproj index ae184c1dc7..f03c8f3ce3 100644 --- a/src/csharp/Grpc.IntegrationTesting.Server/Grpc.IntegrationTesting.Server.csproj +++ b/src/csharp/Grpc.IntegrationTesting.Server/Grpc.IntegrationTesting.Server.csproj @@ -3,8 +3,6 @@ Debug x86 - 10.0.0 - 2.0 {A654F3B8-E859-4E6A-B30D-227527DBEF0D} Exe Grpc.IntegrationTesting.Server @@ -48,6 +46,10 @@ {C61154BA-DD4A-4838-8420-0162A28925E0} Grpc.IntegrationTesting + + {CCC4440E-49F7-4790-B0AF-FEABB0837AE7} + Grpc.Core + -- cgit v1.2.3 From b146ef62c46ef136a991ed08c263c31b24979cc5 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Wed, 22 Jul 2015 11:52:40 -0700 Subject: added generated file Health.cs to stylecop ignore --- src/csharp/Grpc.HealthCheck/Settings.StyleCop | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/csharp/Grpc.HealthCheck/Settings.StyleCop diff --git a/src/csharp/Grpc.HealthCheck/Settings.StyleCop b/src/csharp/Grpc.HealthCheck/Settings.StyleCop new file mode 100644 index 0000000000..2942add962 --- /dev/null +++ b/src/csharp/Grpc.HealthCheck/Settings.StyleCop @@ -0,0 +1,10 @@ + + + Health.cs + + + False + + + + -- cgit v1.2.3 From ae017092ada35fb1297063d3b531b3cad580a461 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Wed, 22 Jul 2015 11:59:13 -0700 Subject: fix stylecop warnings --- src/csharp/Grpc.Core.Tests/ClientServerTest.cs | 2 +- src/csharp/Grpc.Core/AsyncDuplexStreamingCall.cs | 1 - src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs | 2 +- src/csharp/Grpc.Core/Internal/ServerCallHandler.cs | 1 - src/csharp/Grpc.Core/Metadata.cs | 1 - src/csharp/Grpc.Core/ServerCallContext.cs | 5 ++--- src/csharp/Grpc.Core/VersionInfo.cs | 2 -- src/csharp/Grpc.IntegrationTesting/InteropClient.cs | 2 +- 8 files changed, 5 insertions(+), 11 deletions(-) diff --git a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs index 8775c446f7..8ba2c8a9a2 100644 --- a/src/csharp/Grpc.Core.Tests/ClientServerTest.cs +++ b/src/csharp/Grpc.Core.Tests/ClientServerTest.cs @@ -219,7 +219,7 @@ namespace Grpc.Core.Tests var headers = new Metadata { new Metadata.Entry("asciiHeader", "abcdefg"), - new Metadata.Entry("binaryHeader-bin", new byte[] { 1, 2, 3, 0, 0xff } ), + new Metadata.Entry("binaryHeader-bin", new byte[] { 1, 2, 3, 0, 0xff }), }; var internalCall = new Call(ServiceName, EchoMethod, channel, headers); var call = Calls.AsyncUnaryCall(internalCall, "ABC", CancellationToken.None); diff --git a/src/csharp/Grpc.Core/AsyncDuplexStreamingCall.cs b/src/csharp/Grpc.Core/AsyncDuplexStreamingCall.cs index d76272c59b..0979de606f 100644 --- a/src/csharp/Grpc.Core/AsyncDuplexStreamingCall.cs +++ b/src/csharp/Grpc.Core/AsyncDuplexStreamingCall.cs @@ -56,7 +56,6 @@ namespace Grpc.Core this.getTrailersFunc = getTrailersFunc; this.disposeAction = disposeAction; } - /// /// Async stream to read streaming responses. diff --git a/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs b/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs index 5dcc9f06fa..427c16fac6 100644 --- a/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs +++ b/src/csharp/Grpc.Core/Internal/MetadataArraySafeHandle.cs @@ -88,7 +88,7 @@ namespace Grpc.Core.Internal ulong count = grpcsharp_metadata_array_count(metadataArray).ToUInt64(); var metadata = new Metadata(); - for (ulong i = 0; i < count; i ++) + for (ulong i = 0; i < count; i++) { var index = new UIntPtr(i); string key = Marshal.PtrToStringAnsi(grpcsharp_metadata_array_get_key(metadataArray, index)); diff --git a/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs b/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs index bcd438f969..3680b1e791 100644 --- a/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs +++ b/src/csharp/Grpc.Core/Internal/ServerCallHandler.cs @@ -179,7 +179,6 @@ namespace Grpc.Core.Internal var requestStream = new ServerRequestStream(asyncCall); var responseStream = new ServerResponseStream(asyncCall); - Status status; var context = HandlerUtils.NewContext(newRpc); try diff --git a/src/csharp/Grpc.Core/Metadata.cs b/src/csharp/Grpc.Core/Metadata.cs index 0c6fcbc0f8..2f308cbb11 100644 --- a/src/csharp/Grpc.Core/Metadata.cs +++ b/src/csharp/Grpc.Core/Metadata.cs @@ -225,7 +225,6 @@ namespace Grpc.Core { return string.Format("[Entry: key={0}, value={1}]", Key, Value); } - } } } diff --git a/src/csharp/Grpc.Core/ServerCallContext.cs b/src/csharp/Grpc.Core/ServerCallContext.cs index 4fec3dc676..17a2eefd07 100644 --- a/src/csharp/Grpc.Core/ServerCallContext.cs +++ b/src/csharp/Grpc.Core/ServerCallContext.cs @@ -45,16 +45,14 @@ namespace Grpc.Core { // TODO(jtattermusch): expose method to send initial metadata back to client - // TODO(jtattermusch): allow setting status and trailing metadata to send after handler completes. - private readonly string method; private readonly string host; private readonly DateTime deadline; private readonly Metadata requestHeaders; private readonly CancellationToken cancellationToken; + private readonly Metadata responseTrailers = new Metadata(); private Status status = Status.DefaultSuccess; - private readonly Metadata responseTrailers = new Metadata(); public ServerCallContext(string method, string host, DateTime deadline, Metadata requestHeaders, CancellationToken cancellationToken) { @@ -127,6 +125,7 @@ namespace Grpc.Core { return this.status; } + set { status = value; diff --git a/src/csharp/Grpc.Core/VersionInfo.cs b/src/csharp/Grpc.Core/VersionInfo.cs index 396cdb27fd..656a3d47bb 100644 --- a/src/csharp/Grpc.Core/VersionInfo.cs +++ b/src/csharp/Grpc.Core/VersionInfo.cs @@ -11,5 +11,3 @@ namespace Grpc.Core public const string CurrentVersion = "0.6.0"; } } - - diff --git a/src/csharp/Grpc.IntegrationTesting/InteropClient.cs b/src/csharp/Grpc.IntegrationTesting/InteropClient.cs index 2746dc945e..ce255f9423 100644 --- a/src/csharp/Grpc.IntegrationTesting/InteropClient.cs +++ b/src/csharp/Grpc.IntegrationTesting/InteropClient.cs @@ -399,7 +399,7 @@ namespace Grpc.IntegrationTesting .SetFillOauthScope(true) .Build(); - var response = client.UnaryCall(request, headers: new Metadata { new Metadata.Entry("Authorization", "Bearer " + oauth2Token) } ); + var response = client.UnaryCall(request, headers: new Metadata { new Metadata.Entry("Authorization", "Bearer " + oauth2Token) }); Assert.AreEqual(AuthScopeResponse, response.OauthScope); Assert.AreEqual(ServiceAccountUser, response.Username); -- cgit v1.2.3 From f64825f24b796254ef79afcb9a9e451ac09c7adb Mon Sep 17 00:00:00 2001 From: Stanley Cheung Date: Wed, 22 Jul 2015 09:55:30 -0700 Subject: run homebrew installation script on macos jenkins --- tools/jenkins/run_distribution.sh | 98 ++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 53 deletions(-) diff --git a/tools/jenkins/run_distribution.sh b/tools/jenkins/run_distribution.sh index eea25b62e6..e5281adcf4 100755 --- a/tools/jenkins/run_distribution.sh +++ b/tools/jenkins/run_distribution.sh @@ -32,6 +32,15 @@ # linuxbrew installation of a selected language set -ex +if [ "$language" == "core" ]; then + command="curl -fsSL https://goo.gl/getgrpc | bash -" +elif [[ "python nodejs ruby php" =~ "$language" ]]; then + command="curl -fsSL https://goo.gl/getgrpc | bash -s $language" +else + echo "unsupported language $language" + exit 1 +fi + if [ "$platform" == "linux" ]; then if [ "$dist_channel" == "homebrew" ]; then @@ -42,15 +51,6 @@ if [ "$platform" == "linux" ]; then # build docker image, contains all pre-requisites docker build -t $DOCKER_IMAGE_NAME tools/jenkins/grpc_linuxbrew - if [ "$language" == "core" ]; then - command="curl -fsSL https://goo.gl/getgrpc | bash -" - elif [[ "python nodejs ruby php" =~ "$language" ]]; then - command="curl -fsSL https://goo.gl/getgrpc | bash -s $language" - else - echo "unsupported language $language" - exit 1 - fi - # run per-language homebrew installation script docker run $DOCKER_IMAGE_NAME bash -l \ -c "nvm use 0.12; \ @@ -66,67 +66,60 @@ if [ "$platform" == "linux" ]; then elif [ "$platform" == "macos" ]; then if [ "$dist_channel" == "homebrew" ]; then - # system installed homebrew, don't interfere + echo "Formulas installed by system-wide homebrew (before)" brew list -l + # Save the original PATH so that we can run the system `brew` command + # again at the end of the script + export ORIGINAL_PATH=$PATH + # Set up temp directories for test installation of homebrew brew_root=/tmp/homebrew-test-$language rm -rf $brew_root mkdir -p $brew_root git clone https://github.com/Homebrew/homebrew.git $brew_root - # Install grpc via homebrew - # - # The temp $PATH env variable makes sure we are operating at the right copy of - # temp homebrew installation, and do not interfere with the system's main brew - # installation. - # - # TODO: replace the next section with the actual homebrew installation script - # i.e. curl -fsSL https://goo.gl/getgrpc | bash -s $language - # need to resolve a bunch of environment and privilege issue on the jenkins - # mac machine itself - export OLD_PATH=$PATH + # Make sure we are operating at the right copy of temp homebrew + # installation export PATH=$brew_root/bin:$PATH - cd $brew_root - brew tap homebrew/dupes - brew install zlib - brew install openssl - brew tap grpc/grpc - brew install --without-python google-protobuf - brew install grpc - brew list -l - # Install per-language modules/extensions on top of core grpc - # - # If a command below needs root access, the binary had been added to - # /etc/sudoers. This step needs to be repeated if we add more mac instances - # to our jenkins project. - # - # Examples (lines that needed to be added to /etc/sudoers): - # + Defaults env_keep += "CFLAGS CXXFLAGS LDFLAGS enable_grpc" - # + jenkinsnode1 ALL=(ALL) NOPASSWD: /usr/bin/pecl, /usr/local/bin/pip, - # + /usr/local/bin/npm + # Set up right environment for each language + case $language in + *python*) + rm -rf jenkins_python_venv + virtualenv jenkins_python_venv + source jenkins_python_venv/bin/activate + ;; + *nodejs*) + export PATH=$HOME/.nvm/versions/node/v0.12.7/bin:$PATH + ;; + *php*) + export CFLAGS="-Wno-parentheses-equality" + ;; + *) + ;; + esac + + # Run our homebrew installation script + bash -c "$command" + + # Uninstall / clean up per-language modules/extensions after the test case $language in *core*) ;; *python*) - sudo CFLAGS=-I$brew_root/include LDFLAGS=-L$brew_root/lib pip install grpcio - pip list | grep grpcio - echo 'y' | sudo pip uninstall grpcio + deactivate + rm -rf jenkins_python_venv ;; *nodejs*) - sudo CXXFLAGS=-I$brew_root/include LDFLAGS=-L$brew_root/lib npm install grpc - npm list | grep grpc - sudo npm uninstall grpc + npm list -g | grep grpc + npm uninstall -g grpc ;; *ruby*) - gem install grpc -- --with-grpc-dir=$brew_root gem list | grep grpc gem uninstall grpc ;; *php*) - sudo enable_grpc=$brew_root CFLAGS="-Wno-parentheses-equality" pecl install grpc-alpha - pecl list | grep grpc - sudo pecl uninstall grpc + rm grpc.so ;; *) echo "Unsupported language $language" @@ -134,12 +127,11 @@ elif [ "$platform" == "macos" ]; then ;; esac - # clean up - cd ~/ + # Clean up rm -rf $brew_root - # Make sure the system brew installation is still unaffected - export PATH=$OLD_PATH + echo "Formulas installed by system-wide homebrew (after, should be unaffected)" + export PATH=$ORIGINAL_PATH brew list -l else -- cgit v1.2.3 From b6d613730f2b0d8f47973f7be578c3665ec1365c Mon Sep 17 00:00:00 2001 From: Masood Malekghassemi Date: Wed, 22 Jul 2015 14:04:45 -0700 Subject: Fix Python C89 pedantry --- src/python/src/grpc/_adapter/_c/utility.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/python/src/grpc/_adapter/_c/utility.c b/src/python/src/grpc/_adapter/_c/utility.c index 000c8d0c38..d9f911a41a 100644 --- a/src/python/src/grpc/_adapter/_c/utility.c +++ b/src/python/src/grpc/_adapter/_c/utility.c @@ -489,10 +489,10 @@ PyObject *pygrpc_cast_metadata_array_to_pyseq(grpc_metadata_array metadata) { void pygrpc_byte_buffer_to_bytes( grpc_byte_buffer *buffer, char **result, size_t *result_size) { grpc_byte_buffer_reader reader; - grpc_byte_buffer_reader_init(&reader, buffer); gpr_slice slice; char *read_result = NULL; size_t size = 0; + grpc_byte_buffer_reader_init(&reader, buffer); while (grpc_byte_buffer_reader_next(&reader, &slice)) { read_result = gpr_realloc(read_result, size + GPR_SLICE_LENGTH(slice)); memcpy(read_result + size, GPR_SLICE_START_PTR(slice), -- cgit v1.2.3 From 030827426792139dc9b5b1ca883663459bddf174 Mon Sep 17 00:00:00 2001 From: Stanley Cheung Date: Wed, 22 Jul 2015 16:42:39 -0700 Subject: fix ruby gem path --- tools/jenkins/run_distribution.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/jenkins/run_distribution.sh b/tools/jenkins/run_distribution.sh index e5281adcf4..7c306de590 100755 --- a/tools/jenkins/run_distribution.sh +++ b/tools/jenkins/run_distribution.sh @@ -93,6 +93,9 @@ elif [ "$platform" == "macos" ]; then *nodejs*) export PATH=$HOME/.nvm/versions/node/v0.12.7/bin:$PATH ;; + *ruby*) + export PATH=/usr/local/rvm/rubies/ruby-2.2.1/bin:$PATH + ;; *php*) export CFLAGS="-Wno-parentheses-equality" ;; -- cgit v1.2.3 From 6159c07709ea23f2a2452c9ad940f7d7f515cd54 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Wed, 22 Jul 2015 17:01:54 -0700 Subject: Fix interop tests by ensuring non-http-special metadata precedes other metadata --- src/core/channel/compress_filter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/channel/compress_filter.c b/src/core/channel/compress_filter.c index 14cb3da62d..bf02f9296f 100644 --- a/src/core/channel/compress_filter.c +++ b/src/core/channel/compress_filter.c @@ -200,7 +200,7 @@ static void process_send_ops(grpc_call_element *elem, channeld->default_compression_algorithm; calld->has_compression_algorithm = 1; /* GPR_TRUE */ } - grpc_metadata_batch_add_head( + grpc_metadata_batch_add_tail( &(sop->data.metadata), &calld->compression_algorithm_storage, grpc_mdelem_ref(channeld->mdelem_compression_algorithms [calld->compression_algorithm])); -- cgit v1.2.3 From d9ddc77ff0fb5b9e6a5062e73484ca5650d82afa Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Fri, 10 Jul 2015 13:00:05 -0700 Subject: Filter out reserved metadata so that applications cant mess us up --- src/core/channel/http_client_filter.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/core/channel/http_client_filter.c b/src/core/channel/http_client_filter.c index 63e4912397..3a2a479b79 100644 --- a/src/core/channel/http_client_filter.c +++ b/src/core/channel/http_client_filter.c @@ -92,6 +92,18 @@ static void hc_on_recv(void *user_data, int success) { calld->on_done_recv->cb(calld->on_done_recv->cb_arg, success); } +static grpc_mdelem *client_strip_filter(void *user_data, grpc_mdelem *md) { + grpc_call_element *elem = user_data; + channel_data *channeld = elem->channel_data; + /* eat the things we'd like to set ourselves */ + if (md->key == channeld->method->key) return NULL; + if (md->key == channeld->scheme->key) return NULL; + if (md->key == channeld->te_trailers->key) return NULL; + if (md->key == channeld->content_type->key) return NULL; + if (md->key == channeld->user_agent->key) return NULL; + return md; +} + static void hc_mutate_op(grpc_call_element *elem, grpc_transport_stream_op *op) { /* grab pointers to our data from the call element */ @@ -105,6 +117,7 @@ static void hc_mutate_op(grpc_call_element *elem, grpc_stream_op *op = &ops[i]; if (op->type != GRPC_OP_METADATA) continue; calld->sent_initial_metadata = 1; + grpc_metadata_batch_filter(&op->data.metadata, client_strip_filter, elem); /* Send : prefixed headers, which have to be before any application layer headers. */ grpc_metadata_batch_add_head(&op->data.metadata, &calld->method, -- cgit v1.2.3 From 123e5d883fd857382969aafae1ef9314cdb4cd51 Mon Sep 17 00:00:00 2001 From: Jorge Canizales Date: Wed, 22 Jul 2015 21:11:41 -0700 Subject: Fix typo in gtest install instructions for Mac --- INSTALL | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/INSTALL b/INSTALL index 5edb5e6db2..8a0a98ad2e 100644 --- a/INSTALL +++ b/INSTALL @@ -117,7 +117,7 @@ most Mac installations. Do the "git submodule" command listed above. Then execute the following for all the needed build dependencies $ sudo /opt/local/bin/port install autoconf automake libtool gflags cmake - $ mkdir ~/gtest + $ mkdir ~/gtest-svn $ svn checkout http://googletest.googlecode.com/svn/trunk/ gtest-svn $ mkdir mybuild $ cd mybuild -- cgit v1.2.3 From f4f6bc2c77c488de5b2515002e07a427c2fdd873 Mon Sep 17 00:00:00 2001 From: Stanley Cheung Date: Wed, 22 Jul 2015 17:07:55 -0700 Subject: Minor jenkins script cleanjup --- tools/jenkins/run_distribution.sh | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/tools/jenkins/run_distribution.sh b/tools/jenkins/run_distribution.sh index 7c306de590..49b7d306d1 100755 --- a/tools/jenkins/run_distribution.sh +++ b/tools/jenkins/run_distribution.sh @@ -32,6 +32,8 @@ # linuxbrew installation of a selected language set -ex +# Our homebrew installation script command, per language +# Can be used in both linux and macos if [ "$language" == "core" ]; then command="curl -fsSL https://goo.gl/getgrpc | bash -" elif [[ "python nodejs ruby php" =~ "$language" ]]; then @@ -66,6 +68,7 @@ if [ "$platform" == "linux" ]; then elif [ "$platform" == "macos" ]; then if [ "$dist_channel" == "homebrew" ]; then + echo "Formulas installed by system-wide homebrew (before)" brew list -l @@ -99,8 +102,6 @@ elif [ "$platform" == "macos" ]; then *php*) export CFLAGS="-Wno-parentheses-equality" ;; - *) - ;; esac # Run our homebrew installation script @@ -108,7 +109,6 @@ elif [ "$platform" == "macos" ]; then # Uninstall / clean up per-language modules/extensions after the test case $language in - *core*) ;; *python*) deactivate rm -rf jenkins_python_venv @@ -124,10 +124,6 @@ elif [ "$platform" == "macos" ]; then *php*) rm grpc.so ;; - *) - echo "Unsupported language $language" - exit 1 - ;; esac # Clean up -- cgit v1.2.3