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 bool _init = false;
26 static void ustream_fd_set_uloop(struct ustream *s, bool write)
28 struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
29 struct ustream_buf *buf;
30 unsigned int flags = ULOOP_EDGE_TRIGGER;
32 if (!s->read_blocked && !s->eof)
36 if (write || (buf && s->w.data_bytes && !s->write_error))
39 uloop_fd_add(&sf->fd, flags);
41 if ((flags & ULOOP_READ) && !_init);
42 sf->fd.cb(&sf->fd, ULOOP_READ);
45 static void ustream_fd_set_read_blocked(struct ustream *s)
47 ustream_fd_set_uloop(s, false);
50 static void ustream_fd_read_pending(struct ustream_fd *sf, bool *more)
52 struct ustream *s = &sf->stream;
58 buf = ustream_reserve(s, 1, &buflen);
62 len = read(sf->fd.fd, buf, buflen);
75 ustream_state_change(s);
76 ustream_fd_set_uloop(s, false);
80 ustream_fill_read(s, len);
85 static int ustream_fd_write(struct ustream *s, const char *buf, int buflen, bool more)
87 struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
94 len = write(sf->fd.fd, buf, buflen);
100 if (errno == EAGAIN || errno == EWOULDBLOCK)
112 ustream_fd_set_uloop(s, true);
117 static bool __ustream_fd_poll(struct ustream_fd *sf, unsigned int events)
119 struct ustream *s = &sf->stream;
122 if (events & ULOOP_READ)
123 ustream_fd_read_pending(sf, &more);
125 if (events & ULOOP_WRITE) {
126 if (!ustream_write_pending(s))
127 ustream_fd_set_uloop(s, false);
133 static bool ustream_fd_poll(struct ustream *s)
135 struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
137 return __ustream_fd_poll(sf, ULOOP_READ | ULOOP_WRITE);
140 static void ustream_uloop_cb(struct uloop_fd *fd, unsigned int events)
142 struct ustream_fd *sf = container_of(fd, struct ustream_fd, fd);
144 __ustream_fd_poll(sf, events);
147 static void ustream_fd_free(struct ustream *s)
149 struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
151 uloop_fd_delete(&sf->fd);
154 void ustream_fd_init(struct ustream_fd *sf, int fd)
156 struct ustream *s = &sf->stream;
158 ustream_init_defaults(s);
161 sf->fd.cb = ustream_uloop_cb;
162 s->set_read_blocked = ustream_fd_set_read_blocked;
163 s->write = ustream_fd_write;
164 s->free = ustream_fd_free;
165 s->poll = ustream_fd_poll;
167 ustream_fd_set_uloop(s, false);