]> code.delx.au - pulseaudio/blob - src/pulsecore/protocol-cli.c
Merge dead branch 'liboil-test'
[pulseaudio] / src / pulsecore / protocol-cli.c
1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2004-2006 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser 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 PulseAudio 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 Lesser General Public License
17 along with PulseAudio; 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
28 #include <pulse/xmalloc.h>
29
30 #include <pulsecore/cli.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/macro.h>
33
34 #include "protocol-cli.h"
35
36 /* Don't allow more than this many concurrent connections */
37 #define MAX_CONNECTIONS 25
38
39 struct pa_protocol_cli {
40 pa_module *module;
41 pa_core *core;
42 pa_socket_server*server;
43 pa_idxset *connections;
44 };
45
46 static void cli_eof_cb(pa_cli*c, void*userdata) {
47 pa_protocol_cli *p = userdata;
48 pa_assert(p);
49
50 pa_idxset_remove_by_data(p->connections, c, NULL);
51 pa_cli_free(c);
52 }
53
54 static void on_connection(pa_socket_server*s, pa_iochannel *io, void *userdata) {
55 pa_protocol_cli *p = userdata;
56 pa_cli *c;
57
58 pa_assert(s);
59 pa_assert(io);
60 pa_assert(p);
61
62 if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
63 pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
64 pa_iochannel_free(io);
65 return;
66 }
67
68 c = pa_cli_new(p->core, io, p->module);
69 pa_cli_set_eof_callback(c, cli_eof_cb, p);
70
71 pa_idxset_put(p->connections, c, NULL);
72 }
73
74 pa_protocol_cli* pa_protocol_cli_new(pa_core *core, pa_socket_server *server, pa_module *m, PA_GCC_UNUSED pa_modargs *ma) {
75 pa_protocol_cli* p;
76
77 pa_core_assert_ref(core);
78 pa_assert(server);
79
80 p = pa_xnew(pa_protocol_cli, 1);
81 p->module = m;
82 p->core = core;
83 p->server = pa_socket_server_ref(server);
84 p->connections = pa_idxset_new(NULL, NULL);
85
86 pa_socket_server_set_callback(p->server, on_connection, p);
87
88 return p;
89 }
90
91 static void free_connection(void *p, PA_GCC_UNUSED void *userdata) {
92 pa_assert(p);
93
94 pa_cli_free(p);
95 }
96
97 void pa_protocol_cli_free(pa_protocol_cli *p) {
98 pa_assert(p);
99
100 pa_idxset_free(p->connections, free_connection, NULL);
101 pa_socket_server_unref(p->server);
102 pa_xfree(p);
103 }