]>
code.delx.au - pulseaudio/blob - src/polypcore/ioline.c
4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <polypcore/xmalloc.h>
33 #include <polypcore/log.h>
37 #define BUFFER_LIMIT (64*1024)
38 #define READ_SIZE (1024)
42 pa_defer_event
*defer_event
;
43 pa_mainloop_api
*mainloop
;
48 size_t wbuf_length
, wbuf_index
, wbuf_valid_length
;
51 size_t rbuf_length
, rbuf_index
, rbuf_valid_length
;
53 void (*callback
)(pa_ioline
*io
, const char *s
, void *userdata
);
59 static void io_callback(pa_iochannel
*io
, void *userdata
);
60 static void defer_callback(pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
);
62 pa_ioline
* pa_ioline_new(pa_iochannel
*io
) {
66 l
= pa_xmalloc(sizeof(pa_ioline
));
71 l
->wbuf_length
= l
->wbuf_index
= l
->wbuf_valid_length
= 0;
74 l
->rbuf_length
= l
->rbuf_index
= l
->rbuf_valid_length
= 0;
80 l
->mainloop
= pa_iochannel_get_mainloop_api(io
);
82 l
->defer_event
= l
->mainloop
->defer_new(l
->mainloop
, defer_callback
, l
);
83 l
->mainloop
->defer_enable(l
->defer_event
, 0);
87 pa_iochannel_set_callback(io
, io_callback
, l
);
92 static void ioline_free(pa_ioline
*l
) {
96 pa_iochannel_free(l
->io
);
99 l
->mainloop
->defer_free(l
->defer_event
);
106 void pa_ioline_unref(pa_ioline
*l
) {
107 assert(l
&& l
->ref
>= 1);
113 pa_ioline
* pa_ioline_ref(pa_ioline
*l
) {
114 assert(l
&& l
->ref
>= 1);
120 void pa_ioline_close(pa_ioline
*l
) {
121 assert(l
&& l
->ref
>= 1);
125 pa_iochannel_free(l
->io
);
129 if (l
->defer_event
) {
130 l
->mainloop
->defer_free(l
->defer_event
);
131 l
->defer_event
= NULL
;
135 void pa_ioline_puts(pa_ioline
*l
, const char *c
) {
137 assert(l
&& c
&& l
->ref
>= 1 && !l
->dead
);
142 if (len
> BUFFER_LIMIT
- l
->wbuf_valid_length
)
143 len
= BUFFER_LIMIT
- l
->wbuf_valid_length
;
146 assert(l
->wbuf_length
>= l
->wbuf_valid_length
);
148 /* In case the allocated buffer is too small, enlarge it. */
149 if (l
->wbuf_valid_length
+ len
> l
->wbuf_length
) {
150 size_t n
= l
->wbuf_valid_length
+len
;
151 char *new = pa_xmalloc(n
);
153 memcpy(new, l
->wbuf
+l
->wbuf_index
, l
->wbuf_valid_length
);
159 } else if (l
->wbuf_index
+ l
->wbuf_valid_length
+ len
> l
->wbuf_length
) {
161 /* In case the allocated buffer fits, but the current index is too far from the start, move it to the front. */
162 memmove(l
->wbuf
, l
->wbuf
+l
->wbuf_index
, l
->wbuf_valid_length
);
166 assert(l
->wbuf_index
+ l
->wbuf_valid_length
+ len
<= l
->wbuf_length
);
168 /* Append the new string */
169 memcpy(l
->wbuf
+ l
->wbuf_index
+ l
->wbuf_valid_length
, c
, len
);
170 l
->wbuf_valid_length
+= len
;
172 l
->mainloop
->defer_enable(l
->defer_event
, 1);
178 void pa_ioline_set_callback(pa_ioline
*l
, void (*callback
)(pa_ioline
*io
, const char *s
, void *userdata
), void *userdata
) {
179 assert(l
&& l
->ref
>= 1);
180 l
->callback
= callback
;
181 l
->userdata
= userdata
;
184 static void failure(pa_ioline
*l
) {
185 assert(l
&& l
->ref
>= 1 && !l
->dead
);
190 l
->callback(l
, NULL
, l
->userdata
);
195 static void scan_for_lines(pa_ioline
*l
, size_t skip
) {
196 assert(l
&& l
->ref
>= 1 && skip
< l
->rbuf_valid_length
);
198 while (!l
->dead
&& l
->rbuf_valid_length
> skip
) {
202 if (!(e
= memchr(l
->rbuf
+ l
->rbuf_index
+ skip
, '\n', l
->rbuf_valid_length
- skip
)))
207 p
= l
->rbuf
+ l
->rbuf_index
;
210 l
->rbuf_index
+= m
+1;
211 l
->rbuf_valid_length
-= m
+1;
213 /* A shortcut for the next time */
214 if (l
->rbuf_valid_length
== 0)
218 l
->callback(l
, p
, l
->userdata
);
223 /* If the buffer became too large and still no newline was found, drop it. */
224 if (l
->rbuf_valid_length
>= BUFFER_LIMIT
)
225 l
->rbuf_index
= l
->rbuf_valid_length
= 0;
228 static int do_write(pa_ioline
*l
);
230 static int do_read(pa_ioline
*l
) {
231 assert(l
&& l
->ref
>= 1);
233 while (!l
->dead
&& pa_iochannel_is_readable(l
->io
)) {
237 len
= l
->rbuf_length
- l
->rbuf_index
- l
->rbuf_valid_length
;
239 /* Check if we have to enlarge the read buffer */
240 if (len
< READ_SIZE
) {
241 size_t n
= l
->rbuf_valid_length
+READ_SIZE
;
243 if (n
>= BUFFER_LIMIT
)
246 if (l
->rbuf_length
>= n
) {
247 /* The current buffer is large enough, let's just move the data to the front */
248 if (l
->rbuf_valid_length
)
249 memmove(l
->rbuf
, l
->rbuf
+l
->rbuf_index
, l
->rbuf_valid_length
);
251 /* Enlarge the buffer */
252 char *new = pa_xmalloc(n
);
253 if (l
->rbuf_valid_length
)
254 memcpy(new, l
->rbuf
+l
->rbuf_index
, l
->rbuf_valid_length
);
263 len
= l
->rbuf_length
- l
->rbuf_index
- l
->rbuf_valid_length
;
265 assert(len
>= READ_SIZE
);
268 r
= pa_iochannel_read(l
->io
, l
->rbuf
+l
->rbuf_index
+l
->rbuf_valid_length
, len
);
270 /* Got an EOF, so fake an exit command. */
272 snprintf (l
->rbuf
, l
->rbuf_length
, "exit\n");
274 pa_ioline_puts(l
, "\nExiting.\n");
277 pa_log(__FILE__
": read() failed: %s", strerror(errno
));
282 l
->rbuf_valid_length
+= r
;
284 /* Look if a line has been terminated in the newly read data */
285 scan_for_lines(l
, l
->rbuf_valid_length
- r
);
291 /* Try to flush the buffer */
292 static int do_write(pa_ioline
*l
) {
294 assert(l
&& l
->ref
>= 1);
296 while (!l
->dead
&& pa_iochannel_is_writable(l
->io
) && l
->wbuf_valid_length
) {
298 if ((r
= pa_iochannel_write(l
->io
, l
->wbuf
+l
->wbuf_index
, l
->wbuf_valid_length
)) < 0) {
299 pa_log(__FILE__
": write() failed: %s", r
< 0 ? strerror(errno
) : "EOF");
305 l
->wbuf_valid_length
-= r
;
307 /* A shortcut for the next time */
308 if (l
->wbuf_valid_length
== 0)
315 /* Try to flush read/write data */
316 static void do_work(pa_ioline
*l
) {
317 assert(l
&& l
->ref
>= 1);
321 l
->mainloop
->defer_enable(l
->defer_event
, 0);
329 if (l
->defer_close
&& !l
->wbuf_valid_length
)
335 static void io_callback(pa_iochannel
*io
, void *userdata
) {
336 pa_ioline
*l
= userdata
;
337 assert(io
&& l
&& l
->ref
>= 1);
342 static void defer_callback(pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
) {
343 pa_ioline
*l
= userdata
;
344 assert(l
&& l
->ref
>= 1 && l
->mainloop
== m
&& l
->defer_event
== e
);
349 void pa_ioline_defer_close(pa_ioline
*l
) {
354 if (!l
->wbuf_valid_length
)
355 l
->mainloop
->defer_enable(l
->defer_event
, 1);
358 void pa_ioline_printf(pa_ioline
*s
, const char *format
, ...) {
363 va_start(ap
, format
);
364 t
= pa_vsprintf_malloc(format
, ap
);
367 pa_ioline_puts(s
, t
);