Debian Codegraph @

librplay/async.c

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* $Id: async.c,v 1.2 1998/08/13 06:13:35 boyns Exp $ */
2
3 /*
4 * Copyright (C) 1995 Andrew Scherpbier <[email protected]>
5 *
6 * This file is part of rplay.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include "version.h"
27 #include "rplay.h"
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <sys/time.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <memory.h>
37 #ifdef __STDC__
38 # include <stdarg.h>
39 #else
40 # include <varargs.h>
41 #endif
42
43 typedef void (*cb)();
44
45 typedef struct obuf_s
46 {
47 struct obuf_s *next;
48 char *data;
49 char *current;
50 int length;
51 cb callback;
52 } obuf;
53
54 typedef struct ibuf_s
55 {
56 struct ibuf_s *next;
57 char *data;
58 char *current;
59 int length;
60 } ibuf;
61
62 typedef struct
63 {
64 obuf *olist;
65 obuf *otail;
66 ibuf *ilist;
67 ibuf *itail;
68 int writing;
69 int notify_mask;
70 cb rcallback;
71 int rraw;
72 cb wcallback;
73 int wraw;
74 } grp;
75
76
77 static grp group[FD_SETSIZE];
78 static int looping;
79 static int main_loop_return_value;
80
81
82 #ifdef __STDC__
83 static void write_proc(int rptp_fd);
84 static void do_register(int rptp_fd);
85 static void do_unregister(int rptp_fd);
86 static void notify_line(int rptp_fd, char *line);
87 static void read_proc(int rptp_fd);
88 static void process_input(int rptp_fd);
89 #else
90 static void write_proc(/* int rptp_fd */);
91 static void do_register(/* int rptp_fd */);
92 static void do_unregister(/* int rptp_fd */);
93 static void notify_line(/* int rptp_fd, char *line */);
94 static void read_proc(/* int rptp_fd */);
95 static void process_input(/* int rptp_fd */);
96 #endif
97
98
99 /*
100 * Builtin mainloop. Use this if not using another library's event handling routine
101 */
102 int
103 rptp_main_loop()
104 {
105 fd_set read_fds;
106 fd_set write_fds;
107 int i, n;
108
109 looping = 1;
110 main_loop_return_value = 0;
111 while (looping)
112 {
113 FD_ZERO(&read_fds);
114 FD_ZERO(&write_fds);
115
116 for (i = 0; i < FD_SETSIZE; i++)
117 {
118 if (group[i].olist)
119 FD_SET(i, &write_fds);
120 if (group[i].rcallback)
121 FD_SET(i, &read_fds);
122 }
123
124 n = select(FD_SETSIZE, &read_fds, &write_fds, NULL, NULL);
125 if (n < 0)
126 {
127 if (errno == EINTR)
128 continue;
129 return -1;
130 }
131
132 for (i = 0; i < FD_SETSIZE && n; i++)
133 {
134 if (FD_ISSET(i, &read_fds))
135 {
136 n--;
137 if (group[i].rcallback)
138 rptp_async_process(i, RPTP_ASYNC_READ);
139 }
140 if (FD_ISSET(i, &write_fds))
141 {
142 n--;
143 if (group[i].writing)
144 rptp_async_process(i, RPTP_ASYNC_WRITE);
145 }
146 }
147 }
148 return main_loop_return_value;
149 }
150
151
152 /*
153 * Terminate the mainloop at the earliest convenient time. The integer parameter
154 * will be returned by mainloop()
155 */
156 #ifdef __STDC__
157 void
158 rptp_stop_main_loop(int return_value)
159 #else
160 void
161 rptp_stop_main_loop(return_value)
162 int return_value;
163 #endif
164 {
165 looping = 0;
166 main_loop_return_value = return_value;
167 }
168
169
170 /*
171 * Write a line to the RPTP connection asynchronously.
172 *
173 * "\r\n" is appended to the string.
174 */
175 #ifdef __STDC__
176 int
177 rptp_async_putline(int rptp_fd, cb callback, char *fmt, ...)
178 #else
179 int
180 rptp_async_putline(va_alist)
181 va_dcl
182 #endif
183 {
184 va_list args;
185 char buf[RPTP_MAX_LINE];
186
187 #ifdef __STDC__
188 va_start(args, fmt);
189 #else
190 int rptp_fd;
191 char *fmt;
192 cb callback;
193 char *tmp;
194 va_start(args);
195 rptp_fd = va_arg(args, int);
196 tmp = va_arg(args, char *);
197 callback = (cb) tmp;
198 fmt = va_arg(args, char *);
199 #endif
200
201 if (rptp_fd < 0 || rptp_fd >= FD_SETSIZE)
202 {
203 rptp_errno = RPTP_ERROR_SOCKET;
204 return -1;
205 }
206
207 rptp_errno = RPTP_ERROR_NONE;
208
209 vsprintf(buf, fmt, args);
210 va_end(args);
211 strcat(buf, "\r\n");
212
213 return rptp_async_write(rptp_fd, callback, buf, strlen(buf)) != strlen(buf) ? -1 : 0;
214 }
215
216
217 /*
218 * Write nbytes to the RPTP connection asynchronously
219 *
220 * If a callback function is given, it will be called as soon as all
221 * nbytes have been written.
222 */
223 #ifdef __STDC__
224 int
225 rptp_async_write(int rptp_fd, cb callback, char *ptr, int nbytes)
226 #else
227 int
228 rptp_async_write(rptp_fd, callback, ptr, nbytes)
229 int rptp_fd;
230 cb callback;
231 char *ptr;
232 int nbytes;
233 #endif
234 {
235 obuf *new;
236
237 if (rptp_fd < 0 || rptp_fd >= FD_SETSIZE)
238 {
239 rptp_errno = RPTP_ERROR_SOCKET;
240 return -1;
241 }
242
243 /*
244 * Create a new obuf with a copy of the data that needs to be written
245 */
246 new = (obuf *) malloc(sizeof(obuf));
247 new->next = NULL;
248 if (nbytes > 0 && ptr)
249 {
250 new->data = malloc(nbytes);
251 memcpy(new->data, ptr, nbytes);
252 }
253 else /* allow empty data */
254 {
255 new->data = NULL;
256 }
257 new->current = new->data;
258 new->length = nbytes;
259 new->callback = callback;
260
261 /*
262 * Append the obuf to the list of obufs for the given file descriptor
263 */
264 if (group[rptp_fd].otail)
265 {
266 group[rptp_fd].otail->next = new;
267 group[rptp_fd].otail = new;
268 }
269 else
270 {
271 group[rptp_fd].otail = group[rptp_fd].olist = new;
272 }
273
274 /*
275 * If the output system is already writing we don't need to do anything else,
276 * but if not, we need to register our output function
277 */
278 if (!group[rptp_fd].writing)
279 {
280 do_register(rptp_fd);
281 }
282 }
283
284
285 /*
286 * Register a callback for I/O operations on a file descriptor.
287 * There are two modes of operation for this call. The mode
288 * depends on whether you use rptp_main_loop() or not.
289 * Case 1 (using rptp_main_loop()):
290 * Use rptp_async_register to register a callback for reading
291 * or writing to a file descriptor. In this case the
292 * what parameter is one of RPTP_ASYNC_READ or RPTP_ASYNC_WRITE | RPTP_ASYNC_RAW.
293 * A typical prototype for the callback will be something like:
294 * void callback(int fd);
295 * Case 2 (NOT using rptp_main_loop()):
296 * In this case, only RPTP_ASYNC_WRITE should be used. This will register
297 * a callback which will have different parameters than the previous case.
298 * void callback(int rptp_fd, int action);
299 * The action parameter will be one of RPTP_ASYNC_ENABLE or RPTP_ASYNC_DISABLE.
300 * If it is RPTP_ASYNC_ENABLE, the callback is supposed to register the rptp_fd
301 * file descriptor with whatever I/O processing system the application uses.
302 * For RPTP_ASYNC_DISABLE, it should be unregistered.
303 */
304 #ifdef __STDC__
305 void
306 rptp_async_register(int rptp_fd, int what, cb callback)
307 #else
308 void
309 rptp_async_register(rptp_fd, what, callback)
310 int rptp_fd;
311 int what;
312 cb callback;
313 #endif
314 {
315 if (rptp_fd < 0 || rptp_fd >= FD_SETSIZE)
316 {
317 rptp_errno = RPTP_ERROR_SOCKET;
318 return;
319 }
320
321 switch (what)
322 {
323 case RPTP_ASYNC_WRITE:
324 group[rptp_fd].wcallback = callback;
325 group[rptp_fd].wraw = 0;
326 break;
327
328 case RPTP_ASYNC_WRITE | RPTP_ASYNC_RAW:
329 group[rptp_fd].wcallback = callback;
330 group[rptp_fd].wraw = 1;
331 break;
332
333 case RPTP_ASYNC_READ:
334 group[rptp_fd].rcallback = callback;
335 group[rptp_fd].rraw = 1;
336 break;
337 }
338 }
339
340
341 /*
342 * This gets called to let us know that some I/O can be preformed.
343 * When rptp_main_loop() is not used, this function needs to be called
344 * when either data is available or data can be written to a file descriptor.
345 * The what parameter is one of RPTP_ASYNC_READ or RPTP_ASYNC_WRITE.
346 * When rptp_main_loop() is used, this function should NEVER be called
347 * by the application code.
348 */
349 #ifdef __STDC__
350 void
351 rptp_async_process(int rptp_fd, int what)
352 #else
353 void
354 rptp_async_process(rptp_fd, what)
355 int rptp_fd;
356 int what;
357 #endif
358 {
359 if (rptp_fd < 0 || rptp_fd >= FD_SETSIZE)
360 {
361 rptp_errno = RPTP_ERROR_SOCKET;
362 return;
363 }
364
365 switch (what)
366 {
367 case RPTP_ASYNC_READ:
368 if (group[rptp_fd].rraw && group[rptp_fd].rcallback)
369 (*group[rptp_fd].rcallback)(rptp_fd);
370 else
371 read_proc(rptp_fd);
372 break;
373
374 case RPTP_ASYNC_WRITE:
375 if (group[rptp_fd].wraw && group[rptp_fd].wcallback)
376 (*group[rptp_fd].wcallback)(rptp_fd);
377 else
378 write_proc(rptp_fd);
379 break;
380 }
381 }
382
383
384 /*
385 * Register a callback which will be called when data from a RPTP connection
386 * becomes available.
387 * The mask parameter specifies what kinds of events will be looked for and is
388 * the logical OR of RPTP_EVENT_* constants.
389 * The callback function takes 3 parameters. A typical prototype is something
390 * like:
391 * void callback(int rptp_fd, int event, char *string);
392 */
393 #ifdef __STDC__
394 void
395 rptp_async_notify(int rptp_fd, int mask, void (*callback)())
396 #else
397 void
398 rptp_async_notify(rptp_fd, mask, callback)
399 int rptp_fd;
400 int mask;
401 void (*callback)();
402 #endif
403 {
404 char command[RPTP_MAX_LINE];
405 char *p;
406
407 strcpy (command, "set notify=");
408
409 if (mask & RPTP_EVENT_CONTINUE)
410 strcat(command, "continue,");
411 if (mask & RPTP_EVENT_DONE)
412 strcat(command, "done,");
413 if (mask & RPTP_EVENT_PAUSE)
414 strcat(command, "pause,");
415 if (mask & RPTP_EVENT_PLAY)
416 strcat(command, "play,");
417 if (mask & RPTP_EVENT_SKIP)
418 strcat(command, "skip,");
419 if (mask & RPTP_EVENT_STATE)
420 strcat(command, "state,");
421 if (mask & RPTP_EVENT_STOP)
422 strcat(command, "stop,");
423 if (mask & RPTP_EVENT_VOLUME)
424 strcat(command, "volume,");
425 if (mask & RPTP_EVENT_FLOW)
426 strcat(command, "flow,");
427 if (mask & RPTP_EVENT_MODIFY)
428 strcat(command, "modify,");
429 if (mask & RPTP_EVENT_LEVEL)
430 strcat(command, "level,");
431 if (mask & RPTP_EVENT_POSITION)
432 strcat(command, "position,");
433
434 p = strrchr(command, ',');
435 if (!p)
436 strcat(command, "none");
437 else
438 *p = '\0';
439
440 rptp_async_putline(rptp_fd, NULL, command);
441
442 group[rptp_fd].rcallback = callback;
443 group[rptp_fd].notify_mask = mask;
444 group[rptp_fd].rraw = 0;
445 }
446
447
448 /*-------------------------------------------------------------------------
449 * Start of static routines. All functions below are only utility routines
450 * for this module
451 */
452
453 /*
454 * Attempt to write data from the buffers to an fd.
455 */
456 #ifdef __STDC__
457 static void
458 write_proc(int rptp_fd)
459 #else
460 static void
461 write_proc(rptp_fd)
462 #endif
463 {
464 obuf *current;
465 int n;
466
467 if (!group[rptp_fd].olist)
468 {
469 do_unregister(rptp_fd);
470 }
471
472 current = group[rptp_fd].olist;
473 if (current->current)
474 {
475 n = write(rptp_fd, current->current, current->length);
476 if (n < 0)
477 {
478 return;
479 }
480 }
481 else
482 {
483 n = 0;
484 }
485 current->length -= n;
486 current->current += n;
487 if (current->length <= 0)
488 {
489 if (current->callback)
490 {
491 (*current->callback)(rptp_fd);
492 }
493 if (current->data)
494 {
495 free(current->data);
496 }
497 group[rptp_fd].olist = current->next;
498 if (group[rptp_fd].otail == current)
499 {
500 group[rptp_fd].otail = NULL;
501 }
502 free(current);
503
504 if (!group[rptp_fd].olist)
505 {
506 do_unregister(rptp_fd);
507 }
508 }
509 }
510
511
512 /*
513 * Attempt to read data from an fd to the buffer.
514 */
515 #ifdef __STDC__
516 static void
517 read_proc(int rptp_fd)
518 #else
519 static void
520 read_proc(rptp_fd)
521 #endif
522 {
523 char buffer[RPTP_MAX_LINE];
524 int n;
525 ibuf *new;
526
527 n = read(rptp_fd, buffer, sizeof(buffer));
528 if (n <= 0)
529 {
530 if (group[rptp_fd].notify_mask & RPTP_EVENT_CLOSE && group[rptp_fd].rcallback)
531 {
532 (*group[rptp_fd].rcallback)(rptp_fd, RPTP_EVENT_CLOSE, "");
533 group[rptp_fd].rcallback = NULL;
534 }
535 return; /* Don't do anything when we get an error. We'll get it later */
536 }
537
538 new = (ibuf *) malloc(sizeof(ibuf));
539 new->next = NULL;
540 new->data = malloc(n);
541 memcpy(new->data, buffer, n);
542 new->length = n;
543 new->current = new->data;
544
545 if (group[rptp_fd].itail)
546 {
547 group[rptp_fd].itail->next = new;
548 group[rptp_fd].itail = new;
549 }
550 else
551 {
552 group[rptp_fd].itail = group[rptp_fd].ilist = new;
553 }
554
555 process_input(rptp_fd);
556 }
557
558
559 /*
560 * Attempt to make complete lines from the input buffers for the given
561 * file descriptor.
562 */
563 #ifdef __STDC__
564 static void
565 process_input(int rptp_fd)
566 #else
567 static void
568 process_input(rptp_fd)
569 int rptp_fd;
570 #endif
571 {
572 char *p;
573 ibuf *ptr;
574 static char line[RPTP_MAX_LINE * 2] = "";
575 static int length = 0;
576
577 while ((ptr = group[rptp_fd].ilist))
578 {
579 if (p = memchr(ptr->current, '\r', ptr->length))
580 {
581 memcpy(&line[length], ptr->current, p - ptr->current);
582 length += p - ptr->current;
583 line[length] = '\0';
584 notify_line(rptp_fd, line);
585 length = 0;
586
587 p+= 2;
588 if (p < ptr->current + ptr->length)
589 {
590 /*
591 * There is still unused data in this block.
592 */
593 ptr->length -= p - ptr->current;
594 ptr->current = p;
595 }
596 else
597 {
598 free(ptr->data);
599 if (ptr == group[rptp_fd].itail)
600 {
601 group[rptp_fd].ilist = group[rptp_fd].itail = NULL;
602 }
603 else
604 {
605 group[rptp_fd].ilist = ptr->next;
606 }
607 free(ptr);
608 }
609 }
610 else
611 {
612 /*
613 * The current block of data doesn't contain any carriage
614 * returns. We'll just add it to our line and get rid of it.
615 */
616 memcpy(&line[length], ptr->current, ptr->length);
617 length += ptr->length;
618 free(ptr->data);
619 if (ptr == group[rptp_fd].itail)
620 {
621 group[rptp_fd].ilist = group[rptp_fd].itail = NULL;
622 }
623 else
624 {
625 group[rptp_fd].ilist = ptr->next;
626 }
627 free(ptr);
628 }
629 }
630 }
631
632
633 /*
634 * A mapping between event and event mask
635 */
636 static struct
637 {
638 char *event;
639 int mask;
640 } events[] =
641 {
642 {"continue", RPTP_EVENT_CONTINUE},
643 {"done", RPTP_EVENT_DONE},
644 {"pause", RPTP_EVENT_PAUSE},
645 {"play", RPTP_EVENT_PLAY},
646 {"skip", RPTP_EVENT_SKIP},
647 {"state", RPTP_EVENT_STATE},
648 {"stop", RPTP_EVENT_STOP},
649 {"volume", RPTP_EVENT_VOLUME},
650 {"flow", RPTP_EVENT_FLOW},
651 {"modify", RPTP_EVENT_MODIFY},
652 {"level", RPTP_EVENT_LEVEL},
653 {"position", RPTP_EVENT_POSITION},
654 {NULL, 0}
655 };
656
657
658 /*
659 * Tell the application that a line has arrived from a connection
660 */
661 #ifdef __STDC__
662 static void
663 notify_line(int rptp_fd, char *line)
664 #else
665 static void
666 notify_line(rptp_fd, line)
667 int rptp_fd;
668 char *line;
669 #endif
670 {
671 int what;
672 char *event;
673 int i;
674
675 /*
676 * Make sure that there is a callback registered. No use doing
677 * anything without one...
678 */
679 if (!group[rptp_fd].rcallback)
680 return;
681
682 switch (*line)
683 {
684 case '+':
685 what = RPTP_EVENT_OK;
686 break;
687
688 case '-':
689 what = RPTP_EVENT_ERROR;
690 break;
691
692 case '!':
693 what = RPTP_EVENT_TIMEOUT;
694 break;
695
696 case '@':
697 event = rptp_parse(line, "event");
698 what = RPTP_EVENT_OTHER;
699 for (i = 0; events[i].event; i++)
700 {
701 if (strcmp(events[i].event, event) == 0)
702 {
703 what = events[i].mask;
704 break;
705 }
706 }
707 break;
708
709 default:
710 what = RPTP_EVENT_OTHER;
711 break;
712 }
713 if (what & group[rptp_fd].notify_mask)
714 {
715 (*group[rptp_fd].rcallback)(rptp_fd, what, line);
716 }
717 #if 0
718 else
719 {
720 printf("Input line being ignored: '%s'\n", line);
721 }
722 #endif
723 }
724
725
726 /*
727 * Register us with whatever controls the write_fds on the select
728 */
729 #ifdef __STDC__
730 static void
731 do_register(int rptp_fd)
732 #else
733 static void
734 do_register(rptp_fd)
735 int rptp_fd;
736 #endif
737 {
738 if (group[rptp_fd].wcallback)
739 {
740 (*group[rptp_fd].wcallback)(rptp_fd, RPTP_ASYNC_ENABLE);
741 }
742 group[rptp_fd].writing = 1;
743 }
744
745
746 /*
747 * Unregister us with whatever controls the write_fds on the select
748 */
749 #ifdef __STDC__
750 static void
751 do_unregister(int rptp_fd)
752 #else
753 static void
754 do_unregister(rptp_fd)
755 int rptp_fd;
756 #endif
757 {
758 if (group[rptp_fd].wcallback)
759 {
760 (*group[rptp_fd].wcallback)(rptp_fd, RPTP_ASYNC_DISABLE);
761 }
762 group[rptp_fd].writing = 0;
763 }
764
765
766 /*
767 * Local variables:
768 * tab-width: 4
769 * End:
770 */