blob: 395ebef744d459c6e18500cd32e4f3b096288f74 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -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
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +010034#ifndef GRPC_BYTE_BUFFER_H
35#define GRPC_BYTE_BUFFER_H
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080036
David Garcia Quintas3b31fdf2016-07-27 18:17:12 -070037#include <grpc/impl/codegen/grpc_types.h>
Craig Tillerb37d53e2016-10-26 16:16:35 -070038#include <grpc/slice_buffer.h>
David Garcia Quintas3b31fdf2016-07-27 18:17:12 -070039
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44/** Returns a RAW byte buffer instance over the given slices (up to \a nslices).
45 *
46 * Increases the reference count for all \a slices processed. The user is
47 * responsible for invoking grpc_byte_buffer_destroy on the returned instance.*/
Craig Tillerd41a4a72016-10-26 16:16:06 -070048GRPCAPI grpc_byte_buffer *grpc_raw_byte_buffer_create(grpc_slice *slices,
David Garcia Quintas3b31fdf2016-07-27 18:17:12 -070049 size_t nslices);
50
51/** Returns a *compressed* RAW byte buffer instance over the given slices (up to
52 * \a nslices). The \a compression argument defines the compression algorithm
53 * used to generate the data in \a slices.
54 *
55 * Increases the reference count for all \a slices processed. The user is
56 * responsible for invoking grpc_byte_buffer_destroy on the returned instance.*/
57GRPCAPI grpc_byte_buffer *grpc_raw_compressed_byte_buffer_create(
Craig Tillerd41a4a72016-10-26 16:16:06 -070058 grpc_slice *slices, size_t nslices, grpc_compression_algorithm compression);
David Garcia Quintas3b31fdf2016-07-27 18:17:12 -070059
60/** Copies input byte buffer \a bb.
61 *
62 * Increases the reference count of all the source slices. The user is
63 * responsible for calling grpc_byte_buffer_destroy over the returned copy. */
64GRPCAPI grpc_byte_buffer *grpc_byte_buffer_copy(grpc_byte_buffer *bb);
65
66/** Returns the size of the given byte buffer, in bytes. */
67GRPCAPI size_t grpc_byte_buffer_length(grpc_byte_buffer *bb);
68
69/** Destroys \a byte_buffer deallocating all its memory. */
70GRPCAPI void grpc_byte_buffer_destroy(grpc_byte_buffer *byte_buffer);
71
72/** Reader for byte buffers. Iterates over slices in the byte buffer */
73struct grpc_byte_buffer_reader;
74typedef struct grpc_byte_buffer_reader grpc_byte_buffer_reader;
75
76/** Initialize \a reader to read over \a buffer.
77 * Returns 1 upon success, 0 otherwise. */
78GRPCAPI int grpc_byte_buffer_reader_init(grpc_byte_buffer_reader *reader,
79 grpc_byte_buffer *buffer);
80
81/** Cleanup and destroy \a reader */
82GRPCAPI void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader *reader);
83
84/** Updates \a slice with the next piece of data from from \a reader and returns
85 * 1. Returns 0 at the end of the stream. Caller is responsible for calling
Craig Tillerd41a4a72016-10-26 16:16:06 -070086 * grpc_slice_unref on the result. */
David Garcia Quintas3b31fdf2016-07-27 18:17:12 -070087GRPCAPI int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader *reader,
Craig Tillerd41a4a72016-10-26 16:16:06 -070088 grpc_slice *slice);
David Garcia Quintas3b31fdf2016-07-27 18:17:12 -070089
90/** Merge all data from \a reader into single slice */
Craig Tillerd41a4a72016-10-26 16:16:06 -070091GRPCAPI grpc_slice
David Garcia Quintas3b31fdf2016-07-27 18:17:12 -070092grpc_byte_buffer_reader_readall(grpc_byte_buffer_reader *reader);
93
94/** Returns a RAW byte buffer instance from the output of \a reader. */
95GRPCAPI grpc_byte_buffer *grpc_raw_byte_buffer_from_reader(
96 grpc_byte_buffer_reader *reader);
97
98#ifdef __cplusplus
99}
100#endif
David Garcia Quintas59f905d2015-06-08 16:31:19 -0700101
Craig Tiller9a576332015-06-17 10:21:49 -0700102#endif /* GRPC_BYTE_BUFFER_H */