]> code.delx.au - pulseaudio/blob - src/protocol-simple.c
a bunch of fixes
[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 io_callback(struct iochannel*io, void *userdata) {
46 struct connection *c = userdata;
47 assert(io && c);
48
49 if (c->istream && iochannel_is_readable(io)) {
50 struct memchunk chunk;
51 ssize_t r;
52
53 chunk.memblock = memblock_new(BUFSIZE);
54 assert(chunk.memblock);
55
56 if ((r = iochannel_read(io, chunk.memblock->data, BUFSIZE)) <= 0) {
57 fprintf(stderr, "read(): %s\n", r == 0 ? "EOF" : strerror(errno));
58 memblock_unref(chunk.memblock);
59 goto fail;
60 }
61
62 chunk.memblock->length = r;
63 chunk.length = r;
64 chunk.index = 0;
65
66 memblockq_push(c->istream->memblockq, &chunk, 0);
67 input_stream_notify(c->istream);
68 memblock_unref(chunk.memblock);
69 }
70
71 if (c->ostream && iochannel_is_writable(io)) {
72 struct memchunk chunk;
73 ssize_t r;
74
75 memblockq_peek(c->ostream->memblockq, &chunk);
76 assert(chunk.memblock && chunk.length);
77
78 if ((r = iochannel_write(io, chunk.memblock->data+chunk.index, chunk.length)) < 0) {
79 fprintf(stderr, "write(): %s\n", strerror(errno));
80 memblock_unref(chunk.memblock);
81 goto fail;
82 }
83
84 memblockq_drop(c->ostream->memblockq, r);
85 memblock_unref(chunk.memblock);
86 }
87
88 return;
89
90 fail:
91 idxset_remove_by_data(c->protocol->connections, c, NULL);
92 free_connection(c, NULL);
93 }
94
95 static void on_connection(struct socket_server*s, struct iochannel *io, void *userdata) {
96 struct protocol_simple *p = userdata;
97 struct connection *c = NULL;
98 assert(s && io && p);
99
100 c = malloc(sizeof(struct connection));
101 assert(c);
102 c->io = io;
103 c->istream = NULL;
104 c->ostream = NULL;
105 c->protocol = p;
106
107 c->client = client_new(p->core, "SIMPLE", "Client");
108 assert(c->client);
109
110 if (p->mode & PROTOCOL_SIMPLE_RECORD) {
111 struct source *source;
112
113 if (!(source = core_get_default_source(p->core))) {
114 fprintf(stderr, "Failed to get default source.\n");
115 goto fail;
116 }
117
118 c->ostream = output_stream_new(source, &DEFAULT_SAMPLE_SPEC, c->client->name);
119 assert(c->ostream);
120 }
121
122 if (p->mode & PROTOCOL_SIMPLE_PLAYBACK) {
123 struct sink *sink;
124
125 if (!(sink = core_get_default_sink(p->core))) {
126 fprintf(stderr, "Failed to get default sink.\n");
127 goto fail;
128 }
129
130 c->istream = input_stream_new(sink, &DEFAULT_SAMPLE_SPEC, c->client->name);
131 assert(c->istream);
132 }
133
134
135 iochannel_set_callback(c->io, io_callback, c);
136 idxset_put(p->connections, c, NULL);
137 return;
138
139 fail:
140 if (c) {
141 if (c->client)
142 client_free(c->client);
143 if (c->istream)
144 input_stream_free(c->istream);
145 if (c->ostream)
146 output_stream_free(c->ostream);
147
148 iochannel_free(c->io);
149 free(c);
150 }
151 }
152
153 struct protocol_simple* protocol_simple_new(struct core *core, struct socket_server *server, enum protocol_simple_mode mode) {
154 struct protocol_simple* p;
155 assert(core && server && mode <= PROTOCOL_SIMPLE_DUPLEX && mode > 0);
156
157 p = malloc(sizeof(struct protocol_simple));
158 assert(p);
159 p->core = core;
160 p->server = server;
161 p->connections = idxset_new(NULL, NULL);
162 p->mode = mode;
163
164 socket_server_set_callback(p->server, on_connection, p);
165
166 return p;
167 }
168
169
170 void protocol_simple_free(struct protocol_simple *p) {
171 assert(p);
172
173 idxset_free(p->connections, free_connection, NULL);
174 socket_server_free(p->server);
175 free(p);
176 }