]> code.delx.au - pulseaudio/blob - src/polypcore/iochannel.c
Cleaned up the includes after the restructuring. Indicate which headers are
[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_callback_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 (!io->readable)
63 f |= PA_IO_EVENT_INPUT;
64 if (!io->writable)
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, io->readable ? PA_IO_EVENT_NULL : PA_IO_EVENT_INPUT);
71 if (io->output_event)
72 io->mainloop->io_enable(io->output_event, io->writable ? 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 if (e == io->input_event) {
90 io->mainloop->io_free(io->input_event);
91 io->input_event = NULL;
92
93 if (io->output_event == e)
94 io->output_event = NULL;
95 } else if (e == io->output_event) {
96 io->mainloop->io_free(io->output_event);
97 io->output_event = NULL;
98 }
99 } else {
100
101 if ((f & PA_IO_EVENT_INPUT) && !io->readable) {
102 io->readable = 1;
103 changed = 1;
104 assert(e == io->input_event);
105 }
106
107 if ((f & PA_IO_EVENT_OUTPUT) && !io->writable) {
108 io->writable = 1;
109 changed = 1;
110 assert(e == io->output_event);
111 }
112 }
113
114 if (changed) {
115 enable_mainloop_sources(io);
116
117 if (io->callback)
118 io->callback(io, io->userdata);
119 }
120 }
121
122 pa_iochannel* pa_iochannel_new(pa_mainloop_api*m, int ifd, int ofd) {
123 pa_iochannel *io;
124
125 assert(m);
126 assert(ifd >= 0 || ofd >= 0);
127
128 io = pa_xnew(pa_iochannel, 1);
129 io->ifd = ifd;
130 io->ofd = ofd;
131 io->mainloop = m;
132
133 io->userdata = NULL;
134 io->callback = NULL;
135 io->readable = 0;
136 io->writable = 0;
137 io->hungup = 0;
138 io->no_close = 0;
139
140 io->input_event = io->output_event = NULL;
141
142 if (ifd == ofd) {
143 assert(ifd >= 0);
144 pa_make_nonblock_fd(io->ifd);
145 io->input_event = io->output_event = m->io_new(m, ifd, PA_IO_EVENT_INPUT|PA_IO_EVENT_OUTPUT, callback, io);
146 } else {
147
148 if (ifd >= 0) {
149 pa_make_nonblock_fd(io->ifd);
150 io->input_event = m->io_new(m, ifd, PA_IO_EVENT_INPUT, callback, io);
151 }
152
153 if (ofd >= 0) {
154 pa_make_nonblock_fd(io->ofd);
155 io->output_event = m->io_new(m, ofd, PA_IO_EVENT_OUTPUT, callback, io);
156 }
157 }
158
159 return io;
160 }
161
162 void pa_iochannel_free(pa_iochannel*io) {
163 assert(io);
164
165 if (io->input_event)
166 io->mainloop->io_free(io->input_event);
167
168 if (io->output_event && (io->output_event != io->input_event))
169 io->mainloop->io_free(io->output_event);
170
171 if (!io->no_close) {
172 if (io->ifd >= 0)
173
174 close(io->ifd);
175 if (io->ofd >= 0 && io->ofd != io->ifd)
176 close(io->ofd);
177 }
178
179 pa_xfree(io);
180 }
181
182 int pa_iochannel_is_readable(pa_iochannel*io) {
183 assert(io);
184
185 return io->readable || io->hungup;
186 }
187
188 int pa_iochannel_is_writable(pa_iochannel*io) {
189 assert(io);
190
191 return io->writable && !io->hungup;
192 }
193
194 int pa_iochannel_is_hungup(pa_iochannel*io) {
195 assert(io);
196
197 return io->hungup;
198 }
199
200 ssize_t pa_iochannel_write(pa_iochannel*io, const void*data, size_t l) {
201 ssize_t r;
202
203 assert(io);
204 assert(data);
205 assert(l);
206 assert(io->ofd >= 0);
207
208 #ifdef OS_IS_WIN32
209 r = send(io->ofd, data, l, 0);
210 if (r < 0) {
211 if (WSAGetLastError() != WSAENOTSOCK) {
212 errno = WSAGetLastError();
213 return r;
214 }
215 }
216
217 if (r < 0)
218 #endif
219 r = write(io->ofd, data, l);
220 if (r >= 0) {
221 io->writable = 0;
222 enable_mainloop_sources(io);
223 }
224
225 return r;
226 }
227
228 ssize_t pa_iochannel_read(pa_iochannel*io, void*data, size_t l) {
229 ssize_t r;
230
231 assert(io);
232 assert(data);
233 assert(io->ifd >= 0);
234
235 #ifdef OS_IS_WIN32
236 r = recv(io->ifd, data, l, 0);
237 if (r < 0) {
238 if (WSAGetLastError() != WSAENOTSOCK) {
239 errno = WSAGetLastError();
240 return r;
241 }
242 }
243
244 if (r < 0)
245 #endif
246 r = read(io->ifd, data, l);
247
248 if (r >= 0) {
249 io->readable = 0;
250 enable_mainloop_sources(io);
251 }
252
253 return r;
254 }
255
256 void pa_iochannel_set_callback(pa_iochannel*io, pa_iochannel_callback_t _callback, void *userdata) {
257 assert(io);
258
259 io->callback = _callback;
260 io->userdata = userdata;
261 }
262
263 void pa_iochannel_set_noclose(pa_iochannel*io, int b) {
264 assert(io);
265
266 io->no_close = b;
267 }
268
269 void pa_iochannel_socket_peer_to_string(pa_iochannel*io, char*s, size_t l) {
270 assert(io);
271 assert(s);
272 assert(l);
273
274 pa_socket_peer_to_string(io->ifd, s, l);
275 }
276
277 int pa_iochannel_socket_set_rcvbuf(pa_iochannel *io, size_t l) {
278 assert(io);
279
280 return pa_socket_set_rcvbuf(io->ifd, l);
281 }
282
283 int pa_iochannel_socket_set_sndbuf(pa_iochannel *io, size_t l) {
284 assert(io);
285
286 return pa_socket_set_sndbuf(io->ofd, l);
287 }
288
289 pa_mainloop_api* pa_iochannel_get_mainloop_api(pa_iochannel *io) {
290 assert(io);
291
292 return io->mainloop;
293 }