GRPC C++  1.23.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_impl {
33 namespace internal {
34 
35 template <class RequestType, class ResponseType>
37 template <class RequestType, class ResponseType>
39 
40 } // namespace internal
41 } // namespace grpc_impl
42 
43 namespace grpc {
44 
45 class ServerInterface;
46 class ByteBuffer;
47 class ServerInterface;
48 
49 namespace internal {
50 class CallOpSendMessage;
51 template <class R>
54 class MethodHandler;
55 template <class ServiceType, class RequestType, class ResponseType>
57 template <class ServiceType, class RequestType, class ResponseType>
59 template <StatusCode code>
61 class ExternalConnectionAcceptorImpl;
62 template <class R>
64 class GrpcByteBufferPeer;
65 template <class ServiceType, class RequestType, class ResponseType>
66 class RpcMethodHandler;
67 template <class ServiceType, class RequestType, class ResponseType>
69 
70 } // namespace internal
72 class ByteBuffer final {
73  public:
75  ByteBuffer() : buffer_(nullptr) {}
76 
78  ByteBuffer(const Slice* slices, size_t nslices) {
79  // The following assertions check that the representation of a grpc::Slice
80  // is identical to that of a grpc_slice: it has a grpc_slice field, and
81  // nothing else.
82  static_assert(std::is_same<decltype(slices[0].slice_), grpc_slice>::value,
83  "Slice must have same representation as grpc_slice");
84  static_assert(sizeof(Slice) == sizeof(grpc_slice),
85  "Slice must have same representation as grpc_slice");
86  // The following assertions check that the representation of a ByteBuffer is
87  // identical to grpc_byte_buffer*: it has a grpc_byte_buffer* field,
88  // and nothing else.
89  static_assert(std::is_same<decltype(buffer_), grpc_byte_buffer*>::value,
90  "ByteBuffer must have same representation as "
91  "grpc_byte_buffer*");
92  static_assert(sizeof(ByteBuffer) == sizeof(grpc_byte_buffer*),
93  "ByteBuffer must have same representation as "
94  "grpc_byte_buffer*");
95  // The const_cast is legal if grpc_raw_byte_buffer_create() does no more
96  // than its advertised side effect of increasing the reference count of the
97  // slices it processes, and such an increase does not affect the semantics
98  // seen by the caller of this constructor.
100  reinterpret_cast<grpc_slice*>(const_cast<Slice*>(slices)), nslices);
101  }
102 
107  ByteBuffer(const ByteBuffer& buf) : buffer_(nullptr) { operator=(buf); }
108 
110  if (buffer_) {
112  }
113  }
114 
119  if (this != &buf) {
120  Clear(); // first remove existing data
121  }
122  if (buf.buffer_) {
123  // then copy
124  buffer_ = g_core_codegen_interface->grpc_byte_buffer_copy(buf.buffer_);
125  }
126  return *this;
127  }
128 
130  Status Dump(std::vector<Slice>* slices) const;
131 
133  void Clear() {
134  if (buffer_) {
136  buffer_ = nullptr;
137  }
138  }
139 
145  void Duplicate() {
147  }
148 
151  void Release() { buffer_ = nullptr; }
152 
154  size_t Length() const {
155  return buffer_ == nullptr
156  ? 0
158  }
159 
161  void Swap(ByteBuffer* other) {
162  grpc_byte_buffer* tmp = other->buffer_;
163  other->buffer_ = buffer_;
164  buffer_ = tmp;
165  }
166 
168  bool Valid() const { return (buffer_ != nullptr); }
169 
170  private:
171  friend class SerializationTraits<ByteBuffer, void>;
172  friend class ServerInterface;
174  template <class R>
177  template <class ServiceType, class RequestType, class ResponseType>
178  friend class RpcMethodHandler;
179  template <class ServiceType, class RequestType, class ResponseType>
180  friend class ServerStreamingHandler;
181  template <class ServiceType, class RequestType, class ResponseType>
183  template <class ServiceType, class RequestType, class ResponseType>
185  template <class RequestType, class ResponseType>
186  friend class ::grpc_impl::internal::CallbackUnaryHandler;
187  template <class RequestType, class ResponseType>
188  friend class ::grpc_impl::internal::CallbackServerStreamingHandler;
189  template <StatusCode code>
191  template <class R>
193  friend class ProtoBufferReader;
194  friend class ProtoBufferWriter;
195  friend class internal::GrpcByteBufferPeer;
196  friend class internal::ExternalConnectionAcceptorImpl;
197 
198  grpc_byte_buffer* buffer_;
199 
200  // takes ownership
201  void set_buffer(grpc_byte_buffer* buf) {
202  if (buffer_) {
203  Clear();
204  }
205  buffer_ = buf;
206  }
207 
208  grpc_byte_buffer* c_buffer() { return buffer_; }
209  grpc_byte_buffer** c_buffer_ptr() { return &buffer_; }
210 
211  class ByteBufferPointer {
212  public:
213  ByteBufferPointer(const ByteBuffer* b)
214  : bbuf_(const_cast<ByteBuffer*>(b)) {}
215  operator ByteBuffer*() { return bbuf_; }
216  operator grpc_byte_buffer*() { return bbuf_->buffer_; }
217  operator grpc_byte_buffer**() { return &bbuf_->buffer_; }
218 
219  private:
220  ByteBuffer* bbuf_;
221  };
222  ByteBufferPointer bbuf_ptr() const { return ByteBufferPointer(this); }
223 };
224 
225 template <>
227  public:
228  static Status Deserialize(ByteBuffer* byte_buffer, ByteBuffer* dest) {
229  dest->set_buffer(byte_buffer->buffer_);
230  return Status::OK;
231  }
232  static Status Serialize(const ByteBuffer& source, ByteBuffer* buffer,
233  bool* own_buffer) {
234  *buffer = source;
235  *own_buffer = true;
236  return g_core_codegen_interface->ok();
237  }
238 };
239 
240 } // namespace grpc
241 
242 #endif // GRPCPP_IMPL_CODEGEN_BYTE_BUFFER_H
void Clear()
Remove all data.
Definition: byte_buffer.h:133
A wrapper class of an application provided server streaming handler.
Definition: byte_buffer.h:58
static Status Serialize(const ByteBuffer &source, ByteBuffer *buffer, bool *own_buffer)
Definition: byte_buffer.h:232
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:154
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:60
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:145
Not an error; returned on success.
Definition: status_code_enum.h:26
Definition: grpc_types.h:40
ByteBuffer & operator=(const ByteBuffer &buf)
Wrapper of core function grpc_byte_buffer_copy .
Definition: byte_buffer.h:118
ByteBuffer(const Slice *slices, size_t nslices)
Construct buffer from slices, of which there are nslices.
Definition: byte_buffer.h:78
bool Valid() const
Is this ByteBuffer valid?
Definition: byte_buffer.h:168
void Swap(ByteBuffer *other)
Swap the state of *this and *other.
Definition: byte_buffer.h:161
Definition: byte_buffer.h:63
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:151
static Status Deserialize(ByteBuffer *byte_buffer, ByteBuffer *dest)
Definition: byte_buffer.h:228
Definition: call_op_set.h:288
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
Definition: byte_buffer.h:36
A wrapper class of an application provided rpc method handler.
Definition: byte_buffer.h:56
CoreCodegenInterface * g_core_codegen_interface
Definition: completion_queue_impl.h:91
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:107
Definition: byte_buffer.h:52
Definition: server_interface.h:57
virtual grpc_byte_buffer * grpc_byte_buffer_copy(grpc_byte_buffer *bb)=0
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
Base class for running an RPC handler.
Definition: rpc_service_method.h:41
General method handler class for errors that prevent real method use e.g., handle unknown method by r...
Definition: byte_buffer.h:60
Did it work? If it didn&#39;t, why?
Definition: status.h:31
~ByteBuffer()
Definition: byte_buffer.h:109
virtual const Status & ok()=0
Definition: call_op_set.h:516
This is a specialization of the protobuf class ZeroCopyInputStream The principle is to get one chunk ...
Definition: proto_buffer_reader.h:46
ByteBuffer()
Constuct an empty buffer.
Definition: byte_buffer.h:75
A sequence of bytes.
Definition: byte_buffer.h:72
virtual size_t grpc_byte_buffer_length(grpc_byte_buffer *bb) GRPC_MUST_USE_RESULT=0