| Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
| Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
| Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -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 | |
| Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 34 | #ifndef GRPC_BYTE_BUFFER_H |
| 35 | #define GRPC_BYTE_BUFFER_H |
| Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 36 | |
| Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 37 | #include <grpc/support/slice_buffer.h> |
| 38 | |
| David Garcia Quintas | 25d02d5 | 2015-06-04 17:40:54 -0700 | [diff] [blame^] | 39 | typedef 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 Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 48 | |
| nnoble | 0c475f0 | 2014-12-05 15:37:39 -0800 | [diff] [blame] | 49 | /* byte buffers are containers for messages passed in from the public api's */ |
| Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 50 | struct grpc_byte_buffer { |
| 51 | grpc_byte_buffer_type type; |
| 52 | union { |
| 53 | gpr_slice_buffer slice_buffer; |
| 54 | } data; |
| 55 | }; |
| David Garcia Quintas | 25d02d5 | 2015-06-04 17:40:54 -0700 | [diff] [blame^] | 56 | typedef 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. */ |
| 70 | grpc_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 */ |
| 75 | grpc_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. */ |
| 81 | grpc_byte_buffer *grpc_byte_buffer_copy(grpc_byte_buffer *bb); |
| 82 | |
| 83 | /** Returns the size of the given byte buffer, in bytes. */ |
| 84 | size_t grpc_byte_buffer_length(grpc_byte_buffer *bb); |
| 85 | |
| 86 | /** Destroys \a byte_buffer deallocating all its memory. */ |
| 87 | void grpc_byte_buffer_destroy(grpc_byte_buffer *byte_buffer); |
| 88 | |
| 89 | |
| 90 | /** Reader for byte buffers. Iterates over slices in the byte buffer */ |
| 91 | struct grpc_byte_buffer_reader; |
| 92 | typedef struct grpc_byte_buffer_reader grpc_byte_buffer_reader; |
| 93 | |
| 94 | /** Initialize \a reader to read over \a buffer */ |
| 95 | void grpc_byte_buffer_reader_init(grpc_byte_buffer_reader *reader, |
| 96 | grpc_byte_buffer *buffer); |
| 97 | |
| 98 | /** Cleanup and destroy \a reader */ |
| 99 | void 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. */ |
| 104 | int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader *reader, |
| 105 | gpr_slice *slice); |
| Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 106 | |
| Nicolas "Pixel" Noble | 1ff52d5 | 2015-03-01 05:24:36 +0100 | [diff] [blame] | 107 | #endif /* GRPC_BYTE_BUFFER_H */ |