File: async_stream.h

package info (click to toggle)
aws-crt-python 0.20.4%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 72,656 kB
  • sloc: ansic: 381,805; python: 23,008; makefile: 6,251; sh: 4,536; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (117 lines) | stat: -rw-r--r-- 3,993 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#ifndef AWS_IO_ASYNC_STREAM_H
#define AWS_IO_ASYNC_STREAM_H

/**
 * THIS IS AN EXPERIMENTAL AND UNSTABLE API
 * TODO: logging
 * TODO: modify API to return byte-bufs, instead of filling in the provided byte-buf?
 *       this would avoid a copy in the use-cases we know of, but it's more complex
 * TODO: vtable acquire()/release()?
 * TODO: protect against simultaneous reads?
 * TODO: check results of vtable->read() (i.e. 0 byte reads not allowed)?
 *       this would require 1 or 2 additional allocations per read
 */

#include <aws/io/io.h>

#include <aws/common/ref_count.h>

AWS_PUSH_SANE_WARNING_LEVEL

struct aws_async_input_stream;
struct aws_byte_buf;
struct aws_future_bool;
struct aws_input_stream;

struct aws_async_input_stream {
    const struct aws_async_input_stream_vtable *vtable;
    struct aws_allocator *alloc;
    struct aws_ref_count ref_count;
    void *impl;
};

struct aws_async_input_stream_vtable {
    /**
     * Destroy the stream, its refcount has reached 0.
     */
    void (*destroy)(struct aws_async_input_stream *stream);

    /**
     * Read once into the buffer.
     * Complete the read when at least 1 byte is read, the buffer is full, or EOF is reached.
     * Do not resize the buffer (do not use "aws_byte_buf_xyz_dynamic()" functions)
     * Do not assume that buffer len starts at 0.
     * You may assume that read() won't be called again until the current one completes.
     * You may assume that the buffer has some space available.
     * Return a future, which will contain an error code if something went wrong,
     * or a result bool indicating whether EOF has been reached.
     */
    struct aws_future_bool *(*read)(struct aws_async_input_stream *stream, struct aws_byte_buf *dest);
};

AWS_EXTERN_C_BEGIN

/**
 * Initialize aws_async_input_stream "base class"
 */
AWS_IO_API
void aws_async_input_stream_init_base(
    struct aws_async_input_stream *stream,
    struct aws_allocator *alloc,
    const struct aws_async_input_stream_vtable *vtable,
    void *impl);

/**
 * Increment reference count.
 * You may pass in NULL (has no effect).
 * Returns whatever pointer was passed in.
 */
AWS_IO_API
struct aws_async_input_stream *aws_async_input_stream_acquire(struct aws_async_input_stream *stream);

/**
 * Decrement reference count.
 * You may pass in NULL (has no effect).
 * Always returns NULL.
 */
AWS_IO_API
struct aws_async_input_stream *aws_async_input_stream_release(struct aws_async_input_stream *stream);

/**
 * Read once from the async stream into the buffer.
 * The read completes when at least 1 byte is read, the buffer is full, or EOF is reached.
 * Depending on implementation, the read could complete at any time.
 * It may complete synchronously. It may complete on another thread.
 * Returns a future, which will contain an error code if something went wrong,
 * or a result bool indicating whether EOF has been reached.
 *
 * WARNING: The buffer must have space available.
 * WARNING: Do not read again until the previous read is complete.
 */
AWS_IO_API
struct aws_future_bool *aws_async_input_stream_read(struct aws_async_input_stream *stream, struct aws_byte_buf *dest);

/**
 * Read repeatedly from the async stream until the buffer is full, or EOF is reached.
 * Depending on implementation, this could complete at any time.
 * It may complete synchronously. It may complete on another thread.
 * Returns a future, which will contain an error code if something went wrong,
 * or a result bool indicating whether EOF has been reached.
 *
 * WARNING: The buffer must have space available.
 * WARNING: Do not read again until the previous read is complete.
 */
AWS_IO_API
struct aws_future_bool *aws_async_input_stream_read_to_fill(
    struct aws_async_input_stream *stream,
    struct aws_byte_buf *dest);

AWS_EXTERN_C_END
AWS_POP_SANE_WARNING_LEVEL

#endif /* AWS_IO_ASYNC_STREAM_H */