| 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 |
#include <vector> |
| 20 |
|
| 21 |
#include <grpc/byte_buffer.h> |
| 22 |
#include <grpc/byte_buffer_reader.h> |
| 23 |
#include <grpc/impl/codegen/compression_types.h> |
| 24 |
#include <grpc/impl/codegen/grpc_types.h> |
| 25 |
#include <grpc/slice.h> |
| 26 |
#include <grpcpp/impl/grpc_library.h> |
| 27 |
#include <grpcpp/support/byte_buffer.h> |
| 28 |
#include <grpcpp/support/slice.h> |
| 29 |
#include <grpcpp/support/status.h> |
| 30 |
|
| 31 |
namespace grpc { |
| 32 |
|
| 33 |
static internal::GrpcLibraryInitializer g_gli_initializer; |
| 34 |
|
| 35 |
Status ByteBuffer::TrySingleSlice(Slice* slice) const { |
| 36 |
if (!buffer_) { |
| 37 |
return Status(StatusCode::FAILED_PRECONDITION, "Buffer not initialized"); |
| 38 |
} |
| 39 |
if ((buffer_->type == GRPC_BB_RAW) && |
| 40 |
(buffer_->data.raw.compression == GRPC_COMPRESS_NONE) && |
| 41 |
(buffer_->data.raw.slice_buffer.count == 1)) { |
| 42 |
grpc_slice internal_slice = buffer_->data.raw.slice_buffer.slices[0]; |
| 43 |
*slice = Slice(internal_slice, Slice::ADD_REF); |
| 44 |
return Status::OK; |
| 45 |
} else { |
| 46 |
return Status(StatusCode::FAILED_PRECONDITION, |
| 47 |
"Buffer isn't made up of a single uncompressed slice."); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
Status ByteBuffer::DumpToSingleSlice(Slice* slice) const { |
| 52 |
if (!buffer_) { |
| 53 |
return Status(StatusCode::FAILED_PRECONDITION, "Buffer not initialized"); |
| 54 |
} |
| 55 |
grpc_byte_buffer_reader reader; |
| 56 |
if (!grpc_byte_buffer_reader_init(&reader, buffer_)) { |
| 57 |
return Status(StatusCode::INTERNAL, |
| 58 |
"Couldn't initialize byte buffer reader"); |
| 59 |
} |
| 60 |
grpc_slice s = grpc_byte_buffer_reader_readall(&reader); |
| 61 |
*slice = Slice(s, Slice::STEAL_REF); |
| 62 |
grpc_byte_buffer_reader_destroy(&reader); |
| 63 |
return Status::OK; |
| 64 |
} |
| 65 |
|
| 66 |
Status ByteBuffer::Dump(std::vector<Slice>* slices) const { |
| 67 |
slices->clear(); |
| 68 |
if (!buffer_) { |
| 69 |
return Status(StatusCode::FAILED_PRECONDITION, "Buffer not initialized"); |
| 70 |
} |
| 71 |
grpc_byte_buffer_reader reader; |
| 72 |
if (!grpc_byte_buffer_reader_init(&reader, buffer_)) { |
| 73 |
return Status(StatusCode::INTERNAL, |
| 74 |
"Couldn't initialize byte buffer reader"); |
| 75 |
} |
| 76 |
grpc_slice s; |
| 77 |
while (grpc_byte_buffer_reader_next(&reader, &s)) { |
| 78 |
slices->push_back(Slice(s, Slice::STEAL_REF)); |
| 79 |
} |
| 80 |
grpc_byte_buffer_reader_destroy(&reader); |
| 81 |
return Status::OK; |
| 82 |
} |
| 83 |
|
| 84 |
} // namespace grpc |