]>
code.delx.au - pulseaudio/blob - polyp/socket-client.c
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 General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 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 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
36 #include "socket-client.h"
37 #include "socket-util.h"
41 struct pa_socket_client
{
43 struct pa_mainloop_api
*mainloop
;
45 struct pa_io_event
*io_event
;
46 struct pa_defer_event
*defer_event
;
47 void (*callback
)(struct pa_socket_client
*c
, struct pa_iochannel
*io
, void *userdata
);
51 static struct pa_socket_client
*pa_socket_client_new(struct pa_mainloop_api
*m
) {
52 struct pa_socket_client
*c
;
55 c
= pa_xmalloc(sizeof(struct pa_socket_client
));
60 c
->defer_event
= NULL
;
66 static void do_call(struct pa_socket_client
*c
) {
67 struct pa_iochannel
*io
= NULL
;
70 assert(c
&& c
->callback
);
72 pa_socket_client_ref(c
);
74 lerror
= sizeof(error
);
75 if (getsockopt(c
->fd
, SOL_SOCKET
, SO_ERROR
, &error
, &lerror
) < 0) {
76 fprintf(stderr
, "getsockopt(): %s\n", strerror(errno
));
80 if (lerror
!= sizeof(error
)) {
81 fprintf(stderr
, "getsocktop() returned invalid size.\n");
86 fprintf(stderr
, "connect(): %s\n", strerror(error
));
90 io
= pa_iochannel_new(c
->mainloop
, c
->fd
, c
->fd
);
99 c
->callback(c
, io
, c
->userdata
);
101 pa_socket_client_unref(c
);
104 static void connect_fixed_cb(struct pa_mainloop_api
*m
, struct pa_defer_event
*e
, void *userdata
) {
105 struct pa_socket_client
*c
= userdata
;
106 assert(m
&& c
&& c
->defer_event
== e
);
107 m
->defer_free(c
->defer_event
);
108 c
->defer_event
= NULL
;
112 static void connect_io_cb(struct pa_mainloop_api
*m
, struct pa_io_event
*e
, int fd
, enum pa_io_event_flags f
, void *userdata
) {
113 struct pa_socket_client
*c
= userdata
;
114 assert(m
&& c
&& c
->io_event
== e
&& fd
>= 0);
115 m
->io_free(c
->io_event
);
120 static int do_connect(struct pa_socket_client
*c
, const struct sockaddr
*sa
, socklen_t len
) {
122 assert(c
&& sa
&& len
);
124 pa_make_nonblock_fd(c
->fd
);
126 if ((r
= connect(c
->fd
, sa
, len
)) < 0) {
127 if (errno
!= EINPROGRESS
) {
128 /*fprintf(stderr, "connect(): %s\n", strerror(errno));*/
132 c
->io_event
= c
->mainloop
->io_new(c
->mainloop
, c
->fd
, PA_IO_EVENT_OUTPUT
, connect_io_cb
, c
);
135 c
->defer_event
= c
->mainloop
->defer_new(c
->mainloop
, connect_fixed_cb
, c
);
136 assert(c
->defer_event
);
142 struct pa_socket_client
* pa_socket_client_new_ipv4(struct pa_mainloop_api
*m
, uint32_t address
, uint16_t port
) {
143 struct pa_socket_client
*c
;
144 struct sockaddr_in sa
;
145 assert(m
&& address
&& port
);
147 c
= pa_socket_client_new(m
);
150 if ((c
->fd
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0) {
151 fprintf(stderr
, "socket(): %s\n", strerror(errno
));
155 pa_fd_set_cloexec(c
->fd
, 1);
156 pa_socket_tcp_low_delay(c
->fd
);
158 sa
.sin_family
= AF_INET
;
159 sa
.sin_port
= htons(port
);
160 sa
.sin_addr
.s_addr
= htonl(address
);
162 if (do_connect(c
, (struct sockaddr
*) &sa
, sizeof(sa
)) < 0)
168 pa_socket_client_unref(c
);
172 struct pa_socket_client
* pa_socket_client_new_unix(struct pa_mainloop_api
*m
, const char *filename
) {
173 struct pa_socket_client
*c
;
174 struct sockaddr_un sa
;
175 assert(m
&& filename
);
177 c
= pa_socket_client_new(m
);
180 if ((c
->fd
= socket(PF_LOCAL
, SOCK_STREAM
, 0)) < 0) {
181 fprintf(stderr
, "socket(): %s\n", strerror(errno
));
185 pa_fd_set_cloexec(c
->fd
, 1);
186 pa_socket_low_delay(c
->fd
);
188 sa
.sun_family
= AF_LOCAL
;
189 strncpy(sa
.sun_path
, filename
, sizeof(sa
.sun_path
)-1);
190 sa
.sun_path
[sizeof(sa
.sun_path
) - 1] = 0;
192 if (do_connect(c
, (struct sockaddr
*) &sa
, sizeof(sa
)) < 0)
198 pa_socket_client_unref(c
);
202 struct pa_socket_client
* pa_socket_client_new_sockaddr(struct pa_mainloop_api
*m
, const struct sockaddr
*sa
, size_t salen
) {
203 struct pa_socket_client
*c
;
205 c
= pa_socket_client_new(m
);
208 if ((c
->fd
= socket(sa
->sa_family
, SOCK_STREAM
, 0)) < 0) {
209 fprintf(stderr
, "socket(): %s\n", strerror(errno
));
213 pa_fd_set_cloexec(c
->fd
, 1);
214 if (sa
->sa_family
== AF_INET
)
215 pa_socket_tcp_low_delay(c
->fd
);
217 pa_socket_low_delay(c
->fd
);
219 if (do_connect(c
, sa
, salen
) < 0)
225 pa_socket_client_unref(c
);
230 void socket_client_free(struct pa_socket_client
*c
) {
231 assert(c
&& c
->mainloop
);
233 c
->mainloop
->io_free(c
->io_event
);
235 c
->mainloop
->defer_free(c
->defer_event
);
241 void pa_socket_client_unref(struct pa_socket_client
*c
) {
242 assert(c
&& c
->ref
>= 1);
245 socket_client_free(c
);
248 struct pa_socket_client
* pa_socket_client_ref(struct pa_socket_client
*c
) {
249 assert(c
&& c
->ref
>= 1);
254 void pa_socket_client_set_callback(struct pa_socket_client
*c
, void (*on_connection
)(struct pa_socket_client
*c
, struct pa_iochannel
*io
, void *userdata
), void *userdata
) {
256 c
->callback
= on_connection
;
257 c
->userdata
= userdata
;