proto_buffer_reader.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. //
  2. //
  3. // Copyright 2018 gRPC authors.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. //
  18. #ifndef GRPCPP_SUPPORT_PROTO_BUFFER_READER_H
  19. #define GRPCPP_SUPPORT_PROTO_BUFFER_READER_H
  20. #include <type_traits>
  21. #include <grpc/byte_buffer.h>
  22. #include <grpc/byte_buffer_reader.h>
  23. #include <grpc/impl/grpc_types.h>
  24. #include <grpc/slice.h>
  25. #include <grpc/support/log.h>
  26. #include <grpcpp/impl/codegen/config_protobuf.h>
  27. #include <grpcpp/impl/serialization_traits.h>
  28. #include <grpcpp/support/byte_buffer.h>
  29. #include <grpcpp/support/status.h>
  30. /// This header provides an object that reads bytes directly from a
  31. /// grpc::ByteBuffer, via the ZeroCopyInputStream interface
  32. namespace grpc {
  33. /// This is a specialization of the protobuf class ZeroCopyInputStream
  34. /// The principle is to get one chunk of data at a time from the proto layer,
  35. /// with options to backup (re-see some bytes) or skip (forward past some bytes)
  36. ///
  37. /// Read more about ZeroCopyInputStream interface here:
  38. /// https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.io.zero_copy_stream#ZeroCopyInputStream
  39. class ProtoBufferReader : public grpc::protobuf::io::ZeroCopyInputStream {
  40. public:
  41. /// Constructs buffer reader from \a buffer. Will set \a status() to non ok
  42. /// if \a buffer is invalid (the internal buffer has not been initialized).
  43. explicit ProtoBufferReader(ByteBuffer* buffer)
  44. : byte_count_(0), backup_count_(0), status_() {
  45. /// Implemented through a grpc_byte_buffer_reader which iterates
  46. /// over the slices that make up a byte buffer
  47. if (!buffer->Valid() ||
  48. !grpc_byte_buffer_reader_init(&reader_, buffer->c_buffer())) {
  49. status_ = Status(StatusCode::INTERNAL,
  50. "Couldn't initialize byte buffer reader");
  51. }
  52. }
  53. ~ProtoBufferReader() override {
  54. if (status_.ok()) {
  55. grpc_byte_buffer_reader_destroy(&reader_);
  56. }
  57. }
  58. /// Give the proto library a chunk of data from the stream. The caller
  59. /// may safely read from data[0, size - 1].
  60. bool Next(const void** data, int* size) override {
  61. if (!status_.ok()) {
  62. return false;
  63. }
  64. /// If we have backed up previously, we need to return the backed-up slice
  65. if (backup_count_ > 0) {
  66. *data = GRPC_SLICE_START_PTR(*slice_) + GRPC_SLICE_LENGTH(*slice_) -
  67. backup_count_;
  68. GPR_ASSERT(backup_count_ <= INT_MAX);
  69. *size = static_cast<int>(backup_count_);
  70. backup_count_ = 0;
  71. return true;
  72. }
  73. /// Otherwise get the next slice from the byte buffer reader
  74. if (!grpc_byte_buffer_reader_peek(&reader_, &slice_)) {
  75. return false;
  76. }
  77. *data = GRPC_SLICE_START_PTR(*slice_);
  78. // On win x64, int is only 32bit
  79. GPR_ASSERT(GRPC_SLICE_LENGTH(*slice_) <= INT_MAX);
  80. byte_count_ += * size = static_cast<int>(GRPC_SLICE_LENGTH(*slice_));
  81. return true;
  82. }
  83. /// Returns the status of the buffer reader.
  84. Status status() const { return status_; }
  85. /// The proto library calls this to indicate that we should back up \a count
  86. /// bytes that have already been returned by the last call of Next.
  87. /// So do the backup and have that ready for a later Next.
  88. void BackUp(int count) override {
  89. GPR_ASSERT(count <= static_cast<int>(GRPC_SLICE_LENGTH(*slice_)));
  90. backup_count_ = count;
  91. }
  92. /// The proto library calls this to skip over \a count bytes. Implement this
  93. /// using Next and BackUp combined.
  94. bool Skip(int count) override {
  95. const void* data;
  96. int size;
  97. while (Next(&data, &size)) {
  98. if (size >= count) {
  99. BackUp(size - count);
  100. return true;
  101. }
  102. // size < count;
  103. count -= size;
  104. }
  105. // error or we have too large count;
  106. return false;
  107. }
  108. /// Returns the total number of bytes read since this object was created.
  109. int64_t ByteCount() const override { return byte_count_ - backup_count_; }
  110. // These protected members are needed to support internal optimizations.
  111. // they expose internal bits of grpc core that are NOT stable. If you have
  112. // a use case needs to use one of these functions, please send an email to
  113. // https://groups.google.com/forum/#!forum/grpc-io.
  114. protected:
  115. void set_byte_count(int64_t byte_count) { byte_count_ = byte_count; }
  116. int64_t backup_count() { return backup_count_; }
  117. void set_backup_count(int64_t backup_count) { backup_count_ = backup_count; }
  118. grpc_byte_buffer_reader* reader() { return &reader_; }
  119. grpc_slice* slice() { return slice_; }
  120. grpc_slice** mutable_slice_ptr() { return &slice_; }
  121. private:
  122. int64_t byte_count_; ///< total bytes read since object creation
  123. int64_t backup_count_; ///< how far backed up in the stream we are
  124. grpc_byte_buffer_reader reader_; ///< internal object to read \a grpc_slice
  125. ///< from the \a grpc_byte_buffer
  126. grpc_slice* slice_; ///< current slice passed back to the caller
  127. Status status_; ///< status of the entire object
  128. };
  129. } // namespace grpc
  130. #endif // GRPCPP_SUPPORT_PROTO_BUFFER_READER_H