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