GRPC C++  1.3.0
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
proto_utils.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015, Google Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following disclaimer
14  * in the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Google Inc. nor the names of its
17  * contributors may be used to endorse or promote products derived from
18  * this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  */
33 
34 #ifndef GRPCXX_IMPL_CODEGEN_PROTO_UTILS_H
35 #define GRPCXX_IMPL_CODEGEN_PROTO_UTILS_H
36 
37 #include <type_traits>
38 
46 
47 namespace grpc {
48 
49 extern CoreCodegenInterface* g_core_codegen_interface;
50 
51 namespace internal {
52 
53 class GrpcBufferWriterPeer;
54 
56 
57 class GrpcBufferWriter final
59  public:
60  explicit GrpcBufferWriter(grpc_byte_buffer** bp, int block_size)
61  : block_size_(block_size), byte_count_(0), have_backup_(false) {
63  slice_buffer_ = &(*bp)->data.raw.slice_buffer;
64  }
65 
66  ~GrpcBufferWriter() override {
67  if (have_backup_) {
69  }
70  }
71 
72  bool Next(void** data, int* size) override {
73  if (have_backup_) {
74  slice_ = backup_slice_;
75  have_backup_ = false;
76  } else {
77  slice_ = g_core_codegen_interface->grpc_slice_malloc(block_size_);
78  }
79  *data = GRPC_SLICE_START_PTR(slice_);
80  // On win x64, int is only 32bit
81  GPR_CODEGEN_ASSERT(GRPC_SLICE_LENGTH(slice_) <= INT_MAX);
82  byte_count_ += * size = (int)GRPC_SLICE_LENGTH(slice_);
83  g_core_codegen_interface->grpc_slice_buffer_add(slice_buffer_, slice_);
84  return true;
85  }
86 
87  void BackUp(int count) override {
89  if (count == block_size_) {
90  backup_slice_ = slice_;
91  } else {
93  &slice_, GRPC_SLICE_LENGTH(slice_) - count);
94  g_core_codegen_interface->grpc_slice_buffer_add(slice_buffer_, slice_);
95  }
96  // It's dangerous to keep an inlined grpc_slice as the backup slice, since
97  // on a following Next() call, a reference will be returned to this slice
98  // via GRPC_SLICE_START_PTR, which will not be an adddress held by
99  // slice_buffer_.
100  have_backup_ = backup_slice_.refcount != NULL;
101  byte_count_ -= count;
102  }
103 
104  grpc::protobuf::int64 ByteCount() const override { return byte_count_; }
105 
106  private:
107  friend class GrpcBufferWriterPeer;
108  const int block_size_;
109  int64_t byte_count_;
110  grpc_slice_buffer* slice_buffer_;
111  bool have_backup_;
112  grpc_slice backup_slice_;
113  grpc_slice slice_;
114 };
115 
116 class GrpcBufferReader final
118  public:
120  : byte_count_(0), backup_count_(0), status_() {
122  buffer)) {
123  status_ = Status(StatusCode::INTERNAL,
124  "Couldn't initialize byte buffer reader");
125  }
126  }
127  ~GrpcBufferReader() override {
129  }
130 
131  bool Next(const void** data, int* size) override {
132  if (!status_.ok()) {
133  return false;
134  }
135  if (backup_count_ > 0) {
136  *data = GRPC_SLICE_START_PTR(slice_) + GRPC_SLICE_LENGTH(slice_) -
137  backup_count_;
138  GPR_CODEGEN_ASSERT(backup_count_ <= INT_MAX);
139  *size = (int)backup_count_;
140  backup_count_ = 0;
141  return true;
142  }
144  &slice_)) {
145  return false;
146  }
148  *data = GRPC_SLICE_START_PTR(slice_);
149  // On win x64, int is only 32bit
150  GPR_CODEGEN_ASSERT(GRPC_SLICE_LENGTH(slice_) <= INT_MAX);
151  byte_count_ += * size = (int)GRPC_SLICE_LENGTH(slice_);
152  return true;
153  }
154 
155  Status status() const { return status_; }
156 
157  void BackUp(int count) override { backup_count_ = count; }
158 
159  bool Skip(int count) override {
160  const void* data;
161  int size;
162  while (Next(&data, &size)) {
163  if (size >= count) {
164  BackUp(size - count);
165  return true;
166  }
167  // size < count;
168  count -= size;
169  }
170  // error or we have too large count;
171  return false;
172  }
173 
174  grpc::protobuf::int64 ByteCount() const override {
175  return byte_count_ - backup_count_;
176  }
177 
178  private:
179  int64_t byte_count_;
180  int64_t backup_count_;
181  grpc_byte_buffer_reader reader_;
182  grpc_slice slice_;
183  Status status_;
184 };
185 } // namespace internal
186 
187 template <class T>
188 class SerializationTraits<T, typename std::enable_if<std::is_base_of<
189  grpc::protobuf::Message, T>::value>::type> {
190  public:
192  grpc_byte_buffer** bp, bool* own_buffer) {
193  *own_buffer = true;
194  int byte_size = msg.ByteSize();
198  GRPC_SLICE_END_PTR(slice) ==
199  msg.SerializeWithCachedSizesToArray(GRPC_SLICE_START_PTR(slice)));
202  return g_core_codegen_interface->ok();
203  } else {
206  return msg.SerializeToZeroCopyStream(&writer)
208  : Status(StatusCode::INTERNAL, "Failed to serialize message");
209  }
210  }
211 
214  if (buffer == nullptr) {
215  return Status(StatusCode::INTERNAL, "No payload");
216  }
217  Status result = g_core_codegen_interface->ok();
218  {
219  internal::GrpcBufferReader reader(buffer);
220  if (!reader.status().ok()) {
221  return reader.status();
222  }
224  decoder.SetTotalBytesLimit(INT_MAX, INT_MAX);
225  if (!msg->ParseFromCodedStream(&decoder)) {
226  result = Status(StatusCode::INTERNAL, msg->InitializationErrorString());
227  }
228  if (!decoder.ConsumedEntireMessage()) {
229  result = Status(StatusCode::INTERNAL, "Did not read entire message");
230  }
231  }
233  return result;
234  }
235 };
236 
237 } // namespace grpc
238 
239 #endif // GRPCXX_IMPL_CODEGEN_PROTO_UTILS_H
virtual void grpc_slice_buffer_pop(grpc_slice_buffer *sb)=0
virtual grpc_slice grpc_slice_malloc(size_t length)=0
Definition: proto_utils.h:116
~GrpcBufferWriter() override
Definition: proto_utils.h:66
virtual void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader *reader)=0
grpc::protobuf::int64 ByteCount() const override
Definition: proto_utils.h:174
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:122
const int kGrpcBufferWriterMaxBufferLength
Definition: proto_utils.h:55
Definition: slice.h:90
~GrpcBufferReader() override
Definition: proto_utils.h:127
bool Next(const void **data, int *size) override
Definition: proto_utils.h:131
Definition: grpc_types.h:55
#define GRPC_SLICE_START_PTR(slice)
Definition: slice.h:126
bool Skip(int count) override
Definition: proto_utils.h:159
Definition: slice.h:108
#define GRPC_SLICE_END_PTR(slice)
Definition: slice.h:135
void BackUp(int count) override
Definition: proto_utils.h:157
static Status Deserialize(grpc_byte_buffer *buffer, grpc::protobuf::Message *msg)
Definition: proto_utils.h:212
virtual void grpc_byte_buffer_destroy(grpc_byte_buffer *bb)=0
Defines how to serialize and deserialize some type.
Definition: serialization_traits.h:64
::google::protobuf::io::ZeroCopyInputStream ZeroCopyInputStream
Definition: config_protobuf.h:101
static Status Serialize(const grpc::protobuf::Message &msg, grpc_byte_buffer **bp, bool *own_buffer)
Definition: proto_utils.h:191
struct grpc_slice_refcount * refcount
Definition: slice.h:91
void BackUp(int count) override
Definition: proto_utils.h:87
CoreCodegenInterface * g_core_codegen_interface
Definition: call.h:63
virtual grpc_byte_buffer * grpc_raw_byte_buffer_create(grpc_slice *slice, size_t nslices)=0
GrpcBufferWriter(grpc_byte_buffer **bp, int block_size)
Definition: proto_utils.h:60
Definition: proto_utils.h:57
friend class GrpcBufferWriterPeer
Definition: proto_utils.h:107
::google::protobuf::int64 int64
Definition: config_protobuf.h:86
virtual grpc_slice grpc_slice_split_tail(grpc_slice *s, size_t split)=0
grpc::protobuf::int64 ByteCount() const override
Definition: proto_utils.h:104
virtual void grpc_slice_unref(grpc_slice slice)=0
bool Next(void **data, int *size) override
Definition: proto_utils.h:72
virtual void grpc_slice_buffer_add(grpc_slice_buffer *sb, grpc_slice slice)=0
bool ok() const
Is the status OK?
Definition: status.h:76
Did it work? If it didn't, why?
Definition: status.h:45
virtual const Status & ok()=0
virtual int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader *reader, grpc_slice *slice)=0
::google::protobuf::io::ZeroCopyOutputStream ZeroCopyOutputStream
Definition: config_protobuf.h:100
#define GRPC_SLICE_LENGTH(slice)
Definition: slice.h:129
GrpcBufferReader(grpc_byte_buffer *buffer)
Definition: proto_utils.h:119
Definition: byte_buffer_reader.h:43
virtual int grpc_byte_buffer_reader_init(grpc_byte_buffer_reader *reader, grpc_byte_buffer *buffer) GRPC_MUST_USE_RESULT=0
Internal errors.
Definition: status_code_enum.h:134
Status status() const
Definition: proto_utils.h:155
::google::protobuf::Message Message
Definition: config_protobuf.h:85
::google::protobuf::io::CodedInputStream CodedInputStream
Definition: config_protobuf.h:102