summaryrefslogtreecommitdiffstats
path: root/polyp/iochannel.c
blob: 72bdac20e96820e956fb126c52c25209e1cc2189 (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
/* $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 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 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 <stdlib.h>
#include <assert.h>
#include <fcntl.h>
#include <unistd.h>

#include "iochannel.h"
#include "util.h"
#include "socket-util.h"
#include "xmalloc.h"

struct pa_iochannel {
    int ifd, ofd;
    struct pa_mainloop_api* mainloop;

    void (*callback)(struct pa_iochannel*io, void *userdata);
    void*userdata;
    
    int readable;
    int writable;
    int hungup;
    
    int no_close;

    struct pa_io_event* input_event, *output_event;
};

static void enable_mainloop_sources(struct pa_iochannel *io) {
    assert(io);

    if (io->input_event == io->output_event) {
        enum pa_io_event_flags f = PA_IO_EVENT_NULL;
        assert(io->input_event);
        
        if (!io->readable)
            f |= PA_IO_EVENT_INPUT;
        if (!io->writable)
            f |= PA_IO_EVENT_OUTPUT;

        io->mainloop->io_enable(io->input_event, f);
    } else {
        if (io->input_event)
            io->mainloop->io_enable(io->input_event, io->readable ? PA_IO_EVENT_NULL : PA_IO_EVENT_INPUT);
        if (io->output_event)
            io->mainloop->io_enable(io->output_event, io->writable ? PA_IO_EVENT_NULL : PA_IO_EVENT_OUTPUT);
    }
}

static void callback(struct pa_mainloop_api* m, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata) {
    struct pa_iochannel *io = userdata;
    int changed = 0;
    assert(m && e && fd >= 0 && userdata);

    if ((f & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR)) && !io->hungup) {
        io->hungup = 1;
        changed = 1;
    }
    
    if ((f & PA_IO_EVENT_INPUT) && !io->readable) {
        io->readable = 1;
        changed = 1;
        assert(e == io->input_event);
    }
    
    if ((f & PA_IO_EVENT_OUTPUT) && !io->writable) {
        io->writable = 1;
        changed = 1;
        assert(e == io->output_event);
    }

    if (changed) {
        enable_mainloop_sources(io);
        
        if (io->callback)
            io->callback(io, io->userdata);
    }
}

struct pa_iochannel* pa_iochannel_new(struct pa_mainloop_api*m, int ifd, int ofd) {
    struct pa_iochannel *io;
    assert(m && (ifd >= 0 || ofd >= 0));

    io = pa_xmalloc(sizeof(struct pa_iochannel));
    io->ifd = ifd;
    io->ofd = ofd;
    io->mainloop = m;

    io->userdata = NULL;
    io->callback = NULL;
    io->readable = 0;
    io->writable = 0;
    io->hungup = 0;
    io->no_close = 0;

    io->input_event = io->output_event = NULL;

    if (ifd == ofd) {
        assert(ifd >= 0);
        pa_make_nonblock_fd(io->ifd);
        io->input_event = io->output_event = m->io_new(m, ifd, PA_IO_EVENT_INPUT|PA_IO_EVENT_OUTPUT, callback, io);
    } else {

        if (ifd >= 0) {
            pa_make_nonblock_fd(io->ifd);
            io->input_event = m->io_new(m, ifd, PA_IO_EVENT_INPUT, callback, io);
        }

        if (ofd >= 0) {
            pa_make_nonblock_fd(io->ofd);
            io->output_event = m->io_new(m, ofd, PA_IO_EVENT_OUTPUT, callback, io);
        }
    }

    return io;
}

void pa_iochannel_free(struct pa_iochannel*io) {
    assert(io);

    if (io->input_event)
        io->mainloop->io_free(io->input_event);
    if (io->output_event && (io->output_event != io->input_event))
        io->mainloop->io_free(io->output_event);

    if (!io->no_close) {
        if (io->ifd >= 0)
            close(io->ifd);
        if (io->ofd >= 0 && io->ofd != io->ifd)
            close(io->ofd);
    }
    
    pa_xfree(io);
}

int pa_iochannel_is_readable(struct pa_iochannel*io) {
    assert(io);
    return io->readable;
}

int pa_iochannel_is_writable(struct pa_iochannel*io) {
    assert(io);
    return io->writable;
}

int pa_iochannel_is_hungup(struct pa_iochannel*io) {
    assert(io);
    return io->hungup;
}

ssize_t pa_iochannel_write(struct pa_iochannel*io, const void*data, size_t l) {
    ssize_t r;
    assert(io);
    assert(data);
    assert(l);
    assert(io->ofd >= 0);

    
    assert(io && data && l && io->ofd >= 0);


    
    if ((r = write(io->ofd, data, l)) >= 0) {
        io->writable = 0;
        enable_mainloop_sources(io);
    }

    return r;
}

ssize_t pa_iochannel_read(struct pa_iochannel*io, void*data, size_t l) {
    ssize_t r;
    
    assert(io && data && io->ifd >= 0);
    
    if ((r = read(io->ifd, data, l)) >= 0) {
        io->readable = 0;
        enable_mainloop_sources(io);
    }

    return r;
}

void pa_iochannel_set_callback(struct pa_iochannel*io, void (*callback)(struct pa_iochannel*io, void *userdata), void *userdata) {
    assert(io);
    io->callback = callback;
    io->userdata = userdata;
}

void pa_iochannel_set_noclose(struct pa_iochannel*io, int b) {
    assert(io);
    io->no_close = b;
}

void pa_iochannel_socket_peer_to_string(struct pa_iochannel*io, char*s, size_t l) {
    assert(io && s && l);
    pa_socket_peer_to_string(io->ifd, s, l);
}

int pa_iochannel_socket_set_rcvbuf(struct pa_iochannel *io, size_t l) {
    assert(io);
    return pa_socket_set_rcvbuf(io->ifd, l);
}

int pa_iochannel_socket_set_sndbuf(struct pa_iochannel *io, size_t l) {
    assert(io);
    return pa_socket_set_sndbuf(io->ofd, l);
}

void pa_iochannel_force_unreadable(struct pa_iochannel *io) {
    assert(io);
    io->readable = 0;
    enable_mainloop_sources(io);
}

void pa_iochannel_force_unwritable(struct pa_iochannel *io) {
    io->writable = 0;
    enable_mainloop_sources(io);
}