diff --git a/BUILD b/BUILD index 71031f7db7a..aae25b18ed3 100644 --- a/BUILD +++ b/BUILD @@ -260,13 +260,10 @@ GRPCXX_PUBLIC_HDRS = [ "include/grpcpp/server_context.h", "include/grpcpp/server_posix.h", "include/grpcpp/support/async_stream.h", - "include/grpcpp/support/async_stream_impl.h", "include/grpcpp/support/async_unary_call.h", - "include/grpcpp/support/async_unary_call_impl.h", "include/grpcpp/support/byte_buffer.h", "include/grpcpp/support/channel_arguments.h", "include/grpcpp/support/client_callback.h", - "include/grpcpp/support/client_callback_impl.h", "include/grpcpp/support/client_interceptor.h", "include/grpcpp/support/config.h", "include/grpcpp/support/interceptor.h", @@ -275,7 +272,6 @@ GRPCXX_PUBLIC_HDRS = [ "include/grpcpp/support/proto_buffer_reader.h", "include/grpcpp/support/proto_buffer_writer.h", "include/grpcpp/support/server_callback.h", - "include/grpcpp/support/server_callback_impl.h", "include/grpcpp/support/server_interceptor.h", "include/grpcpp/support/slice.h", "include/grpcpp/support/status.h", @@ -283,7 +279,6 @@ GRPCXX_PUBLIC_HDRS = [ "include/grpcpp/support/string_ref.h", "include/grpcpp/support/stub_options.h", "include/grpcpp/support/sync_stream.h", - "include/grpcpp/support/sync_stream_impl.h", "include/grpcpp/support/time.h", "include/grpcpp/support/validate_service_config.h", ] @@ -2206,9 +2201,7 @@ grpc_cc_library( "include/grpc++/impl/codegen/time.h", "include/grpcpp/impl/codegen/async_generic_service.h", "include/grpcpp/impl/codegen/async_stream.h", - "include/grpcpp/impl/codegen/async_stream_impl.h", "include/grpcpp/impl/codegen/async_unary_call.h", - "include/grpcpp/impl/codegen/async_unary_call_impl.h", "include/grpcpp/impl/codegen/byte_buffer.h", "include/grpcpp/impl/codegen/call.h", "include/grpcpp/impl/codegen/call_hook.h", @@ -2217,7 +2210,6 @@ grpc_cc_library( "include/grpcpp/impl/codegen/callback_common.h", "include/grpcpp/impl/codegen/channel_interface.h", "include/grpcpp/impl/codegen/client_callback.h", - "include/grpcpp/impl/codegen/client_callback_impl.h", "include/grpcpp/impl/codegen/client_context.h", "include/grpcpp/impl/codegen/client_interceptor.h", "include/grpcpp/impl/codegen/client_unary_call.h", @@ -2234,14 +2226,12 @@ grpc_cc_library( "include/grpcpp/impl/codegen/message_allocator.h", "include/grpcpp/impl/codegen/metadata_map.h", "include/grpcpp/impl/codegen/method_handler.h", - "include/grpcpp/impl/codegen/method_handler_impl.h", "include/grpcpp/impl/codegen/rpc_method.h", "include/grpcpp/impl/codegen/rpc_service_method.h", "include/grpcpp/impl/codegen/security/auth_context.h", "include/grpcpp/impl/codegen/serialization_traits.h", "include/grpcpp/impl/codegen/server_callback.h", "include/grpcpp/impl/codegen/server_callback_handlers.h", - "include/grpcpp/impl/codegen/server_callback_impl.h", "include/grpcpp/impl/codegen/server_context.h", "include/grpcpp/impl/codegen/server_interceptor.h", "include/grpcpp/impl/codegen/server_interface.h", @@ -2252,7 +2242,6 @@ grpc_cc_library( "include/grpcpp/impl/codegen/string_ref.h", "include/grpcpp/impl/codegen/stub_options.h", "include/grpcpp/impl/codegen/sync_stream.h", - "include/grpcpp/impl/codegen/sync_stream_impl.h", "include/grpcpp/impl/codegen/time.h", ], deps = [ diff --git a/BUILD.gn b/BUILD.gn index 36dae38a5a5..f1c05da02d5 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1132,9 +1132,7 @@ config("grpc_config") { "include/grpcpp/impl/client_unary_call.h", "include/grpcpp/impl/codegen/async_generic_service.h", "include/grpcpp/impl/codegen/async_stream.h", - "include/grpcpp/impl/codegen/async_stream_impl.h", "include/grpcpp/impl/codegen/async_unary_call.h", - "include/grpcpp/impl/codegen/async_unary_call_impl.h", "include/grpcpp/impl/codegen/byte_buffer.h", "include/grpcpp/impl/codegen/call.h", "include/grpcpp/impl/codegen/call_hook.h", @@ -1143,7 +1141,6 @@ config("grpc_config") { "include/grpcpp/impl/codegen/callback_common.h", "include/grpcpp/impl/codegen/channel_interface.h", "include/grpcpp/impl/codegen/client_callback.h", - "include/grpcpp/impl/codegen/client_callback_impl.h", "include/grpcpp/impl/codegen/client_context.h", "include/grpcpp/impl/codegen/client_interceptor.h", "include/grpcpp/impl/codegen/client_unary_call.h", @@ -1162,7 +1159,6 @@ config("grpc_config") { "include/grpcpp/impl/codegen/message_allocator.h", "include/grpcpp/impl/codegen/metadata_map.h", "include/grpcpp/impl/codegen/method_handler.h", - "include/grpcpp/impl/codegen/method_handler_impl.h", "include/grpcpp/impl/codegen/proto_buffer_reader.h", "include/grpcpp/impl/codegen/proto_buffer_writer.h", "include/grpcpp/impl/codegen/proto_utils.h", @@ -1172,7 +1168,6 @@ config("grpc_config") { "include/grpcpp/impl/codegen/serialization_traits.h", "include/grpcpp/impl/codegen/server_callback.h", "include/grpcpp/impl/codegen/server_callback_handlers.h", - "include/grpcpp/impl/codegen/server_callback_impl.h", "include/grpcpp/impl/codegen/server_context.h", "include/grpcpp/impl/codegen/server_interceptor.h", "include/grpcpp/impl/codegen/server_interface.h", @@ -1184,7 +1179,6 @@ config("grpc_config") { "include/grpcpp/impl/codegen/stub_options.h", "include/grpcpp/impl/codegen/sync.h", "include/grpcpp/impl/codegen/sync_stream.h", - "include/grpcpp/impl/codegen/sync_stream_impl.h", "include/grpcpp/impl/codegen/time.h", "include/grpcpp/impl/grpc_library.h", "include/grpcpp/impl/method_handler_impl.h", @@ -1208,13 +1202,10 @@ config("grpc_config") { "include/grpcpp/server_context.h", "include/grpcpp/server_posix.h", "include/grpcpp/support/async_stream.h", - "include/grpcpp/support/async_stream_impl.h", "include/grpcpp/support/async_unary_call.h", - "include/grpcpp/support/async_unary_call_impl.h", "include/grpcpp/support/byte_buffer.h", "include/grpcpp/support/channel_arguments.h", "include/grpcpp/support/client_callback.h", - "include/grpcpp/support/client_callback_impl.h", "include/grpcpp/support/client_interceptor.h", "include/grpcpp/support/config.h", "include/grpcpp/support/interceptor.h", @@ -1223,7 +1214,6 @@ config("grpc_config") { "include/grpcpp/support/proto_buffer_reader.h", "include/grpcpp/support/proto_buffer_writer.h", "include/grpcpp/support/server_callback.h", - "include/grpcpp/support/server_callback_impl.h", "include/grpcpp/support/server_interceptor.h", "include/grpcpp/support/slice.h", "include/grpcpp/support/status.h", @@ -1231,7 +1221,6 @@ config("grpc_config") { "include/grpcpp/support/string_ref.h", "include/grpcpp/support/stub_options.h", "include/grpcpp/support/sync_stream.h", - "include/grpcpp/support/sync_stream_impl.h", "include/grpcpp/support/time.h", "include/grpcpp/support/validate_service_config.h", "src/cpp/client/channel_cc.cc", diff --git a/CMakeLists.txt b/CMakeLists.txt index 0d655de3c50..35d78737639 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2719,9 +2719,7 @@ foreach(_hdr include/grpcpp/impl/client_unary_call.h include/grpcpp/impl/codegen/async_generic_service.h include/grpcpp/impl/codegen/async_stream.h - include/grpcpp/impl/codegen/async_stream_impl.h include/grpcpp/impl/codegen/async_unary_call.h - include/grpcpp/impl/codegen/async_unary_call_impl.h include/grpcpp/impl/codegen/byte_buffer.h include/grpcpp/impl/codegen/call.h include/grpcpp/impl/codegen/call_hook.h @@ -2730,7 +2728,6 @@ foreach(_hdr include/grpcpp/impl/codegen/callback_common.h include/grpcpp/impl/codegen/channel_interface.h include/grpcpp/impl/codegen/client_callback.h - include/grpcpp/impl/codegen/client_callback_impl.h include/grpcpp/impl/codegen/client_context.h include/grpcpp/impl/codegen/client_interceptor.h include/grpcpp/impl/codegen/client_unary_call.h @@ -2749,7 +2746,6 @@ foreach(_hdr include/grpcpp/impl/codegen/message_allocator.h include/grpcpp/impl/codegen/metadata_map.h include/grpcpp/impl/codegen/method_handler.h - include/grpcpp/impl/codegen/method_handler_impl.h include/grpcpp/impl/codegen/proto_buffer_reader.h include/grpcpp/impl/codegen/proto_buffer_writer.h include/grpcpp/impl/codegen/proto_utils.h @@ -2759,7 +2755,6 @@ foreach(_hdr include/grpcpp/impl/codegen/serialization_traits.h include/grpcpp/impl/codegen/server_callback.h include/grpcpp/impl/codegen/server_callback_handlers.h - include/grpcpp/impl/codegen/server_callback_impl.h include/grpcpp/impl/codegen/server_context.h include/grpcpp/impl/codegen/server_interceptor.h include/grpcpp/impl/codegen/server_interface.h @@ -2771,7 +2766,6 @@ foreach(_hdr include/grpcpp/impl/codegen/stub_options.h include/grpcpp/impl/codegen/sync.h include/grpcpp/impl/codegen/sync_stream.h - include/grpcpp/impl/codegen/sync_stream_impl.h include/grpcpp/impl/codegen/time.h include/grpcpp/impl/grpc_library.h include/grpcpp/impl/method_handler_impl.h @@ -2795,13 +2789,10 @@ foreach(_hdr include/grpcpp/server_context.h include/grpcpp/server_posix.h include/grpcpp/support/async_stream.h - include/grpcpp/support/async_stream_impl.h include/grpcpp/support/async_unary_call.h - include/grpcpp/support/async_unary_call_impl.h include/grpcpp/support/byte_buffer.h include/grpcpp/support/channel_arguments.h include/grpcpp/support/client_callback.h - include/grpcpp/support/client_callback_impl.h include/grpcpp/support/client_interceptor.h include/grpcpp/support/config.h include/grpcpp/support/interceptor.h @@ -2810,7 +2801,6 @@ foreach(_hdr include/grpcpp/support/proto_buffer_reader.h include/grpcpp/support/proto_buffer_writer.h include/grpcpp/support/server_callback.h - include/grpcpp/support/server_callback_impl.h include/grpcpp/support/server_interceptor.h include/grpcpp/support/slice.h include/grpcpp/support/status.h @@ -2818,7 +2808,6 @@ foreach(_hdr include/grpcpp/support/string_ref.h include/grpcpp/support/stub_options.h include/grpcpp/support/sync_stream.h - include/grpcpp/support/sync_stream_impl.h include/grpcpp/support/time.h include/grpcpp/support/validate_service_config.h ) @@ -3402,9 +3391,7 @@ foreach(_hdr include/grpcpp/impl/client_unary_call.h include/grpcpp/impl/codegen/async_generic_service.h include/grpcpp/impl/codegen/async_stream.h - include/grpcpp/impl/codegen/async_stream_impl.h include/grpcpp/impl/codegen/async_unary_call.h - include/grpcpp/impl/codegen/async_unary_call_impl.h include/grpcpp/impl/codegen/byte_buffer.h include/grpcpp/impl/codegen/call.h include/grpcpp/impl/codegen/call_hook.h @@ -3413,7 +3400,6 @@ foreach(_hdr include/grpcpp/impl/codegen/callback_common.h include/grpcpp/impl/codegen/channel_interface.h include/grpcpp/impl/codegen/client_callback.h - include/grpcpp/impl/codegen/client_callback_impl.h include/grpcpp/impl/codegen/client_context.h include/grpcpp/impl/codegen/client_interceptor.h include/grpcpp/impl/codegen/client_unary_call.h @@ -3432,7 +3418,6 @@ foreach(_hdr include/grpcpp/impl/codegen/message_allocator.h include/grpcpp/impl/codegen/metadata_map.h include/grpcpp/impl/codegen/method_handler.h - include/grpcpp/impl/codegen/method_handler_impl.h include/grpcpp/impl/codegen/proto_buffer_reader.h include/grpcpp/impl/codegen/proto_buffer_writer.h include/grpcpp/impl/codegen/proto_utils.h @@ -3442,7 +3427,6 @@ foreach(_hdr include/grpcpp/impl/codegen/serialization_traits.h include/grpcpp/impl/codegen/server_callback.h include/grpcpp/impl/codegen/server_callback_handlers.h - include/grpcpp/impl/codegen/server_callback_impl.h include/grpcpp/impl/codegen/server_context.h include/grpcpp/impl/codegen/server_interceptor.h include/grpcpp/impl/codegen/server_interface.h @@ -3454,7 +3438,6 @@ foreach(_hdr include/grpcpp/impl/codegen/stub_options.h include/grpcpp/impl/codegen/sync.h include/grpcpp/impl/codegen/sync_stream.h - include/grpcpp/impl/codegen/sync_stream_impl.h include/grpcpp/impl/codegen/time.h include/grpcpp/impl/grpc_library.h include/grpcpp/impl/method_handler_impl.h @@ -3478,13 +3461,10 @@ foreach(_hdr include/grpcpp/server_context.h include/grpcpp/server_posix.h include/grpcpp/support/async_stream.h - include/grpcpp/support/async_stream_impl.h include/grpcpp/support/async_unary_call.h - include/grpcpp/support/async_unary_call_impl.h include/grpcpp/support/byte_buffer.h include/grpcpp/support/channel_arguments.h include/grpcpp/support/client_callback.h - include/grpcpp/support/client_callback_impl.h include/grpcpp/support/client_interceptor.h include/grpcpp/support/config.h include/grpcpp/support/interceptor.h @@ -3493,7 +3473,6 @@ foreach(_hdr include/grpcpp/support/proto_buffer_reader.h include/grpcpp/support/proto_buffer_writer.h include/grpcpp/support/server_callback.h - include/grpcpp/support/server_callback_impl.h include/grpcpp/support/server_interceptor.h include/grpcpp/support/slice.h include/grpcpp/support/status.h @@ -3501,7 +3480,6 @@ foreach(_hdr include/grpcpp/support/string_ref.h include/grpcpp/support/stub_options.h include/grpcpp/support/sync_stream.h - include/grpcpp/support/sync_stream_impl.h include/grpcpp/support/time.h include/grpcpp/support/validate_service_config.h ) diff --git a/Makefile b/Makefile index 5441fa42e91..609b1079cfa 100644 --- a/Makefile +++ b/Makefile @@ -2854,9 +2854,7 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/client_unary_call.h \ include/grpcpp/impl/codegen/async_generic_service.h \ include/grpcpp/impl/codegen/async_stream.h \ - include/grpcpp/impl/codegen/async_stream_impl.h \ include/grpcpp/impl/codegen/async_unary_call.h \ - include/grpcpp/impl/codegen/async_unary_call_impl.h \ include/grpcpp/impl/codegen/byte_buffer.h \ include/grpcpp/impl/codegen/call.h \ include/grpcpp/impl/codegen/call_hook.h \ @@ -2865,7 +2863,6 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/callback_common.h \ include/grpcpp/impl/codegen/channel_interface.h \ include/grpcpp/impl/codegen/client_callback.h \ - include/grpcpp/impl/codegen/client_callback_impl.h \ include/grpcpp/impl/codegen/client_context.h \ include/grpcpp/impl/codegen/client_interceptor.h \ include/grpcpp/impl/codegen/client_unary_call.h \ @@ -2884,7 +2881,6 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/message_allocator.h \ include/grpcpp/impl/codegen/metadata_map.h \ include/grpcpp/impl/codegen/method_handler.h \ - include/grpcpp/impl/codegen/method_handler_impl.h \ include/grpcpp/impl/codegen/proto_buffer_reader.h \ include/grpcpp/impl/codegen/proto_buffer_writer.h \ include/grpcpp/impl/codegen/proto_utils.h \ @@ -2894,7 +2890,6 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/serialization_traits.h \ include/grpcpp/impl/codegen/server_callback.h \ include/grpcpp/impl/codegen/server_callback_handlers.h \ - include/grpcpp/impl/codegen/server_callback_impl.h \ include/grpcpp/impl/codegen/server_context.h \ include/grpcpp/impl/codegen/server_interceptor.h \ include/grpcpp/impl/codegen/server_interface.h \ @@ -2906,7 +2901,6 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/stub_options.h \ include/grpcpp/impl/codegen/sync.h \ include/grpcpp/impl/codegen/sync_stream.h \ - include/grpcpp/impl/codegen/sync_stream_impl.h \ include/grpcpp/impl/codegen/time.h \ include/grpcpp/impl/grpc_library.h \ include/grpcpp/impl/method_handler_impl.h \ @@ -2930,13 +2924,10 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/server_context.h \ include/grpcpp/server_posix.h \ include/grpcpp/support/async_stream.h \ - include/grpcpp/support/async_stream_impl.h \ include/grpcpp/support/async_unary_call.h \ - include/grpcpp/support/async_unary_call_impl.h \ include/grpcpp/support/byte_buffer.h \ include/grpcpp/support/channel_arguments.h \ include/grpcpp/support/client_callback.h \ - include/grpcpp/support/client_callback_impl.h \ include/grpcpp/support/client_interceptor.h \ include/grpcpp/support/config.h \ include/grpcpp/support/interceptor.h \ @@ -2945,7 +2936,6 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/support/proto_buffer_reader.h \ include/grpcpp/support/proto_buffer_writer.h \ include/grpcpp/support/server_callback.h \ - include/grpcpp/support/server_callback_impl.h \ include/grpcpp/support/server_interceptor.h \ include/grpcpp/support/slice.h \ include/grpcpp/support/status.h \ @@ -2953,7 +2943,6 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/support/string_ref.h \ include/grpcpp/support/stub_options.h \ include/grpcpp/support/sync_stream.h \ - include/grpcpp/support/sync_stream_impl.h \ include/grpcpp/support/time.h \ include/grpcpp/support/validate_service_config.h \ @@ -3382,9 +3371,7 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/client_unary_call.h \ include/grpcpp/impl/codegen/async_generic_service.h \ include/grpcpp/impl/codegen/async_stream.h \ - include/grpcpp/impl/codegen/async_stream_impl.h \ include/grpcpp/impl/codegen/async_unary_call.h \ - include/grpcpp/impl/codegen/async_unary_call_impl.h \ include/grpcpp/impl/codegen/byte_buffer.h \ include/grpcpp/impl/codegen/call.h \ include/grpcpp/impl/codegen/call_hook.h \ @@ -3393,7 +3380,6 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/callback_common.h \ include/grpcpp/impl/codegen/channel_interface.h \ include/grpcpp/impl/codegen/client_callback.h \ - include/grpcpp/impl/codegen/client_callback_impl.h \ include/grpcpp/impl/codegen/client_context.h \ include/grpcpp/impl/codegen/client_interceptor.h \ include/grpcpp/impl/codegen/client_unary_call.h \ @@ -3412,7 +3398,6 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/message_allocator.h \ include/grpcpp/impl/codegen/metadata_map.h \ include/grpcpp/impl/codegen/method_handler.h \ - include/grpcpp/impl/codegen/method_handler_impl.h \ include/grpcpp/impl/codegen/proto_buffer_reader.h \ include/grpcpp/impl/codegen/proto_buffer_writer.h \ include/grpcpp/impl/codegen/proto_utils.h \ @@ -3422,7 +3407,6 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/serialization_traits.h \ include/grpcpp/impl/codegen/server_callback.h \ include/grpcpp/impl/codegen/server_callback_handlers.h \ - include/grpcpp/impl/codegen/server_callback_impl.h \ include/grpcpp/impl/codegen/server_context.h \ include/grpcpp/impl/codegen/server_interceptor.h \ include/grpcpp/impl/codegen/server_interface.h \ @@ -3434,7 +3418,6 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/impl/codegen/stub_options.h \ include/grpcpp/impl/codegen/sync.h \ include/grpcpp/impl/codegen/sync_stream.h \ - include/grpcpp/impl/codegen/sync_stream_impl.h \ include/grpcpp/impl/codegen/time.h \ include/grpcpp/impl/grpc_library.h \ include/grpcpp/impl/method_handler_impl.h \ @@ -3458,13 +3441,10 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/server_context.h \ include/grpcpp/server_posix.h \ include/grpcpp/support/async_stream.h \ - include/grpcpp/support/async_stream_impl.h \ include/grpcpp/support/async_unary_call.h \ - include/grpcpp/support/async_unary_call_impl.h \ include/grpcpp/support/byte_buffer.h \ include/grpcpp/support/channel_arguments.h \ include/grpcpp/support/client_callback.h \ - include/grpcpp/support/client_callback_impl.h \ include/grpcpp/support/client_interceptor.h \ include/grpcpp/support/config.h \ include/grpcpp/support/interceptor.h \ @@ -3473,7 +3453,6 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/support/proto_buffer_reader.h \ include/grpcpp/support/proto_buffer_writer.h \ include/grpcpp/support/server_callback.h \ - include/grpcpp/support/server_callback_impl.h \ include/grpcpp/support/server_interceptor.h \ include/grpcpp/support/slice.h \ include/grpcpp/support/status.h \ @@ -3481,7 +3460,6 @@ PUBLIC_HEADERS_CXX += \ include/grpcpp/support/string_ref.h \ include/grpcpp/support/stub_options.h \ include/grpcpp/support/sync_stream.h \ - include/grpcpp/support/sync_stream_impl.h \ include/grpcpp/support/time.h \ include/grpcpp/support/validate_service_config.h \ diff --git a/build_autogenerated.yaml b/build_autogenerated.yaml index 244a2db3007..5292f07fd48 100644 --- a/build_autogenerated.yaml +++ b/build_autogenerated.yaml @@ -1990,9 +1990,7 @@ libs: - include/grpcpp/impl/client_unary_call.h - include/grpcpp/impl/codegen/async_generic_service.h - include/grpcpp/impl/codegen/async_stream.h - - include/grpcpp/impl/codegen/async_stream_impl.h - include/grpcpp/impl/codegen/async_unary_call.h - - include/grpcpp/impl/codegen/async_unary_call_impl.h - include/grpcpp/impl/codegen/byte_buffer.h - include/grpcpp/impl/codegen/call.h - include/grpcpp/impl/codegen/call_hook.h @@ -2001,7 +1999,6 @@ libs: - include/grpcpp/impl/codegen/callback_common.h - include/grpcpp/impl/codegen/channel_interface.h - include/grpcpp/impl/codegen/client_callback.h - - include/grpcpp/impl/codegen/client_callback_impl.h - include/grpcpp/impl/codegen/client_context.h - include/grpcpp/impl/codegen/client_interceptor.h - include/grpcpp/impl/codegen/client_unary_call.h @@ -2030,7 +2027,6 @@ libs: - include/grpcpp/impl/codegen/serialization_traits.h - include/grpcpp/impl/codegen/server_callback.h - include/grpcpp/impl/codegen/server_callback_handlers.h - - include/grpcpp/impl/codegen/server_callback_impl.h - include/grpcpp/impl/codegen/server_context.h - include/grpcpp/impl/codegen/server_interceptor.h - include/grpcpp/impl/codegen/server_interface.h @@ -2042,7 +2038,6 @@ libs: - include/grpcpp/impl/codegen/stub_options.h - include/grpcpp/impl/codegen/sync.h - include/grpcpp/impl/codegen/sync_stream.h - - include/grpcpp/impl/codegen/sync_stream_impl.h - include/grpcpp/impl/codegen/time.h - include/grpcpp/impl/grpc_library.h - include/grpcpp/impl/method_handler_impl.h @@ -2066,13 +2061,10 @@ libs: - include/grpcpp/server_context.h - include/grpcpp/server_posix.h - include/grpcpp/support/async_stream.h - - include/grpcpp/support/async_stream_impl.h - include/grpcpp/support/async_unary_call.h - - include/grpcpp/support/async_unary_call_impl.h - include/grpcpp/support/byte_buffer.h - include/grpcpp/support/channel_arguments.h - include/grpcpp/support/client_callback.h - - include/grpcpp/support/client_callback_impl.h - include/grpcpp/support/client_interceptor.h - include/grpcpp/support/config.h - include/grpcpp/support/interceptor.h @@ -2081,7 +2073,6 @@ libs: - include/grpcpp/support/proto_buffer_reader.h - include/grpcpp/support/proto_buffer_writer.h - include/grpcpp/support/server_callback.h - - include/grpcpp/support/server_callback_impl.h - include/grpcpp/support/server_interceptor.h - include/grpcpp/support/slice.h - include/grpcpp/support/status.h @@ -2089,7 +2080,6 @@ libs: - include/grpcpp/support/string_ref.h - include/grpcpp/support/stub_options.h - include/grpcpp/support/sync_stream.h - - include/grpcpp/support/sync_stream_impl.h - include/grpcpp/support/time.h - include/grpcpp/support/validate_service_config.h headers: @@ -2365,9 +2355,7 @@ libs: - include/grpcpp/impl/client_unary_call.h - include/grpcpp/impl/codegen/async_generic_service.h - include/grpcpp/impl/codegen/async_stream.h - - include/grpcpp/impl/codegen/async_stream_impl.h - include/grpcpp/impl/codegen/async_unary_call.h - - include/grpcpp/impl/codegen/async_unary_call_impl.h - include/grpcpp/impl/codegen/byte_buffer.h - include/grpcpp/impl/codegen/call.h - include/grpcpp/impl/codegen/call_hook.h @@ -2376,7 +2364,6 @@ libs: - include/grpcpp/impl/codegen/callback_common.h - include/grpcpp/impl/codegen/channel_interface.h - include/grpcpp/impl/codegen/client_callback.h - - include/grpcpp/impl/codegen/client_callback_impl.h - include/grpcpp/impl/codegen/client_context.h - include/grpcpp/impl/codegen/client_interceptor.h - include/grpcpp/impl/codegen/client_unary_call.h @@ -2405,7 +2392,6 @@ libs: - include/grpcpp/impl/codegen/serialization_traits.h - include/grpcpp/impl/codegen/server_callback.h - include/grpcpp/impl/codegen/server_callback_handlers.h - - include/grpcpp/impl/codegen/server_callback_impl.h - include/grpcpp/impl/codegen/server_context.h - include/grpcpp/impl/codegen/server_interceptor.h - include/grpcpp/impl/codegen/server_interface.h @@ -2417,7 +2403,6 @@ libs: - include/grpcpp/impl/codegen/stub_options.h - include/grpcpp/impl/codegen/sync.h - include/grpcpp/impl/codegen/sync_stream.h - - include/grpcpp/impl/codegen/sync_stream_impl.h - include/grpcpp/impl/codegen/time.h - include/grpcpp/impl/grpc_library.h - include/grpcpp/impl/method_handler_impl.h @@ -2441,13 +2426,10 @@ libs: - include/grpcpp/server_context.h - include/grpcpp/server_posix.h - include/grpcpp/support/async_stream.h - - include/grpcpp/support/async_stream_impl.h - include/grpcpp/support/async_unary_call.h - - include/grpcpp/support/async_unary_call_impl.h - include/grpcpp/support/byte_buffer.h - include/grpcpp/support/channel_arguments.h - include/grpcpp/support/client_callback.h - - include/grpcpp/support/client_callback_impl.h - include/grpcpp/support/client_interceptor.h - include/grpcpp/support/config.h - include/grpcpp/support/interceptor.h @@ -2456,7 +2438,6 @@ libs: - include/grpcpp/support/proto_buffer_reader.h - include/grpcpp/support/proto_buffer_writer.h - include/grpcpp/support/server_callback.h - - include/grpcpp/support/server_callback_impl.h - include/grpcpp/support/server_interceptor.h - include/grpcpp/support/slice.h - include/grpcpp/support/status.h @@ -2464,7 +2445,6 @@ libs: - include/grpcpp/support/string_ref.h - include/grpcpp/support/stub_options.h - include/grpcpp/support/sync_stream.h - - include/grpcpp/support/sync_stream_impl.h - include/grpcpp/support/time.h - include/grpcpp/support/validate_service_config.h headers: diff --git a/gRPC-C++.podspec b/gRPC-C++.podspec index d1279e6f38a..02f640f68fa 100644 --- a/gRPC-C++.podspec +++ b/gRPC-C++.podspec @@ -93,9 +93,7 @@ Pod::Spec.new do |s| 'include/grpcpp/impl/client_unary_call.h', 'include/grpcpp/impl/codegen/async_generic_service.h', 'include/grpcpp/impl/codegen/async_stream.h', - 'include/grpcpp/impl/codegen/async_stream_impl.h', 'include/grpcpp/impl/codegen/async_unary_call.h', - 'include/grpcpp/impl/codegen/async_unary_call_impl.h', 'include/grpcpp/impl/codegen/byte_buffer.h', 'include/grpcpp/impl/codegen/call.h', 'include/grpcpp/impl/codegen/call_hook.h', @@ -104,7 +102,6 @@ Pod::Spec.new do |s| 'include/grpcpp/impl/codegen/callback_common.h', 'include/grpcpp/impl/codegen/channel_interface.h', 'include/grpcpp/impl/codegen/client_callback.h', - 'include/grpcpp/impl/codegen/client_callback_impl.h', 'include/grpcpp/impl/codegen/client_context.h', 'include/grpcpp/impl/codegen/client_interceptor.h', 'include/grpcpp/impl/codegen/client_unary_call.h', @@ -122,14 +119,12 @@ Pod::Spec.new do |s| 'include/grpcpp/impl/codegen/message_allocator.h', 'include/grpcpp/impl/codegen/metadata_map.h', 'include/grpcpp/impl/codegen/method_handler.h', - 'include/grpcpp/impl/codegen/method_handler_impl.h', 'include/grpcpp/impl/codegen/rpc_method.h', 'include/grpcpp/impl/codegen/rpc_service_method.h', 'include/grpcpp/impl/codegen/security/auth_context.h', 'include/grpcpp/impl/codegen/serialization_traits.h', 'include/grpcpp/impl/codegen/server_callback.h', 'include/grpcpp/impl/codegen/server_callback_handlers.h', - 'include/grpcpp/impl/codegen/server_callback_impl.h', 'include/grpcpp/impl/codegen/server_context.h', 'include/grpcpp/impl/codegen/server_interceptor.h', 'include/grpcpp/impl/codegen/server_interface.h', @@ -141,7 +136,6 @@ Pod::Spec.new do |s| 'include/grpcpp/impl/codegen/stub_options.h', 'include/grpcpp/impl/codegen/sync.h', 'include/grpcpp/impl/codegen/sync_stream.h', - 'include/grpcpp/impl/codegen/sync_stream_impl.h', 'include/grpcpp/impl/codegen/time.h', 'include/grpcpp/impl/grpc_library.h', 'include/grpcpp/impl/method_handler_impl.h', @@ -165,13 +159,10 @@ Pod::Spec.new do |s| 'include/grpcpp/server_context.h', 'include/grpcpp/server_posix.h', 'include/grpcpp/support/async_stream.h', - 'include/grpcpp/support/async_stream_impl.h', 'include/grpcpp/support/async_unary_call.h', - 'include/grpcpp/support/async_unary_call_impl.h', 'include/grpcpp/support/byte_buffer.h', 'include/grpcpp/support/channel_arguments.h', 'include/grpcpp/support/client_callback.h', - 'include/grpcpp/support/client_callback_impl.h', 'include/grpcpp/support/client_interceptor.h', 'include/grpcpp/support/config.h', 'include/grpcpp/support/interceptor.h', @@ -180,7 +171,6 @@ Pod::Spec.new do |s| 'include/grpcpp/support/proto_buffer_reader.h', 'include/grpcpp/support/proto_buffer_writer.h', 'include/grpcpp/support/server_callback.h', - 'include/grpcpp/support/server_callback_impl.h', 'include/grpcpp/support/server_interceptor.h', 'include/grpcpp/support/slice.h', 'include/grpcpp/support/status.h', @@ -188,7 +178,6 @@ Pod::Spec.new do |s| 'include/grpcpp/support/string_ref.h', 'include/grpcpp/support/stub_options.h', 'include/grpcpp/support/sync_stream.h', - 'include/grpcpp/support/sync_stream_impl.h', 'include/grpcpp/support/time.h', 'include/grpcpp/support/validate_service_config.h' end diff --git a/include/grpcpp/generic/generic_stub.h b/include/grpcpp/generic/generic_stub.h index 70c5f5d763d..a113ee1d0bf 100644 --- a/include/grpcpp/generic/generic_stub.h +++ b/include/grpcpp/generic/generic_stub.h @@ -23,20 +23,19 @@ #include #include -#include -#include +#include +#include #include -#include +#include #include namespace grpc { class CompletionQueue; -typedef ::grpc_impl::ClientAsyncReaderWriter +typedef ClientAsyncReaderWriter GenericClientAsyncReaderWriter; -typedef ::grpc_impl::ClientAsyncResponseReader - GenericClientAsyncResponseReader; +typedef ClientAsyncResponseReader GenericClientAsyncResponseReader; /// Generic stubs provide a type-unaware interface to call gRPC methods /// by name. In practice, the Request and Response types should be basic @@ -51,8 +50,7 @@ class TemplatedGenericStub final { /// start it. Let it be started explicitly with StartCall and a tag. /// The return value only indicates whether or not registration of the call /// succeeded (i.e. the call won't proceed if the return value is nullptr). - std::unique_ptr< - ::grpc_impl::ClientAsyncReaderWriter> + std::unique_ptr> PrepareCall(ClientContext* context, const std::string& method, ::grpc::CompletionQueue* cq) { return CallInternal(channel_.get(), context, method, cq, false, nullptr); @@ -62,17 +60,15 @@ class TemplatedGenericStub final { /// start it. Let it be started explicitly with StartCall. /// The return value only indicates whether or not registration of the call /// succeeded (i.e. the call won't proceed if the return value is nullptr). - std::unique_ptr<::grpc_impl::ClientAsyncResponseReader> - PrepareUnaryCall(ClientContext* context, const std::string& method, - const RequestType& request, ::grpc::CompletionQueue* cq) { - return std::unique_ptr< - ::grpc_impl::ClientAsyncResponseReader>( - grpc_impl::internal::ClientAsyncResponseReaderFactory< - ResponseType>::Create(channel_.get(), cq, - grpc::internal::RpcMethod( - method.c_str(), + std::unique_ptr> PrepareUnaryCall( + ClientContext* context, const std::string& method, + const RequestType& request, ::grpc::CompletionQueue* cq) { + return std::unique_ptr>( + internal::ClientAsyncResponseReaderFactory::Create( + channel_.get(), cq, + grpc::internal::RpcMethod(method.c_str(), grpc::internal::RpcMethod::NORMAL_RPC), - context, request, false)); + context, request, false)); } /// DEPRECATED for multi-threaded use @@ -81,10 +77,9 @@ class TemplatedGenericStub final { /// (i.e, initial metadata has been sent). /// The return value only indicates whether or not registration of the call /// succeeded (i.e. the call won't proceed if the return value is nullptr). - std::unique_ptr< - ::grpc_impl::ClientAsyncReaderWriter> - Call(ClientContext* context, const std::string& method, - ::grpc::CompletionQueue* cq, void* tag) { + std::unique_ptr> Call( + ClientContext* context, const std::string& method, + ::grpc::CompletionQueue* cq, void* tag) { return CallInternal(channel_.get(), context, method, cq, true, tag); } @@ -104,7 +99,7 @@ class TemplatedGenericStub final { /// StartCall is invoked on its reactor. void PrepareUnaryCall(ClientContext* context, const std::string& method, const RequestType* request, ResponseType* response, - ::grpc_impl::ClientUnaryReactor* reactor) { + ClientUnaryReactor* reactor) { PrepareUnaryCallInternal(context, method, request, response, reactor); } @@ -113,7 +108,7 @@ class TemplatedGenericStub final { /// until StartCall is invoked on its reactor. void PrepareBidiStreamingCall( ClientContext* context, const std::string& method, - ::grpc_impl::ClientBidiReactor* reactor) { + ClientBidiReactor* reactor) { PrepareBidiStreamingCallInternal(context, method, reactor); } #endif @@ -140,7 +135,7 @@ class TemplatedGenericStub final { /// StartCall is invoked on its reactor. void PrepareUnaryCall(ClientContext* context, const std::string& method, const RequestType* request, ResponseType* response, - ::grpc_impl::ClientUnaryReactor* reactor) { + ClientUnaryReactor* reactor) { stub_->PrepareUnaryCallInternal(context, method, request, response, reactor); } @@ -150,7 +145,7 @@ class TemplatedGenericStub final { /// until StartCall is invoked on its reactor. void PrepareBidiStreamingCall( ClientContext* context, const std::string& method, - ::grpc_impl::ClientBidiReactor* reactor) { + ClientBidiReactor* reactor) { stub_->PrepareBidiStreamingCallInternal(context, method, reactor); } @@ -169,7 +164,7 @@ class TemplatedGenericStub final { void UnaryCallInternal(ClientContext* context, const std::string& method, const RequestType* request, ResponseType* response, std::function on_completion) { - ::grpc_impl::internal::CallbackUnaryCall( + internal::CallbackUnaryCall( channel_.get(), grpc::internal::RpcMethod(method.c_str(), grpc::internal::RpcMethod::NORMAL_RPC), @@ -180,9 +175,8 @@ class TemplatedGenericStub final { const std::string& method, const RequestType* request, ResponseType* response, - ::grpc_impl::ClientUnaryReactor* reactor) { - ::grpc_impl::internal::ClientCallbackUnaryFactory::Create( + ClientUnaryReactor* reactor) { + internal::ClientCallbackUnaryFactory::Create( channel_.get(), grpc::internal::RpcMethod(method.c_str(), grpc::internal::RpcMethod::NORMAL_RPC), @@ -191,24 +185,21 @@ class TemplatedGenericStub final { void PrepareBidiStreamingCallInternal( ClientContext* context, const std::string& method, - ::grpc_impl::ClientBidiReactor* reactor) { - ::grpc_impl::internal:: - ClientCallbackReaderWriterFactory::Create( - channel_.get(), - grpc::internal::RpcMethod( - method.c_str(), grpc::internal::RpcMethod::BIDI_STREAMING), - context, reactor); + ClientBidiReactor* reactor) { + internal::ClientCallbackReaderWriterFactory:: + Create(channel_.get(), + grpc::internal::RpcMethod( + method.c_str(), grpc::internal::RpcMethod::BIDI_STREAMING), + context, reactor); } - std::unique_ptr< - ::grpc_impl::ClientAsyncReaderWriter> + std::unique_ptr> CallInternal(grpc::ChannelInterface* channel, ClientContext* context, const std::string& method, ::grpc::CompletionQueue* cq, bool start, void* tag) { - return std::unique_ptr< - ::grpc_impl::ClientAsyncReaderWriter>( - ::grpc_impl::internal:: - ClientAsyncReaderWriterFactory::Create( + return std::unique_ptr>( + internal::ClientAsyncReaderWriterFactory:: + Create( channel, cq, grpc::internal::RpcMethod( method.c_str(), grpc::internal::RpcMethod::BIDI_STREAMING), diff --git a/include/grpcpp/impl/codegen/async_generic_service.h b/include/grpcpp/impl/codegen/async_generic_service.h index 56217c1e129..398768b4240 100644 --- a/include/grpcpp/impl/codegen/async_generic_service.h +++ b/include/grpcpp/impl/codegen/async_generic_service.h @@ -21,22 +21,20 @@ #include -#include +#include #include +#include #include -#include struct grpc_server; namespace grpc { -typedef ::grpc_impl::ServerAsyncReaderWriter +typedef ServerAsyncReaderWriter GenericServerAsyncReaderWriter; -typedef ::grpc_impl::ServerAsyncResponseWriter - GenericServerAsyncResponseWriter; -typedef ::grpc_impl::ServerAsyncReader - GenericServerAsyncReader; -typedef ::grpc_impl::ServerAsyncWriter GenericServerAsyncWriter; +typedef ServerAsyncResponseWriter GenericServerAsyncResponseWriter; +typedef ServerAsyncReader GenericServerAsyncReader; +typedef ServerAsyncWriter GenericServerAsyncWriter; class GenericServerContext final : public ::grpc::ServerContext { public: @@ -95,8 +93,7 @@ namespace experimental { /// \a ServerGenericBidiReactor is the reactor class for bidi streaming RPCs /// invoked on a CallbackGenericService. It is just a ServerBidi reactor with /// ByteBuffer arguments. -using ServerGenericBidiReactor = - ::grpc_impl::ServerBidiReactor; +using ServerGenericBidiReactor = ServerBidiReactor; class GenericCallbackServerContext final : public ::grpc::CallbackServerContext { @@ -142,10 +139,8 @@ class CallbackGenericService { private: friend class grpc::Server; - ::grpc_impl::internal::CallbackBidiHandler* - Handler() { - return new ::grpc_impl::internal::CallbackBidiHandler( + internal::CallbackBidiHandler* Handler() { + return new internal::CallbackBidiHandler( [this](::grpc::CallbackServerContext* ctx) { return CreateReactor(static_cast(ctx)); }); diff --git a/include/grpcpp/impl/codegen/async_stream.h b/include/grpcpp/impl/codegen/async_stream.h index d76a8982a4b..aaee93df933 100644 --- a/include/grpcpp/impl/codegen/async_stream.h +++ b/include/grpcpp/impl/codegen/async_stream.h @@ -1,6 +1,6 @@ /* * - * Copyright 2015 gRPC authors. + * Copyright 2019 gRPC authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,83 +13,1119 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ #ifndef GRPCPP_IMPL_CODEGEN_ASYNC_STREAM_H #define GRPCPP_IMPL_CODEGEN_ASYNC_STREAM_H -#include +#include +#include +#include +#include +#include +#include namespace grpc { namespace internal { +/// Common interface for all client side asynchronous streaming. +class ClientAsyncStreamingInterface { + public: + virtual ~ClientAsyncStreamingInterface() {} + + /// Start the call that was set up by the constructor, but only if the + /// constructor was invoked through the "Prepare" API which doesn't actually + /// start the call + virtual void StartCall(void* tag) = 0; + + /// Request notification of the reading of the initial metadata. Completion + /// will be notified by \a tag on the associated completion queue. + /// This call is optional, but if it is used, it cannot be used concurrently + /// with or after the \a AsyncReaderInterface::Read method. + /// + /// \param[in] tag Tag identifying this request. + virtual void ReadInitialMetadata(void* tag) = 0; -typedef ::grpc_impl::internal::ClientAsyncStreamingInterface - ClientAsyncStreamingInterface; + /// Indicate that the stream is to be finished and request notification for + /// when the call has been ended. + /// Should not be used concurrently with other operations. + /// + /// It is appropriate to call this method exactly once when both: + /// * the client side has no more message to send + /// (this can be declared implicitly by calling this method, or + /// explicitly through an earlier call to the WritesDone method + /// of the class in use, e.g. \a ClientAsyncWriterInterface::WritesDone or + /// \a ClientAsyncReaderWriterInterface::WritesDone). + /// * there are no more messages to be received from the server (this can + /// be known implicitly by the calling code, or explicitly from an + /// earlier call to \a AsyncReaderInterface::Read that yielded a failed + /// result, e.g. cq->Next(&read_tag, &ok) filled in 'ok' with 'false'). + /// + /// The tag will be returned when either: + /// - all incoming messages have been read and the server has returned + /// a status. + /// - the server has returned a non-OK status. + /// - the call failed for some reason and the library generated a + /// status. + /// + /// Note that implementations of this method attempt to receive initial + /// metadata from the server if initial metadata hasn't yet been received. + /// + /// \param[in] tag Tag identifying this request. + /// \param[out] status To be updated with the operation status. + virtual void Finish(::grpc::Status* status, void* tag) = 0; +}; +/// An interface that yields a sequence of messages of type \a R. template -using AsyncReaderInterface = ::grpc_impl::internal::AsyncReaderInterface; +class AsyncReaderInterface { + public: + virtual ~AsyncReaderInterface() {} + /// Read a message of type \a R into \a msg. Completion will be notified by \a + /// tag on the associated completion queue. + /// This is thread-safe with respect to \a Write or \a WritesDone methods. It + /// should not be called concurrently with other streaming APIs + /// on the same stream. It is not meaningful to call it concurrently + /// with another \a AsyncReaderInterface::Read on the same stream since reads + /// on the same stream are delivered in order. + /// + /// \param[out] msg Where to eventually store the read message. + /// \param[in] tag The tag identifying the operation. + /// + /// Side effect: note that this method attempt to receive initial metadata for + /// a stream if it hasn't yet been received. + virtual void Read(R* msg, void* tag) = 0; +}; + +/// An interface that can be fed a sequence of messages of type \a W. template -using AsyncWriterInterface = ::grpc_impl::internal::AsyncWriterInterface; +class AsyncWriterInterface { + public: + virtual ~AsyncWriterInterface() {} + + /// Request the writing of \a msg with identifying tag \a tag. + /// + /// Only one write may be outstanding at any given time. This means that + /// after calling Write, one must wait to receive \a tag from the completion + /// queue BEFORE calling Write again. + /// This is thread-safe with respect to \a AsyncReaderInterface::Read + /// + /// gRPC doesn't take ownership or a reference to \a msg, so it is safe to + /// to deallocate once Write returns. + /// + /// \param[in] msg The message to be written. + /// \param[in] tag The tag identifying the operation. + virtual void Write(const W& msg, void* tag) = 0; + + /// Request the writing of \a msg using WriteOptions \a options with + /// identifying tag \a tag. + /// + /// Only one write may be outstanding at any given time. This means that + /// after calling Write, one must wait to receive \a tag from the completion + /// queue BEFORE calling Write again. + /// WriteOptions \a options is used to set the write options of this message. + /// This is thread-safe with respect to \a AsyncReaderInterface::Read + /// + /// gRPC doesn't take ownership or a reference to \a msg, so it is safe to + /// to deallocate once Write returns. + /// + /// \param[in] msg The message to be written. + /// \param[in] options The WriteOptions to be used to write this message. + /// \param[in] tag The tag identifying the operation. + virtual void Write(const W& msg, ::grpc::WriteOptions options, void* tag) = 0; + + /// Request the writing of \a msg and coalesce it with the writing + /// of trailing metadata, using WriteOptions \a options with + /// identifying tag \a tag. + /// + /// For client, WriteLast is equivalent of performing Write and + /// WritesDone in a single step. + /// For server, WriteLast buffers the \a msg. The writing of \a msg is held + /// until Finish is called, where \a msg and trailing metadata are coalesced + /// and write is initiated. Note that WriteLast can only buffer \a msg up to + /// the flow control window size. If \a msg size is larger than the window + /// size, it will be sent on wire without buffering. + /// + /// gRPC doesn't take ownership or a reference to \a msg, so it is safe to + /// to deallocate once Write returns. + /// + /// \param[in] msg The message to be written. + /// \param[in] options The WriteOptions to be used to write this message. + /// \param[in] tag The tag identifying the operation. + void WriteLast(const W& msg, ::grpc::WriteOptions options, void* tag) { + Write(msg, options.set_last_message(), tag); + } +}; } // namespace internal template -using ClientAsyncReaderInterface = ::grpc_impl::ClientAsyncReaderInterface; +class ClientAsyncReaderInterface + : public internal::ClientAsyncStreamingInterface, + public internal::AsyncReaderInterface {}; + +namespace internal { +template +class ClientAsyncReaderFactory { + public: + /// Create a stream object. + /// Write the first request out if \a start is set. + /// \a tag will be notified on \a cq when the call has been started and + /// \a request has been written out. If \a start is not set, \a tag must be + /// nullptr and the actual call must be initiated by StartCall + /// Note that \a context will be used to fill in custom initial metadata + /// used to send to the server when starting the call. + template + static ClientAsyncReader* Create(::grpc::ChannelInterface* channel, + ::grpc::CompletionQueue* cq, + const ::grpc::internal::RpcMethod& method, + ::grpc::ClientContext* context, + const W& request, bool start, void* tag) { + ::grpc::internal::Call call = channel->CreateCall(method, context, cq); + return new (::grpc::g_core_codegen_interface->grpc_call_arena_alloc( + call.call(), sizeof(ClientAsyncReader))) + ClientAsyncReader(call, context, request, start, tag); + } +}; +} // namespace internal +/// Async client-side API for doing server-streaming RPCs, +/// where the incoming message stream coming from the server has +/// messages of type \a R. template -using ClientAsyncReader = ::grpc_impl::ClientAsyncReader; +class ClientAsyncReader final : public ClientAsyncReaderInterface { + public: + // always allocated against a call arena, no memory free required + static void operator delete(void* /*ptr*/, std::size_t size) { + GPR_CODEGEN_ASSERT(size == sizeof(ClientAsyncReader)); + } + // This operator should never be called as the memory should be freed as part + // of the arena destruction. It only exists to provide a matching operator + // delete to the operator new so that some compilers will not complain (see + // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this + // there are no tests catching the compiler warning. + static void operator delete(void*, void*) { GPR_CODEGEN_ASSERT(false); } + + void StartCall(void* tag) override { + GPR_CODEGEN_ASSERT(!started_); + started_ = true; + StartCallInternal(tag); + } + + /// See the \a ClientAsyncStreamingInterface.ReadInitialMetadata + /// method for semantics. + /// + /// Side effect: + /// - upon receiving initial metadata from the server, + /// the \a ClientContext associated with this call is updated, and the + /// calling code can access the received metadata through the + /// \a ClientContext. + void ReadInitialMetadata(void* tag) override { + GPR_CODEGEN_ASSERT(started_); + GPR_CODEGEN_ASSERT(!context_->initial_metadata_received_); + + meta_ops_.set_output_tag(tag); + meta_ops_.RecvInitialMetadata(context_); + call_.PerformOps(&meta_ops_); + } + + void Read(R* msg, void* tag) override { + GPR_CODEGEN_ASSERT(started_); + read_ops_.set_output_tag(tag); + if (!context_->initial_metadata_received_) { + read_ops_.RecvInitialMetadata(context_); + } + read_ops_.RecvMessage(msg); + call_.PerformOps(&read_ops_); + } + + /// See the \a ClientAsyncStreamingInterface.Finish method for semantics. + /// + /// Side effect: + /// - the \a ClientContext associated with this call is updated with + /// possible initial and trailing metadata received from the server. + void Finish(::grpc::Status* status, void* tag) override { + GPR_CODEGEN_ASSERT(started_); + finish_ops_.set_output_tag(tag); + if (!context_->initial_metadata_received_) { + finish_ops_.RecvInitialMetadata(context_); + } + finish_ops_.ClientRecvStatus(context_, status); + call_.PerformOps(&finish_ops_); + } + + private: + friend class internal::ClientAsyncReaderFactory; + template + ClientAsyncReader(::grpc::internal::Call call, ::grpc::ClientContext* context, + const W& request, bool start, void* tag) + : context_(context), call_(call), started_(start) { + // TODO(ctiller): don't assert + GPR_CODEGEN_ASSERT(init_ops_.SendMessage(request).ok()); + init_ops_.ClientSendClose(); + if (start) { + StartCallInternal(tag); + } else { + GPR_CODEGEN_ASSERT(tag == nullptr); + } + } + + void StartCallInternal(void* tag) { + init_ops_.SendInitialMetadata(&context_->send_initial_metadata_, + context_->initial_metadata_flags()); + init_ops_.set_output_tag(tag); + call_.PerformOps(&init_ops_); + } + + ::grpc::ClientContext* context_; + ::grpc::internal::Call call_; + bool started_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, + ::grpc::internal::CallOpSendMessage, + ::grpc::internal::CallOpClientSendClose> + init_ops_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata> + meta_ops_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata, + ::grpc::internal::CallOpRecvMessage> + read_ops_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata, + ::grpc::internal::CallOpClientRecvStatus> + finish_ops_; +}; + +/// Common interface for client side asynchronous writing. template -using ClientAsyncWriterInterface = ::grpc_impl::ClientAsyncWriterInterface; +class ClientAsyncWriterInterface + : public internal::ClientAsyncStreamingInterface, + public internal::AsyncWriterInterface { + public: + /// Signal the client is done with the writes (half-close the client stream). + /// Thread-safe with respect to \a AsyncReaderInterface::Read + /// + /// \param[in] tag The tag identifying the operation. + virtual void WritesDone(void* tag) = 0; +}; +namespace internal { template -using ClientAsyncWriter = ::grpc_impl::ClientAsyncWriter; +class ClientAsyncWriterFactory { + public: + /// Create a stream object. + /// Start the RPC if \a start is set + /// \a tag will be notified on \a cq when the call has been started (i.e. + /// intitial metadata sent) and \a request has been written out. + /// If \a start is not set, \a tag must be nullptr and the actual call + /// must be initiated by StartCall + /// Note that \a context will be used to fill in custom initial metadata + /// used to send to the server when starting the call. + /// \a response will be filled in with the single expected response + /// message from the server upon a successful call to the \a Finish + /// method of this instance. + template + static ClientAsyncWriter* Create(::grpc::ChannelInterface* channel, + ::grpc::CompletionQueue* cq, + const ::grpc::internal::RpcMethod& method, + ::grpc::ClientContext* context, + R* response, bool start, void* tag) { + ::grpc::internal::Call call = channel->CreateCall(method, context, cq); + return new (::grpc::g_core_codegen_interface->grpc_call_arena_alloc( + call.call(), sizeof(ClientAsyncWriter))) + ClientAsyncWriter(call, context, response, start, tag); + } +}; +} // namespace internal +/// Async API on the client side for doing client-streaming RPCs, +/// where the outgoing message stream going to the server contains +/// messages of type \a W. +template +class ClientAsyncWriter final : public ClientAsyncWriterInterface { + public: + // always allocated against a call arena, no memory free required + static void operator delete(void* /*ptr*/, std::size_t size) { + GPR_CODEGEN_ASSERT(size == sizeof(ClientAsyncWriter)); + } + + // This operator should never be called as the memory should be freed as part + // of the arena destruction. It only exists to provide a matching operator + // delete to the operator new so that some compilers will not complain (see + // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this + // there are no tests catching the compiler warning. + static void operator delete(void*, void*) { GPR_CODEGEN_ASSERT(false); } + + void StartCall(void* tag) override { + GPR_CODEGEN_ASSERT(!started_); + started_ = true; + StartCallInternal(tag); + } + + /// See the \a ClientAsyncStreamingInterface.ReadInitialMetadata method for + /// semantics. + /// + /// Side effect: + /// - upon receiving initial metadata from the server, the \a ClientContext + /// associated with this call is updated, and the calling code can access + /// the received metadata through the \a ClientContext. + void ReadInitialMetadata(void* tag) override { + GPR_CODEGEN_ASSERT(started_); + GPR_CODEGEN_ASSERT(!context_->initial_metadata_received_); + + meta_ops_.set_output_tag(tag); + meta_ops_.RecvInitialMetadata(context_); + call_.PerformOps(&meta_ops_); + } + + void Write(const W& msg, void* tag) override { + GPR_CODEGEN_ASSERT(started_); + write_ops_.set_output_tag(tag); + // TODO(ctiller): don't assert + GPR_CODEGEN_ASSERT(write_ops_.SendMessage(msg).ok()); + call_.PerformOps(&write_ops_); + } + + void Write(const W& msg, ::grpc::WriteOptions options, void* tag) override { + GPR_CODEGEN_ASSERT(started_); + write_ops_.set_output_tag(tag); + if (options.is_last_message()) { + options.set_buffer_hint(); + write_ops_.ClientSendClose(); + } + // TODO(ctiller): don't assert + GPR_CODEGEN_ASSERT(write_ops_.SendMessage(msg, options).ok()); + call_.PerformOps(&write_ops_); + } + + void WritesDone(void* tag) override { + GPR_CODEGEN_ASSERT(started_); + write_ops_.set_output_tag(tag); + write_ops_.ClientSendClose(); + call_.PerformOps(&write_ops_); + } + + /// See the \a ClientAsyncStreamingInterface.Finish method for semantics. + /// + /// Side effect: + /// - the \a ClientContext associated with this call is updated with + /// possible initial and trailing metadata received from the server. + /// - attempts to fill in the \a response parameter passed to this class's + /// constructor with the server's response message. + void Finish(::grpc::Status* status, void* tag) override { + GPR_CODEGEN_ASSERT(started_); + finish_ops_.set_output_tag(tag); + if (!context_->initial_metadata_received_) { + finish_ops_.RecvInitialMetadata(context_); + } + finish_ops_.ClientRecvStatus(context_, status); + call_.PerformOps(&finish_ops_); + } + + private: + friend class internal::ClientAsyncWriterFactory; + template + ClientAsyncWriter(::grpc::internal::Call call, ::grpc::ClientContext* context, + R* response, bool start, void* tag) + : context_(context), call_(call), started_(start) { + finish_ops_.RecvMessage(response); + finish_ops_.AllowNoMessage(); + if (start) { + StartCallInternal(tag); + } else { + GPR_CODEGEN_ASSERT(tag == nullptr); + } + } + + void StartCallInternal(void* tag) { + write_ops_.SendInitialMetadata(&context_->send_initial_metadata_, + context_->initial_metadata_flags()); + // if corked bit is set in context, we just keep the initial metadata + // buffered up to coalesce with later message send. No op is performed. + if (!context_->initial_metadata_corked_) { + write_ops_.set_output_tag(tag); + call_.PerformOps(&write_ops_); + } + } + + ::grpc::ClientContext* context_; + ::grpc::internal::Call call_; + bool started_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata> + meta_ops_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, + ::grpc::internal::CallOpSendMessage, + ::grpc::internal::CallOpClientSendClose> + write_ops_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata, + ::grpc::internal::CallOpGenericRecvMessage, + ::grpc::internal::CallOpClientRecvStatus> + finish_ops_; +}; + +/// Async client-side interface for bi-directional streaming, +/// where the client-to-server message stream has messages of type \a W, +/// and the server-to-client message stream has messages of type \a R. +template +class ClientAsyncReaderWriterInterface + : public internal::ClientAsyncStreamingInterface, + public internal::AsyncWriterInterface, + public internal::AsyncReaderInterface { + public: + /// Signal the client is done with the writes (half-close the client stream). + /// Thread-safe with respect to \a AsyncReaderInterface::Read + /// + /// \param[in] tag The tag identifying the operation. + virtual void WritesDone(void* tag) = 0; +}; + +namespace internal { template -using ClientAsyncReaderWriterInterface = - ::grpc_impl::ClientAsyncReaderWriterInterface; +class ClientAsyncReaderWriterFactory { + public: + /// Create a stream object. + /// Start the RPC request if \a start is set. + /// \a tag will be notified on \a cq when the call has been started (i.e. + /// intitial metadata sent). If \a start is not set, \a tag must be + /// nullptr and the actual call must be initiated by StartCall + /// Note that \a context will be used to fill in custom initial metadata + /// used to send to the server when starting the call. + static ClientAsyncReaderWriter* Create( + ::grpc::ChannelInterface* channel, ::grpc::CompletionQueue* cq, + const ::grpc::internal::RpcMethod& method, ::grpc::ClientContext* context, + bool start, void* tag) { + ::grpc::internal::Call call = channel->CreateCall(method, context, cq); + return new (::grpc::g_core_codegen_interface->grpc_call_arena_alloc( + call.call(), sizeof(ClientAsyncReaderWriter))) + ClientAsyncReaderWriter(call, context, start, tag); + } +}; +} // namespace internal + +/// Async client-side interface for bi-directional streaming, +/// where the outgoing message stream going to the server +/// has messages of type \a W, and the incoming message stream coming +/// from the server has messages of type \a R. template -using ClientAsyncReaderWriter = ::grpc_impl::ClientAsyncReaderWriter; +class ClientAsyncReaderWriter final + : public ClientAsyncReaderWriterInterface { + public: + // always allocated against a call arena, no memory free required + static void operator delete(void* /*ptr*/, std::size_t size) { + GPR_CODEGEN_ASSERT(size == sizeof(ClientAsyncReaderWriter)); + } + + // This operator should never be called as the memory should be freed as part + // of the arena destruction. It only exists to provide a matching operator + // delete to the operator new so that some compilers will not complain (see + // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this + // there are no tests catching the compiler warning. + static void operator delete(void*, void*) { GPR_CODEGEN_ASSERT(false); } + + void StartCall(void* tag) override { + GPR_CODEGEN_ASSERT(!started_); + started_ = true; + StartCallInternal(tag); + } + + /// See the \a ClientAsyncStreamingInterface.ReadInitialMetadata method + /// for semantics of this method. + /// + /// Side effect: + /// - upon receiving initial metadata from the server, the \a ClientContext + /// is updated with it, and then the receiving initial metadata can + /// be accessed through this \a ClientContext. + void ReadInitialMetadata(void* tag) override { + GPR_CODEGEN_ASSERT(started_); + GPR_CODEGEN_ASSERT(!context_->initial_metadata_received_); + + meta_ops_.set_output_tag(tag); + meta_ops_.RecvInitialMetadata(context_); + call_.PerformOps(&meta_ops_); + } + + void Read(R* msg, void* tag) override { + GPR_CODEGEN_ASSERT(started_); + read_ops_.set_output_tag(tag); + if (!context_->initial_metadata_received_) { + read_ops_.RecvInitialMetadata(context_); + } + read_ops_.RecvMessage(msg); + call_.PerformOps(&read_ops_); + } + + void Write(const W& msg, void* tag) override { + GPR_CODEGEN_ASSERT(started_); + write_ops_.set_output_tag(tag); + // TODO(ctiller): don't assert + GPR_CODEGEN_ASSERT(write_ops_.SendMessage(msg).ok()); + call_.PerformOps(&write_ops_); + } + + void Write(const W& msg, ::grpc::WriteOptions options, void* tag) override { + GPR_CODEGEN_ASSERT(started_); + write_ops_.set_output_tag(tag); + if (options.is_last_message()) { + options.set_buffer_hint(); + write_ops_.ClientSendClose(); + } + // TODO(ctiller): don't assert + GPR_CODEGEN_ASSERT(write_ops_.SendMessage(msg, options).ok()); + call_.PerformOps(&write_ops_); + } + + void WritesDone(void* tag) override { + GPR_CODEGEN_ASSERT(started_); + write_ops_.set_output_tag(tag); + write_ops_.ClientSendClose(); + call_.PerformOps(&write_ops_); + } + + /// See the \a ClientAsyncStreamingInterface.Finish method for semantics. + /// Side effect + /// - the \a ClientContext associated with this call is updated with + /// possible initial and trailing metadata sent from the server. + void Finish(::grpc::Status* status, void* tag) override { + GPR_CODEGEN_ASSERT(started_); + finish_ops_.set_output_tag(tag); + if (!context_->initial_metadata_received_) { + finish_ops_.RecvInitialMetadata(context_); + } + finish_ops_.ClientRecvStatus(context_, status); + call_.PerformOps(&finish_ops_); + } + + private: + friend class internal::ClientAsyncReaderWriterFactory; + ClientAsyncReaderWriter(::grpc::internal::Call call, + ::grpc::ClientContext* context, bool start, void* tag) + : context_(context), call_(call), started_(start) { + if (start) { + StartCallInternal(tag); + } else { + GPR_CODEGEN_ASSERT(tag == nullptr); + } + } + + void StartCallInternal(void* tag) { + write_ops_.SendInitialMetadata(&context_->send_initial_metadata_, + context_->initial_metadata_flags()); + // if corked bit is set in context, we just keep the initial metadata + // buffered up to coalesce with later message send. No op is performed. + if (!context_->initial_metadata_corked_) { + write_ops_.set_output_tag(tag); + call_.PerformOps(&write_ops_); + } + } + + ::grpc::ClientContext* context_; + ::grpc::internal::Call call_; + bool started_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata> + meta_ops_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata, + ::grpc::internal::CallOpRecvMessage> + read_ops_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, + ::grpc::internal::CallOpSendMessage, + ::grpc::internal::CallOpClientSendClose> + write_ops_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata, + ::grpc::internal::CallOpClientRecvStatus> + finish_ops_; +}; template -using ServerAsyncReaderInterface = - ::grpc_impl::ServerAsyncReaderInterface; +class ServerAsyncReaderInterface + : public ::grpc::internal::ServerAsyncStreamingInterface, + public internal::AsyncReaderInterface { + public: + /// Indicate that the stream is to be finished with a certain status code + /// and also send out \a msg response to the client. + /// Request notification for when the server has sent the response and the + /// appropriate signals to the client to end the call. + /// Should not be used concurrently with other operations. + /// + /// It is appropriate to call this method when: + /// * all messages from the client have been received (either known + /// implictly, or explicitly because a previous + /// \a AsyncReaderInterface::Read operation with a non-ok result, + /// e.g., cq->Next(&read_tag, &ok) filled in 'ok' with 'false'). + /// + /// This operation will end when the server has finished sending out initial + /// metadata (if not sent already), response message, and status, or if + /// some failure occurred when trying to do so. + /// + /// gRPC doesn't take ownership or a reference to \a msg or \a status, so it + /// is safe to deallocate once Finish returns. + /// + /// \param[in] tag Tag identifying this request. + /// \param[in] status To be sent to the client as the result of this call. + /// \param[in] msg To be sent to the client as the response for this call. + virtual void Finish(const W& msg, const ::grpc::Status& status, + void* tag) = 0; + /// Indicate that the stream is to be finished with a certain + /// non-OK status code. + /// Request notification for when the server has sent the appropriate + /// signals to the client to end the call. + /// Should not be used concurrently with other operations. + /// + /// This call is meant to end the call with some error, and can be called at + /// any point that the server would like to "fail" the call (though note + /// this shouldn't be called concurrently with any other "sending" call, like + /// \a AsyncWriterInterface::Write). + /// + /// This operation will end when the server has finished sending out initial + /// metadata (if not sent already), and status, or if some failure occurred + /// when trying to do so. + /// + /// gRPC doesn't take ownership or a reference to \a status, so it is safe to + /// to deallocate once FinishWithError returns. + /// + /// \param[in] tag Tag identifying this request. + /// \param[in] status To be sent to the client as the result of this call. + /// - Note: \a status must have a non-OK code. + virtual void FinishWithError(const ::grpc::Status& status, void* tag) = 0; +}; + +/// Async server-side API for doing client-streaming RPCs, +/// where the incoming message stream from the client has messages of type \a R, +/// and the single response message sent from the server is type \a W. template -using ServerAsyncReader = ::grpc_impl::ServerAsyncReader; +class ServerAsyncReader final : public ServerAsyncReaderInterface { + public: + explicit ServerAsyncReader(::grpc::ServerContext* ctx) + : call_(nullptr, nullptr, nullptr), ctx_(ctx) {} + + /// See \a ServerAsyncStreamingInterface::SendInitialMetadata for semantics. + /// + /// Implicit input parameter: + /// - The initial metadata that will be sent to the client from this op will + /// be taken from the \a ServerContext associated with the call. + void SendInitialMetadata(void* tag) override { + GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_); + + meta_ops_.set_output_tag(tag); + meta_ops_.SendInitialMetadata(&ctx_->initial_metadata_, + ctx_->initial_metadata_flags()); + if (ctx_->compression_level_set()) { + meta_ops_.set_compression_level(ctx_->compression_level()); + } + ctx_->sent_initial_metadata_ = true; + call_.PerformOps(&meta_ops_); + } + + void Read(R* msg, void* tag) override { + read_ops_.set_output_tag(tag); + read_ops_.RecvMessage(msg); + call_.PerformOps(&read_ops_); + } + + /// See the \a ServerAsyncReaderInterface.Read method for semantics + /// + /// Side effect: + /// - also sends initial metadata if not alreay sent. + /// - uses the \a ServerContext associated with this call to send possible + /// initial and trailing metadata. + /// + /// Note: \a msg is not sent if \a status has a non-OK code. + /// + /// gRPC doesn't take ownership or a reference to \a msg and \a status, so it + /// is safe to deallocate once Finish returns. + void Finish(const W& msg, const ::grpc::Status& status, void* tag) override { + finish_ops_.set_output_tag(tag); + if (!ctx_->sent_initial_metadata_) { + finish_ops_.SendInitialMetadata(&ctx_->initial_metadata_, + ctx_->initial_metadata_flags()); + if (ctx_->compression_level_set()) { + finish_ops_.set_compression_level(ctx_->compression_level()); + } + ctx_->sent_initial_metadata_ = true; + } + // The response is dropped if the status is not OK. + if (status.ok()) { + finish_ops_.ServerSendStatus(&ctx_->trailing_metadata_, + finish_ops_.SendMessage(msg)); + } else { + finish_ops_.ServerSendStatus(&ctx_->trailing_metadata_, status); + } + call_.PerformOps(&finish_ops_); + } + + /// See the \a ServerAsyncReaderInterface.Read method for semantics + /// + /// Side effect: + /// - also sends initial metadata if not alreay sent. + /// - uses the \a ServerContext associated with this call to send possible + /// initial and trailing metadata. + /// + /// gRPC doesn't take ownership or a reference to \a status, so it is safe to + /// to deallocate once FinishWithError returns. + void FinishWithError(const ::grpc::Status& status, void* tag) override { + GPR_CODEGEN_ASSERT(!status.ok()); + finish_ops_.set_output_tag(tag); + if (!ctx_->sent_initial_metadata_) { + finish_ops_.SendInitialMetadata(&ctx_->initial_metadata_, + ctx_->initial_metadata_flags()); + if (ctx_->compression_level_set()) { + finish_ops_.set_compression_level(ctx_->compression_level()); + } + ctx_->sent_initial_metadata_ = true; + } + finish_ops_.ServerSendStatus(&ctx_->trailing_metadata_, status); + call_.PerformOps(&finish_ops_); + } + + private: + void BindCall(::grpc::internal::Call* call) override { call_ = *call; } + + ::grpc::internal::Call call_; + ::grpc::ServerContext* ctx_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata> + meta_ops_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvMessage> read_ops_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, + ::grpc::internal::CallOpSendMessage, + ::grpc::internal::CallOpServerSendStatus> + finish_ops_; +}; template -using ServerAsyncWriterInterface = ::grpc_impl::ServerAsyncWriterInterface; +class ServerAsyncWriterInterface + : public ::grpc::internal::ServerAsyncStreamingInterface, + public internal::AsyncWriterInterface { + public: + /// Indicate that the stream is to be finished with a certain status code. + /// Request notification for when the server has sent the appropriate + /// signals to the client to end the call. + /// Should not be used concurrently with other operations. + /// + /// It is appropriate to call this method when either: + /// * all messages from the client have been received (either known + /// implictly, or explicitly because a previous \a + /// AsyncReaderInterface::Read operation with a non-ok + /// result (e.g., cq->Next(&read_tag, &ok) filled in 'ok' with 'false'. + /// * it is desired to end the call early with some non-OK status code. + /// + /// This operation will end when the server has finished sending out initial + /// metadata (if not sent already), response message, and status, or if + /// some failure occurred when trying to do so. + /// + /// gRPC doesn't take ownership or a reference to \a status, so it is safe to + /// to deallocate once Finish returns. + /// + /// \param[in] tag Tag identifying this request. + /// \param[in] status To be sent to the client as the result of this call. + virtual void Finish(const ::grpc::Status& status, void* tag) = 0; + /// Request the writing of \a msg and coalesce it with trailing metadata which + /// contains \a status, using WriteOptions options with + /// identifying tag \a tag. + /// + /// WriteAndFinish is equivalent of performing WriteLast and Finish + /// in a single step. + /// + /// gRPC doesn't take ownership or a reference to \a msg and \a status, so it + /// is safe to deallocate once WriteAndFinish returns. + /// + /// \param[in] msg The message to be written. + /// \param[in] options The WriteOptions to be used to write this message. + /// \param[in] status The Status that server returns to client. + /// \param[in] tag The tag identifying the operation. + virtual void WriteAndFinish(const W& msg, ::grpc::WriteOptions options, + const ::grpc::Status& status, void* tag) = 0; +}; + +/// Async server-side API for doing server streaming RPCs, +/// where the outgoing message stream from the server has messages of type \a W. template -using ServerAsyncWriter = ::grpc_impl::ServerAsyncWriter; +class ServerAsyncWriter final : public ServerAsyncWriterInterface { + public: + explicit ServerAsyncWriter(::grpc::ServerContext* ctx) + : call_(nullptr, nullptr, nullptr), ctx_(ctx) {} + + /// See \a ServerAsyncStreamingInterface::SendInitialMetadata for semantics. + /// + /// Implicit input parameter: + /// - The initial metadata that will be sent to the client from this op will + /// be taken from the \a ServerContext associated with the call. + /// + /// \param[in] tag Tag identifying this request. + void SendInitialMetadata(void* tag) override { + GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_); + + meta_ops_.set_output_tag(tag); + meta_ops_.SendInitialMetadata(&ctx_->initial_metadata_, + ctx_->initial_metadata_flags()); + if (ctx_->compression_level_set()) { + meta_ops_.set_compression_level(ctx_->compression_level()); + } + ctx_->sent_initial_metadata_ = true; + call_.PerformOps(&meta_ops_); + } + + void Write(const W& msg, void* tag) override { + write_ops_.set_output_tag(tag); + EnsureInitialMetadataSent(&write_ops_); + // TODO(ctiller): don't assert + GPR_CODEGEN_ASSERT(write_ops_.SendMessage(msg).ok()); + call_.PerformOps(&write_ops_); + } + + void Write(const W& msg, ::grpc::WriteOptions options, void* tag) override { + write_ops_.set_output_tag(tag); + if (options.is_last_message()) { + options.set_buffer_hint(); + } + EnsureInitialMetadataSent(&write_ops_); + // TODO(ctiller): don't assert + GPR_CODEGEN_ASSERT(write_ops_.SendMessage(msg, options).ok()); + call_.PerformOps(&write_ops_); + } + + /// See the \a ServerAsyncWriterInterface.WriteAndFinish method for semantics. + /// + /// Implicit input parameter: + /// - the \a ServerContext associated with this call is used + /// for sending trailing (and initial) metadata to the client. + /// + /// Note: \a status must have an OK code. + /// + /// gRPC doesn't take ownership or a reference to \a msg and \a status, so it + /// is safe to deallocate once WriteAndFinish returns. + void WriteAndFinish(const W& msg, ::grpc::WriteOptions options, + const ::grpc::Status& status, void* tag) override { + write_ops_.set_output_tag(tag); + EnsureInitialMetadataSent(&write_ops_); + options.set_buffer_hint(); + GPR_CODEGEN_ASSERT(write_ops_.SendMessage(msg, options).ok()); + write_ops_.ServerSendStatus(&ctx_->trailing_metadata_, status); + call_.PerformOps(&write_ops_); + } + + /// See the \a ServerAsyncWriterInterface.Finish method for semantics. + /// + /// Implicit input parameter: + /// - the \a ServerContext associated with this call is used for sending + /// trailing (and initial if not already sent) metadata to the client. + /// + /// Note: there are no restrictions are the code of + /// \a status,it may be non-OK + /// + /// gRPC doesn't take ownership or a reference to \a status, so it is safe to + /// to deallocate once Finish returns. + void Finish(const ::grpc::Status& status, void* tag) override { + finish_ops_.set_output_tag(tag); + EnsureInitialMetadataSent(&finish_ops_); + finish_ops_.ServerSendStatus(&ctx_->trailing_metadata_, status); + call_.PerformOps(&finish_ops_); + } + + private: + void BindCall(::grpc::internal::Call* call) override { call_ = *call; } + + template + void EnsureInitialMetadataSent(T* ops) { + if (!ctx_->sent_initial_metadata_) { + ops->SendInitialMetadata(&ctx_->initial_metadata_, + ctx_->initial_metadata_flags()); + if (ctx_->compression_level_set()) { + ops->set_compression_level(ctx_->compression_level()); + } + ctx_->sent_initial_metadata_ = true; + } + } + + ::grpc::internal::Call call_; + ::grpc::ServerContext* ctx_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata> + meta_ops_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, + ::grpc::internal::CallOpSendMessage, + ::grpc::internal::CallOpServerSendStatus> + write_ops_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, + ::grpc::internal::CallOpServerSendStatus> + finish_ops_; +}; + +/// Server-side interface for asynchronous bi-directional streaming. template -using ServerAsyncReaderWriterInterface = - ::grpc_impl::ServerAsyncReaderWriterInterface; +class ServerAsyncReaderWriterInterface + : public ::grpc::internal::ServerAsyncStreamingInterface, + public internal::AsyncWriterInterface, + public internal::AsyncReaderInterface { + public: + /// Indicate that the stream is to be finished with a certain status code. + /// Request notification for when the server has sent the appropriate + /// signals to the client to end the call. + /// Should not be used concurrently with other operations. + /// + /// It is appropriate to call this method when either: + /// * all messages from the client have been received (either known + /// implictly, or explicitly because a previous \a + /// AsyncReaderInterface::Read operation + /// with a non-ok result (e.g., cq->Next(&read_tag, &ok) filled in 'ok' + /// with 'false'. + /// * it is desired to end the call early with some non-OK status code. + /// + /// This operation will end when the server has finished sending out initial + /// metadata (if not sent already), response message, and status, or if some + /// failure occurred when trying to do so. + /// + /// gRPC doesn't take ownership or a reference to \a status, so it is safe to + /// to deallocate once Finish returns. + /// + /// \param[in] tag Tag identifying this request. + /// \param[in] status To be sent to the client as the result of this call. + virtual void Finish(const ::grpc::Status& status, void* tag) = 0; + /// Request the writing of \a msg and coalesce it with trailing metadata which + /// contains \a status, using WriteOptions options with + /// identifying tag \a tag. + /// + /// WriteAndFinish is equivalent of performing WriteLast and Finish in a + /// single step. + /// + /// gRPC doesn't take ownership or a reference to \a msg and \a status, so it + /// is safe to deallocate once WriteAndFinish returns. + /// + /// \param[in] msg The message to be written. + /// \param[in] options The WriteOptions to be used to write this message. + /// \param[in] status The Status that server returns to client. + /// \param[in] tag The tag identifying the operation. + virtual void WriteAndFinish(const W& msg, ::grpc::WriteOptions options, + const ::grpc::Status& status, void* tag) = 0; +}; + +/// Async server-side API for doing bidirectional streaming RPCs, +/// where the incoming message stream coming from the client has messages of +/// type \a R, and the outgoing message stream coming from the server has +/// messages of type \a W. template -using ServerAsyncReaderWriter = ::grpc_impl::ServerAsyncReaderWriter; +class ServerAsyncReaderWriter final + : public ServerAsyncReaderWriterInterface { + public: + explicit ServerAsyncReaderWriter(::grpc::ServerContext* ctx) + : call_(nullptr, nullptr, nullptr), ctx_(ctx) {} -namespace internal { -template -using ClientAsyncReaderFactory = - ::grpc_impl::internal::ClientAsyncReaderFactory; + /// See \a ServerAsyncStreamingInterface::SendInitialMetadata for semantics. + /// + /// Implicit input parameter: + /// - The initial metadata that will be sent to the client from this op will + /// be taken from the \a ServerContext associated with the call. + /// + /// \param[in] tag Tag identifying this request. + void SendInitialMetadata(void* tag) override { + GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_); -template -using ClientAsyncWriterFactory = - ::grpc_impl::internal::ClientAsyncWriterFactory; + meta_ops_.set_output_tag(tag); + meta_ops_.SendInitialMetadata(&ctx_->initial_metadata_, + ctx_->initial_metadata_flags()); + if (ctx_->compression_level_set()) { + meta_ops_.set_compression_level(ctx_->compression_level()); + } + ctx_->sent_initial_metadata_ = true; + call_.PerformOps(&meta_ops_); + } -template -using ClientAsyncReaderWriterFactory = - ::grpc_impl::internal::ClientAsyncReaderWriterFactory; + void Read(R* msg, void* tag) override { + read_ops_.set_output_tag(tag); + read_ops_.RecvMessage(msg); + call_.PerformOps(&read_ops_); + } -} // namespace internal + void Write(const W& msg, void* tag) override { + write_ops_.set_output_tag(tag); + EnsureInitialMetadataSent(&write_ops_); + // TODO(ctiller): don't assert + GPR_CODEGEN_ASSERT(write_ops_.SendMessage(msg).ok()); + call_.PerformOps(&write_ops_); + } -} // namespace grpc + void Write(const W& msg, ::grpc::WriteOptions options, void* tag) override { + write_ops_.set_output_tag(tag); + if (options.is_last_message()) { + options.set_buffer_hint(); + } + EnsureInitialMetadataSent(&write_ops_); + GPR_CODEGEN_ASSERT(write_ops_.SendMessage(msg, options).ok()); + call_.PerformOps(&write_ops_); + } + + /// See the \a ServerAsyncReaderWriterInterface.WriteAndFinish + /// method for semantics. + /// + /// Implicit input parameter: + /// - the \a ServerContext associated with this call is used + /// for sending trailing (and initial) metadata to the client. + /// + /// Note: \a status must have an OK code. + // + /// gRPC doesn't take ownership or a reference to \a msg and \a status, so it + /// is safe to deallocate once WriteAndFinish returns. + void WriteAndFinish(const W& msg, ::grpc::WriteOptions options, + const ::grpc::Status& status, void* tag) override { + write_ops_.set_output_tag(tag); + EnsureInitialMetadataSent(&write_ops_); + options.set_buffer_hint(); + GPR_CODEGEN_ASSERT(write_ops_.SendMessage(msg, options).ok()); + write_ops_.ServerSendStatus(&ctx_->trailing_metadata_, status); + call_.PerformOps(&write_ops_); + } + + /// See the \a ServerAsyncReaderWriterInterface.Finish method for semantics. + /// + /// Implicit input parameter: + /// - the \a ServerContext associated with this call is used for sending + /// trailing (and initial if not already sent) metadata to the client. + /// + /// Note: there are no restrictions are the code of \a status, + /// it may be non-OK + // + /// gRPC doesn't take ownership or a reference to \a status, so it is safe to + /// to deallocate once Finish returns. + void Finish(const ::grpc::Status& status, void* tag) override { + finish_ops_.set_output_tag(tag); + EnsureInitialMetadataSent(&finish_ops_); + + finish_ops_.ServerSendStatus(&ctx_->trailing_metadata_, status); + call_.PerformOps(&finish_ops_); + } + private: + friend class ::grpc::Server; + + void BindCall(::grpc::internal::Call* call) override { call_ = *call; } + + template + void EnsureInitialMetadataSent(T* ops) { + if (!ctx_->sent_initial_metadata_) { + ops->SendInitialMetadata(&ctx_->initial_metadata_, + ctx_->initial_metadata_flags()); + if (ctx_->compression_level_set()) { + ops->set_compression_level(ctx_->compression_level()); + } + ctx_->sent_initial_metadata_ = true; + } + } + + ::grpc::internal::Call call_; + ::grpc::ServerContext* ctx_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata> + meta_ops_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvMessage> read_ops_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, + ::grpc::internal::CallOpSendMessage, + ::grpc::internal::CallOpServerSendStatus> + write_ops_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, + ::grpc::internal::CallOpServerSendStatus> + finish_ops_; +}; + +} // namespace grpc #endif // GRPCPP_IMPL_CODEGEN_ASYNC_STREAM_H diff --git a/include/grpcpp/impl/codegen/async_stream_impl.h b/include/grpcpp/impl/codegen/async_stream_impl.h deleted file mode 100644 index bb1c2ef6fb3..00000000000 --- a/include/grpcpp/impl/codegen/async_stream_impl.h +++ /dev/null @@ -1,1131 +0,0 @@ -/* - * - * Copyright 2019 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef GRPCPP_IMPL_CODEGEN_ASYNC_STREAM_IMPL_H -#define GRPCPP_IMPL_CODEGEN_ASYNC_STREAM_IMPL_H - -#include -#include -#include -#include -#include -#include - -namespace grpc_impl { - -namespace internal { -/// Common interface for all client side asynchronous streaming. -class ClientAsyncStreamingInterface { - public: - virtual ~ClientAsyncStreamingInterface() {} - - /// Start the call that was set up by the constructor, but only if the - /// constructor was invoked through the "Prepare" API which doesn't actually - /// start the call - virtual void StartCall(void* tag) = 0; - - /// Request notification of the reading of the initial metadata. Completion - /// will be notified by \a tag on the associated completion queue. - /// This call is optional, but if it is used, it cannot be used concurrently - /// with or after the \a AsyncReaderInterface::Read method. - /// - /// \param[in] tag Tag identifying this request. - virtual void ReadInitialMetadata(void* tag) = 0; - - /// Indicate that the stream is to be finished and request notification for - /// when the call has been ended. - /// Should not be used concurrently with other operations. - /// - /// It is appropriate to call this method exactly once when both: - /// * the client side has no more message to send - /// (this can be declared implicitly by calling this method, or - /// explicitly through an earlier call to the WritesDone method - /// of the class in use, e.g. \a ClientAsyncWriterInterface::WritesDone or - /// \a ClientAsyncReaderWriterInterface::WritesDone). - /// * there are no more messages to be received from the server (this can - /// be known implicitly by the calling code, or explicitly from an - /// earlier call to \a AsyncReaderInterface::Read that yielded a failed - /// result, e.g. cq->Next(&read_tag, &ok) filled in 'ok' with 'false'). - /// - /// The tag will be returned when either: - /// - all incoming messages have been read and the server has returned - /// a status. - /// - the server has returned a non-OK status. - /// - the call failed for some reason and the library generated a - /// status. - /// - /// Note that implementations of this method attempt to receive initial - /// metadata from the server if initial metadata hasn't yet been received. - /// - /// \param[in] tag Tag identifying this request. - /// \param[out] status To be updated with the operation status. - virtual void Finish(::grpc::Status* status, void* tag) = 0; -}; - -/// An interface that yields a sequence of messages of type \a R. -template -class AsyncReaderInterface { - public: - virtual ~AsyncReaderInterface() {} - - /// Read a message of type \a R into \a msg. Completion will be notified by \a - /// tag on the associated completion queue. - /// This is thread-safe with respect to \a Write or \a WritesDone methods. It - /// should not be called concurrently with other streaming APIs - /// on the same stream. It is not meaningful to call it concurrently - /// with another \a AsyncReaderInterface::Read on the same stream since reads - /// on the same stream are delivered in order. - /// - /// \param[out] msg Where to eventually store the read message. - /// \param[in] tag The tag identifying the operation. - /// - /// Side effect: note that this method attempt to receive initial metadata for - /// a stream if it hasn't yet been received. - virtual void Read(R* msg, void* tag) = 0; -}; - -/// An interface that can be fed a sequence of messages of type \a W. -template -class AsyncWriterInterface { - public: - virtual ~AsyncWriterInterface() {} - - /// Request the writing of \a msg with identifying tag \a tag. - /// - /// Only one write may be outstanding at any given time. This means that - /// after calling Write, one must wait to receive \a tag from the completion - /// queue BEFORE calling Write again. - /// This is thread-safe with respect to \a AsyncReaderInterface::Read - /// - /// gRPC doesn't take ownership or a reference to \a msg, so it is safe to - /// to deallocate once Write returns. - /// - /// \param[in] msg The message to be written. - /// \param[in] tag The tag identifying the operation. - virtual void Write(const W& msg, void* tag) = 0; - - /// Request the writing of \a msg using WriteOptions \a options with - /// identifying tag \a tag. - /// - /// Only one write may be outstanding at any given time. This means that - /// after calling Write, one must wait to receive \a tag from the completion - /// queue BEFORE calling Write again. - /// WriteOptions \a options is used to set the write options of this message. - /// This is thread-safe with respect to \a AsyncReaderInterface::Read - /// - /// gRPC doesn't take ownership or a reference to \a msg, so it is safe to - /// to deallocate once Write returns. - /// - /// \param[in] msg The message to be written. - /// \param[in] options The WriteOptions to be used to write this message. - /// \param[in] tag The tag identifying the operation. - virtual void Write(const W& msg, ::grpc::WriteOptions options, void* tag) = 0; - - /// Request the writing of \a msg and coalesce it with the writing - /// of trailing metadata, using WriteOptions \a options with - /// identifying tag \a tag. - /// - /// For client, WriteLast is equivalent of performing Write and - /// WritesDone in a single step. - /// For server, WriteLast buffers the \a msg. The writing of \a msg is held - /// until Finish is called, where \a msg and trailing metadata are coalesced - /// and write is initiated. Note that WriteLast can only buffer \a msg up to - /// the flow control window size. If \a msg size is larger than the window - /// size, it will be sent on wire without buffering. - /// - /// gRPC doesn't take ownership or a reference to \a msg, so it is safe to - /// to deallocate once Write returns. - /// - /// \param[in] msg The message to be written. - /// \param[in] options The WriteOptions to be used to write this message. - /// \param[in] tag The tag identifying the operation. - void WriteLast(const W& msg, ::grpc::WriteOptions options, void* tag) { - Write(msg, options.set_last_message(), tag); - } -}; - -} // namespace internal - -template -class ClientAsyncReaderInterface - : public internal::ClientAsyncStreamingInterface, - public internal::AsyncReaderInterface {}; - -namespace internal { -template -class ClientAsyncReaderFactory { - public: - /// Create a stream object. - /// Write the first request out if \a start is set. - /// \a tag will be notified on \a cq when the call has been started and - /// \a request has been written out. If \a start is not set, \a tag must be - /// nullptr and the actual call must be initiated by StartCall - /// Note that \a context will be used to fill in custom initial metadata - /// used to send to the server when starting the call. - template - static ClientAsyncReader* Create(::grpc::ChannelInterface* channel, - ::grpc::CompletionQueue* cq, - const ::grpc::internal::RpcMethod& method, - ::grpc::ClientContext* context, - const W& request, bool start, void* tag) { - ::grpc::internal::Call call = channel->CreateCall(method, context, cq); - return new (::grpc::g_core_codegen_interface->grpc_call_arena_alloc( - call.call(), sizeof(ClientAsyncReader))) - ClientAsyncReader(call, context, request, start, tag); - } -}; -} // namespace internal - -/// Async client-side API for doing server-streaming RPCs, -/// where the incoming message stream coming from the server has -/// messages of type \a R. -template -class ClientAsyncReader final : public ClientAsyncReaderInterface { - public: - // always allocated against a call arena, no memory free required - static void operator delete(void* /*ptr*/, std::size_t size) { - GPR_CODEGEN_ASSERT(size == sizeof(ClientAsyncReader)); - } - - // This operator should never be called as the memory should be freed as part - // of the arena destruction. It only exists to provide a matching operator - // delete to the operator new so that some compilers will not complain (see - // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this - // there are no tests catching the compiler warning. - static void operator delete(void*, void*) { GPR_CODEGEN_ASSERT(false); } - - void StartCall(void* tag) override { - GPR_CODEGEN_ASSERT(!started_); - started_ = true; - StartCallInternal(tag); - } - - /// See the \a ClientAsyncStreamingInterface.ReadInitialMetadata - /// method for semantics. - /// - /// Side effect: - /// - upon receiving initial metadata from the server, - /// the \a ClientContext associated with this call is updated, and the - /// calling code can access the received metadata through the - /// \a ClientContext. - void ReadInitialMetadata(void* tag) override { - GPR_CODEGEN_ASSERT(started_); - GPR_CODEGEN_ASSERT(!context_->initial_metadata_received_); - - meta_ops_.set_output_tag(tag); - meta_ops_.RecvInitialMetadata(context_); - call_.PerformOps(&meta_ops_); - } - - void Read(R* msg, void* tag) override { - GPR_CODEGEN_ASSERT(started_); - read_ops_.set_output_tag(tag); - if (!context_->initial_metadata_received_) { - read_ops_.RecvInitialMetadata(context_); - } - read_ops_.RecvMessage(msg); - call_.PerformOps(&read_ops_); - } - - /// See the \a ClientAsyncStreamingInterface.Finish method for semantics. - /// - /// Side effect: - /// - the \a ClientContext associated with this call is updated with - /// possible initial and trailing metadata received from the server. - void Finish(::grpc::Status* status, void* tag) override { - GPR_CODEGEN_ASSERT(started_); - finish_ops_.set_output_tag(tag); - if (!context_->initial_metadata_received_) { - finish_ops_.RecvInitialMetadata(context_); - } - finish_ops_.ClientRecvStatus(context_, status); - call_.PerformOps(&finish_ops_); - } - - private: - friend class internal::ClientAsyncReaderFactory; - template - ClientAsyncReader(::grpc::internal::Call call, ::grpc::ClientContext* context, - const W& request, bool start, void* tag) - : context_(context), call_(call), started_(start) { - // TODO(ctiller): don't assert - GPR_CODEGEN_ASSERT(init_ops_.SendMessage(request).ok()); - init_ops_.ClientSendClose(); - if (start) { - StartCallInternal(tag); - } else { - GPR_CODEGEN_ASSERT(tag == nullptr); - } - } - - void StartCallInternal(void* tag) { - init_ops_.SendInitialMetadata(&context_->send_initial_metadata_, - context_->initial_metadata_flags()); - init_ops_.set_output_tag(tag); - call_.PerformOps(&init_ops_); - } - - ::grpc::ClientContext* context_; - ::grpc::internal::Call call_; - bool started_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, - ::grpc::internal::CallOpSendMessage, - ::grpc::internal::CallOpClientSendClose> - init_ops_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata> - meta_ops_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata, - ::grpc::internal::CallOpRecvMessage> - read_ops_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata, - ::grpc::internal::CallOpClientRecvStatus> - finish_ops_; -}; - -/// Common interface for client side asynchronous writing. -template -class ClientAsyncWriterInterface - : public internal::ClientAsyncStreamingInterface, - public internal::AsyncWriterInterface { - public: - /// Signal the client is done with the writes (half-close the client stream). - /// Thread-safe with respect to \a AsyncReaderInterface::Read - /// - /// \param[in] tag The tag identifying the operation. - virtual void WritesDone(void* tag) = 0; -}; - -namespace internal { -template -class ClientAsyncWriterFactory { - public: - /// Create a stream object. - /// Start the RPC if \a start is set - /// \a tag will be notified on \a cq when the call has been started (i.e. - /// intitial metadata sent) and \a request has been written out. - /// If \a start is not set, \a tag must be nullptr and the actual call - /// must be initiated by StartCall - /// Note that \a context will be used to fill in custom initial metadata - /// used to send to the server when starting the call. - /// \a response will be filled in with the single expected response - /// message from the server upon a successful call to the \a Finish - /// method of this instance. - template - static ClientAsyncWriter* Create(::grpc::ChannelInterface* channel, - ::grpc::CompletionQueue* cq, - const ::grpc::internal::RpcMethod& method, - ::grpc::ClientContext* context, - R* response, bool start, void* tag) { - ::grpc::internal::Call call = channel->CreateCall(method, context, cq); - return new (::grpc::g_core_codegen_interface->grpc_call_arena_alloc( - call.call(), sizeof(ClientAsyncWriter))) - ClientAsyncWriter(call, context, response, start, tag); - } -}; -} // namespace internal - -/// Async API on the client side for doing client-streaming RPCs, -/// where the outgoing message stream going to the server contains -/// messages of type \a W. -template -class ClientAsyncWriter final : public ClientAsyncWriterInterface { - public: - // always allocated against a call arena, no memory free required - static void operator delete(void* /*ptr*/, std::size_t size) { - GPR_CODEGEN_ASSERT(size == sizeof(ClientAsyncWriter)); - } - - // This operator should never be called as the memory should be freed as part - // of the arena destruction. It only exists to provide a matching operator - // delete to the operator new so that some compilers will not complain (see - // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this - // there are no tests catching the compiler warning. - static void operator delete(void*, void*) { GPR_CODEGEN_ASSERT(false); } - - void StartCall(void* tag) override { - GPR_CODEGEN_ASSERT(!started_); - started_ = true; - StartCallInternal(tag); - } - - /// See the \a ClientAsyncStreamingInterface.ReadInitialMetadata method for - /// semantics. - /// - /// Side effect: - /// - upon receiving initial metadata from the server, the \a ClientContext - /// associated with this call is updated, and the calling code can access - /// the received metadata through the \a ClientContext. - void ReadInitialMetadata(void* tag) override { - GPR_CODEGEN_ASSERT(started_); - GPR_CODEGEN_ASSERT(!context_->initial_metadata_received_); - - meta_ops_.set_output_tag(tag); - meta_ops_.RecvInitialMetadata(context_); - call_.PerformOps(&meta_ops_); - } - - void Write(const W& msg, void* tag) override { - GPR_CODEGEN_ASSERT(started_); - write_ops_.set_output_tag(tag); - // TODO(ctiller): don't assert - GPR_CODEGEN_ASSERT(write_ops_.SendMessage(msg).ok()); - call_.PerformOps(&write_ops_); - } - - void Write(const W& msg, ::grpc::WriteOptions options, void* tag) override { - GPR_CODEGEN_ASSERT(started_); - write_ops_.set_output_tag(tag); - if (options.is_last_message()) { - options.set_buffer_hint(); - write_ops_.ClientSendClose(); - } - // TODO(ctiller): don't assert - GPR_CODEGEN_ASSERT(write_ops_.SendMessage(msg, options).ok()); - call_.PerformOps(&write_ops_); - } - - void WritesDone(void* tag) override { - GPR_CODEGEN_ASSERT(started_); - write_ops_.set_output_tag(tag); - write_ops_.ClientSendClose(); - call_.PerformOps(&write_ops_); - } - - /// See the \a ClientAsyncStreamingInterface.Finish method for semantics. - /// - /// Side effect: - /// - the \a ClientContext associated with this call is updated with - /// possible initial and trailing metadata received from the server. - /// - attempts to fill in the \a response parameter passed to this class's - /// constructor with the server's response message. - void Finish(::grpc::Status* status, void* tag) override { - GPR_CODEGEN_ASSERT(started_); - finish_ops_.set_output_tag(tag); - if (!context_->initial_metadata_received_) { - finish_ops_.RecvInitialMetadata(context_); - } - finish_ops_.ClientRecvStatus(context_, status); - call_.PerformOps(&finish_ops_); - } - - private: - friend class internal::ClientAsyncWriterFactory; - template - ClientAsyncWriter(::grpc::internal::Call call, ::grpc::ClientContext* context, - R* response, bool start, void* tag) - : context_(context), call_(call), started_(start) { - finish_ops_.RecvMessage(response); - finish_ops_.AllowNoMessage(); - if (start) { - StartCallInternal(tag); - } else { - GPR_CODEGEN_ASSERT(tag == nullptr); - } - } - - void StartCallInternal(void* tag) { - write_ops_.SendInitialMetadata(&context_->send_initial_metadata_, - context_->initial_metadata_flags()); - // if corked bit is set in context, we just keep the initial metadata - // buffered up to coalesce with later message send. No op is performed. - if (!context_->initial_metadata_corked_) { - write_ops_.set_output_tag(tag); - call_.PerformOps(&write_ops_); - } - } - - ::grpc::ClientContext* context_; - ::grpc::internal::Call call_; - bool started_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata> - meta_ops_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, - ::grpc::internal::CallOpSendMessage, - ::grpc::internal::CallOpClientSendClose> - write_ops_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata, - ::grpc::internal::CallOpGenericRecvMessage, - ::grpc::internal::CallOpClientRecvStatus> - finish_ops_; -}; - -/// Async client-side interface for bi-directional streaming, -/// where the client-to-server message stream has messages of type \a W, -/// and the server-to-client message stream has messages of type \a R. -template -class ClientAsyncReaderWriterInterface - : public internal::ClientAsyncStreamingInterface, - public internal::AsyncWriterInterface, - public internal::AsyncReaderInterface { - public: - /// Signal the client is done with the writes (half-close the client stream). - /// Thread-safe with respect to \a AsyncReaderInterface::Read - /// - /// \param[in] tag The tag identifying the operation. - virtual void WritesDone(void* tag) = 0; -}; - -namespace internal { -template -class ClientAsyncReaderWriterFactory { - public: - /// Create a stream object. - /// Start the RPC request if \a start is set. - /// \a tag will be notified on \a cq when the call has been started (i.e. - /// intitial metadata sent). If \a start is not set, \a tag must be - /// nullptr and the actual call must be initiated by StartCall - /// Note that \a context will be used to fill in custom initial metadata - /// used to send to the server when starting the call. - static ClientAsyncReaderWriter* Create( - ::grpc::ChannelInterface* channel, ::grpc::CompletionQueue* cq, - const ::grpc::internal::RpcMethod& method, ::grpc::ClientContext* context, - bool start, void* tag) { - ::grpc::internal::Call call = channel->CreateCall(method, context, cq); - - return new (::grpc::g_core_codegen_interface->grpc_call_arena_alloc( - call.call(), sizeof(ClientAsyncReaderWriter))) - ClientAsyncReaderWriter(call, context, start, tag); - } -}; -} // namespace internal - -/// Async client-side interface for bi-directional streaming, -/// where the outgoing message stream going to the server -/// has messages of type \a W, and the incoming message stream coming -/// from the server has messages of type \a R. -template -class ClientAsyncReaderWriter final - : public ClientAsyncReaderWriterInterface { - public: - // always allocated against a call arena, no memory free required - static void operator delete(void* /*ptr*/, std::size_t size) { - GPR_CODEGEN_ASSERT(size == sizeof(ClientAsyncReaderWriter)); - } - - // This operator should never be called as the memory should be freed as part - // of the arena destruction. It only exists to provide a matching operator - // delete to the operator new so that some compilers will not complain (see - // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this - // there are no tests catching the compiler warning. - static void operator delete(void*, void*) { GPR_CODEGEN_ASSERT(false); } - - void StartCall(void* tag) override { - GPR_CODEGEN_ASSERT(!started_); - started_ = true; - StartCallInternal(tag); - } - - /// See the \a ClientAsyncStreamingInterface.ReadInitialMetadata method - /// for semantics of this method. - /// - /// Side effect: - /// - upon receiving initial metadata from the server, the \a ClientContext - /// is updated with it, and then the receiving initial metadata can - /// be accessed through this \a ClientContext. - void ReadInitialMetadata(void* tag) override { - GPR_CODEGEN_ASSERT(started_); - GPR_CODEGEN_ASSERT(!context_->initial_metadata_received_); - - meta_ops_.set_output_tag(tag); - meta_ops_.RecvInitialMetadata(context_); - call_.PerformOps(&meta_ops_); - } - - void Read(R* msg, void* tag) override { - GPR_CODEGEN_ASSERT(started_); - read_ops_.set_output_tag(tag); - if (!context_->initial_metadata_received_) { - read_ops_.RecvInitialMetadata(context_); - } - read_ops_.RecvMessage(msg); - call_.PerformOps(&read_ops_); - } - - void Write(const W& msg, void* tag) override { - GPR_CODEGEN_ASSERT(started_); - write_ops_.set_output_tag(tag); - // TODO(ctiller): don't assert - GPR_CODEGEN_ASSERT(write_ops_.SendMessage(msg).ok()); - call_.PerformOps(&write_ops_); - } - - void Write(const W& msg, ::grpc::WriteOptions options, void* tag) override { - GPR_CODEGEN_ASSERT(started_); - write_ops_.set_output_tag(tag); - if (options.is_last_message()) { - options.set_buffer_hint(); - write_ops_.ClientSendClose(); - } - // TODO(ctiller): don't assert - GPR_CODEGEN_ASSERT(write_ops_.SendMessage(msg, options).ok()); - call_.PerformOps(&write_ops_); - } - - void WritesDone(void* tag) override { - GPR_CODEGEN_ASSERT(started_); - write_ops_.set_output_tag(tag); - write_ops_.ClientSendClose(); - call_.PerformOps(&write_ops_); - } - - /// See the \a ClientAsyncStreamingInterface.Finish method for semantics. - /// Side effect - /// - the \a ClientContext associated with this call is updated with - /// possible initial and trailing metadata sent from the server. - void Finish(::grpc::Status* status, void* tag) override { - GPR_CODEGEN_ASSERT(started_); - finish_ops_.set_output_tag(tag); - if (!context_->initial_metadata_received_) { - finish_ops_.RecvInitialMetadata(context_); - } - finish_ops_.ClientRecvStatus(context_, status); - call_.PerformOps(&finish_ops_); - } - - private: - friend class internal::ClientAsyncReaderWriterFactory; - ClientAsyncReaderWriter(::grpc::internal::Call call, - ::grpc::ClientContext* context, bool start, void* tag) - : context_(context), call_(call), started_(start) { - if (start) { - StartCallInternal(tag); - } else { - GPR_CODEGEN_ASSERT(tag == nullptr); - } - } - - void StartCallInternal(void* tag) { - write_ops_.SendInitialMetadata(&context_->send_initial_metadata_, - context_->initial_metadata_flags()); - // if corked bit is set in context, we just keep the initial metadata - // buffered up to coalesce with later message send. No op is performed. - if (!context_->initial_metadata_corked_) { - write_ops_.set_output_tag(tag); - call_.PerformOps(&write_ops_); - } - } - - ::grpc::ClientContext* context_; - ::grpc::internal::Call call_; - bool started_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata> - meta_ops_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata, - ::grpc::internal::CallOpRecvMessage> - read_ops_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, - ::grpc::internal::CallOpSendMessage, - ::grpc::internal::CallOpClientSendClose> - write_ops_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata, - ::grpc::internal::CallOpClientRecvStatus> - finish_ops_; -}; - -template -class ServerAsyncReaderInterface - : public ::grpc::internal::ServerAsyncStreamingInterface, - public internal::AsyncReaderInterface { - public: - /// Indicate that the stream is to be finished with a certain status code - /// and also send out \a msg response to the client. - /// Request notification for when the server has sent the response and the - /// appropriate signals to the client to end the call. - /// Should not be used concurrently with other operations. - /// - /// It is appropriate to call this method when: - /// * all messages from the client have been received (either known - /// implictly, or explicitly because a previous - /// \a AsyncReaderInterface::Read operation with a non-ok result, - /// e.g., cq->Next(&read_tag, &ok) filled in 'ok' with 'false'). - /// - /// This operation will end when the server has finished sending out initial - /// metadata (if not sent already), response message, and status, or if - /// some failure occurred when trying to do so. - /// - /// gRPC doesn't take ownership or a reference to \a msg or \a status, so it - /// is safe to deallocate once Finish returns. - /// - /// \param[in] tag Tag identifying this request. - /// \param[in] status To be sent to the client as the result of this call. - /// \param[in] msg To be sent to the client as the response for this call. - virtual void Finish(const W& msg, const ::grpc::Status& status, - void* tag) = 0; - - /// Indicate that the stream is to be finished with a certain - /// non-OK status code. - /// Request notification for when the server has sent the appropriate - /// signals to the client to end the call. - /// Should not be used concurrently with other operations. - /// - /// This call is meant to end the call with some error, and can be called at - /// any point that the server would like to "fail" the call (though note - /// this shouldn't be called concurrently with any other "sending" call, like - /// \a AsyncWriterInterface::Write). - /// - /// This operation will end when the server has finished sending out initial - /// metadata (if not sent already), and status, or if some failure occurred - /// when trying to do so. - /// - /// gRPC doesn't take ownership or a reference to \a status, so it is safe to - /// to deallocate once FinishWithError returns. - /// - /// \param[in] tag Tag identifying this request. - /// \param[in] status To be sent to the client as the result of this call. - /// - Note: \a status must have a non-OK code. - virtual void FinishWithError(const ::grpc::Status& status, void* tag) = 0; -}; - -/// Async server-side API for doing client-streaming RPCs, -/// where the incoming message stream from the client has messages of type \a R, -/// and the single response message sent from the server is type \a W. -template -class ServerAsyncReader final : public ServerAsyncReaderInterface { - public: - explicit ServerAsyncReader(::grpc::ServerContext* ctx) - : call_(nullptr, nullptr, nullptr), ctx_(ctx) {} - - /// See \a ServerAsyncStreamingInterface::SendInitialMetadata for semantics. - /// - /// Implicit input parameter: - /// - The initial metadata that will be sent to the client from this op will - /// be taken from the \a ServerContext associated with the call. - void SendInitialMetadata(void* tag) override { - GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_); - - meta_ops_.set_output_tag(tag); - meta_ops_.SendInitialMetadata(&ctx_->initial_metadata_, - ctx_->initial_metadata_flags()); - if (ctx_->compression_level_set()) { - meta_ops_.set_compression_level(ctx_->compression_level()); - } - ctx_->sent_initial_metadata_ = true; - call_.PerformOps(&meta_ops_); - } - - void Read(R* msg, void* tag) override { - read_ops_.set_output_tag(tag); - read_ops_.RecvMessage(msg); - call_.PerformOps(&read_ops_); - } - - /// See the \a ServerAsyncReaderInterface.Read method for semantics - /// - /// Side effect: - /// - also sends initial metadata if not alreay sent. - /// - uses the \a ServerContext associated with this call to send possible - /// initial and trailing metadata. - /// - /// Note: \a msg is not sent if \a status has a non-OK code. - /// - /// gRPC doesn't take ownership or a reference to \a msg and \a status, so it - /// is safe to deallocate once Finish returns. - void Finish(const W& msg, const ::grpc::Status& status, void* tag) override { - finish_ops_.set_output_tag(tag); - if (!ctx_->sent_initial_metadata_) { - finish_ops_.SendInitialMetadata(&ctx_->initial_metadata_, - ctx_->initial_metadata_flags()); - if (ctx_->compression_level_set()) { - finish_ops_.set_compression_level(ctx_->compression_level()); - } - ctx_->sent_initial_metadata_ = true; - } - // The response is dropped if the status is not OK. - if (status.ok()) { - finish_ops_.ServerSendStatus(&ctx_->trailing_metadata_, - finish_ops_.SendMessage(msg)); - } else { - finish_ops_.ServerSendStatus(&ctx_->trailing_metadata_, status); - } - call_.PerformOps(&finish_ops_); - } - - /// See the \a ServerAsyncReaderInterface.Read method for semantics - /// - /// Side effect: - /// - also sends initial metadata if not alreay sent. - /// - uses the \a ServerContext associated with this call to send possible - /// initial and trailing metadata. - /// - /// gRPC doesn't take ownership or a reference to \a status, so it is safe to - /// to deallocate once FinishWithError returns. - void FinishWithError(const ::grpc::Status& status, void* tag) override { - GPR_CODEGEN_ASSERT(!status.ok()); - finish_ops_.set_output_tag(tag); - if (!ctx_->sent_initial_metadata_) { - finish_ops_.SendInitialMetadata(&ctx_->initial_metadata_, - ctx_->initial_metadata_flags()); - if (ctx_->compression_level_set()) { - finish_ops_.set_compression_level(ctx_->compression_level()); - } - ctx_->sent_initial_metadata_ = true; - } - finish_ops_.ServerSendStatus(&ctx_->trailing_metadata_, status); - call_.PerformOps(&finish_ops_); - } - - private: - void BindCall(::grpc::internal::Call* call) override { call_ = *call; } - - ::grpc::internal::Call call_; - ::grpc::ServerContext* ctx_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata> - meta_ops_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvMessage> read_ops_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, - ::grpc::internal::CallOpSendMessage, - ::grpc::internal::CallOpServerSendStatus> - finish_ops_; -}; - -template -class ServerAsyncWriterInterface - : public ::grpc::internal::ServerAsyncStreamingInterface, - public internal::AsyncWriterInterface { - public: - /// Indicate that the stream is to be finished with a certain status code. - /// Request notification for when the server has sent the appropriate - /// signals to the client to end the call. - /// Should not be used concurrently with other operations. - /// - /// It is appropriate to call this method when either: - /// * all messages from the client have been received (either known - /// implictly, or explicitly because a previous \a - /// AsyncReaderInterface::Read operation with a non-ok - /// result (e.g., cq->Next(&read_tag, &ok) filled in 'ok' with 'false'. - /// * it is desired to end the call early with some non-OK status code. - /// - /// This operation will end when the server has finished sending out initial - /// metadata (if not sent already), response message, and status, or if - /// some failure occurred when trying to do so. - /// - /// gRPC doesn't take ownership or a reference to \a status, so it is safe to - /// to deallocate once Finish returns. - /// - /// \param[in] tag Tag identifying this request. - /// \param[in] status To be sent to the client as the result of this call. - virtual void Finish(const ::grpc::Status& status, void* tag) = 0; - - /// Request the writing of \a msg and coalesce it with trailing metadata which - /// contains \a status, using WriteOptions options with - /// identifying tag \a tag. - /// - /// WriteAndFinish is equivalent of performing WriteLast and Finish - /// in a single step. - /// - /// gRPC doesn't take ownership or a reference to \a msg and \a status, so it - /// is safe to deallocate once WriteAndFinish returns. - /// - /// \param[in] msg The message to be written. - /// \param[in] options The WriteOptions to be used to write this message. - /// \param[in] status The Status that server returns to client. - /// \param[in] tag The tag identifying the operation. - virtual void WriteAndFinish(const W& msg, ::grpc::WriteOptions options, - const ::grpc::Status& status, void* tag) = 0; -}; - -/// Async server-side API for doing server streaming RPCs, -/// where the outgoing message stream from the server has messages of type \a W. -template -class ServerAsyncWriter final : public ServerAsyncWriterInterface { - public: - explicit ServerAsyncWriter(::grpc::ServerContext* ctx) - : call_(nullptr, nullptr, nullptr), ctx_(ctx) {} - - /// See \a ServerAsyncStreamingInterface::SendInitialMetadata for semantics. - /// - /// Implicit input parameter: - /// - The initial metadata that will be sent to the client from this op will - /// be taken from the \a ServerContext associated with the call. - /// - /// \param[in] tag Tag identifying this request. - void SendInitialMetadata(void* tag) override { - GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_); - - meta_ops_.set_output_tag(tag); - meta_ops_.SendInitialMetadata(&ctx_->initial_metadata_, - ctx_->initial_metadata_flags()); - if (ctx_->compression_level_set()) { - meta_ops_.set_compression_level(ctx_->compression_level()); - } - ctx_->sent_initial_metadata_ = true; - call_.PerformOps(&meta_ops_); - } - - void Write(const W& msg, void* tag) override { - write_ops_.set_output_tag(tag); - EnsureInitialMetadataSent(&write_ops_); - // TODO(ctiller): don't assert - GPR_CODEGEN_ASSERT(write_ops_.SendMessage(msg).ok()); - call_.PerformOps(&write_ops_); - } - - void Write(const W& msg, ::grpc::WriteOptions options, void* tag) override { - write_ops_.set_output_tag(tag); - if (options.is_last_message()) { - options.set_buffer_hint(); - } - - EnsureInitialMetadataSent(&write_ops_); - // TODO(ctiller): don't assert - GPR_CODEGEN_ASSERT(write_ops_.SendMessage(msg, options).ok()); - call_.PerformOps(&write_ops_); - } - - /// See the \a ServerAsyncWriterInterface.WriteAndFinish method for semantics. - /// - /// Implicit input parameter: - /// - the \a ServerContext associated with this call is used - /// for sending trailing (and initial) metadata to the client. - /// - /// Note: \a status must have an OK code. - /// - /// gRPC doesn't take ownership or a reference to \a msg and \a status, so it - /// is safe to deallocate once WriteAndFinish returns. - void WriteAndFinish(const W& msg, ::grpc::WriteOptions options, - const ::grpc::Status& status, void* tag) override { - write_ops_.set_output_tag(tag); - EnsureInitialMetadataSent(&write_ops_); - options.set_buffer_hint(); - GPR_CODEGEN_ASSERT(write_ops_.SendMessage(msg, options).ok()); - write_ops_.ServerSendStatus(&ctx_->trailing_metadata_, status); - call_.PerformOps(&write_ops_); - } - - /// See the \a ServerAsyncWriterInterface.Finish method for semantics. - /// - /// Implicit input parameter: - /// - the \a ServerContext associated with this call is used for sending - /// trailing (and initial if not already sent) metadata to the client. - /// - /// Note: there are no restrictions are the code of - /// \a status,it may be non-OK - /// - /// gRPC doesn't take ownership or a reference to \a status, so it is safe to - /// to deallocate once Finish returns. - void Finish(const ::grpc::Status& status, void* tag) override { - finish_ops_.set_output_tag(tag); - EnsureInitialMetadataSent(&finish_ops_); - finish_ops_.ServerSendStatus(&ctx_->trailing_metadata_, status); - call_.PerformOps(&finish_ops_); - } - - private: - void BindCall(::grpc::internal::Call* call) override { call_ = *call; } - - template - void EnsureInitialMetadataSent(T* ops) { - if (!ctx_->sent_initial_metadata_) { - ops->SendInitialMetadata(&ctx_->initial_metadata_, - ctx_->initial_metadata_flags()); - if (ctx_->compression_level_set()) { - ops->set_compression_level(ctx_->compression_level()); - } - ctx_->sent_initial_metadata_ = true; - } - } - - ::grpc::internal::Call call_; - ::grpc::ServerContext* ctx_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata> - meta_ops_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, - ::grpc::internal::CallOpSendMessage, - ::grpc::internal::CallOpServerSendStatus> - write_ops_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, - ::grpc::internal::CallOpServerSendStatus> - finish_ops_; -}; - -/// Server-side interface for asynchronous bi-directional streaming. -template -class ServerAsyncReaderWriterInterface - : public ::grpc::internal::ServerAsyncStreamingInterface, - public internal::AsyncWriterInterface, - public internal::AsyncReaderInterface { - public: - /// Indicate that the stream is to be finished with a certain status code. - /// Request notification for when the server has sent the appropriate - /// signals to the client to end the call. - /// Should not be used concurrently with other operations. - /// - /// It is appropriate to call this method when either: - /// * all messages from the client have been received (either known - /// implictly, or explicitly because a previous \a - /// AsyncReaderInterface::Read operation - /// with a non-ok result (e.g., cq->Next(&read_tag, &ok) filled in 'ok' - /// with 'false'. - /// * it is desired to end the call early with some non-OK status code. - /// - /// This operation will end when the server has finished sending out initial - /// metadata (if not sent already), response message, and status, or if some - /// failure occurred when trying to do so. - /// - /// gRPC doesn't take ownership or a reference to \a status, so it is safe to - /// to deallocate once Finish returns. - /// - /// \param[in] tag Tag identifying this request. - /// \param[in] status To be sent to the client as the result of this call. - virtual void Finish(const ::grpc::Status& status, void* tag) = 0; - - /// Request the writing of \a msg and coalesce it with trailing metadata which - /// contains \a status, using WriteOptions options with - /// identifying tag \a tag. - /// - /// WriteAndFinish is equivalent of performing WriteLast and Finish in a - /// single step. - /// - /// gRPC doesn't take ownership or a reference to \a msg and \a status, so it - /// is safe to deallocate once WriteAndFinish returns. - /// - /// \param[in] msg The message to be written. - /// \param[in] options The WriteOptions to be used to write this message. - /// \param[in] status The Status that server returns to client. - /// \param[in] tag The tag identifying the operation. - virtual void WriteAndFinish(const W& msg, ::grpc::WriteOptions options, - const ::grpc::Status& status, void* tag) = 0; -}; - -/// Async server-side API for doing bidirectional streaming RPCs, -/// where the incoming message stream coming from the client has messages of -/// type \a R, and the outgoing message stream coming from the server has -/// messages of type \a W. -template -class ServerAsyncReaderWriter final - : public ServerAsyncReaderWriterInterface { - public: - explicit ServerAsyncReaderWriter(::grpc::ServerContext* ctx) - : call_(nullptr, nullptr, nullptr), ctx_(ctx) {} - - /// See \a ServerAsyncStreamingInterface::SendInitialMetadata for semantics. - /// - /// Implicit input parameter: - /// - The initial metadata that will be sent to the client from this op will - /// be taken from the \a ServerContext associated with the call. - /// - /// \param[in] tag Tag identifying this request. - void SendInitialMetadata(void* tag) override { - GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_); - - meta_ops_.set_output_tag(tag); - meta_ops_.SendInitialMetadata(&ctx_->initial_metadata_, - ctx_->initial_metadata_flags()); - if (ctx_->compression_level_set()) { - meta_ops_.set_compression_level(ctx_->compression_level()); - } - ctx_->sent_initial_metadata_ = true; - call_.PerformOps(&meta_ops_); - } - - void Read(R* msg, void* tag) override { - read_ops_.set_output_tag(tag); - read_ops_.RecvMessage(msg); - call_.PerformOps(&read_ops_); - } - - void Write(const W& msg, void* tag) override { - write_ops_.set_output_tag(tag); - EnsureInitialMetadataSent(&write_ops_); - // TODO(ctiller): don't assert - GPR_CODEGEN_ASSERT(write_ops_.SendMessage(msg).ok()); - call_.PerformOps(&write_ops_); - } - - void Write(const W& msg, ::grpc::WriteOptions options, void* tag) override { - write_ops_.set_output_tag(tag); - if (options.is_last_message()) { - options.set_buffer_hint(); - } - EnsureInitialMetadataSent(&write_ops_); - GPR_CODEGEN_ASSERT(write_ops_.SendMessage(msg, options).ok()); - call_.PerformOps(&write_ops_); - } - - /// See the \a ServerAsyncReaderWriterInterface.WriteAndFinish - /// method for semantics. - /// - /// Implicit input parameter: - /// - the \a ServerContext associated with this call is used - /// for sending trailing (and initial) metadata to the client. - /// - /// Note: \a status must have an OK code. - // - /// gRPC doesn't take ownership or a reference to \a msg and \a status, so it - /// is safe to deallocate once WriteAndFinish returns. - void WriteAndFinish(const W& msg, ::grpc::WriteOptions options, - const ::grpc::Status& status, void* tag) override { - write_ops_.set_output_tag(tag); - EnsureInitialMetadataSent(&write_ops_); - options.set_buffer_hint(); - GPR_CODEGEN_ASSERT(write_ops_.SendMessage(msg, options).ok()); - write_ops_.ServerSendStatus(&ctx_->trailing_metadata_, status); - call_.PerformOps(&write_ops_); - } - - /// See the \a ServerAsyncReaderWriterInterface.Finish method for semantics. - /// - /// Implicit input parameter: - /// - the \a ServerContext associated with this call is used for sending - /// trailing (and initial if not already sent) metadata to the client. - /// - /// Note: there are no restrictions are the code of \a status, - /// it may be non-OK - // - /// gRPC doesn't take ownership or a reference to \a status, so it is safe to - /// to deallocate once Finish returns. - void Finish(const ::grpc::Status& status, void* tag) override { - finish_ops_.set_output_tag(tag); - EnsureInitialMetadataSent(&finish_ops_); - - finish_ops_.ServerSendStatus(&ctx_->trailing_metadata_, status); - call_.PerformOps(&finish_ops_); - } - - private: - friend class ::grpc::Server; - - void BindCall(::grpc::internal::Call* call) override { call_ = *call; } - - template - void EnsureInitialMetadataSent(T* ops) { - if (!ctx_->sent_initial_metadata_) { - ops->SendInitialMetadata(&ctx_->initial_metadata_, - ctx_->initial_metadata_flags()); - if (ctx_->compression_level_set()) { - ops->set_compression_level(ctx_->compression_level()); - } - ctx_->sent_initial_metadata_ = true; - } - } - - ::grpc::internal::Call call_; - ::grpc::ServerContext* ctx_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata> - meta_ops_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvMessage> read_ops_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, - ::grpc::internal::CallOpSendMessage, - ::grpc::internal::CallOpServerSendStatus> - write_ops_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, - ::grpc::internal::CallOpServerSendStatus> - finish_ops_; -}; - -} // namespace grpc_impl -#endif // GRPCPP_IMPL_CODEGEN_ASYNC_STREAM_IMPL_H diff --git a/include/grpcpp/impl/codegen/async_unary_call.h b/include/grpcpp/impl/codegen/async_unary_call.h index cbbe70172c8..3deeda8c7fc 100644 --- a/include/grpcpp/impl/codegen/async_unary_call.h +++ b/include/grpcpp/impl/codegen/async_unary_call.h @@ -19,28 +19,296 @@ #ifndef GRPCPP_IMPL_CODEGEN_ASYNC_UNARY_CALL_H #define GRPCPP_IMPL_CODEGEN_ASYNC_UNARY_CALL_H -#include +#include +#include +#include +#include +#include +#include namespace grpc { +/// An interface relevant for async client side unary RPCs (which send +/// one request message to a server and receive one response message). template -using ClientAsyncResponseReaderInterface = - grpc_impl::ClientAsyncResponseReaderInterface; +class ClientAsyncResponseReaderInterface { + public: + virtual ~ClientAsyncResponseReaderInterface() {} -template -using ClientAsyncResponseReader = grpc_impl::ClientAsyncResponseReader; + /// Start the call that was set up by the constructor, but only if the + /// constructor was invoked through the "Prepare" API which doesn't actually + /// start the call + virtual void StartCall() = 0; -template -using ServerAsyncResponseWriter = ::grpc_impl::ServerAsyncResponseWriter; + /// Request notification of the reading of initial metadata. Completion + /// will be notified by \a tag on the associated completion queue. + /// This call is optional, but if it is used, it cannot be used concurrently + /// with or after the \a Finish method. + /// + /// \param[in] tag Tag identifying this request. + virtual void ReadInitialMetadata(void* tag) = 0; + + /// Request to receive the server's response \a msg and final \a status for + /// the call, and to notify \a tag on this call's completion queue when + /// finished. + /// + /// This function will return when either: + /// - when the server's response message and status have been received. + /// - when the server has returned a non-OK status (no message expected in + /// this case). + /// - when the call failed for some reason and the library generated a + /// non-OK status. + /// + /// \param[in] tag Tag identifying this request. + /// \param[out] status To be updated with the operation status. + /// \param[out] msg To be filled in with the server's response message. + virtual void Finish(R* msg, ::grpc::Status* status, void* tag) = 0; +}; namespace internal { +template +class ClientAsyncResponseReaderFactory { + public: + /// Start a call and write the request out if \a start is set. + /// \a tag will be notified on \a cq when the call has been started (i.e. + /// intitial metadata sent) and \a request has been written out. + /// If \a start is not set, the actual call must be initiated by StartCall + /// Note that \a context will be used to fill in custom initial metadata + /// used to send to the server when starting the call. + template + static ClientAsyncResponseReader* Create( + ::grpc::ChannelInterface* channel, ::grpc::CompletionQueue* cq, + const ::grpc::internal::RpcMethod& method, ::grpc::ClientContext* context, + const W& request, bool start) { + ::grpc::internal::Call call = channel->CreateCall(method, context, cq); + return new (::grpc::g_core_codegen_interface->grpc_call_arena_alloc( + call.call(), sizeof(ClientAsyncResponseReader))) + ClientAsyncResponseReader(call, context, request, start); + } +}; +} // namespace internal +/// Async API for client-side unary RPCs, where the message response +/// received from the server is of type \a R. template -using ClientAsyncResponseReaderFactory = - ::grpc_impl::internal::ClientAsyncResponseReaderFactory; +class ClientAsyncResponseReader final + : public ClientAsyncResponseReaderInterface { + public: + // always allocated against a call arena, no memory free required + static void operator delete(void* /*ptr*/, std::size_t size) { + GPR_CODEGEN_ASSERT(size == sizeof(ClientAsyncResponseReader)); + } -} // namespace internal + // This operator should never be called as the memory should be freed as part + // of the arena destruction. It only exists to provide a matching operator + // delete to the operator new so that some compilers will not complain (see + // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this + // there are no tests catching the compiler warning. + static void operator delete(void*, void*) { GPR_CODEGEN_ASSERT(false); } + + void StartCall() override { + GPR_CODEGEN_ASSERT(!started_); + started_ = true; + StartCallInternal(); + } + + /// See \a ClientAsyncResponseReaderInterface::ReadInitialMetadata for + /// semantics. + /// + /// Side effect: + /// - the \a ClientContext associated with this call is updated with + /// possible initial and trailing metadata sent from the server. + void ReadInitialMetadata(void* tag) override { + GPR_CODEGEN_ASSERT(started_); + GPR_CODEGEN_ASSERT(!context_->initial_metadata_received_); + + single_buf.set_output_tag(tag); + single_buf.RecvInitialMetadata(context_); + call_.PerformOps(&single_buf); + initial_metadata_read_ = true; + } + + /// See \a ClientAysncResponseReaderInterface::Finish for semantics. + /// + /// Side effect: + /// - the \a ClientContext associated with this call is updated with + /// possible initial and trailing metadata sent from the server. + void Finish(R* msg, ::grpc::Status* status, void* tag) override { + GPR_CODEGEN_ASSERT(started_); + if (initial_metadata_read_) { + finish_buf.set_output_tag(tag); + finish_buf.RecvMessage(msg); + finish_buf.AllowNoMessage(); + finish_buf.ClientRecvStatus(context_, status); + call_.PerformOps(&finish_buf); + } else { + single_buf.set_output_tag(tag); + single_buf.RecvInitialMetadata(context_); + single_buf.RecvMessage(msg); + single_buf.AllowNoMessage(); + single_buf.ClientRecvStatus(context_, status); + call_.PerformOps(&single_buf); + } + } + + private: + friend class internal::ClientAsyncResponseReaderFactory; + ::grpc::ClientContext* const context_; + ::grpc::internal::Call call_; + bool started_; + bool initial_metadata_read_ = false; + + template + ClientAsyncResponseReader(::grpc::internal::Call call, + ::grpc::ClientContext* context, const W& request, + bool start) + : context_(context), call_(call), started_(start) { + // Bind the metadata at time of StartCallInternal but set up the rest here + // TODO(ctiller): don't assert + GPR_CODEGEN_ASSERT(single_buf.SendMessage(request).ok()); + single_buf.ClientSendClose(); + if (start) StartCallInternal(); + } + + void StartCallInternal() { + single_buf.SendInitialMetadata(&context_->send_initial_metadata_, + context_->initial_metadata_flags()); + } + + // disable operator new + static void* operator new(std::size_t size); + static void* operator new(std::size_t /*size*/, void* p) { return p; } + + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, + ::grpc::internal::CallOpSendMessage, + ::grpc::internal::CallOpClientSendClose, + ::grpc::internal::CallOpRecvInitialMetadata, + ::grpc::internal::CallOpRecvMessage, + ::grpc::internal::CallOpClientRecvStatus> + single_buf; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvMessage, + ::grpc::internal::CallOpClientRecvStatus> + finish_buf; +}; + +/// Async server-side API for handling unary calls, where the single +/// response message sent to the client is of type \a W. +template +class ServerAsyncResponseWriter final + : public ::grpc::internal::ServerAsyncStreamingInterface { + public: + explicit ServerAsyncResponseWriter(::grpc::ServerContext* ctx) + : call_(nullptr, nullptr, nullptr), ctx_(ctx) {} + + /// See \a ServerAsyncStreamingInterface::SendInitialMetadata for semantics. + /// + /// Side effect: + /// The initial metadata that will be sent to the client from this op will + /// be taken from the \a ServerContext associated with the call. + /// + /// \param[in] tag Tag identifying this request. + void SendInitialMetadata(void* tag) override { + GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_); + + meta_buf_.set_output_tag(tag); + meta_buf_.SendInitialMetadata(&ctx_->initial_metadata_, + ctx_->initial_metadata_flags()); + if (ctx_->compression_level_set()) { + meta_buf_.set_compression_level(ctx_->compression_level()); + } + ctx_->sent_initial_metadata_ = true; + call_.PerformOps(&meta_buf_); + } + + /// Indicate that the stream is to be finished and request notification + /// when the server has sent the appropriate signals to the client to + /// end the call. Should not be used concurrently with other operations. + /// + /// \param[in] tag Tag identifying this request. + /// \param[in] status To be sent to the client as the result of the call. + /// \param[in] msg Message to be sent to the client. + /// + /// Side effect: + /// - also sends initial metadata if not already sent (using the + /// \a ServerContext associated with this call). + /// + /// Note: if \a status has a non-OK code, then \a msg will not be sent, + /// and the client will receive only the status with possible trailing + /// metadata. + void Finish(const W& msg, const ::grpc::Status& status, void* tag) { + finish_buf_.set_output_tag(tag); + finish_buf_.set_core_cq_tag(&finish_buf_); + if (!ctx_->sent_initial_metadata_) { + finish_buf_.SendInitialMetadata(&ctx_->initial_metadata_, + ctx_->initial_metadata_flags()); + if (ctx_->compression_level_set()) { + finish_buf_.set_compression_level(ctx_->compression_level()); + } + ctx_->sent_initial_metadata_ = true; + } + // The response is dropped if the status is not OK. + if (status.ok()) { + finish_buf_.ServerSendStatus(&ctx_->trailing_metadata_, + finish_buf_.SendMessage(msg)); + } else { + finish_buf_.ServerSendStatus(&ctx_->trailing_metadata_, status); + } + call_.PerformOps(&finish_buf_); + } + + /// Indicate that the stream is to be finished with a non-OK status, + /// and request notification for when the server has finished sending the + /// appropriate signals to the client to end the call. + /// Should not be used concurrently with other operations. + /// + /// \param[in] tag Tag identifying this request. + /// \param[in] status To be sent to the client as the result of the call. + /// - Note: \a status must have a non-OK code. + /// + /// Side effect: + /// - also sends initial metadata if not already sent (using the + /// \a ServerContext associated with this call). + void FinishWithError(const ::grpc::Status& status, void* tag) { + GPR_CODEGEN_ASSERT(!status.ok()); + finish_buf_.set_output_tag(tag); + if (!ctx_->sent_initial_metadata_) { + finish_buf_.SendInitialMetadata(&ctx_->initial_metadata_, + ctx_->initial_metadata_flags()); + if (ctx_->compression_level_set()) { + finish_buf_.set_compression_level(ctx_->compression_level()); + } + ctx_->sent_initial_metadata_ = true; + } + finish_buf_.ServerSendStatus(&ctx_->trailing_metadata_, status); + call_.PerformOps(&finish_buf_); + } + + private: + void BindCall(::grpc::internal::Call* call) override { call_ = *call; } + + ::grpc::internal::Call call_; + ::grpc::ServerContext* ctx_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata> + meta_buf_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, + ::grpc::internal::CallOpSendMessage, + ::grpc::internal::CallOpServerSendStatus> + finish_buf_; +}; } // namespace grpc +namespace std { +template +class default_delete<::grpc::ClientAsyncResponseReader> { + public: + void operator()(void* /*p*/) {} +}; +template +class default_delete<::grpc::ClientAsyncResponseReaderInterface> { + public: + void operator()(void* /*p*/) {} +}; +} // namespace std + #endif // GRPCPP_IMPL_CODEGEN_ASYNC_UNARY_CALL_H diff --git a/include/grpcpp/impl/codegen/async_unary_call_impl.h b/include/grpcpp/impl/codegen/async_unary_call_impl.h deleted file mode 100644 index 68e783224da..00000000000 --- a/include/grpcpp/impl/codegen/async_unary_call_impl.h +++ /dev/null @@ -1,314 +0,0 @@ -/* - * - * Copyright 2015 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#ifndef GRPCPP_IMPL_CODEGEN_ASYNC_UNARY_CALL_IMPL_H -#define GRPCPP_IMPL_CODEGEN_ASYNC_UNARY_CALL_IMPL_H - -#include -#include -#include -#include -#include -#include - -namespace grpc_impl { - -/// An interface relevant for async client side unary RPCs (which send -/// one request message to a server and receive one response message). -template -class ClientAsyncResponseReaderInterface { - public: - virtual ~ClientAsyncResponseReaderInterface() {} - - /// Start the call that was set up by the constructor, but only if the - /// constructor was invoked through the "Prepare" API which doesn't actually - /// start the call - virtual void StartCall() = 0; - - /// Request notification of the reading of initial metadata. Completion - /// will be notified by \a tag on the associated completion queue. - /// This call is optional, but if it is used, it cannot be used concurrently - /// with or after the \a Finish method. - /// - /// \param[in] tag Tag identifying this request. - virtual void ReadInitialMetadata(void* tag) = 0; - - /// Request to receive the server's response \a msg and final \a status for - /// the call, and to notify \a tag on this call's completion queue when - /// finished. - /// - /// This function will return when either: - /// - when the server's response message and status have been received. - /// - when the server has returned a non-OK status (no message expected in - /// this case). - /// - when the call failed for some reason and the library generated a - /// non-OK status. - /// - /// \param[in] tag Tag identifying this request. - /// \param[out] status To be updated with the operation status. - /// \param[out] msg To be filled in with the server's response message. - virtual void Finish(R* msg, ::grpc::Status* status, void* tag) = 0; -}; - -namespace internal { -template -class ClientAsyncResponseReaderFactory { - public: - /// Start a call and write the request out if \a start is set. - /// \a tag will be notified on \a cq when the call has been started (i.e. - /// intitial metadata sent) and \a request has been written out. - /// If \a start is not set, the actual call must be initiated by StartCall - /// Note that \a context will be used to fill in custom initial metadata - /// used to send to the server when starting the call. - template - static ClientAsyncResponseReader* Create( - ::grpc::ChannelInterface* channel, ::grpc::CompletionQueue* cq, - const ::grpc::internal::RpcMethod& method, ::grpc::ClientContext* context, - const W& request, bool start) { - ::grpc::internal::Call call = channel->CreateCall(method, context, cq); - return new (::grpc::g_core_codegen_interface->grpc_call_arena_alloc( - call.call(), sizeof(ClientAsyncResponseReader))) - ClientAsyncResponseReader(call, context, request, start); - } -}; -} // namespace internal - -/// Async API for client-side unary RPCs, where the message response -/// received from the server is of type \a R. -template -class ClientAsyncResponseReader final - : public ClientAsyncResponseReaderInterface { - public: - // always allocated against a call arena, no memory free required - static void operator delete(void* /*ptr*/, std::size_t size) { - GPR_CODEGEN_ASSERT(size == sizeof(ClientAsyncResponseReader)); - } - - // This operator should never be called as the memory should be freed as part - // of the arena destruction. It only exists to provide a matching operator - // delete to the operator new so that some compilers will not complain (see - // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this - // there are no tests catching the compiler warning. - static void operator delete(void*, void*) { GPR_CODEGEN_ASSERT(false); } - - void StartCall() override { - GPR_CODEGEN_ASSERT(!started_); - started_ = true; - StartCallInternal(); - } - - /// See \a ClientAsyncResponseReaderInterface::ReadInitialMetadata for - /// semantics. - /// - /// Side effect: - /// - the \a ClientContext associated with this call is updated with - /// possible initial and trailing metadata sent from the server. - void ReadInitialMetadata(void* tag) override { - GPR_CODEGEN_ASSERT(started_); - GPR_CODEGEN_ASSERT(!context_->initial_metadata_received_); - - single_buf.set_output_tag(tag); - single_buf.RecvInitialMetadata(context_); - call_.PerformOps(&single_buf); - initial_metadata_read_ = true; - } - - /// See \a ClientAysncResponseReaderInterface::Finish for semantics. - /// - /// Side effect: - /// - the \a ClientContext associated with this call is updated with - /// possible initial and trailing metadata sent from the server. - void Finish(R* msg, ::grpc::Status* status, void* tag) override { - GPR_CODEGEN_ASSERT(started_); - if (initial_metadata_read_) { - finish_buf.set_output_tag(tag); - finish_buf.RecvMessage(msg); - finish_buf.AllowNoMessage(); - finish_buf.ClientRecvStatus(context_, status); - call_.PerformOps(&finish_buf); - } else { - single_buf.set_output_tag(tag); - single_buf.RecvInitialMetadata(context_); - single_buf.RecvMessage(msg); - single_buf.AllowNoMessage(); - single_buf.ClientRecvStatus(context_, status); - call_.PerformOps(&single_buf); - } - } - - private: - friend class internal::ClientAsyncResponseReaderFactory; - ::grpc::ClientContext* const context_; - ::grpc::internal::Call call_; - bool started_; - bool initial_metadata_read_ = false; - - template - ClientAsyncResponseReader(::grpc::internal::Call call, - ::grpc::ClientContext* context, const W& request, - bool start) - : context_(context), call_(call), started_(start) { - // Bind the metadata at time of StartCallInternal but set up the rest here - // TODO(ctiller): don't assert - GPR_CODEGEN_ASSERT(single_buf.SendMessage(request).ok()); - single_buf.ClientSendClose(); - if (start) StartCallInternal(); - } - - void StartCallInternal() { - single_buf.SendInitialMetadata(&context_->send_initial_metadata_, - context_->initial_metadata_flags()); - } - - // disable operator new - static void* operator new(std::size_t size); - static void* operator new(std::size_t /*size*/, void* p) { return p; } - - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, - ::grpc::internal::CallOpSendMessage, - ::grpc::internal::CallOpClientSendClose, - ::grpc::internal::CallOpRecvInitialMetadata, - ::grpc::internal::CallOpRecvMessage, - ::grpc::internal::CallOpClientRecvStatus> - single_buf; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvMessage, - ::grpc::internal::CallOpClientRecvStatus> - finish_buf; -}; - -/// Async server-side API for handling unary calls, where the single -/// response message sent to the client is of type \a W. -template -class ServerAsyncResponseWriter final - : public ::grpc::internal::ServerAsyncStreamingInterface { - public: - explicit ServerAsyncResponseWriter(::grpc::ServerContext* ctx) - : call_(nullptr, nullptr, nullptr), ctx_(ctx) {} - - /// See \a ServerAsyncStreamingInterface::SendInitialMetadata for semantics. - /// - /// Side effect: - /// The initial metadata that will be sent to the client from this op will - /// be taken from the \a ServerContext associated with the call. - /// - /// \param[in] tag Tag identifying this request. - void SendInitialMetadata(void* tag) override { - GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_); - - meta_buf_.set_output_tag(tag); - meta_buf_.SendInitialMetadata(&ctx_->initial_metadata_, - ctx_->initial_metadata_flags()); - if (ctx_->compression_level_set()) { - meta_buf_.set_compression_level(ctx_->compression_level()); - } - ctx_->sent_initial_metadata_ = true; - call_.PerformOps(&meta_buf_); - } - - /// Indicate that the stream is to be finished and request notification - /// when the server has sent the appropriate signals to the client to - /// end the call. Should not be used concurrently with other operations. - /// - /// \param[in] tag Tag identifying this request. - /// \param[in] status To be sent to the client as the result of the call. - /// \param[in] msg Message to be sent to the client. - /// - /// Side effect: - /// - also sends initial metadata if not already sent (using the - /// \a ServerContext associated with this call). - /// - /// Note: if \a status has a non-OK code, then \a msg will not be sent, - /// and the client will receive only the status with possible trailing - /// metadata. - void Finish(const W& msg, const ::grpc::Status& status, void* tag) { - finish_buf_.set_output_tag(tag); - finish_buf_.set_core_cq_tag(&finish_buf_); - if (!ctx_->sent_initial_metadata_) { - finish_buf_.SendInitialMetadata(&ctx_->initial_metadata_, - ctx_->initial_metadata_flags()); - if (ctx_->compression_level_set()) { - finish_buf_.set_compression_level(ctx_->compression_level()); - } - ctx_->sent_initial_metadata_ = true; - } - // The response is dropped if the status is not OK. - if (status.ok()) { - finish_buf_.ServerSendStatus(&ctx_->trailing_metadata_, - finish_buf_.SendMessage(msg)); - } else { - finish_buf_.ServerSendStatus(&ctx_->trailing_metadata_, status); - } - call_.PerformOps(&finish_buf_); - } - - /// Indicate that the stream is to be finished with a non-OK status, - /// and request notification for when the server has finished sending the - /// appropriate signals to the client to end the call. - /// Should not be used concurrently with other operations. - /// - /// \param[in] tag Tag identifying this request. - /// \param[in] status To be sent to the client as the result of the call. - /// - Note: \a status must have a non-OK code. - /// - /// Side effect: - /// - also sends initial metadata if not already sent (using the - /// \a ServerContext associated with this call). - void FinishWithError(const ::grpc::Status& status, void* tag) { - GPR_CODEGEN_ASSERT(!status.ok()); - finish_buf_.set_output_tag(tag); - if (!ctx_->sent_initial_metadata_) { - finish_buf_.SendInitialMetadata(&ctx_->initial_metadata_, - ctx_->initial_metadata_flags()); - if (ctx_->compression_level_set()) { - finish_buf_.set_compression_level(ctx_->compression_level()); - } - ctx_->sent_initial_metadata_ = true; - } - finish_buf_.ServerSendStatus(&ctx_->trailing_metadata_, status); - call_.PerformOps(&finish_buf_); - } - - private: - void BindCall(::grpc::internal::Call* call) override { call_ = *call; } - - ::grpc::internal::Call call_; - ::grpc::ServerContext* ctx_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata> - meta_buf_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, - ::grpc::internal::CallOpSendMessage, - ::grpc::internal::CallOpServerSendStatus> - finish_buf_; -}; - -} // namespace grpc_impl - -namespace std { -template -class default_delete<::grpc_impl::ClientAsyncResponseReader> { - public: - void operator()(void* /*p*/) {} -}; -template -class default_delete<::grpc_impl::ClientAsyncResponseReaderInterface> { - public: - void operator()(void* /*p*/) {} -}; -} // namespace std - -#endif // GRPCPP_IMPL_CODEGEN_ASYNC_UNARY_CALL_IMPL_H diff --git a/include/grpcpp/impl/codegen/byte_buffer.h b/include/grpcpp/impl/codegen/byte_buffer.h index 54886ae8086..6e64ec9981e 100644 --- a/include/grpcpp/impl/codegen/byte_buffer.h +++ b/include/grpcpp/impl/codegen/byte_buffer.h @@ -29,9 +29,13 @@ #include -namespace grpc_impl { -namespace internal { +namespace grpc { +class ServerInterface; +class ByteBuffer; +class ServerInterface; + +namespace internal { template class CallbackUnaryHandler; template @@ -42,17 +46,6 @@ template class ServerStreamingHandler; template <::grpc::StatusCode code> class ErrorMethodHandler; - -} // namespace internal -} // namespace grpc_impl - -namespace grpc { - -class ServerInterface; -class ByteBuffer; -class ServerInterface; - -namespace internal { class CallOpSendMessage; template class CallOpRecvMessage; @@ -170,15 +163,15 @@ class ByteBuffer final { friend class internal::CallOpRecvMessage; friend class internal::CallOpGenericRecvMessage; template - friend class ::grpc_impl::internal::RpcMethodHandler; + friend class internal::RpcMethodHandler; template - friend class ::grpc_impl::internal::ServerStreamingHandler; + friend class internal::ServerStreamingHandler; template - friend class ::grpc_impl::internal::CallbackUnaryHandler; + friend class internal::CallbackUnaryHandler; template - friend class ::grpc_impl::internal::CallbackServerStreamingHandler; + friend class internal::CallbackServerStreamingHandler; template - friend class ::grpc_impl::internal::ErrorMethodHandler; + friend class internal::ErrorMethodHandler; template friend class internal::DeserializeFuncType; friend class ProtoBufferReader; diff --git a/include/grpcpp/impl/codegen/channel_interface.h b/include/grpcpp/impl/codegen/channel_interface.h index 8cba38cfe08..c5570ddc54c 100644 --- a/include/grpcpp/impl/codegen/channel_interface.h +++ b/include/grpcpp/impl/codegen/channel_interface.h @@ -24,7 +24,7 @@ #include #include -namespace grpc_impl { +namespace grpc { template class ClientReader; template @@ -50,9 +50,7 @@ template class ClientCallbackWriterFactory; class ClientCallbackUnaryFactory; } // namespace internal -} // namespace grpc_impl -namespace grpc { class ChannelInterface; class ClientContext; class CompletionQueue; @@ -107,30 +105,30 @@ class ChannelInterface { private: template - friend class ::grpc_impl::ClientReader; + friend class ::grpc::ClientReader; template - friend class ::grpc_impl::ClientWriter; + friend class ::grpc::ClientWriter; template - friend class ::grpc_impl::ClientReaderWriter; + friend class ::grpc::ClientReaderWriter; template - friend class ::grpc_impl::internal::ClientAsyncReaderFactory; + friend class ::grpc::internal::ClientAsyncReaderFactory; template - friend class ::grpc_impl::internal::ClientAsyncWriterFactory; + friend class ::grpc::internal::ClientAsyncWriterFactory; template - friend class ::grpc_impl::internal::ClientAsyncReaderWriterFactory; + friend class ::grpc::internal::ClientAsyncReaderWriterFactory; template - friend class ::grpc_impl::internal::ClientAsyncResponseReaderFactory; + friend class ::grpc::internal::ClientAsyncResponseReaderFactory; template - friend class ::grpc_impl::internal::ClientCallbackReaderWriterFactory; + friend class ::grpc::internal::ClientCallbackReaderWriterFactory; template - friend class ::grpc_impl::internal::ClientCallbackReaderFactory; + friend class ::grpc::internal::ClientCallbackReaderFactory; template - friend class ::grpc_impl::internal::ClientCallbackWriterFactory; - friend class ::grpc_impl::internal::ClientCallbackUnaryFactory; + friend class ::grpc::internal::ClientCallbackWriterFactory; + friend class ::grpc::internal::ClientCallbackUnaryFactory; template friend class ::grpc::internal::BlockingUnaryCallImpl; template - friend class ::grpc_impl::internal::CallbackUnaryCallImpl; + friend class ::grpc::internal::CallbackUnaryCallImpl; friend class ::grpc::internal::RpcMethod; friend class ::grpc::experimental::DelegatingChannel; friend class ::grpc::internal::InterceptedChannel; diff --git a/include/grpcpp/impl/codegen/client_callback.h b/include/grpcpp/impl/codegen/client_callback.h index a8b58f6b700..90c817ceaa7 100644 --- a/include/grpcpp/impl/codegen/client_callback.h +++ b/include/grpcpp/impl/codegen/client_callback.h @@ -1,6 +1,6 @@ /* * - * Copyright 2018 gRPC authors. + * Copyright 2019 gRPC authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,64 +13,1207 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ #ifndef GRPCPP_IMPL_CODEGEN_CLIENT_CALLBACK_H #define GRPCPP_IMPL_CODEGEN_CLIENT_CALLBACK_H +#include +#include -#include +#include +#include +#include +#include +#include +#include +#include namespace grpc { +class Channel; +class ClientContext; + +namespace internal { +class RpcMethod; + +/// Perform a callback-based unary call +/// TODO(vjpai): Combine as much as possible with the blocking unary call code +template +void CallbackUnaryCall(::grpc::ChannelInterface* channel, + const ::grpc::internal::RpcMethod& method, + ::grpc::ClientContext* context, + const InputMessage* request, OutputMessage* result, + std::function on_completion) { + CallbackUnaryCallImpl x( + channel, method, context, request, result, on_completion); +} + +template +class CallbackUnaryCallImpl { + public: + CallbackUnaryCallImpl(::grpc::ChannelInterface* channel, + const ::grpc::internal::RpcMethod& method, + ::grpc::ClientContext* context, + const InputMessage* request, OutputMessage* result, + std::function on_completion) { + ::grpc::CompletionQueue* cq = channel->CallbackCQ(); + GPR_CODEGEN_ASSERT(cq != nullptr); + grpc::internal::Call call(channel->CreateCall(method, context, cq)); + + using FullCallOpSet = grpc::internal::CallOpSet< + ::grpc::internal::CallOpSendInitialMetadata, + grpc::internal::CallOpSendMessage, + grpc::internal::CallOpRecvInitialMetadata, + grpc::internal::CallOpRecvMessage, + grpc::internal::CallOpClientSendClose, + grpc::internal::CallOpClientRecvStatus>; + + struct OpSetAndTag { + FullCallOpSet opset; + grpc::internal::CallbackWithStatusTag tag; + }; + const size_t alloc_sz = sizeof(OpSetAndTag); + auto* const alloced = static_cast( + ::grpc::g_core_codegen_interface->grpc_call_arena_alloc(call.call(), + alloc_sz)); + auto* ops = new (&alloced->opset) FullCallOpSet; + auto* tag = new (&alloced->tag) + grpc::internal::CallbackWithStatusTag(call.call(), on_completion, ops); + + // TODO(vjpai): Unify code with sync API as much as possible + ::grpc::Status s = ops->SendMessagePtr(request); + if (!s.ok()) { + tag->force_run(s); + return; + } + ops->SendInitialMetadata(&context->send_initial_metadata_, + context->initial_metadata_flags()); + ops->RecvInitialMetadata(context); + ops->RecvMessage(result); + ops->AllowNoMessage(); + ops->ClientSendClose(); + ops->ClientRecvStatus(context, tag->status_ptr()); + ops->set_core_cq_tag(tag); + call.PerformOps(ops); + } +}; + +// Base class for public API classes. +class ClientReactor { + public: + /// Called by the library when all operations associated with this RPC have + /// completed and all Holds have been removed. OnDone provides the RPC status + /// outcome for both successful and failed RPCs. If it is never called on an + /// RPC, it indicates an application-level problem (like failure to remove a + /// hold). + /// + /// \param[in] s The status outcome of this RPC + virtual void OnDone(const ::grpc::Status& /*s*/) = 0; + + /// InternalScheduleOnDone is not part of the API and is not meant to be + /// overridden. It is virtual to allow successful builds for certain bazel + /// build users that only want to depend on gRPC codegen headers and not the + /// full library (although this is not a generally-supported option). Although + /// the virtual call is slower than a direct call, this function is + /// heavyweight and the cost of the virtual call is not much in comparison. + /// This function may be removed or devirtualized in the future. + virtual void InternalScheduleOnDone(::grpc::Status s); +}; + +} // namespace internal -#ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL +// Forward declarations +template +class ClientBidiReactor; template -using ClientCallbackReader = ::grpc_impl::ClientCallbackReader; +class ClientReadReactor; +template +class ClientWriteReactor; +class ClientUnaryReactor; + +// NOTE: The streaming objects are not actually implemented in the public API. +// These interfaces are provided for mocking only. Typical applications +// will interact exclusively with the reactors that they define. +template +class ClientCallbackReaderWriter { + public: + virtual ~ClientCallbackReaderWriter() {} + virtual void StartCall() = 0; + virtual void Write(const Request* req, ::grpc::WriteOptions options) = 0; + virtual void WritesDone() = 0; + virtual void Read(Response* resp) = 0; + virtual void AddHold(int holds) = 0; + virtual void RemoveHold() = 0; + + protected: + void BindReactor(ClientBidiReactor* reactor) { + reactor->BindStream(this); + } +}; + +template +class ClientCallbackReader { + public: + virtual ~ClientCallbackReader() {} + virtual void StartCall() = 0; + virtual void Read(Response* resp) = 0; + virtual void AddHold(int holds) = 0; + virtual void RemoveHold() = 0; + + protected: + void BindReactor(ClientReadReactor* reactor) { + reactor->BindReader(this); + } +}; template -using ClientCallbackWriter = ::grpc_impl::ClientCallbackWriter; +class ClientCallbackWriter { + public: + virtual ~ClientCallbackWriter() {} + virtual void StartCall() = 0; + void Write(const Request* req) { Write(req, ::grpc::WriteOptions()); } + virtual void Write(const Request* req, ::grpc::WriteOptions options) = 0; + void WriteLast(const Request* req, ::grpc::WriteOptions options) { + Write(req, options.set_last_message()); + } + virtual void WritesDone() = 0; + + virtual void AddHold(int holds) = 0; + virtual void RemoveHold() = 0; + protected: + void BindReactor(ClientWriteReactor* reactor) { + reactor->BindWriter(this); + } +}; + +class ClientCallbackUnary { + public: + virtual ~ClientCallbackUnary() {} + virtual void StartCall() = 0; + + protected: + void BindReactor(ClientUnaryReactor* reactor); +}; + +// The following classes are the reactor interfaces that are to be implemented +// by the user. They are passed in to the library as an argument to a call on a +// stub (either a codegen-ed call or a generic call). The streaming RPC is +// activated by calling StartCall, possibly after initiating StartRead, +// StartWrite, or AddHold operations on the streaming object. Note that none of +// the classes are pure; all reactions have a default empty reaction so that the +// user class only needs to override those classes that it cares about. +// The reactor must be passed to the stub invocation before any of the below +// operations can be called. + +/// \a ClientBidiReactor is the interface for a bidirectional streaming RPC. template -using ClientCallbackReaderWriter = - ::grpc_impl::ClientCallbackReaderWriter; +class ClientBidiReactor : public internal::ClientReactor { + public: + virtual ~ClientBidiReactor() {} + + /// Activate the RPC and initiate any reads or writes that have been Start'ed + /// before this call. All streaming RPCs issued by the client MUST have + /// StartCall invoked on them (even if they are canceled) as this call is the + /// activation of their lifecycle. + void StartCall() { stream_->StartCall(); } + + /// Initiate a read operation (or post it for later initiation if StartCall + /// has not yet been invoked). + /// + /// \param[out] resp Where to eventually store the read message. Valid when + /// the library calls OnReadDone + void StartRead(Response* resp) { stream_->Read(resp); } + + /// Initiate a write operation (or post it for later initiation if StartCall + /// has not yet been invoked). + /// + /// \param[in] req The message to be written. The library does not take + /// ownership but the caller must ensure that the message is + /// not deleted or modified until OnWriteDone is called. + void StartWrite(const Request* req) { + StartWrite(req, ::grpc::WriteOptions()); + } + + /// Initiate/post a write operation with specified options. + /// + /// \param[in] req The message to be written. The library does not take + /// ownership but the caller must ensure that the message is + /// not deleted or modified until OnWriteDone is called. + /// \param[in] options The WriteOptions to use for writing this message + void StartWrite(const Request* req, ::grpc::WriteOptions options) { + stream_->Write(req, std::move(options)); + } + + /// Initiate/post a write operation with specified options and an indication + /// that this is the last write (like StartWrite and StartWritesDone, merged). + /// Note that calling this means that no more calls to StartWrite, + /// StartWriteLast, or StartWritesDone are allowed. + /// + /// \param[in] req The message to be written. The library does not take + /// ownership but the caller must ensure that the message is + /// not deleted or modified until OnWriteDone is called. + /// \param[in] options The WriteOptions to use for writing this message + void StartWriteLast(const Request* req, ::grpc::WriteOptions options) { + StartWrite(req, std::move(options.set_last_message())); + } + /// Indicate that the RPC will have no more write operations. This can only be + /// issued once for a given RPC. This is not required or allowed if + /// StartWriteLast is used since that already has the same implication. + /// Note that calling this means that no more calls to StartWrite, + /// StartWriteLast, or StartWritesDone are allowed. + void StartWritesDone() { stream_->WritesDone(); } + + /// Holds are needed if (and only if) this stream has operations that take + /// place on it after StartCall but from outside one of the reactions + /// (OnReadDone, etc). This is _not_ a common use of the streaming API. + /// + /// Holds must be added before calling StartCall. If a stream still has a hold + /// in place, its resources will not be destroyed even if the status has + /// already come in from the wire and there are currently no active callbacks + /// outstanding. Similarly, the stream will not call OnDone if there are still + /// holds on it. + /// + /// For example, if a StartRead or StartWrite operation is going to be + /// initiated from elsewhere in the application, the application should call + /// AddHold or AddMultipleHolds before StartCall. If there is going to be, + /// for example, a read-flow and a write-flow taking place outside the + /// reactions, then call AddMultipleHolds(2) before StartCall. When the + /// application knows that it won't issue any more read operations (such as + /// when a read comes back as not ok), it should issue a RemoveHold(). It + /// should also call RemoveHold() again after it does StartWriteLast or + /// StartWritesDone that indicates that there will be no more write ops. + /// The number of RemoveHold calls must match the total number of AddHold + /// calls plus the number of holds added by AddMultipleHolds. + /// The argument to AddMultipleHolds must be positive. + void AddHold() { AddMultipleHolds(1); } + void AddMultipleHolds(int holds) { + GPR_CODEGEN_DEBUG_ASSERT(holds > 0); + stream_->AddHold(holds); + } + void RemoveHold() { stream_->RemoveHold(); } + + /// Notifies the application that all operations associated with this RPC + /// have completed and all Holds have been removed. OnDone provides the RPC + /// status outcome for both successful and failed RPCs and will be called in + /// all cases. If it is not called, it indicates an application-level problem + /// (like failure to remove a hold). + /// + /// \param[in] s The status outcome of this RPC + void OnDone(const ::grpc::Status& /*s*/) override {} + + /// Notifies the application that a read of initial metadata from the + /// server is done. If the application chooses not to implement this method, + /// it can assume that the initial metadata has been read before the first + /// call of OnReadDone or OnDone. + /// + /// \param[in] ok Was the initial metadata read successfully? If false, no + /// new read/write operation will succeed, and any further + /// Start* operations should not be called. + virtual void OnReadInitialMetadataDone(bool /*ok*/) {} + + /// Notifies the application that a StartRead operation completed. + /// + /// \param[in] ok Was it successful? If false, no new read/write operation + /// will succeed, and any further Start* should not be called. + virtual void OnReadDone(bool /*ok*/) {} + + /// Notifies the application that a StartWrite or StartWriteLast operation + /// completed. + /// + /// \param[in] ok Was it successful? If false, no new read/write operation + /// will succeed, and any further Start* should not be called. + virtual void OnWriteDone(bool /*ok*/) {} + + /// Notifies the application that a StartWritesDone operation completed. Note + /// that this is only used on explicit StartWritesDone operations and not for + /// those that are implicitly invoked as part of a StartWriteLast. + /// + /// \param[in] ok Was it successful? If false, the application will later see + /// the failure reflected as a bad status in OnDone and no + /// further Start* should be called. + virtual void OnWritesDoneDone(bool /*ok*/) {} + + private: + friend class ClientCallbackReaderWriter; + void BindStream(ClientCallbackReaderWriter* stream) { + stream_ = stream; + } + ClientCallbackReaderWriter* stream_; +}; + +/// \a ClientReadReactor is the interface for a server-streaming RPC. +/// All public methods behave as in ClientBidiReactor. template -using ClientReadReactor = ::grpc_impl::ClientReadReactor; +class ClientReadReactor : public internal::ClientReactor { + public: + virtual ~ClientReadReactor() {} + + void StartCall() { reader_->StartCall(); } + void StartRead(Response* resp) { reader_->Read(resp); } + void AddHold() { AddMultipleHolds(1); } + void AddMultipleHolds(int holds) { + GPR_CODEGEN_DEBUG_ASSERT(holds > 0); + reader_->AddHold(holds); + } + void RemoveHold() { reader_->RemoveHold(); } + + void OnDone(const ::grpc::Status& /*s*/) override {} + virtual void OnReadInitialMetadataDone(bool /*ok*/) {} + virtual void OnReadDone(bool /*ok*/) {} + + private: + friend class ClientCallbackReader; + void BindReader(ClientCallbackReader* reader) { reader_ = reader; } + ClientCallbackReader* reader_; +}; + +/// \a ClientWriteReactor is the interface for a client-streaming RPC. +/// All public methods behave as in ClientBidiReactor. template -using ClientWriteReactor = ::grpc_impl::ClientWriteReactor; +class ClientWriteReactor : public internal::ClientReactor { + public: + virtual ~ClientWriteReactor() {} + + void StartCall() { writer_->StartCall(); } + void StartWrite(const Request* req) { + StartWrite(req, ::grpc::WriteOptions()); + } + void StartWrite(const Request* req, ::grpc::WriteOptions options) { + writer_->Write(req, std::move(options)); + } + void StartWriteLast(const Request* req, ::grpc::WriteOptions options) { + StartWrite(req, std::move(options.set_last_message())); + } + void StartWritesDone() { writer_->WritesDone(); } + + void AddHold() { AddMultipleHolds(1); } + void AddMultipleHolds(int holds) { + GPR_CODEGEN_DEBUG_ASSERT(holds > 0); + writer_->AddHold(holds); + } + void RemoveHold() { writer_->RemoveHold(); } + + void OnDone(const ::grpc::Status& /*s*/) override {} + virtual void OnReadInitialMetadataDone(bool /*ok*/) {} + virtual void OnWriteDone(bool /*ok*/) {} + virtual void OnWritesDoneDone(bool /*ok*/) {} + + private: + friend class ClientCallbackWriter; + void BindWriter(ClientCallbackWriter* writer) { writer_ = writer; } + + ClientCallbackWriter* writer_; +}; + +/// \a ClientUnaryReactor is a reactor-style interface for a unary RPC. +/// This is _not_ a common way of invoking a unary RPC. In practice, this +/// option should be used only if the unary RPC wants to receive initial +/// metadata without waiting for the response to complete. Most deployments of +/// RPC systems do not use this option, but it is needed for generality. +/// All public methods behave as in ClientBidiReactor. +/// StartCall is included for consistency with the other reactor flavors: even +/// though there are no StartRead or StartWrite operations to queue before the +/// call (that is part of the unary call itself) and there is no reactor object +/// being created as a result of this call, we keep a consistent 2-phase +/// initiation API among all the reactor flavors. +class ClientUnaryReactor : public internal::ClientReactor { + public: + virtual ~ClientUnaryReactor() {} + + void StartCall() { call_->StartCall(); } + void OnDone(const ::grpc::Status& /*s*/) override {} + virtual void OnReadInitialMetadataDone(bool /*ok*/) {} + private: + friend class ClientCallbackUnary; + void BindCall(ClientCallbackUnary* call) { call_ = call; } + ClientCallbackUnary* call_; +}; + +// Define function out-of-line from class to avoid forward declaration issue +inline void ClientCallbackUnary::BindReactor(ClientUnaryReactor* reactor) { + reactor->BindCall(this); +} + +namespace internal { + +// Forward declare factory classes for friendship template -using ClientBidiReactor = ::grpc_impl::ClientBidiReactor; +class ClientCallbackReaderWriterFactory; +template +class ClientCallbackReaderFactory; +template +class ClientCallbackWriterFactory; + +template +class ClientCallbackReaderWriterImpl + : public ClientCallbackReaderWriter { + public: + // always allocated against a call arena, no memory free required + static void operator delete(void* /*ptr*/, std::size_t size) { + GPR_CODEGEN_ASSERT(size == sizeof(ClientCallbackReaderWriterImpl)); + } + + // This operator should never be called as the memory should be freed as part + // of the arena destruction. It only exists to provide a matching operator + // delete to the operator new so that some compilers will not complain (see + // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this + // there are no tests catching the compiler warning. + static void operator delete(void*, void*) { GPR_CODEGEN_ASSERT(false); } + + void StartCall() override { + // This call initiates two batches, plus any backlog, each with a callback + // 1. Send initial metadata (unless corked) + recv initial metadata + // 2. Any read backlog + // 3. Any write backlog + // 4. Recv trailing metadata (unless corked) + if (!start_corked_) { + start_ops_.SendInitialMetadata(&context_->send_initial_metadata_, + context_->initial_metadata_flags()); + } + + call_.PerformOps(&start_ops_); + + { + grpc::internal::MutexLock lock(&start_mu_); + + if (backlog_.read_ops) { + call_.PerformOps(&read_ops_); + } + if (backlog_.write_ops) { + call_.PerformOps(&write_ops_); + } + if (backlog_.writes_done_ops) { + call_.PerformOps(&writes_done_ops_); + } + call_.PerformOps(&finish_ops_); + // The last thing in this critical section is to set started_ so that it + // can be used lock-free as well. + started_.store(true, std::memory_order_release); + } + // MaybeFinish outside the lock to make sure that destruction of this object + // doesn't take place while holding the lock (which would cause the lock to + // be released after destruction) + this->MaybeFinish(/*from_reaction=*/false); + } + + void Read(Response* msg) override { + read_ops_.RecvMessage(msg); + callbacks_outstanding_.fetch_add(1, std::memory_order_relaxed); + if (GPR_UNLIKELY(!started_.load(std::memory_order_acquire))) { + grpc::internal::MutexLock lock(&start_mu_); + if (GPR_LIKELY(!started_.load(std::memory_order_relaxed))) { + backlog_.read_ops = true; + return; + } + } + call_.PerformOps(&read_ops_); + } + + void Write(const Request* msg, ::grpc::WriteOptions options) override { + if (options.is_last_message()) { + options.set_buffer_hint(); + write_ops_.ClientSendClose(); + } + // TODO(vjpai): don't assert + GPR_CODEGEN_ASSERT(write_ops_.SendMessagePtr(msg, options).ok()); + callbacks_outstanding_.fetch_add(1, std::memory_order_relaxed); + if (GPR_UNLIKELY(corked_write_needed_)) { + write_ops_.SendInitialMetadata(&context_->send_initial_metadata_, + context_->initial_metadata_flags()); + corked_write_needed_ = false; + } + + if (GPR_UNLIKELY(!started_.load(std::memory_order_acquire))) { + grpc::internal::MutexLock lock(&start_mu_); + if (GPR_LIKELY(!started_.load(std::memory_order_relaxed))) { + backlog_.write_ops = true; + return; + } + } + call_.PerformOps(&write_ops_); + } + void WritesDone() override { + writes_done_ops_.ClientSendClose(); + writes_done_tag_.Set(call_.call(), + [this](bool ok) { + reactor_->OnWritesDoneDone(ok); + MaybeFinish(/*from_reaction=*/true); + }, + &writes_done_ops_, /*can_inline=*/false); + writes_done_ops_.set_core_cq_tag(&writes_done_tag_); + callbacks_outstanding_.fetch_add(1, std::memory_order_relaxed); + if (GPR_UNLIKELY(corked_write_needed_)) { + writes_done_ops_.SendInitialMetadata(&context_->send_initial_metadata_, + context_->initial_metadata_flags()); + corked_write_needed_ = false; + } + if (GPR_UNLIKELY(!started_.load(std::memory_order_acquire))) { + grpc::internal::MutexLock lock(&start_mu_); + if (GPR_LIKELY(!started_.load(std::memory_order_relaxed))) { + backlog_.writes_done_ops = true; + return; + } + } + call_.PerformOps(&writes_done_ops_); + } + + void AddHold(int holds) override { + callbacks_outstanding_.fetch_add(holds, std::memory_order_relaxed); + } + void RemoveHold() override { MaybeFinish(/*from_reaction=*/false); } + + private: + friend class ClientCallbackReaderWriterFactory; + + ClientCallbackReaderWriterImpl(grpc::internal::Call call, + ::grpc::ClientContext* context, + ClientBidiReactor* reactor) + : context_(context), + call_(call), + reactor_(reactor), + start_corked_(context_->initial_metadata_corked_), + corked_write_needed_(start_corked_) { + this->BindReactor(reactor); + + // Set up the unchanging parts of the start, read, and write tags and ops. + start_tag_.Set(call_.call(), + [this](bool ok) { + reactor_->OnReadInitialMetadataDone(ok); + MaybeFinish(/*from_reaction=*/true); + }, + &start_ops_, /*can_inline=*/false); + start_ops_.RecvInitialMetadata(context_); + start_ops_.set_core_cq_tag(&start_tag_); + + write_tag_.Set(call_.call(), + [this](bool ok) { + reactor_->OnWriteDone(ok); + MaybeFinish(/*from_reaction=*/true); + }, + &write_ops_, /*can_inline=*/false); + write_ops_.set_core_cq_tag(&write_tag_); + + read_tag_.Set(call_.call(), + [this](bool ok) { + reactor_->OnReadDone(ok); + MaybeFinish(/*from_reaction=*/true); + }, + &read_ops_, /*can_inline=*/false); + read_ops_.set_core_cq_tag(&read_tag_); + + // Also set up the Finish tag and op set. + finish_tag_.Set( + call_.call(), + [this](bool /*ok*/) { MaybeFinish(/*from_reaction=*/true); }, + &finish_ops_, + /*can_inline=*/false); + finish_ops_.ClientRecvStatus(context_, &finish_status_); + finish_ops_.set_core_cq_tag(&finish_tag_); + } + + // MaybeFinish can be called from reactions or from user-initiated operations + // like StartCall or RemoveHold. If this is the last operation or hold on this + // object, it will invoke the OnDone reaction. If MaybeFinish was called from + // a reaction, it can call OnDone directly. If not, it would need to schedule + // OnDone onto an executor thread to avoid the possibility of deadlocking with + // any locks in the user code that invoked it. + void MaybeFinish(bool from_reaction) { + if (GPR_UNLIKELY(callbacks_outstanding_.fetch_sub( + 1, std::memory_order_acq_rel) == 1)) { + ::grpc::Status s = std::move(finish_status_); + auto* reactor = reactor_; + auto* call = call_.call(); + this->~ClientCallbackReaderWriterImpl(); + ::grpc::g_core_codegen_interface->grpc_call_unref(call); + if (GPR_LIKELY(from_reaction)) { + reactor->OnDone(s); + } else { + reactor->InternalScheduleOnDone(std::move(s)); + } + } + } + + ::grpc::ClientContext* const context_; + grpc::internal::Call call_; + ClientBidiReactor* const reactor_; + + grpc::internal::CallOpSet + start_ops_; + grpc::internal::CallbackWithSuccessTag start_tag_; + const bool start_corked_; + bool corked_write_needed_; // no lock needed since only accessed in + // Write/WritesDone which cannot be concurrent + + grpc::internal::CallOpSet finish_ops_; + grpc::internal::CallbackWithSuccessTag finish_tag_; + ::grpc::Status finish_status_; + + grpc::internal::CallOpSet + write_ops_; + grpc::internal::CallbackWithSuccessTag write_tag_; + + grpc::internal::CallOpSet + writes_done_ops_; + grpc::internal::CallbackWithSuccessTag writes_done_tag_; -typedef ::grpc_impl::ClientUnaryReactor ClientUnaryReactor; -#endif + grpc::internal::CallOpSet> + read_ops_; + grpc::internal::CallbackWithSuccessTag read_tag_; + + struct StartCallBacklog { + bool write_ops = false; + bool writes_done_ops = false; + bool read_ops = false; + }; + StartCallBacklog backlog_ /* GUARDED_BY(start_mu_) */; + + // Minimum of 3 callbacks to pre-register for start ops, StartCall, and finish + std::atomic callbacks_outstanding_{3}; + std::atomic_bool started_{false}; + grpc::internal::Mutex start_mu_; +}; + +template +class ClientCallbackReaderWriterFactory { + public: + static void Create(::grpc::ChannelInterface* channel, + const ::grpc::internal::RpcMethod& method, + ::grpc::ClientContext* context, + ClientBidiReactor* reactor) { + grpc::internal::Call call = + channel->CreateCall(method, context, channel->CallbackCQ()); + + ::grpc::g_core_codegen_interface->grpc_call_ref(call.call()); + new (::grpc::g_core_codegen_interface->grpc_call_arena_alloc( + call.call(), sizeof(ClientCallbackReaderWriterImpl))) + ClientCallbackReaderWriterImpl(call, context, + reactor); + } +}; + +template +class ClientCallbackReaderImpl : public ClientCallbackReader { + public: + // always allocated against a call arena, no memory free required + static void operator delete(void* /*ptr*/, std::size_t size) { + GPR_CODEGEN_ASSERT(size == sizeof(ClientCallbackReaderImpl)); + } + + // This operator should never be called as the memory should be freed as part + // of the arena destruction. It only exists to provide a matching operator + // delete to the operator new so that some compilers will not complain (see + // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this + // there are no tests catching the compiler warning. + static void operator delete(void*, void*) { GPR_CODEGEN_ASSERT(false); } + + void StartCall() override { + // This call initiates two batches, plus any backlog, each with a callback + // 1. Send initial metadata (unless corked) + recv initial metadata + // 2. Any backlog + // 3. Recv trailing metadata + + start_tag_.Set(call_.call(), + [this](bool ok) { + reactor_->OnReadInitialMetadataDone(ok); + MaybeFinish(/*from_reaction=*/true); + }, + &start_ops_, /*can_inline=*/false); + start_ops_.SendInitialMetadata(&context_->send_initial_metadata_, + context_->initial_metadata_flags()); + start_ops_.RecvInitialMetadata(context_); + start_ops_.set_core_cq_tag(&start_tag_); + call_.PerformOps(&start_ops_); + + // Also set up the read tag so it doesn't have to be set up each time + read_tag_.Set(call_.call(), + [this](bool ok) { + reactor_->OnReadDone(ok); + MaybeFinish(/*from_reaction=*/true); + }, + &read_ops_, /*can_inline=*/false); + read_ops_.set_core_cq_tag(&read_tag_); + + { + grpc::internal::MutexLock lock(&start_mu_); + if (backlog_.read_ops) { + call_.PerformOps(&read_ops_); + } + started_.store(true, std::memory_order_release); + } + + finish_tag_.Set( + call_.call(), + [this](bool /*ok*/) { MaybeFinish(/*from_reaction=*/true); }, + &finish_ops_, /*can_inline=*/false); + finish_ops_.ClientRecvStatus(context_, &finish_status_); + finish_ops_.set_core_cq_tag(&finish_tag_); + call_.PerformOps(&finish_ops_); + } + + void Read(Response* msg) override { + read_ops_.RecvMessage(msg); + callbacks_outstanding_.fetch_add(1, std::memory_order_relaxed); + if (GPR_UNLIKELY(!started_.load(std::memory_order_acquire))) { + grpc::internal::MutexLock lock(&start_mu_); + if (GPR_LIKELY(!started_.load(std::memory_order_relaxed))) { + backlog_.read_ops = true; + return; + } + } + call_.PerformOps(&read_ops_); + } + + void AddHold(int holds) override { + callbacks_outstanding_.fetch_add(holds, std::memory_order_relaxed); + } + void RemoveHold() override { MaybeFinish(/*from_reaction=*/false); } + + private: + friend class ClientCallbackReaderFactory; + + template + ClientCallbackReaderImpl(::grpc::internal::Call call, + ::grpc::ClientContext* context, Request* request, + ClientReadReactor* reactor) + : context_(context), call_(call), reactor_(reactor) { + this->BindReactor(reactor); + // TODO(vjpai): don't assert + GPR_CODEGEN_ASSERT(start_ops_.SendMessagePtr(request).ok()); + start_ops_.ClientSendClose(); + } + + // MaybeFinish behaves as in ClientCallbackReaderWriterImpl. + void MaybeFinish(bool from_reaction) { + if (GPR_UNLIKELY(callbacks_outstanding_.fetch_sub( + 1, std::memory_order_acq_rel) == 1)) { + ::grpc::Status s = std::move(finish_status_); + auto* reactor = reactor_; + auto* call = call_.call(); + this->~ClientCallbackReaderImpl(); + ::grpc::g_core_codegen_interface->grpc_call_unref(call); + if (GPR_LIKELY(from_reaction)) { + reactor->OnDone(s); + } else { + reactor->InternalScheduleOnDone(std::move(s)); + } + } + } + + ::grpc::ClientContext* const context_; + grpc::internal::Call call_; + ClientReadReactor* const reactor_; + + grpc::internal::CallOpSet + start_ops_; + grpc::internal::CallbackWithSuccessTag start_tag_; + + grpc::internal::CallOpSet finish_ops_; + grpc::internal::CallbackWithSuccessTag finish_tag_; + ::grpc::Status finish_status_; + + grpc::internal::CallOpSet> + read_ops_; + grpc::internal::CallbackWithSuccessTag read_tag_; + + struct StartCallBacklog { + bool read_ops = false; + }; + StartCallBacklog backlog_ /* GUARDED_BY(start_mu_) */; + + // Minimum of 2 callbacks to pre-register for start and finish + std::atomic callbacks_outstanding_{2}; + std::atomic_bool started_{false}; + grpc::internal::Mutex start_mu_; +}; + +template +class ClientCallbackReaderFactory { + public: + template + static void Create(::grpc::ChannelInterface* channel, + const ::grpc::internal::RpcMethod& method, + ::grpc::ClientContext* context, const Request* request, + ClientReadReactor* reactor) { + grpc::internal::Call call = + channel->CreateCall(method, context, channel->CallbackCQ()); + + ::grpc::g_core_codegen_interface->grpc_call_ref(call.call()); + new (::grpc::g_core_codegen_interface->grpc_call_arena_alloc( + call.call(), sizeof(ClientCallbackReaderImpl))) + ClientCallbackReaderImpl(call, context, request, reactor); + } +}; + +template +class ClientCallbackWriterImpl : public ClientCallbackWriter { + public: + // always allocated against a call arena, no memory free required + static void operator delete(void* /*ptr*/, std::size_t size) { + GPR_CODEGEN_ASSERT(size == sizeof(ClientCallbackWriterImpl)); + } + + // This operator should never be called as the memory should be freed as part + // of the arena destruction. It only exists to provide a matching operator + // delete to the operator new so that some compilers will not complain (see + // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this + // there are no tests catching the compiler warning. + static void operator delete(void*, void*) { GPR_CODEGEN_ASSERT(false); } + + void StartCall() override { + // This call initiates two batches, plus any backlog, each with a callback + // 1. Send initial metadata (unless corked) + recv initial metadata + // 2. Any backlog + // 3. Recv trailing metadata + + if (!start_corked_) { + start_ops_.SendInitialMetadata(&context_->send_initial_metadata_, + context_->initial_metadata_flags()); + } + call_.PerformOps(&start_ops_); + + { + grpc::internal::MutexLock lock(&start_mu_); + + if (backlog_.write_ops) { + call_.PerformOps(&write_ops_); + } + if (backlog_.writes_done_ops) { + call_.PerformOps(&writes_done_ops_); + } + call_.PerformOps(&finish_ops_); + // The last thing in this critical section is to set started_ so that it + // can be used lock-free as well. + started_.store(true, std::memory_order_release); + } + // MaybeFinish outside the lock to make sure that destruction of this object + // doesn't take place while holding the lock (which would cause the lock to + // be released after destruction) + this->MaybeFinish(/*from_reaction=*/false); + } + + void Write(const Request* msg, ::grpc::WriteOptions options) override { + if (GPR_UNLIKELY(options.is_last_message())) { + options.set_buffer_hint(); + write_ops_.ClientSendClose(); + } + // TODO(vjpai): don't assert + GPR_CODEGEN_ASSERT(write_ops_.SendMessagePtr(msg, options).ok()); + callbacks_outstanding_.fetch_add(1, std::memory_order_relaxed); + + if (GPR_UNLIKELY(corked_write_needed_)) { + write_ops_.SendInitialMetadata(&context_->send_initial_metadata_, + context_->initial_metadata_flags()); + corked_write_needed_ = false; + } + + if (GPR_UNLIKELY(!started_.load(std::memory_order_acquire))) { + grpc::internal::MutexLock lock(&start_mu_); + if (GPR_LIKELY(!started_.load(std::memory_order_relaxed))) { + backlog_.write_ops = true; + return; + } + } + call_.PerformOps(&write_ops_); + } + + void WritesDone() override { + writes_done_ops_.ClientSendClose(); + writes_done_tag_.Set(call_.call(), + [this](bool ok) { + reactor_->OnWritesDoneDone(ok); + MaybeFinish(/*from_reaction=*/true); + }, + &writes_done_ops_, /*can_inline=*/false); + writes_done_ops_.set_core_cq_tag(&writes_done_tag_); + callbacks_outstanding_.fetch_add(1, std::memory_order_relaxed); + + if (GPR_UNLIKELY(corked_write_needed_)) { + writes_done_ops_.SendInitialMetadata(&context_->send_initial_metadata_, + context_->initial_metadata_flags()); + corked_write_needed_ = false; + } + + if (GPR_UNLIKELY(!started_.load(std::memory_order_acquire))) { + grpc::internal::MutexLock lock(&start_mu_); + if (GPR_LIKELY(!started_.load(std::memory_order_relaxed))) { + backlog_.writes_done_ops = true; + return; + } + } + call_.PerformOps(&writes_done_ops_); + } + + void AddHold(int holds) override { + callbacks_outstanding_.fetch_add(holds, std::memory_order_relaxed); + } + void RemoveHold() override { MaybeFinish(/*from_reaction=*/false); } + + private: + friend class ClientCallbackWriterFactory; + + template + ClientCallbackWriterImpl(::grpc::internal::Call call, + ::grpc::ClientContext* context, Response* response, + ClientWriteReactor* reactor) + : context_(context), + call_(call), + reactor_(reactor), + start_corked_(context_->initial_metadata_corked_), + corked_write_needed_(start_corked_) { + this->BindReactor(reactor); + + // Set up the unchanging parts of the start and write tags and ops. + start_tag_.Set(call_.call(), + [this](bool ok) { + reactor_->OnReadInitialMetadataDone(ok); + MaybeFinish(/*from_reaction=*/true); + }, + &start_ops_, /*can_inline=*/false); + start_ops_.RecvInitialMetadata(context_); + start_ops_.set_core_cq_tag(&start_tag_); + + write_tag_.Set(call_.call(), + [this](bool ok) { + reactor_->OnWriteDone(ok); + MaybeFinish(/*from_reaction=*/true); + }, + &write_ops_, /*can_inline=*/false); + write_ops_.set_core_cq_tag(&write_tag_); + + // Also set up the Finish tag and op set. + finish_ops_.RecvMessage(response); + finish_ops_.AllowNoMessage(); + finish_tag_.Set( + call_.call(), + [this](bool /*ok*/) { MaybeFinish(/*from_reaction=*/true); }, + &finish_ops_, + /*can_inline=*/false); + finish_ops_.ClientRecvStatus(context_, &finish_status_); + finish_ops_.set_core_cq_tag(&finish_tag_); + } + + // MaybeFinish behaves as in ClientCallbackReaderWriterImpl. + void MaybeFinish(bool from_reaction) { + if (GPR_UNLIKELY(callbacks_outstanding_.fetch_sub( + 1, std::memory_order_acq_rel) == 1)) { + ::grpc::Status s = std::move(finish_status_); + auto* reactor = reactor_; + auto* call = call_.call(); + this->~ClientCallbackWriterImpl(); + ::grpc::g_core_codegen_interface->grpc_call_unref(call); + if (GPR_LIKELY(from_reaction)) { + reactor->OnDone(s); + } else { + reactor->InternalScheduleOnDone(std::move(s)); + } + } + } + + ::grpc::ClientContext* const context_; + grpc::internal::Call call_; + ClientWriteReactor* const reactor_; + + grpc::internal::CallOpSet + start_ops_; + grpc::internal::CallbackWithSuccessTag start_tag_; + const bool start_corked_; + bool corked_write_needed_; // no lock needed since only accessed in + // Write/WritesDone which cannot be concurrent + + grpc::internal::CallOpSet + finish_ops_; + grpc::internal::CallbackWithSuccessTag finish_tag_; + ::grpc::Status finish_status_; + + grpc::internal::CallOpSet + write_ops_; + grpc::internal::CallbackWithSuccessTag write_tag_; + + grpc::internal::CallOpSet + writes_done_ops_; + grpc::internal::CallbackWithSuccessTag writes_done_tag_; + + struct StartCallBacklog { + bool write_ops = false; + bool writes_done_ops = false; + }; + StartCallBacklog backlog_ /* GUARDED_BY(start_mu_) */; + + // Minimum of 3 callbacks to pre-register for start ops, StartCall, and finish + std::atomic callbacks_outstanding_{3}; + std::atomic_bool started_{false}; + grpc::internal::Mutex start_mu_; +}; + +template +class ClientCallbackWriterFactory { + public: + template + static void Create(::grpc::ChannelInterface* channel, + const ::grpc::internal::RpcMethod& method, + ::grpc::ClientContext* context, Response* response, + ClientWriteReactor* reactor) { + grpc::internal::Call call = + channel->CreateCall(method, context, channel->CallbackCQ()); + + ::grpc::g_core_codegen_interface->grpc_call_ref(call.call()); + new (::grpc::g_core_codegen_interface->grpc_call_arena_alloc( + call.call(), sizeof(ClientCallbackWriterImpl))) + ClientCallbackWriterImpl(call, context, response, reactor); + } +}; + +class ClientCallbackUnaryImpl final : public ClientCallbackUnary { + public: + // always allocated against a call arena, no memory free required + static void operator delete(void* /*ptr*/, std::size_t size) { + GPR_CODEGEN_ASSERT(size == sizeof(ClientCallbackUnaryImpl)); + } + + // This operator should never be called as the memory should be freed as part + // of the arena destruction. It only exists to provide a matching operator + // delete to the operator new so that some compilers will not complain (see + // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this + // there are no tests catching the compiler warning. + static void operator delete(void*, void*) { GPR_CODEGEN_ASSERT(false); } + + void StartCall() override { + // This call initiates two batches, each with a callback + // 1. Send initial metadata + write + writes done + recv initial metadata + // 2. Read message, recv trailing metadata + + start_tag_.Set(call_.call(), + [this](bool ok) { + reactor_->OnReadInitialMetadataDone(ok); + MaybeFinish(); + }, + &start_ops_, /*can_inline=*/false); + start_ops_.SendInitialMetadata(&context_->send_initial_metadata_, + context_->initial_metadata_flags()); + start_ops_.RecvInitialMetadata(context_); + start_ops_.set_core_cq_tag(&start_tag_); + call_.PerformOps(&start_ops_); + + finish_tag_.Set(call_.call(), [this](bool /*ok*/) { MaybeFinish(); }, + &finish_ops_, + /*can_inline=*/false); + finish_ops_.ClientRecvStatus(context_, &finish_status_); + finish_ops_.set_core_cq_tag(&finish_tag_); + call_.PerformOps(&finish_ops_); + } + + private: + friend class ClientCallbackUnaryFactory; + + template + ClientCallbackUnaryImpl(::grpc::internal::Call call, + ::grpc::ClientContext* context, Request* request, + Response* response, ClientUnaryReactor* reactor) + : context_(context), call_(call), reactor_(reactor) { + this->BindReactor(reactor); + // TODO(vjpai): don't assert + GPR_CODEGEN_ASSERT(start_ops_.SendMessagePtr(request).ok()); + start_ops_.ClientSendClose(); + finish_ops_.RecvMessage(response); + finish_ops_.AllowNoMessage(); + } + + // In the unary case, MaybeFinish is only ever invoked from a + // library-initiated reaction, so it will just directly call OnDone if this is + // the last reaction for this RPC. + void MaybeFinish() { + if (GPR_UNLIKELY(callbacks_outstanding_.fetch_sub( + 1, std::memory_order_acq_rel) == 1)) { + ::grpc::Status s = std::move(finish_status_); + auto* reactor = reactor_; + auto* call = call_.call(); + this->~ClientCallbackUnaryImpl(); + ::grpc::g_core_codegen_interface->grpc_call_unref(call); + reactor->OnDone(s); + } + } + + ::grpc::ClientContext* const context_; + grpc::internal::Call call_; + ClientUnaryReactor* const reactor_; + + grpc::internal::CallOpSet + start_ops_; + grpc::internal::CallbackWithSuccessTag start_tag_; + + grpc::internal::CallOpSet + finish_ops_; + grpc::internal::CallbackWithSuccessTag finish_tag_; + ::grpc::Status finish_status_; + + // This call will have 2 callbacks: start and finish + std::atomic callbacks_outstanding_{2}; +}; + +class ClientCallbackUnaryFactory { + public: + template + static void Create(::grpc::ChannelInterface* channel, + const ::grpc::internal::RpcMethod& method, + ::grpc::ClientContext* context, const Request* request, + Response* response, ClientUnaryReactor* reactor) { + grpc::internal::Call call = + channel->CreateCall(method, context, channel->CallbackCQ()); + + ::grpc::g_core_codegen_interface->grpc_call_ref(call.call()); + + new (::grpc::g_core_codegen_interface->grpc_call_arena_alloc( + call.call(), sizeof(ClientCallbackUnaryImpl))) + ClientCallbackUnaryImpl(call, context, request, response, reactor); + } +}; + +} // namespace internal // TODO(vjpai): Remove namespace experimental when de-experimentalized fully. namespace experimental { template -using ClientCallbackReader = ::grpc_impl::ClientCallbackReader; +using ClientCallbackReader = ::grpc::ClientCallbackReader; template -using ClientCallbackWriter = ::grpc_impl::ClientCallbackWriter; +using ClientCallbackWriter = ::grpc::ClientCallbackWriter; template using ClientCallbackReaderWriter = - ::grpc_impl::ClientCallbackReaderWriter; + ::grpc::ClientCallbackReaderWriter; template -using ClientReadReactor = ::grpc_impl::ClientReadReactor; +using ClientReadReactor = ::grpc::ClientReadReactor; template -using ClientWriteReactor = ::grpc_impl::ClientWriteReactor; +using ClientWriteReactor = ::grpc::ClientWriteReactor; template -using ClientBidiReactor = ::grpc_impl::ClientBidiReactor; +using ClientBidiReactor = ::grpc::ClientBidiReactor; -typedef ::grpc_impl::ClientUnaryReactor ClientUnaryReactor; +typedef ::grpc::ClientUnaryReactor ClientUnaryReactor; } // namespace experimental -} // namespace grpc +} // namespace grpc #endif // GRPCPP_IMPL_CODEGEN_CLIENT_CALLBACK_H diff --git a/include/grpcpp/impl/codegen/client_callback_impl.h b/include/grpcpp/impl/codegen/client_callback_impl.h deleted file mode 100644 index d620db37746..00000000000 --- a/include/grpcpp/impl/codegen/client_callback_impl.h +++ /dev/null @@ -1,1197 +0,0 @@ -/* - * - * Copyright 2019 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef GRPCPP_IMPL_CODEGEN_CLIENT_CALLBACK_IMPL_H -#define GRPCPP_IMPL_CODEGEN_CLIENT_CALLBACK_IMPL_H -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -namespace grpc { -class Channel; -class ClientContext; -namespace internal { -class RpcMethod; -} // namespace internal -} // namespace grpc - -namespace grpc_impl { - -namespace internal { - -/// Perform a callback-based unary call -/// TODO(vjpai): Combine as much as possible with the blocking unary call code -template -void CallbackUnaryCall(::grpc::ChannelInterface* channel, - const ::grpc::internal::RpcMethod& method, - ::grpc::ClientContext* context, - const InputMessage* request, OutputMessage* result, - std::function on_completion) { - CallbackUnaryCallImpl x( - channel, method, context, request, result, on_completion); -} - -template -class CallbackUnaryCallImpl { - public: - CallbackUnaryCallImpl(::grpc::ChannelInterface* channel, - const ::grpc::internal::RpcMethod& method, - ::grpc::ClientContext* context, - const InputMessage* request, OutputMessage* result, - std::function on_completion) { - ::grpc::CompletionQueue* cq = channel->CallbackCQ(); - GPR_CODEGEN_ASSERT(cq != nullptr); - grpc::internal::Call call(channel->CreateCall(method, context, cq)); - - using FullCallOpSet = grpc::internal::CallOpSet< - ::grpc::internal::CallOpSendInitialMetadata, - grpc::internal::CallOpSendMessage, - grpc::internal::CallOpRecvInitialMetadata, - grpc::internal::CallOpRecvMessage, - grpc::internal::CallOpClientSendClose, - grpc::internal::CallOpClientRecvStatus>; - - struct OpSetAndTag { - FullCallOpSet opset; - grpc::internal::CallbackWithStatusTag tag; - }; - const size_t alloc_sz = sizeof(OpSetAndTag); - auto* const alloced = static_cast( - ::grpc::g_core_codegen_interface->grpc_call_arena_alloc(call.call(), - alloc_sz)); - auto* ops = new (&alloced->opset) FullCallOpSet; - auto* tag = new (&alloced->tag) - grpc::internal::CallbackWithStatusTag(call.call(), on_completion, ops); - - // TODO(vjpai): Unify code with sync API as much as possible - ::grpc::Status s = ops->SendMessagePtr(request); - if (!s.ok()) { - tag->force_run(s); - return; - } - ops->SendInitialMetadata(&context->send_initial_metadata_, - context->initial_metadata_flags()); - ops->RecvInitialMetadata(context); - ops->RecvMessage(result); - ops->AllowNoMessage(); - ops->ClientSendClose(); - ops->ClientRecvStatus(context, tag->status_ptr()); - ops->set_core_cq_tag(tag); - call.PerformOps(ops); - } -}; - -// Base class for public API classes. -class ClientReactor { - public: - /// Called by the library when all operations associated with this RPC have - /// completed and all Holds have been removed. OnDone provides the RPC status - /// outcome for both successful and failed RPCs. If it is never called on an - /// RPC, it indicates an application-level problem (like failure to remove a - /// hold). - /// - /// \param[in] s The status outcome of this RPC - virtual void OnDone(const ::grpc::Status& /*s*/) = 0; - - /// InternalScheduleOnDone is not part of the API and is not meant to be - /// overridden. It is virtual to allow successful builds for certain bazel - /// build users that only want to depend on gRPC codegen headers and not the - /// full library (although this is not a generally-supported option). Although - /// the virtual call is slower than a direct call, this function is - /// heavyweight and the cost of the virtual call is not much in comparison. - /// This function may be removed or devirtualized in the future. - virtual void InternalScheduleOnDone(::grpc::Status s); -}; - -} // namespace internal - -// Forward declarations -template -class ClientBidiReactor; -template -class ClientReadReactor; -template -class ClientWriteReactor; -class ClientUnaryReactor; - -// NOTE: The streaming objects are not actually implemented in the public API. -// These interfaces are provided for mocking only. Typical applications -// will interact exclusively with the reactors that they define. -template -class ClientCallbackReaderWriter { - public: - virtual ~ClientCallbackReaderWriter() {} - virtual void StartCall() = 0; - virtual void Write(const Request* req, ::grpc::WriteOptions options) = 0; - virtual void WritesDone() = 0; - virtual void Read(Response* resp) = 0; - virtual void AddHold(int holds) = 0; - virtual void RemoveHold() = 0; - - protected: - void BindReactor(ClientBidiReactor* reactor) { - reactor->BindStream(this); - } -}; - -template -class ClientCallbackReader { - public: - virtual ~ClientCallbackReader() {} - virtual void StartCall() = 0; - virtual void Read(Response* resp) = 0; - virtual void AddHold(int holds) = 0; - virtual void RemoveHold() = 0; - - protected: - void BindReactor(ClientReadReactor* reactor) { - reactor->BindReader(this); - } -}; - -template -class ClientCallbackWriter { - public: - virtual ~ClientCallbackWriter() {} - virtual void StartCall() = 0; - void Write(const Request* req) { Write(req, ::grpc::WriteOptions()); } - virtual void Write(const Request* req, ::grpc::WriteOptions options) = 0; - void WriteLast(const Request* req, ::grpc::WriteOptions options) { - Write(req, options.set_last_message()); - } - virtual void WritesDone() = 0; - - virtual void AddHold(int holds) = 0; - virtual void RemoveHold() = 0; - - protected: - void BindReactor(ClientWriteReactor* reactor) { - reactor->BindWriter(this); - } -}; - -class ClientCallbackUnary { - public: - virtual ~ClientCallbackUnary() {} - virtual void StartCall() = 0; - - protected: - void BindReactor(ClientUnaryReactor* reactor); -}; - -// The following classes are the reactor interfaces that are to be implemented -// by the user. They are passed in to the library as an argument to a call on a -// stub (either a codegen-ed call or a generic call). The streaming RPC is -// activated by calling StartCall, possibly after initiating StartRead, -// StartWrite, or AddHold operations on the streaming object. Note that none of -// the classes are pure; all reactions have a default empty reaction so that the -// user class only needs to override those classes that it cares about. -// The reactor must be passed to the stub invocation before any of the below -// operations can be called. - -/// \a ClientBidiReactor is the interface for a bidirectional streaming RPC. -template -class ClientBidiReactor : public internal::ClientReactor { - public: - virtual ~ClientBidiReactor() {} - - /// Activate the RPC and initiate any reads or writes that have been Start'ed - /// before this call. All streaming RPCs issued by the client MUST have - /// StartCall invoked on them (even if they are canceled) as this call is the - /// activation of their lifecycle. - void StartCall() { stream_->StartCall(); } - - /// Initiate a read operation (or post it for later initiation if StartCall - /// has not yet been invoked). - /// - /// \param[out] resp Where to eventually store the read message. Valid when - /// the library calls OnReadDone - void StartRead(Response* resp) { stream_->Read(resp); } - - /// Initiate a write operation (or post it for later initiation if StartCall - /// has not yet been invoked). - /// - /// \param[in] req The message to be written. The library does not take - /// ownership but the caller must ensure that the message is - /// not deleted or modified until OnWriteDone is called. - void StartWrite(const Request* req) { - StartWrite(req, ::grpc::WriteOptions()); - } - - /// Initiate/post a write operation with specified options. - /// - /// \param[in] req The message to be written. The library does not take - /// ownership but the caller must ensure that the message is - /// not deleted or modified until OnWriteDone is called. - /// \param[in] options The WriteOptions to use for writing this message - void StartWrite(const Request* req, ::grpc::WriteOptions options) { - stream_->Write(req, std::move(options)); - } - - /// Initiate/post a write operation with specified options and an indication - /// that this is the last write (like StartWrite and StartWritesDone, merged). - /// Note that calling this means that no more calls to StartWrite, - /// StartWriteLast, or StartWritesDone are allowed. - /// - /// \param[in] req The message to be written. The library does not take - /// ownership but the caller must ensure that the message is - /// not deleted or modified until OnWriteDone is called. - /// \param[in] options The WriteOptions to use for writing this message - void StartWriteLast(const Request* req, ::grpc::WriteOptions options) { - StartWrite(req, std::move(options.set_last_message())); - } - - /// Indicate that the RPC will have no more write operations. This can only be - /// issued once for a given RPC. This is not required or allowed if - /// StartWriteLast is used since that already has the same implication. - /// Note that calling this means that no more calls to StartWrite, - /// StartWriteLast, or StartWritesDone are allowed. - void StartWritesDone() { stream_->WritesDone(); } - - /// Holds are needed if (and only if) this stream has operations that take - /// place on it after StartCall but from outside one of the reactions - /// (OnReadDone, etc). This is _not_ a common use of the streaming API. - /// - /// Holds must be added before calling StartCall. If a stream still has a hold - /// in place, its resources will not be destroyed even if the status has - /// already come in from the wire and there are currently no active callbacks - /// outstanding. Similarly, the stream will not call OnDone if there are still - /// holds on it. - /// - /// For example, if a StartRead or StartWrite operation is going to be - /// initiated from elsewhere in the application, the application should call - /// AddHold or AddMultipleHolds before StartCall. If there is going to be, - /// for example, a read-flow and a write-flow taking place outside the - /// reactions, then call AddMultipleHolds(2) before StartCall. When the - /// application knows that it won't issue any more read operations (such as - /// when a read comes back as not ok), it should issue a RemoveHold(). It - /// should also call RemoveHold() again after it does StartWriteLast or - /// StartWritesDone that indicates that there will be no more write ops. - /// The number of RemoveHold calls must match the total number of AddHold - /// calls plus the number of holds added by AddMultipleHolds. - /// The argument to AddMultipleHolds must be positive. - void AddHold() { AddMultipleHolds(1); } - void AddMultipleHolds(int holds) { - GPR_CODEGEN_DEBUG_ASSERT(holds > 0); - stream_->AddHold(holds); - } - void RemoveHold() { stream_->RemoveHold(); } - - /// Notifies the application that all operations associated with this RPC - /// have completed and all Holds have been removed. OnDone provides the RPC - /// status outcome for both successful and failed RPCs and will be called in - /// all cases. If it is not called, it indicates an application-level problem - /// (like failure to remove a hold). - /// - /// \param[in] s The status outcome of this RPC - void OnDone(const ::grpc::Status& /*s*/) override {} - - /// Notifies the application that a read of initial metadata from the - /// server is done. If the application chooses not to implement this method, - /// it can assume that the initial metadata has been read before the first - /// call of OnReadDone or OnDone. - /// - /// \param[in] ok Was the initial metadata read successfully? If false, no - /// new read/write operation will succeed, and any further - /// Start* operations should not be called. - virtual void OnReadInitialMetadataDone(bool /*ok*/) {} - - /// Notifies the application that a StartRead operation completed. - /// - /// \param[in] ok Was it successful? If false, no new read/write operation - /// will succeed, and any further Start* should not be called. - virtual void OnReadDone(bool /*ok*/) {} - - /// Notifies the application that a StartWrite or StartWriteLast operation - /// completed. - /// - /// \param[in] ok Was it successful? If false, no new read/write operation - /// will succeed, and any further Start* should not be called. - virtual void OnWriteDone(bool /*ok*/) {} - - /// Notifies the application that a StartWritesDone operation completed. Note - /// that this is only used on explicit StartWritesDone operations and not for - /// those that are implicitly invoked as part of a StartWriteLast. - /// - /// \param[in] ok Was it successful? If false, the application will later see - /// the failure reflected as a bad status in OnDone and no - /// further Start* should be called. - virtual void OnWritesDoneDone(bool /*ok*/) {} - - private: - friend class ClientCallbackReaderWriter; - void BindStream(ClientCallbackReaderWriter* stream) { - stream_ = stream; - } - ClientCallbackReaderWriter* stream_; -}; - -/// \a ClientReadReactor is the interface for a server-streaming RPC. -/// All public methods behave as in ClientBidiReactor. -template -class ClientReadReactor : public internal::ClientReactor { - public: - virtual ~ClientReadReactor() {} - - void StartCall() { reader_->StartCall(); } - void StartRead(Response* resp) { reader_->Read(resp); } - - void AddHold() { AddMultipleHolds(1); } - void AddMultipleHolds(int holds) { - GPR_CODEGEN_DEBUG_ASSERT(holds > 0); - reader_->AddHold(holds); - } - void RemoveHold() { reader_->RemoveHold(); } - - void OnDone(const ::grpc::Status& /*s*/) override {} - virtual void OnReadInitialMetadataDone(bool /*ok*/) {} - virtual void OnReadDone(bool /*ok*/) {} - - private: - friend class ClientCallbackReader; - void BindReader(ClientCallbackReader* reader) { reader_ = reader; } - ClientCallbackReader* reader_; -}; - -/// \a ClientWriteReactor is the interface for a client-streaming RPC. -/// All public methods behave as in ClientBidiReactor. -template -class ClientWriteReactor : public internal::ClientReactor { - public: - virtual ~ClientWriteReactor() {} - - void StartCall() { writer_->StartCall(); } - void StartWrite(const Request* req) { - StartWrite(req, ::grpc::WriteOptions()); - } - void StartWrite(const Request* req, ::grpc::WriteOptions options) { - writer_->Write(req, std::move(options)); - } - void StartWriteLast(const Request* req, ::grpc::WriteOptions options) { - StartWrite(req, std::move(options.set_last_message())); - } - void StartWritesDone() { writer_->WritesDone(); } - - void AddHold() { AddMultipleHolds(1); } - void AddMultipleHolds(int holds) { - GPR_CODEGEN_DEBUG_ASSERT(holds > 0); - writer_->AddHold(holds); - } - void RemoveHold() { writer_->RemoveHold(); } - - void OnDone(const ::grpc::Status& /*s*/) override {} - virtual void OnReadInitialMetadataDone(bool /*ok*/) {} - virtual void OnWriteDone(bool /*ok*/) {} - virtual void OnWritesDoneDone(bool /*ok*/) {} - - private: - friend class ClientCallbackWriter; - void BindWriter(ClientCallbackWriter* writer) { writer_ = writer; } - - ClientCallbackWriter* writer_; -}; - -/// \a ClientUnaryReactor is a reactor-style interface for a unary RPC. -/// This is _not_ a common way of invoking a unary RPC. In practice, this -/// option should be used only if the unary RPC wants to receive initial -/// metadata without waiting for the response to complete. Most deployments of -/// RPC systems do not use this option, but it is needed for generality. -/// All public methods behave as in ClientBidiReactor. -/// StartCall is included for consistency with the other reactor flavors: even -/// though there are no StartRead or StartWrite operations to queue before the -/// call (that is part of the unary call itself) and there is no reactor object -/// being created as a result of this call, we keep a consistent 2-phase -/// initiation API among all the reactor flavors. -class ClientUnaryReactor : public internal::ClientReactor { - public: - virtual ~ClientUnaryReactor() {} - - void StartCall() { call_->StartCall(); } - void OnDone(const ::grpc::Status& /*s*/) override {} - virtual void OnReadInitialMetadataDone(bool /*ok*/) {} - - private: - friend class ClientCallbackUnary; - void BindCall(ClientCallbackUnary* call) { call_ = call; } - ClientCallbackUnary* call_; -}; - -// Define function out-of-line from class to avoid forward declaration issue -inline void ClientCallbackUnary::BindReactor(ClientUnaryReactor* reactor) { - reactor->BindCall(this); -} - -namespace internal { - -// Forward declare factory classes for friendship -template -class ClientCallbackReaderWriterFactory; -template -class ClientCallbackReaderFactory; -template -class ClientCallbackWriterFactory; - -template -class ClientCallbackReaderWriterImpl - : public ClientCallbackReaderWriter { - public: - // always allocated against a call arena, no memory free required - static void operator delete(void* /*ptr*/, std::size_t size) { - GPR_CODEGEN_ASSERT(size == sizeof(ClientCallbackReaderWriterImpl)); - } - - // This operator should never be called as the memory should be freed as part - // of the arena destruction. It only exists to provide a matching operator - // delete to the operator new so that some compilers will not complain (see - // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this - // there are no tests catching the compiler warning. - static void operator delete(void*, void*) { GPR_CODEGEN_ASSERT(false); } - - void StartCall() override { - // This call initiates two batches, plus any backlog, each with a callback - // 1. Send initial metadata (unless corked) + recv initial metadata - // 2. Any read backlog - // 3. Any write backlog - // 4. Recv trailing metadata (unless corked) - if (!start_corked_) { - start_ops_.SendInitialMetadata(&context_->send_initial_metadata_, - context_->initial_metadata_flags()); - } - - call_.PerformOps(&start_ops_); - - { - grpc::internal::MutexLock lock(&start_mu_); - - if (backlog_.read_ops) { - call_.PerformOps(&read_ops_); - } - if (backlog_.write_ops) { - call_.PerformOps(&write_ops_); - } - if (backlog_.writes_done_ops) { - call_.PerformOps(&writes_done_ops_); - } - call_.PerformOps(&finish_ops_); - // The last thing in this critical section is to set started_ so that it - // can be used lock-free as well. - started_.store(true, std::memory_order_release); - } - // MaybeFinish outside the lock to make sure that destruction of this object - // doesn't take place while holding the lock (which would cause the lock to - // be released after destruction) - this->MaybeFinish(/*from_reaction=*/false); - } - - void Read(Response* msg) override { - read_ops_.RecvMessage(msg); - callbacks_outstanding_.fetch_add(1, std::memory_order_relaxed); - if (GPR_UNLIKELY(!started_.load(std::memory_order_acquire))) { - grpc::internal::MutexLock lock(&start_mu_); - if (GPR_LIKELY(!started_.load(std::memory_order_relaxed))) { - backlog_.read_ops = true; - return; - } - } - call_.PerformOps(&read_ops_); - } - - void Write(const Request* msg, ::grpc::WriteOptions options) override { - if (options.is_last_message()) { - options.set_buffer_hint(); - write_ops_.ClientSendClose(); - } - // TODO(vjpai): don't assert - GPR_CODEGEN_ASSERT(write_ops_.SendMessagePtr(msg, options).ok()); - callbacks_outstanding_.fetch_add(1, std::memory_order_relaxed); - if (GPR_UNLIKELY(corked_write_needed_)) { - write_ops_.SendInitialMetadata(&context_->send_initial_metadata_, - context_->initial_metadata_flags()); - corked_write_needed_ = false; - } - - if (GPR_UNLIKELY(!started_.load(std::memory_order_acquire))) { - grpc::internal::MutexLock lock(&start_mu_); - if (GPR_LIKELY(!started_.load(std::memory_order_relaxed))) { - backlog_.write_ops = true; - return; - } - } - call_.PerformOps(&write_ops_); - } - void WritesDone() override { - writes_done_ops_.ClientSendClose(); - writes_done_tag_.Set(call_.call(), - [this](bool ok) { - reactor_->OnWritesDoneDone(ok); - MaybeFinish(/*from_reaction=*/true); - }, - &writes_done_ops_, /*can_inline=*/false); - writes_done_ops_.set_core_cq_tag(&writes_done_tag_); - callbacks_outstanding_.fetch_add(1, std::memory_order_relaxed); - if (GPR_UNLIKELY(corked_write_needed_)) { - writes_done_ops_.SendInitialMetadata(&context_->send_initial_metadata_, - context_->initial_metadata_flags()); - corked_write_needed_ = false; - } - if (GPR_UNLIKELY(!started_.load(std::memory_order_acquire))) { - grpc::internal::MutexLock lock(&start_mu_); - if (GPR_LIKELY(!started_.load(std::memory_order_relaxed))) { - backlog_.writes_done_ops = true; - return; - } - } - call_.PerformOps(&writes_done_ops_); - } - - void AddHold(int holds) override { - callbacks_outstanding_.fetch_add(holds, std::memory_order_relaxed); - } - void RemoveHold() override { MaybeFinish(/*from_reaction=*/false); } - - private: - friend class ClientCallbackReaderWriterFactory; - - ClientCallbackReaderWriterImpl(grpc::internal::Call call, - ::grpc::ClientContext* context, - ClientBidiReactor* reactor) - : context_(context), - call_(call), - reactor_(reactor), - start_corked_(context_->initial_metadata_corked_), - corked_write_needed_(start_corked_) { - this->BindReactor(reactor); - - // Set up the unchanging parts of the start, read, and write tags and ops. - start_tag_.Set(call_.call(), - [this](bool ok) { - reactor_->OnReadInitialMetadataDone(ok); - MaybeFinish(/*from_reaction=*/true); - }, - &start_ops_, /*can_inline=*/false); - start_ops_.RecvInitialMetadata(context_); - start_ops_.set_core_cq_tag(&start_tag_); - - write_tag_.Set(call_.call(), - [this](bool ok) { - reactor_->OnWriteDone(ok); - MaybeFinish(/*from_reaction=*/true); - }, - &write_ops_, /*can_inline=*/false); - write_ops_.set_core_cq_tag(&write_tag_); - - read_tag_.Set(call_.call(), - [this](bool ok) { - reactor_->OnReadDone(ok); - MaybeFinish(/*from_reaction=*/true); - }, - &read_ops_, /*can_inline=*/false); - read_ops_.set_core_cq_tag(&read_tag_); - - // Also set up the Finish tag and op set. - finish_tag_.Set( - call_.call(), - [this](bool /*ok*/) { MaybeFinish(/*from_reaction=*/true); }, - &finish_ops_, - /*can_inline=*/false); - finish_ops_.ClientRecvStatus(context_, &finish_status_); - finish_ops_.set_core_cq_tag(&finish_tag_); - } - - // MaybeFinish can be called from reactions or from user-initiated operations - // like StartCall or RemoveHold. If this is the last operation or hold on this - // object, it will invoke the OnDone reaction. If MaybeFinish was called from - // a reaction, it can call OnDone directly. If not, it would need to schedule - // OnDone onto an executor thread to avoid the possibility of deadlocking with - // any locks in the user code that invoked it. - void MaybeFinish(bool from_reaction) { - if (GPR_UNLIKELY(callbacks_outstanding_.fetch_sub( - 1, std::memory_order_acq_rel) == 1)) { - ::grpc::Status s = std::move(finish_status_); - auto* reactor = reactor_; - auto* call = call_.call(); - this->~ClientCallbackReaderWriterImpl(); - ::grpc::g_core_codegen_interface->grpc_call_unref(call); - if (GPR_LIKELY(from_reaction)) { - reactor->OnDone(s); - } else { - reactor->InternalScheduleOnDone(std::move(s)); - } - } - } - - ::grpc::ClientContext* const context_; - grpc::internal::Call call_; - ClientBidiReactor* const reactor_; - - grpc::internal::CallOpSet - start_ops_; - grpc::internal::CallbackWithSuccessTag start_tag_; - const bool start_corked_; - bool corked_write_needed_; // no lock needed since only accessed in - // Write/WritesDone which cannot be concurrent - - grpc::internal::CallOpSet finish_ops_; - grpc::internal::CallbackWithSuccessTag finish_tag_; - ::grpc::Status finish_status_; - - grpc::internal::CallOpSet - write_ops_; - grpc::internal::CallbackWithSuccessTag write_tag_; - - grpc::internal::CallOpSet - writes_done_ops_; - grpc::internal::CallbackWithSuccessTag writes_done_tag_; - - grpc::internal::CallOpSet> - read_ops_; - grpc::internal::CallbackWithSuccessTag read_tag_; - - struct StartCallBacklog { - bool write_ops = false; - bool writes_done_ops = false; - bool read_ops = false; - }; - StartCallBacklog backlog_ /* GUARDED_BY(start_mu_) */; - - // Minimum of 3 callbacks to pre-register for start ops, StartCall, and finish - std::atomic callbacks_outstanding_{3}; - std::atomic_bool started_{false}; - grpc::internal::Mutex start_mu_; -}; - -template -class ClientCallbackReaderWriterFactory { - public: - static void Create(::grpc::ChannelInterface* channel, - const ::grpc::internal::RpcMethod& method, - ::grpc::ClientContext* context, - ClientBidiReactor* reactor) { - grpc::internal::Call call = - channel->CreateCall(method, context, channel->CallbackCQ()); - - ::grpc::g_core_codegen_interface->grpc_call_ref(call.call()); - new (::grpc::g_core_codegen_interface->grpc_call_arena_alloc( - call.call(), sizeof(ClientCallbackReaderWriterImpl))) - ClientCallbackReaderWriterImpl(call, context, - reactor); - } -}; - -template -class ClientCallbackReaderImpl : public ClientCallbackReader { - public: - // always allocated against a call arena, no memory free required - static void operator delete(void* /*ptr*/, std::size_t size) { - GPR_CODEGEN_ASSERT(size == sizeof(ClientCallbackReaderImpl)); - } - - // This operator should never be called as the memory should be freed as part - // of the arena destruction. It only exists to provide a matching operator - // delete to the operator new so that some compilers will not complain (see - // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this - // there are no tests catching the compiler warning. - static void operator delete(void*, void*) { GPR_CODEGEN_ASSERT(false); } - - void StartCall() override { - // This call initiates two batches, plus any backlog, each with a callback - // 1. Send initial metadata (unless corked) + recv initial metadata - // 2. Any backlog - // 3. Recv trailing metadata - - start_tag_.Set(call_.call(), - [this](bool ok) { - reactor_->OnReadInitialMetadataDone(ok); - MaybeFinish(/*from_reaction=*/true); - }, - &start_ops_, /*can_inline=*/false); - start_ops_.SendInitialMetadata(&context_->send_initial_metadata_, - context_->initial_metadata_flags()); - start_ops_.RecvInitialMetadata(context_); - start_ops_.set_core_cq_tag(&start_tag_); - call_.PerformOps(&start_ops_); - - // Also set up the read tag so it doesn't have to be set up each time - read_tag_.Set(call_.call(), - [this](bool ok) { - reactor_->OnReadDone(ok); - MaybeFinish(/*from_reaction=*/true); - }, - &read_ops_, /*can_inline=*/false); - read_ops_.set_core_cq_tag(&read_tag_); - - { - grpc::internal::MutexLock lock(&start_mu_); - if (backlog_.read_ops) { - call_.PerformOps(&read_ops_); - } - started_.store(true, std::memory_order_release); - } - - finish_tag_.Set( - call_.call(), - [this](bool /*ok*/) { MaybeFinish(/*from_reaction=*/true); }, - &finish_ops_, /*can_inline=*/false); - finish_ops_.ClientRecvStatus(context_, &finish_status_); - finish_ops_.set_core_cq_tag(&finish_tag_); - call_.PerformOps(&finish_ops_); - } - - void Read(Response* msg) override { - read_ops_.RecvMessage(msg); - callbacks_outstanding_.fetch_add(1, std::memory_order_relaxed); - if (GPR_UNLIKELY(!started_.load(std::memory_order_acquire))) { - grpc::internal::MutexLock lock(&start_mu_); - if (GPR_LIKELY(!started_.load(std::memory_order_relaxed))) { - backlog_.read_ops = true; - return; - } - } - call_.PerformOps(&read_ops_); - } - - void AddHold(int holds) override { - callbacks_outstanding_.fetch_add(holds, std::memory_order_relaxed); - } - void RemoveHold() override { MaybeFinish(/*from_reaction=*/false); } - - private: - friend class ClientCallbackReaderFactory; - - template - ClientCallbackReaderImpl(::grpc::internal::Call call, - ::grpc::ClientContext* context, Request* request, - ClientReadReactor* reactor) - : context_(context), call_(call), reactor_(reactor) { - this->BindReactor(reactor); - // TODO(vjpai): don't assert - GPR_CODEGEN_ASSERT(start_ops_.SendMessagePtr(request).ok()); - start_ops_.ClientSendClose(); - } - - // MaybeFinish behaves as in ClientCallbackReaderWriterImpl. - void MaybeFinish(bool from_reaction) { - if (GPR_UNLIKELY(callbacks_outstanding_.fetch_sub( - 1, std::memory_order_acq_rel) == 1)) { - ::grpc::Status s = std::move(finish_status_); - auto* reactor = reactor_; - auto* call = call_.call(); - this->~ClientCallbackReaderImpl(); - ::grpc::g_core_codegen_interface->grpc_call_unref(call); - if (GPR_LIKELY(from_reaction)) { - reactor->OnDone(s); - } else { - reactor->InternalScheduleOnDone(std::move(s)); - } - } - } - - ::grpc::ClientContext* const context_; - grpc::internal::Call call_; - ClientReadReactor* const reactor_; - - grpc::internal::CallOpSet - start_ops_; - grpc::internal::CallbackWithSuccessTag start_tag_; - - grpc::internal::CallOpSet finish_ops_; - grpc::internal::CallbackWithSuccessTag finish_tag_; - ::grpc::Status finish_status_; - - grpc::internal::CallOpSet> - read_ops_; - grpc::internal::CallbackWithSuccessTag read_tag_; - - struct StartCallBacklog { - bool read_ops = false; - }; - StartCallBacklog backlog_ /* GUARDED_BY(start_mu_) */; - - // Minimum of 2 callbacks to pre-register for start and finish - std::atomic callbacks_outstanding_{2}; - std::atomic_bool started_{false}; - grpc::internal::Mutex start_mu_; -}; - -template -class ClientCallbackReaderFactory { - public: - template - static void Create(::grpc::ChannelInterface* channel, - const ::grpc::internal::RpcMethod& method, - ::grpc::ClientContext* context, const Request* request, - ClientReadReactor* reactor) { - grpc::internal::Call call = - channel->CreateCall(method, context, channel->CallbackCQ()); - - ::grpc::g_core_codegen_interface->grpc_call_ref(call.call()); - new (::grpc::g_core_codegen_interface->grpc_call_arena_alloc( - call.call(), sizeof(ClientCallbackReaderImpl))) - ClientCallbackReaderImpl(call, context, request, reactor); - } -}; - -template -class ClientCallbackWriterImpl : public ClientCallbackWriter { - public: - // always allocated against a call arena, no memory free required - static void operator delete(void* /*ptr*/, std::size_t size) { - GPR_CODEGEN_ASSERT(size == sizeof(ClientCallbackWriterImpl)); - } - - // This operator should never be called as the memory should be freed as part - // of the arena destruction. It only exists to provide a matching operator - // delete to the operator new so that some compilers will not complain (see - // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this - // there are no tests catching the compiler warning. - static void operator delete(void*, void*) { GPR_CODEGEN_ASSERT(false); } - - void StartCall() override { - // This call initiates two batches, plus any backlog, each with a callback - // 1. Send initial metadata (unless corked) + recv initial metadata - // 2. Any backlog - // 3. Recv trailing metadata - - if (!start_corked_) { - start_ops_.SendInitialMetadata(&context_->send_initial_metadata_, - context_->initial_metadata_flags()); - } - call_.PerformOps(&start_ops_); - - { - grpc::internal::MutexLock lock(&start_mu_); - - if (backlog_.write_ops) { - call_.PerformOps(&write_ops_); - } - if (backlog_.writes_done_ops) { - call_.PerformOps(&writes_done_ops_); - } - call_.PerformOps(&finish_ops_); - // The last thing in this critical section is to set started_ so that it - // can be used lock-free as well. - started_.store(true, std::memory_order_release); - } - // MaybeFinish outside the lock to make sure that destruction of this object - // doesn't take place while holding the lock (which would cause the lock to - // be released after destruction) - this->MaybeFinish(/*from_reaction=*/false); - } - - void Write(const Request* msg, ::grpc::WriteOptions options) override { - if (GPR_UNLIKELY(options.is_last_message())) { - options.set_buffer_hint(); - write_ops_.ClientSendClose(); - } - // TODO(vjpai): don't assert - GPR_CODEGEN_ASSERT(write_ops_.SendMessagePtr(msg, options).ok()); - callbacks_outstanding_.fetch_add(1, std::memory_order_relaxed); - - if (GPR_UNLIKELY(corked_write_needed_)) { - write_ops_.SendInitialMetadata(&context_->send_initial_metadata_, - context_->initial_metadata_flags()); - corked_write_needed_ = false; - } - - if (GPR_UNLIKELY(!started_.load(std::memory_order_acquire))) { - grpc::internal::MutexLock lock(&start_mu_); - if (GPR_LIKELY(!started_.load(std::memory_order_relaxed))) { - backlog_.write_ops = true; - return; - } - } - call_.PerformOps(&write_ops_); - } - - void WritesDone() override { - writes_done_ops_.ClientSendClose(); - writes_done_tag_.Set(call_.call(), - [this](bool ok) { - reactor_->OnWritesDoneDone(ok); - MaybeFinish(/*from_reaction=*/true); - }, - &writes_done_ops_, /*can_inline=*/false); - writes_done_ops_.set_core_cq_tag(&writes_done_tag_); - callbacks_outstanding_.fetch_add(1, std::memory_order_relaxed); - - if (GPR_UNLIKELY(corked_write_needed_)) { - writes_done_ops_.SendInitialMetadata(&context_->send_initial_metadata_, - context_->initial_metadata_flags()); - corked_write_needed_ = false; - } - - if (GPR_UNLIKELY(!started_.load(std::memory_order_acquire))) { - grpc::internal::MutexLock lock(&start_mu_); - if (GPR_LIKELY(!started_.load(std::memory_order_relaxed))) { - backlog_.writes_done_ops = true; - return; - } - } - call_.PerformOps(&writes_done_ops_); - } - - void AddHold(int holds) override { - callbacks_outstanding_.fetch_add(holds, std::memory_order_relaxed); - } - void RemoveHold() override { MaybeFinish(/*from_reaction=*/false); } - - private: - friend class ClientCallbackWriterFactory; - - template - ClientCallbackWriterImpl(::grpc::internal::Call call, - ::grpc::ClientContext* context, Response* response, - ClientWriteReactor* reactor) - : context_(context), - call_(call), - reactor_(reactor), - start_corked_(context_->initial_metadata_corked_), - corked_write_needed_(start_corked_) { - this->BindReactor(reactor); - - // Set up the unchanging parts of the start and write tags and ops. - start_tag_.Set(call_.call(), - [this](bool ok) { - reactor_->OnReadInitialMetadataDone(ok); - MaybeFinish(/*from_reaction=*/true); - }, - &start_ops_, /*can_inline=*/false); - start_ops_.RecvInitialMetadata(context_); - start_ops_.set_core_cq_tag(&start_tag_); - - write_tag_.Set(call_.call(), - [this](bool ok) { - reactor_->OnWriteDone(ok); - MaybeFinish(/*from_reaction=*/true); - }, - &write_ops_, /*can_inline=*/false); - write_ops_.set_core_cq_tag(&write_tag_); - - // Also set up the Finish tag and op set. - finish_ops_.RecvMessage(response); - finish_ops_.AllowNoMessage(); - finish_tag_.Set( - call_.call(), - [this](bool /*ok*/) { MaybeFinish(/*from_reaction=*/true); }, - &finish_ops_, - /*can_inline=*/false); - finish_ops_.ClientRecvStatus(context_, &finish_status_); - finish_ops_.set_core_cq_tag(&finish_tag_); - } - - // MaybeFinish behaves as in ClientCallbackReaderWriterImpl. - void MaybeFinish(bool from_reaction) { - if (GPR_UNLIKELY(callbacks_outstanding_.fetch_sub( - 1, std::memory_order_acq_rel) == 1)) { - ::grpc::Status s = std::move(finish_status_); - auto* reactor = reactor_; - auto* call = call_.call(); - this->~ClientCallbackWriterImpl(); - ::grpc::g_core_codegen_interface->grpc_call_unref(call); - if (GPR_LIKELY(from_reaction)) { - reactor->OnDone(s); - } else { - reactor->InternalScheduleOnDone(std::move(s)); - } - } - } - - ::grpc::ClientContext* const context_; - grpc::internal::Call call_; - ClientWriteReactor* const reactor_; - - grpc::internal::CallOpSet - start_ops_; - grpc::internal::CallbackWithSuccessTag start_tag_; - const bool start_corked_; - bool corked_write_needed_; // no lock needed since only accessed in - // Write/WritesDone which cannot be concurrent - - grpc::internal::CallOpSet - finish_ops_; - grpc::internal::CallbackWithSuccessTag finish_tag_; - ::grpc::Status finish_status_; - - grpc::internal::CallOpSet - write_ops_; - grpc::internal::CallbackWithSuccessTag write_tag_; - - grpc::internal::CallOpSet - writes_done_ops_; - grpc::internal::CallbackWithSuccessTag writes_done_tag_; - - struct StartCallBacklog { - bool write_ops = false; - bool writes_done_ops = false; - }; - StartCallBacklog backlog_ /* GUARDED_BY(start_mu_) */; - - // Minimum of 3 callbacks to pre-register for start ops, StartCall, and finish - std::atomic callbacks_outstanding_{3}; - std::atomic_bool started_{false}; - grpc::internal::Mutex start_mu_; -}; - -template -class ClientCallbackWriterFactory { - public: - template - static void Create(::grpc::ChannelInterface* channel, - const ::grpc::internal::RpcMethod& method, - ::grpc::ClientContext* context, Response* response, - ClientWriteReactor* reactor) { - grpc::internal::Call call = - channel->CreateCall(method, context, channel->CallbackCQ()); - - ::grpc::g_core_codegen_interface->grpc_call_ref(call.call()); - new (::grpc::g_core_codegen_interface->grpc_call_arena_alloc( - call.call(), sizeof(ClientCallbackWriterImpl))) - ClientCallbackWriterImpl(call, context, response, reactor); - } -}; - -class ClientCallbackUnaryImpl final : public ClientCallbackUnary { - public: - // always allocated against a call arena, no memory free required - static void operator delete(void* /*ptr*/, std::size_t size) { - GPR_CODEGEN_ASSERT(size == sizeof(ClientCallbackUnaryImpl)); - } - - // This operator should never be called as the memory should be freed as part - // of the arena destruction. It only exists to provide a matching operator - // delete to the operator new so that some compilers will not complain (see - // https://github.com/grpc/grpc/issues/11301) Note at the time of adding this - // there are no tests catching the compiler warning. - static void operator delete(void*, void*) { GPR_CODEGEN_ASSERT(false); } - - void StartCall() override { - // This call initiates two batches, each with a callback - // 1. Send initial metadata + write + writes done + recv initial metadata - // 2. Read message, recv trailing metadata - - start_tag_.Set(call_.call(), - [this](bool ok) { - reactor_->OnReadInitialMetadataDone(ok); - MaybeFinish(); - }, - &start_ops_, /*can_inline=*/false); - start_ops_.SendInitialMetadata(&context_->send_initial_metadata_, - context_->initial_metadata_flags()); - start_ops_.RecvInitialMetadata(context_); - start_ops_.set_core_cq_tag(&start_tag_); - call_.PerformOps(&start_ops_); - - finish_tag_.Set(call_.call(), [this](bool /*ok*/) { MaybeFinish(); }, - &finish_ops_, - /*can_inline=*/false); - finish_ops_.ClientRecvStatus(context_, &finish_status_); - finish_ops_.set_core_cq_tag(&finish_tag_); - call_.PerformOps(&finish_ops_); - } - - private: - friend class ClientCallbackUnaryFactory; - - template - ClientCallbackUnaryImpl(::grpc::internal::Call call, - ::grpc::ClientContext* context, Request* request, - Response* response, ClientUnaryReactor* reactor) - : context_(context), call_(call), reactor_(reactor) { - this->BindReactor(reactor); - // TODO(vjpai): don't assert - GPR_CODEGEN_ASSERT(start_ops_.SendMessagePtr(request).ok()); - start_ops_.ClientSendClose(); - finish_ops_.RecvMessage(response); - finish_ops_.AllowNoMessage(); - } - - // In the unary case, MaybeFinish is only ever invoked from a - // library-initiated reaction, so it will just directly call OnDone if this is - // the last reaction for this RPC. - void MaybeFinish() { - if (GPR_UNLIKELY(callbacks_outstanding_.fetch_sub( - 1, std::memory_order_acq_rel) == 1)) { - ::grpc::Status s = std::move(finish_status_); - auto* reactor = reactor_; - auto* call = call_.call(); - this->~ClientCallbackUnaryImpl(); - ::grpc::g_core_codegen_interface->grpc_call_unref(call); - reactor->OnDone(s); - } - } - - ::grpc::ClientContext* const context_; - grpc::internal::Call call_; - ClientUnaryReactor* const reactor_; - - grpc::internal::CallOpSet - start_ops_; - grpc::internal::CallbackWithSuccessTag start_tag_; - - grpc::internal::CallOpSet - finish_ops_; - grpc::internal::CallbackWithSuccessTag finish_tag_; - ::grpc::Status finish_status_; - - // This call will have 2 callbacks: start and finish - std::atomic callbacks_outstanding_{2}; -}; - -class ClientCallbackUnaryFactory { - public: - template - static void Create(::grpc::ChannelInterface* channel, - const ::grpc::internal::RpcMethod& method, - ::grpc::ClientContext* context, const Request* request, - Response* response, ClientUnaryReactor* reactor) { - grpc::internal::Call call = - channel->CreateCall(method, context, channel->CallbackCQ()); - - ::grpc::g_core_codegen_interface->grpc_call_ref(call.call()); - - new (::grpc::g_core_codegen_interface->grpc_call_arena_alloc( - call.call(), sizeof(ClientCallbackUnaryImpl))) - ClientCallbackUnaryImpl(call, context, request, response, reactor); - } -}; - -} // namespace internal -} // namespace grpc_impl -#endif // GRPCPP_IMPL_CODEGEN_CLIENT_CALLBACK_IMPL_H diff --git a/include/grpcpp/impl/codegen/client_context.h b/include/grpcpp/impl/codegen/client_context.h index ce338720f2c..2efba060840 100644 --- a/include/grpcpp/impl/codegen/client_context.h +++ b/include/grpcpp/impl/codegen/client_context.h @@ -56,7 +56,10 @@ struct census_context; struct grpc_call; -namespace grpc_impl { +namespace grpc { +class ServerContext; +class ServerContextBase; +class CallbackServerContext; namespace internal { template @@ -86,14 +89,6 @@ class ClientAsyncReaderWriter; template class ClientAsyncResponseReader; -} // namespace grpc_impl - -namespace grpc { - -class ServerContext; -class ServerContextBase; -class CallbackServerContext; - namespace testing { class InteropClientContextInspector; } // namespace testing @@ -431,31 +426,31 @@ class ClientContext { friend class ::grpc::internal::CallOpRecvInitialMetadata; friend class ::grpc::Channel; template - friend class ::grpc_impl::ClientReader; + friend class ::grpc::ClientReader; template - friend class ::grpc_impl::ClientWriter; + friend class ::grpc::ClientWriter; template - friend class ::grpc_impl::ClientReaderWriter; + friend class ::grpc::ClientReaderWriter; template - friend class ::grpc_impl::ClientAsyncReader; + friend class ::grpc::ClientAsyncReader; template - friend class ::grpc_impl::ClientAsyncWriter; + friend class ::grpc::ClientAsyncWriter; template - friend class ::grpc_impl::ClientAsyncReaderWriter; + friend class ::grpc::ClientAsyncReaderWriter; template - friend class ::grpc_impl::ClientAsyncResponseReader; + friend class ::grpc::ClientAsyncResponseReader; template friend class ::grpc::internal::BlockingUnaryCallImpl; template - friend class ::grpc_impl::internal::CallbackUnaryCallImpl; + friend class ::grpc::internal::CallbackUnaryCallImpl; template - friend class ::grpc_impl::internal::ClientCallbackReaderWriterImpl; + friend class ::grpc::internal::ClientCallbackReaderWriterImpl; template - friend class ::grpc_impl::internal::ClientCallbackReaderImpl; + friend class ::grpc::internal::ClientCallbackReaderImpl; template - friend class ::grpc_impl::internal::ClientCallbackWriterImpl; - friend class ::grpc_impl::internal::ClientCallbackUnaryImpl; - friend class ::grpc_impl::internal::ClientContextAccessor; + friend class ::grpc::internal::ClientCallbackWriterImpl; + friend class ::grpc::internal::ClientCallbackUnaryImpl; + friend class ::grpc::internal::ClientContextAccessor; // Used by friend class CallOpClientRecvStatus void set_debug_error_string(const std::string& debug_error_string) { diff --git a/include/grpcpp/impl/codegen/completion_queue.h b/include/grpcpp/impl/codegen/completion_queue.h index 4ed20411da6..a3a65778f33 100644 --- a/include/grpcpp/impl/codegen/completion_queue.h +++ b/include/grpcpp/impl/codegen/completion_queue.h @@ -45,7 +45,10 @@ struct grpc_completion_queue; namespace grpc_impl { +class ServerContextBase; +} // namespace grpc_impl +namespace grpc { template class ClientReader; template @@ -71,8 +74,6 @@ class TemplatedBidiStreamingHandler; template <::grpc::StatusCode code> class ErrorMethodHandler; } // namespace internal -} // namespace grpc_impl -namespace grpc { class Channel; class ChannelInterface; @@ -257,27 +258,27 @@ class CompletionQueue : private ::grpc::GrpcLibraryCodegen { // Friend synchronous wrappers so that they can access Pluck(), which is // a semi-private API geared towards the synchronous implementation. template - friend class ::grpc_impl::ClientReader; + friend class ::grpc::ClientReader; template - friend class ::grpc_impl::ClientWriter; + friend class ::grpc::ClientWriter; template - friend class ::grpc_impl::ClientReaderWriter; + friend class ::grpc::ClientReaderWriter; template - friend class ::grpc_impl::ServerReader; + friend class ::grpc::ServerReader; template - friend class ::grpc_impl::ServerWriter; + friend class ::grpc::ServerWriter; template - friend class ::grpc_impl::internal::ServerReaderWriterBody; + friend class ::grpc::internal::ServerReaderWriterBody; template - friend class ::grpc_impl::internal::RpcMethodHandler; + friend class ::grpc::internal::RpcMethodHandler; template - friend class ::grpc_impl::internal::ClientStreamingHandler; + friend class ::grpc::internal::ClientStreamingHandler; template - friend class ::grpc_impl::internal::ServerStreamingHandler; + friend class ::grpc::internal::ServerStreamingHandler; template - friend class ::grpc_impl::internal::TemplatedBidiStreamingHandler; + friend class ::grpc::internal::TemplatedBidiStreamingHandler; template <::grpc::StatusCode code> - friend class ::grpc_impl::internal::ErrorMethodHandler; + friend class ::grpc::internal::ErrorMethodHandler; friend class ::grpc::ServerContextBase; friend class ::grpc::ServerInterface; template diff --git a/include/grpcpp/impl/codegen/method_handler.h b/include/grpcpp/impl/codegen/method_handler.h index 4b0f594f237..0033936b04b 100644 --- a/include/grpcpp/impl/codegen/method_handler.h +++ b/include/grpcpp/impl/codegen/method_handler.h @@ -1,6 +1,6 @@ /* * - * Copyright 2019 gRPC authors. + * Copyright 2015 gRPC authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,55 +19,363 @@ #ifndef GRPCPP_IMPL_CODEGEN_METHOD_HANDLER_H #define GRPCPP_IMPL_CODEGEN_METHOD_HANDLER_H -#include +#include +#include +#include +#include namespace grpc { namespace internal { -template -using BidiStreamingHandler = - ::grpc_impl::internal::BidiStreamingHandler; +// Invoke the method handler, fill in the status, and +// return whether or not we finished safely (without an exception). +// Note that exception handling is 0-cost in most compiler/library +// implementations (except when an exception is actually thrown), +// so this process doesn't require additional overhead in the common case. +// Additionally, we don't need to return if we caught an exception or not; +// the handling is the same in either case. +template +::grpc::Status CatchingFunctionHandler(Callable&& handler) { +#if GRPC_ALLOW_EXCEPTIONS + try { + return handler(); + } catch (...) { + return ::grpc::Status(::grpc::StatusCode::UNKNOWN, + "Unexpected error in RPC handling"); + } +#else // GRPC_ALLOW_EXCEPTIONS + return handler(); +#endif // GRPC_ALLOW_EXCEPTIONS +} +/// A wrapper class of an application provided rpc method handler. template -using RpcMethodHandler = - ::grpc_impl::internal::RpcMethodHandler; +class RpcMethodHandler : public ::grpc::internal::MethodHandler { + public: + RpcMethodHandler( + std::function<::grpc::Status(ServiceType*, ::grpc::ServerContext*, + const RequestType*, ResponseType*)> + func, + ServiceType* service) + : func_(func), service_(service) {} + + void RunHandler(const HandlerParameter& param) final { + ResponseType rsp; + ::grpc::Status status = param.status; + if (status.ok()) { + status = CatchingFunctionHandler([this, ¶m, &rsp] { + return func_(service_, + static_cast<::grpc::ServerContext*>(param.server_context), + static_cast(param.request), &rsp); + }); + static_cast(param.request)->~RequestType(); + } + + GPR_CODEGEN_ASSERT(!param.server_context->sent_initial_metadata_); + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, + ::grpc::internal::CallOpSendMessage, + ::grpc::internal::CallOpServerSendStatus> + ops; + ops.SendInitialMetadata(¶m.server_context->initial_metadata_, + param.server_context->initial_metadata_flags()); + if (param.server_context->compression_level_set()) { + ops.set_compression_level(param.server_context->compression_level()); + } + if (status.ok()) { + status = ops.SendMessagePtr(&rsp); + } + ops.ServerSendStatus(¶m.server_context->trailing_metadata_, status); + param.call->PerformOps(&ops); + param.call->cq()->Pluck(&ops); + } + + void* Deserialize(grpc_call* call, grpc_byte_buffer* req, + ::grpc::Status* status, void** /*handler_data*/) final { + ::grpc::ByteBuffer buf; + buf.set_buffer(req); + auto* request = + new (::grpc::g_core_codegen_interface->grpc_call_arena_alloc( + call, sizeof(RequestType))) RequestType(); + *status = + ::grpc::SerializationTraits::Deserialize(&buf, request); + buf.Release(); + if (status->ok()) { + return request; + } + request->~RequestType(); + return nullptr; + } + + private: + /// Application provided rpc handler function. + std::function<::grpc::Status(ServiceType*, ::grpc::ServerContext*, + const RequestType*, ResponseType*)> + func_; + // The class the above handler function lives in. + ServiceType* service_; +}; +/// A wrapper class of an application provided client streaming handler. template -using ClientStreamingHandler = - ::grpc_impl::internal::ClientStreamingHandler; +class ClientStreamingHandler : public ::grpc::internal::MethodHandler { + public: + ClientStreamingHandler( + std::function<::grpc::Status(ServiceType*, ::grpc::ServerContext*, + ServerReader*, ResponseType*)> + func, + ServiceType* service) + : func_(func), service_(service) {} + void RunHandler(const HandlerParameter& param) final { + ServerReader reader( + param.call, static_cast<::grpc::ServerContext*>(param.server_context)); + ResponseType rsp; + ::grpc::Status status = CatchingFunctionHandler([this, ¶m, &reader, + &rsp] { + return func_(service_, + static_cast<::grpc::ServerContext*>(param.server_context), + &reader, &rsp); + }); + + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, + ::grpc::internal::CallOpSendMessage, + ::grpc::internal::CallOpServerSendStatus> + ops; + if (!param.server_context->sent_initial_metadata_) { + ops.SendInitialMetadata(¶m.server_context->initial_metadata_, + param.server_context->initial_metadata_flags()); + if (param.server_context->compression_level_set()) { + ops.set_compression_level(param.server_context->compression_level()); + } + } + if (status.ok()) { + status = ops.SendMessagePtr(&rsp); + } + ops.ServerSendStatus(¶m.server_context->trailing_metadata_, status); + param.call->PerformOps(&ops); + param.call->cq()->Pluck(&ops); + } + + private: + std::function<::grpc::Status(ServiceType*, ::grpc::ServerContext*, + ServerReader*, ResponseType*)> + func_; + ServiceType* service_; +}; + +/// A wrapper class of an application provided server streaming handler. template -using ServerStreamingHandler = - ::grpc_impl::internal::ServerStreamingHandler; +class ServerStreamingHandler : public ::grpc::internal::MethodHandler { + public: + ServerStreamingHandler(std::function<::grpc::Status( + ServiceType*, ::grpc::ServerContext*, + const RequestType*, ServerWriter*)> + func, + ServiceType* service) + : func_(func), service_(service) {} + + void RunHandler(const HandlerParameter& param) final { + ::grpc::Status status = param.status; + if (status.ok()) { + ServerWriter writer( + param.call, + static_cast<::grpc::ServerContext*>(param.server_context)); + status = CatchingFunctionHandler([this, ¶m, &writer] { + return func_(service_, + static_cast<::grpc::ServerContext*>(param.server_context), + static_cast(param.request), &writer); + }); + static_cast(param.request)->~RequestType(); + } + + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, + ::grpc::internal::CallOpServerSendStatus> + ops; + if (!param.server_context->sent_initial_metadata_) { + ops.SendInitialMetadata(¶m.server_context->initial_metadata_, + param.server_context->initial_metadata_flags()); + if (param.server_context->compression_level_set()) { + ops.set_compression_level(param.server_context->compression_level()); + } + } + ops.ServerSendStatus(¶m.server_context->trailing_metadata_, status); + param.call->PerformOps(&ops); + if (param.server_context->has_pending_ops_) { + param.call->cq()->Pluck(¶m.server_context->pending_ops_); + } + param.call->cq()->Pluck(&ops); + } + void* Deserialize(grpc_call* call, grpc_byte_buffer* req, + ::grpc::Status* status, void** /*handler_data*/) final { + ::grpc::ByteBuffer buf; + buf.set_buffer(req); + auto* request = + new (::grpc::g_core_codegen_interface->grpc_call_arena_alloc( + call, sizeof(RequestType))) RequestType(); + *status = + ::grpc::SerializationTraits::Deserialize(&buf, request); + buf.Release(); + if (status->ok()) { + return request; + } + request->~RequestType(); + return nullptr; + } + + private: + std::function<::grpc::Status(ServiceType*, ::grpc::ServerContext*, + const RequestType*, ServerWriter*)> + func_; + ServiceType* service_; +}; + +/// A wrapper class of an application provided bidi-streaming handler. +/// This also applies to server-streamed implementation of a unary method +/// with the additional requirement that such methods must have done a +/// write for status to be ok +/// Since this is used by more than 1 class, the service is not passed in. +/// Instead, it is expected to be an implicitly-captured argument of func +/// (through bind or something along those lines) template -using TemplatedBidiStreamingHandler = - ::grpc_impl::internal::TemplatedBidiStreamingHandler; +class TemplatedBidiStreamingHandler : public ::grpc::internal::MethodHandler { + public: + TemplatedBidiStreamingHandler( + std::function<::grpc::Status(::grpc::ServerContext*, Streamer*)> func) + : func_(func), write_needed_(WriteNeeded) {} + + void RunHandler(const HandlerParameter& param) final { + Streamer stream(param.call, + static_cast<::grpc::ServerContext*>(param.server_context)); + ::grpc::Status status = CatchingFunctionHandler([this, ¶m, &stream] { + return func_(static_cast<::grpc::ServerContext*>(param.server_context), + &stream); + }); + + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, + ::grpc::internal::CallOpServerSendStatus> + ops; + if (!param.server_context->sent_initial_metadata_) { + ops.SendInitialMetadata(¶m.server_context->initial_metadata_, + param.server_context->initial_metadata_flags()); + if (param.server_context->compression_level_set()) { + ops.set_compression_level(param.server_context->compression_level()); + } + if (write_needed_ && status.ok()) { + // If we needed a write but never did one, we need to mark the + // status as a fail + status = ::grpc::Status(::grpc::StatusCode::INTERNAL, + "Service did not provide response message"); + } + } + ops.ServerSendStatus(¶m.server_context->trailing_metadata_, status); + param.call->PerformOps(&ops); + if (param.server_context->has_pending_ops_) { + param.call->cq()->Pluck(¶m.server_context->pending_ops_); + } + param.call->cq()->Pluck(&ops); + } + + private: + std::function<::grpc::Status(::grpc::ServerContext*, Streamer*)> func_; + const bool write_needed_; +}; + +template +class BidiStreamingHandler + : public TemplatedBidiStreamingHandler< + ServerReaderWriter, false> { + public: + BidiStreamingHandler(std::function<::grpc::Status( + ServiceType*, ::grpc::ServerContext*, + ServerReaderWriter*)> + func, + ServiceType* service) + // TODO(vjpai): When gRPC supports C++14, move-capture func in the below + : TemplatedBidiStreamingHandler< + ServerReaderWriter, false>( + [func, service]( + ::grpc::ServerContext* ctx, + ServerReaderWriter* streamer) { + return func(service, ctx, streamer); + }) {} +}; template -using StreamedUnaryHandler = - ::grpc_impl::internal::StreamedUnaryHandler; +class StreamedUnaryHandler + : public TemplatedBidiStreamingHandler< + ServerUnaryStreamer, true> { + public: + explicit StreamedUnaryHandler( + std::function< + ::grpc::Status(::grpc::ServerContext*, + ServerUnaryStreamer*)> + func) + : TemplatedBidiStreamingHandler< + ServerUnaryStreamer, true>( + std::move(func)) {} +}; template -using SplitServerStreamingHandler = - ::grpc_impl::internal::SplitServerStreamingHandler; +class SplitServerStreamingHandler + : public TemplatedBidiStreamingHandler< + ServerSplitStreamer, false> { + public: + explicit SplitServerStreamingHandler( + std::function< + ::grpc::Status(::grpc::ServerContext*, + ServerSplitStreamer*)> + func) + : TemplatedBidiStreamingHandler< + ServerSplitStreamer, false>( + std::move(func)) {} +}; -template -using ErrorMethodHandler = ::grpc_impl::internal::ErrorMethodHandler; +/// General method handler class for errors that prevent real method use +/// e.g., handle unknown method by returning UNIMPLEMENTED error. +template <::grpc::StatusCode code> +class ErrorMethodHandler : public ::grpc::internal::MethodHandler { + public: + template + static void FillOps(::grpc::ServerContextBase* context, T* ops) { + ::grpc::Status status(code, ""); + if (!context->sent_initial_metadata_) { + ops->SendInitialMetadata(&context->initial_metadata_, + context->initial_metadata_flags()); + if (context->compression_level_set()) { + ops->set_compression_level(context->compression_level()); + } + context->sent_initial_metadata_ = true; + } + ops->ServerSendStatus(&context->trailing_metadata_, status); + } -using UnknownMethodHandler = ::grpc_impl::internal::UnknownMethodHandler; + void RunHandler(const HandlerParameter& param) final { + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, + ::grpc::internal::CallOpServerSendStatus> + ops; + FillOps(param.server_context, &ops); + param.call->PerformOps(&ops); + param.call->cq()->Pluck(&ops); + } -using ResourceExhaustedHandler = - ::grpc_impl::internal::ResourceExhaustedHandler; + void* Deserialize(grpc_call* /*call*/, grpc_byte_buffer* req, + ::grpc::Status* /*status*/, void** /*handler_data*/) final { + // We have to destroy any request payload + if (req != nullptr) { + ::grpc::g_core_codegen_interface->grpc_byte_buffer_destroy(req); + } + return nullptr; + } +}; -} // namespace internal +typedef ErrorMethodHandler<::grpc::StatusCode::UNIMPLEMENTED> + UnknownMethodHandler; +typedef ErrorMethodHandler<::grpc::StatusCode::RESOURCE_EXHAUSTED> + ResourceExhaustedHandler; +} // namespace internal } // namespace grpc #endif // GRPCPP_IMPL_CODEGEN_METHOD_HANDLER_H diff --git a/include/grpcpp/impl/codegen/method_handler_impl.h b/include/grpcpp/impl/codegen/method_handler_impl.h deleted file mode 100644 index 19f2eb22e13..00000000000 --- a/include/grpcpp/impl/codegen/method_handler_impl.h +++ /dev/null @@ -1,385 +0,0 @@ -/* - * - * Copyright 2015 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -#ifndef GRPCPP_IMPL_CODEGEN_METHOD_HANDLER_IMPL_H -#define GRPCPP_IMPL_CODEGEN_METHOD_HANDLER_IMPL_H - -#include -#include -#include -#include - -namespace grpc_impl { - -namespace internal { - -// Invoke the method handler, fill in the status, and -// return whether or not we finished safely (without an exception). -// Note that exception handling is 0-cost in most compiler/library -// implementations (except when an exception is actually thrown), -// so this process doesn't require additional overhead in the common case. -// Additionally, we don't need to return if we caught an exception or not; -// the handling is the same in either case. -template -::grpc::Status CatchingFunctionHandler(Callable&& handler) { -#if GRPC_ALLOW_EXCEPTIONS - try { - return handler(); - } catch (...) { - return ::grpc::Status(::grpc::StatusCode::UNKNOWN, - "Unexpected error in RPC handling"); - } -#else // GRPC_ALLOW_EXCEPTIONS - return handler(); -#endif // GRPC_ALLOW_EXCEPTIONS -} - -/// A wrapper class of an application provided rpc method handler. -template -class RpcMethodHandler : public ::grpc::internal::MethodHandler { - public: - RpcMethodHandler( - std::function<::grpc::Status(ServiceType*, ::grpc::ServerContext*, - const RequestType*, ResponseType*)> - func, - ServiceType* service) - : func_(func), service_(service) {} - - void RunHandler(const HandlerParameter& param) final { - ResponseType rsp; - ::grpc::Status status = param.status; - if (status.ok()) { - status = CatchingFunctionHandler([this, ¶m, &rsp] { - return func_(service_, - static_cast<::grpc::ServerContext*>(param.server_context), - static_cast(param.request), &rsp); - }); - static_cast(param.request)->~RequestType(); - } - - GPR_CODEGEN_ASSERT(!param.server_context->sent_initial_metadata_); - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, - ::grpc::internal::CallOpSendMessage, - ::grpc::internal::CallOpServerSendStatus> - ops; - ops.SendInitialMetadata(¶m.server_context->initial_metadata_, - param.server_context->initial_metadata_flags()); - if (param.server_context->compression_level_set()) { - ops.set_compression_level(param.server_context->compression_level()); - } - if (status.ok()) { - status = ops.SendMessagePtr(&rsp); - } - ops.ServerSendStatus(¶m.server_context->trailing_metadata_, status); - param.call->PerformOps(&ops); - param.call->cq()->Pluck(&ops); - } - - void* Deserialize(grpc_call* call, grpc_byte_buffer* req, - ::grpc::Status* status, void** /*handler_data*/) final { - ::grpc::ByteBuffer buf; - buf.set_buffer(req); - auto* request = - new (::grpc::g_core_codegen_interface->grpc_call_arena_alloc( - call, sizeof(RequestType))) RequestType(); - *status = - ::grpc::SerializationTraits::Deserialize(&buf, request); - buf.Release(); - if (status->ok()) { - return request; - } - request->~RequestType(); - return nullptr; - } - - private: - /// Application provided rpc handler function. - std::function<::grpc::Status(ServiceType*, ::grpc::ServerContext*, - const RequestType*, ResponseType*)> - func_; - // The class the above handler function lives in. - ServiceType* service_; -}; - -/// A wrapper class of an application provided client streaming handler. -template -class ClientStreamingHandler : public ::grpc::internal::MethodHandler { - public: - ClientStreamingHandler( - std::function<::grpc::Status(ServiceType*, ::grpc::ServerContext*, - ::grpc_impl::ServerReader*, - ResponseType*)> - func, - ServiceType* service) - : func_(func), service_(service) {} - - void RunHandler(const HandlerParameter& param) final { - ::grpc_impl::ServerReader reader( - param.call, static_cast<::grpc::ServerContext*>(param.server_context)); - ResponseType rsp; - ::grpc::Status status = CatchingFunctionHandler([this, ¶m, &reader, - &rsp] { - return func_(service_, - static_cast<::grpc::ServerContext*>(param.server_context), - &reader, &rsp); - }); - - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, - ::grpc::internal::CallOpSendMessage, - ::grpc::internal::CallOpServerSendStatus> - ops; - if (!param.server_context->sent_initial_metadata_) { - ops.SendInitialMetadata(¶m.server_context->initial_metadata_, - param.server_context->initial_metadata_flags()); - if (param.server_context->compression_level_set()) { - ops.set_compression_level(param.server_context->compression_level()); - } - } - if (status.ok()) { - status = ops.SendMessagePtr(&rsp); - } - ops.ServerSendStatus(¶m.server_context->trailing_metadata_, status); - param.call->PerformOps(&ops); - param.call->cq()->Pluck(&ops); - } - - private: - std::function<::grpc::Status(ServiceType*, ::grpc::ServerContext*, - ::grpc_impl::ServerReader*, - ResponseType*)> - func_; - ServiceType* service_; -}; - -/// A wrapper class of an application provided server streaming handler. -template -class ServerStreamingHandler : public ::grpc::internal::MethodHandler { - public: - ServerStreamingHandler( - std::function<::grpc::Status(ServiceType*, ::grpc::ServerContext*, - const RequestType*, - ::grpc_impl::ServerWriter*)> - func, - ServiceType* service) - : func_(func), service_(service) {} - - void RunHandler(const HandlerParameter& param) final { - ::grpc::Status status = param.status; - if (status.ok()) { - ::grpc_impl::ServerWriter writer( - param.call, - static_cast<::grpc::ServerContext*>(param.server_context)); - status = CatchingFunctionHandler([this, ¶m, &writer] { - return func_(service_, - static_cast<::grpc::ServerContext*>(param.server_context), - static_cast(param.request), &writer); - }); - static_cast(param.request)->~RequestType(); - } - - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, - ::grpc::internal::CallOpServerSendStatus> - ops; - if (!param.server_context->sent_initial_metadata_) { - ops.SendInitialMetadata(¶m.server_context->initial_metadata_, - param.server_context->initial_metadata_flags()); - if (param.server_context->compression_level_set()) { - ops.set_compression_level(param.server_context->compression_level()); - } - } - ops.ServerSendStatus(¶m.server_context->trailing_metadata_, status); - param.call->PerformOps(&ops); - if (param.server_context->has_pending_ops_) { - param.call->cq()->Pluck(¶m.server_context->pending_ops_); - } - param.call->cq()->Pluck(&ops); - } - - void* Deserialize(grpc_call* call, grpc_byte_buffer* req, - ::grpc::Status* status, void** /*handler_data*/) final { - ::grpc::ByteBuffer buf; - buf.set_buffer(req); - auto* request = - new (::grpc::g_core_codegen_interface->grpc_call_arena_alloc( - call, sizeof(RequestType))) RequestType(); - *status = - ::grpc::SerializationTraits::Deserialize(&buf, request); - buf.Release(); - if (status->ok()) { - return request; - } - request->~RequestType(); - return nullptr; - } - - private: - std::function<::grpc::Status(ServiceType*, ::grpc::ServerContext*, - const RequestType*, - ::grpc_impl::ServerWriter*)> - func_; - ServiceType* service_; -}; - -/// A wrapper class of an application provided bidi-streaming handler. -/// This also applies to server-streamed implementation of a unary method -/// with the additional requirement that such methods must have done a -/// write for status to be ok -/// Since this is used by more than 1 class, the service is not passed in. -/// Instead, it is expected to be an implicitly-captured argument of func -/// (through bind or something along those lines) -template -class TemplatedBidiStreamingHandler : public ::grpc::internal::MethodHandler { - public: - TemplatedBidiStreamingHandler( - std::function<::grpc::Status(::grpc::ServerContext*, Streamer*)> func) - : func_(func), write_needed_(WriteNeeded) {} - - void RunHandler(const HandlerParameter& param) final { - Streamer stream(param.call, - static_cast<::grpc::ServerContext*>(param.server_context)); - ::grpc::Status status = CatchingFunctionHandler([this, ¶m, &stream] { - return func_(static_cast<::grpc::ServerContext*>(param.server_context), - &stream); - }); - - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, - ::grpc::internal::CallOpServerSendStatus> - ops; - if (!param.server_context->sent_initial_metadata_) { - ops.SendInitialMetadata(¶m.server_context->initial_metadata_, - param.server_context->initial_metadata_flags()); - if (param.server_context->compression_level_set()) { - ops.set_compression_level(param.server_context->compression_level()); - } - if (write_needed_ && status.ok()) { - // If we needed a write but never did one, we need to mark the - // status as a fail - status = ::grpc::Status(::grpc::StatusCode::INTERNAL, - "Service did not provide response message"); - } - } - ops.ServerSendStatus(¶m.server_context->trailing_metadata_, status); - param.call->PerformOps(&ops); - if (param.server_context->has_pending_ops_) { - param.call->cq()->Pluck(¶m.server_context->pending_ops_); - } - param.call->cq()->Pluck(&ops); - } - - private: - std::function<::grpc::Status(::grpc::ServerContext*, Streamer*)> func_; - const bool write_needed_; -}; - -template -class BidiStreamingHandler - : public TemplatedBidiStreamingHandler< - ::grpc_impl::ServerReaderWriter, false> { - public: - BidiStreamingHandler( - std::function<::grpc::Status( - ServiceType*, ::grpc::ServerContext*, - ::grpc_impl::ServerReaderWriter*)> - func, - ServiceType* service) - // TODO(vjpai): When gRPC supports C++14, move-capture func in the below - : TemplatedBidiStreamingHandler< - ::grpc_impl::ServerReaderWriter, false>( - [func, service]( - ::grpc::ServerContext* ctx, - ::grpc_impl::ServerReaderWriter* - streamer) { return func(service, ctx, streamer); }) {} -}; - -template -class StreamedUnaryHandler - : public TemplatedBidiStreamingHandler< - ::grpc_impl::ServerUnaryStreamer, true> { - public: - explicit StreamedUnaryHandler( - std::function<::grpc::Status( - ::grpc::ServerContext*, - ::grpc_impl::ServerUnaryStreamer*)> - func) - : TemplatedBidiStreamingHandler< - ::grpc_impl::ServerUnaryStreamer, true>( - std::move(func)) {} -}; - -template -class SplitServerStreamingHandler - : public TemplatedBidiStreamingHandler< - ::grpc_impl::ServerSplitStreamer, false> { - public: - explicit SplitServerStreamingHandler( - std::function<::grpc::Status( - ::grpc::ServerContext*, - ::grpc_impl::ServerSplitStreamer*)> - func) - : TemplatedBidiStreamingHandler< - ::grpc_impl::ServerSplitStreamer, false>( - std::move(func)) {} -}; - -/// General method handler class for errors that prevent real method use -/// e.g., handle unknown method by returning UNIMPLEMENTED error. -template <::grpc::StatusCode code> -class ErrorMethodHandler : public ::grpc::internal::MethodHandler { - public: - template - static void FillOps(::grpc::ServerContextBase* context, T* ops) { - ::grpc::Status status(code, ""); - if (!context->sent_initial_metadata_) { - ops->SendInitialMetadata(&context->initial_metadata_, - context->initial_metadata_flags()); - if (context->compression_level_set()) { - ops->set_compression_level(context->compression_level()); - } - context->sent_initial_metadata_ = true; - } - ops->ServerSendStatus(&context->trailing_metadata_, status); - } - - void RunHandler(const HandlerParameter& param) final { - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, - ::grpc::internal::CallOpServerSendStatus> - ops; - FillOps(param.server_context, &ops); - param.call->PerformOps(&ops); - param.call->cq()->Pluck(&ops); - } - - void* Deserialize(grpc_call* /*call*/, grpc_byte_buffer* req, - ::grpc::Status* /*status*/, void** /*handler_data*/) final { - // We have to destroy any request payload - if (req != nullptr) { - ::grpc::g_core_codegen_interface->grpc_byte_buffer_destroy(req); - } - return nullptr; - } -}; - -typedef ErrorMethodHandler<::grpc::StatusCode::UNIMPLEMENTED> - UnknownMethodHandler; -typedef ErrorMethodHandler<::grpc::StatusCode::RESOURCE_EXHAUSTED> - ResourceExhaustedHandler; - -} // namespace internal -} // namespace grpc_impl - -#endif // GRPCPP_IMPL_CODEGEN_METHOD_HANDLER_IMPL_H diff --git a/include/grpcpp/impl/codegen/server_callback.h b/include/grpcpp/impl/codegen/server_callback.h index 1d8b2f6f5bb..50425a9093a 100644 --- a/include/grpcpp/impl/codegen/server_callback.h +++ b/include/grpcpp/impl/codegen/server_callback.h @@ -1,6 +1,6 @@ /* * - * Copyright 2018 gRPC authors. + * Copyright 2019 gRPC authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,44 +13,788 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ #ifndef GRPCPP_IMPL_CODEGEN_SERVER_CALLBACK_H #define GRPCPP_IMPL_CODEGEN_SERVER_CALLBACK_H -#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include namespace grpc { -#ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL +// Declare base class of all reactors as internal +namespace internal { + +// Forward declarations +template +class CallbackUnaryHandler; +template +class CallbackClientStreamingHandler; +template +class CallbackServerStreamingHandler; +template +class CallbackBidiHandler; + +class ServerReactor { + public: + virtual ~ServerReactor() = default; + virtual void OnDone() = 0; + virtual void OnCancel() = 0; + + // The following is not API. It is for internal use only and specifies whether + // all reactions of this Reactor can be run without an extra executor + // scheduling. This should only be used for internally-defined reactors with + // trivial reactions. + virtual bool InternalInlineable() { return false; } + + private: + template + friend class CallbackUnaryHandler; + template + friend class CallbackClientStreamingHandler; + template + friend class CallbackServerStreamingHandler; + template + friend class CallbackBidiHandler; +}; + +/// The base class of ServerCallbackUnary etc. +class ServerCallbackCall { + public: + virtual ~ServerCallbackCall() {} + + // This object is responsible for tracking when it is safe to call OnDone and + // OnCancel. OnDone should not be called until the method handler is complete, + // Finish has been called, the ServerContext CompletionOp (which tracks + // cancellation or successful completion) has completed, and all outstanding + // Read/Write actions have seen their reactions. OnCancel should not be called + // until after the method handler is done and the RPC has completed with a + // cancellation. This is tracked by counting how many of these conditions have + // been met and calling OnCancel when none remain unmet. + + // Public versions of MaybeDone: one where we don't know the reactor in + // advance (used for the ServerContext CompletionOp), and one for where we + // know the inlineability of the OnDone reaction. You should set the inline + // flag to true if either the Reactor is InternalInlineable() or if this + // callback is already being forced to run dispatched to an executor + // (typically because it contains additional work than just the MaybeDone). + + void MaybeDone() { + if (GPR_UNLIKELY(Unref() == 1)) { + ScheduleOnDone(reactor()->InternalInlineable()); + } + } + + void MaybeDone(bool inline_ondone) { + if (GPR_UNLIKELY(Unref() == 1)) { + ScheduleOnDone(inline_ondone); + } + } + + // Fast version called with known reactor passed in, used from derived + // classes, typically in non-cancel case + void MaybeCallOnCancel(ServerReactor* reactor) { + if (GPR_UNLIKELY(UnblockCancellation())) { + CallOnCancel(reactor); + } + } + + // Slower version called from object that doesn't know the reactor a priori + // (such as the ServerContext CompletionOp which is formed before the + // reactor). This is used in cancel cases only, so it's ok to be slower and + // invoke a virtual function. + void MaybeCallOnCancel() { + if (GPR_UNLIKELY(UnblockCancellation())) { + CallOnCancel(reactor()); + } + } + + protected: + /// Increases the reference count + void Ref() { callbacks_outstanding_.fetch_add(1, std::memory_order_relaxed); } + + private: + virtual ServerReactor* reactor() = 0; + + // CallOnDone performs the work required at completion of the RPC: invoking + // the OnDone function and doing all necessary cleanup. This function is only + // ever invoked on a fully-Unref'fed ServerCallbackCall. + virtual void CallOnDone() = 0; + + // If the OnDone reaction is inlineable, execute it inline. Otherwise send it + // to an executor. + void ScheduleOnDone(bool inline_ondone); + + // If the OnCancel reaction is inlineable, execute it inline. Otherwise send + // it to an executor. + void CallOnCancel(ServerReactor* reactor); + + // Implement the cancellation constraint counter. Return true if OnCancel + // should be called, false otherwise. + bool UnblockCancellation() { + return on_cancel_conditions_remaining_.fetch_sub( + 1, std::memory_order_acq_rel) == 1; + } + + /// Decreases the reference count and returns the previous value + int Unref() { + return callbacks_outstanding_.fetch_sub(1, std::memory_order_acq_rel); + } + + std::atomic_int on_cancel_conditions_remaining_{2}; + std::atomic_int callbacks_outstanding_{ + 3}; // reserve for start, Finish, and CompletionOp +}; + +template +class DefaultMessageHolder + : public ::grpc::experimental::MessageHolder { + public: + DefaultMessageHolder() { + this->set_request(&request_obj_); + this->set_response(&response_obj_); + } + void Release() override { + // the object is allocated in the call arena. + this->~DefaultMessageHolder(); + } + + private: + Request request_obj_; + Response response_obj_; +}; + +} // namespace internal + +// Forward declarations +class ServerUnaryReactor; +template +class ServerReadReactor; +template +class ServerWriteReactor; +template +class ServerBidiReactor; + +// NOTE: The actual call/stream object classes are provided as API only to +// support mocking. There are no implementations of these class interfaces in +// the API. +class ServerCallbackUnary : public internal::ServerCallbackCall { + public: + virtual ~ServerCallbackUnary() {} + virtual void Finish(::grpc::Status s) = 0; + virtual void SendInitialMetadata() = 0; + + protected: + // Use a template rather than explicitly specifying ServerUnaryReactor to + // delay binding and avoid a circular forward declaration issue + template + void BindReactor(Reactor* reactor) { + reactor->InternalBindCall(this); + } +}; + template -using ServerReadReactor = ::grpc_impl::ServerReadReactor; +class ServerCallbackReader : public internal::ServerCallbackCall { + public: + virtual ~ServerCallbackReader() {} + virtual void Finish(::grpc::Status s) = 0; + virtual void SendInitialMetadata() = 0; + virtual void Read(Request* msg) = 0; + + protected: + void BindReactor(ServerReadReactor* reactor) { + reactor->InternalBindReader(this); + } +}; template -using ServerWriteReactor = ::grpc_impl::ServerWriteReactor; +class ServerCallbackWriter : public internal::ServerCallbackCall { + public: + virtual ~ServerCallbackWriter() {} + + virtual void Finish(::grpc::Status s) = 0; + virtual void SendInitialMetadata() = 0; + virtual void Write(const Response* msg, ::grpc::WriteOptions options) = 0; + virtual void WriteAndFinish(const Response* msg, ::grpc::WriteOptions options, + ::grpc::Status s) = 0; + + protected: + void BindReactor(ServerWriteReactor* reactor) { + reactor->InternalBindWriter(this); + } +}; + +template +class ServerCallbackReaderWriter : public internal::ServerCallbackCall { + public: + virtual ~ServerCallbackReaderWriter() {} + + virtual void Finish(::grpc::Status s) = 0; + virtual void SendInitialMetadata() = 0; + virtual void Read(Request* msg) = 0; + virtual void Write(const Response* msg, ::grpc::WriteOptions options) = 0; + virtual void WriteAndFinish(const Response* msg, ::grpc::WriteOptions options, + ::grpc::Status s) = 0; + + protected: + void BindReactor(ServerBidiReactor* reactor) { + reactor->InternalBindStream(this); + } +}; + +// The following classes are the reactor interfaces that are to be implemented +// by the user, returned as the output parameter of the method handler for a +// callback method. Note that none of the classes are pure; all reactions have a +// default empty reaction so that the user class only needs to override those +// classes that it cares about. +/// \a ServerBidiReactor is the interface for a bidirectional streaming RPC. template -using ServerBidiReactor = ::grpc_impl::ServerBidiReactor; +class ServerBidiReactor : public internal::ServerReactor { + public: + // NOTE: Initializing stream_ as a constructor initializer rather than a + // default initializer because gcc-4.x requires a copy constructor for + // default initializing a templated member, which isn't ok for atomic. + // TODO(vjpai): Switch to default constructor and default initializer when + // gcc-4.x is no longer supported + ServerBidiReactor() : stream_(nullptr) {} + ~ServerBidiReactor() = default; -using ServerUnaryReactor = ::grpc_impl::ServerUnaryReactor; -#endif + /// Send any initial metadata stored in the RPC context. If not invoked, + /// any initial metadata will be passed along with the first Write or the + /// Finish (if there are no writes). + void StartSendInitialMetadata() { + ServerCallbackReaderWriter* stream = + stream_.load(std::memory_order_acquire); + if (stream == nullptr) { + grpc::internal::MutexLock l(&stream_mu_); + stream = stream_.load(std::memory_order_relaxed); + if (stream == nullptr) { + backlog_.send_initial_metadata_wanted = true; + return; + } + } + stream->SendInitialMetadata(); + } + + /// Initiate a read operation. + /// + /// \param[out] req Where to eventually store the read message. Valid when + /// the library calls OnReadDone + void StartRead(Request* req) { + ServerCallbackReaderWriter* stream = + stream_.load(std::memory_order_acquire); + if (stream == nullptr) { + grpc::internal::MutexLock l(&stream_mu_); + stream = stream_.load(std::memory_order_relaxed); + if (stream == nullptr) { + backlog_.read_wanted = req; + return; + } + } + stream->Read(req); + } + + /// Initiate a write operation. + /// + /// \param[in] resp The message to be written. The library does not take + /// ownership but the caller must ensure that the message is + /// not deleted or modified until OnWriteDone is called. + void StartWrite(const Response* resp) { + StartWrite(resp, ::grpc::WriteOptions()); + } + + /// Initiate a write operation with specified options. + /// + /// \param[in] resp The message to be written. The library does not take + /// ownership but the caller must ensure that the message is + /// not deleted or modified until OnWriteDone is called. + /// \param[in] options The WriteOptions to use for writing this message + void StartWrite(const Response* resp, ::grpc::WriteOptions options) { + ServerCallbackReaderWriter* stream = + stream_.load(std::memory_order_acquire); + if (stream == nullptr) { + grpc::internal::MutexLock l(&stream_mu_); + stream = stream_.load(std::memory_order_relaxed); + if (stream == nullptr) { + backlog_.write_wanted = resp; + backlog_.write_options_wanted = std::move(options); + return; + } + } + stream->Write(resp, std::move(options)); + } + + /// Initiate a write operation with specified options and final RPC Status, + /// which also causes any trailing metadata for this RPC to be sent out. + /// StartWriteAndFinish is like merging StartWriteLast and Finish into a + /// single step. A key difference, though, is that this operation doesn't have + /// an OnWriteDone reaction - it is considered complete only when OnDone is + /// available. An RPC can either have StartWriteAndFinish or Finish, but not + /// both. + /// + /// \param[in] resp The message to be written. The library does not take + /// ownership but the caller must ensure that the message is + /// not deleted or modified until OnDone is called. + /// \param[in] options The WriteOptions to use for writing this message + /// \param[in] s The status outcome of this RPC + void StartWriteAndFinish(const Response* resp, ::grpc::WriteOptions options, + ::grpc::Status s) { + ServerCallbackReaderWriter* stream = + stream_.load(std::memory_order_acquire); + if (stream == nullptr) { + grpc::internal::MutexLock l(&stream_mu_); + stream = stream_.load(std::memory_order_relaxed); + if (stream == nullptr) { + backlog_.write_and_finish_wanted = true; + backlog_.write_wanted = resp; + backlog_.write_options_wanted = std::move(options); + backlog_.status_wanted = std::move(s); + return; + } + } + stream->WriteAndFinish(resp, std::move(options), std::move(s)); + } + + /// Inform system of a planned write operation with specified options, but + /// allow the library to schedule the actual write coalesced with the writing + /// of trailing metadata (which takes place on a Finish call). + /// + /// \param[in] resp The message to be written. The library does not take + /// ownership but the caller must ensure that the message is + /// not deleted or modified until OnWriteDone is called. + /// \param[in] options The WriteOptions to use for writing this message + void StartWriteLast(const Response* resp, ::grpc::WriteOptions options) { + StartWrite(resp, std::move(options.set_last_message())); + } + + /// Indicate that the stream is to be finished and the trailing metadata and + /// RPC status are to be sent. Every RPC MUST be finished using either Finish + /// or StartWriteAndFinish (but not both), even if the RPC is already + /// cancelled. + /// + /// \param[in] s The status outcome of this RPC + void Finish(::grpc::Status s) { + ServerCallbackReaderWriter* stream = + stream_.load(std::memory_order_acquire); + if (stream == nullptr) { + grpc::internal::MutexLock l(&stream_mu_); + stream = stream_.load(std::memory_order_relaxed); + if (stream == nullptr) { + backlog_.finish_wanted = true; + backlog_.status_wanted = std::move(s); + return; + } + } + stream->Finish(std::move(s)); + } + + /// Notifies the application that an explicit StartSendInitialMetadata + /// operation completed. Not used when the sending of initial metadata + /// piggybacks onto the first write. + /// + /// \param[in] ok Was it successful? If false, no further write-side operation + /// will succeed. + virtual void OnSendInitialMetadataDone(bool /*ok*/) {} + + /// Notifies the application that a StartRead operation completed. + /// + /// \param[in] ok Was it successful? If false, no further read-side operation + /// will succeed. + virtual void OnReadDone(bool /*ok*/) {} + + /// Notifies the application that a StartWrite (or StartWriteLast) operation + /// completed. + /// + /// \param[in] ok Was it successful? If false, no further write-side operation + /// will succeed. + virtual void OnWriteDone(bool /*ok*/) {} + + /// Notifies the application that all operations associated with this RPC + /// have completed. This is an override (from the internal base class) but + /// still abstract, so derived classes MUST override it to be instantiated. + void OnDone() override = 0; + + /// Notifies the application that this RPC has been cancelled. This is an + /// override (from the internal base class) but not final, so derived classes + /// should override it if they want to take action. + void OnCancel() override {} + + private: + friend class ServerCallbackReaderWriter; + // May be overridden by internal implementation details. This is not a public + // customization point. + virtual void InternalBindStream( + ServerCallbackReaderWriter* stream) { + // TODO(vjpai): When stream_or_backlog_ becomes a variant (see below), use + // a scoped MutexLock and std::swap stream_or_backlog_ with a variant that + // has stream, then std::get out of that after the lock. + // Do likewise with the remaining InternalBind* functions as well. + grpc::internal::ReleasableMutexLock l(&stream_mu_); + PreBindBacklog ops(std::move(backlog_)); + stream_.store(stream, std::memory_order_release); + l.Unlock(); + + if (ops.send_initial_metadata_wanted) { + stream->SendInitialMetadata(); + } + if (ops.read_wanted != nullptr) { + stream->Read(ops.read_wanted); + } + if (ops.write_and_finish_wanted) { + stream->WriteAndFinish(ops.write_wanted, + std::move(ops.write_options_wanted), + std::move(ops.status_wanted)); + } else { + if (ops.write_wanted != nullptr) { + stream->Write(ops.write_wanted, std::move(ops.write_options_wanted)); + } + if (ops.finish_wanted) { + stream->Finish(std::move(ops.status_wanted)); + } + } + } + + grpc::internal::Mutex stream_mu_; + // TODO(vjpai): Make stream_or_backlog_ into a std::variant or absl::variant + // once C++17 or ABSL is supported since stream and backlog are + // mutually exclusive in this class. Do likewise with the + // remaining reactor classes and their backlogs as well. + std::atomic*> stream_{nullptr}; + struct PreBindBacklog { + bool send_initial_metadata_wanted = false; + bool write_and_finish_wanted = false; + bool finish_wanted = false; + Request* read_wanted = nullptr; + const Response* write_wanted = nullptr; + ::grpc::WriteOptions write_options_wanted; + ::grpc::Status status_wanted; + }; + PreBindBacklog backlog_ /* GUARDED_BY(stream_mu_) */; +}; + +/// \a ServerReadReactor is the interface for a client-streaming RPC. +template +class ServerReadReactor : public internal::ServerReactor { + public: + ServerReadReactor() : reader_(nullptr) {} + ~ServerReadReactor() = default; + + /// The following operation initiations are exactly like ServerBidiReactor. + void StartSendInitialMetadata() { + ServerCallbackReader* reader = + reader_.load(std::memory_order_acquire); + if (reader == nullptr) { + grpc::internal::MutexLock l(&reader_mu_); + reader = reader_.load(std::memory_order_relaxed); + if (reader == nullptr) { + backlog_.send_initial_metadata_wanted = true; + return; + } + } + reader->SendInitialMetadata(); + } + void StartRead(Request* req) { + ServerCallbackReader* reader = + reader_.load(std::memory_order_acquire); + if (reader == nullptr) { + grpc::internal::MutexLock l(&reader_mu_); + reader = reader_.load(std::memory_order_relaxed); + if (reader == nullptr) { + backlog_.read_wanted = req; + return; + } + } + reader->Read(req); + } + void Finish(::grpc::Status s) { + ServerCallbackReader* reader = + reader_.load(std::memory_order_acquire); + if (reader == nullptr) { + grpc::internal::MutexLock l(&reader_mu_); + reader = reader_.load(std::memory_order_relaxed); + if (reader == nullptr) { + backlog_.finish_wanted = true; + backlog_.status_wanted = std::move(s); + return; + } + } + reader->Finish(std::move(s)); + } + + /// The following notifications are exactly like ServerBidiReactor. + virtual void OnSendInitialMetadataDone(bool /*ok*/) {} + virtual void OnReadDone(bool /*ok*/) {} + void OnDone() override = 0; + void OnCancel() override {} + + private: + friend class ServerCallbackReader; + + // May be overridden by internal implementation details. This is not a public + // customization point. + virtual void InternalBindReader(ServerCallbackReader* reader) { + grpc::internal::ReleasableMutexLock l(&reader_mu_); + PreBindBacklog ops(std::move(backlog_)); + reader_.store(reader, std::memory_order_release); + l.Unlock(); + + if (ops.send_initial_metadata_wanted) { + reader->SendInitialMetadata(); + } + if (ops.read_wanted != nullptr) { + reader->Read(ops.read_wanted); + } + if (ops.finish_wanted) { + reader->Finish(std::move(ops.status_wanted)); + } + } + + grpc::internal::Mutex reader_mu_; + std::atomic*> reader_{nullptr}; + struct PreBindBacklog { + bool send_initial_metadata_wanted = false; + bool finish_wanted = false; + Request* read_wanted = nullptr; + ::grpc::Status status_wanted; + }; + PreBindBacklog backlog_ /* GUARDED_BY(reader_mu_) */; +}; + +/// \a ServerWriteReactor is the interface for a server-streaming RPC. +template +class ServerWriteReactor : public internal::ServerReactor { + public: + ServerWriteReactor() : writer_(nullptr) {} + ~ServerWriteReactor() = default; + + /// The following operation initiations are exactly like ServerBidiReactor. + void StartSendInitialMetadata() { + ServerCallbackWriter* writer = + writer_.load(std::memory_order_acquire); + if (writer == nullptr) { + grpc::internal::MutexLock l(&writer_mu_); + writer = writer_.load(std::memory_order_relaxed); + if (writer == nullptr) { + backlog_.send_initial_metadata_wanted = true; + return; + } + } + writer->SendInitialMetadata(); + } + void StartWrite(const Response* resp) { + StartWrite(resp, ::grpc::WriteOptions()); + } + void StartWrite(const Response* resp, ::grpc::WriteOptions options) { + ServerCallbackWriter* writer = + writer_.load(std::memory_order_acquire); + if (writer == nullptr) { + grpc::internal::MutexLock l(&writer_mu_); + writer = writer_.load(std::memory_order_relaxed); + if (writer == nullptr) { + backlog_.write_wanted = resp; + backlog_.write_options_wanted = std::move(options); + return; + } + } + writer->Write(resp, std::move(options)); + } + void StartWriteAndFinish(const Response* resp, ::grpc::WriteOptions options, + ::grpc::Status s) { + ServerCallbackWriter* writer = + writer_.load(std::memory_order_acquire); + if (writer == nullptr) { + grpc::internal::MutexLock l(&writer_mu_); + writer = writer_.load(std::memory_order_relaxed); + if (writer == nullptr) { + backlog_.write_and_finish_wanted = true; + backlog_.write_wanted = resp; + backlog_.write_options_wanted = std::move(options); + backlog_.status_wanted = std::move(s); + return; + } + } + writer->WriteAndFinish(resp, std::move(options), std::move(s)); + } + void StartWriteLast(const Response* resp, ::grpc::WriteOptions options) { + StartWrite(resp, std::move(options.set_last_message())); + } + void Finish(::grpc::Status s) { + ServerCallbackWriter* writer = + writer_.load(std::memory_order_acquire); + if (writer == nullptr) { + grpc::internal::MutexLock l(&writer_mu_); + writer = writer_.load(std::memory_order_relaxed); + if (writer == nullptr) { + backlog_.finish_wanted = true; + backlog_.status_wanted = std::move(s); + return; + } + } + writer->Finish(std::move(s)); + } + + /// The following notifications are exactly like ServerBidiReactor. + virtual void OnSendInitialMetadataDone(bool /*ok*/) {} + virtual void OnWriteDone(bool /*ok*/) {} + void OnDone() override = 0; + void OnCancel() override {} + + private: + friend class ServerCallbackWriter; + // May be overridden by internal implementation details. This is not a public + // customization point. + virtual void InternalBindWriter(ServerCallbackWriter* writer) { + grpc::internal::ReleasableMutexLock l(&writer_mu_); + PreBindBacklog ops(std::move(backlog_)); + writer_.store(writer, std::memory_order_release); + l.Unlock(); + + if (ops.send_initial_metadata_wanted) { + writer->SendInitialMetadata(); + } + if (ops.write_and_finish_wanted) { + writer->WriteAndFinish(ops.write_wanted, + std::move(ops.write_options_wanted), + std::move(ops.status_wanted)); + } else { + if (ops.write_wanted != nullptr) { + writer->Write(ops.write_wanted, std::move(ops.write_options_wanted)); + } + if (ops.finish_wanted) { + writer->Finish(std::move(ops.status_wanted)); + } + } + } + + grpc::internal::Mutex writer_mu_; + std::atomic*> writer_{nullptr}; + struct PreBindBacklog { + bool send_initial_metadata_wanted = false; + bool write_and_finish_wanted = false; + bool finish_wanted = false; + const Response* write_wanted = nullptr; + ::grpc::WriteOptions write_options_wanted; + ::grpc::Status status_wanted; + }; + PreBindBacklog backlog_ /* GUARDED_BY(writer_mu_) */; +}; + +class ServerUnaryReactor : public internal::ServerReactor { + public: + ServerUnaryReactor() : call_(nullptr) {} + ~ServerUnaryReactor() = default; + + /// StartSendInitialMetadata is exactly like ServerBidiReactor. + void StartSendInitialMetadata() { + ServerCallbackUnary* call = call_.load(std::memory_order_acquire); + if (call == nullptr) { + grpc::internal::MutexLock l(&call_mu_); + call = call_.load(std::memory_order_relaxed); + if (call == nullptr) { + backlog_.send_initial_metadata_wanted = true; + return; + } + } + call->SendInitialMetadata(); + } + /// Finish is similar to ServerBidiReactor except for one detail. + /// If the status is non-OK, any message will not be sent. Instead, + /// the client will only receive the status and any trailing metadata. + void Finish(::grpc::Status s) { + ServerCallbackUnary* call = call_.load(std::memory_order_acquire); + if (call == nullptr) { + grpc::internal::MutexLock l(&call_mu_); + call = call_.load(std::memory_order_relaxed); + if (call == nullptr) { + backlog_.finish_wanted = true; + backlog_.status_wanted = std::move(s); + return; + } + } + call->Finish(std::move(s)); + } + + /// The following notifications are exactly like ServerBidiReactor. + virtual void OnSendInitialMetadataDone(bool /*ok*/) {} + void OnDone() override = 0; + void OnCancel() override {} + + private: + friend class ServerCallbackUnary; + // May be overridden by internal implementation details. This is not a public + // customization point. + virtual void InternalBindCall(ServerCallbackUnary* call) { + grpc::internal::ReleasableMutexLock l(&call_mu_); + PreBindBacklog ops(std::move(backlog_)); + call_.store(call, std::memory_order_release); + l.Unlock(); + + if (ops.send_initial_metadata_wanted) { + call->SendInitialMetadata(); + } + if (ops.finish_wanted) { + call->Finish(std::move(ops.status_wanted)); + } + } + + grpc::internal::Mutex call_mu_; + std::atomic call_{nullptr}; + struct PreBindBacklog { + bool send_initial_metadata_wanted = false; + bool finish_wanted = false; + ::grpc::Status status_wanted; + }; + PreBindBacklog backlog_ /* GUARDED_BY(call_mu_) */; +}; + +namespace internal { + +template +class FinishOnlyReactor : public Base { + public: + explicit FinishOnlyReactor(::grpc::Status s) { this->Finish(std::move(s)); } + void OnDone() override { this->~FinishOnlyReactor(); } +}; + +using UnimplementedUnaryReactor = FinishOnlyReactor; +template +using UnimplementedReadReactor = FinishOnlyReactor>; +template +using UnimplementedWriteReactor = + FinishOnlyReactor>; +template +using UnimplementedBidiReactor = + FinishOnlyReactor>; + +} // namespace internal // TODO(vjpai): Remove namespace experimental when de-experimentalized fully. namespace experimental { template -using ServerReadReactor = ::grpc_impl::ServerReadReactor; +using ServerReadReactor = ::grpc::ServerReadReactor; template -using ServerWriteReactor = ::grpc_impl::ServerWriteReactor; +using ServerWriteReactor = ::grpc::ServerWriteReactor; template -using ServerBidiReactor = ::grpc_impl::ServerBidiReactor; +using ServerBidiReactor = ::grpc::ServerBidiReactor; -using ServerUnaryReactor = ::grpc_impl::ServerUnaryReactor; +using ServerUnaryReactor = ::grpc::ServerUnaryReactor; } // namespace experimental + } // namespace grpc #endif // GRPCPP_IMPL_CODEGEN_SERVER_CALLBACK_H diff --git a/include/grpcpp/impl/codegen/server_callback_handlers.h b/include/grpcpp/impl/codegen/server_callback_handlers.h index bd85e079f1b..8120fcaf851 100644 --- a/include/grpcpp/impl/codegen/server_callback_handlers.h +++ b/include/grpcpp/impl/codegen/server_callback_handlers.h @@ -20,11 +20,11 @@ #include #include -#include +#include #include #include -namespace grpc_impl { +namespace grpc { namespace internal { template @@ -862,6 +862,6 @@ class CallbackBidiHandler : public ::grpc::internal::MethodHandler { }; } // namespace internal -} // namespace grpc_impl +} // namespace grpc #endif // GRPCPP_IMPL_CODEGEN_SERVER_CALLBACK_HANDLERS_H diff --git a/include/grpcpp/impl/codegen/server_callback_impl.h b/include/grpcpp/impl/codegen/server_callback_impl.h deleted file mode 100644 index f19ab42716f..00000000000 --- a/include/grpcpp/impl/codegen/server_callback_impl.h +++ /dev/null @@ -1,783 +0,0 @@ -/* - * - * Copyright 2019 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef GRPCPP_IMPL_CODEGEN_SERVER_CALLBACK_IMPL_H -#define GRPCPP_IMPL_CODEGEN_SERVER_CALLBACK_IMPL_H - -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -namespace grpc_impl { - -// Declare base class of all reactors as internal -namespace internal { - -// Forward declarations -template -class CallbackUnaryHandler; -template -class CallbackClientStreamingHandler; -template -class CallbackServerStreamingHandler; -template -class CallbackBidiHandler; - -class ServerReactor { - public: - virtual ~ServerReactor() = default; - virtual void OnDone() = 0; - virtual void OnCancel() = 0; - - // The following is not API. It is for internal use only and specifies whether - // all reactions of this Reactor can be run without an extra executor - // scheduling. This should only be used for internally-defined reactors with - // trivial reactions. - virtual bool InternalInlineable() { return false; } - - private: - template - friend class CallbackUnaryHandler; - template - friend class CallbackClientStreamingHandler; - template - friend class CallbackServerStreamingHandler; - template - friend class CallbackBidiHandler; -}; - -/// The base class of ServerCallbackUnary etc. -class ServerCallbackCall { - public: - virtual ~ServerCallbackCall() {} - - // This object is responsible for tracking when it is safe to call OnDone and - // OnCancel. OnDone should not be called until the method handler is complete, - // Finish has been called, the ServerContext CompletionOp (which tracks - // cancellation or successful completion) has completed, and all outstanding - // Read/Write actions have seen their reactions. OnCancel should not be called - // until after the method handler is done and the RPC has completed with a - // cancellation. This is tracked by counting how many of these conditions have - // been met and calling OnCancel when none remain unmet. - - // Public versions of MaybeDone: one where we don't know the reactor in - // advance (used for the ServerContext CompletionOp), and one for where we - // know the inlineability of the OnDone reaction. You should set the inline - // flag to true if either the Reactor is InternalInlineable() or if this - // callback is already being forced to run dispatched to an executor - // (typically because it contains additional work than just the MaybeDone). - - void MaybeDone() { - if (GPR_UNLIKELY(Unref() == 1)) { - ScheduleOnDone(reactor()->InternalInlineable()); - } - } - - void MaybeDone(bool inline_ondone) { - if (GPR_UNLIKELY(Unref() == 1)) { - ScheduleOnDone(inline_ondone); - } - } - - // Fast version called with known reactor passed in, used from derived - // classes, typically in non-cancel case - void MaybeCallOnCancel(ServerReactor* reactor) { - if (GPR_UNLIKELY(UnblockCancellation())) { - CallOnCancel(reactor); - } - } - - // Slower version called from object that doesn't know the reactor a priori - // (such as the ServerContext CompletionOp which is formed before the - // reactor). This is used in cancel cases only, so it's ok to be slower and - // invoke a virtual function. - void MaybeCallOnCancel() { - if (GPR_UNLIKELY(UnblockCancellation())) { - CallOnCancel(reactor()); - } - } - - protected: - /// Increases the reference count - void Ref() { callbacks_outstanding_.fetch_add(1, std::memory_order_relaxed); } - - private: - virtual ServerReactor* reactor() = 0; - - // CallOnDone performs the work required at completion of the RPC: invoking - // the OnDone function and doing all necessary cleanup. This function is only - // ever invoked on a fully-Unref'fed ServerCallbackCall. - virtual void CallOnDone() = 0; - - // If the OnDone reaction is inlineable, execute it inline. Otherwise send it - // to an executor. - void ScheduleOnDone(bool inline_ondone); - - // If the OnCancel reaction is inlineable, execute it inline. Otherwise send - // it to an executor. - void CallOnCancel(ServerReactor* reactor); - - // Implement the cancellation constraint counter. Return true if OnCancel - // should be called, false otherwise. - bool UnblockCancellation() { - return on_cancel_conditions_remaining_.fetch_sub( - 1, std::memory_order_acq_rel) == 1; - } - - /// Decreases the reference count and returns the previous value - int Unref() { - return callbacks_outstanding_.fetch_sub(1, std::memory_order_acq_rel); - } - - std::atomic_int on_cancel_conditions_remaining_{2}; - std::atomic_int callbacks_outstanding_{ - 3}; // reserve for start, Finish, and CompletionOp -}; - -template -class DefaultMessageHolder - : public ::grpc::experimental::MessageHolder { - public: - DefaultMessageHolder() { - this->set_request(&request_obj_); - this->set_response(&response_obj_); - } - void Release() override { - // the object is allocated in the call arena. - this->~DefaultMessageHolder(); - } - - private: - Request request_obj_; - Response response_obj_; -}; - -} // namespace internal - -// Forward declarations -class ServerUnaryReactor; -template -class ServerReadReactor; -template -class ServerWriteReactor; -template -class ServerBidiReactor; - -// NOTE: The actual call/stream object classes are provided as API only to -// support mocking. There are no implementations of these class interfaces in -// the API. -class ServerCallbackUnary : public internal::ServerCallbackCall { - public: - virtual ~ServerCallbackUnary() {} - virtual void Finish(::grpc::Status s) = 0; - virtual void SendInitialMetadata() = 0; - - protected: - // Use a template rather than explicitly specifying ServerUnaryReactor to - // delay binding and avoid a circular forward declaration issue - template - void BindReactor(Reactor* reactor) { - reactor->InternalBindCall(this); - } -}; - -template -class ServerCallbackReader : public internal::ServerCallbackCall { - public: - virtual ~ServerCallbackReader() {} - virtual void Finish(::grpc::Status s) = 0; - virtual void SendInitialMetadata() = 0; - virtual void Read(Request* msg) = 0; - - protected: - void BindReactor(ServerReadReactor* reactor) { - reactor->InternalBindReader(this); - } -}; - -template -class ServerCallbackWriter : public internal::ServerCallbackCall { - public: - virtual ~ServerCallbackWriter() {} - - virtual void Finish(::grpc::Status s) = 0; - virtual void SendInitialMetadata() = 0; - virtual void Write(const Response* msg, ::grpc::WriteOptions options) = 0; - virtual void WriteAndFinish(const Response* msg, ::grpc::WriteOptions options, - ::grpc::Status s) = 0; - - protected: - void BindReactor(ServerWriteReactor* reactor) { - reactor->InternalBindWriter(this); - } -}; - -template -class ServerCallbackReaderWriter : public internal::ServerCallbackCall { - public: - virtual ~ServerCallbackReaderWriter() {} - - virtual void Finish(::grpc::Status s) = 0; - virtual void SendInitialMetadata() = 0; - virtual void Read(Request* msg) = 0; - virtual void Write(const Response* msg, ::grpc::WriteOptions options) = 0; - virtual void WriteAndFinish(const Response* msg, ::grpc::WriteOptions options, - ::grpc::Status s) = 0; - - protected: - void BindReactor(ServerBidiReactor* reactor) { - reactor->InternalBindStream(this); - } -}; - -// The following classes are the reactor interfaces that are to be implemented -// by the user, returned as the output parameter of the method handler for a -// callback method. Note that none of the classes are pure; all reactions have a -// default empty reaction so that the user class only needs to override those -// classes that it cares about. - -/// \a ServerBidiReactor is the interface for a bidirectional streaming RPC. -template -class ServerBidiReactor : public internal::ServerReactor { - public: - // NOTE: Initializing stream_ as a constructor initializer rather than a - // default initializer because gcc-4.x requires a copy constructor for - // default initializing a templated member, which isn't ok for atomic. - // TODO(vjpai): Switch to default constructor and default initializer when - // gcc-4.x is no longer supported - ServerBidiReactor() : stream_(nullptr) {} - ~ServerBidiReactor() = default; - - /// Send any initial metadata stored in the RPC context. If not invoked, - /// any initial metadata will be passed along with the first Write or the - /// Finish (if there are no writes). - void StartSendInitialMetadata() { - ServerCallbackReaderWriter* stream = - stream_.load(std::memory_order_acquire); - if (stream == nullptr) { - grpc::internal::MutexLock l(&stream_mu_); - stream = stream_.load(std::memory_order_relaxed); - if (stream == nullptr) { - backlog_.send_initial_metadata_wanted = true; - return; - } - } - stream->SendInitialMetadata(); - } - - /// Initiate a read operation. - /// - /// \param[out] req Where to eventually store the read message. Valid when - /// the library calls OnReadDone - void StartRead(Request* req) { - ServerCallbackReaderWriter* stream = - stream_.load(std::memory_order_acquire); - if (stream == nullptr) { - grpc::internal::MutexLock l(&stream_mu_); - stream = stream_.load(std::memory_order_relaxed); - if (stream == nullptr) { - backlog_.read_wanted = req; - return; - } - } - stream->Read(req); - } - - /// Initiate a write operation. - /// - /// \param[in] resp The message to be written. The library does not take - /// ownership but the caller must ensure that the message is - /// not deleted or modified until OnWriteDone is called. - void StartWrite(const Response* resp) { - StartWrite(resp, ::grpc::WriteOptions()); - } - - /// Initiate a write operation with specified options. - /// - /// \param[in] resp The message to be written. The library does not take - /// ownership but the caller must ensure that the message is - /// not deleted or modified until OnWriteDone is called. - /// \param[in] options The WriteOptions to use for writing this message - void StartWrite(const Response* resp, ::grpc::WriteOptions options) { - ServerCallbackReaderWriter* stream = - stream_.load(std::memory_order_acquire); - if (stream == nullptr) { - grpc::internal::MutexLock l(&stream_mu_); - stream = stream_.load(std::memory_order_relaxed); - if (stream == nullptr) { - backlog_.write_wanted = resp; - backlog_.write_options_wanted = std::move(options); - return; - } - } - stream->Write(resp, std::move(options)); - } - - /// Initiate a write operation with specified options and final RPC Status, - /// which also causes any trailing metadata for this RPC to be sent out. - /// StartWriteAndFinish is like merging StartWriteLast and Finish into a - /// single step. A key difference, though, is that this operation doesn't have - /// an OnWriteDone reaction - it is considered complete only when OnDone is - /// available. An RPC can either have StartWriteAndFinish or Finish, but not - /// both. - /// - /// \param[in] resp The message to be written. The library does not take - /// ownership but the caller must ensure that the message is - /// not deleted or modified until OnDone is called. - /// \param[in] options The WriteOptions to use for writing this message - /// \param[in] s The status outcome of this RPC - void StartWriteAndFinish(const Response* resp, ::grpc::WriteOptions options, - ::grpc::Status s) { - ServerCallbackReaderWriter* stream = - stream_.load(std::memory_order_acquire); - if (stream == nullptr) { - grpc::internal::MutexLock l(&stream_mu_); - stream = stream_.load(std::memory_order_relaxed); - if (stream == nullptr) { - backlog_.write_and_finish_wanted = true; - backlog_.write_wanted = resp; - backlog_.write_options_wanted = std::move(options); - backlog_.status_wanted = std::move(s); - return; - } - } - stream->WriteAndFinish(resp, std::move(options), std::move(s)); - } - - /// Inform system of a planned write operation with specified options, but - /// allow the library to schedule the actual write coalesced with the writing - /// of trailing metadata (which takes place on a Finish call). - /// - /// \param[in] resp The message to be written. The library does not take - /// ownership but the caller must ensure that the message is - /// not deleted or modified until OnWriteDone is called. - /// \param[in] options The WriteOptions to use for writing this message - void StartWriteLast(const Response* resp, ::grpc::WriteOptions options) { - StartWrite(resp, std::move(options.set_last_message())); - } - - /// Indicate that the stream is to be finished and the trailing metadata and - /// RPC status are to be sent. Every RPC MUST be finished using either Finish - /// or StartWriteAndFinish (but not both), even if the RPC is already - /// cancelled. - /// - /// \param[in] s The status outcome of this RPC - void Finish(::grpc::Status s) { - ServerCallbackReaderWriter* stream = - stream_.load(std::memory_order_acquire); - if (stream == nullptr) { - grpc::internal::MutexLock l(&stream_mu_); - stream = stream_.load(std::memory_order_relaxed); - if (stream == nullptr) { - backlog_.finish_wanted = true; - backlog_.status_wanted = std::move(s); - return; - } - } - stream->Finish(std::move(s)); - } - - /// Notifies the application that an explicit StartSendInitialMetadata - /// operation completed. Not used when the sending of initial metadata - /// piggybacks onto the first write. - /// - /// \param[in] ok Was it successful? If false, no further write-side operation - /// will succeed. - virtual void OnSendInitialMetadataDone(bool /*ok*/) {} - - /// Notifies the application that a StartRead operation completed. - /// - /// \param[in] ok Was it successful? If false, no further read-side operation - /// will succeed. - virtual void OnReadDone(bool /*ok*/) {} - - /// Notifies the application that a StartWrite (or StartWriteLast) operation - /// completed. - /// - /// \param[in] ok Was it successful? If false, no further write-side operation - /// will succeed. - virtual void OnWriteDone(bool /*ok*/) {} - - /// Notifies the application that all operations associated with this RPC - /// have completed. This is an override (from the internal base class) but - /// still abstract, so derived classes MUST override it to be instantiated. - void OnDone() override = 0; - - /// Notifies the application that this RPC has been cancelled. This is an - /// override (from the internal base class) but not final, so derived classes - /// should override it if they want to take action. - void OnCancel() override {} - - private: - friend class ServerCallbackReaderWriter; - // May be overridden by internal implementation details. This is not a public - // customization point. - virtual void InternalBindStream( - ServerCallbackReaderWriter* stream) { - // TODO(vjpai): When stream_or_backlog_ becomes a variant (see below), use - // a scoped MutexLock and std::swap stream_or_backlog_ with a variant that - // has stream, then std::get out of that after the lock. - // Do likewise with the remaining InternalBind* functions as well. - grpc::internal::ReleasableMutexLock l(&stream_mu_); - PreBindBacklog ops(std::move(backlog_)); - stream_.store(stream, std::memory_order_release); - l.Unlock(); - - if (ops.send_initial_metadata_wanted) { - stream->SendInitialMetadata(); - } - if (ops.read_wanted != nullptr) { - stream->Read(ops.read_wanted); - } - if (ops.write_and_finish_wanted) { - stream->WriteAndFinish(ops.write_wanted, - std::move(ops.write_options_wanted), - std::move(ops.status_wanted)); - } else { - if (ops.write_wanted != nullptr) { - stream->Write(ops.write_wanted, std::move(ops.write_options_wanted)); - } - if (ops.finish_wanted) { - stream->Finish(std::move(ops.status_wanted)); - } - } - } - - grpc::internal::Mutex stream_mu_; - // TODO(vjpai): Make stream_or_backlog_ into a std::variant or absl::variant - // once C++17 or ABSL is supported since stream and backlog are - // mutually exclusive in this class. Do likewise with the - // remaining reactor classes and their backlogs as well. - std::atomic*> stream_{nullptr}; - struct PreBindBacklog { - bool send_initial_metadata_wanted = false; - bool write_and_finish_wanted = false; - bool finish_wanted = false; - Request* read_wanted = nullptr; - const Response* write_wanted = nullptr; - ::grpc::WriteOptions write_options_wanted; - ::grpc::Status status_wanted; - }; - PreBindBacklog backlog_ /* GUARDED_BY(stream_mu_) */; -}; - -/// \a ServerReadReactor is the interface for a client-streaming RPC. -template -class ServerReadReactor : public internal::ServerReactor { - public: - ServerReadReactor() : reader_(nullptr) {} - ~ServerReadReactor() = default; - - /// The following operation initiations are exactly like ServerBidiReactor. - void StartSendInitialMetadata() { - ServerCallbackReader* reader = - reader_.load(std::memory_order_acquire); - if (reader == nullptr) { - grpc::internal::MutexLock l(&reader_mu_); - reader = reader_.load(std::memory_order_relaxed); - if (reader == nullptr) { - backlog_.send_initial_metadata_wanted = true; - return; - } - } - reader->SendInitialMetadata(); - } - void StartRead(Request* req) { - ServerCallbackReader* reader = - reader_.load(std::memory_order_acquire); - if (reader == nullptr) { - grpc::internal::MutexLock l(&reader_mu_); - reader = reader_.load(std::memory_order_relaxed); - if (reader == nullptr) { - backlog_.read_wanted = req; - return; - } - } - reader->Read(req); - } - void Finish(::grpc::Status s) { - ServerCallbackReader* reader = - reader_.load(std::memory_order_acquire); - if (reader == nullptr) { - grpc::internal::MutexLock l(&reader_mu_); - reader = reader_.load(std::memory_order_relaxed); - if (reader == nullptr) { - backlog_.finish_wanted = true; - backlog_.status_wanted = std::move(s); - return; - } - } - reader->Finish(std::move(s)); - } - - /// The following notifications are exactly like ServerBidiReactor. - virtual void OnSendInitialMetadataDone(bool /*ok*/) {} - virtual void OnReadDone(bool /*ok*/) {} - void OnDone() override = 0; - void OnCancel() override {} - - private: - friend class ServerCallbackReader; - - // May be overridden by internal implementation details. This is not a public - // customization point. - virtual void InternalBindReader(ServerCallbackReader* reader) { - grpc::internal::ReleasableMutexLock l(&reader_mu_); - PreBindBacklog ops(std::move(backlog_)); - reader_.store(reader, std::memory_order_release); - l.Unlock(); - - if (ops.send_initial_metadata_wanted) { - reader->SendInitialMetadata(); - } - if (ops.read_wanted != nullptr) { - reader->Read(ops.read_wanted); - } - if (ops.finish_wanted) { - reader->Finish(std::move(ops.status_wanted)); - } - } - - grpc::internal::Mutex reader_mu_; - std::atomic*> reader_{nullptr}; - struct PreBindBacklog { - bool send_initial_metadata_wanted = false; - bool finish_wanted = false; - Request* read_wanted = nullptr; - ::grpc::Status status_wanted; - }; - PreBindBacklog backlog_ /* GUARDED_BY(reader_mu_) */; -}; - -/// \a ServerWriteReactor is the interface for a server-streaming RPC. -template -class ServerWriteReactor : public internal::ServerReactor { - public: - ServerWriteReactor() : writer_(nullptr) {} - ~ServerWriteReactor() = default; - - /// The following operation initiations are exactly like ServerBidiReactor. - void StartSendInitialMetadata() { - ServerCallbackWriter* writer = - writer_.load(std::memory_order_acquire); - if (writer == nullptr) { - grpc::internal::MutexLock l(&writer_mu_); - writer = writer_.load(std::memory_order_relaxed); - if (writer == nullptr) { - backlog_.send_initial_metadata_wanted = true; - return; - } - } - writer->SendInitialMetadata(); - } - void StartWrite(const Response* resp) { - StartWrite(resp, ::grpc::WriteOptions()); - } - void StartWrite(const Response* resp, ::grpc::WriteOptions options) { - ServerCallbackWriter* writer = - writer_.load(std::memory_order_acquire); - if (writer == nullptr) { - grpc::internal::MutexLock l(&writer_mu_); - writer = writer_.load(std::memory_order_relaxed); - if (writer == nullptr) { - backlog_.write_wanted = resp; - backlog_.write_options_wanted = std::move(options); - return; - } - } - writer->Write(resp, std::move(options)); - } - void StartWriteAndFinish(const Response* resp, ::grpc::WriteOptions options, - ::grpc::Status s) { - ServerCallbackWriter* writer = - writer_.load(std::memory_order_acquire); - if (writer == nullptr) { - grpc::internal::MutexLock l(&writer_mu_); - writer = writer_.load(std::memory_order_relaxed); - if (writer == nullptr) { - backlog_.write_and_finish_wanted = true; - backlog_.write_wanted = resp; - backlog_.write_options_wanted = std::move(options); - backlog_.status_wanted = std::move(s); - return; - } - } - writer->WriteAndFinish(resp, std::move(options), std::move(s)); - } - void StartWriteLast(const Response* resp, ::grpc::WriteOptions options) { - StartWrite(resp, std::move(options.set_last_message())); - } - void Finish(::grpc::Status s) { - ServerCallbackWriter* writer = - writer_.load(std::memory_order_acquire); - if (writer == nullptr) { - grpc::internal::MutexLock l(&writer_mu_); - writer = writer_.load(std::memory_order_relaxed); - if (writer == nullptr) { - backlog_.finish_wanted = true; - backlog_.status_wanted = std::move(s); - return; - } - } - writer->Finish(std::move(s)); - } - - /// The following notifications are exactly like ServerBidiReactor. - virtual void OnSendInitialMetadataDone(bool /*ok*/) {} - virtual void OnWriteDone(bool /*ok*/) {} - void OnDone() override = 0; - void OnCancel() override {} - - private: - friend class ServerCallbackWriter; - // May be overridden by internal implementation details. This is not a public - // customization point. - virtual void InternalBindWriter(ServerCallbackWriter* writer) { - grpc::internal::ReleasableMutexLock l(&writer_mu_); - PreBindBacklog ops(std::move(backlog_)); - writer_.store(writer, std::memory_order_release); - l.Unlock(); - - if (ops.send_initial_metadata_wanted) { - writer->SendInitialMetadata(); - } - if (ops.write_and_finish_wanted) { - writer->WriteAndFinish(ops.write_wanted, - std::move(ops.write_options_wanted), - std::move(ops.status_wanted)); - } else { - if (ops.write_wanted != nullptr) { - writer->Write(ops.write_wanted, std::move(ops.write_options_wanted)); - } - if (ops.finish_wanted) { - writer->Finish(std::move(ops.status_wanted)); - } - } - } - - grpc::internal::Mutex writer_mu_; - std::atomic*> writer_{nullptr}; - struct PreBindBacklog { - bool send_initial_metadata_wanted = false; - bool write_and_finish_wanted = false; - bool finish_wanted = false; - const Response* write_wanted = nullptr; - ::grpc::WriteOptions write_options_wanted; - ::grpc::Status status_wanted; - }; - PreBindBacklog backlog_ /* GUARDED_BY(writer_mu_) */; -}; - -class ServerUnaryReactor : public internal::ServerReactor { - public: - ServerUnaryReactor() : call_(nullptr) {} - ~ServerUnaryReactor() = default; - - /// StartSendInitialMetadata is exactly like ServerBidiReactor. - void StartSendInitialMetadata() { - ServerCallbackUnary* call = call_.load(std::memory_order_acquire); - if (call == nullptr) { - grpc::internal::MutexLock l(&call_mu_); - call = call_.load(std::memory_order_relaxed); - if (call == nullptr) { - backlog_.send_initial_metadata_wanted = true; - return; - } - } - call->SendInitialMetadata(); - } - /// Finish is similar to ServerBidiReactor except for one detail. - /// If the status is non-OK, any message will not be sent. Instead, - /// the client will only receive the status and any trailing metadata. - void Finish(::grpc::Status s) { - ServerCallbackUnary* call = call_.load(std::memory_order_acquire); - if (call == nullptr) { - grpc::internal::MutexLock l(&call_mu_); - call = call_.load(std::memory_order_relaxed); - if (call == nullptr) { - backlog_.finish_wanted = true; - backlog_.status_wanted = std::move(s); - return; - } - } - call->Finish(std::move(s)); - } - - /// The following notifications are exactly like ServerBidiReactor. - virtual void OnSendInitialMetadataDone(bool /*ok*/) {} - void OnDone() override = 0; - void OnCancel() override {} - - private: - friend class ServerCallbackUnary; - // May be overridden by internal implementation details. This is not a public - // customization point. - virtual void InternalBindCall(ServerCallbackUnary* call) { - grpc::internal::ReleasableMutexLock l(&call_mu_); - PreBindBacklog ops(std::move(backlog_)); - call_.store(call, std::memory_order_release); - l.Unlock(); - - if (ops.send_initial_metadata_wanted) { - call->SendInitialMetadata(); - } - if (ops.finish_wanted) { - call->Finish(std::move(ops.status_wanted)); - } - } - - grpc::internal::Mutex call_mu_; - std::atomic call_{nullptr}; - struct PreBindBacklog { - bool send_initial_metadata_wanted = false; - bool finish_wanted = false; - ::grpc::Status status_wanted; - }; - PreBindBacklog backlog_ /* GUARDED_BY(call_mu_) */; -}; - -namespace internal { - -template -class FinishOnlyReactor : public Base { - public: - explicit FinishOnlyReactor(::grpc::Status s) { this->Finish(std::move(s)); } - void OnDone() override { this->~FinishOnlyReactor(); } -}; - -using UnimplementedUnaryReactor = FinishOnlyReactor; -template -using UnimplementedReadReactor = FinishOnlyReactor>; -template -using UnimplementedWriteReactor = - FinishOnlyReactor>; -template -using UnimplementedBidiReactor = - FinishOnlyReactor>; - -} // namespace internal -} // namespace grpc_impl - -#endif // GRPCPP_IMPL_CODEGEN_SERVER_CALLBACK_IMPL_H diff --git a/include/grpcpp/impl/codegen/server_context.h b/include/grpcpp/impl/codegen/server_context.h index 001e57dc536..ecedaead636 100644 --- a/include/grpcpp/impl/codegen/server_context.h +++ b/include/grpcpp/impl/codegen/server_context.h @@ -38,7 +38,7 @@ #include #include #include -#include +#include #include #include #include @@ -48,7 +48,7 @@ struct grpc_metadata; struct grpc_call; struct census_context; -namespace grpc_impl { +namespace grpc { template class ServerAsyncReader; template @@ -90,8 +90,6 @@ template <::grpc::StatusCode code> class ErrorMethodHandler; } // namespace internal -} // namespace grpc_impl -namespace grpc { class ClientContext; class CompletionQueue; class GenericServerContext; @@ -316,7 +314,7 @@ class ServerContextBase { /// from the method handler. /// /// WARNING: This is experimental API and could be changed or removed. - ::grpc_impl::ServerUnaryReactor* DefaultReactor() { + ::grpc::ServerUnaryReactor* DefaultReactor() { // Short-circuit the case where a default reactor was already set up by // the TestPeer. if (test_unary_ != nullptr) { @@ -344,39 +342,39 @@ class ServerContextBase { friend class ::grpc::ServerInterface; friend class ::grpc::Server; template - friend class ::grpc_impl::ServerAsyncReader; + friend class ::grpc::ServerAsyncReader; template - friend class ::grpc_impl::ServerAsyncWriter; + friend class ::grpc::ServerAsyncWriter; template - friend class ::grpc_impl::ServerAsyncResponseWriter; + friend class ::grpc::ServerAsyncResponseWriter; template - friend class ::grpc_impl::ServerAsyncReaderWriter; + friend class ::grpc::ServerAsyncReaderWriter; template - friend class ::grpc_impl::ServerReader; + friend class ::grpc::ServerReader; template - friend class ::grpc_impl::ServerWriter; + friend class ::grpc::ServerWriter; template - friend class ::grpc_impl::internal::ServerReaderWriterBody; + friend class ::grpc::internal::ServerReaderWriterBody; template - friend class ::grpc_impl::internal::RpcMethodHandler; + friend class ::grpc::internal::RpcMethodHandler; template - friend class ::grpc_impl::internal::ClientStreamingHandler; + friend class ::grpc::internal::ClientStreamingHandler; template - friend class ::grpc_impl::internal::ServerStreamingHandler; + friend class ::grpc::internal::ServerStreamingHandler; template - friend class ::grpc_impl::internal::TemplatedBidiStreamingHandler; + friend class ::grpc::internal::TemplatedBidiStreamingHandler; template - friend class ::grpc_impl::internal::CallbackUnaryHandler; + friend class ::grpc::internal::CallbackUnaryHandler; template - friend class ::grpc_impl::internal::CallbackClientStreamingHandler; + friend class ::grpc::internal::CallbackClientStreamingHandler; template - friend class ::grpc_impl::internal::CallbackServerStreamingHandler; + friend class ::grpc::internal::CallbackServerStreamingHandler; template - friend class ::grpc_impl::internal::CallbackBidiHandler; + friend class ::grpc::internal::CallbackBidiHandler; template <::grpc::StatusCode code> - friend class ::grpc_impl::internal::ErrorMethodHandler; + friend class ::grpc::internal::ErrorMethodHandler; template - friend class ::grpc_impl::internal::FinishOnlyReactor; + friend class ::grpc::internal::FinishOnlyReactor; friend class ::grpc::ClientContext; friend class ::grpc::GenericServerContext; #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL @@ -393,7 +391,7 @@ class ServerContextBase { void BeginCompletionOp( ::grpc::internal::Call* call, std::function callback, - ::grpc_impl::internal::ServerCallbackCall* callback_controller); + ::grpc::internal::ServerCallbackCall* callback_controller); /// Return the tag queued by BeginCompletionOp() ::grpc::internal::CompletionQueueTag* GetCompletionOpTag(); @@ -449,7 +447,7 @@ class ServerContextBase { ::grpc::experimental::ServerRpcInfo* rpc_info_; ::grpc::experimental::RpcAllocatorState* message_allocator_state_ = nullptr; - class Reactor : public ::grpc_impl::ServerUnaryReactor { + class Reactor : public ::grpc::ServerUnaryReactor { public: void OnCancel() override {} void OnDone() override {} @@ -468,7 +466,7 @@ class ServerContextBase { } ::grpc::Status test_status() const { return test_unary_->status(); } - class TestServerCallbackUnary : public ::grpc_impl::ServerCallbackUnary { + class TestServerCallbackUnary : public ::grpc::ServerCallbackUnary { public: TestServerCallbackUnary(ServerContextBase* ctx, std::function func) @@ -489,11 +487,9 @@ class ServerContextBase { private: void CallOnDone() override {} - ::grpc_impl::internal::ServerReactor* reactor() override { - return reactor_; - } + ::grpc::internal::ServerReactor* reactor() override { return reactor_; } - ::grpc_impl::ServerUnaryReactor* const reactor_; + ::grpc::ServerUnaryReactor* const reactor_; std::atomic_bool status_set_{false}; ::grpc::Status status_; const std::function func_; diff --git a/include/grpcpp/impl/codegen/sync_stream.h b/include/grpcpp/impl/codegen/sync_stream.h index 852fe6676bf..408f42f280d 100644 --- a/include/grpcpp/impl/codegen/sync_stream.h +++ b/include/grpcpp/impl/codegen/sync_stream.h @@ -1,6 +1,6 @@ /* * - * Copyright 2015 gRPC authors. + * Copyright 2019 gRPC authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,87 +13,930 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * */ #ifndef GRPCPP_IMPL_CODEGEN_SYNC_STREAM_H #define GRPCPP_IMPL_CODEGEN_SYNC_STREAM_H -#include +#include +#include +#include +#include +#include +#include +#include +#include namespace grpc { namespace internal { +/// Common interface for all synchronous client side streaming. +class ClientStreamingInterface { + public: + virtual ~ClientStreamingInterface() {} + + /// Block waiting until the stream finishes and a final status of the call is + /// available. + /// + /// It is appropriate to call this method exactly once when both: + /// * the calling code (client-side) has no more message to send + /// (this can be declared implicitly by calling this method, or + /// explicitly through an earlier call to WritesDone method of the + /// class in use, e.g. \a ClientWriterInterface::WritesDone or + /// \a ClientReaderWriterInterface::WritesDone). + /// * there are no more messages to be received from the server (which can + /// be known implicitly, or explicitly from an earlier call to \a + /// ReaderInterface::Read that returned "false"). + /// + /// This function will return either: + /// - when all incoming messages have been read and the server has + /// returned status. + /// - when the server has returned a non-OK status. + /// - OR when the call failed for some reason and the library generated a + /// status. + /// + /// Return values: + /// - \a Status contains the status code, message and details for the call + /// - the \a ClientContext associated with this call is updated with + /// possible trailing metadata sent from the server. + virtual ::grpc::Status Finish() = 0; +}; -typedef ::grpc_impl::internal::ClientStreamingInterface - ClientStreamingInterface; +/// Common interface for all synchronous server side streaming. +class ServerStreamingInterface { + public: + virtual ~ServerStreamingInterface() {} -typedef ::grpc_impl::internal::ServerStreamingInterface - ServerStreamingInterface; + /// Block to send initial metadata to client. + /// This call is optional, but if it is used, it cannot be used concurrently + /// with or after the \a Finish method. + /// + /// The initial metadata that will be sent to the client will be + /// taken from the \a ServerContext associated with the call. + virtual void SendInitialMetadata() = 0; +}; +/// An interface that yields a sequence of messages of type \a R. template -using ReaderInterface = ::grpc_impl::internal::ReaderInterface; +class ReaderInterface { + public: + virtual ~ReaderInterface() {} -template -using WriterInterface = ::grpc_impl::internal::WriterInterface; + /// Get an upper bound on the next message size available for reading on this + /// stream. + virtual bool NextMessageSize(uint32_t* sz) = 0; -template -using ClientReaderFactory = ::grpc_impl::internal::ClientReaderFactory; + /// Block to read a message and parse to \a msg. Returns \a true on success. + /// This is thread-safe with respect to \a Write or \WritesDone methods on + /// the same stream. It should not be called concurrently with another \a + /// Read on the same stream as the order of delivery will not be defined. + /// + /// \param[out] msg The read message. + /// + /// \return \a false when there will be no more incoming messages, either + /// because the other side has called \a WritesDone() or the stream has failed + /// (or been cancelled). + virtual bool Read(R* msg) = 0; +}; +/// An interface that can be fed a sequence of messages of type \a W. template -using ClientWriterFactory = ::grpc_impl::internal::ClientWriterFactory; +class WriterInterface { + public: + virtual ~WriterInterface() {} -template -using ClientReaderWriterFactory = - ::grpc_impl::internal::ClientReaderWriterFactory; + /// Block to write \a msg to the stream with WriteOptions \a options. + /// This is thread-safe with respect to \a ReaderInterface::Read + /// + /// \param msg The message to be written to the stream. + /// \param options The WriteOptions affecting the write operation. + /// + /// \return \a true on success, \a false when the stream has been closed. + virtual bool Write(const W& msg, ::grpc::WriteOptions options) = 0; + + /// Block to write \a msg to the stream with default write options. + /// This is thread-safe with respect to \a ReaderInterface::Read + /// + /// \param msg The message to be written to the stream. + /// + /// \return \a true on success, \a false when the stream has been closed. + inline bool Write(const W& msg) { return Write(msg, ::grpc::WriteOptions()); } + + /// Write \a msg and coalesce it with the writing of trailing metadata, using + /// WriteOptions \a options. + /// + /// For client, WriteLast is equivalent of performing Write and WritesDone in + /// a single step. \a msg and trailing metadata are coalesced and sent on wire + /// by calling this function. For server, WriteLast buffers the \a msg. + /// The writing of \a msg is held until the service handler returns, + /// where \a msg and trailing metadata are coalesced and sent on wire. + /// Note that WriteLast can only buffer \a msg up to the flow control window + /// size. If \a msg size is larger than the window size, it will be sent on + /// wire without buffering. + /// + /// \param[in] msg The message to be written to the stream. + /// \param[in] options The WriteOptions to be used to write this message. + void WriteLast(const W& msg, ::grpc::WriteOptions options) { + Write(msg, options.set_last_message()); + } +}; } // namespace internal +/// Client-side interface for streaming reads of message of type \a R. template -using ClientReaderInterface = ::grpc_impl::ClientReaderInterface; +class ClientReaderInterface : public internal::ClientStreamingInterface, + public internal::ReaderInterface { + public: + /// Block to wait for initial metadata from server. The received metadata + /// can only be accessed after this call returns. Should only be called before + /// the first read. Calling this method is optional, and if it is not called + /// the metadata will be available in ClientContext after the first read. + virtual void WaitForInitialMetadata() = 0; +}; + +namespace internal { +template +class ClientReaderFactory { + public: + template + static ClientReader* Create(::grpc::ChannelInterface* channel, + const ::grpc::internal::RpcMethod& method, + ::grpc::ClientContext* context, + const W& request) { + return new ClientReader(channel, method, context, request); + } +}; +} // namespace internal +/// Synchronous (blocking) client-side API for doing server-streaming RPCs, +/// where the stream of messages coming from the server has messages +/// of type \a R. template -using ClientReader = ::grpc_impl::ClientReader; +class ClientReader final : public ClientReaderInterface { + public: + /// See the \a ClientStreamingInterface.WaitForInitialMetadata method for + /// semantics. + /// + // Side effect: + /// Once complete, the initial metadata read from + /// the server will be accessible through the \a ClientContext used to + /// construct this object. + void WaitForInitialMetadata() override { + GPR_CODEGEN_ASSERT(!context_->initial_metadata_received_); + ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata> + ops; + ops.RecvInitialMetadata(context_); + call_.PerformOps(&ops); + cq_.Pluck(&ops); /// status ignored + } + + bool NextMessageSize(uint32_t* sz) override { + int result = call_.max_receive_message_size(); + *sz = (result > 0) ? result : UINT32_MAX; + return true; + } + + /// See the \a ReaderInterface.Read method for semantics. + /// Side effect: + /// This also receives initial metadata from the server, if not + /// already received (if initial metadata is received, it can be then + /// accessed through the \a ClientContext associated with this call). + bool Read(R* msg) override { + ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata, + ::grpc::internal::CallOpRecvMessage> + ops; + if (!context_->initial_metadata_received_) { + ops.RecvInitialMetadata(context_); + } + ops.RecvMessage(msg); + call_.PerformOps(&ops); + return cq_.Pluck(&ops) && ops.got_message; + } + + /// See the \a ClientStreamingInterface.Finish method for semantics. + /// + /// Side effect: + /// The \a ClientContext associated with this call is updated with + /// possible metadata received from the server. + ::grpc::Status Finish() override { + ::grpc::internal::CallOpSet<::grpc::internal::CallOpClientRecvStatus> ops; + ::grpc::Status status; + ops.ClientRecvStatus(context_, &status); + call_.PerformOps(&ops); + GPR_CODEGEN_ASSERT(cq_.Pluck(&ops)); + return status; + } + + private: + friend class internal::ClientReaderFactory; + ::grpc::ClientContext* context_; + ::grpc::CompletionQueue cq_; + ::grpc::internal::Call call_; + + /// Block to create a stream and write the initial metadata and \a request + /// out. Note that \a context will be used to fill in custom initial + /// metadata used to send to the server when starting the call. + template + ClientReader(::grpc::ChannelInterface* channel, + const ::grpc::internal::RpcMethod& method, + ::grpc::ClientContext* context, const W& request) + : context_(context), + cq_(grpc_completion_queue_attributes{ + GRPC_CQ_CURRENT_VERSION, GRPC_CQ_PLUCK, GRPC_CQ_DEFAULT_POLLING, + nullptr}), // Pluckable cq + call_(channel->CreateCall(method, context, &cq_)) { + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, + ::grpc::internal::CallOpSendMessage, + ::grpc::internal::CallOpClientSendClose> + ops; + ops.SendInitialMetadata(&context->send_initial_metadata_, + context->initial_metadata_flags()); + // TODO(ctiller): don't assert + GPR_CODEGEN_ASSERT(ops.SendMessagePtr(&request).ok()); + ops.ClientSendClose(); + call_.PerformOps(&ops); + cq_.Pluck(&ops); + } +}; + +/// Client-side interface for streaming writes of message type \a W. template -using ClientWriterInterface = ::grpc_impl::ClientWriterInterface; +class ClientWriterInterface : public internal::ClientStreamingInterface, + public internal::WriterInterface { + public: + /// Half close writing from the client. (signal that the stream of messages + /// coming from the client is complete). + /// Blocks until currently-pending writes are completed. + /// Thread safe with respect to \a ReaderInterface::Read operations only + /// + /// \return Whether the writes were successful. + virtual bool WritesDone() = 0; +}; +namespace internal { template -using ClientWriter = ::grpc_impl::ClientWriter; +class ClientWriterFactory { + public: + template + static ClientWriter* Create(::grpc::ChannelInterface* channel, + const ::grpc::internal::RpcMethod& method, + ::grpc::ClientContext* context, R* response) { + return new ClientWriter(channel, method, context, response); + } +}; +} // namespace internal + +/// Synchronous (blocking) client-side API for doing client-streaming RPCs, +/// where the outgoing message stream coming from the client has messages of +/// type \a W. +template +class ClientWriter : public ClientWriterInterface { + public: + /// See the \a ClientStreamingInterface.WaitForInitialMetadata method for + /// semantics. + /// + // Side effect: + /// Once complete, the initial metadata read from the server will be + /// accessible through the \a ClientContext used to construct this object. + void WaitForInitialMetadata() { + GPR_CODEGEN_ASSERT(!context_->initial_metadata_received_); + + ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata> + ops; + ops.RecvInitialMetadata(context_); + call_.PerformOps(&ops); + cq_.Pluck(&ops); // status ignored + } + /// See the WriterInterface.Write(const W& msg, WriteOptions options) method + /// for semantics. + /// + /// Side effect: + /// Also sends initial metadata if not already sent (using the + /// \a ClientContext associated with this call). + using internal::WriterInterface::Write; + bool Write(const W& msg, ::grpc::WriteOptions options) override { + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, + ::grpc::internal::CallOpSendMessage, + ::grpc::internal::CallOpClientSendClose> + ops; + + if (options.is_last_message()) { + options.set_buffer_hint(); + ops.ClientSendClose(); + } + if (context_->initial_metadata_corked_) { + ops.SendInitialMetadata(&context_->send_initial_metadata_, + context_->initial_metadata_flags()); + context_->set_initial_metadata_corked(false); + } + if (!ops.SendMessagePtr(&msg, options).ok()) { + return false; + } + + call_.PerformOps(&ops); + return cq_.Pluck(&ops); + } + + bool WritesDone() override { + ::grpc::internal::CallOpSet<::grpc::internal::CallOpClientSendClose> ops; + ops.ClientSendClose(); + call_.PerformOps(&ops); + return cq_.Pluck(&ops); + } + + /// See the ClientStreamingInterface.Finish method for semantics. + /// Side effects: + /// - Also receives initial metadata if not already received. + /// - Attempts to fill in the \a response parameter passed + /// to the constructor of this instance with the response + /// message from the server. + ::grpc::Status Finish() override { + ::grpc::Status status; + if (!context_->initial_metadata_received_) { + finish_ops_.RecvInitialMetadata(context_); + } + finish_ops_.ClientRecvStatus(context_, &status); + call_.PerformOps(&finish_ops_); + GPR_CODEGEN_ASSERT(cq_.Pluck(&finish_ops_)); + return status; + } + + private: + friend class internal::ClientWriterFactory; + + /// Block to create a stream (i.e. send request headers and other initial + /// metadata to the server). Note that \a context will be used to fill + /// in custom initial metadata. \a response will be filled in with the + /// single expected response message from the server upon a successful + /// call to the \a Finish method of this instance. + template + ClientWriter(::grpc::ChannelInterface* channel, + const ::grpc::internal::RpcMethod& method, + ::grpc::ClientContext* context, R* response) + : context_(context), + cq_(grpc_completion_queue_attributes{ + GRPC_CQ_CURRENT_VERSION, GRPC_CQ_PLUCK, GRPC_CQ_DEFAULT_POLLING, + nullptr}), // Pluckable cq + call_(channel->CreateCall(method, context, &cq_)) { + finish_ops_.RecvMessage(response); + finish_ops_.AllowNoMessage(); + + if (!context_->initial_metadata_corked_) { + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata> + ops; + ops.SendInitialMetadata(&context->send_initial_metadata_, + context->initial_metadata_flags()); + call_.PerformOps(&ops); + cq_.Pluck(&ops); + } + } + + ::grpc::ClientContext* context_; + ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata, + ::grpc::internal::CallOpGenericRecvMessage, + ::grpc::internal::CallOpClientRecvStatus> + finish_ops_; + ::grpc::CompletionQueue cq_; + ::grpc::internal::Call call_; +}; + +/// Client-side interface for bi-directional streaming with +/// client-to-server stream messages of type \a W and +/// server-to-client stream messages of type \a R. template -using ClientReaderWriterInterface = - ::grpc_impl::ClientReaderWriterInterface; +class ClientReaderWriterInterface : public internal::ClientStreamingInterface, + public internal::WriterInterface, + public internal::ReaderInterface { + public: + /// Block to wait for initial metadata from server. The received metadata + /// can only be accessed after this call returns. Should only be called before + /// the first read. Calling this method is optional, and if it is not called + /// the metadata will be available in ClientContext after the first read. + virtual void WaitForInitialMetadata() = 0; + /// Half close writing from the client. (signal that the stream of messages + /// coming from the client is complete). + /// Blocks until currently-pending writes are completed. + /// Thread-safe with respect to \a ReaderInterface::Read + /// + /// \return Whether the writes were successful. + virtual bool WritesDone() = 0; +}; + +namespace internal { template -using ClientReaderWriter = ::grpc_impl::ClientReaderWriter; +class ClientReaderWriterFactory { + public: + static ClientReaderWriter* Create( + ::grpc::ChannelInterface* channel, + const ::grpc::internal::RpcMethod& method, + ::grpc::ClientContext* context) { + return new ClientReaderWriter(channel, method, context); + } +}; +} // namespace internal + +/// Synchronous (blocking) client-side API for bi-directional streaming RPCs, +/// where the outgoing message stream coming from the client has messages of +/// type \a W, and the incoming messages stream coming from the server has +/// messages of type \a R. +template +class ClientReaderWriter final : public ClientReaderWriterInterface { + public: + /// Block waiting to read initial metadata from the server. + /// This call is optional, but if it is used, it cannot be used concurrently + /// with or after the \a Finish method. + /// + /// Once complete, the initial metadata read from the server will be + /// accessible through the \a ClientContext used to construct this object. + void WaitForInitialMetadata() override { + GPR_CODEGEN_ASSERT(!context_->initial_metadata_received_); + + ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata> + ops; + ops.RecvInitialMetadata(context_); + call_.PerformOps(&ops); + cq_.Pluck(&ops); // status ignored + } + + bool NextMessageSize(uint32_t* sz) override { + int result = call_.max_receive_message_size(); + *sz = (result > 0) ? result : UINT32_MAX; + return true; + } + + /// See the \a ReaderInterface.Read method for semantics. + /// Side effect: + /// Also receives initial metadata if not already received (updates the \a + /// ClientContext associated with this call in that case). + bool Read(R* msg) override { + ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata, + ::grpc::internal::CallOpRecvMessage> + ops; + if (!context_->initial_metadata_received_) { + ops.RecvInitialMetadata(context_); + } + ops.RecvMessage(msg); + call_.PerformOps(&ops); + return cq_.Pluck(&ops) && ops.got_message; + } + + /// See the \a WriterInterface.Write method for semantics. + /// + /// Side effect: + /// Also sends initial metadata if not already sent (using the + /// \a ClientContext associated with this call to fill in values). + using internal::WriterInterface::Write; + bool Write(const W& msg, ::grpc::WriteOptions options) override { + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, + ::grpc::internal::CallOpSendMessage, + ::grpc::internal::CallOpClientSendClose> + ops; + + if (options.is_last_message()) { + options.set_buffer_hint(); + ops.ClientSendClose(); + } + if (context_->initial_metadata_corked_) { + ops.SendInitialMetadata(&context_->send_initial_metadata_, + context_->initial_metadata_flags()); + context_->set_initial_metadata_corked(false); + } + if (!ops.SendMessagePtr(&msg, options).ok()) { + return false; + } + + call_.PerformOps(&ops); + return cq_.Pluck(&ops); + } + + bool WritesDone() override { + ::grpc::internal::CallOpSet<::grpc::internal::CallOpClientSendClose> ops; + ops.ClientSendClose(); + call_.PerformOps(&ops); + return cq_.Pluck(&ops); + } + /// See the ClientStreamingInterface.Finish method for semantics. + /// + /// Side effect: + /// - the \a ClientContext associated with this call is updated with + /// possible trailing metadata sent from the server. + ::grpc::Status Finish() override { + ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata, + ::grpc::internal::CallOpClientRecvStatus> + ops; + if (!context_->initial_metadata_received_) { + ops.RecvInitialMetadata(context_); + } + ::grpc::Status status; + ops.ClientRecvStatus(context_, &status); + call_.PerformOps(&ops); + GPR_CODEGEN_ASSERT(cq_.Pluck(&ops)); + return status; + } + + private: + friend class internal::ClientReaderWriterFactory; + + ::grpc::ClientContext* context_; + ::grpc::CompletionQueue cq_; + ::grpc::internal::Call call_; + + /// Block to create a stream and write the initial metadata and \a request + /// out. Note that \a context will be used to fill in custom initial metadata + /// used to send to the server when starting the call. + ClientReaderWriter(::grpc::ChannelInterface* channel, + const ::grpc::internal::RpcMethod& method, + ::grpc::ClientContext* context) + : context_(context), + cq_(grpc_completion_queue_attributes{ + GRPC_CQ_CURRENT_VERSION, GRPC_CQ_PLUCK, GRPC_CQ_DEFAULT_POLLING, + nullptr}), // Pluckable cq + call_(channel->CreateCall(method, context, &cq_)) { + if (!context_->initial_metadata_corked_) { + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata> + ops; + ops.SendInitialMetadata(&context->send_initial_metadata_, + context->initial_metadata_flags()); + call_.PerformOps(&ops); + cq_.Pluck(&ops); + } + } +}; + +/// Server-side interface for streaming reads of message of type \a R. template -using ServerReaderInterface = ::grpc_impl::ServerReaderInterface; +class ServerReaderInterface : public internal::ServerStreamingInterface, + public internal::ReaderInterface {}; +/// Synchronous (blocking) server-side API for doing client-streaming RPCs, +/// where the incoming message stream coming from the client has messages of +/// type \a R. template -using ServerReader = ::grpc_impl::ServerReader; +class ServerReader final : public ServerReaderInterface { + public: + /// See the \a ServerStreamingInterface.SendInitialMetadata method + /// for semantics. Note that initial metadata will be affected by the + /// \a ServerContext associated with this call. + void SendInitialMetadata() override { + GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_); + + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata> + ops; + ops.SendInitialMetadata(&ctx_->initial_metadata_, + ctx_->initial_metadata_flags()); + if (ctx_->compression_level_set()) { + ops.set_compression_level(ctx_->compression_level()); + } + ctx_->sent_initial_metadata_ = true; + call_->PerformOps(&ops); + call_->cq()->Pluck(&ops); + } + + bool NextMessageSize(uint32_t* sz) override { + int result = call_->max_receive_message_size(); + *sz = (result > 0) ? result : UINT32_MAX; + return true; + } + bool Read(R* msg) override { + ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvMessage> ops; + ops.RecvMessage(msg); + call_->PerformOps(&ops); + return call_->cq()->Pluck(&ops) && ops.got_message; + } + + private: + ::grpc::internal::Call* const call_; + ServerContext* const ctx_; + + template + friend class internal::ClientStreamingHandler; + + ServerReader(::grpc::internal::Call* call, ::grpc::ServerContext* ctx) + : call_(call), ctx_(ctx) {} +}; + +/// Server-side interface for streaming writes of message of type \a W. template -using ServerWriterInterface = ::grpc_impl::ServerWriterInterface; +class ServerWriterInterface : public internal::ServerStreamingInterface, + public internal::WriterInterface {}; +/// Synchronous (blocking) server-side API for doing for doing a +/// server-streaming RPCs, where the outgoing message stream coming from the +/// server has messages of type \a W. template -using ServerWriter = ::grpc_impl::ServerWriter; +class ServerWriter final : public ServerWriterInterface { + public: + /// See the \a ServerStreamingInterface.SendInitialMetadata method + /// for semantics. + /// Note that initial metadata will be affected by the + /// \a ServerContext associated with this call. + void SendInitialMetadata() override { + GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_); + + ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata> + ops; + ops.SendInitialMetadata(&ctx_->initial_metadata_, + ctx_->initial_metadata_flags()); + if (ctx_->compression_level_set()) { + ops.set_compression_level(ctx_->compression_level()); + } + ctx_->sent_initial_metadata_ = true; + call_->PerformOps(&ops); + call_->cq()->Pluck(&ops); + } + /// See the \a WriterInterface.Write method for semantics. + /// + /// Side effect: + /// Also sends initial metadata if not already sent (using the + /// \a ClientContext associated with this call to fill in values). + using internal::WriterInterface::Write; + bool Write(const W& msg, ::grpc::WriteOptions options) override { + if (options.is_last_message()) { + options.set_buffer_hint(); + } + + if (!ctx_->pending_ops_.SendMessagePtr(&msg, options).ok()) { + return false; + } + if (!ctx_->sent_initial_metadata_) { + ctx_->pending_ops_.SendInitialMetadata(&ctx_->initial_metadata_, + ctx_->initial_metadata_flags()); + if (ctx_->compression_level_set()) { + ctx_->pending_ops_.set_compression_level(ctx_->compression_level()); + } + ctx_->sent_initial_metadata_ = true; + } + call_->PerformOps(&ctx_->pending_ops_); + // if this is the last message we defer the pluck until AFTER we start + // the trailing md op. This prevents hangs. See + // https://github.com/grpc/grpc/issues/11546 + if (options.is_last_message()) { + ctx_->has_pending_ops_ = true; + return true; + } + ctx_->has_pending_ops_ = false; + return call_->cq()->Pluck(&ctx_->pending_ops_); + } + + private: + ::grpc::internal::Call* const call_; + ::grpc::ServerContext* const ctx_; + + template + friend class internal::ServerStreamingHandler; + + ServerWriter(::grpc::internal::Call* call, ::grpc::ServerContext* ctx) + : call_(call), ctx_(ctx) {} +}; + +/// Server-side interface for bi-directional streaming. template -using ServerReaderWriterInterface = - ::grpc_impl::ServerReaderWriterInterface; +class ServerReaderWriterInterface : public internal::ServerStreamingInterface, + public internal::WriterInterface, + public internal::ReaderInterface {}; + +/// Actual implementation of bi-directional streaming +namespace internal { +template +class ServerReaderWriterBody final { + public: + ServerReaderWriterBody(grpc::internal::Call* call, ::grpc::ServerContext* ctx) + : call_(call), ctx_(ctx) {} + + void SendInitialMetadata() { + GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_); + + grpc::internal::CallOpSet ops; + ops.SendInitialMetadata(&ctx_->initial_metadata_, + ctx_->initial_metadata_flags()); + if (ctx_->compression_level_set()) { + ops.set_compression_level(ctx_->compression_level()); + } + ctx_->sent_initial_metadata_ = true; + call_->PerformOps(&ops); + call_->cq()->Pluck(&ops); + } + bool NextMessageSize(uint32_t* sz) { + int result = call_->max_receive_message_size(); + *sz = (result > 0) ? result : UINT32_MAX; + return true; + } + + bool Read(R* msg) { + ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvMessage> ops; + ops.RecvMessage(msg); + call_->PerformOps(&ops); + return call_->cq()->Pluck(&ops) && ops.got_message; + } + + bool Write(const W& msg, ::grpc::WriteOptions options) { + if (options.is_last_message()) { + options.set_buffer_hint(); + } + if (!ctx_->pending_ops_.SendMessagePtr(&msg, options).ok()) { + return false; + } + if (!ctx_->sent_initial_metadata_) { + ctx_->pending_ops_.SendInitialMetadata(&ctx_->initial_metadata_, + ctx_->initial_metadata_flags()); + if (ctx_->compression_level_set()) { + ctx_->pending_ops_.set_compression_level(ctx_->compression_level()); + } + ctx_->sent_initial_metadata_ = true; + } + call_->PerformOps(&ctx_->pending_ops_); + // if this is the last message we defer the pluck until AFTER we start + // the trailing md op. This prevents hangs. See + // https://github.com/grpc/grpc/issues/11546 + if (options.is_last_message()) { + ctx_->has_pending_ops_ = true; + return true; + } + ctx_->has_pending_ops_ = false; + return call_->cq()->Pluck(&ctx_->pending_ops_); + } + + private: + grpc::internal::Call* const call_; + ::grpc::ServerContext* const ctx_; +}; + +} // namespace internal + +/// Synchronous (blocking) server-side API for a bidirectional +/// streaming call, where the incoming message stream coming from the client has +/// messages of type \a R, and the outgoing message streaming coming from +/// the server has messages of type \a W. template -using ServerReaderWriter = ::grpc_impl::ServerReaderWriter; +class ServerReaderWriter final : public ServerReaderWriterInterface { + public: + /// See the \a ServerStreamingInterface.SendInitialMetadata method + /// for semantics. Note that initial metadata will be affected by the + /// \a ServerContext associated with this call. + void SendInitialMetadata() override { body_.SendInitialMetadata(); } + + bool NextMessageSize(uint32_t* sz) override { + return body_.NextMessageSize(sz); + } + + bool Read(R* msg) override { return body_.Read(msg); } + + /// See the \a WriterInterface.Write(const W& msg, WriteOptions options) + /// method for semantics. + /// Side effect: + /// Also sends initial metadata if not already sent (using the \a + /// ServerContext associated with this call). + using internal::WriterInterface::Write; + bool Write(const W& msg, ::grpc::WriteOptions options) override { + return body_.Write(msg, options); + } + + private: + internal::ServerReaderWriterBody body_; + + friend class internal::TemplatedBidiStreamingHandler, + false>; + ServerReaderWriter(::grpc::internal::Call* call, ::grpc::ServerContext* ctx) + : body_(call, ctx) {} +}; +/// A class to represent a flow-controlled unary call. This is something +/// of a hybrid between conventional unary and streaming. This is invoked +/// through a unary call on the client side, but the server responds to it +/// as though it were a single-ping-pong streaming call. The server can use +/// the \a NextMessageSize method to determine an upper-bound on the size of +/// the message. A key difference relative to streaming: ServerUnaryStreamer +/// must have exactly 1 Read and exactly 1 Write, in that order, to function +/// correctly. Otherwise, the RPC is in error. template -using ServerUnaryStreamer = - ::grpc_impl::ServerUnaryStreamer; +class ServerUnaryStreamer final + : public ServerReaderWriterInterface { + public: + /// Block to send initial metadata to client. + /// Implicit input parameter: + /// - the \a ServerContext associated with this call will be used for + /// sending initial metadata. + void SendInitialMetadata() override { body_.SendInitialMetadata(); } + /// Get an upper bound on the request message size from the client. + bool NextMessageSize(uint32_t* sz) override { + return body_.NextMessageSize(sz); + } + + /// Read a message of type \a R into \a msg. Completion will be notified by \a + /// tag on the associated completion queue. + /// This is thread-safe with respect to \a Write or \a WritesDone methods. It + /// should not be called concurrently with other streaming APIs + /// on the same stream. It is not meaningful to call it concurrently + /// with another \a ReaderInterface::Read on the same stream since reads on + /// the same stream are delivered in order. + /// + /// \param[out] msg Where to eventually store the read message. + /// \param[in] tag The tag identifying the operation. + bool Read(RequestType* request) override { + if (read_done_) { + return false; + } + read_done_ = true; + return body_.Read(request); + } + + /// Block to write \a msg to the stream with WriteOptions \a options. + /// This is thread-safe with respect to \a ReaderInterface::Read + /// + /// \param msg The message to be written to the stream. + /// \param options The WriteOptions affecting the write operation. + /// + /// \return \a true on success, \a false when the stream has been closed. + using internal::WriterInterface::Write; + bool Write(const ResponseType& response, + ::grpc::WriteOptions options) override { + if (write_done_ || !read_done_) { + return false; + } + write_done_ = true; + return body_.Write(response, options); + } + + private: + internal::ServerReaderWriterBody body_; + bool read_done_; + bool write_done_; + + friend class internal::TemplatedBidiStreamingHandler< + ServerUnaryStreamer, true>; + ServerUnaryStreamer(::grpc::internal::Call* call, ::grpc::ServerContext* ctx) + : body_(call, ctx), read_done_(false), write_done_(false) {} +}; + +/// A class to represent a flow-controlled server-side streaming call. +/// This is something of a hybrid between server-side and bidi streaming. +/// This is invoked through a server-side streaming call on the client side, +/// but the server responds to it as though it were a bidi streaming call that +/// must first have exactly 1 Read and then any number of Writes. template -using ServerSplitStreamer = - ::grpc_impl::ServerSplitStreamer; +class ServerSplitStreamer final + : public ServerReaderWriterInterface { + public: + /// Block to send initial metadata to client. + /// Implicit input parameter: + /// - the \a ServerContext associated with this call will be used for + /// sending initial metadata. + void SendInitialMetadata() override { body_.SendInitialMetadata(); } + + /// Get an upper bound on the request message size from the client. + bool NextMessageSize(uint32_t* sz) override { + return body_.NextMessageSize(sz); + } + + /// Read a message of type \a R into \a msg. Completion will be notified by \a + /// tag on the associated completion queue. + /// This is thread-safe with respect to \a Write or \a WritesDone methods. It + /// should not be called concurrently with other streaming APIs + /// on the same stream. It is not meaningful to call it concurrently + /// with another \a ReaderInterface::Read on the same stream since reads on + /// the same stream are delivered in order. + /// + /// \param[out] msg Where to eventually store the read message. + /// \param[in] tag The tag identifying the operation. + bool Read(RequestType* request) override { + if (read_done_) { + return false; + } + read_done_ = true; + return body_.Read(request); + } + + /// Block to write \a msg to the stream with WriteOptions \a options. + /// This is thread-safe with respect to \a ReaderInterface::Read + /// + /// \param msg The message to be written to the stream. + /// \param options The WriteOptions affecting the write operation. + /// + /// \return \a true on success, \a false when the stream has been closed. + using internal::WriterInterface::Write; + bool Write(const ResponseType& response, + ::grpc::WriteOptions options) override { + return read_done_ && body_.Write(response, options); + } + + private: + internal::ServerReaderWriterBody body_; + bool read_done_; + + friend class internal::TemplatedBidiStreamingHandler< + ServerSplitStreamer, false>; + ServerSplitStreamer(::grpc::internal::Call* call, ::grpc::ServerContext* ctx) + : body_(call, ctx), read_done_(false) {} +}; } // namespace grpc diff --git a/include/grpcpp/impl/codegen/sync_stream_impl.h b/include/grpcpp/impl/codegen/sync_stream_impl.h deleted file mode 100644 index ce1ab11db19..00000000000 --- a/include/grpcpp/impl/codegen/sync_stream_impl.h +++ /dev/null @@ -1,943 +0,0 @@ -/* - * - * Copyright 2019 gRPC authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef GRPCPP_IMPL_CODEGEN_SYNC_STREAM_IMPL_H -#define GRPCPP_IMPL_CODEGEN_SYNC_STREAM_IMPL_H - -#include -#include -#include -#include -#include -#include -#include -#include - -namespace grpc_impl { - -namespace internal { -/// Common interface for all synchronous client side streaming. -class ClientStreamingInterface { - public: - virtual ~ClientStreamingInterface() {} - - /// Block waiting until the stream finishes and a final status of the call is - /// available. - /// - /// It is appropriate to call this method exactly once when both: - /// * the calling code (client-side) has no more message to send - /// (this can be declared implicitly by calling this method, or - /// explicitly through an earlier call to WritesDone method of the - /// class in use, e.g. \a ClientWriterInterface::WritesDone or - /// \a ClientReaderWriterInterface::WritesDone). - /// * there are no more messages to be received from the server (which can - /// be known implicitly, or explicitly from an earlier call to \a - /// ReaderInterface::Read that returned "false"). - /// - /// This function will return either: - /// - when all incoming messages have been read and the server has - /// returned status. - /// - when the server has returned a non-OK status. - /// - OR when the call failed for some reason and the library generated a - /// status. - /// - /// Return values: - /// - \a Status contains the status code, message and details for the call - /// - the \a ClientContext associated with this call is updated with - /// possible trailing metadata sent from the server. - virtual ::grpc::Status Finish() = 0; -}; - -/// Common interface for all synchronous server side streaming. -class ServerStreamingInterface { - public: - virtual ~ServerStreamingInterface() {} - - /// Block to send initial metadata to client. - /// This call is optional, but if it is used, it cannot be used concurrently - /// with or after the \a Finish method. - /// - /// The initial metadata that will be sent to the client will be - /// taken from the \a ServerContext associated with the call. - virtual void SendInitialMetadata() = 0; -}; - -/// An interface that yields a sequence of messages of type \a R. -template -class ReaderInterface { - public: - virtual ~ReaderInterface() {} - - /// Get an upper bound on the next message size available for reading on this - /// stream. - virtual bool NextMessageSize(uint32_t* sz) = 0; - - /// Block to read a message and parse to \a msg. Returns \a true on success. - /// This is thread-safe with respect to \a Write or \WritesDone methods on - /// the same stream. It should not be called concurrently with another \a - /// Read on the same stream as the order of delivery will not be defined. - /// - /// \param[out] msg The read message. - /// - /// \return \a false when there will be no more incoming messages, either - /// because the other side has called \a WritesDone() or the stream has failed - /// (or been cancelled). - virtual bool Read(R* msg) = 0; -}; - -/// An interface that can be fed a sequence of messages of type \a W. -template -class WriterInterface { - public: - virtual ~WriterInterface() {} - - /// Block to write \a msg to the stream with WriteOptions \a options. - /// This is thread-safe with respect to \a ReaderInterface::Read - /// - /// \param msg The message to be written to the stream. - /// \param options The WriteOptions affecting the write operation. - /// - /// \return \a true on success, \a false when the stream has been closed. - virtual bool Write(const W& msg, ::grpc::WriteOptions options) = 0; - - /// Block to write \a msg to the stream with default write options. - /// This is thread-safe with respect to \a ReaderInterface::Read - /// - /// \param msg The message to be written to the stream. - /// - /// \return \a true on success, \a false when the stream has been closed. - inline bool Write(const W& msg) { return Write(msg, ::grpc::WriteOptions()); } - - /// Write \a msg and coalesce it with the writing of trailing metadata, using - /// WriteOptions \a options. - /// - /// For client, WriteLast is equivalent of performing Write and WritesDone in - /// a single step. \a msg and trailing metadata are coalesced and sent on wire - /// by calling this function. For server, WriteLast buffers the \a msg. - /// The writing of \a msg is held until the service handler returns, - /// where \a msg and trailing metadata are coalesced and sent on wire. - /// Note that WriteLast can only buffer \a msg up to the flow control window - /// size. If \a msg size is larger than the window size, it will be sent on - /// wire without buffering. - /// - /// \param[in] msg The message to be written to the stream. - /// \param[in] options The WriteOptions to be used to write this message. - void WriteLast(const W& msg, ::grpc::WriteOptions options) { - Write(msg, options.set_last_message()); - } -}; - -} // namespace internal - -/// Client-side interface for streaming reads of message of type \a R. -template -class ClientReaderInterface : public internal::ClientStreamingInterface, - public internal::ReaderInterface { - public: - /// Block to wait for initial metadata from server. The received metadata - /// can only be accessed after this call returns. Should only be called before - /// the first read. Calling this method is optional, and if it is not called - /// the metadata will be available in ClientContext after the first read. - virtual void WaitForInitialMetadata() = 0; -}; - -namespace internal { -template -class ClientReaderFactory { - public: - template - static ClientReader* Create(::grpc::ChannelInterface* channel, - const ::grpc::internal::RpcMethod& method, - ::grpc::ClientContext* context, - const W& request) { - return new ClientReader(channel, method, context, request); - } -}; -} // namespace internal - -/// Synchronous (blocking) client-side API for doing server-streaming RPCs, -/// where the stream of messages coming from the server has messages -/// of type \a R. -template -class ClientReader final : public ClientReaderInterface { - public: - /// See the \a ClientStreamingInterface.WaitForInitialMetadata method for - /// semantics. - /// - // Side effect: - /// Once complete, the initial metadata read from - /// the server will be accessible through the \a ClientContext used to - /// construct this object. - void WaitForInitialMetadata() override { - GPR_CODEGEN_ASSERT(!context_->initial_metadata_received_); - - ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata> - ops; - ops.RecvInitialMetadata(context_); - call_.PerformOps(&ops); - cq_.Pluck(&ops); /// status ignored - } - - bool NextMessageSize(uint32_t* sz) override { - int result = call_.max_receive_message_size(); - *sz = (result > 0) ? result : UINT32_MAX; - return true; - } - - /// See the \a ReaderInterface.Read method for semantics. - /// Side effect: - /// This also receives initial metadata from the server, if not - /// already received (if initial metadata is received, it can be then - /// accessed through the \a ClientContext associated with this call). - bool Read(R* msg) override { - ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata, - ::grpc::internal::CallOpRecvMessage> - ops; - if (!context_->initial_metadata_received_) { - ops.RecvInitialMetadata(context_); - } - ops.RecvMessage(msg); - call_.PerformOps(&ops); - return cq_.Pluck(&ops) && ops.got_message; - } - - /// See the \a ClientStreamingInterface.Finish method for semantics. - /// - /// Side effect: - /// The \a ClientContext associated with this call is updated with - /// possible metadata received from the server. - ::grpc::Status Finish() override { - ::grpc::internal::CallOpSet<::grpc::internal::CallOpClientRecvStatus> ops; - ::grpc::Status status; - ops.ClientRecvStatus(context_, &status); - call_.PerformOps(&ops); - GPR_CODEGEN_ASSERT(cq_.Pluck(&ops)); - return status; - } - - private: - friend class internal::ClientReaderFactory; - ::grpc::ClientContext* context_; - ::grpc::CompletionQueue cq_; - ::grpc::internal::Call call_; - - /// Block to create a stream and write the initial metadata and \a request - /// out. Note that \a context will be used to fill in custom initial - /// metadata used to send to the server when starting the call. - template - ClientReader(::grpc::ChannelInterface* channel, - const ::grpc::internal::RpcMethod& method, - ::grpc::ClientContext* context, const W& request) - : context_(context), - cq_(grpc_completion_queue_attributes{ - GRPC_CQ_CURRENT_VERSION, GRPC_CQ_PLUCK, GRPC_CQ_DEFAULT_POLLING, - nullptr}), // Pluckable cq - call_(channel->CreateCall(method, context, &cq_)) { - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, - ::grpc::internal::CallOpSendMessage, - ::grpc::internal::CallOpClientSendClose> - ops; - ops.SendInitialMetadata(&context->send_initial_metadata_, - context->initial_metadata_flags()); - // TODO(ctiller): don't assert - GPR_CODEGEN_ASSERT(ops.SendMessagePtr(&request).ok()); - ops.ClientSendClose(); - call_.PerformOps(&ops); - cq_.Pluck(&ops); - } -}; - -/// Client-side interface for streaming writes of message type \a W. -template -class ClientWriterInterface : public internal::ClientStreamingInterface, - public internal::WriterInterface { - public: - /// Half close writing from the client. (signal that the stream of messages - /// coming from the client is complete). - /// Blocks until currently-pending writes are completed. - /// Thread safe with respect to \a ReaderInterface::Read operations only - /// - /// \return Whether the writes were successful. - virtual bool WritesDone() = 0; -}; - -namespace internal { -template -class ClientWriterFactory { - public: - template - static ClientWriter* Create(::grpc::ChannelInterface* channel, - const ::grpc::internal::RpcMethod& method, - ::grpc::ClientContext* context, R* response) { - return new ClientWriter(channel, method, context, response); - } -}; -} // namespace internal - -/// Synchronous (blocking) client-side API for doing client-streaming RPCs, -/// where the outgoing message stream coming from the client has messages of -/// type \a W. -template -class ClientWriter : public ClientWriterInterface { - public: - /// See the \a ClientStreamingInterface.WaitForInitialMetadata method for - /// semantics. - /// - // Side effect: - /// Once complete, the initial metadata read from the server will be - /// accessible through the \a ClientContext used to construct this object. - void WaitForInitialMetadata() { - GPR_CODEGEN_ASSERT(!context_->initial_metadata_received_); - - ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata> - ops; - ops.RecvInitialMetadata(context_); - call_.PerformOps(&ops); - cq_.Pluck(&ops); // status ignored - } - - /// See the WriterInterface.Write(const W& msg, WriteOptions options) method - /// for semantics. - /// - /// Side effect: - /// Also sends initial metadata if not already sent (using the - /// \a ClientContext associated with this call). - using internal::WriterInterface::Write; - bool Write(const W& msg, ::grpc::WriteOptions options) override { - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, - ::grpc::internal::CallOpSendMessage, - ::grpc::internal::CallOpClientSendClose> - ops; - - if (options.is_last_message()) { - options.set_buffer_hint(); - ops.ClientSendClose(); - } - if (context_->initial_metadata_corked_) { - ops.SendInitialMetadata(&context_->send_initial_metadata_, - context_->initial_metadata_flags()); - context_->set_initial_metadata_corked(false); - } - if (!ops.SendMessagePtr(&msg, options).ok()) { - return false; - } - - call_.PerformOps(&ops); - return cq_.Pluck(&ops); - } - - bool WritesDone() override { - ::grpc::internal::CallOpSet<::grpc::internal::CallOpClientSendClose> ops; - ops.ClientSendClose(); - call_.PerformOps(&ops); - return cq_.Pluck(&ops); - } - - /// See the ClientStreamingInterface.Finish method for semantics. - /// Side effects: - /// - Also receives initial metadata if not already received. - /// - Attempts to fill in the \a response parameter passed - /// to the constructor of this instance with the response - /// message from the server. - ::grpc::Status Finish() override { - ::grpc::Status status; - if (!context_->initial_metadata_received_) { - finish_ops_.RecvInitialMetadata(context_); - } - finish_ops_.ClientRecvStatus(context_, &status); - call_.PerformOps(&finish_ops_); - GPR_CODEGEN_ASSERT(cq_.Pluck(&finish_ops_)); - return status; - } - - private: - friend class internal::ClientWriterFactory; - - /// Block to create a stream (i.e. send request headers and other initial - /// metadata to the server). Note that \a context will be used to fill - /// in custom initial metadata. \a response will be filled in with the - /// single expected response message from the server upon a successful - /// call to the \a Finish method of this instance. - template - ClientWriter(::grpc::ChannelInterface* channel, - const ::grpc::internal::RpcMethod& method, - ::grpc::ClientContext* context, R* response) - : context_(context), - cq_(grpc_completion_queue_attributes{ - GRPC_CQ_CURRENT_VERSION, GRPC_CQ_PLUCK, GRPC_CQ_DEFAULT_POLLING, - nullptr}), // Pluckable cq - call_(channel->CreateCall(method, context, &cq_)) { - finish_ops_.RecvMessage(response); - finish_ops_.AllowNoMessage(); - - if (!context_->initial_metadata_corked_) { - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata> - ops; - ops.SendInitialMetadata(&context->send_initial_metadata_, - context->initial_metadata_flags()); - call_.PerformOps(&ops); - cq_.Pluck(&ops); - } - } - - ::grpc::ClientContext* context_; - ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata, - ::grpc::internal::CallOpGenericRecvMessage, - ::grpc::internal::CallOpClientRecvStatus> - finish_ops_; - ::grpc::CompletionQueue cq_; - ::grpc::internal::Call call_; -}; - -/// Client-side interface for bi-directional streaming with -/// client-to-server stream messages of type \a W and -/// server-to-client stream messages of type \a R. -template -class ClientReaderWriterInterface : public internal::ClientStreamingInterface, - public internal::WriterInterface, - public internal::ReaderInterface { - public: - /// Block to wait for initial metadata from server. The received metadata - /// can only be accessed after this call returns. Should only be called before - /// the first read. Calling this method is optional, and if it is not called - /// the metadata will be available in ClientContext after the first read. - virtual void WaitForInitialMetadata() = 0; - - /// Half close writing from the client. (signal that the stream of messages - /// coming from the client is complete). - /// Blocks until currently-pending writes are completed. - /// Thread-safe with respect to \a ReaderInterface::Read - /// - /// \return Whether the writes were successful. - virtual bool WritesDone() = 0; -}; - -namespace internal { -template -class ClientReaderWriterFactory { - public: - static ClientReaderWriter* Create( - ::grpc::ChannelInterface* channel, - const ::grpc::internal::RpcMethod& method, - ::grpc::ClientContext* context) { - return new ClientReaderWriter(channel, method, context); - } -}; -} // namespace internal - -/// Synchronous (blocking) client-side API for bi-directional streaming RPCs, -/// where the outgoing message stream coming from the client has messages of -/// type \a W, and the incoming messages stream coming from the server has -/// messages of type \a R. -template -class ClientReaderWriter final : public ClientReaderWriterInterface { - public: - /// Block waiting to read initial metadata from the server. - /// This call is optional, but if it is used, it cannot be used concurrently - /// with or after the \a Finish method. - /// - /// Once complete, the initial metadata read from the server will be - /// accessible through the \a ClientContext used to construct this object. - void WaitForInitialMetadata() override { - GPR_CODEGEN_ASSERT(!context_->initial_metadata_received_); - - ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata> - ops; - ops.RecvInitialMetadata(context_); - call_.PerformOps(&ops); - cq_.Pluck(&ops); // status ignored - } - - bool NextMessageSize(uint32_t* sz) override { - int result = call_.max_receive_message_size(); - *sz = (result > 0) ? result : UINT32_MAX; - return true; - } - - /// See the \a ReaderInterface.Read method for semantics. - /// Side effect: - /// Also receives initial metadata if not already received (updates the \a - /// ClientContext associated with this call in that case). - bool Read(R* msg) override { - ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata, - ::grpc::internal::CallOpRecvMessage> - ops; - if (!context_->initial_metadata_received_) { - ops.RecvInitialMetadata(context_); - } - ops.RecvMessage(msg); - call_.PerformOps(&ops); - return cq_.Pluck(&ops) && ops.got_message; - } - - /// See the \a WriterInterface.Write method for semantics. - /// - /// Side effect: - /// Also sends initial metadata if not already sent (using the - /// \a ClientContext associated with this call to fill in values). - using internal::WriterInterface::Write; - bool Write(const W& msg, ::grpc::WriteOptions options) override { - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata, - ::grpc::internal::CallOpSendMessage, - ::grpc::internal::CallOpClientSendClose> - ops; - - if (options.is_last_message()) { - options.set_buffer_hint(); - ops.ClientSendClose(); - } - if (context_->initial_metadata_corked_) { - ops.SendInitialMetadata(&context_->send_initial_metadata_, - context_->initial_metadata_flags()); - context_->set_initial_metadata_corked(false); - } - if (!ops.SendMessagePtr(&msg, options).ok()) { - return false; - } - - call_.PerformOps(&ops); - return cq_.Pluck(&ops); - } - - bool WritesDone() override { - ::grpc::internal::CallOpSet<::grpc::internal::CallOpClientSendClose> ops; - ops.ClientSendClose(); - call_.PerformOps(&ops); - return cq_.Pluck(&ops); - } - - /// See the ClientStreamingInterface.Finish method for semantics. - /// - /// Side effect: - /// - the \a ClientContext associated with this call is updated with - /// possible trailing metadata sent from the server. - ::grpc::Status Finish() override { - ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvInitialMetadata, - ::grpc::internal::CallOpClientRecvStatus> - ops; - if (!context_->initial_metadata_received_) { - ops.RecvInitialMetadata(context_); - } - ::grpc::Status status; - ops.ClientRecvStatus(context_, &status); - call_.PerformOps(&ops); - GPR_CODEGEN_ASSERT(cq_.Pluck(&ops)); - return status; - } - - private: - friend class internal::ClientReaderWriterFactory; - - ::grpc::ClientContext* context_; - ::grpc::CompletionQueue cq_; - ::grpc::internal::Call call_; - - /// Block to create a stream and write the initial metadata and \a request - /// out. Note that \a context will be used to fill in custom initial metadata - /// used to send to the server when starting the call. - ClientReaderWriter(::grpc::ChannelInterface* channel, - const ::grpc::internal::RpcMethod& method, - ::grpc::ClientContext* context) - : context_(context), - cq_(grpc_completion_queue_attributes{ - GRPC_CQ_CURRENT_VERSION, GRPC_CQ_PLUCK, GRPC_CQ_DEFAULT_POLLING, - nullptr}), // Pluckable cq - call_(channel->CreateCall(method, context, &cq_)) { - if (!context_->initial_metadata_corked_) { - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata> - ops; - ops.SendInitialMetadata(&context->send_initial_metadata_, - context->initial_metadata_flags()); - call_.PerformOps(&ops); - cq_.Pluck(&ops); - } - } -}; - -/// Server-side interface for streaming reads of message of type \a R. -template -class ServerReaderInterface : public internal::ServerStreamingInterface, - public internal::ReaderInterface {}; - -/// Synchronous (blocking) server-side API for doing client-streaming RPCs, -/// where the incoming message stream coming from the client has messages of -/// type \a R. -template -class ServerReader final : public ServerReaderInterface { - public: - /// See the \a ServerStreamingInterface.SendInitialMetadata method - /// for semantics. Note that initial metadata will be affected by the - /// \a ServerContext associated with this call. - void SendInitialMetadata() override { - GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_); - - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata> - ops; - ops.SendInitialMetadata(&ctx_->initial_metadata_, - ctx_->initial_metadata_flags()); - if (ctx_->compression_level_set()) { - ops.set_compression_level(ctx_->compression_level()); - } - ctx_->sent_initial_metadata_ = true; - call_->PerformOps(&ops); - call_->cq()->Pluck(&ops); - } - - bool NextMessageSize(uint32_t* sz) override { - int result = call_->max_receive_message_size(); - *sz = (result > 0) ? result : UINT32_MAX; - return true; - } - - bool Read(R* msg) override { - ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvMessage> ops; - ops.RecvMessage(msg); - call_->PerformOps(&ops); - return call_->cq()->Pluck(&ops) && ops.got_message; - } - - private: - ::grpc::internal::Call* const call_; - ::grpc::ServerContext* const ctx_; - - template - friend class ::grpc_impl::internal::ClientStreamingHandler; - - ServerReader(::grpc::internal::Call* call, ::grpc::ServerContext* ctx) - : call_(call), ctx_(ctx) {} -}; - -/// Server-side interface for streaming writes of message of type \a W. -template -class ServerWriterInterface : public internal::ServerStreamingInterface, - public internal::WriterInterface {}; - -/// Synchronous (blocking) server-side API for doing for doing a -/// server-streaming RPCs, where the outgoing message stream coming from the -/// server has messages of type \a W. -template -class ServerWriter final : public ServerWriterInterface { - public: - /// See the \a ServerStreamingInterface.SendInitialMetadata method - /// for semantics. - /// Note that initial metadata will be affected by the - /// \a ServerContext associated with this call. - void SendInitialMetadata() override { - GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_); - - ::grpc::internal::CallOpSet<::grpc::internal::CallOpSendInitialMetadata> - ops; - ops.SendInitialMetadata(&ctx_->initial_metadata_, - ctx_->initial_metadata_flags()); - if (ctx_->compression_level_set()) { - ops.set_compression_level(ctx_->compression_level()); - } - ctx_->sent_initial_metadata_ = true; - call_->PerformOps(&ops); - call_->cq()->Pluck(&ops); - } - - /// See the \a WriterInterface.Write method for semantics. - /// - /// Side effect: - /// Also sends initial metadata if not already sent (using the - /// \a ClientContext associated with this call to fill in values). - using internal::WriterInterface::Write; - bool Write(const W& msg, ::grpc::WriteOptions options) override { - if (options.is_last_message()) { - options.set_buffer_hint(); - } - - if (!ctx_->pending_ops_.SendMessagePtr(&msg, options).ok()) { - return false; - } - if (!ctx_->sent_initial_metadata_) { - ctx_->pending_ops_.SendInitialMetadata(&ctx_->initial_metadata_, - ctx_->initial_metadata_flags()); - if (ctx_->compression_level_set()) { - ctx_->pending_ops_.set_compression_level(ctx_->compression_level()); - } - ctx_->sent_initial_metadata_ = true; - } - call_->PerformOps(&ctx_->pending_ops_); - // if this is the last message we defer the pluck until AFTER we start - // the trailing md op. This prevents hangs. See - // https://github.com/grpc/grpc/issues/11546 - if (options.is_last_message()) { - ctx_->has_pending_ops_ = true; - return true; - } - ctx_->has_pending_ops_ = false; - return call_->cq()->Pluck(&ctx_->pending_ops_); - } - - private: - ::grpc::internal::Call* const call_; - ::grpc::ServerContext* const ctx_; - - template - friend class ::grpc_impl::internal::ServerStreamingHandler; - - ServerWriter(::grpc::internal::Call* call, ::grpc::ServerContext* ctx) - : call_(call), ctx_(ctx) {} -}; - -/// Server-side interface for bi-directional streaming. -template -class ServerReaderWriterInterface : public internal::ServerStreamingInterface, - public internal::WriterInterface, - public internal::ReaderInterface {}; - -/// Actual implementation of bi-directional streaming -namespace internal { -template -class ServerReaderWriterBody final { - public: - ServerReaderWriterBody(grpc::internal::Call* call, ::grpc::ServerContext* ctx) - : call_(call), ctx_(ctx) {} - - void SendInitialMetadata() { - GPR_CODEGEN_ASSERT(!ctx_->sent_initial_metadata_); - - grpc::internal::CallOpSet ops; - ops.SendInitialMetadata(&ctx_->initial_metadata_, - ctx_->initial_metadata_flags()); - if (ctx_->compression_level_set()) { - ops.set_compression_level(ctx_->compression_level()); - } - ctx_->sent_initial_metadata_ = true; - call_->PerformOps(&ops); - call_->cq()->Pluck(&ops); - } - - bool NextMessageSize(uint32_t* sz) { - int result = call_->max_receive_message_size(); - *sz = (result > 0) ? result : UINT32_MAX; - return true; - } - - bool Read(R* msg) { - ::grpc::internal::CallOpSet<::grpc::internal::CallOpRecvMessage> ops; - ops.RecvMessage(msg); - call_->PerformOps(&ops); - return call_->cq()->Pluck(&ops) && ops.got_message; - } - - bool Write(const W& msg, ::grpc::WriteOptions options) { - if (options.is_last_message()) { - options.set_buffer_hint(); - } - if (!ctx_->pending_ops_.SendMessagePtr(&msg, options).ok()) { - return false; - } - if (!ctx_->sent_initial_metadata_) { - ctx_->pending_ops_.SendInitialMetadata(&ctx_->initial_metadata_, - ctx_->initial_metadata_flags()); - if (ctx_->compression_level_set()) { - ctx_->pending_ops_.set_compression_level(ctx_->compression_level()); - } - ctx_->sent_initial_metadata_ = true; - } - call_->PerformOps(&ctx_->pending_ops_); - // if this is the last message we defer the pluck until AFTER we start - // the trailing md op. This prevents hangs. See - // https://github.com/grpc/grpc/issues/11546 - if (options.is_last_message()) { - ctx_->has_pending_ops_ = true; - return true; - } - ctx_->has_pending_ops_ = false; - return call_->cq()->Pluck(&ctx_->pending_ops_); - } - - private: - grpc::internal::Call* const call_; - ::grpc::ServerContext* const ctx_; -}; - -} // namespace internal - -/// Synchronous (blocking) server-side API for a bidirectional -/// streaming call, where the incoming message stream coming from the client has -/// messages of type \a R, and the outgoing message streaming coming from -/// the server has messages of type \a W. -template -class ServerReaderWriter final : public ServerReaderWriterInterface { - public: - /// See the \a ServerStreamingInterface.SendInitialMetadata method - /// for semantics. Note that initial metadata will be affected by the - /// \a ServerContext associated with this call. - void SendInitialMetadata() override { body_.SendInitialMetadata(); } - - bool NextMessageSize(uint32_t* sz) override { - return body_.NextMessageSize(sz); - } - - bool Read(R* msg) override { return body_.Read(msg); } - - /// See the \a WriterInterface.Write(const W& msg, WriteOptions options) - /// method for semantics. - /// Side effect: - /// Also sends initial metadata if not already sent (using the \a - /// ServerContext associated with this call). - using internal::WriterInterface::Write; - bool Write(const W& msg, ::grpc::WriteOptions options) override { - return body_.Write(msg, options); - } - - private: - internal::ServerReaderWriterBody body_; - - friend class ::grpc_impl::internal::TemplatedBidiStreamingHandler< - ServerReaderWriter, false>; - ServerReaderWriter(::grpc::internal::Call* call, ::grpc::ServerContext* ctx) - : body_(call, ctx) {} -}; - -/// A class to represent a flow-controlled unary call. This is something -/// of a hybrid between conventional unary and streaming. This is invoked -/// through a unary call on the client side, but the server responds to it -/// as though it were a single-ping-pong streaming call. The server can use -/// the \a NextMessageSize method to determine an upper-bound on the size of -/// the message. A key difference relative to streaming: ServerUnaryStreamer -/// must have exactly 1 Read and exactly 1 Write, in that order, to function -/// correctly. Otherwise, the RPC is in error. -template -class ServerUnaryStreamer final - : public ServerReaderWriterInterface { - public: - /// Block to send initial metadata to client. - /// Implicit input parameter: - /// - the \a ServerContext associated with this call will be used for - /// sending initial metadata. - void SendInitialMetadata() override { body_.SendInitialMetadata(); } - - /// Get an upper bound on the request message size from the client. - bool NextMessageSize(uint32_t* sz) override { - return body_.NextMessageSize(sz); - } - - /// Read a message of type \a R into \a msg. Completion will be notified by \a - /// tag on the associated completion queue. - /// This is thread-safe with respect to \a Write or \a WritesDone methods. It - /// should not be called concurrently with other streaming APIs - /// on the same stream. It is not meaningful to call it concurrently - /// with another \a ReaderInterface::Read on the same stream since reads on - /// the same stream are delivered in order. - /// - /// \param[out] msg Where to eventually store the read message. - /// \param[in] tag The tag identifying the operation. - bool Read(RequestType* request) override { - if (read_done_) { - return false; - } - read_done_ = true; - return body_.Read(request); - } - - /// Block to write \a msg to the stream with WriteOptions \a options. - /// This is thread-safe with respect to \a ReaderInterface::Read - /// - /// \param msg The message to be written to the stream. - /// \param options The WriteOptions affecting the write operation. - /// - /// \return \a true on success, \a false when the stream has been closed. - using internal::WriterInterface::Write; - bool Write(const ResponseType& response, - ::grpc::WriteOptions options) override { - if (write_done_ || !read_done_) { - return false; - } - write_done_ = true; - return body_.Write(response, options); - } - - private: - internal::ServerReaderWriterBody body_; - bool read_done_; - bool write_done_; - - friend class ::grpc_impl::internal::TemplatedBidiStreamingHandler< - ServerUnaryStreamer, true>; - ServerUnaryStreamer(::grpc::internal::Call* call, ::grpc::ServerContext* ctx) - : body_(call, ctx), read_done_(false), write_done_(false) {} -}; - -/// A class to represent a flow-controlled server-side streaming call. -/// This is something of a hybrid between server-side and bidi streaming. -/// This is invoked through a server-side streaming call on the client side, -/// but the server responds to it as though it were a bidi streaming call that -/// must first have exactly 1 Read and then any number of Writes. -template -class ServerSplitStreamer final - : public ServerReaderWriterInterface { - public: - /// Block to send initial metadata to client. - /// Implicit input parameter: - /// - the \a ServerContext associated with this call will be used for - /// sending initial metadata. - void SendInitialMetadata() override { body_.SendInitialMetadata(); } - - /// Get an upper bound on the request message size from the client. - bool NextMessageSize(uint32_t* sz) override { - return body_.NextMessageSize(sz); - } - - /// Read a message of type \a R into \a msg. Completion will be notified by \a - /// tag on the associated completion queue. - /// This is thread-safe with respect to \a Write or \a WritesDone methods. It - /// should not be called concurrently with other streaming APIs - /// on the same stream. It is not meaningful to call it concurrently - /// with another \a ReaderInterface::Read on the same stream since reads on - /// the same stream are delivered in order. - /// - /// \param[out] msg Where to eventually store the read message. - /// \param[in] tag The tag identifying the operation. - bool Read(RequestType* request) override { - if (read_done_) { - return false; - } - read_done_ = true; - return body_.Read(request); - } - - /// Block to write \a msg to the stream with WriteOptions \a options. - /// This is thread-safe with respect to \a ReaderInterface::Read - /// - /// \param msg The message to be written to the stream. - /// \param options The WriteOptions affecting the write operation. - /// - /// \return \a true on success, \a false when the stream has been closed. - using internal::WriterInterface::Write; - bool Write(const ResponseType& response, - ::grpc::WriteOptions options) override { - return read_done_ && body_.Write(response, options); - } - - private: - internal::ServerReaderWriterBody body_; - bool read_done_; - - friend class ::grpc_impl::internal::TemplatedBidiStreamingHandler< - ServerSplitStreamer, false>; - ServerSplitStreamer(::grpc::internal::Call* call, ::grpc::ServerContext* ctx) - : body_(call, ctx), read_done_(false) {} -}; - -} // namespace grpc_impl - -#endif // GRPCPP_IMPL_CODEGEN_SYNC_STREAM_IMPL_H diff --git a/include/grpcpp/test/mock_stream.h b/include/grpcpp/test/mock_stream.h index 55e76f6f0a2..e33595d7096 100644 --- a/include/grpcpp/test/mock_stream.h +++ b/include/grpcpp/test/mock_stream.h @@ -31,7 +31,7 @@ namespace grpc { namespace testing { template -class MockClientReader : public ::grpc_impl::ClientReaderInterface { +class MockClientReader : public ::grpc::ClientReaderInterface { public: MockClientReader() = default; @@ -47,7 +47,7 @@ class MockClientReader : public ::grpc_impl::ClientReaderInterface { }; template -class MockClientWriter : public ::grpc_impl::ClientWriterInterface { +class MockClientWriter : public ::grpc::ClientWriterInterface { public: MockClientWriter() = default; @@ -63,7 +63,7 @@ class MockClientWriter : public ::grpc_impl::ClientWriterInterface { template class MockClientReaderWriter - : public ::grpc_impl::ClientReaderWriterInterface { + : public ::grpc::ClientReaderWriterInterface { public: MockClientReaderWriter() = default; @@ -86,7 +86,7 @@ class MockClientReaderWriter template class MockClientAsyncResponseReader - : public ::grpc_impl::ClientAsyncResponseReaderInterface { + : public ::grpc::ClientAsyncResponseReaderInterface { public: MockClientAsyncResponseReader() = default; @@ -108,8 +108,7 @@ class MockClientAsyncReader : public ClientAsyncReaderInterface { }; template -class MockClientAsyncWriter - : public ::grpc_impl::ClientAsyncWriterInterface { +class MockClientAsyncWriter : public ::grpc::ClientAsyncWriterInterface { public: MockClientAsyncWriter() = default; diff --git a/src/compiler/cpp_generator.cc b/src/compiler/cpp_generator.cc index 1fce393ba0f..a8c54993442 100644 --- a/src/compiler/cpp_generator.cc +++ b/src/compiler/cpp_generator.cc @@ -1110,7 +1110,7 @@ void PrintHeaderServerMethodCallback(grpc_generator::Printer* printer, " ::grpc::Service::experimental().\n" "#endif\n" " MarkMethodCallback($Idx$,\n" - " new ::grpc_impl::internal::CallbackUnaryHandler< " + " new ::grpc::internal::CallbackUnaryHandler< " "$RealRequest$, $RealResponse$>(\n" " [this](\n" "#ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL\n" @@ -1134,7 +1134,7 @@ void PrintHeaderServerMethodCallback(grpc_generator::Printer* printer, " ::grpc::internal::MethodHandler* const handler = " "::grpc::Service::experimental().GetHandler($Idx$);\n" "#endif\n" - " static_cast<::grpc_impl::internal::CallbackUnaryHandler< " + " static_cast<::grpc::internal::CallbackUnaryHandler< " "$RealRequest$, $RealResponse$>*>(handler)\n" " ->SetMessageAllocator(allocator);\n"); } else if (ClientOnlyStreaming(method)) { @@ -1146,7 +1146,7 @@ void PrintHeaderServerMethodCallback(grpc_generator::Printer* printer, " ::grpc::Service::experimental().\n" "#endif\n" " MarkMethodCallback($Idx$,\n" - " new ::grpc_impl::internal::CallbackClientStreamingHandler< " + " new ::grpc::internal::CallbackClientStreamingHandler< " "$RealRequest$, $RealResponse$>(\n" " [this](\n" "#ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL\n" @@ -1167,7 +1167,7 @@ void PrintHeaderServerMethodCallback(grpc_generator::Printer* printer, " ::grpc::Service::experimental().\n" "#endif\n" " MarkMethodCallback($Idx$,\n" - " new ::grpc_impl::internal::CallbackServerStreamingHandler< " + " new ::grpc::internal::CallbackServerStreamingHandler< " "$RealRequest$, $RealResponse$>(\n" " [this](\n" "#ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL\n" @@ -1188,7 +1188,7 @@ void PrintHeaderServerMethodCallback(grpc_generator::Printer* printer, " ::grpc::Service::experimental().\n" "#endif\n" " MarkMethodCallback($Idx$,\n" - " new ::grpc_impl::internal::CallbackBidiHandler< " + " new ::grpc::internal::CallbackBidiHandler< " "$RealRequest$, $RealResponse$>(\n" " [this](\n" "#ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL\n" @@ -1239,7 +1239,7 @@ void PrintHeaderServerMethodRawCallback( " ::grpc::Service::experimental().\n" "#endif\n" " MarkMethodRawCallback($Idx$,\n" - " new ::grpc_impl::internal::CallbackUnaryHandler< " + " new ::grpc::internal::CallbackUnaryHandler< " "$RealRequest$, $RealResponse$>(\n" " [this](\n" "#ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL\n" @@ -1261,7 +1261,7 @@ void PrintHeaderServerMethodRawCallback( " ::grpc::Service::experimental().\n" "#endif\n" " MarkMethodRawCallback($Idx$,\n" - " new ::grpc_impl::internal::CallbackClientStreamingHandler< " + " new ::grpc::internal::CallbackClientStreamingHandler< " "$RealRequest$, $RealResponse$>(\n" " [this](\n" "#ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL\n" @@ -1281,7 +1281,7 @@ void PrintHeaderServerMethodRawCallback( " ::grpc::Service::experimental().\n" "#endif\n" " MarkMethodRawCallback($Idx$,\n" - " new ::grpc_impl::internal::CallbackServerStreamingHandler< " + " new ::grpc::internal::CallbackServerStreamingHandler< " "$RealRequest$, $RealResponse$>(\n" " [this](\n" "#ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL\n" @@ -1302,7 +1302,7 @@ void PrintHeaderServerMethodRawCallback( " ::grpc::Service::experimental().\n" "#endif\n" " MarkMethodRawCallback($Idx$,\n" - " new ::grpc_impl::internal::CallbackBidiHandler< " + " new ::grpc::internal::CallbackBidiHandler< " "$RealRequest$, $RealResponse$>(\n" " [this](\n" "#ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL\n" @@ -1346,7 +1346,7 @@ void PrintHeaderServerMethodStreamedUnary( " new ::grpc::internal::StreamedUnaryHandler<\n" " $Request$, $Response$>(\n" " [this](::grpc::ServerContext* context,\n" - " ::grpc_impl::ServerUnaryStreamer<\n" + " ::grpc::ServerUnaryStreamer<\n" " $Request$, $Response$>* streamer) {\n" " return this->Streamed$Method$(context,\n" " streamer);\n" @@ -1400,7 +1400,7 @@ void PrintHeaderServerMethodSplitStreaming( " new ::grpc::internal::SplitServerStreamingHandler<\n" " $Request$, $Response$>(\n" " [this](::grpc::ServerContext* context,\n" - " ::grpc_impl::ServerSplitStreamer<\n" + " ::grpc::ServerSplitStreamer<\n" " $Request$, $Response$>* streamer) {\n" " return this->Streamed$Method$(context,\n" " streamer);\n" @@ -1925,7 +1925,7 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer, "const $Request$* request, $Response$* response, " "std::function f) {\n"); printer->Print(*vars, - " ::grpc_impl::internal::CallbackUnaryCall" + " ::grpc::internal::CallbackUnaryCall" "(stub_->channel_.get(), stub_->rpcmethod_$Method$_, " "context, request, response, std::move(f));\n}\n\n"); @@ -1935,7 +1935,7 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer, "const ::grpc::ByteBuffer* request, $Response$* response, " "std::function f) {\n"); printer->Print(*vars, - " ::grpc_impl::internal::CallbackUnaryCall" + " ::grpc::internal::CallbackUnaryCall" "(stub_->channel_.get(), stub_->rpcmethod_$Method$_, " "context, request, response, std::move(f));\n}\n\n"); @@ -1945,7 +1945,7 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer, "const $Request$* request, $Response$* response, " "::grpc::experimental::ClientUnaryReactor* reactor) {\n"); printer->Print(*vars, - " ::grpc_impl::internal::ClientCallbackUnaryFactory::Create" + " ::grpc::internal::ClientCallbackUnaryFactory::Create" "(stub_->channel_.get(), stub_->rpcmethod_$Method$_, " "context, request, response, reactor);\n}\n\n"); @@ -1955,7 +1955,7 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer, "const ::grpc::ByteBuffer* request, $Response$* response, " "::grpc::experimental::ClientUnaryReactor* reactor) {\n"); printer->Print(*vars, - " ::grpc_impl::internal::ClientCallbackUnaryFactory::Create" + " ::grpc::internal::ClientCallbackUnaryFactory::Create" "(stub_->channel_.get(), stub_->rpcmethod_$Method$_, " "context, request, response, reactor);\n}\n\n"); @@ -1971,7 +1971,7 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer, printer->Print( *vars, " return " - "::grpc_impl::internal::ClientAsyncResponseReaderFactory< $Response$>" + "::grpc::internal::ClientAsyncResponseReaderFactory< $Response$>" "::Create(channel_.get(), cq, " "rpcmethod_$Method$_, " "context, request, $AsyncStart$);\n" @@ -1983,7 +1983,7 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer, "$ns$$Service$::Stub::$Method$Raw(" "::grpc::ClientContext* context, $Response$* response) {\n"); printer->Print(*vars, - " return ::grpc_impl::internal::ClientWriterFactory< " + " return ::grpc::internal::ClientWriterFactory< " "$Request$>::Create(" "channel_.get(), " "rpcmethod_$Method$_, " @@ -1997,7 +1997,7 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer, "$Response$* response, " "::grpc::experimental::ClientWriteReactor< $Request$>* reactor) {\n"); printer->Print(*vars, - " ::grpc_impl::internal::ClientCallbackWriterFactory< " + " ::grpc::internal::ClientCallbackWriterFactory< " "$Request$>::Create(" "stub_->channel_.get(), " "stub_->rpcmethod_$Method$_, " @@ -2016,7 +2016,7 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer, "::grpc::CompletionQueue* cq$AsyncMethodParams$) {\n"); printer->Print( *vars, - " return ::grpc_impl::internal::ClientAsyncWriterFactory< $Request$>" + " return ::grpc::internal::ClientAsyncWriterFactory< $Request$>" "::Create(channel_.get(), cq, " "rpcmethod_$Method$_, " "context, response, $AsyncStart$$AsyncCreateArgs$);\n" @@ -2029,7 +2029,7 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer, "$ns$$Service$::Stub::$Method$Raw(" "::grpc::ClientContext* context, const $Request$& request) {\n"); printer->Print(*vars, - " return ::grpc_impl::internal::ClientReaderFactory< " + " return ::grpc::internal::ClientReaderFactory< " "$Response$>::Create(" "channel_.get(), " "rpcmethod_$Method$_, " @@ -2043,7 +2043,7 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer, "$Request$* request, " "::grpc::experimental::ClientReadReactor< $Response$>* reactor) {\n"); printer->Print(*vars, - " ::grpc_impl::internal::ClientCallbackReaderFactory< " + " ::grpc::internal::ClientCallbackReaderFactory< " "$Response$>::Create(" "stub_->channel_.get(), " "stub_->rpcmethod_$Method$_, " @@ -2061,14 +2061,13 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer, "$ns$$Service$::Stub::$AsyncPrefix$$Method$Raw(" "::grpc::ClientContext* context, const $Request$& request, " "::grpc::CompletionQueue* cq$AsyncMethodParams$) {\n"); - printer->Print( - *vars, - " return ::grpc_impl::internal::ClientAsyncReaderFactory< " - "$Response$>" - "::Create(channel_.get(), cq, " - "rpcmethod_$Method$_, " - "context, request, $AsyncStart$$AsyncCreateArgs$);\n" - "}\n\n"); + printer->Print(*vars, + " return ::grpc::internal::ClientAsyncReaderFactory< " + "$Response$>" + "::Create(channel_.get(), cq, " + "rpcmethod_$Method$_, " + "context, request, $AsyncStart$$AsyncCreateArgs$);\n" + "}\n\n"); } } else if (method->BidiStreaming()) { printer->Print( @@ -2076,7 +2075,7 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer, "::grpc::ClientReaderWriter< $Request$, $Response$>* " "$ns$$Service$::Stub::$Method$Raw(::grpc::ClientContext* context) {\n"); printer->Print(*vars, - " return ::grpc_impl::internal::ClientReaderWriterFactory< " + " return ::grpc::internal::ClientReaderWriterFactory< " "$Request$, $Response$>::Create(" "channel_.get(), " "rpcmethod_$Method$_, " @@ -2089,14 +2088,13 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer, "ClientContext* context, " "::grpc::experimental::ClientBidiReactor< $Request$,$Response$>* " "reactor) {\n"); - printer->Print( - *vars, - " ::grpc_impl::internal::ClientCallbackReaderWriterFactory< " - "$Request$,$Response$>::Create(" - "stub_->channel_.get(), " - "stub_->rpcmethod_$Method$_, " - "context, reactor);\n" - "}\n\n"); + printer->Print(*vars, + " ::grpc::internal::ClientCallbackReaderWriterFactory< " + "$Request$,$Response$>::Create(" + "stub_->channel_.get(), " + "stub_->rpcmethod_$Method$_, " + "context, reactor);\n" + "}\n\n"); for (auto async_prefix : async_prefixes) { (*vars)["AsyncPrefix"] = async_prefix.prefix; @@ -2110,7 +2108,7 @@ void PrintSourceClientMethod(grpc_generator::Printer* printer, "::grpc::CompletionQueue* cq$AsyncMethodParams$) {\n"); printer->Print(*vars, " return " - "::grpc_impl::internal::ClientAsyncReaderWriterFactory< " + "::grpc::internal::ClientAsyncReaderWriterFactory< " "$Request$, $Response$>::Create(" "channel_.get(), cq, " "rpcmethod_$Method$_, " @@ -2272,7 +2270,7 @@ void PrintSourceService(grpc_generator::Printer* printer, "$ns$$Service$::Service, $Request$, $Response$>(\n" " []($ns$$Service$::Service* service,\n" " ::grpc::ServerContext* ctx,\n" - " ::grpc_impl::ServerReader<$Request$>* reader,\n" + " ::grpc::ServerReader<$Request$>* reader,\n" " $Response$* resp) {\n" " return service->$Method$(ctx, reader, resp);\n" " }, this)));\n"); @@ -2287,7 +2285,7 @@ void PrintSourceService(grpc_generator::Printer* printer, " []($ns$$Service$::Service* service,\n" " ::grpc::ServerContext* ctx,\n" " const $Request$* req,\n" - " ::grpc_impl::ServerWriter<$Response$>* writer) {\n" + " ::grpc::ServerWriter<$Response$>* writer) {\n" " return service->$Method$(ctx, req, writer);\n" " }, this)));\n"); } else if (method->BidiStreaming()) { @@ -2299,7 +2297,7 @@ void PrintSourceService(grpc_generator::Printer* printer, "$ns$$Service$::Service, $Request$, $Response$>(\n" " []($ns$$Service$::Service* service,\n" " ::grpc::ServerContext* ctx,\n" - " ::grpc_impl::ServerReaderWriter<$Response$,\n" + " ::grpc::ServerReaderWriter<$Response$,\n" " $Request$>* stream) {\n" " return service->$Method$(ctx, stream);\n" " }, this)));\n"); diff --git a/src/cpp/client/client_callback.cc b/src/cpp/client/client_callback.cc index ca325ba1dbf..f4cbc97d34b 100644 --- a/src/cpp/client/client_callback.cc +++ b/src/cpp/client/client_callback.cc @@ -15,13 +15,13 @@ * */ -#include +#include #include "src/core/lib/iomgr/closure.h" #include "src/core/lib/iomgr/exec_ctx.h" #include "src/core/lib/iomgr/executor.h" -namespace grpc_impl { +namespace grpc { namespace internal { void ClientReactor::InternalScheduleOnDone(grpc::Status s) { @@ -49,4 +49,4 @@ void ClientReactor::InternalScheduleOnDone(grpc::Status s) { } } // namespace internal -} // namespace grpc_impl +} // namespace grpc diff --git a/src/cpp/server/health/default_health_check_service.cc b/src/cpp/server/health/default_health_check_service.cc index 187e69166fc..405ce1c8ca3 100644 --- a/src/cpp/server/health/default_health_check_service.cc +++ b/src/cpp/server/health/default_health_check_service.cc @@ -23,7 +23,7 @@ #include #include #include -#include +#include #include "src/cpp/server/health/default_health_check_service.h" #include "src/proto/grpc/health/v1/health.upb.h" diff --git a/src/cpp/server/server_callback.cc b/src/cpp/server/server_callback.cc index c1620b721ac..40aef8e7359 100644 --- a/src/cpp/server/server_callback.cc +++ b/src/cpp/server/server_callback.cc @@ -15,13 +15,13 @@ * */ -#include +#include #include "src/core/lib/iomgr/closure.h" #include "src/core/lib/iomgr/exec_ctx.h" #include "src/core/lib/iomgr/executor.h" -namespace grpc_impl { +namespace grpc { namespace internal { void ServerCallbackCall::ScheduleOnDone(bool inline_ondone) { @@ -81,4 +81,4 @@ void ServerCallbackCall::CallOnCancel(ServerReactor* reactor) { } } // namespace internal -} // namespace grpc_impl +} // namespace grpc diff --git a/src/cpp/server/server_context.cc b/src/cpp/server/server_context.cc index f2e871be2b3..787a3fb4ad1 100644 --- a/src/cpp/server/server_context.cc +++ b/src/cpp/server/server_context.cc @@ -45,7 +45,7 @@ class ServerContextBase::CompletionOp final // initial refs: one in the server context, one in the cq // must ref the call before calling constructor and after deleting this CompletionOp(internal::Call* call, - ::grpc_impl::internal::ServerCallbackCall* callback_controller) + ::grpc::internal::ServerCallbackCall* callback_controller) : call_(*call), callback_controller_(callback_controller), has_tag_(false), @@ -137,7 +137,7 @@ class ServerContextBase::CompletionOp final } internal::Call call_; - ::grpc_impl::internal::ServerCallbackCall* const callback_controller_; + ::grpc::internal::ServerCallbackCall* const callback_controller_; bool has_tag_; void* tag_; void* core_cq_tag_; @@ -281,7 +281,7 @@ void ServerContextBase::Clear() { void ServerContextBase::BeginCompletionOp( internal::Call* call, std::function callback, - ::grpc_impl::internal::ServerCallbackCall* callback_controller) { + ::grpc::internal::ServerCallbackCall* callback_controller) { GPR_ASSERT(!completion_op_); if (rpc_info_) { rpc_info_->Ref(); diff --git a/test/cpp/codegen/compiler_test_golden b/test/cpp/codegen/compiler_test_golden index 3e93a33f087..dc22e1e1506 100644 --- a/test/cpp/codegen/compiler_test_golden +++ b/test/cpp/codegen/compiler_test_golden @@ -378,7 +378,7 @@ class ServiceA final { ::grpc::Service::experimental(). #endif MarkMethodCallback(0, - new ::grpc_impl::internal::CallbackUnaryHandler< ::grpc::testing::Request, ::grpc::testing::Response>( + new ::grpc::internal::CallbackUnaryHandler< ::grpc::testing::Request, ::grpc::testing::Response>( [this]( #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL ::grpc::CallbackServerContext* @@ -393,7 +393,7 @@ class ServiceA final { #else ::grpc::internal::MethodHandler* const handler = ::grpc::Service::experimental().GetHandler(0); #endif - static_cast<::grpc_impl::internal::CallbackUnaryHandler< ::grpc::testing::Request, ::grpc::testing::Response>*>(handler) + static_cast<::grpc::internal::CallbackUnaryHandler< ::grpc::testing::Request, ::grpc::testing::Response>*>(handler) ->SetMessageAllocator(allocator); } ~ExperimentalWithCallbackMethod_MethodA1() override { @@ -425,7 +425,7 @@ class ServiceA final { ::grpc::Service::experimental(). #endif MarkMethodCallback(1, - new ::grpc_impl::internal::CallbackClientStreamingHandler< ::grpc::testing::Request, ::grpc::testing::Response>( + new ::grpc::internal::CallbackClientStreamingHandler< ::grpc::testing::Request, ::grpc::testing::Response>( [this]( #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL ::grpc::CallbackServerContext* @@ -463,7 +463,7 @@ class ServiceA final { ::grpc::Service::experimental(). #endif MarkMethodCallback(2, - new ::grpc_impl::internal::CallbackServerStreamingHandler< ::grpc::testing::Request, ::grpc::testing::Response>( + new ::grpc::internal::CallbackServerStreamingHandler< ::grpc::testing::Request, ::grpc::testing::Response>( [this]( #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL ::grpc::CallbackServerContext* @@ -501,7 +501,7 @@ class ServiceA final { ::grpc::Service::experimental(). #endif MarkMethodCallback(3, - new ::grpc_impl::internal::CallbackBidiHandler< ::grpc::testing::Request, ::grpc::testing::Response>( + new ::grpc::internal::CallbackBidiHandler< ::grpc::testing::Request, ::grpc::testing::Response>( [this]( #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL ::grpc::CallbackServerContext* @@ -692,7 +692,7 @@ class ServiceA final { ::grpc::Service::experimental(). #endif MarkMethodRawCallback(0, - new ::grpc_impl::internal::CallbackUnaryHandler< ::grpc::ByteBuffer, ::grpc::ByteBuffer>( + new ::grpc::internal::CallbackUnaryHandler< ::grpc::ByteBuffer, ::grpc::ByteBuffer>( [this]( #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL ::grpc::CallbackServerContext* @@ -730,7 +730,7 @@ class ServiceA final { ::grpc::Service::experimental(). #endif MarkMethodRawCallback(1, - new ::grpc_impl::internal::CallbackClientStreamingHandler< ::grpc::ByteBuffer, ::grpc::ByteBuffer>( + new ::grpc::internal::CallbackClientStreamingHandler< ::grpc::ByteBuffer, ::grpc::ByteBuffer>( [this]( #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL ::grpc::CallbackServerContext* @@ -768,7 +768,7 @@ class ServiceA final { ::grpc::Service::experimental(). #endif MarkMethodRawCallback(2, - new ::grpc_impl::internal::CallbackServerStreamingHandler< ::grpc::ByteBuffer, ::grpc::ByteBuffer>( + new ::grpc::internal::CallbackServerStreamingHandler< ::grpc::ByteBuffer, ::grpc::ByteBuffer>( [this]( #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL ::grpc::CallbackServerContext* @@ -806,7 +806,7 @@ class ServiceA final { ::grpc::Service::experimental(). #endif MarkMethodRawCallback(3, - new ::grpc_impl::internal::CallbackBidiHandler< ::grpc::ByteBuffer, ::grpc::ByteBuffer>( + new ::grpc::internal::CallbackBidiHandler< ::grpc::ByteBuffer, ::grpc::ByteBuffer>( [this]( #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL ::grpc::CallbackServerContext* @@ -842,7 +842,7 @@ class ServiceA final { new ::grpc::internal::StreamedUnaryHandler< ::grpc::testing::Request, ::grpc::testing::Response>( [this](::grpc::ServerContext* context, - ::grpc_impl::ServerUnaryStreamer< + ::grpc::ServerUnaryStreamer< ::grpc::testing::Request, ::grpc::testing::Response>* streamer) { return this->StreamedMethodA1(context, streamer); @@ -870,7 +870,7 @@ class ServiceA final { new ::grpc::internal::SplitServerStreamingHandler< ::grpc::testing::Request, ::grpc::testing::Response>( [this](::grpc::ServerContext* context, - ::grpc_impl::ServerSplitStreamer< + ::grpc::ServerSplitStreamer< ::grpc::testing::Request, ::grpc::testing::Response>* streamer) { return this->StreamedMethodA3(context, streamer); @@ -1021,7 +1021,7 @@ class ServiceB final { ::grpc::Service::experimental(). #endif MarkMethodCallback(0, - new ::grpc_impl::internal::CallbackUnaryHandler< ::grpc::testing::Request, ::grpc::testing::Response>( + new ::grpc::internal::CallbackUnaryHandler< ::grpc::testing::Request, ::grpc::testing::Response>( [this]( #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL ::grpc::CallbackServerContext* @@ -1036,7 +1036,7 @@ class ServiceB final { #else ::grpc::internal::MethodHandler* const handler = ::grpc::Service::experimental().GetHandler(0); #endif - static_cast<::grpc_impl::internal::CallbackUnaryHandler< ::grpc::testing::Request, ::grpc::testing::Response>*>(handler) + static_cast<::grpc::internal::CallbackUnaryHandler< ::grpc::testing::Request, ::grpc::testing::Response>*>(handler) ->SetMessageAllocator(allocator); } ~ExperimentalWithCallbackMethod_MethodB1() override { @@ -1110,7 +1110,7 @@ class ServiceB final { ::grpc::Service::experimental(). #endif MarkMethodRawCallback(0, - new ::grpc_impl::internal::CallbackUnaryHandler< ::grpc::ByteBuffer, ::grpc::ByteBuffer>( + new ::grpc::internal::CallbackUnaryHandler< ::grpc::ByteBuffer, ::grpc::ByteBuffer>( [this]( #ifdef GRPC_CALLBACK_API_NONEXPERIMENTAL ::grpc::CallbackServerContext* @@ -1146,7 +1146,7 @@ class ServiceB final { new ::grpc::internal::StreamedUnaryHandler< ::grpc::testing::Request, ::grpc::testing::Response>( [this](::grpc::ServerContext* context, - ::grpc_impl::ServerUnaryStreamer< + ::grpc::ServerUnaryStreamer< ::grpc::testing::Request, ::grpc::testing::Response>* streamer) { return this->StreamedMethodB1(context, streamer); diff --git a/tools/doxygen/Doxyfile.c++ b/tools/doxygen/Doxyfile.c++ index 8c6cbb0e2a4..d9658f30be7 100644 --- a/tools/doxygen/Doxyfile.c++ +++ b/tools/doxygen/Doxyfile.c++ @@ -949,9 +949,7 @@ include/grpcpp/impl/channel_argument_option.h \ include/grpcpp/impl/client_unary_call.h \ include/grpcpp/impl/codegen/async_generic_service.h \ include/grpcpp/impl/codegen/async_stream.h \ -include/grpcpp/impl/codegen/async_stream_impl.h \ include/grpcpp/impl/codegen/async_unary_call.h \ -include/grpcpp/impl/codegen/async_unary_call_impl.h \ include/grpcpp/impl/codegen/byte_buffer.h \ include/grpcpp/impl/codegen/call.h \ include/grpcpp/impl/codegen/call_hook.h \ @@ -960,7 +958,6 @@ include/grpcpp/impl/codegen/call_op_set_interface.h \ include/grpcpp/impl/codegen/callback_common.h \ include/grpcpp/impl/codegen/channel_interface.h \ include/grpcpp/impl/codegen/client_callback.h \ -include/grpcpp/impl/codegen/client_callback_impl.h \ include/grpcpp/impl/codegen/client_context.h \ include/grpcpp/impl/codegen/client_interceptor.h \ include/grpcpp/impl/codegen/client_unary_call.h \ @@ -979,7 +976,6 @@ include/grpcpp/impl/codegen/interceptor_common.h \ include/grpcpp/impl/codegen/message_allocator.h \ include/grpcpp/impl/codegen/metadata_map.h \ include/grpcpp/impl/codegen/method_handler.h \ -include/grpcpp/impl/codegen/method_handler_impl.h \ include/grpcpp/impl/codegen/proto_buffer_reader.h \ include/grpcpp/impl/codegen/proto_buffer_writer.h \ include/grpcpp/impl/codegen/proto_utils.h \ @@ -989,7 +985,6 @@ include/grpcpp/impl/codegen/security/auth_context.h \ include/grpcpp/impl/codegen/serialization_traits.h \ include/grpcpp/impl/codegen/server_callback.h \ include/grpcpp/impl/codegen/server_callback_handlers.h \ -include/grpcpp/impl/codegen/server_callback_impl.h \ include/grpcpp/impl/codegen/server_context.h \ include/grpcpp/impl/codegen/server_interceptor.h \ include/grpcpp/impl/codegen/server_interface.h \ @@ -1001,7 +996,6 @@ include/grpcpp/impl/codegen/string_ref.h \ include/grpcpp/impl/codegen/stub_options.h \ include/grpcpp/impl/codegen/sync.h \ include/grpcpp/impl/codegen/sync_stream.h \ -include/grpcpp/impl/codegen/sync_stream_impl.h \ include/grpcpp/impl/codegen/time.h \ include/grpcpp/impl/grpc_library.h \ include/grpcpp/impl/method_handler_impl.h \ @@ -1025,13 +1019,10 @@ include/grpcpp/server_builder.h \ include/grpcpp/server_context.h \ include/grpcpp/server_posix.h \ include/grpcpp/support/async_stream.h \ -include/grpcpp/support/async_stream_impl.h \ include/grpcpp/support/async_unary_call.h \ -include/grpcpp/support/async_unary_call_impl.h \ include/grpcpp/support/byte_buffer.h \ include/grpcpp/support/channel_arguments.h \ include/grpcpp/support/client_callback.h \ -include/grpcpp/support/client_callback_impl.h \ include/grpcpp/support/client_interceptor.h \ include/grpcpp/support/config.h \ include/grpcpp/support/interceptor.h \ @@ -1040,7 +1031,6 @@ include/grpcpp/support/method_handler.h \ include/grpcpp/support/proto_buffer_reader.h \ include/grpcpp/support/proto_buffer_writer.h \ include/grpcpp/support/server_callback.h \ -include/grpcpp/support/server_callback_impl.h \ include/grpcpp/support/server_interceptor.h \ include/grpcpp/support/slice.h \ include/grpcpp/support/status.h \ @@ -1048,7 +1038,6 @@ include/grpcpp/support/status_code_enum.h \ include/grpcpp/support/string_ref.h \ include/grpcpp/support/stub_options.h \ include/grpcpp/support/sync_stream.h \ -include/grpcpp/support/sync_stream_impl.h \ include/grpcpp/support/time.h \ include/grpcpp/support/validate_service_config.h diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal index ad07812ca84..da554e3bd73 100644 --- a/tools/doxygen/Doxyfile.c++.internal +++ b/tools/doxygen/Doxyfile.c++.internal @@ -949,9 +949,7 @@ include/grpcpp/impl/channel_argument_option.h \ include/grpcpp/impl/client_unary_call.h \ include/grpcpp/impl/codegen/async_generic_service.h \ include/grpcpp/impl/codegen/async_stream.h \ -include/grpcpp/impl/codegen/async_stream_impl.h \ include/grpcpp/impl/codegen/async_unary_call.h \ -include/grpcpp/impl/codegen/async_unary_call_impl.h \ include/grpcpp/impl/codegen/byte_buffer.h \ include/grpcpp/impl/codegen/call.h \ include/grpcpp/impl/codegen/call_hook.h \ @@ -960,7 +958,6 @@ include/grpcpp/impl/codegen/call_op_set_interface.h \ include/grpcpp/impl/codegen/callback_common.h \ include/grpcpp/impl/codegen/channel_interface.h \ include/grpcpp/impl/codegen/client_callback.h \ -include/grpcpp/impl/codegen/client_callback_impl.h \ include/grpcpp/impl/codegen/client_context.h \ include/grpcpp/impl/codegen/client_interceptor.h \ include/grpcpp/impl/codegen/client_unary_call.h \ @@ -979,7 +976,6 @@ include/grpcpp/impl/codegen/interceptor_common.h \ include/grpcpp/impl/codegen/message_allocator.h \ include/grpcpp/impl/codegen/metadata_map.h \ include/grpcpp/impl/codegen/method_handler.h \ -include/grpcpp/impl/codegen/method_handler_impl.h \ include/grpcpp/impl/codegen/proto_buffer_reader.h \ include/grpcpp/impl/codegen/proto_buffer_writer.h \ include/grpcpp/impl/codegen/proto_utils.h \ @@ -989,7 +985,6 @@ include/grpcpp/impl/codegen/security/auth_context.h \ include/grpcpp/impl/codegen/serialization_traits.h \ include/grpcpp/impl/codegen/server_callback.h \ include/grpcpp/impl/codegen/server_callback_handlers.h \ -include/grpcpp/impl/codegen/server_callback_impl.h \ include/grpcpp/impl/codegen/server_context.h \ include/grpcpp/impl/codegen/server_interceptor.h \ include/grpcpp/impl/codegen/server_interface.h \ @@ -1001,7 +996,6 @@ include/grpcpp/impl/codegen/string_ref.h \ include/grpcpp/impl/codegen/stub_options.h \ include/grpcpp/impl/codegen/sync.h \ include/grpcpp/impl/codegen/sync_stream.h \ -include/grpcpp/impl/codegen/sync_stream_impl.h \ include/grpcpp/impl/codegen/time.h \ include/grpcpp/impl/grpc_library.h \ include/grpcpp/impl/method_handler_impl.h \ @@ -1025,13 +1019,10 @@ include/grpcpp/server_builder.h \ include/grpcpp/server_context.h \ include/grpcpp/server_posix.h \ include/grpcpp/support/async_stream.h \ -include/grpcpp/support/async_stream_impl.h \ include/grpcpp/support/async_unary_call.h \ -include/grpcpp/support/async_unary_call_impl.h \ include/grpcpp/support/byte_buffer.h \ include/grpcpp/support/channel_arguments.h \ include/grpcpp/support/client_callback.h \ -include/grpcpp/support/client_callback_impl.h \ include/grpcpp/support/client_interceptor.h \ include/grpcpp/support/config.h \ include/grpcpp/support/interceptor.h \ @@ -1040,7 +1031,6 @@ include/grpcpp/support/method_handler.h \ include/grpcpp/support/proto_buffer_reader.h \ include/grpcpp/support/proto_buffer_writer.h \ include/grpcpp/support/server_callback.h \ -include/grpcpp/support/server_callback_impl.h \ include/grpcpp/support/server_interceptor.h \ include/grpcpp/support/slice.h \ include/grpcpp/support/status.h \ @@ -1048,7 +1038,6 @@ include/grpcpp/support/status_code_enum.h \ include/grpcpp/support/string_ref.h \ include/grpcpp/support/stub_options.h \ include/grpcpp/support/sync_stream.h \ -include/grpcpp/support/sync_stream_impl.h \ include/grpcpp/support/time.h \ include/grpcpp/support/validate_service_config.h \ src/core/ext/filters/census/grpc_context.cc \