2 This file is part of PulseAudio.
4 Copyright 2004-2006 Lennart Poettering
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.1 of the License,
9 or (at your option) any later version.
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.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
26 #include <pulse/xmalloc.h>
28 #include <pulsecore/cli.h>
29 #include <pulsecore/log.h>
30 #include <pulsecore/macro.h>
31 #include <pulsecore/shared.h>
33 #include "protocol-cli.h"
35 /* Don't allow more than this many concurrent connections */
36 #define MAX_CONNECTIONS 25
38 struct pa_cli_protocol {
42 pa_idxset *connections;
45 static void cli_unlink(pa_cli_protocol *p, pa_cli *c) {
49 pa_idxset_remove_by_data(p->connections, c, NULL);
53 static void cli_eof_cb(pa_cli*c, void*userdata) {
54 pa_cli_protocol *p = userdata;
60 void pa_cli_protocol_connect(pa_cli_protocol *p, pa_iochannel *io, pa_module *m) {
67 if (pa_idxset_size(p->connections)+1 > MAX_CONNECTIONS) {
68 pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS);
69 pa_iochannel_free(io);
73 c = pa_cli_new(p->core, io, m);
74 pa_cli_set_eof_callback(c, cli_eof_cb, p);
76 pa_idxset_put(p->connections, c, NULL);
79 void pa_cli_protocol_disconnect(pa_cli_protocol *p, pa_module *m) {
86 while ((c = pa_idxset_iterate(p->connections, &state, NULL)))
87 if (pa_cli_get_module(c) == m)
91 static pa_cli_protocol* cli_protocol_new(pa_core *c) {
96 p = pa_xnew(pa_cli_protocol, 1);
99 p->connections = pa_idxset_new(NULL, NULL);
101 pa_assert_se(pa_shared_set(c, "cli-protocol", p) >= 0);
106 pa_cli_protocol* pa_cli_protocol_get(pa_core *c) {
109 if ((p = pa_shared_get(c, "cli-protocol")))
110 return pa_cli_protocol_ref(p);
112 return cli_protocol_new(c);
115 pa_cli_protocol* pa_cli_protocol_ref(pa_cli_protocol *p) {
117 pa_assert(PA_REFCNT_VALUE(p) >= 1);
124 void pa_cli_protocol_unref(pa_cli_protocol *p) {
127 pa_assert(PA_REFCNT_VALUE(p) >= 1);
129 if (PA_REFCNT_DEC(p) > 0)
132 while ((c = pa_idxset_first(p->connections, NULL)))
135 pa_idxset_free(p->connections, NULL);
137 pa_assert_se(pa_shared_remove(p->core, "cli-protocol") >= 0);