]>
code.delx.au - pulseaudio/blob - src/pulsecore/ioline.c
4 This file is part of PulseAudio.
6 PulseAudio 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 PulseAudio 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 PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <pulse/xmalloc.h>
34 #include <pulsecore/core-error.h>
35 #include <pulsecore/log.h>
39 #define BUFFER_LIMIT (64*1024)
40 #define READ_SIZE (1024)
44 pa_defer_event
*defer_event
;
45 pa_mainloop_api
*mainloop
;
50 size_t wbuf_length
, wbuf_index
, wbuf_valid_length
;
53 size_t rbuf_length
, rbuf_index
, rbuf_valid_length
;
55 void (*callback
)(pa_ioline
*io
, const char *s
, void *userdata
);
61 static void io_callback(pa_iochannel
*io
, void *userdata
);
62 static void defer_callback(pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
);
64 pa_ioline
* pa_ioline_new(pa_iochannel
*io
) {
68 l
= pa_xnew(pa_ioline
, 1);
73 l
->wbuf_length
= l
->wbuf_index
= l
->wbuf_valid_length
= 0;
76 l
->rbuf_length
= l
->rbuf_index
= l
->rbuf_valid_length
= 0;
82 l
->mainloop
= pa_iochannel_get_mainloop_api(io
);
84 l
->defer_event
= l
->mainloop
->defer_new(l
->mainloop
, defer_callback
, l
);
85 l
->mainloop
->defer_enable(l
->defer_event
, 0);
89 pa_iochannel_set_callback(io
, io_callback
, l
);
94 static void ioline_free(pa_ioline
*l
) {
98 pa_iochannel_free(l
->io
);
101 l
->mainloop
->defer_free(l
->defer_event
);
108 void pa_ioline_unref(pa_ioline
*l
) {
116 pa_ioline
* pa_ioline_ref(pa_ioline
*l
) {
124 void pa_ioline_close(pa_ioline
*l
) {
131 pa_iochannel_free(l
->io
);
135 if (l
->defer_event
) {
136 l
->mainloop
->defer_free(l
->defer_event
);
137 l
->defer_event
= NULL
;
144 void pa_ioline_puts(pa_ioline
*l
, const char *c
) {
155 if (len
> BUFFER_LIMIT
- l
->wbuf_valid_length
)
156 len
= BUFFER_LIMIT
- l
->wbuf_valid_length
;
159 assert(l
->wbuf_length
>= l
->wbuf_valid_length
);
161 /* In case the allocated buffer is too small, enlarge it. */
162 if (l
->wbuf_valid_length
+ len
> l
->wbuf_length
) {
163 size_t n
= l
->wbuf_valid_length
+len
;
164 char *new = pa_xmalloc(n
);
166 memcpy(new, l
->wbuf
+l
->wbuf_index
, l
->wbuf_valid_length
);
172 } else if (l
->wbuf_index
+ l
->wbuf_valid_length
+ len
> l
->wbuf_length
) {
174 /* In case the allocated buffer fits, but the current index is too far from the start, move it to the front. */
175 memmove(l
->wbuf
, l
->wbuf
+l
->wbuf_index
, l
->wbuf_valid_length
);
179 assert(l
->wbuf_index
+ l
->wbuf_valid_length
+ len
<= l
->wbuf_length
);
181 /* Append the new string */
182 memcpy(l
->wbuf
+ l
->wbuf_index
+ l
->wbuf_valid_length
, c
, len
);
183 l
->wbuf_valid_length
+= len
;
185 l
->mainloop
->defer_enable(l
->defer_event
, 1);
189 void pa_ioline_set_callback(pa_ioline
*l
, void (*callback
)(pa_ioline
*io
, const char *s
, void *userdata
), void *userdata
) {
193 l
->callback
= callback
;
194 l
->userdata
= userdata
;
197 static void failure(pa_ioline
*l
, int process_leftover
) {
202 if (process_leftover
&& l
->rbuf_valid_length
> 0) {
203 /* Pass the last missing bit to the client */
206 char *p
= pa_xstrndup(l
->rbuf
+l
->rbuf_index
, l
->rbuf_valid_length
);
207 l
->callback(l
, p
, l
->userdata
);
213 l
->callback(l
, NULL
, l
->userdata
);
220 static void scan_for_lines(pa_ioline
*l
, size_t skip
) {
221 assert(l
&& l
->ref
>= 1 && skip
< l
->rbuf_valid_length
);
223 while (!l
->dead
&& l
->rbuf_valid_length
> skip
) {
227 if (!(e
= memchr(l
->rbuf
+ l
->rbuf_index
+ skip
, '\n', l
->rbuf_valid_length
- skip
)))
232 p
= l
->rbuf
+ l
->rbuf_index
;
235 l
->rbuf_index
+= m
+1;
236 l
->rbuf_valid_length
-= m
+1;
238 /* A shortcut for the next time */
239 if (l
->rbuf_valid_length
== 0)
243 l
->callback(l
, p
, l
->userdata
);
248 /* If the buffer became too large and still no newline was found, drop it. */
249 if (l
->rbuf_valid_length
>= BUFFER_LIMIT
)
250 l
->rbuf_index
= l
->rbuf_valid_length
= 0;
253 static int do_write(pa_ioline
*l
);
255 static int do_read(pa_ioline
*l
) {
256 assert(l
&& l
->ref
>= 1);
258 while (!l
->dead
&& pa_iochannel_is_readable(l
->io
)) {
262 len
= l
->rbuf_length
- l
->rbuf_index
- l
->rbuf_valid_length
;
264 /* Check if we have to enlarge the read buffer */
265 if (len
< READ_SIZE
) {
266 size_t n
= l
->rbuf_valid_length
+READ_SIZE
;
268 if (n
>= BUFFER_LIMIT
)
271 if (l
->rbuf_length
>= n
) {
272 /* The current buffer is large enough, let's just move the data to the front */
273 if (l
->rbuf_valid_length
)
274 memmove(l
->rbuf
, l
->rbuf
+l
->rbuf_index
, l
->rbuf_valid_length
);
276 /* Enlarge the buffer */
277 char *new = pa_xmalloc(n
);
278 if (l
->rbuf_valid_length
)
279 memcpy(new, l
->rbuf
+l
->rbuf_index
, l
->rbuf_valid_length
);
288 len
= l
->rbuf_length
- l
->rbuf_index
- l
->rbuf_valid_length
;
290 assert(len
>= READ_SIZE
);
293 if ((r
= pa_iochannel_read(l
->io
, l
->rbuf
+l
->rbuf_index
+l
->rbuf_valid_length
, len
)) <= 0) {
295 pa_log(__FILE__
": read(): %s", pa_cstrerror(errno
));
303 l
->rbuf_valid_length
+= r
;
305 /* Look if a line has been terminated in the newly read data */
306 scan_for_lines(l
, l
->rbuf_valid_length
- r
);
312 /* Try to flush the buffer */
313 static int do_write(pa_ioline
*l
) {
315 assert(l
&& l
->ref
>= 1);
317 while (!l
->dead
&& pa_iochannel_is_writable(l
->io
) && l
->wbuf_valid_length
) {
319 if ((r
= pa_iochannel_write(l
->io
, l
->wbuf
+l
->wbuf_index
, l
->wbuf_valid_length
)) < 0) {
320 pa_log(__FILE__
": write(): %s", r
< 0 ? pa_cstrerror(errno
) : "EOF");
326 l
->wbuf_valid_length
-= r
;
328 /* A shortcut for the next time */
329 if (l
->wbuf_valid_length
== 0)
336 /* Try to flush read/write data */
337 static void do_work(pa_ioline
*l
) {
343 l
->mainloop
->defer_enable(l
->defer_event
, 0);
351 if (l
->defer_close
&& !l
->wbuf_valid_length
)
357 static void io_callback(pa_iochannel
*io
, void *userdata
) {
358 pa_ioline
*l
= userdata
;
359 assert(io
&& l
&& l
->ref
>= 1);
364 static void defer_callback(pa_mainloop_api
*m
, pa_defer_event
*e
, void *userdata
) {
365 pa_ioline
*l
= userdata
;
366 assert(l
&& l
->ref
>= 1 && l
->mainloop
== m
&& l
->defer_event
== e
);
371 void pa_ioline_defer_close(pa_ioline
*l
) {
377 if (!l
->wbuf_valid_length
)
378 l
->mainloop
->defer_enable(l
->defer_event
, 1);
381 void pa_ioline_printf(pa_ioline
*l
, const char *format
, ...) {
388 va_start(ap
, format
);
389 t
= pa_vsprintf_malloc(format
, ap
);
392 pa_ioline_puts(l
, t
);