GRPC C++  1.17.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 ServiceType, class RequestType, class ResponseType>
50 template <StatusCode code>
52 template <class R>
54 class GrpcByteBufferPeer;
55 template <class ServiceType, class RequestType, class ResponseType>
56 class RpcMethodHandler;
57 template <class ServiceType, class RequestType, class ResponseType>
59 
60 } // namespace internal
62 class ByteBuffer final {
63  public:
65  ByteBuffer() : buffer_(nullptr) {}
66 
68  ByteBuffer(const Slice* slices, size_t nslices) {
69  // The following assertions check that the representation of a grpc::Slice
70  // is identical to that of a grpc_slice: it has a grpc_slice field, and
71  // nothing else.
72  static_assert(std::is_same<decltype(slices[0].slice_), grpc_slice>::value,
73  "Slice must have same representation as grpc_slice");
74  static_assert(sizeof(Slice) == sizeof(grpc_slice),
75  "Slice must have same representation as grpc_slice");
76  // The following assertions check that the representation of a ByteBuffer is
77  // identical to grpc_byte_buffer*: it has a grpc_byte_buffer* field,
78  // and nothing else.
79  static_assert(std::is_same<decltype(buffer_), grpc_byte_buffer*>::value,
80  "ByteBuffer must have same representation as "
81  "grpc_byte_buffer*");
82  static_assert(sizeof(ByteBuffer) == sizeof(grpc_byte_buffer*),
83  "ByteBuffer must have same representation as "
84  "grpc_byte_buffer*");
85  // The const_cast is legal if grpc_raw_byte_buffer_create() does no more
86  // than its advertised side effect of increasing the reference count of the
87  // slices it processes, and such an increase does not affect the semantics
88  // seen by the caller of this constructor.
90  reinterpret_cast<grpc_slice*>(const_cast<Slice*>(slices)), nslices);
91  }
92 
95  ByteBuffer(const ByteBuffer& buf);
96 
98  if (buffer_) {
100  }
101  }
102 
103  ByteBuffer& operator=(const ByteBuffer&);
104 
106  Status Dump(std::vector<Slice>* slices) const;
107 
109  void Clear() {
110  if (buffer_) {
112  buffer_ = nullptr;
113  }
114  }
115 
119  void Duplicate() {
121  }
122 
125  void Release() { buffer_ = nullptr; }
126 
128  size_t Length() const {
129  return buffer_ == nullptr
130  ? 0
132  }
133 
135  void Swap(ByteBuffer* other) {
136  grpc_byte_buffer* tmp = other->buffer_;
137  other->buffer_ = buffer_;
138  buffer_ = tmp;
139  }
140 
142  bool Valid() const { return (buffer_ != nullptr); }
143 
144  private:
145  friend class SerializationTraits<ByteBuffer, void>;
146  friend class ServerInterface;
148  template <class R>
151  template <class ServiceType, class RequestType, class ResponseType>
152  friend class RpcMethodHandler;
153  template <class ServiceType, class RequestType, class ResponseType>
154  friend class ServerStreamingHandler;
155  template <class ServiceType, class RequestType, class ResponseType>
157  template <class ServiceType, class RequestType, class ResponseType>
159  template <class ServiceType, class RequestType, class ResponseType>
161  template <StatusCode code>
163  template <class R>
165  friend class ProtoBufferReader;
166  friend class ProtoBufferWriter;
167  friend class internal::GrpcByteBufferPeer;
168 
169  grpc_byte_buffer* buffer_;
170 
171  // takes ownership
172  void set_buffer(grpc_byte_buffer* buf) {
173  if (buffer_) {
174  Clear();
175  }
176  buffer_ = buf;
177  }
178 
179  grpc_byte_buffer* c_buffer() { return buffer_; }
180  grpc_byte_buffer** c_buffer_ptr() { return &buffer_; }
181 
182  class ByteBufferPointer {
183  public:
184  ByteBufferPointer(const ByteBuffer* b)
185  : bbuf_(const_cast<ByteBuffer*>(b)) {}
186  operator ByteBuffer*() { return bbuf_; }
187  operator grpc_byte_buffer*() { return bbuf_->buffer_; }
188  operator grpc_byte_buffer**() { return &bbuf_->buffer_; }
189 
190  private:
191  ByteBuffer* bbuf_;
192  };
193  ByteBufferPointer bbuf_ptr() const { return ByteBufferPointer(this); }
194 };
195 
196 template <>
198  public:
199  static Status Deserialize(ByteBuffer* byte_buffer, ByteBuffer* dest) {
200  dest->set_buffer(byte_buffer->buffer_);
201  return Status::OK;
202  }
203  static Status Serialize(const ByteBuffer& source, ByteBuffer* buffer,
204  bool* own_buffer) {
205  *buffer = source;
206  *own_buffer = true;
207  return Status::OK;
208  }
209 };
210 
211 } // namespace grpc
212 
213 #endif // GRPCPP_IMPL_CODEGEN_BYTE_BUFFER_H
void Clear()
Remove all data.
Definition: byte_buffer.h:109
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:203
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:128
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:119
Definition: grpc_types.h:40
ByteBuffer(const Slice *slices, size_t nslices)
Construct buffer from slices, of which there are nslices.
Definition: byte_buffer.h:68
bool Valid() const
Is this ByteBuffer valid?
Definition: byte_buffer.h:142
void Swap(ByteBuffer *other)
Swap the state of *this and *other.
Definition: byte_buffer.h:135
Definition: byte_buffer.h:53
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:125
static Status Deserialize(ByteBuffer *byte_buffer, ByteBuffer *dest)
Definition: byte_buffer.h:199
Definition: call_op_set.h:293
An Alarm posts the user provided tag to its associated completion queue upon expiry or cancellation...
Definition: alarm.h:33
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:50
virtual grpc_byte_buffer * grpc_raw_byte_buffer_create(grpc_slice *slice, size_t nslices)=0
Definition: byte_buffer.h:41
Definition: server_interface.h:50
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:51
Did it work? If it didn&#39;t, why?
Definition: status.h:31
~ByteBuffer()
Definition: byte_buffer.h:97
Definition: call_op_set.h:453
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:65
A sequence of bytes.
Definition: byte_buffer.h:62
virtual size_t grpc_byte_buffer_length(grpc_byte_buffer *bb) GRPC_MUST_USE_RESULT=0