ustream.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. #ifndef __USTREAM_H
  19. #define __USTREAM_H
  20. #include <stdarg.h>
  21. #include "uloop.h"
  22. struct ustream;
  23. struct ustream_buf;
  24. enum read_blocked_reason {
  25. READ_BLOCKED_USER = (1 << 0),
  26. READ_BLOCKED_FULL = (1 << 1),
  27. };
  28. struct ustream_buf_list {
  29. struct ustream_buf *head;
  30. struct ustream_buf *data_tail;
  31. struct ustream_buf *tail;
  32. int (*alloc)(struct ustream *s, struct ustream_buf_list *l);
  33. int data_bytes;
  34. int min_buffers;
  35. int max_buffers;
  36. int buffer_len;
  37. int buffers;
  38. };
  39. struct ustream {
  40. struct ustream_buf_list r, w;
  41. struct uloop_timeout state_change;
  42. struct ustream *next;
  43. /*
  44. * notify_read: (optional)
  45. * called by the ustream core to notify that new data is available
  46. * for reading.
  47. * must not free the ustream from this callback
  48. */
  49. void (*notify_read)(struct ustream *s, int bytes_new);
  50. /*
  51. * notify_write: (optional)
  52. * called by the ustream core to notify that some buffered data has
  53. * been written to the stream.
  54. * must not free the ustream from this callback
  55. */
  56. void (*notify_write)(struct ustream *s, int bytes);
  57. /*
  58. * notify_state: (optional)
  59. * called by the ustream implementation to notify that the read
  60. * side of the stream is closed (eof is set) or there was a write
  61. * error (write_error is set).
  62. * will be called again after the write buffer has been emptied when
  63. * the read side has hit EOF.
  64. */
  65. void (*notify_state)(struct ustream *s);
  66. /*
  67. * write:
  68. * must be defined by ustream implementation, accepts new write data.
  69. * 'more' is used to indicate that a subsequent call will provide more
  70. * data (useful for aggregating writes)
  71. * returns the number of bytes accepted, or -1 if no more writes can
  72. * be accepted (link error)
  73. */
  74. int (*write)(struct ustream *s, const char *buf, int len, bool more);
  75. /*
  76. * free: (optional)
  77. * defined by ustream implementation, tears down the ustream and frees data
  78. */
  79. void (*free)(struct ustream *s);
  80. /*
  81. * set_read_blocked: (optional)
  82. * defined by ustream implementation, called when the read_blocked flag
  83. * changes
  84. */
  85. void (*set_read_blocked)(struct ustream *s);
  86. /*
  87. * poll: (optional)
  88. * defined by the upstream implementation, called to request polling for
  89. * available data.
  90. * returns true if data was fetched.
  91. */
  92. bool (*poll)(struct ustream *s);
  93. /*
  94. * ustream user should set this if the input stream is expected
  95. * to contain string data. the core will keep all data 0-terminated.
  96. */
  97. bool string_data;
  98. bool write_error;
  99. bool eof;
  100. uint8_t pending_cb;
  101. enum read_blocked_reason read_blocked;
  102. };
  103. struct ustream_fd {
  104. struct ustream stream;
  105. struct uloop_fd fd;
  106. };
  107. struct ustream_buf {
  108. struct ustream_buf *next;
  109. char *data;
  110. char *tail;
  111. char *end;
  112. char head[];
  113. };
  114. /* ustream_fd_init: create a file descriptor ustream (uses uloop) */
  115. void ustream_fd_init(struct ustream_fd *s, int fd);
  116. /* ustream_free: free all buffers and data associated with a ustream */
  117. void ustream_free(struct ustream *s);
  118. /* ustream_consume: remove data from the head of the read buffer */
  119. void ustream_consume(struct ustream *s, int len);
  120. /*
  121. * ustream_read: read and consume data in read buffer into caller-specified
  122. * area. Return length of data read.
  123. */
  124. int ustream_read(struct ustream *s, char *buf, int buflen);
  125. /* ustream_write: add data to the write buffer */
  126. int ustream_write(struct ustream *s, const char *buf, int len, bool more);
  127. int ustream_printf(struct ustream *s, const char *format, ...)
  128. __attribute__ ((format (printf, 2, 3)));
  129. int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
  130. __attribute__ ((format (printf, 2, 0)));
  131. /* ustream_get_read_buf: get a pointer to the next read buffer data */
  132. char *ustream_get_read_buf(struct ustream *s, int *buflen);
  133. /*
  134. * ustream_set_read_blocked: set read blocked state
  135. *
  136. * if set, the ustream will no longer fetch pending data.
  137. */
  138. void ustream_set_read_blocked(struct ustream *s, bool set);
  139. static inline bool ustream_read_blocked(struct ustream *s)
  140. {
  141. return !!(s->read_blocked & READ_BLOCKED_USER);
  142. }
  143. static inline int ustream_pending_data(struct ustream *s, bool write)
  144. {
  145. struct ustream_buf_list *b = write ? &s->w : &s->r;
  146. return b->data_bytes;
  147. }
  148. static inline bool ustream_read_buf_full(struct ustream *s)
  149. {
  150. struct ustream_buf *buf = s->r.data_tail;
  151. return buf && buf->data == buf->head && buf->tail == buf->end &&
  152. s->r.buffers == s->r.max_buffers;
  153. }
  154. /*** --- functions only used by ustream implementations --- ***/
  155. /* ustream_init_defaults: fill default callbacks and options */
  156. void ustream_init_defaults(struct ustream *s);
  157. /*
  158. * ustream_reserve: allocate rx buffer space
  159. *
  160. * len: hint for how much space is needed (not guaranteed to be met)
  161. * maxlen: pointer to where the actual buffer size is going to be stored
  162. */
  163. char *ustream_reserve(struct ustream *s, int len, int *maxlen);
  164. /* ustream_fill_read: mark rx buffer space as filled */
  165. void ustream_fill_read(struct ustream *s, int len);
  166. /*
  167. * ustream_write_pending: attempt to write more data from write buffers
  168. * returns true if all write buffers have been emptied.
  169. */
  170. bool ustream_write_pending(struct ustream *s);
  171. static inline void ustream_state_change(struct ustream *s)
  172. {
  173. uloop_timeout_set(&s->state_change, 0);
  174. }
  175. static inline bool ustream_poll(struct ustream *s)
  176. {
  177. if (!s->poll)
  178. return false;
  179. return s->poll(s);
  180. }
  181. #endif