blob: 06f8969b7042c076b21a52dc7435fedb8d44a323 [file] [log] [blame]
Craig Tillerdb57c4f2015-02-24 10:34:47 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Craig Tillerdb57c4f2015-02-24 10:34:47 -08004 * 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-g9e2f90c2015-08-21 15:35:03 -070034#ifndef GRPCXX_SUPPORT_BYTE_BUFFER_H
35#define GRPCXX_SUPPORT_BYTE_BUFFER_H
Craig Tillerdb57c4f2015-02-24 10:34:47 -080036
Craig Tiller50a7a682015-06-04 12:53:40 -070037#include <grpc++/impl/serialization_traits.h>
yang-g9e2f90c2015-08-21 15:35:03 -070038#include <grpc++/support/config.h>
39#include <grpc++/support/slice.h>
40#include <grpc++/support/status.h>
Craig Tillerf40df232016-03-25 13:38:14 -070041#include <grpc/byte_buffer.h>
42#include <grpc/grpc.h>
43#include <grpc/support/log.h>
Yang Gao61c41312015-03-10 12:42:18 -070044
45#include <vector>
Craig Tillerdb57c4f2015-02-24 10:34:47 -080046
47namespace grpc {
48
Craig Tillerd6599a32015-09-03 09:37:02 -070049/// A sequence of bytes.
Yang Gao5f4539f2015-03-06 16:11:16 -080050class ByteBuffer GRPC_FINAL {
Craig Tillerdb57c4f2015-02-24 10:34:47 -080051 public:
Craig Tillerd6599a32015-09-03 09:37:02 -070052 /// Constuct an empty buffer.
Yang Gao5f4539f2015-03-06 16:11:16 -080053 ByteBuffer() : buffer_(nullptr) {}
54
Craig Tillerd6599a32015-09-03 09:37:02 -070055 /// Construct buffer from \a slices, of which there are \a nslices.
Yang Gao89c5a562015-06-22 16:31:11 -070056 ByteBuffer(const Slice* slices, size_t nslices);
Yang Gao61c41312015-03-10 12:42:18 -070057
Vijay Pai18c04772016-01-04 09:52:10 -080058 /// Constuct a byte buffer by referencing elements of existing buffer
59 /// \a buf. Wrapper of core function grpc_byte_buffer_copy
Craig Tiller754bd262016-01-14 06:10:15 -080060 ByteBuffer(const ByteBuffer& buf);
Vijay Pai18c04772016-01-04 09:52:10 -080061
Craig Tillerd6599a32015-09-03 09:37:02 -070062 ~ByteBuffer();
Craig Tillerdb57c4f2015-02-24 10:34:47 -080063
vjpaide332df2016-01-07 10:20:46 -080064 ByteBuffer& operator=(const ByteBuffer&);
65
Craig Tillerd6599a32015-09-03 09:37:02 -070066 /// Dump (read) the buffer contents into \a slices.
David Garcia Quintas6721d4f2016-06-30 17:17:23 -070067 Status Dump(std::vector<Slice>* slices) const;
Yang Gao61c41312015-03-10 12:42:18 -070068
Craig Tillerd6599a32015-09-03 09:37:02 -070069 /// Remove all data.
Yang Gao61c41312015-03-10 12:42:18 -070070 void Clear();
Craig Tillerd6599a32015-09-03 09:37:02 -070071
72 /// Buffer size in bytes.
Yang Gao89c5a562015-06-22 16:31:11 -070073 size_t Length() const;
Yang Gao61c41312015-03-10 12:42:18 -070074
Mark D. Roth2665bdd2016-08-18 08:02:24 -070075 /// Swap the state of *this and *other.
76 void Swap(ByteBuffer* other);
77
Craig Tillerdb57c4f2015-02-24 10:34:47 -080078 private:
Craig Tiller789471c2015-06-04 16:19:22 -070079 friend class SerializationTraits<ByteBuffer, void>;
80
Yang Gao5f4539f2015-03-06 16:11:16 -080081 // takes ownership
82 void set_buffer(grpc_byte_buffer* buf) {
Yang Gao61c41312015-03-10 12:42:18 -070083 if (buffer_) {
Yang Gao61c41312015-03-10 12:42:18 -070084 Clear();
85 }
Yang Gao5f4539f2015-03-06 16:11:16 -080086 buffer_ = buf;
87 }
88
Craig Tillerd6599a32015-09-03 09:37:02 -070089 // For \a SerializationTraits's usage.
Yang Gao6baa9b62015-03-17 10:49:39 -070090 grpc_byte_buffer* buffer() const { return buffer_; }
Yang Gao5f4539f2015-03-06 16:11:16 -080091
92 grpc_byte_buffer* buffer_;
Craig Tillerdb57c4f2015-02-24 10:34:47 -080093};
94
Craig Tiller50a7a682015-06-04 12:53:40 -070095template <>
96class SerializationTraits<ByteBuffer, void> {
97 public:
Craig Tillerce40de52015-06-05 07:14:58 -070098 static Status Deserialize(grpc_byte_buffer* byte_buffer, ByteBuffer* dest,
Mark D. Roth69803622016-09-06 08:15:36 -070099 int max_receive_message_size) {
Craig Tiller50a7a682015-06-04 12:53:40 -0700100 dest->set_buffer(byte_buffer);
101 return Status::OK;
102 }
Craig Tillerd6c98df2015-08-18 09:33:44 -0700103 static Status Serialize(const ByteBuffer& source, grpc_byte_buffer** buffer,
104 bool* own_buffer) {
yang-gf64befd2016-03-21 14:04:10 -0700105 *buffer = grpc_byte_buffer_copy(source.buffer());
106 *own_buffer = true;
Craig Tillerbd277cb2015-06-05 10:43:43 -0700107 return Status::OK;
Craig Tiller789471c2015-06-04 16:19:22 -0700108 }
Craig Tiller50a7a682015-06-04 12:53:40 -0700109};
110
Yang Gao61c41312015-03-10 12:42:18 -0700111} // namespace grpc
Craig Tillerdb57c4f2015-02-24 10:34:47 -0800112
David Garcia Quintas2bf574f2016-01-14 15:27:08 -0800113#endif // GRPCXX_SUPPORT_BYTE_BUFFER_H