GRPC C++  1.13.0-dev
proto_buffer_reader.h
Go to the documentation of this file.
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_IMPL_CODEGEN_PROTO_BUFFER_READER_H
20 #define GRPCPP_IMPL_CODEGEN_PROTO_BUFFER_READER_H
21 
22 #include <type_traits>
23 
32 
35 
36 namespace grpc {
37 
38 extern CoreCodegenInterface* g_core_codegen_interface;
39 
47  public:
50  explicit ProtoBufferReader(ByteBuffer* buffer)
51  : byte_count_(0), backup_count_(0), status_() {
54  if (!buffer->Valid() ||
55  !g_core_codegen_interface->grpc_byte_buffer_reader_init(
56  &reader_, buffer->c_buffer())) {
57  status_ = Status(StatusCode::INTERNAL,
58  "Couldn't initialize byte buffer reader");
59  }
60  }
61 
63  if (status_.ok()) {
64  g_core_codegen_interface->grpc_byte_buffer_reader_destroy(&reader_);
65  }
66  }
67 
70  bool Next(const void** data, int* size) override {
71  if (!status_.ok()) {
72  return false;
73  }
75  if (backup_count_ > 0) {
76  *data = GRPC_SLICE_START_PTR(slice_) + GRPC_SLICE_LENGTH(slice_) -
77  backup_count_;
78  GPR_CODEGEN_ASSERT(backup_count_ <= INT_MAX);
79  *size = (int)backup_count_;
80  backup_count_ = 0;
81  return true;
82  }
84  if (!g_core_codegen_interface->grpc_byte_buffer_reader_next(&reader_,
85  &slice_)) {
86  return false;
87  }
88  g_core_codegen_interface->grpc_slice_unref(slice_);
89  *data = GRPC_SLICE_START_PTR(slice_);
90  // On win x64, int is only 32bit
91  GPR_CODEGEN_ASSERT(GRPC_SLICE_LENGTH(slice_) <= INT_MAX);
92  byte_count_ += * size = (int)GRPC_SLICE_LENGTH(slice_);
93  return true;
94  }
95 
97  Status status() const { return status_; }
98 
102  void BackUp(int count) override {
103  GPR_CODEGEN_ASSERT(count <= static_cast<int>(GRPC_SLICE_LENGTH(slice_)));
104  backup_count_ = count;
105  }
106 
109  bool Skip(int count) override {
110  const void* data;
111  int size;
112  while (Next(&data, &size)) {
113  if (size >= count) {
114  BackUp(size - count);
115  return true;
116  }
117  // size < count;
118  count -= size;
119  }
120  // error or we have too large count;
121  return false;
122  }
123 
125  grpc::protobuf::int64 ByteCount() const override {
126  return byte_count_ - backup_count_;
127  }
128 
129  // These protected members are needed to support internal optimizations.
130  // they expose internal bits of grpc core that are NOT stable. If you have
131  // a use case needs to use one of these functions, please send an email to
132  // https://groups.google.com/forum/#!forum/grpc-io.
133  protected:
134  void set_byte_count(int64_t byte_count) { byte_count_ = byte_count; }
135  int64_t backup_count() { return backup_count_; }
136  void set_backup_count(int64_t backup_count) { backup_count_ = backup_count; }
137  grpc_byte_buffer_reader* reader() { return &reader_; }
138  grpc_slice* slice() { return &slice_; }
139 
140  private:
141  int64_t byte_count_;
142  int64_t backup_count_;
143  grpc_byte_buffer_reader reader_;
144  grpc_slice slice_;
146  Status status_;
147 };
148 
149 } // namespace grpc
150 
151 #endif // GRPCPP_IMPL_CODEGEN_PROTO_BUFFER_READER_H
grpc_byte_buffer_reader * reader()
Definition: proto_buffer_reader.h:137
#define GPR_CODEGEN_ASSERT(x)
Codegen specific version of GPR_ASSERT.
Definition: core_codegen_interface.h:138
void BackUp(int count) override
The proto library calls this to indicate that we should back up count bytes that have already been re...
Definition: proto_buffer_reader.h:102
A grpc_slice s, if initialized, represents the byte range s.bytes[0..s.length-1]. ...
Definition: slice.h:80
#define GRPC_SLICE_START_PTR(slice)
Definition: slice.h:116
bool Valid() const
Is this ByteBuffer valid?
Definition: byte_buffer.h:133
Status status() const
Returns the status of the buffer reader.
Definition: proto_buffer_reader.h:97
::google::protobuf::io::ZeroCopyInputStream ZeroCopyInputStream
Definition: config_protobuf.h:88
An Alarm posts the user provided tag to its associated completion queue upon expiry or cancellation...
Definition: alarm.h:31
CoreCodegenInterface * g_core_codegen_interface
Definition: call.h:46
bool Next(const void **data, int *size) override
Give the proto library a chunk of data from the stream.
Definition: proto_buffer_reader.h:70
::google::protobuf::int64 int64
Definition: config_protobuf.h:73
bool ok() const
Is the status OK?
Definition: status.h:118
ProtoBufferReader(ByteBuffer *buffer)
Constructs buffer reader from buffer.
Definition: proto_buffer_reader.h:50
void set_backup_count(int64_t backup_count)
Definition: proto_buffer_reader.h:136
grpc_slice * slice()
Definition: proto_buffer_reader.h:138
grpc::protobuf::int64 ByteCount() const override
Returns the total number of bytes read since this object was created.
Definition: proto_buffer_reader.h:125
Did it work? If it didn&#39;t, why?
Definition: status.h:31
void set_byte_count(int64_t byte_count)
Definition: proto_buffer_reader.h:134
int64_t backup_count()
Definition: proto_buffer_reader.h:135
This is a specialization of the protobuf class ZeroCopyInputStream The principle is to get one chunk ...
Definition: proto_buffer_reader.h:46
#define GRPC_SLICE_LENGTH(slice)
Definition: slice.h:119
bool Skip(int count) override
The proto library calls this to skip over count bytes.
Definition: proto_buffer_reader.h:109
Definition: byte_buffer_reader.h:28
~ProtoBufferReader()
Definition: proto_buffer_reader.h:62
Internal errors.
Definition: status_code_enum.h:119
A sequence of bytes.
Definition: byte_buffer.h:53