GRPC C++  1.74.0
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 <cstddef>
30 #include <limits>
31 #include <type_traits>
32 
33 #include "absl/log/absl_check.h"
34 #include "absl/strings/str_cat.h"
35 
38 
39 namespace grpc {
40 
41 // ProtoBufferWriter must be a subclass of ::protobuf::io::ZeroCopyOutputStream.
42 template <class ProtoBufferWriter, class T>
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  size_t byte_size = msg.ByteSizeLong();
51  if (byte_size > std::numeric_limits<int>::max()) {
53  "Protobuf is too large to be serialized",
54  absl::StrCat(byte_size, " bytes is beyond the limit 2^31-1"));
55  }
56  if (byte_size <= GRPC_SLICE_INLINED_SIZE) {
57  Slice slice(byte_size);
58  // We serialize directly into the allocated slices memory
59  ABSL_CHECK(slice.end() == msg.SerializeWithCachedSizesToArray(
60  const_cast<uint8_t*>(slice.begin())));
61  ByteBuffer tmp(&slice, 1);
62  bb->Swap(&tmp);
63 
64  return grpc::Status::OK;
65  }
67  static_cast<int>(byte_size));
69  msg.SerializeWithCachedSizes(&cs);
70  return !cs.HadError()
72  : Status(StatusCode::INTERNAL, "Failed to serialize message");
73 }
74 
75 // BufferReader must be a subclass of ::protobuf::io::ZeroCopyInputStream.
76 template <class ProtoBufferReader, class T>
79  static_assert(std::is_base_of<protobuf::io::ZeroCopyInputStream,
80  ProtoBufferReader>::value,
81  "ProtoBufferReader must be a subclass of "
82  "::protobuf::io::ZeroCopyInputStream");
83  if (buffer == nullptr) {
84  return Status(StatusCode::INTERNAL, "No payload");
85  }
86  Status result = grpc::Status::OK;
87  {
88  ProtoBufferReader reader(buffer);
89  if (!reader.status().ok()) {
90  return reader.status();
91  }
92  if (!msg->ParseFromZeroCopyStream(&reader)) {
93  result = Status(StatusCode::INTERNAL, msg->InitializationErrorString());
94  }
95  }
96  buffer->Clear();
97  return result;
98 }
99 
100 } // namespace grpc
101 
102 #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:43
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:77
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