]> code.delx.au - pulseaudio/blob - src/polypcore/iochannel.c
273d47e0da085bef2e83d6fb9fb3b71293b6fc75
[pulseaudio] / src / polypcore / iochannel.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
10
11 polypaudio 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 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <assert.h>
28 #include <fcntl.h>
29 #include <unistd.h>
30
31 #include "winsock.h"
32
33 #include "iochannel.h"
34 #include "util.h"
35 #include "socket-util.h"
36 #include "xmalloc.h"
37
38 struct pa_iochannel {
39 int ifd, ofd;
40 pa_mainloop_api* mainloop;
41
42 pa_iochannel_callback_t callback;
43 void*userdata;
44
45 int readable;
46 int writable;
47 int hungup;
48
49 int no_close;
50
51 pa_io_event* input_event, *output_event;
52 };
53
54 static void enable_mainloop_sources(pa_iochannel *io) {
55 assert(io);
56
57 if (io->input_event == io->output_event && io->input_event) {
58 pa_io_event_flags_t f = PA_IO_EVENT_NULL;
59 assert(io->input_event);
60
61 if (!io->readable)
62 f |= PA_IO_EVENT_INPUT;
63 if (!io->writable)
64 f |= PA_IO_EVENT_OUTPUT;
65
66 io->mainloop->io_enable(io->input_event, f);
67 } else {
68 if (io->input_event)
69 io->mainloop->io_enable(io->input_event, io->readable ? PA_IO_EVENT_NULL : PA_IO_EVENT_INPUT);
70 if (io->output_event)
71 io->mainloop->io_enable(io->output_event, io->writable ? PA_IO_EVENT_NULL : PA_IO_EVENT_OUTPUT);
72 }
73 }
74
75 static void callback(pa_mainloop_api* m, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
76 pa_iochannel *io = userdata;
77 int changed = 0;
78
79 assert(m);
80 assert(e);
81 assert(fd >= 0);
82 assert(userdata);
83
84 if ((f & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR)) && !io->hungup) {
85 io->hungup = 1;
86 changed = 1;
87
88 if (e == io->input_event) {
89 io->mainloop->io_free(io->input_event);
90 io->input_event = NULL;
91
92 if (io->output_event == e)
93 io->output_event = NULL;
94 } else if (e == io->output_event) {
95 io->mainloop->io_free(io->output_event);
96 io->output_event = NULL;
97 }
98 } else {
99
100 if ((f & PA_IO_EVENT_INPUT) && !io->readable) {
101 io->readable = 1;
102 changed = 1;
103 assert(e == io->input_event);
104 }
105
106 if ((f & PA_IO_EVENT_OUTPUT) && !io->writable) {
107 io->writable = 1;
108 changed = 1;
109 assert(e == io->output_event);
110 }
111 }
112
113 if (changed) {
114 enable_mainloop_sources(io);
115
116 if (io->callback)
117 io->callback(io, io->userdata);
118 }
119 }
120
121 pa_iochannel* pa_iochannel_new(pa_mainloop_api*m, int ifd, int ofd) {
122 pa_iochannel *io;
123
124 assert(m);
125 assert(ifd >= 0 || ofd >= 0);
126
127 io = pa_xnew(pa_iochannel, 1);
128 io->ifd = ifd;
129 io->ofd = ofd;
130 io->mainloop = m;
131
132 io->userdata = NULL;
133 io->callback = NULL;
134 io->readable = 0;
135 io->writable = 0;
136 io->hungup = 0;
137 io->no_close = 0;
138
139 io->input_event = io->output_event = NULL;
140
141 if (ifd == ofd) {
142 assert(ifd >= 0);
143 pa_make_nonblock_fd(io->ifd);
144 io->input_event = io->output_event = m->io_new(m, ifd, PA_IO_EVENT_INPUT|PA_IO_EVENT_OUTPUT, callback, io);
145 } else {
146
147 if (ifd >= 0) {
148 pa_make_nonblock_fd(io->ifd);
149 io->input_event = m->io_new(m, ifd, PA_IO_EVENT_INPUT, callback, io);
150 }
151
152 if (ofd >= 0) {
153 pa_make_nonblock_fd(io->ofd);
154 io->output_event = m->io_new(m, ofd, PA_IO_EVENT_OUTPUT, callback, io);
155 }
156 }
157
158 return io;
159 }
160
161 void pa_iochannel_free(pa_iochannel*io) {
162 assert(io);
163
164 if (io->input_event)
165 io->mainloop->io_free(io->input_event);
166
167 if (io->output_event && (io->output_event != io->input_event))
168 io->mainloop->io_free(io->output_event);
169
170 if (!io->no_close) {
171 if (io->ifd >= 0)
172
173 close(io->ifd);
174 if (io->ofd >= 0 && io->ofd != io->ifd)
175 close(io->ofd);
176 }
177
178 pa_xfree(io);
179 }
180
181 int pa_iochannel_is_readable(pa_iochannel*io) {
182 assert(io);
183
184 return io->readable || io->hungup;
185 }
186
187 int pa_iochannel_is_writable(pa_iochannel*io) {
188 assert(io);
189
190 return io->writable && !io->hungup;
191 }
192
193 int pa_iochannel_is_hungup(pa_iochannel*io) {
194 assert(io);
195
196 return io->hungup;
197 }
198
199 ssize_t pa_iochannel_write(pa_iochannel*io, const void*data, size_t l) {
200 ssize_t r;
201
202 assert(io);
203 assert(data);
204 assert(l);
205 assert(io->ofd >= 0);
206
207 #ifdef OS_IS_WIN32
208 r = send(io->ofd, data, l, 0);
209 if (r < 0) {
210 if (WSAGetLastError() != WSAENOTSOCK) {
211 errno = WSAGetLastError();
212 return r;
213 }
214 }
215
216 if (r < 0)
217 #endif
218 r = write(io->ofd, data, l);
219 if (r >= 0) {
220 io->writable = 0;
221 enable_mainloop_sources(io);
222 }
223
224 return r;
225 }
226
227 ssize_t pa_iochannel_read(pa_iochannel*io, void*data, size_t l) {
228 ssize_t r;
229
230 assert(io);
231 assert(data);
232 assert(io->ifd >= 0);
233
234 #ifdef OS_IS_WIN32
235 r = recv(io->ifd, data, l, 0);
236 if (r < 0) {
237 if (WSAGetLastError() != WSAENOTSOCK) {
238 errno = WSAGetLastError();
239 return r;
240 }
241 }
242
243 if (r < 0)
244 #endif
245 r = read(io->ifd, data, l);
246
247 if (r >= 0) {
248 io->readable = 0;
249 enable_mainloop_sources(io);
250 }
251
252 return r;
253 }
254
255 void pa_iochannel_set_callback(pa_iochannel*io, pa_iochannel_callback_t _callback, void *userdata) {
256 assert(io);
257
258 io->callback = _callback;
259 io->userdata = userdata;
260 }
261
262 void pa_iochannel_set_noclose(pa_iochannel*io, int b) {
263 assert(io);
264
265 io->no_close = b;
266 }
267
268 void pa_iochannel_socket_peer_to_string(pa_iochannel*io, char*s, size_t l) {
269 assert(io);
270 assert(s);
271 assert(l);
272
273 pa_socket_peer_to_string(io->ifd, s, l);
274 }
275
276 int pa_iochannel_socket_set_rcvbuf(pa_iochannel *io, size_t l) {
277 assert(io);
278
279 return pa_socket_set_rcvbuf(io->ifd, l);
280 }
281
282 int pa_iochannel_socket_set_sndbuf(pa_iochannel *io, size_t l) {
283 assert(io);
284
285 return pa_socket_set_sndbuf(io->ofd, l);
286 }
287
288 pa_mainloop_api* pa_iochannel_get_mainloop_api(pa_iochannel *io) {
289 assert(io);
290
291 return io->mainloop;
292 }