GRPC C++  1.70.1
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
59 class ByteBuffer final {
60  public:
62  ByteBuffer() : buffer_(nullptr) {}
63 
65  ByteBuffer(const Slice* slices, size_t nslices) {
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,
70  "Slice must have same representation as grpc_slice");
71  static_assert(sizeof(Slice) == sizeof(grpc_slice),
72  "Slice must have same representation as grpc_slice");
73  // The following assertions check that the representation of a ByteBuffer is
74  // identical to grpc_byte_buffer*: it has a grpc_byte_buffer* field,
75  // and nothing else.
76  static_assert(std::is_same<decltype(buffer_), grpc_byte_buffer*>::value,
77  "ByteBuffer must have same representation as "
78  "grpc_byte_buffer*");
79  static_assert(sizeof(ByteBuffer) == sizeof(grpc_byte_buffer*),
80  "ByteBuffer must have same representation as "
81  "grpc_byte_buffer*");
82  // The const_cast is legal if grpc_raw_byte_buffer_create() does no more
83  // than its advertised side effect of increasing the reference count of the
84  // slices it processes, and such an increase does not affect the semantics
85  // seen by the caller of this constructor.
87  reinterpret_cast<grpc_slice*>(const_cast<Slice*>(slices)), nslices);
88  }
89 
94  ByteBuffer(const ByteBuffer& buf) : buffer_(nullptr) { operator=(buf); }
95 
97  if (buffer_) {
98  grpc_byte_buffer_destroy(buffer_);
99  }
100  }
101 
106  if (this != &buf) {
107  Clear(); // first remove existing data
108  }
109  if (buf.buffer_) {
110  // then copy
111  buffer_ = grpc_byte_buffer_copy(buf.buffer_);
112  }
113  return *this;
114  }
115 
116  // If this ByteBuffer's representation is a single flat slice, returns a
117  // slice referencing that array.
118  Status TrySingleSlice(Slice* slice) const;
119 
121  Status DumpToSingleSlice(Slice* slice) const;
122 
124  Status Dump(std::vector<Slice>* slices) const;
125 
127  void Clear() {
128  if (buffer_) {
129  grpc_byte_buffer_destroy(buffer_);
130  buffer_ = nullptr;
131  }
132  }
133 
139  void Duplicate() { buffer_ = grpc_byte_buffer_copy(buffer_); }
140 
143  void Release() { buffer_ = nullptr; }
144 
146  size_t Length() const {
147  return buffer_ == nullptr ? 0 : grpc_byte_buffer_length(buffer_);
148  }
149 
151  void Swap(ByteBuffer* other) {
152  grpc_byte_buffer* tmp = other->buffer_;
153  other->buffer_ = buffer_;
154  buffer_ = tmp;
155  }
156 
158  bool Valid() const { return (buffer_ != nullptr); }
159 
160  private:
161  friend class SerializationTraits<ByteBuffer, void>;
162  friend class ServerInterface;
164  template <class R>
167  template <class RequestType>
169  grpc::Status*, RequestType*);
170  template <class ServiceType, class RequestType, class ResponseType>
172  template <class RequestType, class ResponseType>
174  template <class RequestType, class ResponseType>
176  template <StatusCode code>
178  template <class R>
180  friend class ProtoBufferReader;
181  friend class ProtoBufferWriter;
184 
185  grpc_byte_buffer* buffer_;
186 
187  // takes ownership
188  void set_buffer(grpc_byte_buffer* buf) {
189  if (buffer_) {
190  Clear();
191  }
192  buffer_ = buf;
193  }
194 
195  grpc_byte_buffer* c_buffer() { return buffer_; }
196  grpc_byte_buffer** c_buffer_ptr() { return &buffer_; }
197 
198  class ByteBufferPointer {
199  public:
200  // NOLINTNEXTLINE(google-explicit-constructor)
201  ByteBufferPointer(const ByteBuffer* b)
202  : bbuf_(const_cast<ByteBuffer*>(b)) {}
203  // NOLINTNEXTLINE(google-explicit-constructor)
204  operator ByteBuffer*() { return bbuf_; }
205  // NOLINTNEXTLINE(google-explicit-constructor)
206  operator grpc_byte_buffer*() { return bbuf_->buffer_; }
207  // NOLINTNEXTLINE(google-explicit-constructor)
208  operator grpc_byte_buffer**() { return &bbuf_->buffer_; }
209 
210  private:
211  ByteBuffer* bbuf_;
212  };
213  ByteBufferPointer bbuf_ptr() const { return ByteBufferPointer(this); }
214 };
215 
216 template <>
218  public:
219  static Status Deserialize(ByteBuffer* byte_buffer, ByteBuffer* dest) {
220  dest->set_buffer(byte_buffer->buffer_);
221  return Status::OK;
222  }
223  static Status Serialize(const ByteBuffer& source, ByteBuffer* buffer,
224  bool* own_buffer) {
225  *buffer = source;
226  *own_buffer = true;
227  return grpc::Status::OK;
228  }
229 };
230 
231 } // namespace grpc
232 
233 #endif // GRPCPP_SUPPORT_BYTE_BUFFER_H
grpc::ProtoBufferWriter
This is a specialization of the protobuf class ZeroCopyOutputStream.
Definition: proto_buffer_writer.h:55
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:139
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:288
grpc::ByteBuffer::Length
size_t Length() const
Buffer size in bytes.
Definition: byte_buffer.h:146
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:65
grpc::ByteBuffer::operator=
ByteBuffer & operator=(const ByteBuffer &buf)
Wrapper of core function grpc_byte_buffer_copy .
Definition: byte_buffer.h:105
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:96
grpc::ByteBuffer::ByteBuffer
ByteBuffer()
Construct an empty buffer.
Definition: byte_buffer.h:62
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:127
grpc::SerializationTraits< ByteBuffer, void >::Deserialize
static Status Deserialize(ByteBuffer *byte_buffer, ByteBuffer *dest)
Definition: byte_buffer.h:219
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:59
grpc::internal::CallbackUnaryHandler
Definition: server_callback_handlers.h:35
grpc::SerializationTraits< ByteBuffer, void >::Serialize
static Status Serialize(const ByteBuffer &source, ByteBuffer *buffer, bool *own_buffer)
Definition: byte_buffer.h:223
grpc::ByteBuffer::Swap
void Swap(ByteBuffer *other)
Swap the state of *this and *other.
Definition: byte_buffer.h:151
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:448
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:58
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:182
grpc::Slice
A wrapper around grpc_slice.
Definition: slice.h:33
byte_buffer.h
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:112
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:94
grpc::ByteBuffer::ExternalConnectionAcceptorImpl
friend class internal::ExternalConnectionAcceptorImpl
Definition: byte_buffer.h:183
grpc::ByteBuffer::Valid
bool Valid() const
Is this ByteBuffer valid?
Definition: byte_buffer.h:158
grpc::internal::CallOpRecvMessage
Definition: call_op_set.h:426
grpc::ByteBuffer::Release
void Release()
Forget underlying byte buffer without destroying Use this only for un-owned byte buffers.
Definition: byte_buffer.h:143
grpc::ServerInterface
Definition: server_interface.h:60
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).