4 This file is part of polypaudio.
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.
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.
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
33 #include <polypcore/util.h>
34 #include <polypcore/socket-util.h>
35 #include <polypcore/xmalloc.h>
37 #include "iochannel.h"
41 pa_mainloop_api
* mainloop
;
43 pa_iochannel_cb_t callback
;
52 pa_io_event
* input_event
, *output_event
;
55 static void enable_mainloop_sources(pa_iochannel
*io
) {
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
);
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
;
67 io
->mainloop
->io_enable(io
->input_event
, f
);
70 io
->mainloop
->io_enable(io
->input_event
, pa_iochannel_is_readable(io
) ? PA_IO_EVENT_NULL
: PA_IO_EVENT_INPUT
);
72 io
->mainloop
->io_enable(io
->output_event
, pa_iochannel_is_writable(io
) ? PA_IO_EVENT_NULL
: PA_IO_EVENT_OUTPUT
);
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
;
85 if ((f
& (PA_IO_EVENT_HANGUP
|PA_IO_EVENT_ERROR
)) & !io
->hungup
) {
90 if ((f
& PA_IO_EVENT_INPUT
) && !io
->readable
) {
93 assert(e
== io
->input_event
);
96 if ((f
& PA_IO_EVENT_OUTPUT
) && !io
->writable
) {
99 assert(e
== io
->output_event
);
103 enable_mainloop_sources(io
);
106 io
->callback(io
, io
->userdata
);
110 pa_iochannel
* pa_iochannel_new(pa_mainloop_api
*m
, int ifd
, int ofd
) {
114 assert(ifd
>= 0 || ofd
>= 0);
116 io
= pa_xnew(pa_iochannel
, 1);
128 io
->input_event
= io
->output_event
= NULL
;
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
);
137 pa_make_nonblock_fd(io
->ifd
);
138 io
->input_event
= m
->io_new(m
, ifd
, PA_IO_EVENT_INPUT
, callback
, io
);
142 pa_make_nonblock_fd(io
->ofd
);
143 io
->output_event
= m
->io_new(m
, ofd
, PA_IO_EVENT_OUTPUT
, callback
, io
);
150 void pa_iochannel_free(pa_iochannel
*io
) {
154 io
->mainloop
->io_free(io
->input_event
);
156 if (io
->output_event
&& (io
->output_event
!= io
->input_event
))
157 io
->mainloop
->io_free(io
->output_event
);
163 if (io
->ofd
>= 0 && io
->ofd
!= io
->ifd
)
170 int pa_iochannel_is_readable(pa_iochannel
*io
) {
173 return io
->readable
|| io
->hungup
;
176 int pa_iochannel_is_writable(pa_iochannel
*io
) {
179 return io
->writable
&& !io
->hungup
;
182 int pa_iochannel_is_hungup(pa_iochannel
*io
) {
188 ssize_t
pa_iochannel_write(pa_iochannel
*io
, const void*data
, size_t l
) {
194 assert(io
->ofd
>= 0);
197 r
= send(io
->ofd
, data
, l
, 0);
199 if (WSAGetLastError() != WSAENOTSOCK
) {
200 errno
= WSAGetLastError();
207 r
= write(io
->ofd
, data
, l
);
211 enable_mainloop_sources(io
);
217 ssize_t
pa_iochannel_read(pa_iochannel
*io
, void*data
, size_t l
) {
222 assert(io
->ifd
>= 0);
225 r
= recv(io
->ifd
, data
, l
, 0);
227 if (WSAGetLastError() != WSAENOTSOCK
) {
228 errno
= WSAGetLastError();
235 r
= read(io
->ifd
, data
, l
);
239 enable_mainloop_sources(io
);
245 void pa_iochannel_set_callback(pa_iochannel
*io
, pa_iochannel_cb_t _callback
, void *userdata
) {
248 io
->callback
= _callback
;
249 io
->userdata
= userdata
;
252 void pa_iochannel_set_noclose(pa_iochannel
*io
, int b
) {
258 void pa_iochannel_socket_peer_to_string(pa_iochannel
*io
, char*s
, size_t l
) {
263 pa_socket_peer_to_string(io
->ifd
, s
, l
);
266 int pa_iochannel_socket_set_rcvbuf(pa_iochannel
*io
, size_t l
) {
269 return pa_socket_set_rcvbuf(io
->ifd
, l
);
272 int pa_iochannel_socket_set_sndbuf(pa_iochannel
*io
, size_t l
) {
275 return pa_socket_set_sndbuf(io
->ofd
, l
);
278 pa_mainloop_api
* pa_iochannel_get_mainloop_api(pa_iochannel
*io
) {