OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, 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_SUPPORT_BYTE_BUFFER_H |
| 35 #define GRPCXX_SUPPORT_BYTE_BUFFER_H |
| 36 |
| 37 #include <grpc/grpc.h> |
| 38 #include <grpc/byte_buffer.h> |
| 39 #include <grpc/support/log.h> |
| 40 #include <grpc++/impl/serialization_traits.h> |
| 41 #include <grpc++/support/config.h> |
| 42 #include <grpc++/support/slice.h> |
| 43 #include <grpc++/support/status.h> |
| 44 |
| 45 #include <vector> |
| 46 |
| 47 namespace grpc { |
| 48 |
| 49 /// A sequence of bytes. |
| 50 class ByteBuffer GRPC_FINAL { |
| 51 public: |
| 52 /// Constuct an empty buffer. |
| 53 ByteBuffer() : buffer_(nullptr) {} |
| 54 |
| 55 /// Construct buffer from \a slices, of which there are \a nslices. |
| 56 ByteBuffer(const Slice* slices, size_t nslices); |
| 57 |
| 58 /// Constuct a byte buffer by referencing elements of existing buffer |
| 59 /// \a buf. Wrapper of core function grpc_byte_buffer_copy |
| 60 ByteBuffer(const ByteBuffer& buf); |
| 61 |
| 62 ~ByteBuffer(); |
| 63 |
| 64 ByteBuffer& operator=(const ByteBuffer&); |
| 65 |
| 66 /// Dump (read) the buffer contents into \a slices. |
| 67 void Dump(std::vector<Slice>* slices) const; |
| 68 |
| 69 /// Remove all data. |
| 70 void Clear(); |
| 71 |
| 72 /// Buffer size in bytes. |
| 73 size_t Length() const; |
| 74 |
| 75 private: |
| 76 friend class SerializationTraits<ByteBuffer, void>; |
| 77 |
| 78 // takes ownership |
| 79 void set_buffer(grpc_byte_buffer* buf) { |
| 80 if (buffer_) { |
| 81 Clear(); |
| 82 } |
| 83 buffer_ = buf; |
| 84 } |
| 85 |
| 86 // For \a SerializationTraits's usage. |
| 87 grpc_byte_buffer* buffer() const { return buffer_; } |
| 88 |
| 89 grpc_byte_buffer* buffer_; |
| 90 }; |
| 91 |
| 92 template <> |
| 93 class SerializationTraits<ByteBuffer, void> { |
| 94 public: |
| 95 static Status Deserialize(grpc_byte_buffer* byte_buffer, ByteBuffer* dest, |
| 96 int max_message_size) { |
| 97 dest->set_buffer(byte_buffer); |
| 98 return Status::OK; |
| 99 } |
| 100 static Status Serialize(const ByteBuffer& source, grpc_byte_buffer** buffer, |
| 101 bool* own_buffer) { |
| 102 *buffer = source.buffer(); |
| 103 *own_buffer = false; |
| 104 return Status::OK; |
| 105 } |
| 106 }; |
| 107 |
| 108 } // namespace grpc |
| 109 |
| 110 #endif // GRPCXX_SUPPORT_BYTE_BUFFER_H |
OLD | NEW |