examples: fix linking with json-c
[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, bool write)
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 (write || (buf && s->w.data_bytes && !s->write_error))
35                 flags |= ULOOP_WRITE;
36
37         uloop_fd_add(&sf->fd, flags);
38 }
39
40 static void ustream_fd_set_read_blocked(struct ustream *s)
41 {
42         ustream_fd_set_uloop(s, false);
43 }
44
45 static void ustream_fd_read_pending(struct ustream_fd *sf, bool *more)
46 {
47         struct ustream *s = &sf->stream;
48         int buflen = 0;
49         ssize_t len;
50         char *buf;
51
52         do {
53                 buf = ustream_reserve(s, 1, &buflen);
54                 if (!buf)
55                         break;
56
57                 len = read(sf->fd.fd, buf, buflen);
58                 if (len < 0) {
59                         if (errno == EINTR)
60                                 continue;
61
62                         if (errno == EAGAIN)
63                                 return;
64
65                         len = 0;
66                 }
67
68                 if (!len) {
69                         if (!s->eof)
70                                 ustream_state_change(s);
71                         s->eof = true;
72                         ustream_fd_set_uloop(s, false);
73                         return;
74                 }
75
76                 ustream_fill_read(s, len);
77                 *more = true;
78         } while (1);
79 }
80
81 static int ustream_fd_write(struct ustream *s, const char *buf, int buflen, bool more)
82 {
83         struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
84         ssize_t ret = 0, len;
85
86         if (!buflen)
87                 return 0;
88
89         while (buflen) {
90                 len = write(sf->fd.fd, buf, buflen);
91
92                 if (len < 0) {
93                         if (errno == EINTR)
94                                 continue;
95
96                         if (errno == EAGAIN || errno == EWOULDBLOCK)
97                                 break;
98
99                         return -1;
100                 }
101
102                 ret += len;
103                 buf += len;
104                 buflen -= len;
105         }
106
107         if (buflen)
108                 ustream_fd_set_uloop(s, true);
109
110         return ret;
111 }
112
113 static bool __ustream_fd_poll(struct ustream_fd *sf, unsigned int events)
114 {
115         struct ustream *s = &sf->stream;
116         bool more = false;
117
118         if (events & ULOOP_READ)
119                 ustream_fd_read_pending(sf, &more);
120
121         if (events & ULOOP_WRITE) {
122                 if (!ustream_write_pending(s))
123                         ustream_fd_set_uloop(s, false);
124         }
125
126         return more;
127 }
128
129 static bool ustream_fd_poll(struct ustream *s)
130 {
131         struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
132
133         return __ustream_fd_poll(sf, ULOOP_READ | ULOOP_WRITE);
134 }
135
136 static void ustream_uloop_cb(struct uloop_fd *fd, unsigned int events)
137 {
138         struct ustream_fd *sf = container_of(fd, struct ustream_fd, fd);
139
140         __ustream_fd_poll(sf, events);
141 }
142
143 static void ustream_fd_free(struct ustream *s)
144 {
145         struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
146
147         uloop_fd_delete(&sf->fd);
148 }
149
150 void ustream_fd_init(struct ustream_fd *sf, int fd)
151 {
152         struct ustream *s = &sf->stream;
153
154         ustream_init_defaults(s);
155
156         sf->fd.fd = fd;
157         sf->fd.cb = ustream_uloop_cb;
158         s->set_read_blocked = ustream_fd_set_read_blocked;
159         s->write = ustream_fd_write;
160         s->free = ustream_fd_free;
161         s->poll = ustream_fd_poll;
162         ustream_fd_set_uloop(s, false);
163 }