GRPC C++  1.70.1
generic_serialize.h
Go to the documentation of this file.
1 // Copyright 2024 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GRPCPP_IMPL_GENERIC_SERIALIZE_H
16 #define GRPCPP_IMPL_GENERIC_SERIALIZE_H
17 
19 #include <grpc/impl/grpc_types.h>
20 #include <grpc/slice.h>
26 #include <grpcpp/support/slice.h>
27 #include <grpcpp/support/status.h>
28 
29 #include <type_traits>
30 
31 #include "absl/log/absl_check.h"
32 
35 
36 namespace grpc {
37 
38 // ProtoBufferWriter must be a subclass of ::protobuf::io::ZeroCopyOutputStream.
39 template <class ProtoBufferWriter, class T>
41  bool* own_buffer) {
42  static_assert(std::is_base_of<protobuf::io::ZeroCopyOutputStream,
43  ProtoBufferWriter>::value,
44  "ProtoBufferWriter must be a subclass of "
45  "::protobuf::io::ZeroCopyOutputStream");
46  *own_buffer = true;
47  int byte_size = static_cast<int>(msg.ByteSizeLong());
48  if (static_cast<size_t>(byte_size) <= GRPC_SLICE_INLINED_SIZE) {
49  Slice slice(byte_size);
50  // We serialize directly into the allocated slices memory
51  ABSL_CHECK(slice.end() == msg.SerializeWithCachedSizesToArray(
52  const_cast<uint8_t*>(slice.begin())));
53  ByteBuffer tmp(&slice, 1);
54  bb->Swap(&tmp);
55 
56  return grpc::Status::OK;
57  }
60  msg.SerializeWithCachedSizes(&cs);
61  return !cs.HadError()
63  : Status(StatusCode::INTERNAL, "Failed to serialize message");
64 }
65 
66 // BufferReader must be a subclass of ::protobuf::io::ZeroCopyInputStream.
67 template <class ProtoBufferReader, class T>
70  static_assert(std::is_base_of<protobuf::io::ZeroCopyInputStream,
71  ProtoBufferReader>::value,
72  "ProtoBufferReader must be a subclass of "
73  "::protobuf::io::ZeroCopyInputStream");
74  if (buffer == nullptr) {
75  return Status(StatusCode::INTERNAL, "No payload");
76  }
77  Status result = grpc::Status::OK;
78  {
79  ProtoBufferReader reader(buffer);
80  if (!reader.status().ok()) {
81  return reader.status();
82  }
83  if (!msg->ParseFromZeroCopyStream(&reader)) {
84  result = Status(StatusCode::INTERNAL, msg->InitializationErrorString());
85  }
86  }
87  buffer->Clear();
88  return result;
89 }
90 
91 } // namespace grpc
92 
93 #endif // GRPCPP_IMPL_GENERIC_SERIALIZE_H
grpc::ProtoBufferWriter
This is a specialization of the protobuf class ZeroCopyOutputStream.
Definition: proto_buffer_writer.h:55
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
grpc::protobuf::io::ZeroCopyOutputStream
::google::protobuf::io::ZeroCopyOutputStream ZeroCopyOutputStream
Definition: config_protobuf.h:114
grpc::Slice::begin
const uint8_t * begin() const
Raw pointer to the beginning (first element) of the slice.
Definition: slice.h:102
grpc::protobuf::io::ZeroCopyInputStream
::google::protobuf::io::ZeroCopyInputStream ZeroCopyInputStream
Definition: config_protobuf.h:115
status.h
grpc::Status::ok
bool ok() const
Is the status OK?
Definition: status.h:125
proto_buffer_reader.h
grpc::Slice::end
const uint8_t * end() const
Raw pointer to the end (one byte past the last element) of the slice.
Definition: slice.h:105
byte_buffer_reader.h
grpc_types.h
grpc::GenericSerialize
Status GenericSerialize(const grpc::protobuf::MessageLite &msg, ByteBuffer *bb, bool *own_buffer)
Definition: generic_serialize.h:40
grpc::Status
Did it work? If it didn't, why?
Definition: status.h:34
grpc::ByteBuffer::Clear
void Clear()
Remove all data.
Definition: byte_buffer.h:127
grpc::ByteBuffer
A sequence of bytes.
Definition: byte_buffer.h:59
grpc::ByteBuffer::Swap
void Swap(ByteBuffer *other)
Swap the state of *this and *other.
Definition: byte_buffer.h:151
grpc::ProtoBufferReader
This is a specialization of the protobuf class ZeroCopyInputStream The principle is to get one chunk ...
Definition: proto_buffer_reader.h:47
proto_buffer_writer.h
grpc::GenericDeserialize
Status GenericDeserialize(ByteBuffer *buffer, grpc::protobuf::MessageLite *msg)
Definition: generic_serialize.h:68
grpc::kProtoBufferWriterMaxBufferLength
const int kProtoBufferWriterMaxBufferLength
Definition: proto_buffer_writer.h:46
grpc::Slice
A wrapper around grpc_slice.
Definition: slice.h:33
grpc::protobuf::io::CodedOutputStream
::google::protobuf::io::CodedOutputStream CodedOutputStream
Definition: config_protobuf.h:117
grpc::ProtoBufferReader::status
Status status() const
Returns the status of the buffer reader.
Definition: proto_buffer_reader.h:95
serialization_traits.h
grpc::Status::OK
static const Status & OK
An OK pre-defined instance.
Definition: status.h:112
config_protobuf.h
slice.h
byte_buffer.h
slice.h
grpc::protobuf::util::Status
::absl::Status Status
Definition: config_protobuf.h:107
GRPC_SLICE_INLINED_SIZE
#define GRPC_SLICE_INLINED_SIZE
Definition: slice_type.h:46
grpc::protobuf::MessageLite
::google::protobuf::MessageLite MessageLite
Definition: config_protobuf.h:90
grpc::INTERNAL
@ INTERNAL
Internal errors.
Definition: status_code_enum.h:121