GRPC C++  1.39.1
byte_buffer.h
Go to the documentation of this file.
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 
19 #ifndef GRPCPP_IMPL_CODEGEN_BYTE_BUFFER_H
20 #define GRPCPP_IMPL_CODEGEN_BYTE_BUFFER_H
21 
23 
29 
30 #include <vector>
31 
32 namespace grpc {
33 
34 class ServerInterface;
35 class ByteBuffer;
36 class ServerInterface;
37 
38 namespace internal {
39 template <class RequestType, class ResponseType>
40 class CallbackUnaryHandler;
41 template <class RequestType, class ResponseType>
42 class CallbackServerStreamingHandler;
43 template <class RequestType>
44 void* UnaryDeserializeHelper(grpc_byte_buffer*, ::grpc::Status*, RequestType*);
45 template <class ServiceType, class RequestType, class ResponseType>
46 class ServerStreamingHandler;
47 template <::grpc::StatusCode code>
48 class ErrorMethodHandler;
49 class CallOpSendMessage;
50 template <class R>
51 class CallOpRecvMessage;
52 class CallOpGenericRecvMessage;
53 class ExternalConnectionAcceptorImpl;
54 template <class R>
55 class DeserializeFuncType;
56 class GrpcByteBufferPeer;
57 
58 } // namespace internal
60 class ByteBuffer final {
61  public:
63  ByteBuffer() : buffer_(nullptr) {}
64 
66  ByteBuffer(const Slice* slices, size_t nslices) {
67  // The following assertions check that the representation of a grpc::Slice
68  // is identical to that of a grpc_slice: it has a grpc_slice field, and
69  // nothing else.
70  static_assert(std::is_same<decltype(slices[0].slice_), grpc_slice>::value,
71  "Slice must have same representation as grpc_slice");
72  static_assert(sizeof(Slice) == sizeof(grpc_slice),
73  "Slice must have same representation as grpc_slice");
74  // The following assertions check that the representation of a ByteBuffer is
75  // identical to grpc_byte_buffer*: it has a grpc_byte_buffer* field,
76  // and nothing else.
77  static_assert(std::is_same<decltype(buffer_), grpc_byte_buffer*>::value,
78  "ByteBuffer must have same representation as "
79  "grpc_byte_buffer*");
80  static_assert(sizeof(ByteBuffer) == sizeof(grpc_byte_buffer*),
81  "ByteBuffer must have same representation as "
82  "grpc_byte_buffer*");
83  // The const_cast is legal if grpc_raw_byte_buffer_create() does no more
84  // than its advertised side effect of increasing the reference count of the
85  // slices it processes, and such an increase does not affect the semantics
86  // seen by the caller of this constructor.
88  reinterpret_cast<grpc_slice*>(const_cast<Slice*>(slices)), nslices);
89  }
90 
95  ByteBuffer(const ByteBuffer& buf) : buffer_(nullptr) { operator=(buf); }
96 
98  if (buffer_) {
100  }
101  }
102 
107  if (this != &buf) {
108  Clear(); // first remove existing data
109  }
110  if (buf.buffer_) {
111  // then copy
112  buffer_ = g_core_codegen_interface->grpc_byte_buffer_copy(buf.buffer_);
113  }
114  return *this;
115  }
116 
117  // If this ByteBuffer's representation is a single flat slice, returns a
118  // slice referencing that array.
119  Status TrySingleSlice(Slice* slice) const;
120 
123 
125  Status Dump(std::vector<Slice>* slices) const;
126 
128  void Clear() {
129  if (buffer_) {
131  buffer_ = nullptr;
132  }
133  }
134 
140  void Duplicate() {
142  }
143 
146  void Release() { buffer_ = nullptr; }
147 
149  size_t Length() const {
150  return buffer_ == nullptr
151  ? 0
153  }
154 
156  void Swap(ByteBuffer* other) {
157  grpc_byte_buffer* tmp = other->buffer_;
158  other->buffer_ = buffer_;
159  buffer_ = tmp;
160  }
161 
163  bool Valid() const { return (buffer_ != nullptr); }
164 
165  private:
166  friend class SerializationTraits<ByteBuffer, void>;
167  friend class ServerInterface;
169  template <class R>
172  template <class RequestType>
174  ::grpc::Status*, RequestType*);
175  template <class ServiceType, class RequestType, class ResponseType>
177  template <class RequestType, class ResponseType>
179  template <class RequestType, class ResponseType>
181  template <StatusCode code>
183  template <class R>
185  friend class ProtoBufferReader;
186  friend class ProtoBufferWriter;
189 
190  grpc_byte_buffer* buffer_;
191 
192  // takes ownership
193  void set_buffer(grpc_byte_buffer* buf) {
194  if (buffer_) {
195  Clear();
196  }
197  buffer_ = buf;
198  }
199 
200  grpc_byte_buffer* c_buffer() { return buffer_; }
201  grpc_byte_buffer** c_buffer_ptr() { return &buffer_; }
202 
203  class ByteBufferPointer {
204  public:
205  /* NOLINTNEXTLINE(google-explicit-constructor) */
206  ByteBufferPointer(const ByteBuffer* b)
207  : bbuf_(const_cast<ByteBuffer*>(b)) {}
208  /* NOLINTNEXTLINE(google-explicit-constructor) */
209  operator ByteBuffer*() { return bbuf_; }
210  /* NOLINTNEXTLINE(google-explicit-constructor) */
211  operator grpc_byte_buffer*() { return bbuf_->buffer_; }
212  /* NOLINTNEXTLINE(google-explicit-constructor) */
213  operator grpc_byte_buffer**() { return &bbuf_->buffer_; }
214 
215  private:
216  ByteBuffer* bbuf_;
217  };
218  ByteBufferPointer bbuf_ptr() const { return ByteBufferPointer(this); }
219 };
220 
221 template <>
223  public:
224  static Status Deserialize(ByteBuffer* byte_buffer, ByteBuffer* dest) {
225  dest->set_buffer(byte_buffer->buffer_);
226  return Status::OK;
227  }
228  static Status Serialize(const ByteBuffer& source, ByteBuffer* buffer,
229  bool* own_buffer) {
230  *buffer = source;
231  *own_buffer = true;
232  return g_core_codegen_interface->ok();
233  }
234 };
235 
236 } // namespace grpc
237 
238 #endif // GRPCPP_IMPL_CODEGEN_BYTE_BUFFER_H
A sequence of bytes.
Definition: byte_buffer.h:60
void Clear()
Remove all data.
Definition: byte_buffer.h:128
friend class internal::GrpcByteBufferPeer
Definition: byte_buffer.h:187
size_t Length() const
Buffer size in bytes.
Definition: byte_buffer.h:149
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:140
void Swap(ByteBuffer *other)
Swap the state of *this and *other.
Definition: byte_buffer.h:156
ByteBuffer(const ByteBuffer &buf)
Constuct a byte buffer by referencing elements of existing buffer buf.
Definition: byte_buffer.h:95
friend class internal::ExternalConnectionAcceptorImpl
Definition: byte_buffer.h:188
Status TrySingleSlice(Slice *slice) const
ByteBuffer(const Slice *slices, size_t nslices)
Construct buffer from slices, of which there are nslices.
Definition: byte_buffer.h:66
ByteBuffer()
Constuct an empty buffer.
Definition: byte_buffer.h:63
Status DumpToSingleSlice(Slice *slice) const
Dump (read) the buffer contents into slics.
Status Dump(std::vector< Slice > *slices) const
Dump (read) the buffer contents into slices.
void Release()
Forget underlying byte buffer without destroying Use this only for un-owned byte buffers.
Definition: byte_buffer.h:146
bool Valid() const
Is this ByteBuffer valid?
Definition: byte_buffer.h:163
~ByteBuffer()
Definition: byte_buffer.h:97
ByteBuffer & operator=(const ByteBuffer &buf)
Wrapper of core function grpc_byte_buffer_copy .
Definition: byte_buffer.h:106
virtual grpc_byte_buffer * grpc_byte_buffer_copy(grpc_byte_buffer *bb)=0
virtual grpc_byte_buffer * grpc_raw_byte_buffer_create(grpc_slice *slice, size_t nslices)=0
virtual const Status & ok()=0
virtual void grpc_byte_buffer_destroy(grpc_byte_buffer *bb)=0
virtual size_t grpc_byte_buffer_length(grpc_byte_buffer *bb) GRPC_MUST_USE_RESULT=0
This is a specialization of the protobuf class ZeroCopyInputStream The principle is to get one chunk ...
Definition: proto_buffer_reader.h:46
This is a specialization of the protobuf class ZeroCopyOutputStream.
Definition: proto_buffer_writer.h:53
static Status Deserialize(ByteBuffer *byte_buffer, ByteBuffer *dest)
Definition: byte_buffer.h:224
static Status Serialize(const ByteBuffer &source, ByteBuffer *buffer, bool *own_buffer)
Definition: byte_buffer.h:228
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:58
Definition: server_interface.h:59
A wrapper around grpc_slice.
Definition: slice.h:35
Did it work? If it didn't, why?
Definition: status.h:31
static const Status & OK
An OK pre-defined instance.
Definition: status.h:105
Definition: call_op_set.h:526
Definition: call_op_set.h:424
Definition: call_op_set.h:282
Definition: server_callback_handlers.h:444
Definition: server_callback_handlers.h:31
Definition: call_op_set.h:513
General method handler class for errors that prevent real method use e.g., handle unknown method by r...
Definition: method_handler.h:358
A wrapper class of an application provided server streaming handler.
Definition: method_handler.h:189
void * UnaryDeserializeHelper(grpc_byte_buffer *, ::grpc::Status *, RequestType *)
A helper function with reduced templating to do deserializing.
Definition: method_handler.h:80
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
CoreCodegenInterface * g_core_codegen_interface
Definition: completion_queue.h:96
Definition: grpc_types.h:40
A grpc_slice s, if initialized, represents the byte range s.bytes[0..s.length-1].
Definition: slice.h:60