]> code.delx.au - pulseaudio/blob - src/protocol-simple.c
more cleanups
[pulseaudio] / src / protocol-simple.c
1 #include <assert.h>
2 #include <stdlib.h>
3 #include <limits.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <string.h>
7
8 #include "inputstream.h"
9 #include "outputstream.h"
10 #include "protocol-simple.h"
11 #include "client.h"
12
13 struct connection {
14 struct protocol_simple *protocol;
15 struct iochannel *io;
16 struct input_stream *istream;
17 struct output_stream *ostream;
18 struct client *client;
19 };
20
21 struct protocol_simple {
22 struct core *core;
23 struct socket_server*server;
24 struct idxset *connections;
25 enum protocol_simple_mode mode;
26 };
27
28 #define BUFSIZE PIPE_BUF
29
30 static void free_connection(void *data, void *userdata) {
31 struct connection *c = data;
32 assert(data);
33
34 if (c->istream)
35 input_stream_free(c->istream);
36 if (c->ostream)
37 output_stream_free(c->ostream);
38
39 client_free(c->client);
40
41 iochannel_free(c->io);
42 free(c);
43 }
44
45 static void destroy_connection(struct connection *c) {
46 assert(c && c->protocol);
47 idxset_remove_by_data(c->protocol->connections, c, NULL);
48 free_connection(c, NULL);
49 }
50
51 static void istream_kill_cb(struct input_stream *i, void *userdata) {
52 struct connection *c = userdata;
53 assert(i && c);
54 destroy_connection(c);
55 }
56
57 static void ostream_kill_cb(struct output_stream *o, void *userdata) {
58 struct connection *c = userdata;
59 assert(o && c);
60 destroy_connection(c);
61 }
62
63 static void client_kill_cb(struct client *client, void*userdata) {
64 struct connection *c= userdata;
65 assert(client && c);
66 destroy_connection(c);
67 }
68
69 static void io_callback(struct iochannel*io, void *userdata) {
70 struct connection *c = userdata;
71 assert(io && c);
72
73 if (c->istream && iochannel_is_readable(io)) {
74 struct memchunk chunk;
75 ssize_t r;
76
77 chunk.memblock = memblock_new(BUFSIZE);
78 assert(chunk.memblock);
79
80 if ((r = iochannel_read(io, chunk.memblock->data, BUFSIZE)) <= 0) {
81 fprintf(stderr, "read(): %s\n", r == 0 ? "EOF" : strerror(errno));
82 memblock_unref(chunk.memblock);
83 goto fail;
84 }
85
86 chunk.memblock->length = r;
87 chunk.length = r;
88 chunk.index = 0;
89
90 memblockq_push(c->istream->memblockq, &chunk, 0);
91 input_stream_notify_sink(c->istream);
92 memblock_unref(chunk.memblock);
93 }
94
95 if (c->ostream && iochannel_is_writable(io)) {
96 struct memchunk chunk;
97 ssize_t r;
98
99 memblockq_peek(c->ostream->memblockq, &chunk);
100 assert(chunk.memblock && chunk.length);
101
102 if ((r = iochannel_write(io, chunk.memblock->data+chunk.index, chunk.length)) < 0) {
103 fprintf(stderr, "write(): %s\n", strerror(errno));
104 memblock_unref(chunk.memblock);
105 goto fail;
106 }
107
108 memblockq_drop(c->ostream->memblockq, r);
109 memblock_unref(chunk.memblock);
110 }
111
112 return;
113
114 fail:
115 destroy_connection(c);
116 }
117
118 static void on_connection(struct socket_server*s, struct iochannel *io, void *userdata) {
119 struct protocol_simple *p = userdata;
120 struct connection *c = NULL;
121 assert(s && io && p);
122
123 c = malloc(sizeof(struct connection));
124 assert(c);
125 c->io = io;
126 c->istream = NULL;
127 c->ostream = NULL;
128 c->protocol = p;
129
130 c->client = client_new(p->core, "SIMPLE", "Client");
131 assert(c->client);
132 client_set_kill_callback(c->client, client_kill_cb, c);
133
134 if (p->mode & PROTOCOL_SIMPLE_RECORD) {
135 struct source *source;
136
137 if (!(source = core_get_default_source(p->core))) {
138 fprintf(stderr, "Failed to get default source.\n");
139 goto fail;
140 }
141
142 c->ostream = output_stream_new(source, &DEFAULT_SAMPLE_SPEC, c->client->name);
143 assert(c->ostream);
144 output_stream_set_kill_callback(c->ostream, ostream_kill_cb, c);
145 }
146
147 if (p->mode & PROTOCOL_SIMPLE_PLAYBACK) {
148 struct sink *sink;
149
150 if (!(sink = core_get_default_sink(p->core))) {
151 fprintf(stderr, "Failed to get default sink.\n");
152 goto fail;
153 }
154
155 c->istream = input_stream_new(sink, &DEFAULT_SAMPLE_SPEC, c->client->name);
156 assert(c->istream);
157 input_stream_set_kill_callback(c->istream, istream_kill_cb, c);
158 }
159
160
161 iochannel_set_callback(c->io, io_callback, c);
162 idxset_put(p->connections, c, NULL);
163 return;
164
165 fail:
166 if (c) {
167 if (c->client)
168 client_free(c->client);
169 if (c->istream)
170 input_stream_free(c->istream);
171 if (c->ostream)
172 output_stream_free(c->ostream);
173
174 iochannel_free(c->io);
175 free(c);
176 }
177 }
178
179 struct protocol_simple* protocol_simple_new(struct core *core, struct socket_server *server, enum protocol_simple_mode mode) {
180 struct protocol_simple* p;
181 assert(core && server && mode <= PROTOCOL_SIMPLE_DUPLEX && mode > 0);
182
183 p = malloc(sizeof(struct protocol_simple));
184 assert(p);
185 p->core = core;
186 p->server = server;
187 p->connections = idxset_new(NULL, NULL);
188 p->mode = mode;
189
190 socket_server_set_callback(p->server, on_connection, p);
191
192 return p;
193 }
194
195
196 void protocol_simple_free(struct protocol_simple *p) {
197 assert(p);
198
199 idxset_free(p->connections, free_connection, NULL);
200 socket_server_free(p->server);
201 free(p);
202 }