diff --git a/BUILD b/BUILD index 1011dd4de6f..c8c84083a88 100644 --- a/BUILD +++ b/BUILD @@ -1858,6 +1858,7 @@ grpc_cc_library( "include/grpcpp/impl/codegen/proto_buffer_reader.h", "include/grpcpp/impl/codegen/proto_buffer_writer.h", "include/grpcpp/impl/codegen/proto_utils.h", + "include/grpcpp/impl/proto_utils.h", ], tags = ["nofixdeps"], visibility = ["@grpc:public"], diff --git a/CMakeLists.txt b/CMakeLists.txt index ae6bb82180e..440abc1cd4f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3460,6 +3460,7 @@ foreach(_hdr include/grpcpp/impl/interceptor_common.h include/grpcpp/impl/metadata_map.h include/grpcpp/impl/method_handler_impl.h + include/grpcpp/impl/proto_utils.h include/grpcpp/impl/rpc_method.h include/grpcpp/impl/rpc_service_method.h include/grpcpp/impl/serialization_traits.h @@ -4155,6 +4156,7 @@ foreach(_hdr include/grpcpp/impl/interceptor_common.h include/grpcpp/impl/metadata_map.h include/grpcpp/impl/method_handler_impl.h + include/grpcpp/impl/proto_utils.h include/grpcpp/impl/rpc_method.h include/grpcpp/impl/rpc_service_method.h include/grpcpp/impl/serialization_traits.h diff --git a/build_autogenerated.yaml b/build_autogenerated.yaml index 09aad0f37e5..a38cd258763 100644 --- a/build_autogenerated.yaml +++ b/build_autogenerated.yaml @@ -2833,6 +2833,7 @@ libs: - include/grpcpp/impl/interceptor_common.h - include/grpcpp/impl/metadata_map.h - include/grpcpp/impl/method_handler_impl.h + - include/grpcpp/impl/proto_utils.h - include/grpcpp/impl/rpc_method.h - include/grpcpp/impl/rpc_service_method.h - include/grpcpp/impl/serialization_traits.h @@ -3259,6 +3260,7 @@ libs: - include/grpcpp/impl/interceptor_common.h - include/grpcpp/impl/metadata_map.h - include/grpcpp/impl/method_handler_impl.h + - include/grpcpp/impl/proto_utils.h - include/grpcpp/impl/rpc_method.h - include/grpcpp/impl/rpc_service_method.h - include/grpcpp/impl/serialization_traits.h diff --git a/gRPC-C++.podspec b/gRPC-C++.podspec index da1491bdfbc..e15cb638a8b 100644 --- a/gRPC-C++.podspec +++ b/gRPC-C++.podspec @@ -154,6 +154,7 @@ Pod::Spec.new do |s| 'include/grpcpp/impl/interceptor_common.h', 'include/grpcpp/impl/metadata_map.h', 'include/grpcpp/impl/method_handler_impl.h', + 'include/grpcpp/impl/proto_utils.h', 'include/grpcpp/impl/rpc_method.h', 'include/grpcpp/impl/rpc_service_method.h', 'include/grpcpp/impl/serialization_traits.h', diff --git a/include/grpcpp/impl/codegen/proto_utils.h b/include/grpcpp/impl/codegen/proto_utils.h index 9f9eb77b532..3251cec6442 100644 --- a/include/grpcpp/impl/codegen/proto_utils.h +++ b/include/grpcpp/impl/codegen/proto_utils.h @@ -21,101 +21,7 @@ // IWYU pragma: private -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -/// This header provides serialization and deserialization between gRPC -/// messages serialized using protobuf and the C++ objects they represent. - -namespace grpc { - -extern CoreCodegenInterface* g_core_codegen_interface; - -// ProtoBufferWriter must be a subclass of ::protobuf::io::ZeroCopyOutputStream. -template -Status GenericSerialize(const grpc::protobuf::MessageLite& msg, ByteBuffer* bb, - bool* own_buffer) { - static_assert(std::is_base_of::value, - "ProtoBufferWriter must be a subclass of " - "::protobuf::io::ZeroCopyOutputStream"); - *own_buffer = true; - int byte_size = static_cast(msg.ByteSizeLong()); - if (static_cast(byte_size) <= GRPC_SLICE_INLINED_SIZE) { - Slice slice(byte_size); - // We serialize directly into the allocated slices memory - GPR_CODEGEN_ASSERT(slice.end() == msg.SerializeWithCachedSizesToArray( - const_cast(slice.begin()))); - ByteBuffer tmp(&slice, 1); - bb->Swap(&tmp); - - return g_core_codegen_interface->ok(); - } - ProtoBufferWriter writer(bb, kProtoBufferWriterMaxBufferLength, byte_size); - return msg.SerializeToZeroCopyStream(&writer) - ? g_core_codegen_interface->ok() - : Status(StatusCode::INTERNAL, "Failed to serialize message"); -} - -// BufferReader must be a subclass of ::protobuf::io::ZeroCopyInputStream. -template -Status GenericDeserialize(ByteBuffer* buffer, - grpc::protobuf::MessageLite* msg) { - static_assert(std::is_base_of::value, - "ProtoBufferReader must be a subclass of " - "::protobuf::io::ZeroCopyInputStream"); - if (buffer == nullptr) { - return Status(StatusCode::INTERNAL, "No payload"); - } - Status result = g_core_codegen_interface->ok(); - { - ProtoBufferReader reader(buffer); - if (!reader.status().ok()) { - return reader.status(); - } - if (!msg->ParseFromZeroCopyStream(&reader)) { - result = Status(StatusCode::INTERNAL, msg->InitializationErrorString()); - } - } - buffer->Clear(); - return result; -} - -// this is needed so the following class does not conflict with protobuf -// serializers that utilize internal-only tools. -#ifdef GRPC_OPEN_SOURCE_PROTO -// This class provides a protobuf serializer. It translates between protobuf -// objects and grpc_byte_buffers. More information about SerializationTraits can -// be found in include/grpcpp/impl/codegen/serialization_traits.h. -template -class SerializationTraits< - T, typename std::enable_if< - std::is_base_of::value>::type> { - public: - static Status Serialize(const grpc::protobuf::MessageLite& msg, - ByteBuffer* bb, bool* own_buffer) { - return GenericSerialize(msg, bb, own_buffer); - } - - static Status Deserialize(ByteBuffer* buffer, - grpc::protobuf::MessageLite* msg) { - return GenericDeserialize(buffer, msg); - } -}; -#endif - -} // namespace grpc +/// TODO(chengyuc): Remove this file after solving compatibility. +#include #endif // GRPCPP_IMPL_CODEGEN_PROTO_UTILS_H diff --git a/include/grpcpp/impl/proto_utils.h b/include/grpcpp/impl/proto_utils.h new file mode 100644 index 00000000000..46a2405c205 --- /dev/null +++ b/include/grpcpp/impl/proto_utils.h @@ -0,0 +1,119 @@ +/* + * + * 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_PROTO_UTILS_H +#define GRPCPP_IMPL_PROTO_UTILS_H + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/// This header provides serialization and deserialization between gRPC +/// messages serialized using protobuf and the C++ objects they represent. + +namespace grpc { + +extern CoreCodegenInterface* g_core_codegen_interface; + +// ProtoBufferWriter must be a subclass of ::protobuf::io::ZeroCopyOutputStream. +template +Status GenericSerialize(const grpc::protobuf::MessageLite& msg, ByteBuffer* bb, + bool* own_buffer) { + static_assert(std::is_base_of::value, + "ProtoBufferWriter must be a subclass of " + "::protobuf::io::ZeroCopyOutputStream"); + *own_buffer = true; + int byte_size = static_cast(msg.ByteSizeLong()); + if (static_cast(byte_size) <= GRPC_SLICE_INLINED_SIZE) { + Slice slice(byte_size); + // We serialize directly into the allocated slices memory + GPR_CODEGEN_ASSERT(slice.end() == msg.SerializeWithCachedSizesToArray( + const_cast(slice.begin()))); + ByteBuffer tmp(&slice, 1); + bb->Swap(&tmp); + + return g_core_codegen_interface->ok(); + } + ProtoBufferWriter writer(bb, kProtoBufferWriterMaxBufferLength, byte_size); + return msg.SerializeToZeroCopyStream(&writer) + ? g_core_codegen_interface->ok() + : Status(StatusCode::INTERNAL, "Failed to serialize message"); +} + +// BufferReader must be a subclass of ::protobuf::io::ZeroCopyInputStream. +template +Status GenericDeserialize(ByteBuffer* buffer, + grpc::protobuf::MessageLite* msg) { + static_assert(std::is_base_of::value, + "ProtoBufferReader must be a subclass of " + "::protobuf::io::ZeroCopyInputStream"); + if (buffer == nullptr) { + return Status(StatusCode::INTERNAL, "No payload"); + } + Status result = g_core_codegen_interface->ok(); + { + ProtoBufferReader reader(buffer); + if (!reader.status().ok()) { + return reader.status(); + } + if (!msg->ParseFromZeroCopyStream(&reader)) { + result = Status(StatusCode::INTERNAL, msg->InitializationErrorString()); + } + } + buffer->Clear(); + return result; +} + +// this is needed so the following class does not conflict with protobuf +// serializers that utilize internal-only tools. +#ifdef GRPC_OPEN_SOURCE_PROTO +// This class provides a protobuf serializer. It translates between protobuf +// objects and grpc_byte_buffers. More information about SerializationTraits can +// be found in include/grpcpp/impl/codegen/serialization_traits.h. +template +class SerializationTraits< + T, typename std::enable_if< + std::is_base_of::value>::type> { + public: + static Status Serialize(const grpc::protobuf::MessageLite& msg, + ByteBuffer* bb, bool* own_buffer) { + return GenericSerialize(msg, bb, own_buffer); + } + + static Status Deserialize(ByteBuffer* buffer, + grpc::protobuf::MessageLite* msg) { + return GenericDeserialize(buffer, msg); + } +}; +#endif + +} // namespace grpc + +#endif // GRPCPP_IMPL_PROTO_UTILS_H diff --git a/src/compiler/cpp_generator.cc b/src/compiler/cpp_generator.cc index e463fef4e64..5b25a16b0ef 100644 --- a/src/compiler/cpp_generator.cc +++ b/src/compiler/cpp_generator.cc @@ -145,7 +145,7 @@ std::string GetHeaderIncludes(grpc_generator::File* file, "grpcpp/completion_queue.h", "grpcpp/support/message_allocator.h", "grpcpp/support/method_handler.h", - "grpcpp/impl/codegen/proto_utils.h", + "grpcpp/impl/proto_utils.h", "grpcpp/impl/rpc_method.h", "grpcpp/support/server_callback.h", "grpcpp/impl/server_callback_handlers.h", diff --git a/test/cpp/codegen/compiler_test_golden b/test/cpp/codegen/compiler_test_golden index 765b8bccfc4..303952be875 100644 --- a/test/cpp/codegen/compiler_test_golden +++ b/test/cpp/codegen/compiler_test_golden @@ -35,7 +35,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/test/cpp/codegen/proto_utils_test.cc b/test/cpp/codegen/proto_utils_test.cc index 5af64968717..ffb160a2e45 100644 --- a/test/cpp/codegen/proto_utils_test.cc +++ b/test/cpp/codegen/proto_utils_test.cc @@ -20,8 +20,8 @@ #include #include -#include #include +#include #include "test/core/util/test_config.h" diff --git a/test/cpp/end2end/client_callback_end2end_test.cc b/test/cpp/end2end/client_callback_end2end_test.cc index bed2dd37336..a783622bb51 100644 --- a/test/cpp/end2end/client_callback_end2end_test.cc +++ b/test/cpp/end2end/client_callback_end2end_test.cc @@ -31,7 +31,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/test/cpp/end2end/client_interceptors_end2end_test.cc b/test/cpp/end2end/client_interceptors_end2end_test.cc index a2c995bbd97..635f6217b4c 100644 --- a/test/cpp/end2end/client_interceptors_end2end_test.cc +++ b/test/cpp/end2end/client_interceptors_end2end_test.cc @@ -28,7 +28,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/test/cpp/end2end/delegating_channel_test.cc b/test/cpp/end2end/delegating_channel_test.cc index deb47dacf4a..2a8fadd90ac 100644 --- a/test/cpp/end2end/delegating_channel_test.cc +++ b/test/cpp/end2end/delegating_channel_test.cc @@ -25,8 +25,8 @@ #include #include #include -#include #include +#include #include #include #include diff --git a/test/cpp/end2end/filter_end2end_test.cc b/test/cpp/end2end/filter_end2end_test.cc index 4ca68aef67a..a9c6bb8c55c 100644 --- a/test/cpp/end2end/filter_end2end_test.cc +++ b/test/cpp/end2end/filter_end2end_test.cc @@ -31,7 +31,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/test/cpp/end2end/generic_end2end_test.cc b/test/cpp/end2end/generic_end2end_test.cc index d74dda6ae06..4c11ef2ce73 100644 --- a/test/cpp/end2end/generic_end2end_test.cc +++ b/test/cpp/end2end/generic_end2end_test.cc @@ -30,7 +30,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/test/cpp/end2end/server_interceptors_end2end_test.cc b/test/cpp/end2end/server_interceptors_end2end_test.cc index 1a0ec8f3fe5..5611aec13e5 100644 --- a/test/cpp/end2end/server_interceptors_end2end_test.cc +++ b/test/cpp/end2end/server_interceptors_end2end_test.cc @@ -28,7 +28,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/tools/doxygen/Doxyfile.c++ b/tools/doxygen/Doxyfile.c++ index ec092834bcf..b16d94a9a98 100644 --- a/tools/doxygen/Doxyfile.c++ +++ b/tools/doxygen/Doxyfile.c++ @@ -1019,6 +1019,7 @@ include/grpcpp/impl/intercepted_channel.h \ include/grpcpp/impl/interceptor_common.h \ include/grpcpp/impl/metadata_map.h \ include/grpcpp/impl/method_handler_impl.h \ +include/grpcpp/impl/proto_utils.h \ include/grpcpp/impl/rpc_method.h \ include/grpcpp/impl/rpc_service_method.h \ include/grpcpp/impl/serialization_traits.h \ diff --git a/tools/doxygen/Doxyfile.c++.internal b/tools/doxygen/Doxyfile.c++.internal index bfd18171dfc..5908488b828 100644 --- a/tools/doxygen/Doxyfile.c++.internal +++ b/tools/doxygen/Doxyfile.c++.internal @@ -1019,6 +1019,7 @@ include/grpcpp/impl/intercepted_channel.h \ include/grpcpp/impl/interceptor_common.h \ include/grpcpp/impl/metadata_map.h \ include/grpcpp/impl/method_handler_impl.h \ +include/grpcpp/impl/proto_utils.h \ include/grpcpp/impl/rpc_method.h \ include/grpcpp/impl/rpc_service_method.h \ include/grpcpp/impl/serialization_traits.h \