2 * ustream - library for stream buffer management
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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.
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.
24 static void ustream_fd_set_uloop(struct ustream *s, bool write)
26 struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
27 struct ustream_buf *buf;
28 unsigned int flags = ULOOP_EDGE_TRIGGER | ULOOP_ERROR_CB;
30 if (!s->read_blocked && !s->eof)
34 if (write || (buf && s->w.data_bytes && !s->write_error))
37 uloop_fd_add(&sf->fd, flags);
40 static void ustream_fd_set_read_blocked(struct ustream *s)
42 ustream_fd_set_uloop(s, false);
45 static void ustream_fd_read_pending(struct ustream_fd *sf, bool *more)
47 struct ustream *s = &sf->stream;
56 buf = ustream_reserve(s, 1, &buflen);
60 len = read(sf->fd.fd, buf, buflen);
65 if (errno == EAGAIN || errno == ENOTCONN)
73 ustream_state_change(s);
75 ustream_fd_set_uloop(s, false);
79 ustream_fill_read(s, len);
84 static int ustream_fd_write(struct ustream *s, const char *buf, int buflen, bool more)
86 struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
93 len = write(sf->fd.fd, buf, buflen);
99 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == ENOTCONN)
111 ustream_fd_set_uloop(s, true);
116 static bool __ustream_fd_poll(struct ustream_fd *sf, unsigned int events)
118 struct ustream *s = &sf->stream;
121 if (events & ULOOP_READ)
122 ustream_fd_read_pending(sf, &more);
124 if (events & ULOOP_WRITE) {
125 bool no_more = ustream_write_pending(s);
127 ustream_fd_set_uloop(s, false);
130 if (sf->fd.error && !s->write_error) {
131 ustream_state_change(s);
132 s->write_error = true;
133 ustream_fd_set_uloop(s, false);
139 static bool ustream_fd_poll(struct ustream *s)
141 struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
143 return __ustream_fd_poll(sf, ULOOP_READ | ULOOP_WRITE);
146 static void ustream_uloop_cb(struct uloop_fd *fd, unsigned int events)
148 struct ustream_fd *sf = container_of(fd, struct ustream_fd, fd);
150 __ustream_fd_poll(sf, events);
153 static void ustream_fd_free(struct ustream *s)
155 struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
157 uloop_fd_delete(&sf->fd);
160 void ustream_fd_init(struct ustream_fd *sf, int fd)
162 struct ustream *s = &sf->stream;
164 ustream_init_defaults(s);
167 sf->fd.cb = ustream_uloop_cb;
168 s->set_read_blocked = ustream_fd_set_read_blocked;
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);