byte_buffer.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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_SUPPORT_BYTE_BUFFER_H
  19. #define GRPCPP_SUPPORT_BYTE_BUFFER_H
  20. #include <vector>
  21. #include <grpc/byte_buffer.h>
  22. #include <grpc/grpc.h>
  23. #include <grpc/support/log.h>
  24. #include <grpcpp/impl/serialization_traits.h>
  25. #include <grpcpp/support/config.h>
  26. #include <grpcpp/support/slice.h>
  27. #include <grpcpp/support/status.h>
  28. namespace grpc {
  29. class ServerInterface;
  30. class ByteBuffer;
  31. class ServerInterface;
  32. namespace internal {
  33. template <class RequestType, class ResponseType>
  34. class CallbackUnaryHandler;
  35. template <class RequestType, class ResponseType>
  36. class CallbackServerStreamingHandler;
  37. template <class RequestType>
  38. void* UnaryDeserializeHelper(grpc_byte_buffer*, grpc::Status*, RequestType*);
  39. template <class ServiceType, class RequestType, class ResponseType>
  40. class ServerStreamingHandler;
  41. template <grpc::StatusCode code>
  42. class ErrorMethodHandler;
  43. class CallOpSendMessage;
  44. template <class R>
  45. class CallOpRecvMessage;
  46. class CallOpGenericRecvMessage;
  47. class ExternalConnectionAcceptorImpl;
  48. template <class R>
  49. class DeserializeFuncType;
  50. class GrpcByteBufferPeer;
  51. } // namespace internal
  52. /// A sequence of bytes.
  53. class ByteBuffer final {
  54. public:
  55. /// Constuct an empty buffer.
  56. ByteBuffer() : buffer_(nullptr) {}
  57. /// Construct buffer from \a slices, of which there are \a nslices.
  58. ByteBuffer(const Slice* slices, size_t nslices) {
  59. // The following assertions check that the representation of a grpc::Slice
  60. // is identical to that of a grpc_slice: it has a grpc_slice field, and
  61. // nothing else.
  62. static_assert(std::is_same<decltype(slices[0].slice_), grpc_slice>::value,
  63. "Slice must have same representation as grpc_slice");
  64. static_assert(sizeof(Slice) == sizeof(grpc_slice),
  65. "Slice must have same representation as grpc_slice");
  66. // The following assertions check that the representation of a ByteBuffer is
  67. // identical to grpc_byte_buffer*: it has a grpc_byte_buffer* field,
  68. // and nothing else.
  69. static_assert(std::is_same<decltype(buffer_), grpc_byte_buffer*>::value,
  70. "ByteBuffer must have same representation as "
  71. "grpc_byte_buffer*");
  72. static_assert(sizeof(ByteBuffer) == sizeof(grpc_byte_buffer*),
  73. "ByteBuffer must have same representation as "
  74. "grpc_byte_buffer*");
  75. // The const_cast is legal if grpc_raw_byte_buffer_create() does no more
  76. // than its advertised side effect of increasing the reference count of the
  77. // slices it processes, and such an increase does not affect the semantics
  78. // seen by the caller of this constructor.
  79. buffer_ = grpc_raw_byte_buffer_create(
  80. reinterpret_cast<grpc_slice*>(const_cast<Slice*>(slices)), nslices);
  81. }
  82. /// Construct a byte buffer by referencing elements of existing buffer
  83. /// \a buf. Wrapper of core function grpc_byte_buffer_copy . This is not
  84. /// a deep copy; it is just a referencing. As a result, its performance is
  85. /// size-independent.
  86. ByteBuffer(const ByteBuffer& buf) : buffer_(nullptr) { operator=(buf); }
  87. ~ByteBuffer() {
  88. if (buffer_) {
  89. grpc_byte_buffer_destroy(buffer_);
  90. }
  91. }
  92. /// Wrapper of core function grpc_byte_buffer_copy . This is not
  93. /// a deep copy; it is just a referencing. As a result, its performance is
  94. /// size-independent.
  95. ByteBuffer& operator=(const ByteBuffer& buf) {
  96. if (this != &buf) {
  97. Clear(); // first remove existing data
  98. }
  99. if (buf.buffer_) {
  100. // then copy
  101. buffer_ = grpc_byte_buffer_copy(buf.buffer_);
  102. }
  103. return *this;
  104. }
  105. // If this ByteBuffer's representation is a single flat slice, returns a
  106. // slice referencing that array.
  107. Status TrySingleSlice(Slice* slice) const;
  108. /// Dump (read) the buffer contents into \a slics.
  109. Status DumpToSingleSlice(Slice* slice) const;
  110. /// Dump (read) the buffer contents into \a slices.
  111. Status Dump(std::vector<Slice>* slices) const;
  112. /// Remove all data.
  113. void Clear() {
  114. if (buffer_) {
  115. grpc_byte_buffer_destroy(buffer_);
  116. buffer_ = nullptr;
  117. }
  118. }
  119. /// Make a duplicate copy of the internals of this byte
  120. /// buffer so that we have our own owned version of it.
  121. /// bbuf.Duplicate(); is equivalent to bbuf=bbuf; but is actually readable.
  122. /// This is not a deep copy; it is a referencing and its performance
  123. /// is size-independent.
  124. void Duplicate() { buffer_ = grpc_byte_buffer_copy(buffer_); }
  125. /// Forget underlying byte buffer without destroying
  126. /// Use this only for un-owned byte buffers
  127. void Release() { buffer_ = nullptr; }
  128. /// Buffer size in bytes.
  129. size_t Length() const {
  130. return buffer_ == nullptr ? 0 : grpc_byte_buffer_length(buffer_);
  131. }
  132. /// Swap the state of *this and *other.
  133. void Swap(ByteBuffer* other) {
  134. grpc_byte_buffer* tmp = other->buffer_;
  135. other->buffer_ = buffer_;
  136. buffer_ = tmp;
  137. }
  138. /// Is this ByteBuffer valid?
  139. bool Valid() const { return (buffer_ != nullptr); }
  140. private:
  141. friend class SerializationTraits<ByteBuffer, void>;
  142. friend class ServerInterface;
  143. friend class internal::CallOpSendMessage;
  144. template <class R>
  145. friend class internal::CallOpRecvMessage;
  146. friend class internal::CallOpGenericRecvMessage;
  147. template <class RequestType>
  148. friend void* internal::UnaryDeserializeHelper(grpc_byte_buffer*,
  149. grpc::Status*, RequestType*);
  150. template <class ServiceType, class RequestType, class ResponseType>
  151. friend class internal::ServerStreamingHandler;
  152. template <class RequestType, class ResponseType>
  153. friend class internal::CallbackUnaryHandler;
  154. template <class RequestType, class ResponseType>
  155. friend class internal::CallbackServerStreamingHandler;
  156. template <StatusCode code>
  157. friend class internal::ErrorMethodHandler;
  158. template <class R>
  159. friend class internal::DeserializeFuncType;
  160. friend class ProtoBufferReader;
  161. friend class ProtoBufferWriter;
  162. friend class internal::GrpcByteBufferPeer;
  163. friend class internal::ExternalConnectionAcceptorImpl;
  164. grpc_byte_buffer* buffer_;
  165. // takes ownership
  166. void set_buffer(grpc_byte_buffer* buf) {
  167. if (buffer_) {
  168. Clear();
  169. }
  170. buffer_ = buf;
  171. }
  172. grpc_byte_buffer* c_buffer() { return buffer_; }
  173. grpc_byte_buffer** c_buffer_ptr() { return &buffer_; }
  174. class ByteBufferPointer {
  175. public:
  176. // NOLINTNEXTLINE(google-explicit-constructor)
  177. ByteBufferPointer(const ByteBuffer* b)
  178. : bbuf_(const_cast<ByteBuffer*>(b)) {}
  179. // NOLINTNEXTLINE(google-explicit-constructor)
  180. operator ByteBuffer*() { return bbuf_; }
  181. // NOLINTNEXTLINE(google-explicit-constructor)
  182. operator grpc_byte_buffer*() { return bbuf_->buffer_; }
  183. // NOLINTNEXTLINE(google-explicit-constructor)
  184. operator grpc_byte_buffer**() { return &bbuf_->buffer_; }
  185. private:
  186. ByteBuffer* bbuf_;
  187. };
  188. ByteBufferPointer bbuf_ptr() const { return ByteBufferPointer(this); }
  189. };
  190. template <>
  191. class SerializationTraits<ByteBuffer, void> {
  192. public:
  193. static Status Deserialize(ByteBuffer* byte_buffer, ByteBuffer* dest) {
  194. dest->set_buffer(byte_buffer->buffer_);
  195. return Status::OK;
  196. }
  197. static Status Serialize(const ByteBuffer& source, ByteBuffer* buffer,
  198. bool* own_buffer) {
  199. *buffer = source;
  200. *own_buffer = true;
  201. return grpc::Status::OK;
  202. }
  203. };
  204. } // namespace grpc
  205. #endif // GRPCPP_SUPPORT_BYTE_BUFFER_H