]> code.delx.au - pulseaudio/blob - polyp/socket-server.c
add CPU load limiter
[pulseaudio] / polyp / socket-server.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 <stdlib.h>
27 #include <assert.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <stdio.h>
33 #include <unistd.h>
34 #include <sys/un.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
37
38 #include "socket-server.h"
39 #include "socket-util.h"
40 #include "xmalloc.h"
41 #include "util.h"
42
43 struct pa_socket_server {
44 int ref;
45 int fd;
46 char *filename;
47
48 void (*on_connection)(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata);
49 void *userdata;
50
51 struct pa_io_event *io_event;
52 struct pa_mainloop_api *mainloop;
53 enum { SOCKET_SERVER_GENERIC, SOCKET_SERVER_IPV4, SOCKET_SERVER_UNIX } type;
54 };
55
56 static void callback(struct pa_mainloop_api *mainloop, struct pa_io_event *e, int fd, enum pa_io_event_flags f, void *userdata) {
57 struct pa_socket_server *s = userdata;
58 struct pa_iochannel *io;
59 int nfd;
60 assert(s && s->mainloop == mainloop && s->io_event == e && e && fd >= 0 && fd == s->fd);
61
62 pa_socket_server_ref(s);
63
64 if ((nfd = accept(fd, NULL, NULL)) < 0) {
65 fprintf(stderr, "accept(): %s\n", strerror(errno));
66 goto finish;
67 }
68
69 pa_fd_set_cloexec(nfd, 1);
70
71 if (!s->on_connection) {
72 close(nfd);
73 goto finish;
74 }
75
76 /* There should be a check for socket type here */
77 if (s->type == SOCKET_SERVER_IPV4)
78 pa_socket_tcp_low_delay(fd);
79 else
80 pa_socket_low_delay(fd);
81
82 io = pa_iochannel_new(s->mainloop, nfd, nfd);
83 assert(io);
84 s->on_connection(s, io, s->userdata);
85
86 finish:
87 pa_socket_server_unref(s);
88 }
89
90 struct pa_socket_server* pa_socket_server_new(struct pa_mainloop_api *m, int fd) {
91 struct pa_socket_server *s;
92 assert(m && fd >= 0);
93
94 s = pa_xmalloc(sizeof(struct pa_socket_server));
95 s->ref = 1;
96 s->fd = fd;
97 s->filename = NULL;
98 s->on_connection = NULL;
99 s->userdata = NULL;
100
101 s->mainloop = m;
102 s->io_event = m->io_new(m, fd, PA_IO_EVENT_INPUT, callback, s);
103 assert(s->io_event);
104
105 s->type = SOCKET_SERVER_GENERIC;
106
107 return s;
108 }
109
110 struct pa_socket_server* pa_socket_server_ref(struct pa_socket_server *s) {
111 assert(s && s->ref >= 1);
112 s->ref++;
113 return s;
114 }
115
116 struct pa_socket_server* pa_socket_server_new_unix(struct pa_mainloop_api *m, const char *filename) {
117 int fd = -1;
118 struct sockaddr_un sa;
119 struct pa_socket_server *s;
120
121 assert(m && filename);
122
123 if ((fd = socket(PF_LOCAL, SOCK_STREAM, 0)) < 0) {
124 fprintf(stderr, "socket(): %s\n", strerror(errno));
125 goto fail;
126 }
127
128 pa_fd_set_cloexec(fd, 1);
129
130 sa.sun_family = AF_LOCAL;
131 strncpy(sa.sun_path, filename, sizeof(sa.sun_path)-1);
132 sa.sun_path[sizeof(sa.sun_path) - 1] = 0;
133
134 pa_socket_low_delay(fd);
135
136 if (bind(fd, (struct sockaddr*) &sa, SUN_LEN(&sa)) < 0) {
137 fprintf(stderr, "bind(): %s\n", strerror(errno));
138 goto fail;
139 }
140
141 if (listen(fd, 5) < 0) {
142 fprintf(stderr, "listen(): %s\n", strerror(errno));
143 goto fail;
144 }
145
146 s = pa_socket_server_new(m, fd);
147 assert(s);
148
149 s->filename = pa_xstrdup(filename);
150 s->type = SOCKET_SERVER_UNIX;
151
152 return s;
153
154 fail:
155 if (fd >= 0)
156 close(fd);
157
158 return NULL;
159 }
160
161 struct pa_socket_server* pa_socket_server_new_ipv4(struct pa_mainloop_api *m, uint32_t address, uint16_t port) {
162 struct pa_socket_server *ss;
163 int fd = -1;
164 struct sockaddr_in sa;
165 int on = 1;
166
167 assert(m && port);
168
169 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
170 fprintf(stderr, "socket(): %s\n", strerror(errno));
171 goto fail;
172 }
173
174 pa_fd_set_cloexec(fd, 1);
175
176 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
177 fprintf(stderr, "setsockopt(): %s\n", strerror(errno));
178
179 pa_socket_tcp_low_delay(fd);
180
181 sa.sin_family = AF_INET;
182 sa.sin_port = htons(port);
183 sa.sin_addr.s_addr = htonl(address);
184
185 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) {
186 fprintf(stderr, "bind(): %s\n", strerror(errno));
187 goto fail;
188 }
189
190 if (listen(fd, 5) < 0) {
191 fprintf(stderr, "listen(): %s\n", strerror(errno));
192 goto fail;
193 }
194
195 if ((ss = pa_socket_server_new(m, fd)))
196 ss->type = SOCKET_SERVER_IPV4;
197
198 return ss;
199
200 fail:
201 if (fd >= 0)
202 close(fd);
203
204 return NULL;
205 }
206
207 static void socket_server_free(struct pa_socket_server*s) {
208 assert(s);
209 close(s->fd);
210
211 if (s->filename) {
212 unlink(s->filename);
213 pa_xfree(s->filename);
214 }
215
216 s->mainloop->io_free(s->io_event);
217 pa_xfree(s);
218 }
219
220 void pa_socket_server_unref(struct pa_socket_server *s) {
221 assert(s && s->ref >= 1);
222
223 if (!(--(s->ref)))
224 socket_server_free(s);
225 }
226
227 void pa_socket_server_set_callback(struct pa_socket_server*s, void (*on_connection)(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata), void *userdata) {
228 assert(s && s->ref >= 1);
229
230 s->on_connection = on_connection;
231 s->userdata = userdata;
232 }