]> code.delx.au - pulseaudio/blob - polyp/socket-client.c
rename src to polyp
[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
40 struct pa_socket_client {
41 struct pa_mainloop_api *mainloop;
42 int fd;
43
44 void *io_source, *fixed_source;
45 void (*callback)(struct pa_socket_client*c, struct pa_iochannel *io, void *userdata);
46 void *userdata;
47 };
48
49 static struct pa_socket_client*pa_socket_client_new(struct pa_mainloop_api *m) {
50 struct pa_socket_client *c;
51 assert(m);
52
53 c = malloc(sizeof(struct pa_socket_client));
54 assert(c);
55 c->mainloop = m;
56 c->fd = -1;
57 c->io_source = c->fixed_source = NULL;
58 c->callback = NULL;
59 c->userdata = NULL;
60 return c;
61 }
62
63 static void do_call(struct pa_socket_client *c) {
64 struct pa_iochannel *io;
65 int error, lerror;
66 assert(c && c->callback);
67
68 lerror = sizeof(error);
69 if (getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &error, &lerror) < 0) {
70 fprintf(stderr, "getsockopt(): %s\n", strerror(errno));
71 goto failed;
72 }
73
74 if (lerror != sizeof(error)) {
75 fprintf(stderr, "getsocktop() returned invalid size.\n");
76 goto failed;
77 }
78
79 if (error != 0) {
80 fprintf(stderr, "connect(): %s\n", strerror(error));
81 goto failed;
82 }
83
84 io = pa_iochannel_new(c->mainloop, c->fd, c->fd);
85 assert(io);
86 c->fd = -1;
87 c->callback(c, io, c->userdata);
88
89 return;
90
91 failed:
92 close(c->fd);
93 c->fd = -1;
94 c->callback(c, NULL, c->userdata);
95 return;
96 }
97
98 static void connect_fixed_cb(struct pa_mainloop_api *m, void *id, void *userdata) {
99 struct pa_socket_client *c = userdata;
100 assert(m && c && c->fixed_source == id);
101 m->cancel_fixed(m, c->fixed_source);
102 c->fixed_source = NULL;
103 do_call(c);
104 }
105
106 static void connect_io_cb(struct pa_mainloop_api*m, void *id, int fd, enum pa_mainloop_api_io_events events, void *userdata) {
107 struct pa_socket_client *c = userdata;
108 assert(m && c && c->io_source == id && fd >= 0);
109 m->cancel_io(m, c->io_source);
110 c->io_source = NULL;
111 do_call(c);
112 }
113
114 static int do_connect(struct pa_socket_client *c, const struct sockaddr *sa, socklen_t len) {
115 int r;
116 assert(c && sa && len);
117
118 pa_make_nonblock_fd(c->fd);
119
120 if ((r = connect(c->fd, sa, len)) < 0) {
121 if (errno != EINPROGRESS) {
122 fprintf(stderr, "connect(): %s\n", strerror(errno));
123 return -1;
124 }
125
126 c->io_source = c->mainloop->source_io(c->mainloop, c->fd, PA_MAINLOOP_API_IO_EVENT_OUTPUT, connect_io_cb, c);
127 assert(c->io_source);
128 } else {
129 c->fixed_source = c->mainloop->source_fixed(c->mainloop, connect_fixed_cb, c);
130 assert(c->fixed_source);
131 }
132
133 return 0;
134 }
135
136 struct pa_socket_client* pa_socket_client_new_ipv4(struct pa_mainloop_api *m, uint32_t address, uint16_t port) {
137 struct pa_socket_client *c;
138 struct sockaddr_in sa;
139 assert(m && address && port);
140
141 c = pa_socket_client_new(m);
142 assert(c);
143
144 if ((c->fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
145 fprintf(stderr, "socket(): %s\n", strerror(errno));
146 goto fail;
147 }
148
149 pa_socket_tcp_low_delay(c->fd);
150
151 sa.sin_family = AF_INET;
152 sa.sin_port = htons(port);
153 sa.sin_addr.s_addr = htonl(address);
154
155 if (do_connect(c, (struct sockaddr*) &sa, sizeof(sa)) < 0)
156 goto fail;
157
158 return c;
159
160 fail:
161 pa_socket_client_free(c);
162 return NULL;
163 }
164
165 struct pa_socket_client* pa_socket_client_new_unix(struct pa_mainloop_api *m, const char *filename) {
166 struct pa_socket_client *c;
167 struct sockaddr_un sa;
168 assert(m && filename);
169
170 c = pa_socket_client_new(m);
171 assert(c);
172
173 if ((c->fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
174 fprintf(stderr, "socket(): %s\n", strerror(errno));
175 goto fail;
176 }
177
178 pa_socket_low_delay(c->fd);
179
180 sa.sun_family = AF_LOCAL;
181 strncpy(sa.sun_path, filename, sizeof(sa.sun_path)-1);
182 sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
183
184 if (do_connect(c, (struct sockaddr*) &sa, sizeof(sa)) < 0)
185 goto fail;
186
187 return c;
188
189 fail:
190 pa_socket_client_free(c);
191 return NULL;
192 }
193
194 struct pa_socket_client* pa_socket_client_new_sockaddr(struct pa_mainloop_api *m, const struct sockaddr *sa, size_t salen) {
195 struct pa_socket_client *c;
196 assert(m && sa);
197 c = pa_socket_client_new(m);
198 assert(c);
199
200 if ((c->fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) {
201 fprintf(stderr, "socket(): %s\n", strerror(errno));
202 goto fail;
203 }
204
205 if (sa->sa_family == AF_INET)
206 pa_socket_tcp_low_delay(c->fd);
207 else
208 pa_socket_low_delay(c->fd);
209
210 if (do_connect(c, sa, salen) < 0)
211 goto fail;
212
213 return c;
214
215 fail:
216 pa_socket_client_free(c);
217 return NULL;
218
219 }
220
221 void pa_socket_client_free(struct pa_socket_client *c) {
222 assert(c && c->mainloop);
223 if (c->io_source)
224 c->mainloop->cancel_io(c->mainloop, c->io_source);
225 if (c->fixed_source)
226 c->mainloop->cancel_fixed(c->mainloop, c->fixed_source);
227 if (c->fd >= 0)
228 close(c->fd);
229 free(c);
230 }
231
232 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) {
233 assert(c);
234 c->callback = on_connection;
235 c->userdata = userdata;
236 }