include/grpcpp/impl/codegen/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 
22 // IWYU pragma: private, include <grpcpp/support/byte_buffer.h>
23 
24 #include <vector>
25 
32 
33 namespace grpc {
34 
35 class ServerInterface;
36 class ByteBuffer;
37 class ServerInterface;
38 
39 namespace internal {
40 template <class RequestType, class ResponseType>
42 template <class RequestType, class ResponseType>
44 template <class RequestType>
46 template <class ServiceType, class RequestType, class ResponseType>
48 template <grpc::StatusCode code>
50 class CallOpSendMessage;
51 template <class R>
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
114  }
115  return *this;
116  }
117 
118  // If this ByteBuffer's representation is a single flat slice, returns a
119  // slice referencing that array.
121 
124 
126  Status Dump(std::vector<Slice>* slices) const;
127 
129  void Clear() {
130  if (buffer_) {
132  buffer_ = nullptr;
133  }
134  }
135 
141  void Duplicate() {
143  }
144 
147  void Release() { buffer_ = nullptr; }
148 
150  size_t Length() const {
151  return buffer_ == nullptr
152  ? 0
154  }
155 
157  void Swap(ByteBuffer* other) {
158  grpc_byte_buffer* tmp = other->buffer_;
159  other->buffer_ = buffer_;
160  buffer_ = tmp;
161  }
162 
164  bool Valid() const { return (buffer_ != nullptr); }
165 
166  private:
167  friend class SerializationTraits<ByteBuffer, void>;
168  friend class ServerInterface;
170  template <class R>
173  template <class RequestType>
176  template <class ServiceType, class RequestType, class ResponseType>
178  template <class RequestType, class ResponseType>
180  template <class RequestType, class ResponseType>
182  template <StatusCode code>
184  template <class R>
186  friend class ProtoBufferReader;
187  friend class ProtoBufferWriter;
190 
192 
193  // takes ownership
195  if (buffer_) {
196  Clear();
197  }
198  buffer_ = buf;
199  }
200 
203 
205  public:
206  /* NOLINTNEXTLINE(google-explicit-constructor) */
208  : bbuf_(const_cast<ByteBuffer*>(b)) {}
209  /* NOLINTNEXTLINE(google-explicit-constructor) */
210  operator ByteBuffer*() { return bbuf_; }
211  /* NOLINTNEXTLINE(google-explicit-constructor) */
212  operator grpc_byte_buffer*() { return bbuf_->buffer_; }
213  /* NOLINTNEXTLINE(google-explicit-constructor) */
214  operator grpc_byte_buffer**() { return &bbuf_->buffer_; }
215 
216  private:
218  };
219  ByteBufferPointer bbuf_ptr() const { return ByteBufferPointer(this); }
220 };
221 
222 template <>
224  public:
225  static Status Deserialize(ByteBuffer* byte_buffer, ByteBuffer* dest) {
226  dest->set_buffer(byte_buffer->buffer_);
227  return Status::OK;
228  }
229  static Status Serialize(const ByteBuffer& source, ByteBuffer* buffer,
230  bool* own_buffer) {
231  *buffer = source;
232  *own_buffer = true;
233  return g_core_codegen_interface->ok();
234  }
235 };
236 
237 } // namespace grpc
238 
239 #endif // GRPCPP_IMPL_CODEGEN_BYTE_BUFFER_H
grpc::internal::CallOpSendMessage
Definition: call_op_set.h:289
grpc::ByteBuffer::buffer_
grpc_byte_buffer * buffer_
Definition: include/grpcpp/impl/codegen/byte_buffer.h:191
grpc._simple_stubs.RequestType
RequestType
Definition: _simple_stubs.py:27
grpc::ByteBuffer::c_buffer
grpc_byte_buffer * c_buffer()
Definition: include/grpcpp/impl/codegen/byte_buffer.h:201
grpc
Definition: grpcpp/alarm.h:33
grpc::ByteBuffer::ByteBufferPointer::bbuf_
ByteBuffer * bbuf_
Definition: include/grpcpp/impl/codegen/byte_buffer.h:217
grpc::ByteBuffer::Swap
void Swap(ByteBuffer *other)
Swap the state of *this and *other.
Definition: include/grpcpp/impl/codegen/byte_buffer.h:157
grpc::internal::GrpcByteBufferPeer
Definition: proto_utils_test.cc:46
serialization_traits.h
grpc::internal::CallOpGenericRecvMessage
Definition: call_op_set.h:533
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
grpc::ByteBuffer::TrySingleSlice
Status TrySingleSlice(Slice *slice) const
Definition: byte_buffer_cc.cc:35
slice.h
grpc::ByteBuffer::c_buffer_ptr
grpc_byte_buffer ** c_buffer_ptr()
Definition: include/grpcpp/impl/codegen/byte_buffer.h:202
core_codegen_interface.h
grpc::internal::ErrorMethodHandler
Definition: include/grpcpp/impl/codegen/byte_buffer.h:49
grpc::ByteBuffer::~ByteBuffer
~ByteBuffer()
Definition: include/grpcpp/impl/codegen/byte_buffer.h:98
grpc::SerializationTraits< ByteBuffer, void >::Deserialize
static Status Deserialize(ByteBuffer *byte_buffer, ByteBuffer *dest)
Definition: include/grpcpp/impl/codegen/byte_buffer.h:225
config.h
grpc::ByteBuffer::Release
void Release()
Definition: include/grpcpp/impl/codegen/byte_buffer.h:147
grpc::internal::ExternalConnectionAcceptorImpl
Definition: external_connection_acceptor_impl.h:36
grpc::SerializationTraits
Definition: grpcpp/impl/codegen/serialization_traits.h:60
grpc::SerializationTraits< ByteBuffer, void >::Serialize
static Status Serialize(const ByteBuffer &source, ByteBuffer *buffer, bool *own_buffer)
Definition: include/grpcpp/impl/codegen/byte_buffer.h:229
grpc::CoreCodegenInterface::ok
virtual const Status & ok()=0
grpc::ByteBuffer::Valid
bool Valid() const
Is this ByteBuffer valid?
Definition: include/grpcpp/impl/codegen/byte_buffer.h:164
grpc::g_core_codegen_interface
CoreCodegenInterface * g_core_codegen_interface
Definition: include/grpcpp/impl/codegen/completion_queue.h:98
grpc::ProtoBufferReader
Definition: impl/codegen/proto_buffer_reader.h:48
grpc::internal::DeserializeFuncType
Definition: include/grpcpp/impl/codegen/byte_buffer.h:56
grpc::CoreCodegenInterface::grpc_raw_byte_buffer_create
virtual grpc_byte_buffer * grpc_raw_byte_buffer_create(grpc_slice *slice, size_t nslices)=0
slice
grpc_slice slice
Definition: src/core/lib/surface/server.cc:467
grpc::internal::UnaryDeserializeHelper
void * UnaryDeserializeHelper(grpc_byte_buffer *, grpc::Status *, RequestType *)
A helper function with reduced templating to do deserializing.
Definition: impl/codegen/method_handler.h:82
grpc::ByteBuffer::ByteBuffer
ByteBuffer()
Constuct an empty buffer.
Definition: include/grpcpp/impl/codegen/byte_buffer.h:64
grpc_byte_buffer
Definition: grpc_types.h:43
grpc::ByteBuffer::Duplicate
void Duplicate()
Definition: include/grpcpp/impl/codegen/byte_buffer.h:141
grpc::CoreCodegenInterface::grpc_byte_buffer_length
virtual size_t grpc_byte_buffer_length(grpc_byte_buffer *bb) GRPC_MUST_USE_RESULT=0
grpc_slice
Definition: include/grpc/impl/codegen/slice.h:65
grpc::ByteBuffer
A sequence of bytes.
Definition: include/grpcpp/impl/codegen/byte_buffer.h:61
grpc::ByteBuffer::ByteBufferPointer::ByteBufferPointer
ByteBufferPointer(const ByteBuffer *b)
Definition: include/grpcpp/impl/codegen/byte_buffer.h:207
grpc::ServerInterface
Definition: grpcpp/impl/codegen/server_interface.h:61
grpc::ByteBuffer::ByteBuffer
ByteBuffer(const Slice *slices, size_t nslices)
Construct buffer from slices, of which there are nslices.
Definition: include/grpcpp/impl/codegen/byte_buffer.h:67
grpc::Status::OK
static const Status & OK
An OK pre-defined instance.
Definition: include/grpcpp/impl/codegen/status.h:113
grpc::ByteBuffer::Dump
Status Dump(std::vector< Slice > *slices) const
Dump (read) the buffer contents into slices.
Definition: byte_buffer_cc.cc:66
grpc::ProtoBufferWriter
Definition: impl/codegen/proto_buffer_writer.h:55
buffer
char buffer[1024]
Definition: libuv/docs/code/idle-compute/main.c:8
grpc::ByteBuffer::Clear
void Clear()
Remove all data.
Definition: include/grpcpp/impl/codegen/byte_buffer.h:129
grpc::internal::CallOpRecvMessage
Definition: include/grpcpp/impl/codegen/byte_buffer.h:52
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
grpc::ByteBuffer::ByteBufferPointer
Definition: include/grpcpp/impl/codegen/byte_buffer.h:204
grpc::ByteBuffer::DumpToSingleSlice
Status DumpToSingleSlice(Slice *slice) const
Dump (read) the buffer contents into slics.
Definition: byte_buffer_cc.cc:51
tests.qps.qps_worker.dest
dest
Definition: qps_worker.py:45
grpc::internal::CallbackUnaryHandler
Definition: include/grpcpp/impl/codegen/byte_buffer.h:41
grpc::CoreCodegenInterface::grpc_byte_buffer_destroy
virtual void grpc_byte_buffer_destroy(grpc_byte_buffer *bb)=0
value
const char * value
Definition: hpack_parser_table.cc:165
status.h
byte_buffer.h
grpc::ByteBuffer::Length
size_t Length() const
Buffer size in bytes.
Definition: include/grpcpp/impl/codegen/byte_buffer.h:150
grpc::internal::CallbackServerStreamingHandler
Definition: include/grpcpp/impl/codegen/byte_buffer.h:43
grpc::CoreCodegenInterface::grpc_byte_buffer_copy
virtual grpc_byte_buffer * grpc_byte_buffer_copy(grpc_byte_buffer *bb)=0
slices
SliceBuffer * slices
Definition: retry_filter.cc:631
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
grpc::ByteBuffer::ByteBuffer
ByteBuffer(const ByteBuffer &buf)
Definition: include/grpcpp/impl/codegen/byte_buffer.h:96
grpc::ByteBuffer::set_buffer
void set_buffer(grpc_byte_buffer *buf)
Definition: include/grpcpp/impl/codegen/byte_buffer.h:194
internal
Definition: benchmark/test/output_test_helper.cc:20
autogen_x86imm.tmp
tmp
Definition: autogen_x86imm.py:12
grpc::Slice
Definition: include/grpcpp/impl/codegen/slice.h:36
grpc::internal::ServerStreamingHandler
A wrapper class of an application provided server streaming handler.
Definition: include/grpcpp/impl/codegen/byte_buffer.h:47
grpc::ByteBuffer::bbuf_ptr
ByteBufferPointer bbuf_ptr() const
Definition: include/grpcpp/impl/codegen/byte_buffer.h:219
grpc::ByteBuffer::operator=
ByteBuffer & operator=(const ByteBuffer &buf)
Definition: include/grpcpp/impl/codegen/byte_buffer.h:107


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:41