]> code.delx.au - pulseaudio/blob - src/polypcore/iochannel.c
* modify pa_context_exit_daemon() to return a pa_operation object
[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 <polypcore/util.h>
34 #include <polypcore/socket-util.h>
35 #include <polypcore/xmalloc.h>
36
37 #include "iochannel.h"
38
39 struct pa_iochannel {
40 int ifd, ofd;
41 pa_mainloop_api* mainloop;
42
43 pa_iochannel_cb_t callback;
44 void*userdata;
45
46 int readable;
47 int writable;
48 int hungup;
49
50 int no_close;
51
52 pa_io_event* input_event, *output_event;
53 };
54
55 static void enable_mainloop_sources(pa_iochannel *io) {
56 assert(io);
57
58 if (io->input_event == io->output_event && io->input_event) {
59 pa_io_event_flags_t f = PA_IO_EVENT_NULL;
60 assert(io->input_event);
61
62 if (!pa_iochannel_is_readable(io))
63 f |= PA_IO_EVENT_INPUT;
64 if (!pa_iochannel_is_writable(io))
65 f |= PA_IO_EVENT_OUTPUT;
66
67 io->mainloop->io_enable(io->input_event, f);
68 } else {
69 if (io->input_event)
70 io->mainloop->io_enable(io->input_event, pa_iochannel_is_readable(io) ? PA_IO_EVENT_NULL : PA_IO_EVENT_INPUT);
71 if (io->output_event)
72 io->mainloop->io_enable(io->output_event, pa_iochannel_is_writable(io) ? PA_IO_EVENT_NULL : PA_IO_EVENT_OUTPUT);
73 }
74 }
75
76 static void callback(pa_mainloop_api* m, pa_io_event *e, int fd, pa_io_event_flags_t f, void *userdata) {
77 pa_iochannel *io = userdata;
78 int changed = 0;
79
80 assert(m);
81 assert(e);
82 assert(fd >= 0);
83 assert(userdata);
84
85 if ((f & (PA_IO_EVENT_HANGUP|PA_IO_EVENT_ERROR)) & !io->hungup) {
86 io->hungup = 1;
87 changed = 1;
88 }
89
90 if ((f & PA_IO_EVENT_INPUT) && !io->readable) {
91 io->readable = 1;
92 changed = 1;
93 assert(e == io->input_event);
94 }
95
96 if ((f & PA_IO_EVENT_OUTPUT) && !io->writable) {
97 io->writable = 1;
98 changed = 1;
99 assert(e == io->output_event);
100 }
101
102 if (changed) {
103 enable_mainloop_sources(io);
104
105 if (io->callback)
106 io->callback(io, io->userdata);
107 }
108 }
109
110 pa_iochannel* pa_iochannel_new(pa_mainloop_api*m, int ifd, int ofd) {
111 pa_iochannel *io;
112
113 assert(m);
114 assert(ifd >= 0 || ofd >= 0);
115
116 io = pa_xnew(pa_iochannel, 1);
117 io->ifd = ifd;
118 io->ofd = ofd;
119 io->mainloop = m;
120
121 io->userdata = NULL;
122 io->callback = NULL;
123 io->readable = 0;
124 io->writable = 0;
125 io->hungup = 0;
126 io->no_close = 0;
127
128 io->input_event = io->output_event = NULL;
129
130 if (ifd == ofd) {
131 assert(ifd >= 0);
132 pa_make_nonblock_fd(io->ifd);
133 io->input_event = io->output_event = m->io_new(m, ifd, PA_IO_EVENT_INPUT|PA_IO_EVENT_OUTPUT, callback, io);
134 } else {
135
136 if (ifd >= 0) {
137 pa_make_nonblock_fd(io->ifd);
138 io->input_event = m->io_new(m, ifd, PA_IO_EVENT_INPUT, callback, io);
139 }
140
141 if (ofd >= 0) {
142 pa_make_nonblock_fd(io->ofd);
143 io->output_event = m->io_new(m, ofd, PA_IO_EVENT_OUTPUT, callback, io);
144 }
145 }
146
147 return io;
148 }
149
150 void pa_iochannel_free(pa_iochannel*io) {
151 assert(io);
152
153 if (io->input_event)
154 io->mainloop->io_free(io->input_event);
155
156 if (io->output_event && (io->output_event != io->input_event))
157 io->mainloop->io_free(io->output_event);
158
159 if (!io->no_close) {
160 if (io->ifd >= 0)
161
162 close(io->ifd);
163 if (io->ofd >= 0 && io->ofd != io->ifd)
164 close(io->ofd);
165 }
166
167 pa_xfree(io);
168 }
169
170 int pa_iochannel_is_readable(pa_iochannel*io) {
171 assert(io);
172
173 return io->readable || io->hungup;
174 }
175
176 int pa_iochannel_is_writable(pa_iochannel*io) {
177 assert(io);
178
179 return io->writable && !io->hungup;
180 }
181
182 int pa_iochannel_is_hungup(pa_iochannel*io) {
183 assert(io);
184
185 return io->hungup;
186 }
187
188 ssize_t pa_iochannel_write(pa_iochannel*io, const void*data, size_t l) {
189 ssize_t r;
190
191 assert(io);
192 assert(data);
193 assert(l);
194 assert(io->ofd >= 0);
195
196 #ifdef OS_IS_WIN32
197 r = send(io->ofd, data, l, 0);
198 if (r < 0) {
199 if (WSAGetLastError() != WSAENOTSOCK) {
200 errno = WSAGetLastError();
201 return r;
202 }
203 }
204
205 if (r < 0)
206 #endif
207 r = write(io->ofd, data, l);
208
209 if (r >= 0) {
210 io->writable = 0;
211 enable_mainloop_sources(io);
212 }
213
214 return r;
215 }
216
217 ssize_t pa_iochannel_read(pa_iochannel*io, void*data, size_t l) {
218 ssize_t r;
219
220 assert(io);
221 assert(data);
222 assert(io->ifd >= 0);
223
224 #ifdef OS_IS_WIN32
225 r = recv(io->ifd, data, l, 0);
226 if (r < 0) {
227 if (WSAGetLastError() != WSAENOTSOCK) {
228 errno = WSAGetLastError();
229 return r;
230 }
231 }
232
233 if (r < 0)
234 #endif
235 r = read(io->ifd, data, l);
236
237 if (r >= 0) {
238 io->readable = 0;
239 enable_mainloop_sources(io);
240 }
241
242 return r;
243 }
244
245 void pa_iochannel_set_callback(pa_iochannel*io, pa_iochannel_cb_t _callback, void *userdata) {
246 assert(io);
247
248 io->callback = _callback;
249 io->userdata = userdata;
250 }
251
252 void pa_iochannel_set_noclose(pa_iochannel*io, int b) {
253 assert(io);
254
255 io->no_close = b;
256 }
257
258 void pa_iochannel_socket_peer_to_string(pa_iochannel*io, char*s, size_t l) {
259 assert(io);
260 assert(s);
261 assert(l);
262
263 pa_socket_peer_to_string(io->ifd, s, l);
264 }
265
266 int pa_iochannel_socket_set_rcvbuf(pa_iochannel *io, size_t l) {
267 assert(io);
268
269 return pa_socket_set_rcvbuf(io->ifd, l);
270 }
271
272 int pa_iochannel_socket_set_sndbuf(pa_iochannel *io, size_t l) {
273 assert(io);
274
275 return pa_socket_set_sndbuf(io->ofd, l);
276 }
277
278 pa_mainloop_api* pa_iochannel_get_mainloop_api(pa_iochannel *io) {
279 assert(io);
280
281 return io->mainloop;
282 }