2 * ustream - library for stream buffer management
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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.
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.
28 enum read_blocked_reason {
29 READ_BLOCKED_USER = (1 << 0),
30 READ_BLOCKED_FULL = (1 << 1),
33 struct ustream_buf_list {
34 struct ustream_buf *head;
35 struct ustream_buf *data_tail;
36 struct ustream_buf *tail;
38 int (*alloc)(struct ustream *s, struct ustream_buf_list *l);
50 struct ustream_buf_list r, w;
51 struct uloop_timeout state_change;
55 * notify_read: (optional)
56 * called by the ustream core to notify that new data is available
58 * must not free the ustream from this callback
60 void (*notify_read)(struct ustream *s, int bytes_new);
63 * notify_write: (optional)
64 * called by the ustream core to notify that some buffered data has
65 * been written to the stream.
66 * must not free the ustream from this callback
68 void (*notify_write)(struct ustream *s, int bytes);
71 * notify_state: (optional)
72 * called by the ustream implementation to notify that the read
73 * side of the stream is closed (eof is set) or there was a write
74 * error (write_error is set).
75 * will be called again after the write buffer has been emptied when
76 * the read side has hit EOF.
78 void (*notify_state)(struct ustream *s);
82 * must be defined by ustream implementation, accepts new write data.
83 * 'more' is used to indicate that a subsequent call will provide more
84 * data (useful for aggregating writes)
85 * returns the number of bytes accepted, or -1 if no more writes can
86 * be accepted (link error)
88 int (*write)(struct ustream *s, const char *buf, int len, bool more);
92 * defined by ustream implementation, tears down the ustream and frees data
94 void (*free)(struct ustream *s);
97 * set_read_blocked: (optional)
98 * defined by ustream implementation, called when the read_blocked flag
101 void (*set_read_blocked)(struct ustream *s);
105 * defined by the upstream implementation, called to request polling for
107 * returns true if data was fetched.
109 bool (*poll)(struct ustream *s);
112 * ustream user should set this if the input stream is expected
113 * to contain string data. the core will keep all data 0-terminated.
117 bool eof, eof_write_done;
119 enum read_blocked_reason read_blocked;
123 struct ustream stream;
128 struct ustream_buf *next;
137 /* ustream_fd_init: create a file descriptor ustream (uses uloop) */
138 void ustream_fd_init(struct ustream_fd *s, int fd);
140 /* ustream_free: free all buffers and data associated with a ustream */
141 void ustream_free(struct ustream *s);
143 /* ustream_consume: remove data from the head of the read buffer */
144 void ustream_consume(struct ustream *s, int len);
146 /* ustream_write: add data to the write buffer */
147 int ustream_write(struct ustream *s, const char *buf, int len, bool more);
148 int ustream_printf(struct ustream *s, const char *format, ...);
149 int ustream_vprintf(struct ustream *s, const char *format, va_list arg);
151 /* ustream_get_read_buf: get a pointer to the next read buffer data */
152 char *ustream_get_read_buf(struct ustream *s, int *buflen);
155 * ustream_set_read_blocked: set read blocked state
157 * if set, the ustream will no longer fetch pending data.
159 void ustream_set_read_blocked(struct ustream *s, bool set);
161 static inline bool ustream_read_blocked(struct ustream *s)
163 return !!(s->read_blocked & READ_BLOCKED_USER);
166 static inline int ustream_pending_data(struct ustream *s, bool write)
168 struct ustream_buf_list *b = write ? &s->w : &s->r;
169 return b->data_bytes;
172 static inline bool ustream_read_buf_full(struct ustream *s)
174 struct ustream_buf *buf = s->r.data_tail;
175 return buf && buf->data == buf->head && buf->tail == buf->end &&
176 s->r.buffers == s->r.max_buffers;
179 /*** --- functions only used by ustream implementations --- ***/
181 /* ustream_init_defaults: fill default callbacks and options */
182 void ustream_init_defaults(struct ustream *s);
185 * ustream_reserve: allocate rx buffer space
187 * len: hint for how much space is needed (not guaranteed to be met)
188 * maxlen: pointer to where the actual buffer size is going to be stored
190 char *ustream_reserve(struct ustream *s, int len, int *maxlen);
192 /* ustream_fill_read: mark rx buffer space as filled */
193 void ustream_fill_read(struct ustream *s, int len);
196 * ustream_write_pending: attempt to write more data from write buffers
197 * returns true if all write buffers have been emptied.
199 bool ustream_write_pending(struct ustream *s);
201 static inline void ustream_state_change(struct ustream *s)
203 uloop_timeout_set(&s->state_change, 0);
206 static inline bool ustream_poll(struct ustream *s)