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;
54 class ExternalConnectionAcceptorImpl;
56 class DeserializeFuncType;
57 class GrpcByteBufferPeer;
58 template <class ServiceType, class RequestType, class ResponseType>
59 class RpcMethodHandler;
60 template <class ServiceType, class RequestType, class ResponseType>
61 class ServerStreamingHandler;
63 } // namespace internal
64 /// A sequence of bytes.
65 class ByteBuffer final {
67 /// Constuct an empty buffer.
68 ByteBuffer() : buffer_(nullptr) {}
70 /// Construct buffer from \a slices, of which there are \a nslices.
71 ByteBuffer(const Slice* slices, size_t nslices) {
72 // The following assertions check that the representation of a grpc::Slice
73 // is identical to that of a grpc_slice: it has a grpc_slice field, and
75 static_assert(std::is_same<decltype(slices[0].slice_), grpc_slice>::value,
76 "Slice must have same representation as grpc_slice");
77 static_assert(sizeof(Slice) == sizeof(grpc_slice),
78 "Slice must have same representation as grpc_slice");
79 // The following assertions check that the representation of a ByteBuffer is
80 // identical to grpc_byte_buffer*: it has a grpc_byte_buffer* field,
82 static_assert(std::is_same<decltype(buffer_), grpc_byte_buffer*>::value,
83 "ByteBuffer must have same representation as "
85 static_assert(sizeof(ByteBuffer) == sizeof(grpc_byte_buffer*),
86 "ByteBuffer must have same representation as "
88 // The const_cast is legal if grpc_raw_byte_buffer_create() does no more
89 // than its advertised side effect of increasing the reference count of the
90 // slices it processes, and such an increase does not affect the semantics
91 // seen by the caller of this constructor.
92 buffer_ = g_core_codegen_interface->grpc_raw_byte_buffer_create(
93 reinterpret_cast<grpc_slice*>(const_cast<Slice*>(slices)), nslices);
96 /// Constuct a byte buffer by referencing elements of existing buffer
97 /// \a buf. Wrapper of core function grpc_byte_buffer_copy . This is not
98 /// a deep copy; it is just a referencing. As a result, its performance is
100 ByteBuffer(const ByteBuffer& buf) : buffer_(nullptr) { operator=(buf); }
104 g_core_codegen_interface->grpc_byte_buffer_destroy(buffer_);
108 /// Wrapper of core function grpc_byte_buffer_copy . This is not
109 /// a deep copy; it is just a referencing. As a result, its performance is
110 /// size-independent.
111 ByteBuffer& operator=(const ByteBuffer& buf) {
113 Clear(); // first remove existing data
117 buffer_ = g_core_codegen_interface->grpc_byte_buffer_copy(buf.buffer_);
122 /// Dump (read) the buffer contents into \a slices.
123 Status Dump(std::vector<Slice>* slices) const;
128 g_core_codegen_interface->grpc_byte_buffer_destroy(buffer_);
133 /// Make a duplicate copy of the internals of this byte
134 /// buffer so that we have our own owned version of it.
135 /// bbuf.Duplicate(); is equivalent to bbuf=bbuf; but is actually readable.
136 /// This is not a deep copy; it is a referencing and its performance
137 /// is size-independent.
139 buffer_ = g_core_codegen_interface->grpc_byte_buffer_copy(buffer_);
142 /// Forget underlying byte buffer without destroying
143 /// Use this only for un-owned byte buffers
144 void Release() { buffer_ = nullptr; }
146 /// Buffer size in bytes.
147 size_t Length() const {
148 return buffer_ == nullptr
150 : g_core_codegen_interface->grpc_byte_buffer_length(buffer_);
153 /// Swap the state of *this and *other.
154 void Swap(ByteBuffer* other) {
155 grpc_byte_buffer* tmp = other->buffer_;
156 other->buffer_ = buffer_;
160 /// Is this ByteBuffer valid?
161 bool Valid() const { return (buffer_ != nullptr); }
164 friend class SerializationTraits<ByteBuffer, void>;
165 friend class ServerInterface;
166 friend class internal::CallOpSendMessage;
168 friend class internal::CallOpRecvMessage;
169 friend class internal::CallOpGenericRecvMessage;
170 template <class ServiceType, class RequestType, class ResponseType>
171 friend class RpcMethodHandler;
172 template <class ServiceType, class RequestType, class ResponseType>
173 friend class ServerStreamingHandler;
174 template <class ServiceType, class RequestType, class ResponseType>
175 friend class internal::RpcMethodHandler;
176 template <class ServiceType, class RequestType, class ResponseType>
177 friend class internal::ServerStreamingHandler;
178 template <class RequestType, class ResponseType>
179 friend class internal::CallbackUnaryHandler;
180 template <class RequestType, class ResponseType>
181 friend class ::grpc::internal::CallbackServerStreamingHandler;
182 template <StatusCode code>
183 friend class internal::ErrorMethodHandler;
185 friend class internal::DeserializeFuncType;
186 friend class ProtoBufferReader;
187 friend class ProtoBufferWriter;
188 friend class internal::GrpcByteBufferPeer;
189 friend class internal::ExternalConnectionAcceptorImpl;
191 grpc_byte_buffer* buffer_;
194 void set_buffer(grpc_byte_buffer* buf) {
201 grpc_byte_buffer* c_buffer() { return buffer_; }
202 grpc_byte_buffer** c_buffer_ptr() { return &buffer_; }
204 class ByteBufferPointer {
206 ByteBufferPointer(const ByteBuffer* b)
207 : bbuf_(const_cast<ByteBuffer*>(b)) {}
208 operator ByteBuffer*() { return bbuf_; }
209 operator grpc_byte_buffer*() { return bbuf_->buffer_; }
210 operator grpc_byte_buffer**() { return &bbuf_->buffer_; }
215 ByteBufferPointer bbuf_ptr() const { return ByteBufferPointer(this); }
219 class SerializationTraits<ByteBuffer, void> {
221 static Status Deserialize(ByteBuffer* byte_buffer, ByteBuffer* dest) {
222 dest->set_buffer(byte_buffer->buffer_);
225 static Status Serialize(const ByteBuffer& source, ByteBuffer* buffer,
229 return g_core_codegen_interface->ok();
235 #endif // GRPCPP_IMPL_CODEGEN_BYTE_BUFFER_H