GRPC C++  1.30.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 template <class ServiceType, class RequestType, class ResponseType>
41 template <class ServiceType, class RequestType, class ResponseType>
43 template <::grpc::StatusCode code>
45 
46 } // namespace internal
47 } // namespace grpc_impl
48 
49 namespace grpc {
50 
51 class ServerInterface;
52 class ByteBuffer;
53 class ServerInterface;
54 
55 namespace internal {
56 class CallOpSendMessage;
57 template <class R>
60 class ExternalConnectionAcceptorImpl;
61 template <class R>
63 class GrpcByteBufferPeer;
64 
65 } // namespace internal
67 class ByteBuffer final {
68  public:
70  ByteBuffer() : buffer_(nullptr) {}
71 
73  ByteBuffer(const Slice* slices, size_t nslices) {
74  // The following assertions check that the representation of a grpc::Slice
75  // is identical to that of a grpc_slice: it has a grpc_slice field, and
76  // nothing else.
77  static_assert(std::is_same<decltype(slices[0].slice_), grpc_slice>::value,
78  "Slice must have same representation as grpc_slice");
79  static_assert(sizeof(Slice) == sizeof(grpc_slice),
80  "Slice must have same representation as grpc_slice");
81  // The following assertions check that the representation of a ByteBuffer is
82  // identical to grpc_byte_buffer*: it has a grpc_byte_buffer* field,
83  // and nothing else.
84  static_assert(std::is_same<decltype(buffer_), grpc_byte_buffer*>::value,
85  "ByteBuffer must have same representation as "
86  "grpc_byte_buffer*");
87  static_assert(sizeof(ByteBuffer) == sizeof(grpc_byte_buffer*),
88  "ByteBuffer must have same representation as "
89  "grpc_byte_buffer*");
90  // The const_cast is legal if grpc_raw_byte_buffer_create() does no more
91  // than its advertised side effect of increasing the reference count of the
92  // slices it processes, and such an increase does not affect the semantics
93  // seen by the caller of this constructor.
95  reinterpret_cast<grpc_slice*>(const_cast<Slice*>(slices)), nslices);
96  }
97 
102  ByteBuffer(const ByteBuffer& buf) : buffer_(nullptr) { operator=(buf); }
103 
105  if (buffer_) {
107  }
108  }
109 
114  if (this != &buf) {
115  Clear(); // first remove existing data
116  }
117  if (buf.buffer_) {
118  // then copy
119  buffer_ = g_core_codegen_interface->grpc_byte_buffer_copy(buf.buffer_);
120  }
121  return *this;
122  }
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 ServiceType, class RequestType, class ResponseType>
174  template <class ServiceType, class RequestType, class ResponseType>
176  template <class RequestType, class ResponseType>
177  friend class ::grpc_impl::internal::CallbackUnaryHandler;
178  template <class RequestType, class ResponseType>
179  friend class ::grpc_impl::internal::CallbackServerStreamingHandler;
180  template <StatusCode code>
182  template <class R>
184  friend class ProtoBufferReader;
185  friend class ProtoBufferWriter;
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
grpc_impl::internal::ErrorMethodHandler
General method handler class for errors that prevent real method use e.g., handle unknown method by r...
Definition: byte_buffer.h:44
grpc::ProtoBufferWriter
This is a specialization of the protobuf class ZeroCopyOutputStream.
Definition: proto_buffer_writer.h:53
grpc::internal::RpcMethodHandler
::grpc_impl::internal::RpcMethodHandler< ServiceType, RequestType, ResponseType > RpcMethodHandler
Definition: method_handler.h:36
grpc::internal::CallOpGenericRecvMessage
Definition: call_op_set.h:524
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:140
grpc
This header provides an object that reads bytes directly from a grpc::ByteBuffer, via the ZeroCopyInp...
Definition: alarm.h:24
status.h
grpc::internal::CallOpSendMessage
Definition: call_op_set.h:286
grpc::ByteBuffer::Length
size_t Length() const
Buffer size in bytes.
Definition: byte_buffer.h:149
serialization_traits.h
config.h
grpc::CoreCodegenInterface::ok
virtual const Status & ok()=0
grpc::ByteBuffer::ByteBuffer
ByteBuffer(const Slice *slices, size_t nslices)
Construct buffer from slices, of which there are nslices.
Definition: byte_buffer.h:73
grpc::ByteBuffer::operator=
ByteBuffer & operator=(const ByteBuffer &buf)
Wrapper of core function grpc_byte_buffer_copy .
Definition: byte_buffer.h:113
core_codegen_interface.h
grpc_impl::internal::CallbackServerStreamingHandler
Definition: byte_buffer.h:38
grpc::ByteBuffer::~ByteBuffer
~ByteBuffer()
Definition: byte_buffer.h:104
grpc::CoreCodegenInterface::grpc_byte_buffer_copy
virtual grpc_byte_buffer * grpc_byte_buffer_copy(grpc_byte_buffer *bb)=0
grpc::CoreCodegenInterface::grpc_byte_buffer_destroy
virtual void grpc_byte_buffer_destroy(grpc_byte_buffer *bb)=0
grpc::ByteBuffer::ByteBuffer
ByteBuffer()
Constuct an empty buffer.
Definition: byte_buffer.h:70
grpc::Status
Did it work? If it didn't, why?
Definition: status.h:31
grpc::internal::ServerStreamingHandler
::grpc_impl::internal::ServerStreamingHandler< ServiceType, RequestType, ResponseType > ServerStreamingHandler
Definition: method_handler.h:46
grpc::ByteBuffer::Clear
void Clear()
Remove all data.
Definition: byte_buffer.h:128
grpc::SerializationTraits< ByteBuffer, void >::Deserialize
static Status Deserialize(ByteBuffer *byte_buffer, ByteBuffer *dest)
Definition: byte_buffer.h:219
grpc::internal::DeserializeFuncType
Definition: byte_buffer.h:62
grpc_byte_buffer
Definition: grpc_types.h:40
grpc::ByteBuffer
A sequence of bytes.
Definition: byte_buffer.h:67
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:156
grpc::ProtoBufferReader
This is a specialization of the protobuf class ZeroCopyInputStream The principle is to get one chunk ...
Definition: proto_buffer_reader.h:46
grpc_impl::internal::RpcMethodHandler
A wrapper class of an application provided rpc method handler.
Definition: byte_buffer.h:40
grpc_slice
A grpc_slice s, if initialized, represents the byte range s.bytes[0..s.length-1].
Definition: slice.h:60
grpc::CoreCodegenInterface::grpc_raw_byte_buffer_create
virtual grpc_byte_buffer * grpc_raw_byte_buffer_create(grpc_slice *slice, size_t nslices)=0
grpc::SerializationTraits
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:58
grpc::ByteBuffer::GrpcByteBufferPeer
friend class internal::GrpcByteBufferPeer
Definition: byte_buffer.h:186
byte_buffer.h
grpc::Slice
A wrapper around grpc_slice.
Definition: slice.h:35
grpc::Status::OK
static const Status & OK
An OK pre-defined instance.
Definition: status.h:105
grpc::ByteBuffer::Dump
Status Dump(std::vector< Slice > *slices) const
Dump (read) the buffer contents into slices.
grpc::ByteBuffer::ByteBuffer
ByteBuffer(const ByteBuffer &buf)
Constuct a byte buffer by referencing elements of existing buffer buf.
Definition: byte_buffer.h:102
grpc_impl
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm_impl.h:33
grpc::ByteBuffer::ExternalConnectionAcceptorImpl
friend class internal::ExternalConnectionAcceptorImpl
Definition: byte_buffer.h:187
grpc::ByteBuffer::Valid
bool Valid() const
Is this ByteBuffer valid?
Definition: byte_buffer.h:163
grpc::CoreCodegenInterface::grpc_byte_buffer_length
virtual size_t grpc_byte_buffer_length(grpc_byte_buffer *bb) GRPC_MUST_USE_RESULT=0
grpc::g_core_codegen_interface
CoreCodegenInterface * g_core_codegen_interface
Definition: completion_queue_impl.h:93
slice.h
grpc_impl::internal::CallbackUnaryHandler
Definition: byte_buffer.h:36
grpc::internal::CallOpRecvMessage
Definition: byte_buffer.h:58
grpc::ByteBuffer::Release
void Release()
Forget underlying byte buffer without destroying Use this only for un-owned byte buffers.
Definition: byte_buffer.h:146
grpc::ServerInterface
Definition: server_interface.h:68
grpc::internal::ErrorMethodHandler
::grpc_impl::internal::ErrorMethodHandler< code > ErrorMethodHandler
Definition: method_handler.h:62
grpc_impl::internal::ServerStreamingHandler
A wrapper class of an application provided server streaming handler.
Definition: byte_buffer.h:42