Debian Codegraph @

include/grpcpp/impl/codegen/proto_utils.h

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