byte_buffer.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #ifndef GRPC_IMPL_CODEGEN_BYTE_BUFFER_H
  19. #define GRPC_IMPL_CODEGEN_BYTE_BUFFER_H
  20. // IWYU pragma: private, include <grpc/byte_buffer.h>
  21. #include <grpc/impl/codegen/port_platform.h>
  22. #include <grpc/impl/codegen/grpc_types.h>
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /** Returns a RAW byte buffer instance over the given slices (up to \a nslices).
  27. *
  28. * Increases the reference count for all \a slices processed. The user is
  29. * responsible for invoking grpc_byte_buffer_destroy on the returned instance.*/
  30. GRPCAPI grpc_byte_buffer* grpc_raw_byte_buffer_create(grpc_slice* slices,
  31. size_t nslices);
  32. /** Returns a *compressed* RAW byte buffer instance over the given slices (up to
  33. * \a nslices). The \a compression argument defines the compression algorithm
  34. * used to generate the data in \a slices.
  35. *
  36. * Increases the reference count for all \a slices processed. The user is
  37. * responsible for invoking grpc_byte_buffer_destroy on the returned instance.*/
  38. GRPCAPI grpc_byte_buffer* grpc_raw_compressed_byte_buffer_create(
  39. grpc_slice* slices, size_t nslices, grpc_compression_algorithm compression);
  40. /** Copies input byte buffer \a bb.
  41. *
  42. * Increases the reference count of all the source slices. The user is
  43. * responsible for calling grpc_byte_buffer_destroy over the returned copy. */
  44. GRPCAPI grpc_byte_buffer* grpc_byte_buffer_copy(grpc_byte_buffer* bb);
  45. /** Returns the size of the given byte buffer, in bytes. */
  46. GRPCAPI size_t grpc_byte_buffer_length(grpc_byte_buffer* bb);
  47. /** Destroys \a byte_buffer deallocating all its memory. */
  48. GRPCAPI void grpc_byte_buffer_destroy(grpc_byte_buffer* bb);
  49. /** Reader for byte buffers. Iterates over slices in the byte buffer */
  50. struct grpc_byte_buffer_reader;
  51. typedef struct grpc_byte_buffer_reader grpc_byte_buffer_reader;
  52. /** Initialize \a reader to read over \a buffer.
  53. * Returns 1 upon success, 0 otherwise. */
  54. GRPCAPI int grpc_byte_buffer_reader_init(grpc_byte_buffer_reader* reader,
  55. grpc_byte_buffer* buffer);
  56. /** Cleanup and destroy \a reader */
  57. GRPCAPI void grpc_byte_buffer_reader_destroy(grpc_byte_buffer_reader* reader);
  58. /** Updates \a slice with the next piece of data from from \a reader and returns
  59. * 1. Returns 0 at the end of the stream. Caller is responsible for calling
  60. * grpc_slice_unref on the result. */
  61. GRPCAPI int grpc_byte_buffer_reader_next(grpc_byte_buffer_reader* reader,
  62. grpc_slice* slice);
  63. /** EXPERIMENTAL API - This function may be removed and changed, in the future.
  64. *
  65. * Updates \a slice with the next piece of data from from \a reader and returns
  66. * 1. Returns 0 at the end of the stream. Caller is responsible for making sure
  67. * the slice pointer remains valid when accessed.
  68. *
  69. * NOTE: Do not use this function unless the caller can guarantee that the
  70. * underlying grpc_byte_buffer outlasts the use of the slice. This is only
  71. * safe when the underlying grpc_byte_buffer remains immutable while slice
  72. * is being accessed. */
  73. GRPCAPI int grpc_byte_buffer_reader_peek(grpc_byte_buffer_reader* reader,
  74. grpc_slice** slice);
  75. /** Merge all data from \a reader into single slice */
  76. GRPCAPI grpc_slice
  77. grpc_byte_buffer_reader_readall(grpc_byte_buffer_reader* reader);
  78. /** Returns a RAW byte buffer instance from the output of \a reader. */
  79. GRPCAPI grpc_byte_buffer* grpc_raw_byte_buffer_from_reader(
  80. grpc_byte_buffer_reader* reader);
  81. #ifdef __cplusplus
  82. }
  83. #endif
  84. #endif /* GRPC_IMPL_CODEGEN_BYTE_BUFFER_H */