GRPC C++  1.81.0
byte_buffer.h
Go to the documentation of this file.
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 
19 #ifndef GRPCPP_SUPPORT_BYTE_BUFFER_H
20 #define GRPCPP_SUPPORT_BYTE_BUFFER_H
21 
22 #include <grpc/byte_buffer.h>
23 #include <grpc/grpc.h>
25 #include <grpcpp/support/config.h>
26 #include <grpcpp/support/slice.h>
27 #include <grpcpp/support/status.h>
28 
29 #include <vector>
30 
31 namespace grpc {
32 
33 class ServerInterface;
34 class ByteBuffer;
35 class ServerInterface;
36 
37 namespace internal {
38 template <class RequestType, class ResponseType>
39 class CallbackUnaryHandler;
40 template <class RequestType, class ResponseType>
41 class CallbackServerStreamingHandler;
42 template <class RequestType>
44 template <class ServiceType, class RequestType, class ResponseType>
45 class ServerStreamingHandler;
46 template <grpc::StatusCode code>
47 class ErrorMethodHandler;
48 class CallOpSendMessage;
49 template <class R>
50 class CallOpRecvMessage;
51 class CallOpGenericRecvMessage;
52 class ExternalConnectionAcceptorImpl;
53 template <class R>
54 class DeserializeFuncType;
55 class GrpcByteBufferPeer;
56 
57 } // namespace internal
58 
59 namespace experimental {
60 namespace internal {
61 template <class RequestType>
62 class CallbackSessionHandler;
63 } // namespace internal
64 } // namespace experimental
65 
67 class ByteBuffer final {
68  public:
70  ByteBuffer() : buffer_(nullptr) {}
71 
73  ByteBuffer(const Slice* slices, size_t nslices) {
74  // The following assertions check that the representation of a grpc::Slice
75  // is identical to that of a grpc_slice: it has a grpc_slice field, and
76  // nothing else.
77  static_assert(std::is_same<decltype(slices[0].slice_), grpc_slice>::value,
78  "Slice must have same representation as grpc_slice");
79  static_assert(sizeof(Slice) == sizeof(grpc_slice),
80  "Slice must have same representation as grpc_slice");
81  // The following assertions check that the representation of a ByteBuffer is
82  // identical to grpc_byte_buffer*: it has a grpc_byte_buffer* field,
83  // and nothing else.
84  static_assert(std::is_same<decltype(buffer_), grpc_byte_buffer*>::value,
85  "ByteBuffer must have same representation as "
86  "grpc_byte_buffer*");
87  static_assert(sizeof(ByteBuffer) == sizeof(grpc_byte_buffer*),
88  "ByteBuffer must have same representation as "
89  "grpc_byte_buffer*");
90  // The const_cast is legal if grpc_raw_byte_buffer_create() does no more
91  // than its advertised side effect of increasing the reference count of the
92  // slices it processes, and such an increase does not affect the semantics
93  // seen by the caller of this constructor.
95  reinterpret_cast<grpc_slice*>(const_cast<Slice*>(slices)), nslices);
96  }
97 
102  ByteBuffer(const ByteBuffer& buf) : buffer_(nullptr) { operator=(buf); }
103 
105  if (buffer_) {
106  grpc_byte_buffer_destroy(buffer_);
107  }
108  }
109 
114  if (this != &buf) {
115  Clear(); // first remove existing data
116  }
117  if (buf.buffer_) {
118  // then copy
119  buffer_ = grpc_byte_buffer_copy(buf.buffer_);
120  }
121  return *this;
122  }
123 
124  // If this ByteBuffer's representation is a single flat slice, returns a
125  // slice referencing that array.
126  Status TrySingleSlice(Slice* slice) const;
127 
129  Status DumpToSingleSlice(Slice* slice) const;
130 
132  Status Dump(std::vector<Slice>* slices) const;
133 
135  void Clear() {
136  if (buffer_) {
137  grpc_byte_buffer_destroy(buffer_);
138  buffer_ = nullptr;
139  }
140  }
141 
147  void Duplicate() { buffer_ = grpc_byte_buffer_copy(buffer_); }
148 
151  void Release() { buffer_ = nullptr; }
152 
154  size_t Length() const {
155  return buffer_ == nullptr ? 0 : grpc_byte_buffer_length(buffer_);
156  }
157 
159  void Swap(ByteBuffer* other) {
160  grpc_byte_buffer* tmp = other->buffer_;
161  other->buffer_ = buffer_;
162  buffer_ = tmp;
163  }
164 
166  bool Valid() const { return (buffer_ != nullptr); }
167 
168  private:
169  friend class SerializationTraits<ByteBuffer, void>;
170  friend class ServerInterface;
172  template <class R>
175  template <class RequestType>
177  grpc::Status*, RequestType*);
178  template <class ServiceType, class RequestType, class ResponseType>
180  template <class RequestType, class ResponseType>
182  template <class RequestType, class ResponseType>
184  template <class RequestType>
186  template <StatusCode code>
188  template <class R>
190  friend class ProtoBufferReader;
191  friend class ProtoBufferWriter;
194 
195  grpc_byte_buffer* buffer_;
196 
197  // takes ownership
198  void set_buffer(grpc_byte_buffer* buf) {
199  if (buffer_) {
200  Clear();
201  }
202  buffer_ = buf;
203  }
204 
205  grpc_byte_buffer* c_buffer() { return buffer_; }
206  grpc_byte_buffer** c_buffer_ptr() { return &buffer_; }
207 
208  class ByteBufferPointer {
209  public:
210  // NOLINTNEXTLINE(google-explicit-constructor)
211  ByteBufferPointer(const ByteBuffer* b)
212  : bbuf_(const_cast<ByteBuffer*>(b)) {}
213  // NOLINTNEXTLINE(google-explicit-constructor)
214  operator ByteBuffer*() { return bbuf_; }
215  // NOLINTNEXTLINE(google-explicit-constructor)
216  operator grpc_byte_buffer*() { return bbuf_->buffer_; }
217  // NOLINTNEXTLINE(google-explicit-constructor)
218  operator grpc_byte_buffer**() { return &bbuf_->buffer_; }
219 
220  private:
221  ByteBuffer* bbuf_;
222  };
223  ByteBufferPointer bbuf_ptr() const { return ByteBufferPointer(this); }
224 };
225 
226 template <>
228  public:
229  static Status Deserialize(ByteBuffer* byte_buffer, ByteBuffer* dest) {
230  dest->set_buffer(byte_buffer->buffer_);
231  return Status::OK;
232  }
233  static Status Serialize(const ByteBuffer& source, ByteBuffer* buffer,
234  bool* own_buffer) {
235  *buffer = source;
236  *own_buffer = true;
237  return grpc::Status::OK;
238  }
239 };
240 
241 } // namespace grpc
242 
243 #endif // GRPCPP_SUPPORT_BYTE_BUFFER_H
grpc::ProtoBufferWriter
This is a specialization of the protobuf class ZeroCopyOutputStream.
Definition: proto_buffer_writer.h:56
grpc::internal::CallOpGenericRecvMessage
Definition: call_op_set.h:528
grpc::ByteBuffer::Duplicate
void Duplicate()
Make a duplicate copy of the internals of this byte buffer so that we have our own owned version of i...
Definition: byte_buffer.h:147
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
grpc::internal::CallOpSendMessage
Definition: call_op_set.h:287
grpc::ByteBuffer::Length
size_t Length() const
Buffer size in bytes.
Definition: byte_buffer.h:154
grpc::internal::ErrorMethodHandler
General method handler class for errors that prevent real method use e.g., handle unknown method by r...
Definition: completion_queue.h:81
grpc_byte_buffer_destroy
GRPCAPI void grpc_byte_buffer_destroy(grpc_byte_buffer *bb)
Destroys byte_buffer deallocating all its memory.
grpc::ByteBuffer::ByteBuffer
ByteBuffer(const Slice *slices, size_t nslices)
Construct buffer from slices, of which there are nslices.
Definition: byte_buffer.h:73
grpc::ByteBuffer::operator=
ByteBuffer & operator=(const ByteBuffer &buf)
Wrapper of core function grpc_byte_buffer_copy .
Definition: byte_buffer.h:113
status.h
grpc::ByteBuffer::DumpToSingleSlice
Status DumpToSingleSlice(Slice *slice) const
Dump (read) the buffer contents into slics.
grpc_byte_buffer_length
GRPCAPI size_t grpc_byte_buffer_length(grpc_byte_buffer *bb)
Returns the size of the given byte buffer, in bytes.
grpc::ByteBuffer::~ByteBuffer
~ByteBuffer()
Definition: byte_buffer.h:104
grpc::ByteBuffer::ByteBuffer
ByteBuffer()
Construct an empty buffer.
Definition: byte_buffer.h:70
grpc::Status
Did it work? If it didn't, why?
Definition: status.h:34
grpc::internal::UnaryDeserializeHelper
void * UnaryDeserializeHelper(grpc_byte_buffer *, grpc::Status *, RequestType *)
A helper function with reduced templating to do deserializing.
Definition: method_handler.h:82
grpc::ByteBuffer::Clear
void Clear()
Remove all data.
Definition: byte_buffer.h:135
grpc::SerializationTraits< ByteBuffer, void >::Deserialize
static Status Deserialize(ByteBuffer *byte_buffer, ByteBuffer *dest)
Definition: byte_buffer.h:229
grpc.h
grpc::internal::DeserializeFuncType
Definition: call_op_set.h:515
grpc_byte_buffer
Definition: grpc_types.h:41
grpc::ByteBuffer
A sequence of bytes.
Definition: byte_buffer.h:67
grpc::internal::CallbackUnaryHandler
Definition: server_callback_handlers.h:37
grpc::SerializationTraits< ByteBuffer, void >::Serialize
static Status Serialize(const ByteBuffer &source, ByteBuffer *buffer, bool *own_buffer)
Definition: byte_buffer.h:233
grpc::ByteBuffer::Swap
void Swap(ByteBuffer *other)
Swap the state of *this and *other.
Definition: byte_buffer.h:159
grpc::ProtoBufferReader
This is a specialization of the protobuf class ZeroCopyInputStream The principle is to get one chunk ...
Definition: proto_buffer_reader.h:47
grpc::internal::CallbackServerStreamingHandler
Definition: server_callback_handlers.h:451
grpc_slice
A grpc_slice s, if initialized, represents the byte range s.bytes[0..s.length-1].
Definition: slice_type.h:62
grpc::SerializationTraits
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:63
grpc_byte_buffer_copy
GRPCAPI grpc_byte_buffer * grpc_byte_buffer_copy(grpc_byte_buffer *bb)
Copies input byte buffer bb.
grpc::ByteBuffer::GrpcByteBufferPeer
friend class internal::GrpcByteBufferPeer
Definition: byte_buffer.h:192
grpc::Slice
A wrapper around grpc_slice.
Definition: slice.h:33
byte_buffer.h
grpc::experimental::internal::CallbackSessionHandler
Definition: server_callback_handlers.h:911
serialization_traits.h
grpc::internal::ServerStreamingHandler
A wrapper class of an application provided server streaming handler.
Definition: completion_queue.h:77
grpc::Status::OK
static const Status & OK
An OK pre-defined instance.
Definition: status.h:111
grpc::ByteBuffer::Dump
Status Dump(std::vector< Slice > *slices) const
Dump (read) the buffer contents into slices.
config.h
grpc::ByteBuffer::ByteBuffer
ByteBuffer(const ByteBuffer &buf)
Construct a byte buffer by referencing elements of existing buffer buf.
Definition: byte_buffer.h:102
grpc::ByteBuffer::ExternalConnectionAcceptorImpl
friend class internal::ExternalConnectionAcceptorImpl
Definition: byte_buffer.h:193
grpc::ByteBuffer::Valid
bool Valid() const
Is this ByteBuffer valid?
Definition: byte_buffer.h:166
grpc::internal::CallOpRecvMessage
Definition: call_op_set.h:427
grpc::ByteBuffer::Release
void Release()
Forget underlying byte buffer without destroying Use this only for un-owned byte buffers.
Definition: byte_buffer.h:151
grpc::ServerInterface
Definition: server_interface.h:59
slice.h
grpc::ByteBuffer::TrySingleSlice
Status TrySingleSlice(Slice *slice) const
grpc_raw_byte_buffer_create
GRPCAPI grpc_byte_buffer * grpc_raw_byte_buffer_create(grpc_slice *slices, size_t nslices)
Returns a RAW byte buffer instance over the given slices (up to nslices).