]>
code.delx.au - pulseaudio/blob - src/pulsecore/ioline.c
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
33 #include <pulse/xmalloc.h>
35 #include <pulsecore/core-error.h>
36 #include <pulsecore/log.h>
37 #include <pulsecore/macro.h>
38 #include <pulsecore/refcnt.h>
42 #define BUFFER_LIMIT (64*1024)
43 #define READ_SIZE (1024)
49 pa_defer_event
*defer_event
;
50 pa_mainloop_api
*mainloop
;
54 size_t wbuf_length
, wbuf_index
, wbuf_valid_length
;
57 size_t rbuf_length
, rbuf_index
, rbuf_valid_length
;
59 void (*callback
)(pa_ioline
*io
, const char *s
, void *userdata
);
65 static void io_callback(pa_iochannel
*io
, void *userdata
);
66 static void defer_callback(pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
);
68 pa_ioline
* pa_ioline_new(pa_iochannel
*io
) {
72 l
= pa_xnew(pa_ioline
, 1);
78 l
->wbuf_length
= l
->wbuf_index
= l
->wbuf_valid_length
= 0;
81 l
->rbuf_length
= l
->rbuf_index
= l
->rbuf_valid_length
= 0;
86 l
->mainloop
= pa_iochannel_get_mainloop_api(io
);
88 l
->defer_event
= l
->mainloop
->defer_new(l
->mainloop
, defer_callback
, l
);
89 l
->mainloop
->defer_enable(l
->defer_event
, 0);
93 pa_iochannel_set_callback(io
, io_callback
, l
);
98 static void ioline_free(pa_ioline
*l
) {
102 pa_iochannel_free(l
->io
);
105 l
->mainloop
->defer_free(l
->defer_event
);
112 void pa_ioline_unref(pa_ioline
*l
) {
114 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
116 if (PA_REFCNT_DEC(l
) <= 0)
120 pa_ioline
* pa_ioline_ref(pa_ioline
*l
) {
122 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
128 void pa_ioline_close(pa_ioline
*l
) {
130 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
135 pa_iochannel_free(l
->io
);
139 if (l
->defer_event
) {
140 l
->mainloop
->defer_free(l
->defer_event
);
141 l
->defer_event
= NULL
;
148 void pa_ioline_puts(pa_ioline
*l
, const char *c
) {
152 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
159 if (len
> BUFFER_LIMIT
- l
->wbuf_valid_length
)
160 len
= BUFFER_LIMIT
- l
->wbuf_valid_length
;
163 pa_assert(l
->wbuf_length
>= l
->wbuf_valid_length
);
165 /* In case the allocated buffer is too small, enlarge it. */
166 if (l
->wbuf_valid_length
+ len
> l
->wbuf_length
) {
167 size_t n
= l
->wbuf_valid_length
+len
;
168 char *new = pa_xmalloc(n
);
170 memcpy(new, l
->wbuf
+l
->wbuf_index
, l
->wbuf_valid_length
);
176 } else if (l
->wbuf_index
+ l
->wbuf_valid_length
+ len
> l
->wbuf_length
) {
178 /* In case the allocated buffer fits, but the current index is too far from the start, move it to the front. */
179 memmove(l
->wbuf
, l
->wbuf
+l
->wbuf_index
, l
->wbuf_valid_length
);
183 pa_assert(l
->wbuf_index
+ l
->wbuf_valid_length
+ len
<= l
->wbuf_length
);
185 /* Append the new string */
186 memcpy(l
->wbuf
+ l
->wbuf_index
+ l
->wbuf_valid_length
, c
, len
);
187 l
->wbuf_valid_length
+= len
;
189 l
->mainloop
->defer_enable(l
->defer_event
, 1);
193 void pa_ioline_set_callback(pa_ioline
*l
, void (*callback
)(pa_ioline
*io
, const char *s
, void *userdata
), void *userdata
) {
195 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
197 l
->callback
= callback
;
198 l
->userdata
= userdata
;
201 static void failure(pa_ioline
*l
, int process_leftover
) {
203 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
206 if (process_leftover
&& l
->rbuf_valid_length
> 0) {
207 /* Pass the last missing bit to the client */
210 char *p
= pa_xstrndup(l
->rbuf
+l
->rbuf_index
, l
->rbuf_valid_length
);
211 l
->callback(l
, p
, l
->userdata
);
217 l
->callback(l
, NULL
, l
->userdata
);
224 static void scan_for_lines(pa_ioline
*l
, size_t skip
) {
226 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
227 pa_assert(skip
< l
->rbuf_valid_length
);
229 while (!l
->dead
&& l
->rbuf_valid_length
> skip
) {
233 if (!(e
= memchr(l
->rbuf
+ l
->rbuf_index
+ skip
, '\n', l
->rbuf_valid_length
- skip
)))
238 p
= l
->rbuf
+ l
->rbuf_index
;
241 l
->rbuf_index
+= m
+1;
242 l
->rbuf_valid_length
-= m
+1;
244 /* A shortcut for the next time */
245 if (l
->rbuf_valid_length
== 0)
249 l
->callback(l
, p
, l
->userdata
);
254 /* If the buffer became too large and still no newline was found, drop it. */
255 if (l
->rbuf_valid_length
>= BUFFER_LIMIT
)
256 l
->rbuf_index
= l
->rbuf_valid_length
= 0;
259 static int do_write(pa_ioline
*l
);
261 static int do_read(pa_ioline
*l
) {
263 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
265 while (!l
->dead
&& pa_iochannel_is_readable(l
->io
)) {
269 len
= l
->rbuf_length
- l
->rbuf_index
- l
->rbuf_valid_length
;
271 /* Check if we have to enlarge the read buffer */
272 if (len
< READ_SIZE
) {
273 size_t n
= l
->rbuf_valid_length
+READ_SIZE
;
275 if (n
>= BUFFER_LIMIT
)
278 if (l
->rbuf_length
>= n
) {
279 /* The current buffer is large enough, let's just move the data to the front */
280 if (l
->rbuf_valid_length
)
281 memmove(l
->rbuf
, l
->rbuf
+l
->rbuf_index
, l
->rbuf_valid_length
);
283 /* Enlarge the buffer */
284 char *new = pa_xmalloc(n
);
285 if (l
->rbuf_valid_length
)
286 memcpy(new, l
->rbuf
+l
->rbuf_index
, l
->rbuf_valid_length
);
295 len
= l
->rbuf_length
- l
->rbuf_index
- l
->rbuf_valid_length
;
297 pa_assert(len
>= READ_SIZE
);
300 if ((r
= pa_iochannel_read(l
->io
, l
->rbuf
+l
->rbuf_index
+l
->rbuf_valid_length
, len
)) <= 0) {
301 if (r
< 0 && errno
!= ECONNRESET
) {
302 pa_log("read(): %s", pa_cstrerror(errno
));
310 l
->rbuf_valid_length
+= r
;
312 /* Look if a line has been terminated in the newly read data */
313 scan_for_lines(l
, l
->rbuf_valid_length
- r
);
319 /* Try to flush the buffer */
320 static int do_write(pa_ioline
*l
) {
324 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
326 while (!l
->dead
&& pa_iochannel_is_writable(l
->io
) && l
->wbuf_valid_length
) {
328 if ((r
= pa_iochannel_write(l
->io
, l
->wbuf
+l
->wbuf_index
, l
->wbuf_valid_length
)) <= 0) {
330 if (r
< 0 && errno
!= EPIPE
)
331 pa_log("write(): %s", pa_cstrerror(errno
));
339 l
->wbuf_valid_length
-= r
;
341 /* A shortcut for the next time */
342 if (l
->wbuf_valid_length
== 0)
349 /* Try to flush read/write data */
350 static void do_work(pa_ioline
*l
) {
352 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
356 l
->mainloop
->defer_enable(l
->defer_event
, 0);
364 if (l
->defer_close
&& !l
->wbuf_valid_length
)
370 static void io_callback(pa_iochannel
*io
, void *userdata
) {
371 pa_ioline
*l
= userdata
;
375 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
380 static void defer_callback(pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
) {
381 pa_ioline
*l
= userdata
;
384 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
385 pa_assert(l
->mainloop
== m
);
386 pa_assert(l
->defer_event
== e
);
391 void pa_ioline_defer_close(pa_ioline
*l
) {
393 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
397 if (!l
->wbuf_valid_length
)
398 l
->mainloop
->defer_enable(l
->defer_event
, 1);
401 void pa_ioline_printf(pa_ioline
*l
, const char *format
, ...) {
406 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
408 va_start(ap
, format
);
409 t
= pa_vsprintf_malloc(format
, ap
);
412 pa_ioline_puts(l
, t
);