3 * Copyright 2017 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #ifndef GRPCPP_IMPL_CODEGEN_BYTE_BUFFER_H
20 #define GRPCPP_IMPL_CODEGEN_BYTE_BUFFER_H
22 #include <grpc/impl/codegen/byte_buffer.h>
24 #include <grpcpp/impl/codegen/config.h>
25 #include <grpcpp/impl/codegen/core_codegen_interface.h>
26 #include <grpcpp/impl/codegen/serialization_traits.h>
27 #include <grpcpp/impl/codegen/slice.h>
28 #include <grpcpp/impl/codegen/status.h>
34 class ServerInterface;
36 class ServerInterface;
39 class CallOpSendMessage;
41 class CallOpRecvMessage;
42 class CallOpGenericRecvMessage;
44 template <class ServiceType, class RequestType, class ResponseType>
45 class RpcMethodHandler;
46 template <class ServiceType, class RequestType, class ResponseType>
47 class ServerStreamingHandler;
48 template <class RequestType, class ResponseType>
49 class CallbackUnaryHandler;
50 template <class RequestType, class ResponseType>
51 class CallbackServerStreamingHandler;
52 template <StatusCode code>
53 class ErrorMethodHandler;
55 class DeserializeFuncType;
56 class GrpcByteBufferPeer;
57 template <class ServiceType, class RequestType, class ResponseType>
58 class RpcMethodHandler;
59 template <class ServiceType, class RequestType, class ResponseType>
60 class ServerStreamingHandler;
62 } // namespace internal
63 /// A sequence of bytes.
64 class ByteBuffer final {
66 /// Constuct an empty buffer.
67 ByteBuffer() : buffer_(nullptr) {}
69 /// Construct buffer from \a slices, of which there are \a nslices.
70 ByteBuffer(const Slice* slices, size_t nslices) {
71 // The following assertions check that the representation of a grpc::Slice
72 // is identical to that of a grpc_slice: it has a grpc_slice field, and
74 static_assert(std::is_same<decltype(slices[0].slice_), grpc_slice>::value,
75 "Slice must have same representation as grpc_slice");
76 static_assert(sizeof(Slice) == sizeof(grpc_slice),
77 "Slice must have same representation as grpc_slice");
78 // The following assertions check that the representation of a ByteBuffer is
79 // identical to grpc_byte_buffer*: it has a grpc_byte_buffer* field,
81 static_assert(std::is_same<decltype(buffer_), grpc_byte_buffer*>::value,
82 "ByteBuffer must have same representation as "
84 static_assert(sizeof(ByteBuffer) == sizeof(grpc_byte_buffer*),
85 "ByteBuffer must have same representation as "
87 // The const_cast is legal if grpc_raw_byte_buffer_create() does no more
88 // than its advertised side effect of increasing the reference count of the
89 // slices it processes, and such an increase does not affect the semantics
90 // seen by the caller of this constructor.
91 buffer_ = g_core_codegen_interface->grpc_raw_byte_buffer_create(
92 reinterpret_cast<grpc_slice*>(const_cast<Slice*>(slices)), nslices);
95 /// Constuct a byte buffer by referencing elements of existing buffer
96 /// \a buf. Wrapper of core function grpc_byte_buffer_copy . This is not
97 /// a deep copy; it is just a referencing. As a result, its performance is
99 ByteBuffer(const ByteBuffer& buf) : buffer_(nullptr) { operator=(buf); }
103 g_core_codegen_interface->grpc_byte_buffer_destroy(buffer_);
107 /// Wrapper of core function grpc_byte_buffer_copy . This is not
108 /// a deep copy; it is just a referencing. As a result, its performance is
109 /// size-independent.
110 ByteBuffer& operator=(const ByteBuffer& buf) {
112 Clear(); // first remove existing data
116 buffer_ = g_core_codegen_interface->grpc_byte_buffer_copy(buf.buffer_);
121 /// Dump (read) the buffer contents into \a slices.
122 Status Dump(std::vector<Slice>* slices) const;
127 g_core_codegen_interface->grpc_byte_buffer_destroy(buffer_);
132 /// Make a duplicate copy of the internals of this byte
133 /// buffer so that we have our own owned version of it.
134 /// bbuf.Duplicate(); is equivalent to bbuf=bbuf; but is actually readable.
135 /// This is not a deep copy; it is a referencing and its performance
136 /// is size-independent.
138 buffer_ = g_core_codegen_interface->grpc_byte_buffer_copy(buffer_);
141 /// Forget underlying byte buffer without destroying
142 /// Use this only for un-owned byte buffers
143 void Release() { buffer_ = nullptr; }
145 /// Buffer size in bytes.
146 size_t Length() const {
147 return buffer_ == nullptr
149 : g_core_codegen_interface->grpc_byte_buffer_length(buffer_);
152 /// Swap the state of *this and *other.
153 void Swap(ByteBuffer* other) {
154 grpc_byte_buffer* tmp = other->buffer_;
155 other->buffer_ = buffer_;
159 /// Is this ByteBuffer valid?
160 bool Valid() const { return (buffer_ != nullptr); }
163 friend class SerializationTraits<ByteBuffer, void>;
164 friend class ServerInterface;
165 friend class internal::CallOpSendMessage;
167 friend class internal::CallOpRecvMessage;
168 friend class internal::CallOpGenericRecvMessage;
169 template <class ServiceType, class RequestType, class ResponseType>
170 friend class RpcMethodHandler;
171 template <class ServiceType, class RequestType, class ResponseType>
172 friend class ServerStreamingHandler;
173 template <class ServiceType, class RequestType, class ResponseType>
174 friend class internal::RpcMethodHandler;
175 template <class ServiceType, class RequestType, class ResponseType>
176 friend class internal::ServerStreamingHandler;
177 template <class RequestType, class ResponseType>
178 friend class internal::CallbackUnaryHandler;
179 template <class RequestType, class ResponseType>
180 friend class ::grpc::internal::CallbackServerStreamingHandler;
181 template <StatusCode code>
182 friend class internal::ErrorMethodHandler;
184 friend class internal::DeserializeFuncType;
185 friend class ProtoBufferReader;
186 friend class ProtoBufferWriter;
187 friend class internal::GrpcByteBufferPeer;
189 grpc_byte_buffer* buffer_;
192 void set_buffer(grpc_byte_buffer* buf) {
199 grpc_byte_buffer* c_buffer() { return buffer_; }
200 grpc_byte_buffer** c_buffer_ptr() { return &buffer_; }
202 class ByteBufferPointer {
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_; }
213 ByteBufferPointer bbuf_ptr() const { return ByteBufferPointer(this); }
217 class SerializationTraits<ByteBuffer, void> {
219 static Status Deserialize(ByteBuffer* byte_buffer, ByteBuffer* dest) {
220 dest->set_buffer(byte_buffer->buffer_);
223 static Status Serialize(const ByteBuffer& source, ByteBuffer* buffer,
227 return g_core_codegen_interface->ok();
233 #endif // GRPCPP_IMPL_CODEGEN_BYTE_BUFFER_H