]> code.delx.au - pulseaudio/blob - polyp/socket-client.c
implement proper refcounting in polyplib
[pulseaudio] / polyp / socket-client.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 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.
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 General Public License for more details.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <stdio.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <assert.h>
31 #include <stdlib.h>
32 #include <sys/un.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35
36 #include "socket-client.h"
37 #include "socket-util.h"
38 #include "util.h"
39 #include "xmalloc.h"
40
41 struct pa_socket_client {
42 struct pa_mainloop_api *mainloop;
43 int fd;
44 struct pa_io_event *io_event;
45 struct pa_defer_event *defer_event;
46 void (*callback)(struct pa_socket_client*c, struct pa_iochannel *io, void *userdata);
47 void *userdata;
48 };
49
50 static struct pa_socket_client*pa_socket_client_new(struct pa_mainloop_api *m) {
51 struct pa_socket_client *c;
52 assert(m);
53
54 c = pa_xmalloc(sizeof(struct pa_socket_client));
55 c->mainloop = m;
56 c->fd = -1;
57 c->io_event = NULL;
58 c->defer_event = NULL;
59 c->callback = NULL;
60 c->userdata = NULL;
61 return c;
62 }
63
64 static void do_call(struct pa_socket_client *c) {
65 struct pa_iochannel *io;
66 int error, lerror;
67 assert(c && c->callback);
68
69 lerror = sizeof(error);
70 if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &error, &lerror) < 0) {
71 fprintf(stderr, "getsockopt(): %s\n", strerror(errno));
72 goto failed;
73 }
74
75 if (lerror != sizeof(error)) {
76 fprintf(stderr, "getsocktop() returned invalid size.\n");
77 goto failed;
78 }
79
80 if (error != 0) {
81 fprintf(stderr, "connect(): %s\n", strerror(error));
82 goto failed;
83 }
84
85 io = pa_iochannel_new(c->mainloop, c->fd, c->fd);
86 assert(io);
87 c->fd = -1;
88 c->callback(c, io, c->userdata);
89
90 return;
91
92 failed:
93 close(c->fd);
94 c->fd = -1;
95 c->callback(c, NULL, c->userdata);
96 return;
97 }
98
99 static void connect_fixed_cb(struct pa_mainloop_api *m, struct pa_defer_event *e, void *userdata) {
100 struct pa_socket_client *c = userdata;
101 assert(m && c && c->defer_event == e);
102 m->defer_free(c->defer_event);
103 c->defer_event = NULL;
104 do_call(c);
105 }
106
107 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) {
108 struct pa_socket_client *c = userdata;
109 assert(m && c && c->io_event == e && fd >= 0);
110 m->io_free(c->io_event);
111 c->io_event = NULL;
112 do_call(c);
113 }
114
115 static int do_connect(struct pa_socket_client *c, const struct sockaddr *sa, socklen_t len) {
116 int r;
117 assert(c && sa && len);
118
119 pa_make_nonblock_fd(c->fd);
120
121 if ((r = connect(c->fd, sa, len)) < 0) {
122 if (errno != EINPROGRESS) {
123 /*fprintf(stderr, "connect(): %s\n", strerror(errno));*/
124 return -1;
125 }
126
127 c->io_event = c->mainloop->io_new(c->mainloop, c->fd, PA_IO_EVENT_OUTPUT, connect_io_cb, c);
128 assert(c->io_event);
129 } else {
130 c->defer_event = c->mainloop->defer_new(c->mainloop, connect_fixed_cb, c);
131 assert(c->defer_event);
132 }
133
134 return 0;
135 }
136
137 struct pa_socket_client* pa_socket_client_new_ipv4(struct pa_mainloop_api *m, uint32_t address, uint16_t port) {
138 struct pa_socket_client *c;
139 struct sockaddr_in sa;
140 assert(m && address && port);
141
142 c = pa_socket_client_new(m);
143 assert(c);
144
145 if ((c->fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
146 fprintf(stderr, "socket(): %s\n", strerror(errno));
147 goto fail;
148 }
149
150 pa_socket_tcp_low_delay(c->fd);
151
152 sa.sin_family = AF_INET;
153 sa.sin_port = htons(port);
154 sa.sin_addr.s_addr = htonl(address);
155
156 if (do_connect(c, (struct sockaddr*) &sa, sizeof(sa)) < 0)
157 goto fail;
158
159 return c;
160
161 fail:
162 pa_socket_client_free(c);
163 return NULL;
164 }
165
166 struct pa_socket_client* pa_socket_client_new_unix(struct pa_mainloop_api *m, const char *filename) {
167 struct pa_socket_client *c;
168 struct sockaddr_un sa;
169 assert(m && filename);
170
171 c = pa_socket_client_new(m);
172 assert(c);
173
174 if ((c->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
175 fprintf(stderr, "socket(): %s\n", strerror(errno));
176 goto fail;
177 }
178
179 pa_socket_low_delay(c->fd);
180
181 sa.sun_family = AF_LOCAL;
182 strncpy(sa.sun_path, filename, sizeof(sa.sun_path)-1);
183 sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
184
185 if (do_connect(c, (struct sockaddr*) &sa, sizeof(sa)) < 0)
186 goto fail;
187
188 return c;
189
190 fail:
191 pa_socket_client_free(c);
192 return NULL;
193 }
194
195 struct pa_socket_client* pa_socket_client_new_sockaddr(struct pa_mainloop_api *m, const struct sockaddr *sa, size_t salen) {
196 struct pa_socket_client *c;
197 assert(m && sa);
198 c = pa_socket_client_new(m);
199 assert(c);
200
201 if ((c->fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) {
202 fprintf(stderr, "socket(): %s\n", strerror(errno));
203 goto fail;
204 }
205
206 if (sa->sa_family == AF_INET)
207 pa_socket_tcp_low_delay(c->fd);
208 else
209 pa_socket_low_delay(c->fd);
210
211 if (do_connect(c, sa, salen) < 0)
212 goto fail;
213
214 return c;
215
216 fail:
217 pa_socket_client_free(c);
218 return NULL;
219
220 }
221
222 void pa_socket_client_free(struct pa_socket_client *c) {
223 assert(c && c->mainloop);
224 if (c->io_event)
225 c->mainloop->io_free(c->io_event);
226 if (c->defer_event)
227 c->mainloop->defer_free(c->defer_event);
228 if (c->fd >= 0)
229 close(c->fd);
230 pa_xfree(c);
231 }
232
233 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) {
234 assert(c);
235 c->callback = on_connection;
236 c->userdata = userdata;
237 }