proto_utils.h 4.5 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. extern CoreCodegenInterface* g_core_codegen_interface;
  37. // ProtoBufferWriter must be a subclass of ::protobuf::io::ZeroCopyOutputStream.
  38. template <class ProtoBufferWriter, class T>
  39. Status GenericSerialize(const grpc::protobuf::MessageLite& msg, ByteBuffer* bb,
  40. bool* own_buffer) {
  41. static_assert(std::is_base_of<protobuf::io::ZeroCopyOutputStream,
  42. ProtoBufferWriter>::value,
  43. "ProtoBufferWriter must be a subclass of "
  44. "::protobuf::io::ZeroCopyOutputStream");
  45. *own_buffer = true;
  46. int byte_size = static_cast<int>(msg.ByteSizeLong());
  47. if (static_cast<size_t>(byte_size) <= GRPC_SLICE_INLINED_SIZE) {
  48. Slice slice(byte_size);
  49. // We serialize directly into the allocated slices memory
  50. GPR_CODEGEN_ASSERT(slice.end() == msg.SerializeWithCachedSizesToArray(
  51. const_cast<uint8_t*>(slice.begin())));
  52. ByteBuffer tmp(&slice, 1);
  53. bb->Swap(&tmp);
  54. return g_core_codegen_interface->ok();
  55. }
  56. ProtoBufferWriter writer(bb, kProtoBufferWriterMaxBufferLength, byte_size);
  57. return msg.SerializeToZeroCopyStream(&writer)
  58. ? g_core_codegen_interface->ok()
  59. : Status(StatusCode::INTERNAL, "Failed to serialize message");
  60. }
  61. // BufferReader must be a subclass of ::protobuf::io::ZeroCopyInputStream.
  62. template <class ProtoBufferReader, class T>
  63. Status GenericDeserialize(ByteBuffer* buffer,
  64. grpc::protobuf::MessageLite* msg) {
  65. static_assert(std::is_base_of<protobuf::io::ZeroCopyInputStream,
  66. ProtoBufferReader>::value,
  67. "ProtoBufferReader must be a subclass of "
  68. "::protobuf::io::ZeroCopyInputStream");
  69. if (buffer == nullptr) {
  70. return Status(StatusCode::INTERNAL, "No payload");
  71. }
  72. Status result = g_core_codegen_interface->ok();
  73. {
  74. ProtoBufferReader reader(buffer);
  75. if (!reader.status().ok()) {
  76. return reader.status();
  77. }
  78. if (!msg->ParseFromZeroCopyStream(&reader)) {
  79. result = Status(StatusCode::INTERNAL, msg->InitializationErrorString());
  80. }
  81. }
  82. buffer->Clear();
  83. return result;
  84. }
  85. // this is needed so the following class does not conflict with protobuf
  86. // serializers that utilize internal-only tools.
  87. #ifdef GRPC_OPEN_SOURCE_PROTO
  88. // This class provides a protobuf serializer. It translates between protobuf
  89. // objects and grpc_byte_buffers. More information about SerializationTraits can
  90. // be found in include/grpcpp/impl/codegen/serialization_traits.h.
  91. template <class T>
  92. class SerializationTraits<
  93. T, typename std::enable_if<
  94. std::is_base_of<grpc::protobuf::MessageLite, T>::value>::type> {
  95. public:
  96. static Status Serialize(const grpc::protobuf::MessageLite& msg,
  97. ByteBuffer* bb, bool* own_buffer) {
  98. return GenericSerialize<ProtoBufferWriter, T>(msg, bb, own_buffer);
  99. }
  100. static Status Deserialize(ByteBuffer* buffer,
  101. grpc::protobuf::MessageLite* msg) {
  102. return GenericDeserialize<ProtoBufferReader, T>(buffer, msg);
  103. }
  104. };
  105. #endif
  106. } // namespace grpc
  107. #endif // GRPCPP_IMPL_CODEGEN_PROTO_UTILS_H