2
0

ustream-fd.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #include <unistd.h>
  19. #include <errno.h>
  20. #include <stdio.h>
  21. #include "ustream.h"
  22. static void ustream_fd_set_uloop(struct ustream *s, bool write)
  23. {
  24. struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
  25. struct ustream_buf *buf;
  26. unsigned int flags = ULOOP_EDGE_TRIGGER | ULOOP_ERROR_CB;
  27. if (!s->read_blocked && !s->eof)
  28. flags |= ULOOP_READ;
  29. buf = s->w.head;
  30. if (write || (buf && s->w.data_bytes && !s->write_error))
  31. flags |= ULOOP_WRITE;
  32. uloop_fd_add(&sf->fd, flags);
  33. }
  34. static void ustream_fd_set_read_blocked(struct ustream *s)
  35. {
  36. ustream_fd_set_uloop(s, false);
  37. }
  38. static void ustream_fd_read_pending(struct ustream_fd *sf, bool *more)
  39. {
  40. struct ustream *s = &sf->stream;
  41. int buflen = 0;
  42. ssize_t len;
  43. char *buf;
  44. do {
  45. if (s->read_blocked)
  46. break;
  47. buf = ustream_reserve(s, 1, &buflen);
  48. if (!buf)
  49. break;
  50. len = read(sf->fd.fd, buf, buflen);
  51. if (len < 0) {
  52. if (errno == EINTR)
  53. continue;
  54. if (errno == EAGAIN || errno == ENOTCONN)
  55. return;
  56. len = 0;
  57. }
  58. if (!len) {
  59. if (!s->eof)
  60. ustream_state_change(s);
  61. s->eof = true;
  62. ustream_fd_set_uloop(s, false);
  63. return;
  64. }
  65. ustream_fill_read(s, len);
  66. *more = true;
  67. } while (1);
  68. }
  69. static int ustream_fd_write(struct ustream *s, const char *buf, int buflen, bool more)
  70. {
  71. struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
  72. ssize_t ret = 0, len;
  73. if (!buflen)
  74. return 0;
  75. while (buflen) {
  76. len = write(sf->fd.fd, buf, buflen);
  77. if (len < 0) {
  78. if (errno == EINTR)
  79. continue;
  80. if (errno == EAGAIN || errno == EWOULDBLOCK || errno == ENOTCONN)
  81. break;
  82. return -1;
  83. }
  84. ret += len;
  85. buf += len;
  86. buflen -= len;
  87. }
  88. if (buflen)
  89. ustream_fd_set_uloop(s, true);
  90. return ret;
  91. }
  92. static bool __ustream_fd_poll(struct ustream_fd *sf, unsigned int events)
  93. {
  94. struct ustream *s = &sf->stream;
  95. bool more = false;
  96. if (events & ULOOP_READ)
  97. ustream_fd_read_pending(sf, &more);
  98. if (events & ULOOP_WRITE) {
  99. bool no_more = ustream_write_pending(s);
  100. if (no_more)
  101. ustream_fd_set_uloop(s, false);
  102. }
  103. if (sf->fd.error && !s->write_error) {
  104. ustream_state_change(s);
  105. s->write_error = true;
  106. ustream_fd_set_uloop(s, false);
  107. }
  108. return more;
  109. }
  110. static bool ustream_fd_poll(struct ustream *s)
  111. {
  112. struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
  113. return __ustream_fd_poll(sf, ULOOP_READ | ULOOP_WRITE);
  114. }
  115. static void ustream_uloop_cb(struct uloop_fd *fd, unsigned int events)
  116. {
  117. struct ustream_fd *sf = container_of(fd, struct ustream_fd, fd);
  118. __ustream_fd_poll(sf, events);
  119. }
  120. static void ustream_fd_free(struct ustream *s)
  121. {
  122. struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
  123. uloop_fd_delete(&sf->fd);
  124. }
  125. void ustream_fd_init(struct ustream_fd *sf, int fd)
  126. {
  127. struct ustream *s = &sf->stream;
  128. ustream_init_defaults(s);
  129. sf->fd.fd = fd;
  130. sf->fd.cb = ustream_uloop_cb;
  131. s->set_read_blocked = ustream_fd_set_read_blocked;
  132. s->write = ustream_fd_write;
  133. s->free = ustream_fd_free;
  134. s->poll = ustream_fd_poll;
  135. ustream_fd_set_uloop(s, false);
  136. }