blob: ae01f9129533d5c8209610486d4b3de23184eb6f [file] [log] [blame]
Craig Tiller9d35a1f2015-11-02 14:16:12 -08001/*
2 *
David Garcia Quintas3598d442016-03-15 14:53:05 -07003 * Copyright 2015-2016, Google Inc.
Craig Tiller9d35a1f2015-11-02 14:16:12 -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
Craig Tiller9a4dddd2016-03-25 17:08:13 -070034#ifndef GRPC_CORE_LIB_TRANSPORT_BYTE_STREAM_H
35#define GRPC_CORE_LIB_TRANSPORT_BYTE_STREAM_H
Craig Tiller9d35a1f2015-11-02 14:16:12 -080036
Craig Tiller9d35a1f2015-11-02 14:16:12 -080037#include <grpc/support/slice_buffer.h>
Craig Tillerf40df232016-03-25 13:38:14 -070038#include "src/core/iomgr/exec_ctx.h"
Craig Tiller9d35a1f2015-11-02 14:16:12 -080039
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
46struct grpc_byte_stream;
47typedef struct grpc_byte_stream grpc_byte_stream;
48
49struct grpc_byte_stream {
Craig Tiller7536af02015-12-22 13:49:30 -080050 uint32_t length;
51 uint32_t flags;
Craig Tiller9d35a1f2015-11-02 14:16:12 -080052 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 Tiller3b66ab92015-12-09 19:42:22 -080055 void (*destroy)(grpc_exec_ctx *exec_ctx, grpc_byte_stream *byte_stream);
Craig Tiller9d35a1f2015-11-02 14:16:12 -080056};
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 */
71int 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 Tillera3f298f2015-12-16 19:42:09 -080075void grpc_byte_stream_destroy(grpc_exec_ctx *exec_ctx,
76 grpc_byte_stream *byte_stream);
Craig Tiller9d35a1f2015-11-02 14:16:12 -080077
78/* grpc_byte_stream that wraps a slice buffer */
79typedef 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
85void grpc_slice_buffer_stream_init(grpc_slice_buffer_stream *stream,
86 gpr_slice_buffer *slice_buffer,
Craig Tiller7536af02015-12-22 13:49:30 -080087 uint32_t flags);
Craig Tiller9d35a1f2015-11-02 14:16:12 -080088
Craig Tiller9a4dddd2016-03-25 17:08:13 -070089#endif /* GRPC_CORE_LIB_TRANSPORT_BYTE_STREAM_H */