blob: a62054ac19a5dfea549e90f5d4d593af8313debf [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
David Garcia Quintas59f905d2015-06-08 16:31:19 -070037#include <grpc/compression.h>
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080038#include <grpc/support/slice_buffer.h>
39
David Garcia Quintas59f905d2015-06-08 16:31:19 -070040#ifdef __cplusplus
41extern "C" {
42#endif
David Garcia Quintas25d02d52015-06-04 17:40:54 -070043
David Garcia Quintas59f905d2015-06-08 16:31:19 -070044typedef enum {
45 GRPC_BB_RAW
46 /* Future types may include GRPC_BB_PROTOBUF, etc. */
David Garcia Quintas25d02d52015-06-04 17:40:54 -070047} grpc_byte_buffer_type;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080049struct grpc_byte_buffer {
50 grpc_byte_buffer_type type;
51 union {
David Garcia Quintas59f905d2015-06-08 16:31:19 -070052 struct {
53 grpc_compression_algorithm compression;
54 gpr_slice_buffer slice_buffer;
55 } raw;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080056 } data;
57};
David Garcia Quintas25d02d52015-06-04 17:40:54 -070058typedef struct grpc_byte_buffer grpc_byte_buffer;
59
David Garcia Quintas59f905d2015-06-08 16:31:19 -070060/** Returns a RAW byte buffer instance over the given slices (up to \a nslices).
61 *
62 * Increases the reference count for all \a slices processed. The user is
63 * responsible for invoking grpc_byte_buffer_destroy on the returned instance.*/
64grpc_byte_buffer *grpc_raw_byte_buffer_create(gpr_slice *slices,
65 size_t nslices);
David Garcia Quintas25d02d52015-06-04 17:40:54 -070066
David Garcia Quintas59f905d2015-06-08 16:31:19 -070067/** Returns a *compressed* RAW byte buffer instance over the given slices (up to
68 * \a nslices). The \a compression argument defines the compression algorithm
69 * used to generate the data in \a slices.
David Garcia Quintas25d02d52015-06-04 17:40:54 -070070 *
David Garcia Quintas59f905d2015-06-08 16:31:19 -070071 * Increases the reference count for all \a slices processed. The user is
72 * responsible for invoking grpc_byte_buffer_destroy on the returned instance.*/
73grpc_byte_buffer *grpc_raw_compressed_byte_buffer_create(
74 gpr_slice *slices, size_t nslices, grpc_compression_algorithm compression);
David Garcia Quintas25d02d52015-06-04 17:40:54 -070075
76/** Copies input byte buffer \a bb.
77 *
78 * Increases the reference count of all the source slices. The user is
79 * responsible for calling grpc_byte_buffer_destroy over the returned copy. */
80grpc_byte_buffer *grpc_byte_buffer_copy(grpc_byte_buffer *bb);
81
82/** Returns the size of the given byte buffer, in bytes. */
83size_t grpc_byte_buffer_length(grpc_byte_buffer *bb);
84
85/** Destroys \a byte_buffer deallocating all its memory. */
86void grpc_byte_buffer_destroy(grpc_byte_buffer *byte_buffer);
87
David Garcia Quintas25d02d52015-06-04 17:40:54 -070088/** Reader for byte buffers. Iterates over slices in the byte buffer */
89struct grpc_byte_buffer_reader;
90typedef struct grpc_byte_buffer_reader grpc_byte_buffer_reader;
91
92/** Initialize \a reader to read over \a buffer */
93void grpc_byte_buffer_reader_init(grpc_byte_buffer_reader *reader,
94 grpc_byte_buffer *buffer);
95
96/** Cleanup and destroy \a reader */
97void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader *reader);
98
99/** Updates \a slice with the next piece of data from from \a reader and returns
100 * 1. Returns 0 at the end of the stream. Caller is responsible for calling
101 * gpr_slice_unref on the result. */
102int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader *reader,
103 gpr_slice *slice);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800104
David Garcia Quintas59f905d2015-06-08 16:31:19 -0700105#ifdef __cplusplus
106}
107#endif
108
Craig Tiller9a576332015-06-17 10:21:49 -0700109#endif /* GRPC_BYTE_BUFFER_H */