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, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 #include <pulse/xmalloc.h>
30 #include <pulsecore/cli.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/macro.h>
33 #include <pulsecore/shared.h>
35 #include "protocol-cli.h"
37 /* Don't allow more than this many concurrent connections */
38 #define MAX_CONNECTIONS 25
40 struct pa_cli_protocol
{
44 pa_idxset
*connections
;
47 static void cli_unlink(pa_cli_protocol
*p
, pa_cli
*c
) {
51 pa_idxset_remove_by_data(p
->connections
, c
, NULL
);
55 static void cli_eof_cb(pa_cli
*c
, void*userdata
) {
56 pa_cli_protocol
*p
= userdata
;
62 void pa_cli_protocol_connect(pa_cli_protocol
*p
, pa_iochannel
*io
, pa_module
*m
) {
69 if (pa_idxset_size(p
->connections
)+1 > MAX_CONNECTIONS
) {
70 pa_log("Warning! Too many connections (%u), dropping incoming connection.", MAX_CONNECTIONS
);
71 pa_iochannel_free(io
);
75 c
= pa_cli_new(p
->core
, io
, m
);
76 pa_cli_set_eof_callback(c
, cli_eof_cb
, p
);
78 pa_idxset_put(p
->connections
, c
, NULL
);
81 void pa_cli_protocol_disconnect(pa_cli_protocol
*p
, pa_module
*m
) {
88 while ((c
= pa_idxset_iterate(p
->connections
, &state
, NULL
)))
89 if (pa_cli_get_module(c
) == m
)
93 static pa_cli_protocol
* cli_protocol_new(pa_core
*c
) {
98 p
= pa_xnew(pa_cli_protocol
, 1);
101 p
->connections
= pa_idxset_new(NULL
, NULL
);
103 pa_assert_se(pa_shared_set(c
, "cli-protocol", p
) >= 0);
108 pa_cli_protocol
* pa_cli_protocol_get(pa_core
*c
) {
111 if ((p
= pa_shared_get(c
, "cli-protocol")))
112 return pa_cli_protocol_ref(p
);
114 return cli_protocol_new(c
);
117 pa_cli_protocol
* pa_cli_protocol_ref(pa_cli_protocol
*p
) {
119 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
126 void pa_cli_protocol_unref(pa_cli_protocol
*p
) {
129 pa_assert(PA_REFCNT_VALUE(p
) >= 1);
131 if (PA_REFCNT_DEC(p
) > 0)
134 while ((c
= pa_idxset_first(p
->connections
, NULL
)))
137 pa_idxset_free(p
->connections
, NULL
, NULL
);
139 pa_assert_se(pa_shared_remove(p
->core
, "cli-protocol") >= 0);