libubox
C utility functions for OpenWrt.
ustream.h
Go to the documentation of this file.
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 
29  READ_BLOCKED_USER = (1 << 0),
30  READ_BLOCKED_FULL = (1 << 1),
31 };
32 
34  struct ustream_buf *head;
36  struct ustream_buf *tail;
37 
38  int (*alloc)(struct ustream *s, struct ustream_buf_list *l);
39 
41 
45 
46  int buffers;
47 };
48 
49 struct ustream {
50  struct ustream_buf_list r, w;
52  struct ustream *next;
53 
54  /*
55  * notify_read: (optional)
56  * called by the ustream core to notify that new data is available
57  * for reading.
58  * must not free the ustream from this callback
59  */
60  void (*notify_read)(struct ustream *s, int bytes_new);
61 
62  /*
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
67  */
68  void (*notify_write)(struct ustream *s, int bytes);
69 
70  /*
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.
77  */
78  void (*notify_state)(struct ustream *s);
79 
80  /*
81  * write:
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)
87  */
88  int (*write)(struct ustream *s, const char *buf, int len, bool more);
89 
90  /*
91  * free: (optional)
92  * defined by ustream implementation, tears down the ustream and frees data
93  */
94  void (*free)(struct ustream *s);
95 
96  /*
97  * set_read_blocked: (optional)
98  * defined by ustream implementation, called when the read_blocked flag
99  * changes
100  */
101  void (*set_read_blocked)(struct ustream *s);
102 
103  /*
104  * poll: (optional)
105  * defined by the upstream implementation, called to request polling for
106  * available data.
107  * returns true if data was fetched.
108  */
109  bool (*poll)(struct ustream *s);
110 
111  /*
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.
114  */
118 
120 };
121 
122 struct ustream_fd {
123  struct ustream stream;
124  struct uloop_fd fd;
125 };
126 
127 struct ustream_buf {
128  struct ustream_buf *next;
129 
130  char *data;
131  char *tail;
132  char *end;
133 
134  char head[];
135 };
136 
137 /* ustream_fd_init: create a file descriptor ustream (uses uloop) */
138 void ustream_fd_init(struct ustream_fd *s, int fd);
139 
140 /* ustream_free: free all buffers and data associated with a ustream */
141 void ustream_free(struct ustream *s);
142 
143 /* ustream_consume: remove data from the head of the read buffer */
144 void ustream_consume(struct ustream *s, int len);
145 
146 /*
147  * ustream_read: read and consume data in read buffer into caller-specified
148  * area. Return length of data read.
149  */
150 int ustream_read(struct ustream *s, char *buf, int buflen);
151 /* ustream_write: add data to the write buffer */
152 int ustream_write(struct ustream *s, const char *buf, int len, bool more);
153 int ustream_printf(struct ustream *s, const char *format, ...)
154  __attribute__ ((format (printf, 2, 3)));
155 int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
156  __attribute__ ((format (printf, 2, 0)));
157 
158 /* ustream_get_read_buf: get a pointer to the next read buffer data */
159 char *ustream_get_read_buf(struct ustream *s, int *buflen);
160 
161 /*
162  * ustream_set_read_blocked: set read blocked state
163  *
164  * if set, the ustream will no longer fetch pending data.
165  */
166 void ustream_set_read_blocked(struct ustream *s, bool set);
167 
168 static inline bool ustream_read_blocked(struct ustream *s)
169 {
170  return !!(s->read_blocked & READ_BLOCKED_USER);
171 }
172 
173 static inline int ustream_pending_data(struct ustream *s, bool write)
174 {
175  struct ustream_buf_list *b = write ? &s->w : &s->r;
176  return b->data_bytes;
177 }
178 
179 static inline bool ustream_read_buf_full(struct ustream *s)
180 {
181  struct ustream_buf *buf = s->r.data_tail;
182  return buf && buf->data == buf->head && buf->tail == buf->end &&
183  s->r.buffers == s->r.max_buffers;
184 }
185 
186 /*** --- functions only used by ustream implementations --- ***/
187 
188 /* ustream_init_defaults: fill default callbacks and options */
189 void ustream_init_defaults(struct ustream *s);
190 
191 /*
192  * ustream_reserve: allocate rx buffer space
193  *
194  * len: hint for how much space is needed (not guaranteed to be met)
195  * maxlen: pointer to where the actual buffer size is going to be stored
196  */
197 char *ustream_reserve(struct ustream *s, int len, int *maxlen);
198 
199 /* ustream_fill_read: mark rx buffer space as filled */
200 void ustream_fill_read(struct ustream *s, int len);
201 
202 /*
203  * ustream_write_pending: attempt to write more data from write buffers
204  * returns true if all write buffers have been emptied.
205  */
206 bool ustream_write_pending(struct ustream *s);
207 
208 static inline void ustream_state_change(struct ustream *s)
209 {
211 }
212 
213 static inline bool ustream_poll(struct ustream *s)
214 {
215  if (!s->poll)
216  return false;
217 
218  return s->poll(s);
219 }
220 
221 #endif
struct blob_attr_info __attribute__
Definition: ulog.c:107
static struct blob_buf b
Definition: jshn.c:40
Definition: uloop.h:63
int(* alloc)(struct ustream *s, struct ustream_buf_list *l)
Definition: ustream.h:38
struct ustream_buf * tail
Definition: ustream.h:36
struct ustream_buf * head
Definition: ustream.h:34
struct ustream_buf * data_tail
Definition: ustream.h:35
struct ustream_buf * next
Definition: ustream.h:128
char * data
Definition: ustream.h:130
char * tail
Definition: ustream.h:131
char head[]
Definition: ustream.h:134
char * end
Definition: ustream.h:132
struct uloop_fd fd
Definition: ustream.h:124
struct ustream stream
Definition: ustream.h:123
struct ustream_buf_list r w
Definition: ustream.h:50
bool eof
Definition: ustream.h:117
void(* notify_write)(struct ustream *s, int bytes)
Definition: ustream.h:68
int(* write)(struct ustream *s, const char *buf, int len, bool more)
Definition: ustream.h:88
void(* set_read_blocked)(struct ustream *s)
Definition: ustream.h:101
void(* notify_read)(struct ustream *s, int bytes_new)
Definition: ustream.h:60
bool eof_write_done
Definition: ustream.h:117
void(* free)(struct ustream *s)
Definition: ustream.h:94
struct ustream * next
Definition: ustream.h:52
void(* notify_state)(struct ustream *s)
Definition: ustream.h:78
struct uloop_timeout state_change
Definition: ustream.h:51
bool(* poll)(struct ustream *s)
Definition: ustream.h:109
bool write_error
Definition: ustream.h:116
enum read_blocked_reason read_blocked
Definition: ustream.h:119
bool string_data
Definition: ustream.h:115
int fd
Definition: udebug-priv.h:27
int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
Definition: uloop.c:328
static bool ustream_read_blocked(struct ustream *s)
Definition: ustream.h:168
int int char * ustream_get_read_buf(struct ustream *s, int *buflen)
Definition: ustream.c:328
void ustream_fd_init(struct ustream_fd *s, int fd)
Definition: ustream-fd.c:160
static bool ustream_read_buf_full(struct ustream *s)
Definition: ustream.h:179
int ustream_printf(struct ustream *s, const char *format,...) __attribute__((format(printf
read_blocked_reason
Definition: ustream.h:28
@ READ_BLOCKED_FULL
Definition: ustream.h:30
@ READ_BLOCKED_USER
Definition: ustream.h:29
void ustream_init_defaults(struct ustream *s)
Definition: ustream.c:112
void ustream_free(struct ustream *s)
Definition: ustream.c:92
void ustream_consume(struct ustream *s, int len)
Definition: ustream.c:214
bool ustream_write_pending(struct ustream *s)
Definition: ustream.c:372
void ustream_fill_read(struct ustream *s, int len)
Definition: ustream.c:301
static bool ustream_poll(struct ustream *s)
Definition: ustream.h:213
char * ustream_reserve(struct ustream *s, int len, int *maxlen)
Definition: ustream.c:286
int int ustream_vprintf(struct ustream *s, const char *format, va_list arg) __attribute__((format(printf
void ustream_set_read_blocked(struct ustream *s, bool set)
Definition: ustream.c:204
static int ustream_pending_data(struct ustream *s, bool write)
Definition: ustream.h:173
static void ustream_state_change(struct ustream *s)
Definition: ustream.h:208
int ustream_read(struct ustream *s, char *buf, int buflen)
Definition: ustream.c:345
int ustream_write(struct ustream *s, const char *buf, int len, bool more)
Definition: ustream.c:440