summaryrefslogtreecommitdiffstats
path: root/polyp/ioline.c
blob: 6f7886da5b428df963563bdfb24b7172f94e31bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/* $Id$ */

/***
  This file is part of polypaudio.
 
  polypaudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published
  by the Free Software Foundation; either version 2 of the License,
  or (at your option) any later version.
 
  polypaudio is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.
 
  You should have received a copy of the GNU Lesser General Public License
  along with polypaudio; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "ioline.h"
#include "xmalloc.h"
#include "log.h"

#define BUFFER_LIMIT (64*1024)
#define READ_SIZE (1024)

struct pa_ioline {
    struct pa_iochannel *io;
    struct pa_defer_event *defer_event;
    struct pa_mainloop_api *mainloop;
    int ref;
    int dead;

    char *wbuf;
    size_t wbuf_length, wbuf_index, wbuf_valid_length;

    char *rbuf;
    size_t rbuf_length, rbuf_index, rbuf_valid_length;

    void (*callback)(struct pa_ioline*io, const char *s, void *userdata);
    void *userdata;

    int defer_close;
};

static void io_callback(struct pa_iochannel*io, void *userdata);
static void defer_callback(struct pa_mainloop_api*m, struct pa_defer_event*e, void *userdata);

struct pa_ioline* pa_ioline_new(struct pa_iochannel *io) {
    struct pa_ioline *l;
    assert(io);
    
    l = pa_xmalloc(sizeof(struct pa_ioline));
    l->io = io;
    l->dead = 0;

    l->wbuf = NULL;
    l->wbuf_length = l->wbuf_index = l->wbuf_valid_length = 0;

    l->rbuf = NULL;
    l->rbuf_length = l->rbuf_index = l->rbuf_valid_length = 0;

    l->callback = NULL;
    l->userdata = NULL;
    l->ref = 1;

    l->mainloop = pa_iochannel_get_mainloop_api(io);

    l->defer_event = l->mainloop->defer_new(l->mainloop, defer_callback, l);
    l->mainloop->defer_enable(l->defer_event, 0);

    l->defer_close = 0;
    
    pa_iochannel_set_callback(io, io_callback, l);
    
    return l;
}

static void ioline_free(struct pa_ioline *l) {
    assert(l);

    if (l->io)
        pa_iochannel_free(l->io);

    if (l->defer_event)
        l->mainloop->defer_free(l->defer_event);

    pa_xfree(l->wbuf);
    pa_xfree(l->rbuf);
    pa_xfree(l);
}

void pa_ioline_unref(struct pa_ioline *l) {
    assert(l && l->ref >= 1);

    if ((--l->ref) <= 0)
        ioline_free(l);
}

struct pa_ioline* pa_ioline_ref(struct pa_ioline *l) {
    assert(l && l->ref >= 1);

    l->ref++;
    return l;
}

void pa_ioline_close(struct pa_ioline *l) {
    assert(l && l->ref >= 1);

    l->dead = 1;
    if (l->io) {
        pa_iochannel_free(l->io);
        l->io = NULL;
    }

    if (l->defer_event) {
        l->mainloop->defer_free(l->defer_event);
        l->defer_event = NULL;
    }
}

void pa_ioline_puts(struct pa_ioline *l, const char *c) {
    size_t len;
    assert(l && c && l->ref >= 1 && !l->dead);

    pa_ioline_ref(l);
    
    len = strlen(c);
    if (len > BUFFER_LIMIT - l->wbuf_valid_length)
        len = BUFFER_LIMIT - l->wbuf_valid_length;

    if (len) {
        assert(l->wbuf_length >= l->wbuf_valid_length);
        
        /* In case the allocated buffer is too small, enlarge it. */
        if (l->wbuf_valid_length + len > l->wbuf_length) {
            size_t n = l->wbuf_valid_length+len;
            char *new = pa_xmalloc(n);
            if (l->wbuf) {
                memcpy(new, l->wbuf+l->wbuf_index, l->wbuf_valid_length);
                pa_xfree(l->wbuf);
            }
            l->wbuf = new;
            l->wbuf_length = n;
            l->wbuf_index = 0;
        } else if (l->wbuf_index + l->wbuf_valid_length + len > l->wbuf_length) {
            
            /* In case the allocated buffer fits, but the current index is too far from the start, move it to the front. */
            memmove(l->wbuf, l->wbuf+l->wbuf_index, l->wbuf_valid_length);
            l->wbuf_index = 0;
        }
        
        assert(l->wbuf_index + l->wbuf_valid_length + len <= l->wbuf_length);
        
        /* Append the new string */
        memcpy(l->wbuf + l->wbuf_index + l->wbuf_valid_length, c, len);
        l->wbuf_valid_length += len;

        l->mainloop->defer_enable(l->defer_event, 1);
    }

    pa_ioline_unref(l);
}

void pa_ioline_set_callback(struct pa_ioline*l, void (*callback)(struct pa_ioline*io, const char *s, void *userdata), void *userdata) {
    assert(l && l->ref >= 1);
    l->callback = callback;
    l->userdata = userdata;
}

