Craig Tiller | db57c4f | 2015-02-24 10:34:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Craig Tiller | db57c4f | 2015-02-24 10:34:47 -0800 | [diff] [blame] | 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 | |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 34 | #ifndef GRPCXX_SUPPORT_BYTE_BUFFER_H |
| 35 | #define GRPCXX_SUPPORT_BYTE_BUFFER_H |
Craig Tiller | db57c4f | 2015-02-24 10:34:47 -0800 | [diff] [blame] | 36 | |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 37 | #include <grpc++/impl/serialization_traits.h> |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 38 | #include <grpc++/support/config.h> |
| 39 | #include <grpc++/support/slice.h> |
| 40 | #include <grpc++/support/status.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 41 | #include <grpc/byte_buffer.h> |
| 42 | #include <grpc/grpc.h> |
| 43 | #include <grpc/support/log.h> |
Yang Gao | 61c4131 | 2015-03-10 12:42:18 -0700 | [diff] [blame] | 44 | |
| 45 | #include <vector> |
Craig Tiller | db57c4f | 2015-02-24 10:34:47 -0800 | [diff] [blame] | 46 | |
| 47 | namespace grpc { |
| 48 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 49 | /// A sequence of bytes. |
Yang Gao | 5f4539f | 2015-03-06 16:11:16 -0800 | [diff] [blame] | 50 | class ByteBuffer GRPC_FINAL { |
Craig Tiller | db57c4f | 2015-02-24 10:34:47 -0800 | [diff] [blame] | 51 | public: |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 52 | /// Constuct an empty buffer. |
Yang Gao | 5f4539f | 2015-03-06 16:11:16 -0800 | [diff] [blame] | 53 | ByteBuffer() : buffer_(nullptr) {} |
| 54 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 55 | /// Construct buffer from \a slices, of which there are \a nslices. |
Yang Gao | 89c5a56 | 2015-06-22 16:31:11 -0700 | [diff] [blame] | 56 | ByteBuffer(const Slice* slices, size_t nslices); |
Yang Gao | 61c4131 | 2015-03-10 12:42:18 -0700 | [diff] [blame] | 57 | |
Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 58 | /// Constuct a byte buffer by referencing elements of existing buffer |
| 59 | /// \a buf. Wrapper of core function grpc_byte_buffer_copy |
Craig Tiller | 754bd26 | 2016-01-14 06:10:15 -0800 | [diff] [blame] | 60 | ByteBuffer(const ByteBuffer& buf); |
Vijay Pai | 18c0477 | 2016-01-04 09:52:10 -0800 | [diff] [blame] | 61 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 62 | ~ByteBuffer(); |
Craig Tiller | db57c4f | 2015-02-24 10:34:47 -0800 | [diff] [blame] | 63 | |
vjpai | de332df | 2016-01-07 10:20:46 -0800 | [diff] [blame] | 64 | ByteBuffer& operator=(const ByteBuffer&); |
| 65 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 66 | /// Dump (read) the buffer contents into \a slices. |
David Garcia Quintas | 6721d4f | 2016-06-30 17:17:23 -0700 | [diff] [blame] | 67 | Status Dump(std::vector<Slice>* slices) const; |
Yang Gao | 61c4131 | 2015-03-10 12:42:18 -0700 | [diff] [blame] | 68 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 69 | /// Remove all data. |
Yang Gao | 61c4131 | 2015-03-10 12:42:18 -0700 | [diff] [blame] | 70 | void Clear(); |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 71 | |
| 72 | /// Buffer size in bytes. |
Yang Gao | 89c5a56 | 2015-06-22 16:31:11 -0700 | [diff] [blame] | 73 | size_t Length() const; |
Yang Gao | 61c4131 | 2015-03-10 12:42:18 -0700 | [diff] [blame] | 74 | |
Mark D. Roth | 2665bdd | 2016-08-18 08:02:24 -0700 | [diff] [blame] | 75 | /// Swap the state of *this and *other. |
| 76 | void Swap(ByteBuffer* other); |
| 77 | |
Craig Tiller | db57c4f | 2015-02-24 10:34:47 -0800 | [diff] [blame] | 78 | private: |
Craig Tiller | 789471c | 2015-06-04 16:19:22 -0700 | [diff] [blame] | 79 | friend class SerializationTraits<ByteBuffer, void>; |
| 80 | |
Yang Gao | 5f4539f | 2015-03-06 16:11:16 -0800 | [diff] [blame] | 81 | // takes ownership |
| 82 | void set_buffer(grpc_byte_buffer* buf) { |
Yang Gao | 61c4131 | 2015-03-10 12:42:18 -0700 | [diff] [blame] | 83 | if (buffer_) { |
Yang Gao | 61c4131 | 2015-03-10 12:42:18 -0700 | [diff] [blame] | 84 | Clear(); |
| 85 | } |
Yang Gao | 5f4539f | 2015-03-06 16:11:16 -0800 | [diff] [blame] | 86 | buffer_ = buf; |
| 87 | } |
| 88 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 89 | // For \a SerializationTraits's usage. |
Yang Gao | 6baa9b6 | 2015-03-17 10:49:39 -0700 | [diff] [blame] | 90 | grpc_byte_buffer* buffer() const { return buffer_; } |
Yang Gao | 5f4539f | 2015-03-06 16:11:16 -0800 | [diff] [blame] | 91 | |
| 92 | grpc_byte_buffer* buffer_; |
Craig Tiller | db57c4f | 2015-02-24 10:34:47 -0800 | [diff] [blame] | 93 | }; |
| 94 | |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 95 | template <> |
| 96 | class SerializationTraits<ByteBuffer, void> { |
| 97 | public: |
Craig Tiller | ce40de5 | 2015-06-05 07:14:58 -0700 | [diff] [blame] | 98 | static Status Deserialize(grpc_byte_buffer* byte_buffer, ByteBuffer* dest, |
Mark D. Roth | 6980362 | 2016-09-06 08:15:36 -0700 | [diff] [blame] | 99 | int max_receive_message_size) { |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 100 | dest->set_buffer(byte_buffer); |
| 101 | return Status::OK; |
| 102 | } |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 103 | static Status Serialize(const ByteBuffer& source, grpc_byte_buffer** buffer, |
| 104 | bool* own_buffer) { |
yang-g | f64befd | 2016-03-21 14:04:10 -0700 | [diff] [blame] | 105 | *buffer = grpc_byte_buffer_copy(source.buffer()); |
| 106 | *own_buffer = true; |
Craig Tiller | bd277cb | 2015-06-05 10:43:43 -0700 | [diff] [blame] | 107 | return Status::OK; |
Craig Tiller | 789471c | 2015-06-04 16:19:22 -0700 | [diff] [blame] | 108 | } |
Craig Tiller | 50a7a68 | 2015-06-04 12:53:40 -0700 | [diff] [blame] | 109 | }; |
| 110 | |
Yang Gao | 61c4131 | 2015-03-10 12:42:18 -0700 | [diff] [blame] | 111 | } // namespace grpc |
Craig Tiller | db57c4f | 2015-02-24 10:34:47 -0800 | [diff] [blame] | 112 | |
David Garcia Quintas | 2bf574f | 2016-01-14 15:27:08 -0800 | [diff] [blame] | 113 | #endif // GRPCXX_SUPPORT_BYTE_BUFFER_H |