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 "iochannel.h"
35 #include "socket-util.h"
40 pa_mainloop_api
* mainloop
;
42 pa_iochannel_callback_t callback
;
51 pa_io_event
* input_event
, *output_event
;
54 static void enable_mainloop_sources(pa_iochannel
*io
) {
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
);
62 f
|= PA_IO_EVENT_INPUT
;
64 f
|= PA_IO_EVENT_OUTPUT
;
66 io
->mainloop
->io_enable(io
->input_event
, f
);
69 io
->mainloop
->io_enable(io
->input_event
, io
->readable
? PA_IO_EVENT_NULL
: PA_IO_EVENT_INPUT
);
71 io
->mainloop
->io_enable(io
->output_event
, io
->writable
? PA_IO_EVENT_NULL
: PA_IO_EVENT_OUTPUT
);
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
;
84 if ((f
& (PA_IO_EVENT_HANGUP
|PA_IO_EVENT_ERROR
)) && !io
->hungup
) {
88 if (e
== io
->input_event
) {
89 io
->mainloop
->io_free(io
->input_event
);
90 io
->input_event
= NULL
;
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
;
100 if ((f
& PA_IO_EVENT_INPUT
) && !io
->readable
) {
103 assert(e
== io
->input_event
);
106 if ((f
& PA_IO_EVENT_OUTPUT
) && !io
->writable
) {
109 assert(e
== io
->output_event
);
114 enable_mainloop_sources(io
);
117 io
->callback(io
, io
->userdata
);
121 pa_iochannel
* pa_iochannel_new(pa_mainloop_api
*m
, int ifd
, int ofd
) {
125 assert(ifd
>= 0 || ofd
>= 0);
127 io
= pa_xnew(pa_iochannel
, 1);
139 io
->input_event
= io
->output_event
= NULL
;
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
);
148 pa_make_nonblock_fd(io
->ifd
);
149 io
->input_event
= m
->io_new(m
, ifd
, PA_IO_EVENT_INPUT
, callback
, io
);
153 pa_make_nonblock_fd(io
->ofd
);
154 io
->output_event
= m
->io_new(m
, ofd
, PA_IO_EVENT_OUTPUT
, callback
, io
);
161 void pa_iochannel_free(pa_iochannel
*io
) {
165 io
->mainloop
->io_free(io
->input_event
);
167 if (io
->output_event
&& (io
->output_event
!= io
->input_event
))
168 io
->mainloop
->io_free(io
->output_event
);
174 if (io
->ofd
>= 0 && io
->ofd
!= io
->ifd
)
181 int pa_iochannel_is_readable(pa_iochannel
*io
) {
184 return io
->readable
|| io
->hungup
;
187 int pa_iochannel_is_writable(pa_iochannel
*io
) {
190 return io
->writable
&& !io
->hungup
;
193 int pa_iochannel_is_hungup(pa_iochannel
*io
) {
199 ssize_t
pa_iochannel_write(pa_iochannel
*io
, const void*data
, size_t l
) {
205 assert(io
->ofd
>= 0);
208 r
= send(io
->ofd
, data
, l
, 0);
210 if (WSAGetLastError() != WSAENOTSOCK
) {
211 errno
= WSAGetLastError();
218 r
= write(io
->ofd
, data
, l
);
221 enable_mainloop_sources(io
);
227 ssize_t
pa_iochannel_read(pa_iochannel
*io
, void*data
, size_t l
) {
232 assert(io
->ifd
>= 0);
235 r
= recv(io
->ifd
, data
, l
, 0);
237 if (WSAGetLastError() != WSAENOTSOCK
) {
238 errno
= WSAGetLastError();
245 r
= read(io
->ifd
, data
, l
);
249 enable_mainloop_sources(io
);
255 void pa_iochannel_set_callback(pa_iochannel
*io
, pa_iochannel_callback_t _callback
, void *userdata
) {
258 io
->callback
= _callback
;
259 io
->userdata
= userdata
;
262 void pa_iochannel_set_noclose(pa_iochannel
*io
, int b
) {
268 void pa_iochannel_socket_peer_to_string(pa_iochannel
*io
, char*s
, size_t l
) {
273 pa_socket_peer_to_string(io
->ifd
, s
, l
);
276 int pa_iochannel_socket_set_rcvbuf(pa_iochannel
*io
, size_t l
) {
279 return pa_socket_set_rcvbuf(io
->ifd
, l
);
282 int pa_iochannel_socket_set_sndbuf(pa_iochannel
*io
, size_t l
) {
285 return pa_socket_set_sndbuf(io
->ofd
, l
);
288 pa_mainloop_api
* pa_iochannel_get_mainloop_api(pa_iochannel
*io
) {