]> code.delx.au - pulseaudio/blob - polyp/pdispatch.c
proper ref counting for more objects
[pulseaudio] / polyp / pdispatch.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 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 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 <stdio.h>
27 #include <stdlib.h>
28 #include <assert.h>
29
30 #include "pdispatch.h"
31 #include "native-common.h"
32 #include "xmalloc.h"
33 #include "llist.h"
34
35 /*#define DEBUG_OPCODES*/
36
37 #ifdef DEBUG_OPCODES
38
39 static const char *command_names[PA_COMMAND_MAX] = {
40 [PA_COMMAND_ERROR] = "ERROR",
41 [PA_COMMAND_TIMEOUT] = "TIMEOUT",
42 [PA_COMMAND_REPLY] = "REPLY",
43 [PA_COMMAND_CREATE_PLAYBACK_STREAM] = "CREATE_PLAYBACK_STREAM",
44 [PA_COMMAND_DELETE_PLAYBACK_STREAM] = "DELETE_PLAYBACK_STREAM",
45 [PA_COMMAND_CREATE_RECORD_STREAM] = "CREATE_RECORD_STREAM",
46 [PA_COMMAND_DELETE_RECORD_STREAM] = "DELETE_RECORD_STREAM",
47 [PA_COMMAND_AUTH] = "AUTH",
48 [PA_COMMAND_REQUEST] = "REQUEST",
49 [PA_COMMAND_EXIT] = "EXIT",
50 [PA_COMMAND_SET_NAME] = "SET_NAME",
51 [PA_COMMAND_LOOKUP_SINK] = "LOOKUP_SINK",
52 [PA_COMMAND_LOOKUP_SOURCE] = "LOOKUP_SOURCE",
53 [PA_COMMAND_DRAIN_PLAYBACK_STREAM] = "DRAIN_PLAYBACK_STREAM",
54 [PA_COMMAND_PLAYBACK_STREAM_KILLED] = "PLAYBACK_STREAM_KILLED",
55 [PA_COMMAND_RECORD_STREAM_KILLED] = "RECORD_STREAM_KILLED",
56 [PA_COMMAND_STAT] = "STAT",
57 [PA_COMMAND_GET_PLAYBACK_LATENCY] = "PLAYBACK_LATENCY",
58 [PA_COMMAND_CREATE_UPLOAD_STREAM] = "CREATE_UPLOAD_STREAM",
59 [PA_COMMAND_DELETE_UPLOAD_STREAM] = "DELETE_UPLOAD_STREAM",
60 [PA_COMMAND_FINISH_UPLOAD_STREAM] = "FINISH_UPLOAD_STREAM",
61 [PA_COMMAND_PLAY_SAMPLE] = "PLAY_SAMPLE",
62 [PA_COMMAND_REMOVE_SAMPLE] = "REMOVE_SAMPLE",
63 };
64
65 #endif
66
67 struct reply_info {
68 struct pa_pdispatch *pdispatch;
69 PA_LLIST_FIELDS(struct reply_info);
70 void (*callback)(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
71 void *userdata;
72 uint32_t tag;
73 struct pa_time_event *time_event;
74 };
75
76 struct pa_pdispatch {
77 int ref;
78 struct pa_mainloop_api *mainloop;
79 const struct pa_pdispatch_command *command_table;
80 unsigned n_commands;
81 PA_LLIST_HEAD(struct reply_info, replies);
82 void (*drain_callback)(struct pa_pdispatch *pd, void *userdata);
83 void *drain_userdata;
84 };
85
86 static void reply_info_free(struct reply_info *r) {
87 assert(r && r->pdispatch && r->pdispatch->mainloop);
88
89 if (r->time_event)
90 r->pdispatch->mainloop->time_free(r->time_event);
91
92 PA_LLIST_REMOVE(struct reply_info, r->pdispatch->replies, r);
93
94 pa_xfree(r);
95 }
96
97 struct pa_pdispatch* pa_pdispatch_new(struct pa_mainloop_api *mainloop, const struct pa_pdispatch_command*table, unsigned entries) {
98 struct pa_pdispatch *pd;
99 assert(mainloop);
100
101 assert((entries && table) || (!entries && !table));
102
103 pd = pa_xmalloc(sizeof(struct pa_pdispatch));
104 pd->ref = 1;
105 pd->mainloop = mainloop;
106 pd->command_table = table;
107 pd->n_commands = entries;
108 PA_LLIST_HEAD_INIT(struct pa_reply_info, pd->replies);
109 pd->drain_callback = NULL;
110 pd->drain_userdata = NULL;
111
112 return pd;
113 }
114
115 void pdispatch_free(struct pa_pdispatch *pd) {
116 assert(pd);
117
118 while (pd->replies)
119 reply_info_free(pd->replies);
120
121 pa_xfree(pd);
122 }
123
124 static void run_action(struct pa_pdispatch *pd, struct reply_info *r, uint32_t command, struct pa_tagstruct *ts) {
125 void (*callback)(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata);
126 void *userdata;
127 uint32_t tag;
128 assert(r);
129
130 pa_pdispatch_ref(pd);
131
132 callback = r->callback;
133 userdata = r->userdata;
134 tag = r->tag;
135
136 reply_info_free(r);
137
138 callback(pd, command, tag, ts, userdata);
139
140 if (pd->drain_callback && !pa_pdispatch_is_pending(pd))
141 pd->drain_callback(pd, pd->drain_userdata);
142
143 pa_pdispatch_unref(pd);
144 }
145
146 int pa_pdispatch_run(struct pa_pdispatch *pd, struct pa_packet*packet, void *userdata) {
147 uint32_t tag, command;
148 struct pa_tagstruct *ts = NULL;
149 int ret = -1;
150 assert(pd && packet && packet->data);
151
152 pa_pdispatch_ref(pd);
153
154 if (packet->length <= 8)
155 goto finish;
156
157 ts = pa_tagstruct_new(packet->data, packet->length);
158 assert(ts);
159
160 if (pa_tagstruct_getu32(ts, &command) < 0 ||
161 pa_tagstruct_getu32(ts, &tag) < 0)
162 goto finish;
163
164 #ifdef DEBUG_OPCODES
165 fprintf(stderr, __FILE__": Recieved opcode <%s>\n", command_names[command]);
166 #endif
167
168 if (command == PA_COMMAND_ERROR || command == PA_COMMAND_REPLY) {
169 struct reply_info *r;
170
171 for (r = pd->replies; r; r = r->next)
172 if (r->tag == tag)
173 break;
174
175 if (r)
176 run_action(pd, r, command, ts);
177
178 } else if (pd->command_table && command < pd->n_commands) {
179 const struct pa_pdispatch_command *c = pd->command_table+command;
180
181 if (c->proc)
182 c->proc(pd, command, tag, ts, userdata);
183 } else
184 goto finish;
185
186 ret = 0;
187
188 finish:
189 if (ts)
190 pa_tagstruct_free(ts);
191
192 pa_pdispatch_unref(pd);
193
194 return ret;
195 }
196
197 static void timeout_callback(struct pa_mainloop_api*m, struct pa_time_event*e, const struct timeval *tv, void *userdata) {
198 struct reply_info*r = userdata;
199 assert(r && r->time_event == e && r->pdispatch && r->pdispatch->mainloop == m && r->callback);
200
201 run_action(r->pdispatch, r, PA_COMMAND_TIMEOUT, NULL);
202 }
203
204 void pa_pdispatch_register_reply(struct pa_pdispatch *pd, uint32_t tag, int timeout, void (*cb)(struct pa_pdispatch *pd, uint32_t command, uint32_t tag, struct pa_tagstruct *t, void *userdata), void *userdata) {
205 struct reply_info *r;
206 struct timeval tv;
207 assert(pd && pd->ref >= 1 && cb);
208
209 r = pa_xmalloc(sizeof(struct reply_info));
210 r->pdispatch = pd;
211 r->callback = cb;
212 r->userdata = userdata;
213 r->tag = tag;
214
215 gettimeofday(&tv, NULL);
216 tv.tv_sec += timeout;
217
218 r->time_event = pd->mainloop->time_new(pd->mainloop, &tv, timeout_callback, r);
219 assert(r->time_event);
220
221 PA_LLIST_PREPEND(struct reply_info, pd->replies, r);
222 }
223
224 int pa_pdispatch_is_pending(struct pa_pdispatch *pd) {
225 assert(pd);
226
227 return !!pd->replies;
228 }
229
230 void pa_pdispatch_set_drain_callback(struct pa_pdispatch *pd, void (*cb)(struct pa_pdispatch *pd, void *userdata), void *userdata) {
231 assert(pd);
232 assert(!cb || pa_pdispatch_is_pending(pd));
233
234 pd->drain_callback = cb;
235 pd->drain_userdata = userdata;
236 }
237
238 void pa_pdispatch_unregister_reply(struct pa_pdispatch *pd, void *userdata) {
239 struct reply_info *r, *n;
240 assert(pd);
241
242 for (r = pd->replies; r; r = n) {
243 n = r->next;
244
245 if (r->userdata == userdata)
246 reply_info_free(r);
247 }
248 }
249
250 void pa_pdispatch_unref(struct pa_pdispatch *pd) {
251 assert(pd && pd->ref >= 1);
252
253 if (!(--(pd->ref)))
254 pdispatch_free(pd);
255 }
256
257 struct pa_pdispatch* pa_pdispatch_ref(struct pa_pdispatch *pd) {
258 assert(pd && pd->ref >= 1);
259 pd->ref++;
260 return pd;
261 }