|
| 1 | +// Copyright 2023-2025 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. |
| 2 | + |
| 3 | +#ifndef GRPC_BYTE_BUFFER_H |
| 4 | +#define GRPC_BYTE_BUFFER_H |
| 5 | + |
| 6 | +#include <grpc/impl/grpc_types.h> |
| 7 | +#include <grpc/slice_buffer.h> |
| 8 | +#include <grpc/support/port_platform.h> |
| 9 | + |
| 10 | +#ifdef __cplusplus |
| 11 | +extern "C" { |
| 12 | +#endif |
| 13 | + |
| 14 | +/** Returns a RAW byte buffer instance over the given slices (up to \a nslices). |
| 15 | + * |
| 16 | + * Increases the reference count for all \a slices processed. The user is |
| 17 | + * responsible for invoking grpc_byte_buffer_destroy on the returned instance.*/ |
| 18 | +GRPCAPI grpc_byte_buffer* grpc_raw_byte_buffer_create(grpc_slice* slices, |
| 19 | + size_t nslices); |
| 20 | + |
| 21 | +/** Returns a *compressed* RAW byte buffer instance over the given slices (up to |
| 22 | + * \a nslices). The \a compression argument defines the compression algorithm |
| 23 | + * used to generate the data in \a slices. |
| 24 | + * |
| 25 | + * Increases the reference count for all \a slices processed. The user is |
| 26 | + * responsible for invoking grpc_byte_buffer_destroy on the returned instance.*/ |
| 27 | +GRPCAPI grpc_byte_buffer* grpc_raw_compressed_byte_buffer_create( |
| 28 | + grpc_slice* slices, size_t nslices, grpc_compression_algorithm compression); |
| 29 | + |
| 30 | +/** Copies input byte buffer \a bb. |
| 31 | + * |
| 32 | + * Increases the reference count of all the source slices. The user is |
| 33 | + * responsible for calling grpc_byte_buffer_destroy over the returned copy. */ |
| 34 | +GRPCAPI grpc_byte_buffer* grpc_byte_buffer_copy(grpc_byte_buffer* bb); |
| 35 | + |
| 36 | +/** Returns the size of the given byte buffer, in bytes. */ |
| 37 | +GRPCAPI size_t grpc_byte_buffer_length(grpc_byte_buffer* bb); |
| 38 | + |
| 39 | +/** Destroys \a byte_buffer deallocating all its memory. */ |
| 40 | +GRPCAPI void grpc_byte_buffer_destroy(grpc_byte_buffer* bb); |
| 41 | + |
| 42 | +/** Reader for byte buffers. Iterates over slices in the byte buffer */ |
| 43 | +struct grpc_byte_buffer_reader; |
| 44 | +typedef struct grpc_byte_buffer_reader grpc_byte_buffer_reader; |
| 45 | + |
| 46 | +/** Initialize \a reader to read over \a buffer. |
| 47 | + * Returns 1 upon success, 0 otherwise. */ |
| 48 | +GRPCAPI int grpc_byte_buffer_reader_init(grpc_byte_buffer_reader* reader, |
| 49 | + grpc_byte_buffer* buffer); |
| 50 | + |
| 51 | +/** Cleanup and destroy \a reader */ |
| 52 | +GRPCAPI void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader* reader); |
| 53 | + |
| 54 | +/** Updates \a slice with the next piece of data from from \a reader and returns |
| 55 | + * 1. Returns 0 at the end of the stream. Caller is responsible for calling |
| 56 | + * grpc_slice_unref on the result. */ |
| 57 | +GRPCAPI int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader* reader, |
| 58 | + grpc_slice* slice); |
| 59 | + |
| 60 | +/** EXPERIMENTAL API - This function may be removed and changed, in the future. |
| 61 | + * |
| 62 | + * Updates \a slice with the next piece of data from from \a reader and returns |
| 63 | + * 1. Returns 0 at the end of the stream. Caller is responsible for making sure |
| 64 | + * the slice pointer remains valid when accessed. |
| 65 | + * |
| 66 | + * NOTE: Do not use this function unless the caller can guarantee that the |
| 67 | + * underlying grpc_byte_buffer outlasts the use of the slice. This is only |
| 68 | + * safe when the underlying grpc_byte_buffer remains immutable while slice |
| 69 | + * is being accessed. */ |
| 70 | +GRPCAPI int grpc_byte_buffer_reader_peek(grpc_byte_buffer_reader* reader, |
| 71 | + grpc_slice** slice); |
| 72 | + |
| 73 | +/** Merge all data from \a reader into single slice */ |
| 74 | +GRPCAPI grpc_slice |
| 75 | +grpc_byte_buffer_reader_readall(grpc_byte_buffer_reader* reader); |
| 76 | + |
| 77 | +/** Returns a RAW byte buffer instance from the output of \a reader. */ |
| 78 | +GRPCAPI grpc_byte_buffer* grpc_raw_byte_buffer_from_reader( |
| 79 | + grpc_byte_buffer_reader* reader); |
| 80 | + |
| 81 | +#ifdef __cplusplus |
| 82 | +} |
| 83 | +#endif |
| 84 | + |
| 85 | +#endif /* GRPC_BYTE_BUFFER_H */ |
0 commit comments