libubox
C utility functions for OpenWrt.
ustream.h File Reference
#include <stdarg.h>
#include "uloop.h"

Go to the source code of this file.

Data Structures

struct  ustream_buf_list
 
struct  ustream
 
struct  ustream_fd
 
struct  ustream_buf
 

Enumerations

enum  read_blocked_reason { READ_BLOCKED_USER = (1 << 0) , READ_BLOCKED_FULL = (1 << 1) }
 

Functions

void ustream_fd_init (struct ustream_fd *s, int fd)
 
void ustream_free (struct ustream *s)
 
void ustream_consume (struct ustream *s, int len)
 
int ustream_read (struct ustream *s, char *buf, int buflen)
 
int ustream_write (struct ustream *s, const char *buf, int len, bool more)
 
int ustream_printf (struct ustream *s, const char *format,...) __attribute__((format(printf
 
int int ustream_vprintf (struct ustream *s, const char *format, va_list arg) __attribute__((format(printf
 
int int char * ustream_get_read_buf (struct ustream *s, int *buflen)
 
void ustream_set_read_blocked (struct ustream *s, bool set)
 
static bool ustream_read_blocked (struct ustream *s)
 
static int ustream_pending_data (struct ustream *s, bool write)
 
static bool ustream_read_buf_full (struct ustream *s)
 
void ustream_init_defaults (struct ustream *s)
 
char * ustream_reserve (struct ustream *s, int len, int *maxlen)
 
void ustream_fill_read (struct ustream *s, int len)
 
bool ustream_write_pending (struct ustream *s)
 
static void ustream_state_change (struct ustream *s)
 
static bool ustream_poll (struct ustream *s)
 

Enumeration Type Documentation

◆ read_blocked_reason

Enumerator
READ_BLOCKED_USER 
READ_BLOCKED_FULL 

Definition at line 28 of file ustream.h.

28  {
29  READ_BLOCKED_USER = (1 << 0),
30  READ_BLOCKED_FULL = (1 << 1),
31 };
@ READ_BLOCKED_FULL
Definition: ustream.h:30
@ READ_BLOCKED_USER
Definition: ustream.h:29

Function Documentation

◆ ustream_consume()

void ustream_consume ( struct ustream s,
int  len 
)

Definition at line 214 of file ustream.c.

215 {
216  struct ustream_buf *buf = s->r.head;
217 
218  if (!len)
219  return;
220 
221  s->r.data_bytes -= len;
222  if (s->r.data_bytes < 0)
223  abort();
224 
225  do {
226  struct ustream_buf *next = buf->next;
227  int buf_len = buf->tail - buf->data;
228 
229  if (len < buf_len) {
230  buf->data += len;
231  break;
232  }
233 
234  len -= buf_len;
235  ustream_free_buf(&s->r, buf);
236  buf = next;
237  } while(len);
238 
240 }
struct ustream_buf * next
Definition: ustream.h:128
char * data
Definition: ustream.h:130
char * tail
Definition: ustream.h:131
enum read_blocked_reason read_blocked
Definition: ustream.h:119
static void ustream_free_buf(struct ustream_buf_list *l, struct ustream_buf *buf)
Definition: ustream.c:174
static void __ustream_set_read_blocked(struct ustream *s, unsigned char val)
Definition: ustream.c:195
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ustream_fd_init()

void ustream_fd_init ( struct ustream_fd s,
int  fd 
)

Definition at line 160 of file ustream-fd.c.

161 {
162  struct ustream *s = &sf->stream;
163 
165 
166  sf->fd.fd = fd;
167  sf->fd.cb = ustream_uloop_cb;
169  s->write = ustream_fd_write;
170  s->free = ustream_fd_free;
171  s->poll = ustream_fd_poll;
172  ustream_fd_set_uloop(s, false);
173 }
struct ustream stream
Definition: ustream.h:123
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(* free)(struct ustream *s)
Definition: ustream.h:94
bool(* poll)(struct ustream *s)
Definition: ustream.h:109
int fd
Definition: udebug-priv.h:27
static void ustream_fd_set_uloop(struct ustream *s, bool write)
Definition: ustream-fd.c:24
static int ustream_fd_write(struct ustream *s, const char *buf, int buflen, bool more)
Definition: ustream-fd.c:84
static bool ustream_fd_poll(struct ustream *s)
Definition: ustream-fd.c:139
static void ustream_fd_free(struct ustream *s)
Definition: ustream-fd.c:153
static void ustream_fd_set_read_blocked(struct ustream *s)
Definition: ustream-fd.c:40
static void ustream_uloop_cb(struct uloop_fd *fd, unsigned int events)
Definition: ustream-fd.c:146
void ustream_init_defaults(struct ustream *s)
Definition: ustream.c:112
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ustream_fill_read()

void ustream_fill_read ( struct ustream s,
int  len 
)

Definition at line 301 of file ustream.c.

302 {
303  struct ustream_buf *buf = s->r.data_tail;
304  int n = len;
305  int maxlen;
306 
307  s->r.data_bytes += len;
308  do {
309  if (!buf)
310  abort();
311 
312  maxlen = buf->end - buf->tail;
313  if (len < maxlen)
314  maxlen = len;
315 
316  len -= maxlen;
317  buf->tail += maxlen;
318  ustream_fixup_string(s, buf);
319 
320  s->r.data_tail = buf;
321  buf = buf->next;
322  } while (len);
323 
324  if (s->notify_read)
325  s->notify_read(s, n);
326 }
char * end
Definition: ustream.h:132
void(* notify_read)(struct ustream *s, int bytes_new)
Definition: ustream.h:60
static void ustream_fixup_string(struct ustream *s, struct ustream_buf *buf)
Definition: ustream.c:242
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ustream_free()

void ustream_free ( struct ustream s)

Definition at line 92 of file ustream.c.

93 {
94  if (s->free)
95  s->free(s);
96 
98  ustream_free_buffers(&s->r);
100 }
struct ustream_buf_list r w
Definition: ustream.h:50
struct uloop_timeout state_change
Definition: ustream.h:51
int uloop_timeout_cancel(struct uloop_timeout *timeout)
Definition: uloop.c:348
static void ustream_free_buffers(struct ustream_buf_list *l)
Definition: ustream.c:77
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ustream_get_read_buf()

int int char* ustream_get_read_buf ( struct ustream s,
int *  buflen 
)

Definition at line 328 of file ustream.c.

329 {
330  char *data = NULL;
331  int len = 0;
332 
333  if (s->r.head) {
334  len = s->r.head->tail - s->r.head->data;
335  if (len > 0)
336  data = s->r.head->data;
337  }
338 
339  if (buflen)
340  *buflen = len;
341 
342  return data;
343 }
char data[]
Definition: blob.h:1
Here is the caller graph for this function:

◆ ustream_init_defaults()

void ustream_init_defaults ( struct ustream s)

Definition at line 112 of file ustream.c.

113 {
114 #define DEFAULT_SET(_f, _default) \
115  do { \
116  if (!_f) \
117  _f = _default; \
118  } while(0)
119 
120  DEFAULT_SET(s->r.alloc, ustream_alloc_default);
122 
123  DEFAULT_SET(s->r.min_buffers, 1);
124  DEFAULT_SET(s->r.max_buffers, 1);
125  DEFAULT_SET(s->r.buffer_len, 4096);
126 
127  DEFAULT_SET(s->w.min_buffers, 2);
128  DEFAULT_SET(s->w.max_buffers, -1);
129  DEFAULT_SET(s->w.buffer_len, 256);
130 
131 #undef DEFAULT_SET
132 
134  s->write_error = false;
135  s->eof = false;
136  s->eof_write_done = false;
137  s->read_blocked = 0;
138 
139  s->r.buffers = 0;
140  s->r.data_bytes = 0;
141 
142  s->w.buffers = 0;
143  s->w.data_bytes = 0;
144 }
uloop_timeout_handler cb
Definition: uloop.h:77
int(* alloc)(struct ustream *s, struct ustream_buf_list *l)
Definition: ustream.h:38
bool eof
Definition: ustream.h:117
bool eof_write_done
Definition: ustream.h:117
bool write_error
Definition: ustream.h:116
static int ustream_alloc_default(struct ustream *s, struct ustream_buf_list *l)
Definition: ustream.c:60
#define DEFAULT_SET(_f, _default)
static void ustream_state_change_cb(struct uloop_timeout *t)
Definition: ustream.c:102
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ustream_pending_data()

static int ustream_pending_data ( struct ustream s,
bool  write 
)
inlinestatic

Definition at line 173 of file ustream.h.

174 {
175  struct ustream_buf_list *b = write ? &s->w : &s->r;
176  return b->data_bytes;
177 }
static struct blob_buf b
Definition: jshn.c:40

◆ ustream_poll()

static bool ustream_poll ( struct ustream s)
inlinestatic

Definition at line 213 of file ustream.h.

214 {
215  if (!s->poll)
216  return false;
217 
218  return s->poll(s);
219 }

◆ ustream_printf()

int ustream_printf ( struct ustream s,
const char *  format,
  ... 
)

◆ ustream_read()

int ustream_read ( struct ustream s,
char *  buf,
int  buflen 
)

Definition at line 345 of file ustream.c.

346 {
347  char *chunk;
348  int chunk_len;
349  int len = 0;
350 
351  do {
352  chunk = ustream_get_read_buf(s, &chunk_len);
353  if (!chunk)
354  break;
355  if (chunk_len > buflen - len)
356  chunk_len = buflen - len;
357  memcpy(buf + len, chunk, chunk_len);
358  ustream_consume(s, chunk_len);
359  len += chunk_len;
360  } while (len < buflen);
361 
362  return len;
363 }
void ustream_consume(struct ustream *s, int len)
Definition: ustream.c:214
char * ustream_get_read_buf(struct ustream *s, int *buflen)
Definition: ustream.c:328
Here is the call graph for this function:

◆ ustream_read_blocked()

static bool ustream_read_blocked ( struct ustream s)
inlinestatic

Definition at line 168 of file ustream.h.

169 {
170  return !!(s->read_blocked & READ_BLOCKED_USER);
171 }
Here is the caller graph for this function:

◆ ustream_read_buf_full()

static bool ustream_read_buf_full ( struct ustream s)
inlinestatic

Definition at line 179 of file ustream.h.

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 }
char head[]
Definition: ustream.h:134

◆ ustream_reserve()

char* ustream_reserve ( struct ustream s,
int  len,
int *  maxlen 
)

Definition at line 286 of file ustream.c.

287 {
288  struct ustream_buf *buf;
289 
290  if (!ustream_prepare_buf(s, &s->r, len)) {
292  *maxlen = 0;
293  return NULL;
294  }
295 
296  buf = s->r.data_tail;
297  *maxlen = buf->end - buf->tail;
298  return buf->tail;
299 }
static bool ustream_prepare_buf(struct ustream *s, struct ustream_buf_list *l, int len)
Definition: ustream.c:250
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ustream_set_read_blocked()

void ustream_set_read_blocked ( struct ustream s,
bool  set 
)

Definition at line 204 of file ustream.c.

205 {
206  unsigned char val = s->read_blocked & ~READ_BLOCKED_USER;
207 
208  if (set)
209  val |= READ_BLOCKED_USER;
210 
212 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ustream_state_change()

static void ustream_state_change ( struct ustream s)
inlinestatic

Definition at line 208 of file ustream.h.

209 {
211 }
int uloop_timeout_set(struct uloop_timeout *timeout, int msecs)
Definition: uloop.c:328
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ustream_vprintf()

int int ustream_vprintf ( struct ustream s,
const char *  format,
va_list  arg 
)

◆ ustream_write()

int ustream_write ( struct ustream s,
const char *  buf,
int  len,
bool  more 
)

Definition at line 440 of file ustream.c.

441 {
442  struct ustream_buf_list *l = &s->w;
443  int wr = 0;
444 
445  if (s->write_error)
446  return 0;
447 
448  if (!l->data_bytes) {
449  wr = s->write(s, data, len, more);
450  if (wr == len)
451  return wr;
452 
453  if (wr < 0) {
455  return wr;
456  }
457 
458  data += wr;
459  len -= wr;
460  }
461 
462  return ustream_write_buffered(s, data, len, wr);
463 }
static void ustream_write_error(struct ustream *s)
Definition: ustream.c:365
static int ustream_write_buffered(struct ustream *s, const char *data, int len, int wr)
Definition: ustream.c:413
Here is the call graph for this function:
Here is the caller graph for this function:

◆ ustream_write_pending()

bool ustream_write_pending ( struct ustream s)

Definition at line 372 of file ustream.c.

373 {
374  struct ustream_buf *buf = s->w.head;
375  int wr = 0, len;
376 
377  if (s->write_error)
378  return false;
379 
380  while (buf && s->w.data_bytes) {
381  struct ustream_buf *next = buf->next;
382  int maxlen = buf->tail - buf->data;
383 
384  len = s->write(s, buf->data, maxlen, !!buf->next);
385  if (len < 0) {
387  break;
388  }
389 
390  if (len == 0)
391  break;
392 
393  wr += len;
394  s->w.data_bytes -= len;
395  if (len < maxlen) {
396  buf->data += len;
397  break;
398  }
399 
400  ustream_free_buf(&s->w, buf);
401  buf = next;
402  }
403 
404  if (s->notify_write)
405  s->notify_write(s, wr);
406 
407  if (s->eof && wr && !s->w.data_bytes)
409 
410  return !s->w.data_bytes;
411 }
struct ustream_buf * head
Definition: ustream.h:34
void(* notify_write)(struct ustream *s, int bytes)
Definition: ustream.h:68
static void ustream_state_change(struct ustream *s)
Definition: ustream.h:208
Here is the call graph for this function:
Here is the caller graph for this function: