]>
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"
42 struct pa_socket_client
{
44 struct pa_mainloop_api
*mainloop
;
46 struct pa_io_event
*io_event
;
47 struct pa_defer_event
*defer_event
;
48 void (*callback
)(struct pa_socket_client
*c
, struct pa_iochannel
*io
, void *userdata
);
52 static struct pa_socket_client
*pa_socket_client_new(struct pa_mainloop_api
*m
) {
53 struct pa_socket_client
*c
;
56 c
= pa_xmalloc(sizeof(struct pa_socket_client
));
61 c
->defer_event
= NULL
;
67 static void do_call(struct pa_socket_client
*c
) {
68 struct pa_iochannel
*io
= NULL
;
71 assert(c
&& c
->callback
);
73 pa_socket_client_ref(c
);
75 lerror
= sizeof(error
);
76 if (getsockopt(c
->fd
, SOL_SOCKET
, SO_ERROR
, &error
, &lerror
) < 0) {
77 pa_log(__FILE__
": getsockopt(): %s\n", strerror(errno
));
81 if (lerror
!= sizeof(error
)) {
82 pa_log(__FILE__
": getsockopt() returned invalid size.\n");
87 pa_log(__FILE__
": connect(): %s\n", strerror(error
));
91 io
= pa_iochannel_new(c
->mainloop
, c
->fd
, c
->fd
);
100 c
->callback(c
, io
, c
->userdata
);
102 pa_socket_client_unref(c
);
105 static void connect_fixed_cb(struct pa_mainloop_api
*m
, struct pa_defer_event
*e
, void *userdata
) {
106 struct pa_socket_client
*c
= userdata
;
107 assert(m
&& c
&& c
->defer_event
== e
);
108 m
->defer_free(c
->defer_event
);
109 c
->defer_event
= NULL
;
113 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
) {
114 struct pa_socket_client
*c
= userdata
;
115 assert(m
&& c
&& c
->io_event
== e
&& fd
>= 0);
116 m
->io_free(c
->io_event
);
121 static int do_connect(struct pa_socket_client
*c
, const struct sockaddr
*sa
, socklen_t len
) {
123 assert(c
&& sa
&& len
);
125 pa_make_nonblock_fd(c
->fd
);
127 if ((r
= connect(c
->fd
, sa
, len
)) < 0) {
128 if (errno
!= EINPROGRESS
) {
129 /*pa_log(__FILE__": connect(): %s\n", strerror(errno));*/
133 c
->io_event
= c
->mainloop
->io_new(c
->mainloop
, c
->fd
, PA_IO_EVENT_OUTPUT
, connect_io_cb
, c
);
136 c
->defer_event
= c
->mainloop
->defer_new(c
->mainloop
, connect_fixed_cb
, c
);
137 assert(c
->defer_event
);
143 struct pa_socket_client
* pa_socket_client_new_ipv4(struct pa_mainloop_api
*m
, uint32_t address
, uint16_t port
) {
144 struct pa_socket_client
*c
;
145 struct sockaddr_in sa
;
146 assert(m
&& address
&& port
);
148 c
= pa_socket_client_new(m
);
151 if ((c
->fd
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0) {
152 pa_log(__FILE__
": socket(): %s\n", strerror(errno
));
156 pa_fd_set_cloexec(c
->fd
, 1);
157 pa_socket_tcp_low_delay(c
->fd
);
159 sa
.sin_family
= AF_INET
;
160 sa
.sin_port
= htons(port
);
161 sa
.sin_addr
.s_addr
= htonl(address
);
163 if (do_connect(c
, (struct sockaddr
*) &sa
, sizeof(sa
)) < 0)
169 pa_socket_client_unref(c
);
173 struct pa_socket_client
* pa_socket_client_new_unix(struct pa_mainloop_api
*m
, const char *filename
) {
174 struct pa_socket_client
*c
;
175 struct sockaddr_un sa
;
176 assert(m
&& filename
);
178 c
= pa_socket_client_new(m
);
181 if ((c
->fd
= socket(PF_LOCAL
, SOCK_STREAM
, 0)) < 0) {
182 pa_log(__FILE__
": socket(): %s\n", strerror(errno
));
186 pa_fd_set_cloexec(c
->fd
, 1);
187 pa_socket_low_delay(c
->fd
);
189 sa
.sun_family
= AF_LOCAL
;
190 strncpy(sa
.sun_path
, filename
, sizeof(sa
.sun_path
)-1);
191 sa
.sun_path
[sizeof(sa
.sun_path
) - 1] = 0;
193 if (do_connect(c
, (struct sockaddr
*) &sa
, sizeof(sa
)) < 0)
199 pa_socket_client_unref(c
);
203 struct pa_socket_client
* pa_socket_client_new_sockaddr(struct pa_mainloop_api
*m
, const struct sockaddr
*sa
, size_t salen
) {
204 struct pa_socket_client
*c
;
206 c
= pa_socket_client_new(m
);
209 if ((c
->fd
= socket(sa
->sa_family
, SOCK_STREAM
, 0)) < 0) {
210 pa_log(__FILE__
": socket(): %s\n", strerror(errno
));
214 pa_fd_set_cloexec(c
->fd
, 1);
215 if (sa
->sa_family
== AF_INET
)
216 pa_socket_tcp_low_delay(c
->fd
);
218 pa_socket_low_delay(c
->fd
);
220 if (do_connect(c
, sa
, salen
) < 0)
226 pa_socket_client_unref(c
);
231 void socket_client_free(struct pa_socket_client
*c
) {
232 assert(c
&& c
->mainloop
);
234 c
->mainloop
->io_free(c
->io_event
);
236 c
->mainloop
->defer_free(c
->defer_event
);
242 void pa_socket_client_unref(struct pa_socket_client
*c
) {
243 assert(c
&& c
->ref
>= 1);
246 socket_client_free(c
);
249 struct pa_socket_client
* pa_socket_client_ref(struct pa_socket_client
*c
) {
250 assert(c
&& c
->ref
>= 1);
255 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
) {
257 c
->callback
= on_connection
;
258 c
->userdata
= userdata
;