limit the number of concurrent connections for all four protocols
[profile/ivi/pulseaudio-panda.git] / polyp / protocol-cli.c
1 /* $Id$ */
2
3 /***
4   This file is part of polypaudio.
5  
6   polypaudio 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 of the License,
9   or (at your option) any later version.
10  
11   polypaudio 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 polypaudio; 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 <assert.h>
27 #include <stdlib.h>
28
29 #include "protocol-cli.h"
30 #include "cli.h"
31 #include "xmalloc.h"
32 #include "log.h"
33
34 /* Don't allow more than this many concurrent connections */
35 #define MAX_CONNECTIONS 10
36
37 struct pa_protocol_cli {
38     struct pa_module *module;
39     struct pa_core *core;
40     struct pa_socket_server*server;
41     struct pa_idxset *connections;
42 };
43
44 static void cli_eof_cb(struct pa_cli*c, void*userdata) {
45     struct pa_protocol_cli *p = userdata;
46     assert(p);
47     pa_idxset_remove_by_data(p->connections, c, NULL);
48     pa_cli_free(c);
49 }
50
51 static void on_connection(struct pa_socket_server*s, struct pa_iochannel *io, void *userdata) {
52     struct pa_protocol_cli *p = userdata;
53     struct pa_cli *c;
54     assert(s && io && p);
55
56     if (pa_idxset_ncontents(p->connections)+1 > MAX_CONNECTIONS) {
57         pa_log(__FILE__": Warning! Too many connections (%u), dropping incoming connection.\n", MAX_CONNECTIONS);
58         pa_iochannel_free(io);
59         return;
60     }
61     
62     c = pa_cli_new(p->core, io, p->module);
63     assert(c);
64     pa_cli_set_eof_callback(c, cli_eof_cb, p);
65
66     pa_idxset_put(p->connections, c, NULL);
67 }
68
69 struct pa_protocol_cli* pa_protocol_cli_new(struct pa_core *core, struct pa_socket_server *server, struct pa_module *m, struct pa_modargs *ma) {
70     struct pa_protocol_cli* p;
71     assert(core && server);
72
73     p = pa_xmalloc(sizeof(struct pa_protocol_cli));
74     p->module = m;
75     p->core = core;
76     p->server = server;
77     p->connections = pa_idxset_new(NULL, NULL);
78
79     pa_socket_server_set_callback(p->server, on_connection, p);
80     
81     return p;
82 }
83
84 static void free_connection(void *p, void *userdata) {
85     assert(p);
86     pa_cli_free(p);
87 }
88
89 void pa_protocol_cli_free(struct pa_protocol_cli *p) {
90     assert(p);
91
92     pa_idxset_free(p->connections, free_connection, NULL);
93     pa_socket_server_unref(p->server);
94     pa_xfree(p);
95 }