Fix checking for NULL after usage
[pulseaudio-mirror.git] / src / pulsecore / protocol-cli.c
blobda64874488557f9d95fe0ec9c977521fe2f53f95
1 /***
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
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <stdlib.h>
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 {
41 PA_REFCNT_DECLARE;
43 pa_core *core;
44 pa_idxset *connections;
47 static void cli_unlink(pa_cli_protocol *p, pa_cli *c) {
48 pa_assert(p);
49 pa_assert(c);
51 pa_idxset_remove_by_data(p->connections, c, NULL);
52 pa_cli_free(c);
55 static void cli_eof_cb(pa_cli*c, void*userdata) {
56 pa_cli_protocol *p = userdata;
57 pa_assert(p);
59 cli_unlink(p, c);
62 void pa_cli_protocol_connect(pa_cli_protocol *p, pa_iochannel *io, pa_module *m) {
63 pa_cli *c;
65 pa_assert(p);
66 pa_assert(io);
67 pa_assert(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);
72 return;
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) {
82 pa_cli *c;
83 void *state = NULL;
85 pa_assert(p);
86 pa_assert(m);
88 while ((c = pa_idxset_iterate(p->connections, &state, NULL)))
89 if (pa_cli_get_module(c) == m)
90 cli_unlink(p, c);
93 static pa_cli_protocol* cli_protocol_new(pa_core *c) {
94 pa_cli_protocol *p;
96 pa_assert(c);
98 p = pa_xnew(pa_cli_protocol, 1);
99 PA_REFCNT_INIT(p);
100 p->core = c;
101 p->connections = pa_idxset_new(NULL, NULL);
103 pa_assert_se(pa_shared_set(c, "cli-protocol", p) >= 0);
105 return p;
108 pa_cli_protocol* pa_cli_protocol_get(pa_core *c) {
109 pa_cli_protocol *p;
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) {
118 pa_assert(p);
119 pa_assert(PA_REFCNT_VALUE(p) >= 1);
121 PA_REFCNT_INC(p);
123 return p;
126 void pa_cli_protocol_unref(pa_cli_protocol *p) {
127 pa_cli *c;
128 pa_assert(p);
129 pa_assert(PA_REFCNT_VALUE(p) >= 1);
131 if (PA_REFCNT_DEC(p) > 0)
132 return;
134 while ((c = pa_idxset_first(p->connections, NULL)))
135 cli_unlink(p, c);
137 pa_idxset_free(p->connections, NULL, NULL);
139 pa_assert_se(pa_shared_remove(p->core, "cli-protocol") >= 0);
141 pa_xfree(p);