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/winsock.h>
36 #include <pulsecore/core-error.h>
37 #include <pulsecore/log.h>
38 #include <pulsecore/macro.h>
39 #include <pulsecore/refcnt.h>
43 #define BUFFER_LIMIT (64*1024)
44 #define READ_SIZE (1024)
50 pa_defer_event
*defer_event
;
51 pa_mainloop_api
*mainloop
;
55 size_t wbuf_length
, wbuf_index
, wbuf_valid_length
;
58 size_t rbuf_length
, rbuf_index
, rbuf_valid_length
;
60 void (*callback
)(pa_ioline
*io
, const char *s
, void *userdata
);
66 static void io_callback(pa_iochannel
*io
, void *userdata
);
67 static void defer_callback(pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
);
69 pa_ioline
* pa_ioline_new(pa_iochannel
*io
) {
73 l
= pa_xnew(pa_ioline
, 1);
79 l
->wbuf_length
= l
->wbuf_index
= l
->wbuf_valid_length
= 0;
82 l
->rbuf_length
= l
->rbuf_index
= l
->rbuf_valid_length
= 0;
87 l
->mainloop
= pa_iochannel_get_mainloop_api(io
);
89 l
->defer_event
= l
->mainloop
->defer_new(l
->mainloop
, defer_callback
, l
);
90 l
->mainloop
->defer_enable(l
->defer_event
, 0);
94 pa_iochannel_set_callback(io
, io_callback
, l
);
99 static void ioline_free(pa_ioline
*l
) {
103 pa_iochannel_free(l
->io
);
106 l
->mainloop
->defer_free(l
->defer_event
);
113 void pa_ioline_unref(pa_ioline
*l
) {
115 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
117 if (PA_REFCNT_DEC(l
) <= 0)
121 pa_ioline
* pa_ioline_ref(pa_ioline
*l
) {
123 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
129 void pa_ioline_close(pa_ioline
*l
) {
131 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
136 pa_iochannel_free(l
->io
);
140 if (l
->defer_event
) {
141 l
->mainloop
->defer_free(l
->defer_event
);
142 l
->defer_event
= NULL
;
149 void pa_ioline_puts(pa_ioline
*l
, const char *c
) {
153 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
160 if (len
> BUFFER_LIMIT
- l
->wbuf_valid_length
)
161 len
= BUFFER_LIMIT
- l
->wbuf_valid_length
;
164 pa_assert(l
->wbuf_length
>= l
->wbuf_valid_length
);
166 /* In case the allocated buffer is too small, enlarge it. */
167 if (l
->wbuf_valid_length
+ len
> l
->wbuf_length
) {
168 size_t n
= l
->wbuf_valid_length
+len
;
169 char *new = pa_xmalloc(n
);
171 memcpy(new, l
->wbuf
+l
->wbuf_index
, l
->wbuf_valid_length
);
177 } else if (l
->wbuf_index
+ l
->wbuf_valid_length
+ len
> l
->wbuf_length
) {
179 /* In case the allocated buffer fits, but the current index is too far from the start, move it to the front. */
180 memmove(l
->wbuf
, l
->wbuf
+l
->wbuf_index
, l
->wbuf_valid_length
);
184 pa_assert(l
->wbuf_index
+ l
->wbuf_valid_length
+ len
<= l
->wbuf_length
);
186 /* Append the new string */
187 memcpy(l
->wbuf
+ l
->wbuf_index
+ l
->wbuf_valid_length
, c
, len
);
188 l
->wbuf_valid_length
+= len
;
190 l
->mainloop
->defer_enable(l
->defer_event
, 1);
194 void pa_ioline_set_callback(pa_ioline
*l
, void (*callback
)(pa_ioline
*io
, const char *s
, void *userdata
), void *userdata
) {
196 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
198 l
->callback
= callback
;
199 l
->userdata
= userdata
;
202 static void failure(pa_ioline
*l
, int process_leftover
) {
204 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
207 if (process_leftover
&& l
->rbuf_valid_length
> 0) {
208 /* Pass the last missing bit to the client */
211 char *p
= pa_xstrndup(l
->rbuf
+l
->rbuf_index
, l
->rbuf_valid_length
);
212 l
->callback(l
, p
, l
->userdata
);
218 l
->callback(l
, NULL
, l
->userdata
);
225 static void scan_for_lines(pa_ioline
*l
, size_t skip
) {
227 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
228 pa_assert(skip
< l
->rbuf_valid_length
);
230 while (!l
->dead
&& l
->rbuf_valid_length
> skip
) {
234 if (!(e
= memchr(l
->rbuf
+ l
->rbuf_index
+ skip
, '\n', l
->rbuf_valid_length
- skip
)))
239 p
= l
->rbuf
+ l
->rbuf_index
;
242 l
->rbuf_index
+= m
+1;
243 l
->rbuf_valid_length
-= m
+1;
245 /* A shortcut for the next time */
246 if (l
->rbuf_valid_length
== 0)
250 l
->callback(l
, p
, l
->userdata
);
255 /* If the buffer became too large and still no newline was found, drop it. */
256 if (l
->rbuf_valid_length
>= BUFFER_LIMIT
)
257 l
->rbuf_index
= l
->rbuf_valid_length
= 0;
260 static int do_write(pa_ioline
*l
);
262 static int do_read(pa_ioline
*l
) {
264 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
266 while (!l
->dead
&& pa_iochannel_is_readable(l
->io
)) {
270 len
= l
->rbuf_length
- l
->rbuf_index
- l
->rbuf_valid_length
;
272 /* Check if we have to enlarge the read buffer */
273 if (len
< READ_SIZE
) {
274 size_t n
= l
->rbuf_valid_length
+READ_SIZE
;
276 if (n
>= BUFFER_LIMIT
)
279 if (l
->rbuf_length
>= n
) {
280 /* The current buffer is large enough, let's just move the data to the front */
281 if (l
->rbuf_valid_length
)
282 memmove(l
->rbuf
, l
->rbuf
+l
->rbuf_index
, l
->rbuf_valid_length
);
284 /* Enlarge the buffer */
285 char *new = pa_xmalloc(n
);
286 if (l
->rbuf_valid_length
)
287 memcpy(new, l
->rbuf
+l
->rbuf_index
, l
->rbuf_valid_length
);
296 len
= l
->rbuf_length
- l
->rbuf_index
- l
->rbuf_valid_length
;
298 pa_assert(len
>= READ_SIZE
);
301 if ((r
= pa_iochannel_read(l
->io
, l
->rbuf
+l
->rbuf_index
+l
->rbuf_valid_length
, len
)) <= 0) {
302 if (r
< 0 && errno
!= ECONNRESET
) {
303 pa_log("read(): %s", pa_cstrerror(errno
));
311 l
->rbuf_valid_length
+= r
;
313 /* Look if a line has been terminated in the newly read data */
314 scan_for_lines(l
, l
->rbuf_valid_length
- r
);
320 /* Try to flush the buffer */
321 static int do_write(pa_ioline
*l
) {
325 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
327 while (!l
->dead
&& pa_iochannel_is_writable(l
->io
) && l
->wbuf_valid_length
) {
329 if ((r
= pa_iochannel_write(l
->io
, l
->wbuf
+l
->wbuf_index
, l
->wbuf_valid_length
)) <= 0) {
331 if (r
< 0 && errno
!= EPIPE
)
332 pa_log("write(): %s", pa_cstrerror(errno
));
340 l
->wbuf_valid_length
-= r
;
342 /* A shortcut for the next time */
343 if (l
->wbuf_valid_length
== 0)
350 /* Try to flush read/write data */
351 static void do_work(pa_ioline
*l
) {
353 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
357 l
->mainloop
->defer_enable(l
->defer_event
, 0);
365 if (l
->defer_close
&& !l
->wbuf_valid_length
)
371 static void io_callback(pa_iochannel
*io
, void *userdata
) {
372 pa_ioline
*l
= userdata
;
376 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
381 static void defer_callback(pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
) {
382 pa_ioline
*l
= userdata
;
385 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
386 pa_assert(l
->mainloop
== m
);
387 pa_assert(l
->defer_event
== e
);
392 void pa_ioline_defer_close(pa_ioline
*l
) {
394 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
398 if (!l
->wbuf_valid_length
)
399 l
->mainloop
->defer_enable(l
->defer_event
, 1);
402 void pa_ioline_printf(pa_ioline
*l
, const char *format
, ...) {
407 pa_assert(PA_REFCNT_VALUE(l
) >= 1);
409 va_start(ap
, format
);
410 t
= pa_vsprintf_malloc(format
, ap
);
413 pa_ioline_puts(l
, t
);