GRPC C++  1.78.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 
20 #include <grpc/impl/grpc_types.h>
21 #include <grpc/slice.h>
27 #include <grpcpp/support/slice.h>
28 #include <grpcpp/support/status.h>
29 
30 #include <cstddef>
31 #include <limits>
32 #include <type_traits>
33 
34 #include "absl/log/absl_check.h"
35 #include "absl/strings/str_cat.h"
36 
39 
40 namespace grpc {
41 
42 // ProtoBufferWriter must be a subclass of ::protobuf::io::ZeroCopyOutputStream.
43 template <class ProtoBufferWriter, class T>
45  const grpc::protobuf::MessageLite& msg, ByteBuffer* bb, bool* own_buffer,
47  static_assert(std::is_base_of<protobuf::io::ZeroCopyOutputStream,
48  ProtoBufferWriter>::value,
49  "ProtoBufferWriter must be a subclass of "
50  "::protobuf::io::ZeroCopyOutputStream");
51  *own_buffer = true;
52  size_t byte_size = msg.ByteSizeLong();
53  if (byte_size > std::numeric_limits<int>::max()) {
55  "Protobuf is too large to be serialized",
56  absl::StrCat(byte_size, " bytes is beyond the limit 2^31-1"));
57  }
58  if (byte_size <= GRPC_SLICE_INLINED_SIZE) {
59  Slice slice(byte_size);
60  // We serialize directly into the allocated slices memory
61  ABSL_CHECK(slice.end() == msg.SerializeWithCachedSizesToArray(
62  const_cast<uint8_t*>(slice.begin())));
63  ByteBuffer tmp(&slice, 1);
64  bb->Swap(&tmp);
65 
66  return grpc::Status::OK;
67  }
69  static_cast<int>(byte_size), allocator);
71  msg.SerializeWithCachedSizes(&cs);
72  return !cs.HadError()
74  : Status(StatusCode::INTERNAL, "Failed to serialize message");
75 }
76 
77 // BufferReader must be a subclass of ::protobuf::io::ZeroCopyInputStream.
78 template <class ProtoBufferReader, class T>
81  static_assert(std::is_base_of<protobuf::io::ZeroCopyInputStream,
82  ProtoBufferReader>::value,
83  "ProtoBufferReader must be a subclass of "
84  "::protobuf::io::ZeroCopyInputStream");
85  if (buffer == nullptr) {
86  return Status(StatusCode::INTERNAL, "No payload");
87  }
88  Status result = grpc::Status::OK;
89  {
90  ProtoBufferReader reader(buffer);
91  if (!reader.status().ok()) {
92  return reader.status();
93  }
94  if (!msg->ParseFromZeroCopyStream(&reader)) {
95  result = Status(StatusCode::INTERNAL, msg->InitializationErrorString());
96  }
97  }
98  buffer->Clear();
99  return result;
100 }
101 
102 } // namespace grpc
103 
104 #endif // GRPCPP_IMPL_GENERIC_SERIALIZE_H
grpc::ProtoBufferWriter
This is a specialization of the protobuf class ZeroCopyOutputStream.
Definition: proto_buffer_writer.h:56
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::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:79
grpc::kProtoBufferWriterMaxBufferLength
const int kProtoBufferWriterMaxBufferLength
Definition: proto_buffer_writer.h:47
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
grpc_event_engine::experimental::MemoryAllocator
Definition: memory_allocator.h:33
memory_allocator.h
slice.h
grpc::GenericSerialize
Status GenericSerialize(const grpc::protobuf::MessageLite &msg, ByteBuffer *bb, bool *own_buffer, grpc_event_engine::experimental::MemoryAllocator *allocator=nullptr)
Definition: generic_serialize.h:44
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