Cleanup gRPC's protobuf usage within Google

PiperOrigin-RevId: 694646432
pull/38061/head
Craig Tiller 2 weeks ago committed by Copybara-Service
parent 5fcc5f8a43
commit da58cffb04
  1. 2
      BUILD
  2. 1
      CMakeLists.txt
  3. 1
      build_autogenerated.yaml
  4. 16
      include/grpcpp/impl/codegen/config_protobuf.h
  5. 93
      include/grpcpp/impl/generic_serialize.h
  6. 60
      include/grpcpp/impl/proto_utils.h

@ -2472,6 +2472,7 @@ grpc_cc_library(
grpc_cc_library(
name = "grpc++_codegen_proto",
hdrs = ["include/grpcpp/impl/generic_serialize.h"],
external_deps = [
"absl/strings:cord",
"protobuf_headers",
@ -2497,6 +2498,7 @@ grpc_cc_library(
grpc_cc_library(
name = "grpc++_config_proto",
external_deps = [
"absl/status",
"protobuf_headers",
"protobuf",
],

1
CMakeLists.txt generated

@ -6089,6 +6089,7 @@ target_include_directories(grpc_plugin_support
)
target_link_libraries(grpc_plugin_support
${_gRPC_ALLTARGETS_LIBRARIES}
absl::status
${_gRPC_PROTOBUF_LIBRARIES}
${_gRPC_PROTOBUF_PROTOC_LIBRARIES}
)

@ -5111,6 +5111,7 @@ libs:
- src/compiler/python_generator.cc
- src/compiler/ruby_generator.cc
deps:
- absl/status:status
- protobuf
- protoc
- name: grpcpp_channelz

@ -21,7 +21,7 @@
// IWYU pragma: private
#define GRPC_OPEN_SOURCE_PROTO
// #define GRPC_OPEN_SOURCE_PROTO
#define GRPC_PROTOBUF_CORD_SUPPORT_ENABLED
@ -40,7 +40,7 @@
#ifndef GRPC_CUSTOM_DESCRIPTOR
#include <google/protobuf/descriptor.h>
#include <google/protobuf/descriptor.pb.h>
#if GOOGLE_PROTOBUF_VERSION >= 4025000
#if !defined(GOOGLE_PROTOBUF_VERSION) || GOOGLE_PROTOBUF_VERSION >= 4025000
#define GRPC_PROTOBUF_EDITION_SUPPORT
#endif
#define GRPC_CUSTOM_DESCRIPTOR ::google::protobuf::Descriptor
@ -59,26 +59,22 @@
#ifndef GRPC_CUSTOM_DESCRIPTORDATABASE
#include <google/protobuf/descriptor_database.h>
#define GRPC_CUSTOM_DESCRIPTORDATABASE ::google::protobuf::DescriptorDatabase
#define GRPC_CUSTOM_SIMPLEDESCRIPTORDATABASE \
::google::protobuf::SimpleDescriptorDatabase
#define GRPC_CUSTOM_SIMPLEDESCRIPTORDATABASE ::google::protobuf::SimpleDescriptorDatabase
#endif
#ifndef GRPC_CUSTOM_ZEROCOPYOUTPUTSTREAM
#include <google/protobuf/io/coded_stream.h>
#include <google/protobuf/io/zero_copy_stream.h>
#define GRPC_CUSTOM_ZEROCOPYOUTPUTSTREAM \
::google::protobuf::io::ZeroCopyOutputStream
#define GRPC_CUSTOM_ZEROCOPYINPUTSTREAM \
::google::protobuf::io::ZeroCopyInputStream
#define GRPC_CUSTOM_ZEROCOPYOUTPUTSTREAM ::google::protobuf::io::ZeroCopyOutputStream
#define GRPC_CUSTOM_ZEROCOPYINPUTSTREAM ::google::protobuf::io::ZeroCopyInputStream
#define GRPC_CUSTOM_CODEDINPUTSTREAM ::google::protobuf::io::CodedInputStream
#define GRPC_CUSTOM_CODEDOUTPUTSTREAM ::google::protobuf::io::CodedOutputStream
#endif
#ifndef GRPC_CUSTOM_JSONUTIL
#include "absl/status/status.h"
#include <google/protobuf/util/json_util.h>
#include <google/protobuf/util/type_resolver_util.h>
#include "absl/status/status.h"
#define GRPC_CUSTOM_JSONUTIL ::google::protobuf::util
#define GRPC_CUSTOM_UTIL_STATUS ::absl::Status
#endif

@ -0,0 +1,93 @@
// Copyright 2024 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef GRPCPP_IMPL_PROTO_UTILS_H
#define GRPCPP_IMPL_PROTO_UTILS_H
#include <grpc/byte_buffer_reader.h>
#include <grpc/impl/grpc_types.h>
#include <grpc/slice.h>
#include <grpcpp/impl/codegen/config_protobuf.h>
#include <grpcpp/impl/serialization_traits.h>
#include <grpcpp/support/byte_buffer.h>
#include <grpcpp/support/proto_buffer_reader.h>
#include <grpcpp/support/proto_buffer_writer.h>
#include <grpcpp/support/slice.h>
#include <grpcpp/support/status.h>
#include <type_traits>
#include "absl/log/absl_check.h"
/// This header provides serialization and deserialization between gRPC
/// messages serialized using protobuf and the C++ objects they represent.
namespace grpc {
// ProtoBufferWriter must be a subclass of ::protobuf::io::ZeroCopyOutputStream.
template <class ProtoBufferWriter, class T>
Status GenericSerialize(const grpc::protobuf::MessageLite& msg, ByteBuffer* bb,
bool* own_buffer) {
static_assert(std::is_base_of<protobuf::io::ZeroCopyOutputStream,
ProtoBufferWriter>::value,
"ProtoBufferWriter must be a subclass of "
"::protobuf::io::ZeroCopyOutputStream");
*own_buffer = true;
int byte_size = static_cast<int>(msg.ByteSizeLong());
if (static_cast<size_t>(byte_size) <= GRPC_SLICE_INLINED_SIZE) {
Slice slice(byte_size);
// We serialize directly into the allocated slices memory
ABSL_CHECK(slice.end() == msg.SerializeWithCachedSizesToArray(
const_cast<uint8_t*>(slice.begin())));
ByteBuffer tmp(&slice, 1);
bb->Swap(&tmp);
return grpc::Status::OK;
}
ProtoBufferWriter writer(bb, kProtoBufferWriterMaxBufferLength, byte_size);
protobuf::io::CodedOutputStream cs(&writer);
msg.SerializeWithCachedSizes(&cs);
return !cs.HadError()
? grpc::Status::OK
: Status(StatusCode::INTERNAL, "Failed to serialize message");
}
// BufferReader must be a subclass of ::protobuf::io::ZeroCopyInputStream.
template <class ProtoBufferReader, class T>
Status GenericDeserialize(ByteBuffer* buffer,
grpc::protobuf::MessageLite* msg) {
static_assert(std::is_base_of<protobuf::io::ZeroCopyInputStream,
ProtoBufferReader>::value,
"ProtoBufferReader must be a subclass of "
"::protobuf::io::ZeroCopyInputStream");
if (buffer == nullptr) {
return Status(StatusCode::INTERNAL, "No payload");
}
Status result = grpc::Status::OK;
{
ProtoBufferReader reader(buffer);
if (!reader.status().ok()) {
return reader.status();
}
if (!msg->ParseFromZeroCopyStream(&reader)) {
result = Status(StatusCode::INTERNAL, msg->InitializationErrorString());
}
}
buffer->Clear();
return result;
}
} // namespace grpc
#endif // GRPCPP_IMPL_PROTO_UTILS_H

@ -23,6 +23,7 @@
#include <grpc/impl/grpc_types.h>
#include <grpc/slice.h>
#include <grpcpp/impl/codegen/config_protobuf.h>
#include <grpcpp/impl/generic_serialize.h>
#include <grpcpp/impl/serialization_traits.h>
#include <grpcpp/support/byte_buffer.h>
#include <grpcpp/support/proto_buffer_reader.h>
@ -32,69 +33,11 @@
#include <type_traits>
#include "absl/log/absl_check.h"
/// This header provides serialization and deserialization between gRPC
/// messages serialized using protobuf and the C++ objects they represent.
namespace grpc {
// ProtoBufferWriter must be a subclass of ::protobuf::io::ZeroCopyOutputStream.
template <class ProtoBufferWriter, class T>
Status GenericSerialize(const grpc::protobuf::MessageLite& msg, ByteBuffer* bb,
bool* own_buffer) {
static_assert(std::is_base_of<protobuf::io::ZeroCopyOutputStream,
ProtoBufferWriter>::value,
"ProtoBufferWriter must be a subclass of "
"::protobuf::io::ZeroCopyOutputStream");
*own_buffer = true;
int byte_size = static_cast<int>(msg.ByteSizeLong());
if (static_cast<size_t>(byte_size) <= GRPC_SLICE_INLINED_SIZE) {
Slice slice(byte_size);
// We serialize directly into the allocated slices memory
ABSL_CHECK(slice.end() == msg.SerializeWithCachedSizesToArray(
const_cast<uint8_t*>(slice.begin())));
ByteBuffer tmp(&slice, 1);
bb->Swap(&tmp);
return grpc::Status::OK;
}
ProtoBufferWriter writer(bb, kProtoBufferWriterMaxBufferLength, byte_size);
protobuf::io::CodedOutputStream cs(&writer);
msg.SerializeWithCachedSizes(&cs);
return !cs.HadError()
? grpc::Status::OK
: Status(StatusCode::INTERNAL, "Failed to serialize message");
}
// BufferReader must be a subclass of ::protobuf::io::ZeroCopyInputStream.
template <class ProtoBufferReader, class T>
Status GenericDeserialize(ByteBuffer* buffer,
grpc::protobuf::MessageLite* msg) {
static_assert(std::is_base_of<protobuf::io::ZeroCopyInputStream,
ProtoBufferReader>::value,
"ProtoBufferReader must be a subclass of "
"::protobuf::io::ZeroCopyInputStream");
if (buffer == nullptr) {
return Status(StatusCode::INTERNAL, "No payload");
}
Status result = grpc::Status::OK;
{
ProtoBufferReader reader(buffer);
if (!reader.status().ok()) {
return reader.status();
}
if (!msg->ParseFromZeroCopyStream(&reader)) {
result = Status(StatusCode::INTERNAL, msg->InitializationErrorString());
}
}
buffer->Clear();
return result;
}
// this is needed so the following class does not conflict with protobuf
// serializers that utilize internal-only tools.
#ifdef GRPC_OPEN_SOURCE_PROTO
// This class provides a protobuf serializer. It translates between protobuf
// objects and grpc_byte_buffers. More information about SerializationTraits can
// be found in include/grpcpp/impl/codegen/serialization_traits.h.
@ -113,7 +56,6 @@ class SerializationTraits<
return GenericDeserialize<ProtoBufferReader, T>(buffer, msg);
}
};
#endif
} // namespace grpc

Loading…
Cancel
Save