From 81447ed392e57f822e09cf648cb16732a3e3773f Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sat, 19 Jun 2004 19:27:47 +0000 Subject: cli protocol git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@28 fefdeb5f-60dc-0310-8127-8f9354f1896f --- src/cli.c | 2 +- src/iochannel.c | 18 ++++++++++---- src/iochannel.h | 2 ++ src/module-cli.c | 1 + src/module-protocol-stub.c | 8 ++++--- src/protocol-cli.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++ src/protocol-cli.h | 12 ++++++++++ 7 files changed, 94 insertions(+), 8 deletions(-) create mode 100644 src/protocol-cli.c create mode 100644 src/protocol-cli.h (limited to 'src') diff --git a/src/cli.c b/src/cli.c index 4f67f8b7..ec484ace 100644 --- a/src/cli.c +++ b/src/cli.c @@ -111,7 +111,7 @@ static void line_callback(struct ioline *line, const char *s, void *userdata) { l = strcspn(s, delimiter); for (command = commands; command->name; command++) - if (!strncmp(s, command->name, l)) { + if (strlen(command->name) == l && !strncmp(s, command->name, l)) { struct tokenizer *t = tokenizer_new(s, command->args); assert(t); command->proc(c, t); diff --git a/src/iochannel.c b/src/iochannel.c index 2044d561..f0c4c499 100644 --- a/src/iochannel.c +++ b/src/iochannel.c @@ -15,6 +15,8 @@ struct iochannel { int readable; int writable; + int no_close; + struct mainloop_source* input_source, *output_source; }; @@ -83,6 +85,7 @@ struct iochannel* iochannel_new(struct mainloop*m, int ifd, int ofd) { io->callback = NULL; io->readable = 0; io->writable = 0; + io->no_close = 0; if (ifd == ofd) { assert(ifd >= 0); @@ -109,10 +112,12 @@ struct iochannel* iochannel_new(struct mainloop*m, int ifd, int ofd) { void iochannel_free(struct iochannel*io) { assert(io); - if (io->ifd >= 0) - close(io->ifd); - if (io->ofd >= 0 && io->ofd != io->ifd) - close(io->ofd); + if (!io->no_close) { + if (io->ifd >= 0) + close(io->ifd); + if (io->ofd >= 0 && io->ofd != io->ifd) + close(io->ofd); + } if (io->input_source) mainloop_source_free(io->input_source); @@ -162,3 +167,8 @@ void iochannel_set_callback(struct iochannel*io, void (*callback)(struct iochann io->callback = callback; io->userdata = userdata; } + +void iochannel_set_noclose(struct iochannel*io, int b) { + assert(io); + io->no_close = b; +} diff --git a/src/iochannel.h b/src/iochannel.h index f97fabba..8ed8b878 100644 --- a/src/iochannel.h +++ b/src/iochannel.h @@ -15,6 +15,8 @@ ssize_t iochannel_read(struct iochannel*io, void*data, size_t l); int iochannel_is_readable(struct iochannel*io); int iochannel_is_writable(struct iochannel*io); +void iochannel_set_noclose(struct iochannel*io, int b); + void iochannel_set_callback(struct iochannel*io, void (*callback)(struct iochannel*io, void *userdata), void *userdata); #endif diff --git a/src/module-cli.c b/src/module-cli.c index 4af37f67..883f4f53 100644 --- a/src/module-cli.c +++ b/src/module-cli.c @@ -19,6 +19,7 @@ int module_init(struct core *c, struct module*m) { stdin_inuse = stdout_inuse = 1; io = iochannel_new(c->mainloop, STDIN_FILENO, STDOUT_FILENO); assert(io); + iochannel_set_noclose(io, 1); m->userdata = cli_new(c, io); assert(m->userdata); diff --git a/src/module-protocol-stub.c b/src/module-protocol-stub.c index 905594c6..9cbf236e 100644 --- a/src/module-protocol-stub.c +++ b/src/module-protocol-stub.c @@ -6,12 +6,14 @@ #ifdef USE_PROTOCOL_SIMPLE #include "protocol-simple.h" - #define protocol_free protcol_simple_free + #define protocol_free protocol_simple_free + #define IPV4_PORT 4712 #else #ifdef USE_PROTOCOL_CLI #include "protocol-cli.h" #define protocol_new protocol_cli_new #define protocol_free protocol_cli_free + #define IPV4_PORT 4711 #else #error "Broken build system" #endif @@ -22,7 +24,7 @@ int module_init(struct core *c, struct module*m) { assert(c && m); #ifdef USE_TCP_SOCKETS - if (!(s = socket_server_new_ipv4(c->mainloop, INADDR_LOOPBACK, 4712))) + if (!(s = socket_server_new_ipv4(c->mainloop, INADDR_LOOPBACK, IPV4_PORT))) return -1; #else if (!(s = socket_server_new_unix(c->mainloop, "/tmp/polypsimple"))) @@ -42,5 +44,5 @@ int module_init(struct core *c, struct module*m) { void module_done(struct core *c, struct module*m) { assert(c && m); - protocol_simple_free(m->userdata); + protocol_free(m->userdata); } diff --git a/src/protocol-cli.c b/src/protocol-cli.c new file mode 100644 index 00000000..c0c93d98 --- /dev/null +++ b/src/protocol-cli.c @@ -0,0 +1,59 @@ +#include +#include + +#include "protocol-cli.h" +#include "cli.h" + +struct protocol_cli { + struct core *core; + struct socket_server*server; + struct idxset *connections; +}; + +static void cli_eof_cb(struct cli*c, void*userdata) { + struct protocol_cli *p = userdata; + assert(c && p); + + idxset_remove_by_data(p->connections, c, NULL); + cli_free(c); +} + +static void on_connection(struct socket_server*s, struct iochannel *io, void *userdata) { + struct protocol_cli *p = userdata; + struct cli *c; + assert(s && io && p); + + c = cli_new(p->core, io); + assert(c); + cli_set_eof_callback(c, cli_eof_cb, p); + + idxset_put(p->connections, c, NULL); +} + +struct protocol_cli* protocol_cli_new(struct core *core, struct socket_server *server) { + struct protocol_cli* p; + assert(core && server); + + p = malloc(sizeof(struct protocol_cli)); + assert(p); + p->core = core; + p->server = server; + p->connections = idxset_new(NULL, NULL); + + socket_server_set_callback(p->server, on_connection, p); + + return p; +} + +static void free_connection(void *p, void *userdata) { + assert(p); + cli_free(p); +} + +void protocol_cli_free(struct protocol_cli *p) { + assert(p); + + idxset_free(p->connections, free_connection, NULL); + socket_server_free(p->server); + free(p); +} diff --git a/src/protocol-cli.h b/src/protocol-cli.h new file mode 100644 index 00000000..8c150ce1 --- /dev/null +++ b/src/protocol-cli.h @@ -0,0 +1,12 @@ +#ifndef fooprotocolclihfoo +#define fooprotocolclihfoo + +#include "core.h" +#include "socket-server.h" + +struct protocol_cli; + +struct protocol_cli* protocol_cli_new(struct core *core, struct socket_server *server); +void protocol_cli_free(struct protocol_cli *n); + +#endif -- cgit