GRPC C++  1.11.0
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 
23 
29 
30 #include <vector>
31 
32 namespace grpc {
33 
34 class ServerInterface;
35 
36 namespace internal {
37 class CallOpSendMessage;
38 template <class R>
41 class MethodHandler;
42 template <class ServiceType, class RequestType, class ResponseType>
44 template <class ServiceType, class RequestType, class ResponseType>
46 template <class R>
48 } // namespace internal
50 class ByteBuffer final {
51  public:
53  ByteBuffer() : buffer_(nullptr) {}
54 
56  ByteBuffer(const Slice* slices, size_t nslices);
57 
60  ByteBuffer(const ByteBuffer& buf);
61 
63  if (buffer_) {
65  }
66  }
67 
68  ByteBuffer& operator=(const ByteBuffer&);
69 
71  Status Dump(std::vector<Slice>* slices) const;
72 
74  void Clear() {
75  if (buffer_) {
77  buffer_ = nullptr;
78  }
79  }
80 
84  void Duplicate() {
86  }
87 
90  void Release() { buffer_ = nullptr; }
91 
93  size_t Length() const;
94 
96  void Swap(ByteBuffer* other);
97 
99  bool Valid() const { return (buffer_ != nullptr); }
100 
101  private:
102  friend class SerializationTraits<ByteBuffer, void>;
103  friend class ServerInterface;
105  template <class R>
109  template <class ServiceType, class RequestType, class ResponseType>
111  template <class ServiceType, class RequestType, class ResponseType>
113  template <class R>
115 
116  grpc_byte_buffer* buffer_;
117 
118  // takes ownership
119  void set_buffer(grpc_byte_buffer* buf) {
120  if (buffer_) {
121  Clear();
122  }
123  buffer_ = buf;
124  }
125 
126  grpc_byte_buffer* c_buffer() { return buffer_; }
127  grpc_byte_buffer** c_buffer_ptr() { return &buffer_; }
128 
129  class ByteBufferPointer {
130  public:
131  ByteBufferPointer(const ByteBuffer* b)
132  : bbuf_(const_cast<ByteBuffer*>(b)) {}
133  operator ByteBuffer*() { return bbuf_; }
134  operator grpc_byte_buffer*() { return bbuf_->buffer_; }
135  operator grpc_byte_buffer**() { return &bbuf_->buffer_; }
136 
137  private:
138  ByteBuffer* bbuf_;
139  };
140  ByteBufferPointer bbuf_ptr() const { return ByteBufferPointer(this); }
141 };
142 
143 template <>
145  public:
146  static Status Deserialize(ByteBuffer* byte_buffer, ByteBuffer* dest) {
147  dest->set_buffer(byte_buffer->buffer_);
148  return Status::OK;
149  }
150  static Status Serialize(const ByteBuffer& source, ByteBuffer* buffer,
151  bool* own_buffer) {
152  *buffer = source;
153  *own_buffer = true;
154  return Status::OK;
155  }
156 };
157 
158 } // namespace grpc
159 
160 #endif // GRPCPP_IMPL_CODEGEN_BYTE_BUFFER_H
void Clear()
Remove all data.
Definition: byte_buffer.h:74
A wrapper class of an application provided server streaming handler.
Definition: byte_buffer.h:45
static Status Serialize(const ByteBuffer &source, ByteBuffer *buffer, bool *own_buffer)
Definition: byte_buffer.h:150
A wrapper around grpc_slice.
Definition: slice.h:35
void Duplicate()
Make a duplicate copy of the internals of this byte buffer so that we have our own owned version of i...
Definition: byte_buffer.h:84
Definition: grpc_types.h:40
bool Valid() const
Is this ByteBuffer valid?
Definition: byte_buffer.h:99
Definition: byte_buffer.h:47
virtual void grpc_byte_buffer_destroy(grpc_byte_buffer *bb)=0
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:58
void Release()
Forget underlying byte buffer without destroying Use this only for un-owned byte buffers.
Definition: byte_buffer.h:90
static Status Deserialize(ByteBuffer *byte_buffer, ByteBuffer *dest)
Definition: byte_buffer.h:146
Definition: call.h:268
An Alarm posts the user provided tag to its associated completion queue upon expiry or cancellation...
Definition: alarm.h:31
A wrapper class of an application provided rpc method handler.
Definition: byte_buffer.h:43
CoreCodegenInterface * g_core_codegen_interface
Definition: call.h:46
Definition: byte_buffer.h:39
Definition: server_interface.h:48
virtual grpc_byte_buffer * grpc_byte_buffer_copy(grpc_byte_buffer *bb)=0
Base class for running an RPC handler.
Definition: rpc_service_method.h:38
Did it work? If it didn&#39;t, why?
Definition: status.h:31
~ByteBuffer()
Definition: byte_buffer.h:62
static const Status & OK
An OK pre-defined instance.
Definition: status.h:105
ByteBuffer()
Constuct an empty buffer.
Definition: byte_buffer.h:53
A sequence of bytes.
Definition: byte_buffer.h:50