Merge pull request #15052 from ncteisen/department-of-redundancy-department

Rename ProtoBuffer Helpers
pull/15060/head
Vijay Pai 7 years ago committed by GitHub
commit d184fa229d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      include/grpcpp/impl/codegen/byte_buffer.h
  2. 6
      include/grpcpp/impl/codegen/proto_buffer_reader.h
  3. 13
      include/grpcpp/impl/codegen/proto_buffer_writer.h
  4. 7
      include/grpcpp/impl/codegen/proto_utils.h
  5. 19
      test/cpp/codegen/proto_utils_test.cc

@ -146,8 +146,8 @@ class ByteBuffer final {
friend class internal::ServerStreamingHandler; friend class internal::ServerStreamingHandler;
template <class R> template <class R>
friend class internal::DeserializeFuncType; friend class internal::DeserializeFuncType;
friend class GrpcProtoBufferReader; friend class ProtoBufferReader;
friend class GrpcProtoBufferWriter; friend class ProtoBufferWriter;
friend class internal::GrpcByteBufferPeer; friend class internal::GrpcByteBufferPeer;
grpc_byte_buffer* buffer_; grpc_byte_buffer* buffer_;

@ -43,11 +43,11 @@ extern CoreCodegenInterface* g_core_codegen_interface;
/// ///
/// Read more about ZeroCopyInputStream interface here: /// Read more about ZeroCopyInputStream interface here:
/// https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.io.zero_copy_stream#ZeroCopyInputStream /// https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.io.zero_copy_stream#ZeroCopyInputStream
class GrpcProtoBufferReader : public ::grpc::protobuf::io::ZeroCopyInputStream { class ProtoBufferReader : public ::grpc::protobuf::io::ZeroCopyInputStream {
public: public:
/// Constructs buffer reader from \a buffer. Will set \a status() to non ok /// Constructs buffer reader from \a buffer. Will set \a status() to non ok
/// if \a buffer is invalid (the internal buffer has not been initialized). /// if \a buffer is invalid (the internal buffer has not been initialized).
explicit GrpcProtoBufferReader(ByteBuffer* buffer) explicit ProtoBufferReader(ByteBuffer* buffer)
: byte_count_(0), backup_count_(0), status_() { : byte_count_(0), backup_count_(0), status_() {
/// Implemented through a grpc_byte_buffer_reader which iterates /// Implemented through a grpc_byte_buffer_reader which iterates
/// over the slices that make up a byte buffer /// over the slices that make up a byte buffer
@ -59,7 +59,7 @@ class GrpcProtoBufferReader : public ::grpc::protobuf::io::ZeroCopyInputStream {
} }
} }
~GrpcProtoBufferReader() { ~ProtoBufferReader() {
if (status_.ok()) { if (status_.ok()) {
g_core_codegen_interface->grpc_byte_buffer_reader_destroy(&reader_); g_core_codegen_interface->grpc_byte_buffer_reader_destroy(&reader_);
} }

@ -38,10 +38,10 @@ extern CoreCodegenInterface* g_core_codegen_interface;
// Forward declaration for testing use only // Forward declaration for testing use only
namespace internal { namespace internal {
class GrpcProtoBufferWriterPeer; class ProtoBufferWriterPeer;
} // namespace internal } // namespace internal
const int kGrpcProtoBufferWriterMaxBufferLength = 1024 * 1024; const int kProtoBufferWriterMaxBufferLength = 1024 * 1024;
/// This is a specialization of the protobuf class ZeroCopyOutputStream. /// This is a specialization of the protobuf class ZeroCopyOutputStream.
/// The principle is to give the proto layer one buffer of bytes at a time /// The principle is to give the proto layer one buffer of bytes at a time
@ -50,15 +50,14 @@ const int kGrpcProtoBufferWriterMaxBufferLength = 1024 * 1024;
/// ///
/// Read more about ZeroCopyOutputStream interface here: /// Read more about ZeroCopyOutputStream interface here:
/// https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.io.zero_copy_stream#ZeroCopyOutputStream /// https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.io.zero_copy_stream#ZeroCopyOutputStream
class GrpcProtoBufferWriter class ProtoBufferWriter : public ::grpc::protobuf::io::ZeroCopyOutputStream {
: public ::grpc::protobuf::io::ZeroCopyOutputStream {
public: public:
/// Constructor for this derived class /// Constructor for this derived class
/// ///
/// \param[out] byte_buffer A pointer to the grpc::ByteBuffer created /// \param[out] byte_buffer A pointer to the grpc::ByteBuffer created
/// \param block_size How big are the chunks to allocate at a time /// \param block_size How big are the chunks to allocate at a time
/// \param total_size How many total bytes are required for this proto /// \param total_size How many total bytes are required for this proto
GrpcProtoBufferWriter(ByteBuffer* byte_buffer, int block_size, int total_size) ProtoBufferWriter(ByteBuffer* byte_buffer, int block_size, int total_size)
: block_size_(block_size), : block_size_(block_size),
total_size_(total_size), total_size_(total_size),
byte_count_(0), byte_count_(0),
@ -71,7 +70,7 @@ class GrpcProtoBufferWriter
slice_buffer_ = &bp->data.raw.slice_buffer; slice_buffer_ = &bp->data.raw.slice_buffer;
} }
~GrpcProtoBufferWriter() { ~ProtoBufferWriter() {
if (have_backup_) { if (have_backup_) {
g_core_codegen_interface->grpc_slice_unref(backup_slice_); g_core_codegen_interface->grpc_slice_unref(backup_slice_);
} }
@ -151,7 +150,7 @@ class GrpcProtoBufferWriter
private: private:
// friend for testing purposes only // friend for testing purposes only
friend class internal::GrpcProtoBufferWriterPeer; friend class internal::ProtoBufferWriterPeer;
const int block_size_; ///< size to alloc for each new \a grpc_slice needed const int block_size_; ///< size to alloc for each new \a grpc_slice needed
const int total_size_; ///< byte size of proto being serialized const int total_size_; ///< byte size of proto being serialized
int64_t byte_count_; ///< bytes written since this object was created int64_t byte_count_; ///< bytes written since this object was created

@ -60,8 +60,7 @@ Status GenericSerialize(const grpc::protobuf::Message& msg, ByteBuffer* bb,
return g_core_codegen_interface->ok(); return g_core_codegen_interface->ok();
} }
ProtoBufferWriter writer(bb, kGrpcProtoBufferWriterMaxBufferLength, ProtoBufferWriter writer(bb, kProtoBufferWriterMaxBufferLength, byte_size);
byte_size);
return msg.SerializeToZeroCopyStream(&writer) return msg.SerializeToZeroCopyStream(&writer)
? g_core_codegen_interface->ok() ? g_core_codegen_interface->ok()
: Status(StatusCode::INTERNAL, "Failed to serialize message"); : Status(StatusCode::INTERNAL, "Failed to serialize message");
@ -108,11 +107,11 @@ class SerializationTraits<T, typename std::enable_if<std::is_base_of<
public: public:
static Status Serialize(const grpc::protobuf::Message& msg, ByteBuffer* bb, static Status Serialize(const grpc::protobuf::Message& msg, ByteBuffer* bb,
bool* own_buffer) { bool* own_buffer) {
return GenericSerialize<GrpcProtoBufferWriter, T>(msg, bb, own_buffer); return GenericSerialize<ProtoBufferWriter, T>(msg, bb, own_buffer);
} }
static Status Deserialize(ByteBuffer* buffer, grpc::protobuf::Message* msg) { static Status Deserialize(ByteBuffer* buffer, grpc::protobuf::Message* msg) {
return GenericDeserialize<GrpcProtoBufferReader, T>(buffer, msg); return GenericDeserialize<ProtoBufferReader, T>(buffer, msg);
} }
}; };
#endif #endif

@ -27,17 +27,16 @@ namespace grpc {
namespace internal { namespace internal {
// Provide access to GrpcProtoBufferWriter internals. // Provide access to ProtoBufferWriter internals.
class GrpcProtoBufferWriterPeer { class ProtoBufferWriterPeer {
public: public:
explicit GrpcProtoBufferWriterPeer(GrpcProtoBufferWriter* writer) explicit ProtoBufferWriterPeer(ProtoBufferWriter* writer) : writer_(writer) {}
: writer_(writer) {}
bool have_backup() const { return writer_->have_backup_; } bool have_backup() const { return writer_->have_backup_; }
const grpc_slice& backup_slice() const { return writer_->backup_slice_; } const grpc_slice& backup_slice() const { return writer_->backup_slice_; }
const grpc_slice& slice() const { return writer_->slice_; } const grpc_slice& slice() const { return writer_->slice_; }
private: private:
GrpcProtoBufferWriter* writer_; ProtoBufferWriter* writer_;
}; };
// Provide access to ByteBuffer internals. // Provide access to ByteBuffer internals.
@ -53,14 +52,14 @@ class GrpcByteBufferPeer {
class ProtoUtilsTest : public ::testing::Test {}; class ProtoUtilsTest : public ::testing::Test {};
// Regression test for a memory corruption bug where a series of // Regression test for a memory corruption bug where a series of
// GrpcProtoBufferWriter Next()/Backup() invocations could result in a dangling // ProtoBufferWriter Next()/Backup() invocations could result in a dangling
// pointer returned by Next() due to the interaction between grpc_slice inlining // pointer returned by Next() due to the interaction between grpc_slice inlining
// and GRPC_SLICE_START_PTR. // and GRPC_SLICE_START_PTR.
TEST_F(ProtoUtilsTest, TinyBackupThenNext) { TEST_F(ProtoUtilsTest, TinyBackupThenNext) {
ByteBuffer bp; ByteBuffer bp;
const int block_size = 1024; const int block_size = 1024;
GrpcProtoBufferWriter writer(&bp, block_size, 8192); ProtoBufferWriter writer(&bp, block_size, 8192);
GrpcProtoBufferWriterPeer peer(&writer); ProtoBufferWriterPeer peer(&writer);
void* data; void* data;
int size; int size;
@ -81,7 +80,7 @@ namespace {
// Set backup_size to 0 to indicate no backup is needed. // Set backup_size to 0 to indicate no backup is needed.
void BufferWriterTest(int block_size, int total_size, int backup_size) { void BufferWriterTest(int block_size, int total_size, int backup_size) {
ByteBuffer bb; ByteBuffer bb;
GrpcProtoBufferWriter writer(&bb, block_size, total_size); ProtoBufferWriter writer(&bb, block_size, total_size);
int written_size = 0; int written_size = 0;
void* data; void* data;
@ -162,7 +161,7 @@ TEST(WriterTest, LargeBlockLargeBackup) { BufferWriterTest(4096, 8192, 4095); }
} // namespace grpc } // namespace grpc
int main(int argc, char** argv) { int main(int argc, char** argv) {
// Ensure the GrpcProtoBufferWriter internals are initialized. // Ensure the ProtoBufferWriter internals are initialized.
grpc::internal::GrpcLibraryInitializer init; grpc::internal::GrpcLibraryInitializer init;
init.summon(); init.summon();
grpc::GrpcLibraryCodegen lib; grpc::GrpcLibraryCodegen lib;

Loading…
Cancel
Save