You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

byte_buffer.h 9.9 kB

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