byte_buffer_cc.cc 3.4 KB

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