xref: /aosp_15_r20/external/grpc-grpc/include/grpcpp/impl/proto_utils.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 
19 #ifndef GRPCPP_IMPL_PROTO_UTILS_H
20 #define GRPCPP_IMPL_PROTO_UTILS_H
21 
22 #include <type_traits>
23 
24 #include <grpc/byte_buffer_reader.h>
25 #include <grpc/impl/grpc_types.h>
26 #include <grpc/slice.h>
27 #include <grpc/support/log.h>
28 #include <grpcpp/impl/codegen/config_protobuf.h>
29 #include <grpcpp/impl/serialization_traits.h>
30 #include <grpcpp/support/byte_buffer.h>
31 #include <grpcpp/support/proto_buffer_reader.h>
32 #include <grpcpp/support/proto_buffer_writer.h>
33 #include <grpcpp/support/slice.h>
34 #include <grpcpp/support/status.h>
35 
36 /// This header provides serialization and deserialization between gRPC
37 /// messages serialized using protobuf and the C++ objects they represent.
38 
39 namespace grpc {
40 
41 // ProtoBufferWriter must be a subclass of ::protobuf::io::ZeroCopyOutputStream.
42 template <class ProtoBufferWriter, class T>
GenericSerialize(const grpc::protobuf::MessageLite & msg,ByteBuffer * bb,bool * own_buffer)43 Status GenericSerialize(const grpc::protobuf::MessageLite& msg, ByteBuffer* bb,
44                         bool* own_buffer) {
45   static_assert(std::is_base_of<protobuf::io::ZeroCopyOutputStream,
46                                 ProtoBufferWriter>::value,
47                 "ProtoBufferWriter must be a subclass of "
48                 "::protobuf::io::ZeroCopyOutputStream");
49   *own_buffer = true;
50   int byte_size = static_cast<int>(msg.ByteSizeLong());
51   if (static_cast<size_t>(byte_size) <= GRPC_SLICE_INLINED_SIZE) {
52     Slice slice(byte_size);
53     // We serialize directly into the allocated slices memory
54     GPR_ASSERT(slice.end() == msg.SerializeWithCachedSizesToArray(
55                                   const_cast<uint8_t*>(slice.begin())));
56     ByteBuffer tmp(&slice, 1);
57     bb->Swap(&tmp);
58 
59     return grpc::Status::OK;
60   }
61   ProtoBufferWriter writer(bb, kProtoBufferWriterMaxBufferLength, byte_size);
62   return msg.SerializeToZeroCopyStream(&writer)
63              ? grpc::Status::OK
64              : Status(StatusCode::INTERNAL, "Failed to serialize message");
65 }
66 
67 // BufferReader must be a subclass of ::protobuf::io::ZeroCopyInputStream.
68 template <class ProtoBufferReader, class T>
GenericDeserialize(ByteBuffer * buffer,grpc::protobuf::MessageLite * msg)69 Status GenericDeserialize(ByteBuffer* buffer,
70                           grpc::protobuf::MessageLite* msg) {
71   static_assert(std::is_base_of<protobuf::io::ZeroCopyInputStream,
72                                 ProtoBufferReader>::value,
73                 "ProtoBufferReader must be a subclass of "
74                 "::protobuf::io::ZeroCopyInputStream");
75   if (buffer == nullptr) {
76     return Status(StatusCode::INTERNAL, "No payload");
77   }
78   Status result = grpc::Status::OK;
79   {
80     ProtoBufferReader reader(buffer);
81     if (!reader.status().ok()) {
82       return reader.status();
83     }
84     if (!msg->ParseFromZeroCopyStream(&reader)) {
85       result = Status(StatusCode::INTERNAL, msg->InitializationErrorString());
86     }
87   }
88   buffer->Clear();
89   return result;
90 }
91 
92 // this is needed so the following class does not conflict with protobuf
93 // serializers that utilize internal-only tools.
94 #ifdef GRPC_OPEN_SOURCE_PROTO
95 // This class provides a protobuf serializer. It translates between protobuf
96 // objects and grpc_byte_buffers. More information about SerializationTraits can
97 // be found in include/grpcpp/impl/codegen/serialization_traits.h.
98 template <class T>
99 class SerializationTraits<
100     T, typename std::enable_if<
101            std::is_base_of<grpc::protobuf::MessageLite, T>::value>::type> {
102  public:
Serialize(const grpc::protobuf::MessageLite & msg,ByteBuffer * bb,bool * own_buffer)103   static Status Serialize(const grpc::protobuf::MessageLite& msg,
104                           ByteBuffer* bb, bool* own_buffer) {
105     return GenericSerialize<ProtoBufferWriter, T>(msg, bb, own_buffer);
106   }
107 
Deserialize(ByteBuffer * buffer,grpc::protobuf::MessageLite * msg)108   static Status Deserialize(ByteBuffer* buffer,
109                             grpc::protobuf::MessageLite* msg) {
110     return GenericDeserialize<ProtoBufferReader, T>(buffer, msg);
111   }
112 };
113 #endif
114 
115 }  // namespace grpc
116 
117 #endif  // GRPCPP_IMPL_PROTO_UTILS_H
118