merge glitch-free branch back into trunk
[profile/ivi/pulseaudio.git] / src / pulsecore / protocol-cli.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2004-2006 Lennart Poettering
7
8   PulseAudio is free software; you can redistribute it and/or modify
9   it under the terms of the GNU Lesser General Public License as published
10   by the Free Software Foundation; either version 2 of the License,
11   or (at your option) any later version.
12
13   PulseAudio is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with PulseAudio; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21   USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdlib.h>
29
30 #include <pulse/xmalloc.h>
31
32 #include <pulsecore/cli.h>
33 #include <pulsecore/log.h>
34 #include <pulsecore/macro.h>
35
36 #include "protocol-cli.h"
37
38 /* Don't allow more than this many concurrent connections */
39 #define MAX_CONNECTIONS 25
40
41 struct pa_protocol_cli {
42     pa_module *module;
43     pa_core *core;
44     pa_socket_server*server;
45     pa_idxset *connections;
46 };
47
48 static void cli_eof_cb(pa_cli*c, void*userdata) {
49     pa_protocol_cli *p = userdata;
50     pa_assert(p);
51
52     pa_idxset_remove_by_data(p->connections, c, NULL);
53     pa_cli_free(c);
54 }
55
56 static void on_connection(pa_socket_server*s, pa_iochannel *io, void *userdata) {
57     pa_protocol_cli *p = userdata;
58     pa_cli *c;
59
60     pa_assert(s);
61     pa_assert(io);
62     pa_assert(p);
63
64     if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
65         pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
66         pa_iochannel_free(io);
67         return;
68     }
69
70     c = pa_cli_new(p->core, io, p->module);
71     pa_cli_set_eof_callback(c, cli_eof_cb, p);
72
73     pa_idxset_put(p->connections, c, NULL);
74 }
75
76 pa_protocol_cli* pa_protocol_cli_new(pa_core *core, pa_socket_server *server, pa_module *m, PA_GCC_UNUSED pa_modargs *ma) {
77     pa_protocol_cli* p;
78
79     pa_core_assert_ref(core);
80     pa_assert(server);
81
82     p = pa_xnew(pa_protocol_cli, 1);
83     p->module = m;
84     p->core = core;
85     p->server = pa_socket_server_ref(server);
86     p->connections = pa_idxset_new(NULL, NULL);
87
88     pa_socket_server_set_callback(p->server, on_connection, p);
89
90     return p;
91 }
92
93 static void free_connection(void *p, PA_GCC_UNUSED void *userdata) {
94     pa_assert(p);
95
96     pa_cli_free(p);
97 }
98
99 void pa_protocol_cli_free(pa_protocol_cli *p) {
100     pa_assert(p);
101
102     pa_idxset_free(p->connections, free_connection, NULL);
103     pa_socket_server_unref(p->server);
104     pa_xfree(p);
105 }