proto_buffer_reader.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. *
  3. * Copyright 2015 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_IMPL_CODEGEN_PROTO_BUFFER_READER_H
  19. #define GRPCPP_IMPL_CODEGEN_PROTO_BUFFER_READER_H
  20. // IWYU pragma: private, include <grpcpp/support/proto_buffer_reader.h>
  21. #include <type_traits>
  22. #include <grpc/impl/codegen/byte_buffer_reader.h>
  23. #include <grpc/impl/codegen/grpc_types.h>
  24. #include <grpc/impl/codegen/slice.h>
  25. #include <grpcpp/impl/codegen/byte_buffer.h>
  26. #include <grpcpp/impl/codegen/config_protobuf.h>
  27. #include <grpcpp/impl/codegen/core_codegen_interface.h>
  28. #include <grpcpp/impl/codegen/serialization_traits.h>
  29. #include <grpcpp/impl/codegen/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. extern CoreCodegenInterface* g_core_codegen_interface;
  34. /// This is a specialization of the protobuf class ZeroCopyInputStream
  35. /// The principle is to get one chunk of data at a time from the proto layer,
  36. /// with options to backup (re-see some bytes) or skip (forward past some bytes)
  37. ///
  38. /// Read more about ZeroCopyInputStream interface here:
  39. /// https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.io.zero_copy_stream#ZeroCopyInputStream
  40. class ProtoBufferReader : public grpc::protobuf::io::ZeroCopyInputStream {
  41. public:
  42. /// Constructs buffer reader from \a buffer. Will set \a status() to non ok
  43. /// if \a buffer is invalid (the internal buffer has not been initialized).
  44. explicit ProtoBufferReader(ByteBuffer* buffer)
  45. : byte_count_(0), backup_count_(0), status_() {
  46. /// Implemented through a grpc_byte_buffer_reader which iterates
  47. /// over the slices that make up a byte buffer
  48. if (!buffer->Valid() ||
  49. !g_core_codegen_interface->grpc_byte_buffer_reader_init(
  50. &reader_, buffer->c_buffer())) {
  51. status_ = Status(StatusCode::INTERNAL,
  52. "Couldn't initialize byte buffer reader");
  53. }
  54. }
  55. ~ProtoBufferReader() override {
  56. if (status_.ok()) {
  57. g_core_codegen_interface->grpc_byte_buffer_reader_destroy(&reader_);
  58. }
  59. }
  60. /// Give the proto library a chunk of data from the stream. The caller
  61. /// may safely read from data[0, size - 1].
  62. bool Next(const void** data, int* size) override {
  63. if (!status_.ok()) {
  64. return false;
  65. }
  66. /// If we have backed up previously, we need to return the backed-up slice
  67. if (backup_count_ > 0) {
  68. *data = GRPC_SLICE_START_PTR(*slice_) + GRPC_SLICE_LENGTH(*slice_) -
  69. backup_count_;
  70. GPR_CODEGEN_ASSERT(backup_count_ <= INT_MAX);
  71. *size = static_cast<int>(backup_count_);
  72. backup_count_ = 0;
  73. return true;
  74. }
  75. /// Otherwise get the next slice from the byte buffer reader
  76. if (!g_core_codegen_interface->grpc_byte_buffer_reader_peek(&reader_,
  77. &slice_)) {
  78. return false;
  79. }
  80. *data = GRPC_SLICE_START_PTR(*slice_);
  81. // On win x64, int is only 32bit
  82. GPR_CODEGEN_ASSERT(GRPC_SLICE_LENGTH(*slice_) <= INT_MAX);
  83. byte_count_ += * size = static_cast<int>(GRPC_SLICE_LENGTH(*slice_));
  84. return true;
  85. }
  86. /// Returns the status of the buffer reader.
  87. Status status() const { return status_; }
  88. /// The proto library calls this to indicate that we should back up \a count
  89. /// bytes that have already been returned by the last call of Next.
  90. /// So do the backup and have that ready for a later Next.
  91. void BackUp(int count) override {
  92. GPR_CODEGEN_ASSERT(count <= static_cast<int>(GRPC_SLICE_LENGTH(*slice_)));
  93. backup_count_ = count;
  94. }
  95. /// The proto library calls this to skip over \a count bytes. Implement this
  96. /// using Next and BackUp combined.
  97. bool Skip(int count) override {
  98. const void* data;
  99. int size;
  100. while (Next(&data, &size)) {
  101. if (size >= count) {
  102. BackUp(size - count);
  103. return true;
  104. }
  105. // size < count;
  106. count -= size;
  107. }
  108. // error or we have too large count;
  109. return false;
  110. }
  111. /// Returns the total number of bytes read since this object was created.
  112. int64_t ByteCount() const override { return byte_count_ - backup_count_; }
  113. // These protected members are needed to support internal optimizations.
  114. // they expose internal bits of grpc core that are NOT stable. If you have
  115. // a use case needs to use one of these functions, please send an email to
  116. // https://groups.google.com/forum/#!forum/grpc-io.
  117. protected:
  118. void set_byte_count(int64_t byte_count) { byte_count_ = byte_count; }
  119. int64_t backup_count() { return backup_count_; }
  120. void set_backup_count(int64_t backup_count) { backup_count_ = backup_count; }
  121. grpc_byte_buffer_reader* reader() { return &reader_; }
  122. grpc_slice* slice() { return slice_; }
  123. grpc_slice** mutable_slice_ptr() { return &slice_; }
  124. private:
  125. int64_t byte_count_; ///< total bytes read since object creation
  126. int64_t backup_count_; ///< how far backed up in the stream we are
  127. grpc_byte_buffer_reader reader_; ///< internal object to read \a grpc_slice
  128. ///< from the \a grpc_byte_buffer
  129. grpc_slice* slice_; ///< current slice passed back to the caller
  130. Status status_; ///< status of the entire object
  131. };
  132. } // namespace grpc
  133. #endif // GRPCPP_IMPL_CODEGEN_PROTO_BUFFER_READER_H