Merge branch 'master' of ssh://rootserver/home/lennart/git/public/pulseaudio
[platform/upstream/pulseaudio.git] / 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.1 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 #include <pulsecore/shared.h>
34
35 #include "protocol-cli.h"
36
37 /* Don't allow more than this many concurrent connections */
38 #define MAX_CONNECTIONS 25
39
40 struct pa_cli_protocol {
41     PA_REFCNT_DECLARE;
42
43     pa_core *core;
44     pa_idxset *connections;
45 };
46
47 static void cli_unlink(pa_cli_protocol *p, pa_cli *c) {
48     pa_assert(p);
49     pa_assert(c);
50
51     pa_idxset_remove_by_data(p->connections, c, NULL);
52     pa_cli_free(c);
53 }
54
55 static void cli_eof_cb(pa_cli*c, void*userdata) {
56     pa_cli_protocol *p = userdata;
57     pa_assert(p);
58
59     cli_unlink(p, c);
60 }
61
62 void pa_cli_protocol_connect(pa_cli_protocol *p, pa_iochannel *io, pa_module *m) {
63     pa_cli *c;
64
65     pa_assert(p);
66     pa_assert(io);
67     pa_assert(m);
68
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;
73     }
74
75     c = pa_cli_new(p->core, io, m);
76     pa_cli_set_eof_callback(c, cli_eof_cb, p);
77
78     pa_idxset_put(p->connections, c, NULL);
79 }
80
81 void pa_cli_protocol_disconnect(pa_cli_protocol *p, pa_module *m) {
82     pa_cli *c;
83     void *state = NULL;
84
85     pa_assert(p);
86     pa_assert(m);
87
88     while ((c = pa_idxset_iterate(p->connections, &state, NULL)))
89         if (pa_cli_get_module(c) == m)
90             cli_unlink(p, c);
91 }
92
93 static pa_cli_protocol* cli_protocol_new(pa_core *c) {
94     pa_cli_protocol *p;
95
96     pa_assert(c);
97
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);
102
103     pa_assert_se(pa_shared_set(c, "cli-protocol", p) >= 0);
104
105     return p;
106 }
107
108 pa_cli_protocol* pa_cli_protocol_get(pa_core *c) {
109     pa_cli_protocol *p;
110
111     if ((p = pa_shared_get(c, "cli-protocol")))
112         return pa_cli_protocol_ref(p);
113
114     return cli_protocol_new(c);
115 }
116
117 pa_cli_protocol* pa_cli_protocol_ref(pa_cli_protocol *p) {
118     pa_assert(p);
119     pa_assert(PA_REFCNT_VALUE(p) >= 1);
120
121     PA_REFCNT_INC(p);
122
123     return p;
124 }
125
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);
130
131     if (PA_REFCNT_DEC(p) > 0)
132         return;
133
134     while ((c = pa_idxset_first(p->connections, NULL)))
135         cli_unlink(p, c);
136
137     pa_idxset_free(p->connections, NULL, NULL);
138
139     pa_assert_se(pa_shared_remove(p->core, "cli-protocol") >= 0);
140
141     pa_xfree(p);
142 }