byte_buffer.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. *
  3. * Copyright 2017 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_BYTE_BUFFER_H
  19. #define GRPCPP_IMPL_CODEGEN_BYTE_BUFFER_H
  20. // IWYU pragma: private, include <grpcpp/support/byte_buffer.h>
  21. #include <vector>
  22. #include <grpc/impl/codegen/byte_buffer.h>
  23. #include <grpcpp/impl/codegen/config.h>
  24. #include <grpcpp/impl/codegen/core_codegen_interface.h>
  25. #include <grpcpp/impl/codegen/serialization_traits.h>
  26. #include <grpcpp/impl/codegen/slice.h>
  27. #include <grpcpp/impl/codegen/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_ = g_core_codegen_interface->grpc_raw_byte_buffer_create(
  80. reinterpret_cast<grpc_slice*>(const_cast<Slice*>(slices)), nslices);
  81. }
  82. /// Constuct 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. g_core_codegen_interface->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_ = g_core_codegen_interface->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. g_core_codegen_interface->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() {
  125. buffer_ = g_core_codegen_interface->grpc_byte_buffer_copy(buffer_);
  126. }
  127. /// Forget underlying byte buffer without destroying
  128. /// Use this only for un-owned byte buffers
  129. void Release() { buffer_ = nullptr; }
  130. /// Buffer size in bytes.
  131. size_t Length() const {
  132. return buffer_ == nullptr
  133. ? 0
  134. : g_core_codegen_interface->grpc_byte_buffer_length(buffer_);
  135. }
  136. /// Swap the state of *this and *other.
  137. void Swap(ByteBuffer* other) {
  138. grpc_byte_buffer* tmp = other->buffer_;
  139. other->buffer_ = buffer_;
  140. buffer_ = tmp;
  141. }
  142. /// Is this ByteBuffer valid?
  143. bool Valid() const { return (buffer_ != nullptr); }
  144. private:
  145. friend class SerializationTraits<ByteBuffer, void>;
  146. friend class ServerInterface;
  147. friend class internal::CallOpSendMessage;
  148. template <class R>
  149. friend class internal::CallOpRecvMessage;
  150. friend class internal::CallOpGenericRecvMessage;
  151. template <class RequestType>
  152. friend void* internal::UnaryDeserializeHelper(grpc_byte_buffer*,
  153. grpc::Status*, RequestType*);
  154. template <class ServiceType, class RequestType, class ResponseType>
  155. friend class internal::ServerStreamingHandler;
  156. template <class RequestType, class ResponseType>
  157. friend class internal::CallbackUnaryHandler;
  158. template <class RequestType, class ResponseType>
  159. friend class internal::CallbackServerStreamingHandler;
  160. template <StatusCode code>
  161. friend class internal::ErrorMethodHandler;
  162. template <class R>
  163. friend class internal::DeserializeFuncType;
  164. friend class ProtoBufferReader;
  165. friend class ProtoBufferWriter;
  166. friend class internal::GrpcByteBufferPeer;
  167. friend class internal::ExternalConnectionAcceptorImpl;
  168. grpc_byte_buffer* buffer_;
  169. // takes ownership
  170. void set_buffer(grpc_byte_buffer* buf) {
  171. if (buffer_) {
  172. Clear();
  173. }
  174. buffer_ = buf;
  175. }
  176. grpc_byte_buffer* c_buffer() { return buffer_; }
  177. grpc_byte_buffer** c_buffer_ptr() { return &buffer_; }
  178. class ByteBufferPointer {
  179. public:
  180. /* NOLINTNEXTLINE(google-explicit-constructor) */
  181. ByteBufferPointer(const ByteBuffer* b)
  182. : bbuf_(const_cast<ByteBuffer*>(b)) {}
  183. /* NOLINTNEXTLINE(google-explicit-constructor) */
  184. operator ByteBuffer*() { return bbuf_; }
  185. /* NOLINTNEXTLINE(google-explicit-constructor) */
  186. operator grpc_byte_buffer*() { return bbuf_->buffer_; }
  187. /* NOLINTNEXTLINE(google-explicit-constructor) */
  188. operator grpc_byte_buffer**() { return &bbuf_->buffer_; }
  189. private:
  190. ByteBuffer* bbuf_;
  191. };
  192. ByteBufferPointer bbuf_ptr() const { return ByteBufferPointer(this); }
  193. };
  194. template <>
  195. class SerializationTraits<ByteBuffer, void> {
  196. public:
  197. static Status Deserialize(ByteBuffer* byte_buffer, ByteBuffer* dest) {
  198. dest->set_buffer(byte_buffer->buffer_);
  199. return Status::OK;
  200. }
  201. static Status Serialize(const ByteBuffer& source, ByteBuffer* buffer,
  202. bool* own_buffer) {
  203. *buffer = source;
  204. *own_buffer = true;
  205. return g_core_codegen_interface->ok();
  206. }
  207. };
  208. } // namespace grpc
  209. #endif // GRPCPP_IMPL_CODEGEN_BYTE_BUFFER_H