ustream.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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, eof_write_done;
  100. enum read_blocked_reason read_blocked;
  101. };
  102. struct ustream_fd {
  103. struct ustream stream;
  104. struct uloop_fd fd;
  105. };
  106. struct ustream_buf {
  107. struct ustream_buf *next;
  108. char *data;
  109. char *tail;
  110. char *end;
  111. char head[];
  112. };
  113. /* ustream_fd_init: create a file descriptor ustream (uses uloop) */
  114. void ustream_fd_init(struct ustream_fd *s, int fd);
  115. /* ustream_free: free all buffers and data associated with a ustream */
  116. void ustream_free(struct ustream *s);
  117. /* ustream_consume: remove data from the head of the read buffer */
  118. void ustream_consume(struct ustream *s, int len);
  119. /*
  120. * ustream_read: read and consume data in read buffer into caller-specified
  121. * area. Return length of data read.
  122. */
  123. int ustream_read(struct ustream *s, char *buf, int buflen);
  124. /* ustream_write: add data to the write buffer */
  125. int ustream_write(struct ustream *s, const char *buf, int len, bool more);
  126. int ustream_printf(struct ustream *s, const char *format, ...)
  127. __attribute__ ((format (printf, 2, 3)));
  128. int ustream_vprintf(struct ustream *s, const char *format, va_list arg)
  129. __attribute__ ((format (printf, 2, 0)));
  130. /* ustream_get_read_buf: get a pointer to the next read buffer data */
  131. char *ustream_get_read_buf(struct ustream *s, int *buflen);
  132. /*
  133. * ustream_set_read_blocked: set read blocked state
  134. *
  135. * if set, the ustream will no longer fetch pending data.
  136. */
  137. void ustream_set_read_blocked(struct ustream *s, bool set);
  138. static inline bool ustream_read_blocked(struct ustream *s)
  139. {
  140. return !!(s->read_blocked & READ_BLOCKED_USER);
  141. }
  142. static inline int ustream_pending_data(struct ustream *s, bool write)
  143. {
  144. struct ustream_buf_list *b = write ? &s->w : &s->r;
  145. return b->data_bytes;
  146. }
  147. static inline bool ustream_read_buf_full(struct ustream *s)
  148. {
  149. struct ustream_buf *buf = s->r.data_tail;
  150. return buf && buf->data == buf->head && buf->tail == buf->end &&
  151. s->r.buffers == s->r.max_buffers;
  152. }
  153. /*** --- functions only used by ustream implementations --- ***/
  154. /* ustream_init_defaults: fill default callbacks and options */
  155. void ustream_init_defaults(struct ustream *s);
  156. /*
  157. * ustream_reserve: allocate rx buffer space
  158. *
  159. * len: hint for how much space is needed (not guaranteed to be met)
  160. * maxlen: pointer to where the actual buffer size is going to be stored
  161. */
  162. char *ustream_reserve(struct ustream *s, int len, int *maxlen);
  163. /* ustream_fill_read: mark rx buffer space as filled */
  164. void ustream_fill_read(struct ustream *s, int len);
  165. /*
  166. * ustream_write_pending: attempt to write more data from write buffers
  167. * returns true if all write buffers have been emptied.
  168. */
  169. bool ustream_write_pending(struct ustream *s);
  170. static inline void ustream_state_change(struct ustream *s)
  171. {
  172. uloop_timeout_set(&s->state_change, 0);
  173. }
  174. static inline bool ustream_poll(struct ustream *s)
  175. {
  176. if (!s->poll)
  177. return false;
  178. return s->poll(s);
  179. }
  180. #endif