• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2015 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_SUPPORT_BYTE_BUFFER_H
20 #define GRPCPP_SUPPORT_BYTE_BUFFER_H
21 
22 #include <grpc/byte_buffer.h>
23 #include <grpc/grpc.h>
24 #include <grpcpp/impl/serialization_traits.h>
25 #include <grpcpp/support/config.h>
26 #include <grpcpp/support/slice.h>
27 #include <grpcpp/support/status.h>
28 
29 #include <vector>
30 
31 namespace grpc {
32 
33 class ServerInterface;
34 class ByteBuffer;
35 class ServerInterface;
36 
37 namespace internal {
38 template <class RequestType, class ResponseType>
39 class CallbackUnaryHandler;
40 template <class RequestType, class ResponseType>
41 class CallbackServerStreamingHandler;
42 template <class RequestType>
43 void* UnaryDeserializeHelper(grpc_byte_buffer*, grpc::Status*, RequestType*);
44 template <class ServiceType, class RequestType, class ResponseType>
45 class ServerStreamingHandler;
46 template <grpc::StatusCode code>
47 class ErrorMethodHandler;
48 class CallOpSendMessage;
49 template <class R>
50 class CallOpRecvMessage;
51 class CallOpGenericRecvMessage;
52 class ExternalConnectionAcceptorImpl;
53 template <class R>
54 class DeserializeFuncType;
55 class GrpcByteBufferPeer;
56 
57 }  // namespace internal
58 /// A sequence of bytes.
59 class ByteBuffer final {
60  public:
61   /// Construct an empty buffer.
ByteBuffer()62   ByteBuffer() : buffer_(nullptr) {}
63 
64   /// Construct buffer from \a slices, of which there are \a nslices.
ByteBuffer(const Slice * slices,size_t nslices)65   ByteBuffer(const Slice* slices, size_t nslices) {
66     // The following assertions check that the representation of a grpc::Slice
67     // is identical to that of a grpc_slice:  it has a grpc_slice field, and
68     // nothing else.
69     static_assert(std::is_same<decltype(slices[0].slice_), grpc_slice>::value,
70                   "Slice must have same representation as grpc_slice");
71     static_assert(sizeof(Slice) == sizeof(grpc_slice),
72                   "Slice must have same representation as grpc_slice");
73     // The following assertions check that the representation of a ByteBuffer is
74     // identical to grpc_byte_buffer*:  it has a grpc_byte_buffer* field,
75     // and nothing else.
76     static_assert(std::is_same<decltype(buffer_), grpc_byte_buffer*>::value,
77                   "ByteBuffer must have same representation as "
78                   "grpc_byte_buffer*");
79     static_assert(sizeof(ByteBuffer) == sizeof(grpc_byte_buffer*),
80                   "ByteBuffer must have same representation as "
81                   "grpc_byte_buffer*");
82     // The const_cast is legal if grpc_raw_byte_buffer_create() does no more
83     // than its advertised side effect of increasing the reference count of the
84     // slices it processes, and such an increase does not affect the semantics
85     // seen by the caller of this constructor.
86     buffer_ = grpc_raw_byte_buffer_create(
87         reinterpret_cast<grpc_slice*>(const_cast<Slice*>(slices)), nslices);
88   }
89 
90   /// Construct a byte buffer by referencing elements of existing buffer
91   /// \a buf. Wrapper of core function grpc_byte_buffer_copy . This is not
92   /// a deep copy; it is just a referencing. As a result, its performance is
93   /// size-independent.
ByteBuffer(const ByteBuffer & buf)94   ByteBuffer(const ByteBuffer& buf) : buffer_(nullptr) { operator=(buf); }
95 
~ByteBuffer()96   ~ByteBuffer() {
97     if (buffer_) {
98       grpc_byte_buffer_destroy(buffer_);
99     }
100   }
101 
102   /// Wrapper of core function grpc_byte_buffer_copy . This is not
103   /// a deep copy; it is just a referencing. As a result, its performance is
104   /// size-independent.
105   ByteBuffer& operator=(const ByteBuffer& buf) {
106     if (this != &buf) {
107       Clear();  // first remove existing data
108     }
109     if (buf.buffer_) {
110       // then copy
111       buffer_ = grpc_byte_buffer_copy(buf.buffer_);
112     }
113     return *this;
114   }
115 
116   // If this ByteBuffer's representation is a single flat slice, returns a
117   // slice referencing that array.
118   Status TrySingleSlice(Slice* slice) const;
119 
120   /// Dump (read) the buffer contents into \a slics.
121   Status DumpToSingleSlice(Slice* slice) const;
122 
123   /// Dump (read) the buffer contents into \a slices.
124   Status Dump(std::vector<Slice>* slices) const;
125 
126   /// Remove all data.
Clear()127   void Clear() {
128     if (buffer_) {
129       grpc_byte_buffer_destroy(buffer_);
130       buffer_ = nullptr;
131     }
132   }
133 
134   /// Make a duplicate copy of the internals of this byte
135   /// buffer so that we have our own owned version of it.
136   /// bbuf.Duplicate(); is equivalent to bbuf=bbuf; but is actually readable.
137   /// This is not a deep copy; it is a referencing and its performance
138   /// is size-independent.
Duplicate()139   void Duplicate() { buffer_ = grpc_byte_buffer_copy(buffer_); }
140 
141   /// Forget underlying byte buffer without destroying
142   /// Use this only for un-owned byte buffers
Release()143   void Release() { buffer_ = nullptr; }
144 
145   /// Buffer size in bytes.
Length()146   size_t Length() const {
147     return buffer_ == nullptr ? 0 : grpc_byte_buffer_length(buffer_);
148   }
149 
150   /// Swap the state of *this and *other.
Swap(ByteBuffer * other)151   void Swap(ByteBuffer* other) {
152     grpc_byte_buffer* tmp = other->buffer_;
153     other->buffer_ = buffer_;
154     buffer_ = tmp;
155   }
156 
157   /// Is this ByteBuffer valid?
Valid()158   bool Valid() const { return (buffer_ != nullptr); }
159 
160  private:
161   friend class SerializationTraits<ByteBuffer, void>;
162   friend class ServerInterface;
163   friend class internal::CallOpSendMessage;
164   template <class R>
165   friend class internal::CallOpRecvMessage;
166   friend class internal::CallOpGenericRecvMessage;
167   template <class RequestType>
168   friend void* internal::UnaryDeserializeHelper(grpc_byte_buffer*,
169                                                 grpc::Status*, RequestType*);
170   template <class ServiceType, class RequestType, class ResponseType>
171   friend class internal::ServerStreamingHandler;
172   template <class RequestType, class ResponseType>
173   friend class internal::CallbackUnaryHandler;
174   template <class RequestType, class ResponseType>
175   friend class internal::CallbackServerStreamingHandler;
176   template <StatusCode code>
177   friend class internal::ErrorMethodHandler;
178   template <class R>
179   friend class internal::DeserializeFuncType;
180   friend class ProtoBufferReader;
181   friend class ProtoBufferWriter;
182   friend class internal::GrpcByteBufferPeer;
183   friend class internal::ExternalConnectionAcceptorImpl;
184 
185   grpc_byte_buffer* buffer_;
186 
187   // takes ownership
set_buffer(grpc_byte_buffer * buf)188   void set_buffer(grpc_byte_buffer* buf) {
189     if (buffer_) {
190       Clear();
191     }
192     buffer_ = buf;
193   }
194 
c_buffer()195   grpc_byte_buffer* c_buffer() { return buffer_; }
c_buffer_ptr()196   grpc_byte_buffer** c_buffer_ptr() { return &buffer_; }
197 
198   class ByteBufferPointer {
199    public:
200     // NOLINTNEXTLINE(google-explicit-constructor)
ByteBufferPointer(const ByteBuffer * b)201     ByteBufferPointer(const ByteBuffer* b)
202         : bbuf_(const_cast<ByteBuffer*>(b)) {}
203     // NOLINTNEXTLINE(google-explicit-constructor)
204     operator ByteBuffer*() { return bbuf_; }
205     // NOLINTNEXTLINE(google-explicit-constructor)
206     operator grpc_byte_buffer*() { return bbuf_->buffer_; }
207     // NOLINTNEXTLINE(google-explicit-constructor)
208     operator grpc_byte_buffer**() { return &bbuf_->buffer_; }
209 
210    private:
211     ByteBuffer* bbuf_;
212   };
bbuf_ptr()213   ByteBufferPointer bbuf_ptr() const { return ByteBufferPointer(this); }
214 };
215 
216 template <>
217 class SerializationTraits<ByteBuffer, void> {
218  public:
Deserialize(ByteBuffer * byte_buffer,ByteBuffer * dest)219   static Status Deserialize(ByteBuffer* byte_buffer, ByteBuffer* dest) {
220     dest->set_buffer(byte_buffer->buffer_);
221     return Status::OK;
222   }
Serialize(const ByteBuffer & source,ByteBuffer * buffer,bool * own_buffer)223   static Status Serialize(const ByteBuffer& source, ByteBuffer* buffer,
224                           bool* own_buffer) {
225     *buffer = source;
226     *own_buffer = true;
227     return grpc::Status::OK;
228   }
229 };
230 
231 }  // namespace grpc
232 
233 #endif  // GRPCPP_SUPPORT_BYTE_BUFFER_H
234