You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

proto_utils.h 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #ifndef GRPCPP_IMPL_CODEGEN_PROTO_UTILS_H
  19. #define GRPCPP_IMPL_CODEGEN_PROTO_UTILS_H
  20. // IWYU pragma: private
  21. #include <type_traits>
  22. #include <grpc/impl/codegen/byte_buffer_reader.h>
  23. #include <grpc/impl/codegen/grpc_types.h>
  24. #include <grpc/impl/codegen/slice.h>
  25. #include <grpcpp/impl/codegen/byte_buffer.h>
  26. #include <grpcpp/impl/codegen/config_protobuf.h>
  27. #include <grpcpp/impl/codegen/core_codegen_interface.h>
  28. #include <grpcpp/impl/codegen/proto_buffer_reader.h>
  29. #include <grpcpp/impl/codegen/proto_buffer_writer.h>
  30. #include <grpcpp/impl/codegen/serialization_traits.h>
  31. #include <grpcpp/impl/codegen/slice.h>
  32. #include <grpcpp/impl/codegen/status.h>
  33. /// This header provides serialization and deserialization between gRPC
  34. /// messages serialized using protobuf and the C++ objects they represent.
  35. namespace grpc
  36. {
  37. extern CoreCodegenInterface* g_core_codegen_interface;
  38. // ProtoBufferWriter must be a subclass of ::protobuf::io::ZeroCopyOutputStream.
  39. template<class ProtoBufferWriter, class T>
  40. Status GenericSerialize(const grpc::protobuf::MessageLite& msg, ByteBuffer* bb, bool* own_buffer)
  41. {
  42. static_assert(std::is_base_of<protobuf::io::ZeroCopyOutputStream, ProtoBufferWriter>::value, "ProtoBufferWriter must be a subclass of "
  43. "::protobuf::io::ZeroCopyOutputStream");
  44. *own_buffer = true;
  45. int byte_size = static_cast<int>(msg.ByteSizeLong());
  46. if (static_cast<size_t>(byte_size) <= GRPC_SLICE_INLINED_SIZE)
  47. {
  48. Slice slice(byte_size);
  49. // We serialize directly into the allocated slices memory
  50. GPR_CODEGEN_ASSERT(slice.end() == msg.SerializeWithCachedSizesToArray(const_cast<uint8_t*>(slice.begin())));
  51. ByteBuffer tmp(&slice, 1);
  52. bb->Swap(&tmp);
  53. return g_core_codegen_interface->ok();
  54. }
  55. ProtoBufferWriter writer(bb, kProtoBufferWriterMaxBufferLength, byte_size);
  56. return msg.SerializeToZeroCopyStream(&writer) ? g_core_codegen_interface->ok() : Status(StatusCode::INTERNAL, "Failed to serialize message");
  57. }
  58. // BufferReader must be a subclass of ::protobuf::io::ZeroCopyInputStream.
  59. template<class ProtoBufferReader, class T>
  60. Status GenericDeserialize(ByteBuffer* buffer, grpc::protobuf::MessageLite* msg)
  61. {
  62. static_assert(std::is_base_of<protobuf::io::ZeroCopyInputStream, ProtoBufferReader>::value, "ProtoBufferReader must be a subclass of "
  63. "::protobuf::io::ZeroCopyInputStream");
  64. if (buffer == nullptr)
  65. {
  66. return Status(StatusCode::INTERNAL, "No payload");
  67. }
  68. Status result = g_core_codegen_interface->ok();
  69. {
  70. ProtoBufferReader reader(buffer);
  71. if (!reader.status().ok())
  72. {
  73. return reader.status();
  74. }
  75. if (!msg->ParseFromZeroCopyStream(&reader))
  76. {
  77. result = Status(StatusCode::INTERNAL, msg->InitializationErrorString());
  78. }
  79. }
  80. buffer->Clear();
  81. return result;
  82. }
  83. // this is needed so the following class does not conflict with protobuf
  84. // serializers that utilize internal-only tools.
  85. #ifdef GRPC_OPEN_SOURCE_PROTO
  86. // This class provides a protobuf serializer. It translates between protobuf
  87. // objects and grpc_byte_buffers. More information about SerializationTraits can
  88. // be found in include/grpcpp/impl/codegen/serialization_traits.h.
  89. template<class T>
  90. class SerializationTraits<
  91. T,
  92. typename std::enable_if<
  93. std::is_base_of<grpc::protobuf::MessageLite, T>::value>::type>
  94. {
  95. public:
  96. static Status Serialize(const grpc::protobuf::MessageLite& msg, ByteBuffer* bb, bool* own_buffer)
  97. {
  98. return GenericSerialize<ProtoBufferWriter, T>(msg, bb, own_buffer);
  99. }
  100. static Status Deserialize(ByteBuffer* buffer, grpc::protobuf::MessageLite* msg)
  101. {
  102. return GenericDeserialize<ProtoBufferReader, T>(buffer, msg);
  103. }
  104. };
  105. #endif
  106. } // namespace grpc
  107. #endif // GRPCPP_IMPL_CODEGEN_PROTO_UTILS_H