byte_buffer_cc.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #include <grpc++/support/byte_buffer.h>
  19. #include <grpc/byte_buffer_reader.h>
  20. namespace grpc {
  21. ByteBuffer::ByteBuffer(const Slice* slices, size_t nslices) {
  22. // The following assertions check that the representation of a grpc::Slice is
  23. // identical to that of a grpc_slice: it has a grpc_slice field, and nothing
  24. // else.
  25. static_assert(std::is_same<decltype(slices[0].slice_), grpc_slice>::value,
  26. "Slice must have same representation as grpc_slice");
  27. static_assert(sizeof(Slice) == sizeof(grpc_slice),
  28. "Slice must have same representation as grpc_slice");
  29. // The const_cast is legal if grpc_raw_byte_buffer_create() does no more
  30. // than its advertised side effect of increasing the reference count of the
  31. // slices it processes, and such an increase does not affect the semantics
  32. // seen by the caller of this constructor.
  33. buffer_ = grpc_raw_byte_buffer_create(
  34. reinterpret_cast<grpc_slice*>(const_cast<Slice*>(slices)), nslices);
  35. }
  36. ByteBuffer::~ByteBuffer() {
  37. if (buffer_) {
  38. grpc_byte_buffer_destroy(buffer_);
  39. }
  40. }
  41. void ByteBuffer::Clear() {
  42. if (buffer_) {
  43. grpc_byte_buffer_destroy(buffer_);
  44. buffer_ = nullptr;
  45. }
  46. }
  47. Status ByteBuffer::Dump(std::vector<Slice>* slices) const {
  48. slices->clear();
  49. if (!buffer_) {
  50. return Status(StatusCode::FAILED_PRECONDITION, "Buffer not initialized");
  51. }
  52. grpc_byte_buffer_reader reader;
  53. if (!grpc_byte_buffer_reader_init(&reader, buffer_)) {
  54. return Status(StatusCode::INTERNAL,
  55. "Couldn't initialize byte buffer reader");
  56. }
  57. grpc_slice s;
  58. while (grpc_byte_buffer_reader_next(&reader, &s)) {
  59. slices->push_back(Slice(s, Slice::STEAL_REF));
  60. }
  61. grpc_byte_buffer_reader_destroy(&reader);
  62. return Status::OK;
  63. }
  64. size_t ByteBuffer::Length() const {
  65. if (buffer_) {
  66. return grpc_byte_buffer_length(buffer_);
  67. } else {
  68. return 0;
  69. }
  70. }
  71. ByteBuffer::ByteBuffer(const ByteBuffer& buf)
  72. : buffer_(grpc_byte_buffer_copy(buf.buffer_)) {}
  73. ByteBuffer& ByteBuffer::operator=(const ByteBuffer& buf) {
  74. Clear(); // first remove existing data
  75. if (buf.buffer_) {
  76. buffer_ = grpc_byte_buffer_copy(buf.buffer_); // then copy
  77. }
  78. return *this;
  79. }
  80. void ByteBuffer::Swap(ByteBuffer* other) {
  81. grpc_byte_buffer* tmp = other->buffer_;
  82. other->buffer_ = buffer_;
  83. buffer_ = tmp;
  84. }
  85. } // namespace grpc