• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/libubox/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 | ULOOP_ERROR_CB;
 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         struct ustream_free_guard guard;
 49         int buflen = 0;
 50         ssize_t len;
 51         char *buf;
 52 
 53         do {
 54                 if (s->read_blocked)
 55                         break;
 56 
 57                 buf = ustream_reserve(s, 1, &buflen);
 58                 if (!buf)
 59                         break;
 60 
 61                 len = read(sf->fd.fd, buf, buflen);
 62                 if (len < 0) {
 63                         if (errno == EINTR)
 64                                 continue;
 65 
 66                         if (errno == EAGAIN || errno == ENOTCONN)
 67                                 return;
 68 
 69                         len = 0;
 70                 }
 71 
 72                 if (!len) {
 73                         if (!s->eof)
 74                                 ustream_state_change(s);
 75                         s->eof = true;
 76                         ustream_fd_set_uloop(s, false);
 77                         return;
 78                 }
 79 
 80                 ustream_free_guard_set(s, &guard);
 81                 ustream_fill_read(s, len);
 82                 if (ustream_free_guard_check(s, &guard))
 83                         return;
 84 
 85                 *more = true;
 86         } while (1);
 87 }
 88 
 89 static int ustream_fd_write(struct ustream *s, const char *buf, int buflen, bool more)
 90 {
 91         struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
 92         ssize_t ret = 0, len;
 93 
 94         if (!buflen)
 95                 return 0;
 96 
 97         while (buflen) {
 98                 len = write(sf->fd.fd, buf, buflen);
 99 
100                 if (len < 0) {
101                         if (errno == EINTR)
102                                 continue;
103 
104                         if (errno == EAGAIN || errno == EWOULDBLOCK || errno == ENOTCONN)
105                                 break;
106 
107                         return -1;
108                 }
109 
110                 ret += len;
111                 buf += len;
112                 buflen -= len;
113         }
114 
115         if (buflen)
116                 ustream_fd_set_uloop(s, true);
117 
118         return ret;
119 }
120 
121 static bool __ustream_fd_poll(struct ustream_fd *sf, unsigned int events)
122 {
123         struct ustream *s = &sf->stream;
124         struct ustream_free_guard guard;
125         bool more = false;
126 
127         if (events & ULOOP_READ) {
128                 ustream_free_guard_set(s, &guard);
129                 ustream_fd_read_pending(sf, &more);
130                 if (ustream_free_guard_check(s, &guard))
131                         return more;
132         }
133 
134         if (events & ULOOP_WRITE) {
135                 bool no_more;
136 
137                 ustream_free_guard_set(s, &guard);
138                 no_more = ustream_write_pending(s);
139                 if (ustream_free_guard_check(s, &guard))
140                         return more;
141 
142                 if (no_more)
143                         ustream_fd_set_uloop(s, false);
144         }
145 
146         if (sf->fd.error && !s->write_error) {
147                 ustream_state_change(s);
148                 s->write_error = true;
149                 ustream_fd_set_uloop(s, false);
150         }
151 
152         return more;
153 }
154 
155 static bool ustream_fd_poll(struct ustream *s)
156 {
157         struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
158 
159         return __ustream_fd_poll(sf, ULOOP_READ | ULOOP_WRITE);
160 }
161 
162 static void ustream_uloop_cb(struct uloop_fd *fd, unsigned int events)
163 {
164         struct ustream_fd *sf = container_of(fd, struct ustream_fd, fd);
165 
166         __ustream_fd_poll(sf, events);
167 }
168 
169 static void ustream_fd_free(struct ustream *s)
170 {
171         struct ustream_fd *sf = container_of(s, struct ustream_fd, stream);
172 
173         uloop_fd_delete(&sf->fd);
174 }
175 
176 void ustream_fd_init(struct ustream_fd *sf, int fd)
177 {
178         struct ustream *s = &sf->stream;
179 
180         ustream_init_defaults(s);
181 
182         sf->fd.fd = fd;
183         sf->fd.cb = ustream_uloop_cb;
184         s->set_read_blocked = ustream_fd_set_read_blocked;
185         s->write = ustream_fd_write;
186         s->free = ustream_fd_free;
187         s->poll = ustream_fd_poll;
188         ustream_fd_set_uloop(s, false);
189 }
190 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt