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