0ec2ac313bd12ed1d6a1d842aa02dc3623d2cdd8
[project/libubox.git] / ustream.h
1 /*
2 * ustream - library for stream buffer management
3 *
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #ifndef __USTREAM_H
20 #define __USTREAM_H
21
22 #include <stdarg.h>
23 #include "uloop.h"
24
25 struct ustream;
26 struct ustream_buf;
27
28 enum read_blocked_reason {
29 READ_BLOCKED_USER = (1 << 0),
30 READ_BLOCKED_FULL = (1 << 1),
31 };
32
33 struct ustream_buf_list {
34 struct ustream_buf *head;
35 struct ustream_buf *data_tail;
36 struct ustream_buf *tail;
37
38 int (*alloc)(struct ustream *s, struct ustream_buf_list *l);
39
40 int data_bytes;
41
42 int min_buffers;
43 int max_buffers;
44 int buffer_len;
45
46 int buffers;
47 };
48
49 struct ustream {
50 struct ustream_buf_list r, w;
51 struct uloop_timeout state_change;
52
53 /*
54 * notify_read:
55 * called by the ustream core to notify that new data is available
56 * for reading.
57 * must not free the ustream from this callback
58 */
59 void (*notify_read)(struct ustream *s, int bytes_new);
60
61 /*
62 * notify_write: (optional)
63 * called by the ustream core to notify that some buffered data has
64 * been written to the stream.
65 * must not free the ustream from this callback
66 */
67 void (*notify_write)(struct ustream *s, int bytes);
68
69 /*
70 * notify_state:
71 * called by the ustream implementation to notify that the read
72 * side of the stream is closed (eof is set) or there was a write
73 * error (write_error is set).
74 * will be called again after the write buffer has been emptied when
75 * the read side has hit EOF.
76 */
77 void (*notify_state)(struct ustream *s);
78
79 /*
80 * write:
81 * must be defined by ustream implementation, accepts new write data.
82 * 'more' is used to indicate that a subsequent call will provide more
83 * data (useful for aggregating writes)
84 * returns the number of bytes accepted, or -1 if no more writes can
85 * be accepted (link error)
86 */
87 int (*write)(struct ustream *s, const char *buf, int len, bool more);
88
89 /*
90 * free: (optional)
91 * defined by ustream implementation, tears down the ustream and frees data
92 */
93 void (*free)(struct ustream *s);
94
95 /*
96 * set_read_blocked: (optional)
97 * defined by ustream implementation, called when the read_blocked flag
98 * changes
99 */
100 void (*set_read_blocked)(struct ustream *s);
101
102 /*
103 * ustream user should set this if the input stream is expected
104 * to contain string data. the core will keep all data 0-terminated.
105 */
106 bool string_data;
107 bool write_error;
108 bool eof, eof_write_done;
109
110 enum read_blocked_reason read_blocked;
111 };
112
113 struct ustream_fd {
114 struct ustream stream;
115 struct uloop_fd fd;
116 };
117
118 struct ustream_buf {
119 struct ustream_buf *next;
120
121 char *data;
122 char *tail;
123 char *end;
124
125 char head[];
126 };
127
128 /* ustream_fd_init: create a file descriptor ustream (uses uloop) */
129 void ustream_fd_init(struct ustream_fd *s, int fd);
130
131 /* ustream_free: free all buffers and data associated with a ustream */
132 void ustream_free(struct ustream *s);
133
134 /* ustream_consume: remove data from the head of the read buffer */
135 void ustream_consume(struct ustream *s, int len);
136
137 /* ustream_write: add data to the write buffer */
138 int ustream_write(struct ustream *s, const char *buf, int len, bool more);
139 int ustream_printf(struct ustream *s, const char *format, ...);
140 int ustream_vprintf(struct ustream *s, const char *format, va_list arg);
141
142 /* ustream_get_read_buf: get a pointer to the next read buffer data */
143 char *ustream_get_read_buf(struct ustream *s, int *buflen);
144
145 /*
146 * ustream_set_read_blocked: set read blocked state
147 *
148 * if set, the ustream will no longer fetch pending data.
149 */
150 void ustream_set_read_blocked(struct ustream *s, bool set);
151
152 static inline bool ustream_read_blocked(struct ustream *s)
153 {
154 return !!(s->read_blocked & READ_BLOCKED_USER);
155 }
156
157 /*** --- functions only used by ustream implementations --- ***/
158
159 /* ustream_init_defaults: fill default callbacks and options */
160 void ustream_init_defaults(struct ustream *s);
161
162 /*
163 * ustream_reserve: allocate rx buffer space
164 *
165 * len: hint for how much space is needed (not guaranteed to be met)
166 * maxlen: pointer to where the actual buffer size is going to be stored
167 */
168 char *ustream_reserve(struct ustream *s, int len, int *maxlen);
169
170 /* ustream_fill_read: mark rx buffer space as filled */
171 void ustream_fill_read(struct ustream *s, int len);
172
173 /*
174 * ustream_write_pending: attempt to write more data from write buffers
175 * returns true if all write buffers have been emptied.
176 */
177 bool ustream_write_pending(struct ustream *s);
178
179 static inline void ustream_state_change(struct ustream *s)
180 {
181 uloop_timeout_set(&s->state_change, 0);
182 }
183
184 #endif