Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
David Garcia Quintas | 3598d44 | 2016-03-15 14:53:05 -0700 | [diff] [blame] | 3 | * Copyright 2015-2016, Google Inc. |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -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 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 34 | #ifndef GRPC_CORE_LIB_TRANSPORT_BYTE_STREAM_H |
| 35 | #define GRPC_CORE_LIB_TRANSPORT_BYTE_STREAM_H |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 36 | |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 37 | #include <grpc/support/slice_buffer.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 38 | #include "src/core/iomgr/exec_ctx.h" |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 39 | |
| 40 | /** Internal bit flag for grpc_begin_message's \a flags signaling the use of |
| 41 | * compression for the message */ |
| 42 | #define GRPC_WRITE_INTERNAL_COMPRESS (0x80000000u) |
| 43 | /** Mask of all valid internal flags. */ |
| 44 | #define GRPC_WRITE_INTERNAL_USED_MASK (GRPC_WRITE_INTERNAL_COMPRESS) |
| 45 | |
| 46 | struct grpc_byte_stream; |
| 47 | typedef struct grpc_byte_stream grpc_byte_stream; |
| 48 | |
| 49 | struct grpc_byte_stream { |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 50 | uint32_t length; |
| 51 | uint32_t flags; |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 52 | int (*next)(grpc_exec_ctx *exec_ctx, grpc_byte_stream *byte_stream, |
| 53 | gpr_slice *slice, size_t max_size_hint, |
| 54 | grpc_closure *on_complete); |
Craig Tiller | 3b66ab9 | 2015-12-09 19:42:22 -0800 | [diff] [blame] | 55 | void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_byte_stream *byte_stream); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | /* returns 1 if the bytes are available immediately (in which case |
| 59 | * on_complete will not be called), 0 if the bytes will be available |
| 60 | * asynchronously. |
| 61 | * |
| 62 | * on entry, *remaining can be set as a hint as to the maximum number |
| 63 | * of bytes that would be acceptable to read. |
| 64 | * |
| 65 | * fills *buffer, *length, *remaining with the bytes, length of bytes |
| 66 | * and length of data remaining to be read before either returning 1 |
| 67 | * or calling on_complete. |
| 68 | * |
| 69 | * once a slice is returned into *slice, it is owned by the caller. |
| 70 | */ |
| 71 | int grpc_byte_stream_next(grpc_exec_ctx *exec_ctx, |
| 72 | grpc_byte_stream *byte_stream, gpr_slice *slice, |
| 73 | size_t max_size_hint, grpc_closure *on_complete); |
| 74 | |
Craig Tiller | a3f298f | 2015-12-16 19:42:09 -0800 | [diff] [blame] | 75 | void grpc_byte_stream_destroy(grpc_exec_ctx *exec_ctx, |
| 76 | grpc_byte_stream *byte_stream); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 77 | |
| 78 | /* grpc_byte_stream that wraps a slice buffer */ |
| 79 | typedef struct grpc_slice_buffer_stream { |
| 80 | grpc_byte_stream base; |
| 81 | gpr_slice_buffer *backing_buffer; |
| 82 | size_t cursor; |
| 83 | } grpc_slice_buffer_stream; |
| 84 | |
| 85 | void grpc_slice_buffer_stream_init(grpc_slice_buffer_stream *stream, |
| 86 | gpr_slice_buffer *slice_buffer, |
Craig Tiller | 7536af0 | 2015-12-22 13:49:30 -0800 | [diff] [blame] | 87 | uint32_t flags); |
Craig Tiller | 9d35a1f | 2015-11-02 14:16:12 -0800 | [diff] [blame] | 88 | |
Craig Tiller | 9a4dddd | 2016-03-25 17:08:13 -0700 | [diff] [blame] | 89 | #endif /* GRPC_CORE_LIB_TRANSPORT_BYTE_STREAM_H */ |