blob: eb9608a14e4df2eec107745792d3015c12e0e3d5 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * 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
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080037#include <grpc/support/slice_buffer.h>
38
David Garcia Quintas25d02d52015-06-04 17:40:54 -070039typedef enum {
40 GRPC_BB_SLICE_BUFFER,
41
42 /* Keep the GRPC_BB_COMPRESSED_* entries in the same order as the
43 * grpc_compression_algorithm enum entries. */
44 GRPC_BB_COMPRESSED_NONE, /* for overriding otherwise compressed channels */
45 GRPC_BB_COMPRESSED_DEFLATE,
46 GRPC_BB_COMPRESSED_GZIP
47} grpc_byte_buffer_type;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048
nnoble0c475f02014-12-05 15:37:39 -080049/* byte buffers are containers for messages passed in from the public api's */
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080050struct grpc_byte_buffer {
51 grpc_byte_buffer_type type;
52 union {
53 gpr_slice_buffer slice_buffer;
54 } data;
55};
David Garcia Quintas25d02d52015-06-04 17:40:54 -070056typedef struct grpc_byte_buffer grpc_byte_buffer;
57
58/** Returns the grpc_compression_algorithm enum value for a
59 * grpc_byte_buffer_type GRPC_BB_COMPRESSED_* value. */
60#define GRPC_COMPRESS_ALGORITHM_FROM_BB_TYPE(bb_type) \
61 (bb_type - GRPC_BB_COMPRESSED_NONE)
62
63/** Returns a byte buffer instance over the given slices (up to \a nslices) of
64 * \a type type.
65 *
66 * Increases the reference count for all \a slices processed.
67 *
68 * The user is responsible for invoking grpc_byte_buffer_destroy on
69 * the returned instance. */
70grpc_byte_buffer *grpc_byte_buffer_typed_create(gpr_slice *slices,
71 size_t nslices,
72 grpc_byte_buffer_type type);
73/** Convenience method for creating GRPC_BB_SLICE_BUFFER byte buffers. \sa
74 * grpc_byte_buffer_typed_create */
75grpc_byte_buffer *grpc_byte_buffer_create(gpr_slice *slices, size_t nslices);
76
77/** Copies input byte buffer \a bb.
78 *
79 * Increases the reference count of all the source slices. The user is
80 * responsible for calling grpc_byte_buffer_destroy over the returned copy. */
81grpc_byte_buffer *grpc_byte_buffer_copy(grpc_byte_buffer *bb);
82
83/** Returns the size of the given byte buffer, in bytes. */
84size_t grpc_byte_buffer_length(grpc_byte_buffer *bb);
85
86/** Destroys \a byte_buffer deallocating all its memory. */
87void grpc_byte_buffer_destroy(grpc_byte_buffer *byte_buffer);
88
89
90/** Reader for byte buffers. Iterates over slices in the byte buffer */
91struct grpc_byte_buffer_reader;
92typedef struct grpc_byte_buffer_reader grpc_byte_buffer_reader;
93
94/** Initialize \a reader to read over \a buffer */
95void grpc_byte_buffer_reader_init(grpc_byte_buffer_reader *reader,
96 grpc_byte_buffer *buffer);
97
98/** Cleanup and destroy \a reader */
99void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader *reader);
100
101/** Updates \a slice with the next piece of data from from \a reader and returns
102 * 1. Returns 0 at the end of the stream. Caller is responsible for calling
103 * gpr_slice_unref on the result. */
104int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader *reader,
105 gpr_slice *slice);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800106
Nicolas "Pixel" Noble1ff52d52015-03-01 05:24:36 +0100107#endif /* GRPC_BYTE_BUFFER_H */