]>
code.delx.au - pulseaudio/blob - src/pulsecore/ioline.c
07b60bee31777dd0ea9900f32fb785fb292ce697
4 This file is part of PulseAudio.
6 Copyright 2004-2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #include <pulse/xmalloc.h>
36 #include <pulsecore/core-error.h>
37 #include <pulsecore/log.h>
41 #define BUFFER_LIMIT (64*1024)
42 #define READ_SIZE (1024)
46 pa_defer_event
*defer_event
;
47 pa_mainloop_api
*mainloop
;
52 size_t wbuf_length
, wbuf_index
, wbuf_valid_length
;
55 size_t rbuf_length
, rbuf_index
, rbuf_valid_length
;
57 void (*callback
)(pa_ioline
*io
, const char *s
, void *userdata
);
63 static void io_callback(pa_iochannel
*io
, void *userdata
);
64 static void defer_callback(pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
);
66 pa_ioline
* pa_ioline_new(pa_iochannel
*io
) {
70 l
= pa_xnew(pa_ioline
, 1);
75 l
->wbuf_length
= l
->wbuf_index
= l
->wbuf_valid_length
= 0;
78 l
->rbuf_length
= l
->rbuf_index
= l
->rbuf_valid_length
= 0;
84 l
->mainloop
= pa_iochannel_get_mainloop_api(io
);
86 l
->defer_event
= l
->mainloop
->defer_new(l
->mainloop
, defer_callback
, l
);
87 l
->mainloop
->defer_enable(l
->defer_event
, 0);
91 pa_iochannel_set_callback(io
, io_callback
, l
);
96 static void ioline_free(pa_ioline
*l
) {
100 pa_iochannel_free(l
->io
);
103 l
->mainloop
->defer_free(l
->defer_event
);
110 void pa_ioline_unref(pa_ioline
*l
) {
118 pa_ioline
* pa_ioline_ref(pa_ioline
*l
) {
126 void pa_ioline_close(pa_ioline
*l
) {
133 pa_iochannel_free(l
->io
);
137 if (l
->defer_event
) {
138 l
->mainloop
->defer_free(l
->defer_event
);
139 l
->defer_event
= NULL
;
146 void pa_ioline_puts(pa_ioline
*l
, const char *c
) {
157 if (len
> BUFFER_LIMIT
- l
->wbuf_valid_length
)
158 len
= BUFFER_LIMIT
- l
->wbuf_valid_length
;
161 assert(l
->wbuf_length
>= l
->wbuf_valid_length
);
163 /* In case the allocated buffer is too small, enlarge it. */
164 if (l
->wbuf_valid_length
+ len
> l
->wbuf_length
) {
165 size_t n
= l
->wbuf_valid_length
+len
;
166 char *new = pa_xmalloc(n
);
168 memcpy(new, l
->wbuf
+l
->wbuf_index
, l
->wbuf_valid_length
);
174 } else if (l
->wbuf_index
+ l
->wbuf_valid_length
+ len
> l
->wbuf_length
) {
176 /* In case the allocated buffer fits, but the current index is too far from the start, move it to the front. */
177 memmove(l
->wbuf
, l
->wbuf
+l
->wbuf_index
, l
->wbuf_valid_length
);
181 assert(l
->wbuf_index
+ l
->wbuf_valid_length
+ len
<= l
->wbuf_length
);
183 /* Append the new string */
184 memcpy(l
->wbuf
+ l
->wbuf_index
+ l
->wbuf_valid_length
, c
, len
);
185 l
->wbuf_valid_length
+= len
;
187 l
->mainloop
->defer_enable(l
->defer_event
, 1);
191 void pa_ioline_set_callback(pa_ioline
*l
, void (*callback
)(pa_ioline
*io
, const char *s
, void *userdata
), void *userdata
) {
195 l
->callback
= callback
;
196 l
->userdata
= userdata
;
199 static void failure(pa_ioline
*l
, int process_leftover
) {
204 if (process_leftover
&& l
->rbuf_valid_length
> 0) {
205 /* Pass the last missing bit to the client */
208 char *p
= pa_xstrndup(l
->rbuf
+l
->rbuf_index
, l
->rbuf_valid_length
);
209 l
->callback(l
, p
, l
->userdata
);
215 l
->callback(l
, NULL
, l
->userdata
);
222 static void scan_for_lines(pa_ioline
*l
, size_t skip
) {
223 assert(l
&& l
->ref
>= 1 && skip
< l
->rbuf_valid_length
);
225 while (!l
->dead
&& l
->rbuf_valid_length
> skip
) {
229 if (!(e
= memchr(l
->rbuf
+ l
->rbuf_index
+ skip
, '\n', l
->rbuf_valid_length
- skip
)))
234 p
= l
->rbuf
+ l
->rbuf_index
;
237 l
->rbuf_index
+= m
+1;
238 l
->rbuf_valid_length
-= m
+1;
240 /* A shortcut for the next time */
241 if (l
->rbuf_valid_length
== 0)
245 l
->callback(l
, p
, l
->userdata
);
250 /* If the buffer became too large and still no newline was found, drop it. */
251 if (l
->rbuf_valid_length
>= BUFFER_LIMIT
)
252 l
->rbuf_index
= l
->rbuf_valid_length
= 0;
255 static int do_write(pa_ioline
*l
);
257 static int do_read(pa_ioline
*l
) {
258 assert(l
&& l
->ref
>= 1);
260 while (!l
->dead
&& pa_iochannel_is_readable(l
->io
)) {
264 len
= l
->rbuf_length
- l
->rbuf_index
- l
->rbuf_valid_length
;
266 /* Check if we have to enlarge the read buffer */
267 if (len
< READ_SIZE
) {
268 size_t n
= l
->rbuf_valid_length
+READ_SIZE
;
270 if (n
>= BUFFER_LIMIT
)
273 if (l
->rbuf_length
>= n
) {
274 /* The current buffer is large enough, let's just move the data to the front */
275 if (l
->rbuf_valid_length
)
276 memmove(l
->rbuf
, l
->rbuf
+l
->rbuf_index
, l
->rbuf_valid_length
);
278 /* Enlarge the buffer */
279 char *new = pa_xmalloc(n
);
280 if (l
->rbuf_valid_length
)
281 memcpy(new, l
->rbuf
+l
->rbuf_index
, l
->rbuf_valid_length
);
290 len
= l
->rbuf_length
- l
->rbuf_index
- l
->rbuf_valid_length
;
292 assert(len
>= READ_SIZE
);
295 if ((r
= pa_iochannel_read(l
->io
, l
->rbuf
+l
->rbuf_index
+l
->rbuf_valid_length
, len
)) <= 0) {
297 pa_log("read(): %s", pa_cstrerror(errno
));
305 l
->rbuf_valid_length
+= r
;
307 /* Look if a line has been terminated in the newly read data */
308 scan_for_lines(l
, l
->rbuf_valid_length
- r
);
314 /* Try to flush the buffer */
315 static int do_write(pa_ioline
*l
) {
317 assert(l
&& l
->ref
>= 1);
319 while (!l
->dead
&& pa_iochannel_is_writable(l
->io
) && l
->wbuf_valid_length
) {
321 if ((r
= pa_iochannel_write(l
->io
, l
->wbuf
+l
->wbuf_index
, l
->wbuf_valid_length
)) < 0) {
322 pa_log("write(): %s", r
< 0 ? pa_cstrerror(errno
) : "EOF");
328 l
->wbuf_valid_length
-= r
;
330 /* A shortcut for the next time */
331 if (l
->wbuf_valid_length
== 0)
338 /* Try to flush read/write data */
339 static void do_work(pa_ioline
*l
) {
345 l
->mainloop
->defer_enable(l
->defer_event
, 0);
353 if (l
->defer_close
&& !l
->wbuf_valid_length
)
359 static void io_callback(pa_iochannel
*io
, void *userdata
) {
360 pa_ioline
*l
= userdata
;
361 assert(io
&& l
&& l
->ref
>= 1);
366 static void defer_callback(pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
) {
367 pa_ioline
*l
= userdata
;
368 assert(l
&& l
->ref
>= 1 && l
->mainloop
== m
&& l
->defer_event
== e
);
373 void pa_ioline_defer_close(pa_ioline
*l
) {
379 if (!l
->wbuf_valid_length
)
380 l
->mainloop
->defer_enable(l
->defer_event
, 1);
383 void pa_ioline_printf(pa_ioline
*l
, const char *format
, ...) {
390 va_start(ap
, format
);
391 t
= pa_vsprintf_malloc(format
, ap
);
394 pa_ioline_puts(l
, t
);