GRPC C++  1.34.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 template <class RequestType, class ResponseType>
41 template <class RequestType, class ResponseType>
43 template <class RequestType>
45  RequestType*);
46 template <class ServiceType, class RequestType, class ResponseType>
48 template <::grpc::StatusCode code>
50 class CallOpSendMessage;
51 template <class R>
54 class ExternalConnectionAcceptorImpl;
55 template <class R>
57 class GrpcByteBufferPeer;
58 
59 } // namespace internal
61 class ByteBuffer final {
62  public:
64  ByteBuffer() : buffer_(nullptr) {}
65 
67  ByteBuffer(const Slice* slices, size_t nslices) {
68  // The following assertions check that the representation of a grpc::Slice
69  // is identical to that of a grpc_slice: it has a grpc_slice field, and
70  // nothing else.
71  static_assert(std::is_same<decltype(slices[0].slice_), grpc_slice>::value,
72  "Slice must have same representation as grpc_slice");
73  static_assert(sizeof(Slice) == sizeof(grpc_slice),
74  "Slice must have same representation as grpc_slice");
75  // The following assertions check that the representation of a ByteBuffer is
76  // identical to grpc_byte_buffer*: it has a grpc_byte_buffer* field,
77  // and nothing else.
78  static_assert(std::is_same<decltype(buffer_), grpc_byte_buffer*>::value,
79  "ByteBuffer must have same representation as "
80  "grpc_byte_buffer*");
81  static_assert(sizeof(ByteBuffer) == sizeof(grpc_byte_buffer*),
82  "ByteBuffer must have same representation as "
83  "grpc_byte_buffer*");
84  // The const_cast is legal if grpc_raw_byte_buffer_create() does no more
85  // than its advertised side effect of increasing the reference count of the
86  // slices it processes, and such an increase does not affect the semantics
87  // seen by the caller of this constructor.
89  reinterpret_cast<grpc_slice*>(const_cast<Slice*>(slices)), nslices);
90  }
91 
96  ByteBuffer(const ByteBuffer& buf) : buffer_(nullptr) { operator=(buf); }
97 
99  if (buffer_) {
101  }
102  }
103 
108  if (this != &buf) {
109  Clear(); // first remove existing data
110  }
111  if (buf.buffer_) {
112  // then copy
113  buffer_ = g_core_codegen_interface->grpc_byte_buffer_copy(buf.buffer_);
114  }
115  return *this;
116  }
117 
119  Status Dump(std::vector<Slice>* slices) const;
120 
122  void Clear() {
123  if (buffer_) {
125  buffer_ = nullptr;
126  }
127  }
128 
134  void Duplicate() {
136  }
137 
140  void Release() { buffer_ = nullptr; }
141 
143  size_t Length() const {
144  return buffer_ == nullptr
145  ? 0
147  }
148 
150  void Swap(ByteBuffer* other) {
151  grpc_byte_buffer* tmp = other->buffer_;
152  other->buffer_ = buffer_;
153  buffer_ = tmp;
154  }
155 
157  bool Valid() const { return (buffer_ != nullptr); }
158 
159  private:
160  friend class SerializationTraits<ByteBuffer, void>;
161  friend class ServerInterface;
163  template <class R>
166  template <class RequestType>
168  ::grpc::Status*, RequestType*);
169  template <class ServiceType, class RequestType, class ResponseType>
171  template <class RequestType, class ResponseType>
173  template <class RequestType, class ResponseType>
175  template <StatusCode code>
177  template <class R>
179  friend class ProtoBufferReader;
180  friend class ProtoBufferWriter;
183 
184  grpc_byte_buffer* buffer_;
185 
186  // takes ownership
187  void set_buffer(grpc_byte_buffer* buf) {
188  if (buffer_) {
189  Clear();
190  }
191  buffer_ = buf;
192  }
193 
194  grpc_byte_buffer* c_buffer() { return buffer_; }
195  grpc_byte_buffer** c_buffer_ptr() { return &buffer_; }
196 
197  class ByteBufferPointer {
198  public:
199  ByteBufferPointer(const ByteBuffer* b)
200  : bbuf_(const_cast<ByteBuffer*>(b)) {}
201  operator ByteBuffer*() { return bbuf_; }
202  operator grpc_byte_buffer*() { return bbuf_->buffer_; }
203  operator grpc_byte_buffer**() { return &bbuf_->buffer_; }
204 
205  private:
206  ByteBuffer* bbuf_;
207  };
208  ByteBufferPointer bbuf_ptr() const { return ByteBufferPointer(this); }
209 };
210 
211 template <>
213  public:
214  static Status Deserialize(ByteBuffer* byte_buffer, ByteBuffer* dest) {
215  dest->set_buffer(byte_buffer->buffer_);
216  return Status::OK;
217  }
218  static Status Serialize(const ByteBuffer& source, ByteBuffer* buffer,
219  bool* own_buffer) {
220  *buffer = source;
221  *own_buffer = true;
222  return g_core_codegen_interface->ok();
223  }
224 };
225 
226 } // namespace grpc
227 
228 #endif // GRPCPP_IMPL_CODEGEN_BYTE_BUFFER_H
grpc::ProtoBufferWriter
This is a specialization of the protobuf class ZeroCopyOutputStream.
Definition: proto_buffer_writer.h:53
grpc::internal::CallOpGenericRecvMessage
Definition: call_op_set.h:526
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:134
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
status.h
grpc::internal::CallOpSendMessage
Definition: call_op_set.h:282
grpc::ByteBuffer::Length
size_t Length() const
Buffer size in bytes.
Definition: byte_buffer.h:143
grpc::internal::ErrorMethodHandler
General method handler class for errors that prevent real method use e.g., handle unknown method by r...
Definition: byte_buffer.h:49
serialization_traits.h
config.h
grpc::internal::UnaryDeserializeHelper
void * UnaryDeserializeHelper(grpc_call *, grpc_byte_buffer *, ::grpc::Status *, RequestType *)
A helper function with reduced templating to do deserializing.
Definition: method_handler.h:80
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:67
grpc::ByteBuffer::operator=
ByteBuffer & operator=(const ByteBuffer &buf)
Wrapper of core function grpc_byte_buffer_copy .
Definition: byte_buffer.h:107
core_codegen_interface.h
grpc::ByteBuffer::~ByteBuffer
~ByteBuffer()
Definition: byte_buffer.h:98
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:64
grpc::Status
Did it work? If it didn't, why?
Definition: status.h:31
grpc::ByteBuffer::Clear
void Clear()
Remove all data.
Definition: byte_buffer.h:122
grpc::SerializationTraits< ByteBuffer, void >::Deserialize
static Status Deserialize(ByteBuffer *byte_buffer, ByteBuffer *dest)
Definition: byte_buffer.h:214
grpc_call
struct grpc_call grpc_call
A Call represents an RPC.
Definition: grpc_types.h:70
grpc::internal::DeserializeFuncType
Definition: byte_buffer.h:56
grpc_byte_buffer
Definition: grpc_types.h:40
grpc::ByteBuffer
A sequence of bytes.
Definition: byte_buffer.h:61
grpc::internal::CallbackUnaryHandler
Definition: byte_buffer.h:40
grpc::SerializationTraits< ByteBuffer, void >::Serialize
static Status Serialize(const ByteBuffer &source, ByteBuffer *buffer, bool *own_buffer)
Definition: byte_buffer.h:218
grpc::ByteBuffer::Swap
void Swap(ByteBuffer *other)
Swap the state of *this and *other.
Definition: byte_buffer.h:150
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::internal::CallbackServerStreamingHandler
Definition: byte_buffer.h:42
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:181
byte_buffer.h
grpc::Slice
A wrapper around grpc_slice.
Definition: slice.h:35
grpc::internal::ServerStreamingHandler
A wrapper class of an application provided server streaming handler.
Definition: byte_buffer.h:47
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:96
grpc::ByteBuffer::ExternalConnectionAcceptorImpl
friend class internal::ExternalConnectionAcceptorImpl
Definition: byte_buffer.h:182
grpc::ByteBuffer::Valid
bool Valid() const
Is this ByteBuffer valid?
Definition: byte_buffer.h:157
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.h:96
slice.h
grpc::internal::CallOpRecvMessage
Definition: byte_buffer.h:52
grpc::ByteBuffer::Release
void Release()
Forget underlying byte buffer without destroying Use this only for un-owned byte buffers.
Definition: byte_buffer.h:140
grpc::ServerInterface
Definition: server_interface.h:65