GRPC C++  1.20.0
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 class CallOpSendMessage;
40 template <class R>
43 class MethodHandler;
44 template <class ServiceType, class RequestType, class ResponseType>
46 template <class ServiceType, class RequestType, class ResponseType>
48 template <class RequestType, class ResponseType>
50 template <class RequestType, class ResponseType>
52 template <StatusCode code>
54 template <class R>
56 class GrpcByteBufferPeer;
57 template <class ServiceType, class RequestType, class ResponseType>
58 class RpcMethodHandler;
59 template <class ServiceType, class RequestType, class ResponseType>
61 
62 } // namespace internal
64 class ByteBuffer final {
65  public:
67  ByteBuffer() : buffer_(nullptr) {}
68 
70  ByteBuffer(const Slice* slices, size_t nslices) {
71  // The following assertions check that the representation of a grpc::Slice
72  // is identical to that of a grpc_slice: it has a grpc_slice field, and
73  // nothing else.
74  static_assert(std::is_same<decltype(slices[0].slice_), grpc_slice>::value,
75  "Slice must have same representation as grpc_slice");
76  static_assert(sizeof(Slice) == sizeof(grpc_slice),
77  "Slice must have same representation as grpc_slice");
78  // The following assertions check that the representation of a ByteBuffer is
79  // identical to grpc_byte_buffer*: it has a grpc_byte_buffer* field,
80  // and nothing else.
81  static_assert(std::is_same<decltype(buffer_), grpc_byte_buffer*>::value,
82  "ByteBuffer must have same representation as "
83  "grpc_byte_buffer*");
84  static_assert(sizeof(ByteBuffer) == sizeof(grpc_byte_buffer*),
85  "ByteBuffer must have same representation as "
86  "grpc_byte_buffer*");
87  // The const_cast is legal if grpc_raw_byte_buffer_create() does no more
88  // than its advertised side effect of increasing the reference count of the
89  // slices it processes, and such an increase does not affect the semantics
90  // seen by the caller of this constructor.
92  reinterpret_cast<grpc_slice*>(const_cast<Slice*>(slices)), nslices);
93  }
94 
99  ByteBuffer(const ByteBuffer& buf) : buffer_(nullptr) { operator=(buf); }
100 
102  if (buffer_) {
104  }
105  }
106 
111  if (this != &buf) {
112  Clear(); // first remove existing data
113  }
114  if (buf.buffer_) {
115  // then copy
116  buffer_ = g_core_codegen_interface->grpc_byte_buffer_copy(buf.buffer_);
117  }
118  return *this;
119  }
120 
122  Status Dump(std::vector<Slice>* slices) const;
123 
125  void Clear() {
126  if (buffer_) {
128  buffer_ = nullptr;
129  }
130  }
131 
137  void Duplicate() {
139  }
140 
143  void Release() { buffer_ = nullptr; }
144 
146  size_t Length() const {
147  return buffer_ == nullptr
148  ? 0
150  }
151 
153  void Swap(ByteBuffer* other) {
154  grpc_byte_buffer* tmp = other->buffer_;
155  other->buffer_ = buffer_;
156  buffer_ = tmp;
157  }
158 
160  bool Valid() const { return (buffer_ != nullptr); }
161 
162  private:
163  friend class SerializationTraits<ByteBuffer, void>;
164  friend class ServerInterface;
166  template <class R>
169  template <class ServiceType, class RequestType, class ResponseType>
170  friend class RpcMethodHandler;
171  template <class ServiceType, class RequestType, class ResponseType>
172  friend class ServerStreamingHandler;
173  template <class ServiceType, class RequestType, class ResponseType>
175  template <class ServiceType, class RequestType, class ResponseType>
177  template <class RequestType, class ResponseType>
179  template <class RequestType, class ResponseType>
180  friend class ::grpc::internal::CallbackServerStreamingHandler;
181  template <StatusCode code>
183  template <class R>
185  friend class ProtoBufferReader;
186  friend class ProtoBufferWriter;
187  friend class internal::GrpcByteBufferPeer;
188 
189  grpc_byte_buffer* buffer_;
190 
191  // takes ownership
192  void set_buffer(grpc_byte_buffer* buf) {
193  if (buffer_) {
194  Clear();
195  }
196  buffer_ = buf;
197  }
198 
199  grpc_byte_buffer* c_buffer() { return buffer_; }
200  grpc_byte_buffer** c_buffer_ptr() { return &buffer_; }
201 
202  class ByteBufferPointer {
203  public:
204  ByteBufferPointer(const ByteBuffer* b)
205  : bbuf_(const_cast<ByteBuffer*>(b)) {}
206  operator ByteBuffer*() { return bbuf_; }
207  operator grpc_byte_buffer*() { return bbuf_->buffer_; }
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 g_core_codegen_interface->ok();
228  }
229 };
230 
231 } // namespace grpc
232 
233 #endif // GRPCPP_IMPL_CODEGEN_BYTE_BUFFER_H
void Clear()
Remove all data.
Definition: byte_buffer.h:125
A wrapper class of an application provided server streaming handler.
Definition: byte_buffer.h:47
static Status Serialize(const ByteBuffer &source, ByteBuffer *buffer, bool *own_buffer)
Definition: byte_buffer.h:223
This is a specialization of the protobuf class ZeroCopyOutputStream.
Definition: proto_buffer_writer.h:53
size_t Length() const
Buffer size in bytes.
Definition: byte_buffer.h:146
A wrapper around grpc_slice.
Definition: slice.h:35
A grpc_slice s, if initialized, represents the byte range s.bytes[0..s.length-1]. ...
Definition: slice.h:80
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:137
Definition: grpc_types.h:40
ByteBuffer & operator=(const ByteBuffer &buf)
Wrapper of core function grpc_byte_buffer_copy .
Definition: byte_buffer.h:110
ByteBuffer(const Slice *slices, size_t nslices)
Construct buffer from slices, of which there are nslices.
Definition: byte_buffer.h:70
bool Valid() const
Is this ByteBuffer valid?
Definition: byte_buffer.h:160
void Swap(ByteBuffer *other)
Swap the state of *this and *other.
Definition: byte_buffer.h:153
Definition: byte_buffer.h:55
virtual void grpc_byte_buffer_destroy(grpc_byte_buffer *bb)=0
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:58
void Release()
Forget underlying byte buffer without destroying Use this only for un-owned byte buffers.
Definition: byte_buffer.h:143
static Status Deserialize(ByteBuffer *byte_buffer, ByteBuffer *dest)
Definition: byte_buffer.h:219
Definition: call_op_set.h:294
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
A wrapper class of an application provided rpc method handler.
Definition: byte_buffer.h:45
CoreCodegenInterface * g_core_codegen_interface
Definition: call_op_set.h:51
virtual grpc_byte_buffer * grpc_raw_byte_buffer_create(grpc_slice *slice, size_t nslices)=0
ByteBuffer(const ByteBuffer &buf)
Constuct a byte buffer by referencing elements of existing buffer buf.
Definition: byte_buffer.h:99
Definition: byte_buffer.h:41
Definition: server_interface.h:54
virtual grpc_byte_buffer * grpc_byte_buffer_copy(grpc_byte_buffer *bb)=0
Definition: byte_buffer.h:49
Base class for running an RPC handler.
Definition: rpc_service_method.h:39
General method handler class for errors that prevent real method use e.g., handle unknown method by r...
Definition: byte_buffer.h:53
Did it work? If it didn&#39;t, why?
Definition: status.h:31
~ByteBuffer()
Definition: byte_buffer.h:101
virtual const Status & ok()=0
Definition: call_op_set.h:523
This is a specialization of the protobuf class ZeroCopyInputStream The principle is to get one chunk ...
Definition: proto_buffer_reader.h:46
static const Status & OK
An OK pre-defined instance.
Definition: status.h:105
ByteBuffer()
Constuct an empty buffer.
Definition: byte_buffer.h:67
A sequence of bytes.
Definition: byte_buffer.h:64
virtual size_t grpc_byte_buffer_length(grpc_byte_buffer *bb) GRPC_MUST_USE_RESULT=0