static void failure(struct pa_ioline *l) {
    assert(l && l->ref >= 1 && !l->dead);

    pa_ioline_close(l);

    if (l->callback) {
        l->callback(l, NULL, l->userdata);
        l->callback = NULL;
    }
}

static void scan_for_lines(struct pa_ioline *l, size_t skip) {
    assert(l && l->ref >= 1 && skip < l->rbuf_valid_length);

    while (!l->dead && l->rbuf_valid_length > skip) {
        char *e, *p;
        size_t m;
        
        if (!(e = memchr(l->rbuf + l->rbuf_index + skip, '\n', l->rbuf_valid_length - skip)))
            break;

        *e = 0;
    
        p = l->rbuf + l->rbuf_index;
        m = strlen(p);

        l->rbuf_index += m+1;
        l->rbuf_valid_length -= m+1;

        /* A shortcut for the next time */
        if (l->rbuf_valid_length == 0)
            l->rbuf_index = 0;

        if (l->callback)
            l->callback(l, p, l->userdata);

        skip = 0;
    }

    /* If the buffer became too large and still no newline was found, drop it. */
    if (l->rbuf_valid_length >= BUFFER_LIMIT)
        l->rbuf_index = l->rbuf_valid_length = 0;
}

static int do_read(struct pa_ioline *l) {
    assert(l && l->ref >= 1);

    while (!l->dead && pa_iochannel_is_readable(l->io)) {
        ssize_t r;
        size_t len;

        len = l->rbuf_length - l->rbuf_index - l->rbuf_valid_length;
        
        /* Check if we have to enlarge the read buffer */
        if (len < READ_SIZE) {
            size_t n = l->rbuf_valid_length+READ_SIZE;
            
            if (n >= BUFFER_LIMIT)
                n = BUFFER_LIMIT;
            
            if (l->rbuf_length >= n) {
                /* The current buffer is large enough, let's just move the data to the front */
                if (l->rbuf_valid_length)
                    memmove(l->rbuf, l->rbuf+l->rbuf_index, l->rbuf_valid_length);
            } else {
                /* Enlarge the buffer */
                char *new = pa_xmalloc(n);
                if (l->rbuf_valid_length)
                    memcpy(new, l->rbuf+l->rbuf_index, l->rbuf_valid_length);
                pa_xfree(l->rbuf);
                l->rbuf = new;
                l->rbuf_length = n;
            }
            
            l->rbuf_index = 0;
        }
        
        len = l->rbuf_length - l->rbuf_index - l->rbuf_valid_length;
        
        assert(len >= READ_SIZE);
        
        /* Read some data */
        if ((r = pa_iochannel_read(l->io, l->rbuf+l->rbuf_index+l->rbuf_valid_length, len)) <= 0) {
            pa_log(__FILE__": read() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
            failure(l);
            return -1;
        }
        
        l->rbuf_valid_length += r;
        
        /* Look if a line has been terminated in the newly read data */
        scan_for_lines(l, l->rbuf_valid_length - r);
    }
        
    return 0;
}

/* Try to flush the buffer */
static int do_write(struct pa_ioline *l) {
    ssize_t r;
    assert(l && l->ref >= 1);

    while (!l->dead && pa_iochannel_is_writable(l->io) && l->wbuf_valid_length) {
        
        if ((r = pa_iochannel_write(l->io, l->wbuf+l->wbuf_index, l->wbuf_valid_length)) < 0) {
            pa_log(__FILE__": write() failed: %s\n", r < 0 ? strerror(errno) : "EOF");
            failure(l);
            return -1;
        }
        
        l->wbuf_index += r;
        l->wbuf_valid_length -= r;
        
        /* A shortcut for the next time */
        if (l->wbuf_valid_length == 0)
            l->wbuf_index = 0;
    }
        
    return 0;
}

/* Try to flush read/write data */
static void do_work(struct pa_ioline *l) {
    assert(l && l->ref >= 1);

    pa_ioline_ref(l);

    l->mainloop->defer_enable(l->defer_event, 0);
    
    if (!l->dead)
        do_write(l);

    if (!l->dead)
        do_read(l);

    if (l->defer_close && !l->wbuf_valid_length)
        failure(l);

    pa_ioline_unref(l);
}

static void io_callback(struct pa_iochannel*io, void *userdata) {
    struct pa_ioline *l = userdata;
    assert(io && l && l->ref >= 1);

    do_work(l);
}

static void defer_callback(struct pa_mainloop_api*m, struct pa_defer_event*e, void *userdata) {
    struct pa_ioline *l = userdata;
    assert(l && l->ref >= 1 && l->mainloop == m && l->defer_event == e);

    do_work(l);
}

void pa_ioline_defer_close(struct pa_ioline *l) {
    assert(l);

    l->defer_close = 1;

    if (!l->wbuf_valid_length)
        l->mainloop->defer_enable(l->defer_event, 1);
}

void pa_ioline_printf(struct pa_ioline *s, const char *format, ...) {
    char *t;
    va_list ap;

    
    va_start(ap, format);
    t = pa_vsprintf_malloc(format, ap);
    va_end(ap);

    pa_ioline_puts(s, t);
    pa_xfree(t);
}