Debian Codegraph @

rplayd/connection.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: connection.c,v 1.6 1999/03/10 07:58:03 boyns Exp $ */
2
3 /*
4 * Copyright (C) 1993-99 Mark R. Boyns <[email protected]>
5 *
6 * This file is part of rplay.
7 *
8 * rplay 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 * rplay 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 rplay; see the file COPYING. If not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27 #include <sys/time.h>
28 #include <sys/errno.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #ifdef HAVE_STRING_H
32 #include <string.h>
33 #endif
34 #ifdef __STDC__
35 #include <stdarg.h>
36 #else
37 #include <varargs.h>
38 #endif
39 #include "rplayd.h"
40 #include "connection.h"
41 #include "command.h"
42 #include "version.h"
43 #include "sound.h"
44 #include "server.h"
45 #include "host.h"
46 #include "cache.h"
47 #include "rplay.h"
48 #include "spool.h"
49 #include "timer.h"
50 #include "buffer.h"
51 #include "strdup.h"
52
53 CONNECTION *connections = NULL;
54 int nconnections = 0;
55 int connection_want_level_notify = 0;
56 int connection_level_notify = 0;
57
58 #undef EVENT_DEBUG
59 #ifdef EVENT_DEBUG
60 static void event_print(CONNECTION *c, char *message);
61 #endif
62
63 /*
64 * create a connection object
65 * types are CONNECTION_CLIENT and CONNECTION_SERVER
66 */
67 #ifdef __STDC__
68 CONNECTION *
69 connection_create(int type)
70 #else
71 CONNECTION *
72 connection_create(type)
73 int type;
74 #endif
75 {
76 CONNECTION *c;
77 int i;
78
79 c = (CONNECTION *) malloc(sizeof(CONNECTION));
80 if (c == NULL)
81 {
82 report(REPORT_ERROR, "connection_create: out of memory\n");
83 done(1);
84 }
85 c->type = type;
86 c->fd = -1;
87 c->event = NULL;
88 c->ep = &c->event;
89 c->server = NULL;
90 c->time = time(0);
91 c->application = NULL;
92 c->notify_mask = 0;
93 c->notify_id = 0;
94 for (i = 0; i < NOTIFY_RATE_MAX; i++)
95 {
96 c->notify_rate[i].rate = 0.0;
97 c->notify_rate[i].next = 0.0;
98 }
99 c->monitor = 0;
100
101 /*
102 * insert new connection into the connection list
103 */
104 c->prev = NULL;
105 c->next = connections;
106 if (connections)
107 {
108 connections->prev = c;
109 }
110 connections = c;
111
112 if (type == CONNECTION_CLIENT)
113 {
114 nconnections++;
115 }
116
117 return c;
118 }
119
120 /*
121 * destroy the connection and remove it from the connection list
122 */
123 #ifdef __STDC__
124 void
125 connection_destroy(CONNECTION *c)
126 #else
127 void
128 connection_destroy(c)
129 CONNECTION *c;
130 #endif
131 {
132 if (c->type == CONNECTION_CLIENT)
133 {
134 nconnections--;
135 }
136 if (c->prev)
137 {
138 c->prev->next = c->next;
139 }
140 else
141 {
142 connections = c->next;
143 }
144 if (c->next)
145 {
146 c->next->prev = c->prev;
147 }
148 if (c->application)
149 {
150 free(c->application);
151 }
152 free((char *) c);
153
154 connection_want_level_notify = 0;
155 for (c = connections; c; c = c->next)
156 {
157 if (BIT(c->notify_mask, NOTIFY_LEVEL)
158 || (c->event && BIT(c->event->wait_mask, NOTIFY_LEVEL)))
159 {
160 connection_want_level_notify++;
161 }
162 }
163 }
164
165 /*
166 * start a server connection to try and find the given sound
167 *
168 * The sound can be NULL which means that events have already
169 * been created and will be forwarded to this server.
170 */
171 #ifdef __STDC__
172 CONNECTION *
173 connection_server_open(SERVER *server, SOUND *sound)
174 #else
175 CONNECTION *
176 connection_server_open(server, sound)
177 SERVER *server;
178 SOUND *sound;
179 #endif
180 {
181 int n;
182 CONNECTION *c;
183 EVENT *e;
184
185 /*
186 * see if there is already a connection to that server
187 */
188 for (c = connections; c; c = c->next)
189 {
190 if (c->type == CONNECTION_SERVER && c->server == server)
191 {
192 break;
193 }
194 }
195 if (c == NULL)
196 {
197 c = connection_create(CONNECTION_SERVER);
198 c->server = server;
199 c->sin = server->sin;
200 }
201 if (c->fd == -1)
202 {
203 c->fd = socket(AF_INET, SOCK_STREAM, 0);
204 if (c->fd < 0)
205 {
206 report(REPORT_ERROR, "connection_open_server: socket: %s\n", sys_err_str(errno));
207 done(1);
208 }
209 fd_nonblock(c->fd);
210
211 /*
212 * ping the server
213 */
214 connection_server_ping(c);
215
216 /*
217 * try to connect to the server immediately
218 */
219 report(REPORT_DEBUG, "%s server connection attempt\n", inet_ntoa(c->server->sin.sin_addr));
220 n = connect(c->fd, (struct sockaddr *) &c->server->sin, sizeof(c->server->sin));
221 e = event_create(EVENT_CONNECT);
222 e->time = time(0);
223 event_insert(c, e);
224 }
225
226 /*
227 * create events to try and find the sound
228 */
229 if (sound)
230 {
231 BUFFER *b;
232
233 b = buffer_create();
234 SNPRINTF(SIZE(b->buf, BUFFER_SIZE), "find %s\r\n", sound->name);
235 b->nbytes = strlen(b->buf);
236 e = event_create(EVENT_WRITE_FIND, b, sound);
237 event_insert(c, e);
238 }
239
240 return c;
241 }
242
243 #ifdef __STDC__
244 void
245 connection_server_reopen(CONNECTION *c)
246 #else
247 void
248 connection_server_reopen(c)
249 CONNECTION *c;
250 #endif
251 {
252 EVENT *e;
253 CONNECTION *server;
254
255 close(c->fd);
256 c->fd = -1;
257 e = c->event;
258 c->event = NULL;
259 c->ep = &c->event;
260
261 server = connection_server_open(c->server, NULL);
262 if (server != c)
263 {
264 report(REPORT_ERROR, "connection_server_reopen: server != c\n");
265 done(1);
266 }
267 event_insert(server, e);
268 }
269
270 /*
271 * send a ping packet to the server
272 */
273 #ifdef __STDC__
274 void
275 connection_server_ping(CONNECTION *c)
276 #else
277 void
278 connection_server_ping(c)
279 CONNECTION *c;
280 #endif
281 {
282 struct sockaddr_in ping_addr;
283
284 memcpy((char *) &ping_addr, (char *) &c->server->sin, sizeof(ping_addr));
285 ping_addr.sin_port = htons(RPLAY_PORT);
286 rplay_ping_sockaddr_in(&ping_addr);
287 }
288
289 /*
290 * open a client connection
291 */
292 #ifdef __STDC__
293 void
294 connection_client_open(int sock_fd)
295 #else
296 void
297 connection_client_open(sock_fd)
298 int sock_fd;
299 #endif
300 {
301 struct sockaddr_in f;
302 int fd, flen = sizeof(f);
303 CONNECTION *c;
304 EVENT *e;
305 char line[RPTP_MAX_LINE];
306
307 fd = accept(sock_fd, (struct sockaddr *) &f, &flen);
308 if (fd < 0)
309 {
310 report(REPORT_ERROR, "accept: %s\n", sys_err_str(errno));
311 return;
312 }
313
314 #ifdef DEBUG
315 report(REPORT_DEBUG, "%s client connection request\n", inet_ntoa(f.sin_addr));
316 #endif
317
318 #ifdef AUTH
319 /*
320 * see if this host has any access
321 */
322 if (!host_access(f, HOST_READ) && !host_access(f, HOST_WRITE)
323 && !host_access(f, HOST_WRITE))
324 {
325 report(REPORT_NOTICE, "%s RPTP access denied\n", inet_ntoa(f.sin_addr));
326 SNPRINTF(SIZE(line, sizeof(line)), "%cerror=\"connection refused\"\r\n", RPTP_ERROR);
327 write(fd, line, strlen(line));
328 close(fd);
329 return;
330 }
331 #endif /* AUTH */
332
333 /*
334 * see if there are too many connections
335 */
336 if (nconnections + 1 == RPTP_MAX_CONNECTIONS)
337 {
338 SNPRINTF(SIZE(line, sizeof(line)), "%cerror=\"connection refused\"\r\n", RPTP_ERROR);
339 write(fd, line, strlen(line));
340 close(fd);
341 return;
342 }
343
344 c = connection_create(CONNECTION_CLIENT);
345 c->fd = fd;
346 fd_nonblock(c->fd);
347 c->sin = f;
348
349 #if 1
350 report(REPORT_NOTICE, "%s client connection established\n", inet_ntoa(c->sin.sin_addr));
351 #endif
352
353 /* Greet the connection with the server's status. */
354 e = event_create(EVENT_WRITE, rplayd_status());
355 event_insert(c, e);
356 }
357
358 /*
359 * Update connections by monitoring the read and write fd_sets.
360 *
361 * If the given fd_sets are NULL then select must be called to update
362 * the fd_sets. (This should not be necessary anymore)
363 */
364 #ifdef __STDC__
365 void
366 connection_update(fd_set * read_fds, fd_set * write_fds)
367 #else
368 void
369 connection_update(read_fds, write_fds)
370 fd_set *read_fds;
371 fd_set *write_fds;
372 #endif
373 {
374 int i, n, size;
375 CONNECTION *c;
376 struct timeval tv;
377 fd_set rfds, wfds;
378 time_t t;
379 char buf[RPTP_MAX_LINE];
380 EVENT *e;
381 SOUND *s;
382 SPOOL *sp;
383 fd_set cmask;
384
385 if (read_fds == NULL && write_fds == NULL)
386 {
387 int nfds;
388
389 tv.tv_sec = 0;
390 tv.tv_usec = 0;
391 rfds = read_mask;
392 wfds = write_mask;
393 FD_CLR(rplay_fd, &rfds);
394 #ifdef __hpux
395 nfds = select(FD_SETSIZE, (int *) &rfds, (int *) &wfds, 0, &tv);
396 #else
397 nfds = select(FD_SETSIZE, &rfds, &wfds, 0, &tv);
398 #endif
399 if (nfds == 0)
400 {
401 /* nothing to do */
402 return;
403 }
404 else if (nfds < 0)
405 {
406 report(REPORT_ERROR, "connection_update: select: %s\n",
407 sys_err_str(errno));
408 done(1);
409 }
410 }
411 else
412 {
413 rfds = *read_fds;
414 wfds = *write_fds;
415 }
416
417 /*
418 * Check for new RPTP connections.
419 */
420 if (FD_ISSET(rptp_fd, &rfds))
421 {
422 connection_client_open(rptp_fd);
423 }
424 #ifdef OTHER_RPLAY_PORTS
425 if (FD_ISSET(other_rptp_fd, &rfds))
426 {
427 connection_client_open(other_rptp_fd);
428 }
429 #endif
430
431 t = time(0);
432
433 /* It's not safe to simply traverse the connection list
434 since connections can be closed during this loop.
435 Instead a bitmask is used to keep track of the connections
436 that have been updated. (efence) */
437 FD_ZERO(&cmask);
438 for (;;)
439 {
440 for (c = connections; c; c = c->next)
441 {
442 if (!FD_ISSET(c->fd, &cmask))
443 {
444 break;
445 }
446 }
447 if (c == NULL)
448 {
449 break;
450 }
451 FD_SET(c->fd, &cmask);
452
453 /* connection has no events */
454 if (c->event == NULL)
455 {
456 continue;
457 }
458
459 /*
460 * read events
461 */
462 if (FD_ISSET(c->fd, &rfds))
463 {
464 c->time = t;
465
466 switch (c->event->type)
467 {
468 case EVENT_READ_SOUND:
469 restart:
470 n = read(c->fd, c->event->buffer->buf,
471 MIN(c->event->nleft, sizeof(c->event->buffer->buf)));
472 if (n < 0 && (errno == EINTR || errno == EAGAIN))
473 {
474 goto restart;
475 }
476 #ifdef DEBUG
477 report(REPORT_DEBUG, "- %s read %d (%d)\n",
478 inet_ntoa(c->sin.sin_addr),
479 n, c->event->nleft);
480 #endif
481
482 if (n <= 0)
483 {
484 event_update(c);
485 }
486 else
487 {
488 int r;
489
490 restart1:
491 r = write(c->event->fd, c->event->buffer->buf, n);
492 if (r < 0 && (errno == EINTR || errno == EAGAIN))
493 {
494 goto restart1;
495 }
496 c->event->nleft -= n;
497 c->event->byte_offset += n;
498 if (c->event->nleft == 0)
499 {
500 c->event->success++;
501 event_update(c);
502 }
503 }
504 break;
505
506 case EVENT_READ_FLOW:
507 /* Find the spool entry. */
508 sp = spool_find(c->event->id);
509 if (!sp)
510 {
511 if (c->event->nbytes == -1) /* single-put flow */
512 {
513 event_update(c);
514 break;
515 }
516
517 /* The spool entry no longer exists. The rest of
518 this flow must be read -- the next `put' will
519 fail. */
520 c->event->buffer = buffer_create();
521 retry:
522 n = read(c->fd, c->event->buffer->buf,
523 MIN(BUFFER_SIZE, c->event->nleft));
524 if (n < 0 && (errno == EINTR || errno == EAGAIN))
525 {
526 goto retry;
527 }
528 #if 0
529 report(REPORT_DEBUG, "eating %d flow bytes (%d), id=#%d\n",
530 n, c->event->nleft, c->event->id);
531 #endif
532 buffer_destroy(c->event->buffer); /* Eat the flow. */
533 c->event->buffer = NULL;
534 if (n <= 0)
535 {
536 event_update(c);
537 break;
538 }
539 c->event->nleft -= n;
540 if (c->event->nleft <= 0)
541 {
542 c->event->success++;
543 event_update(c);
544 break;
545 }
546 break;
547 }
548
549 /* Pause the connection if the high_water_mark has been exceeded. */
550 if (sp->si
551 && ((sp->si->water_mark - sp->si->offset) >= sp->si->high_water_mark))
552 {
553 connection_notify(0, NOTIFY_FLOW, sp);
554 connection_flow_pause(sp);
555 break;
556 }
557
558 /* Read data from the flow. single-put flows will always
559 try to read BUFFER_SIZE. */
560 c->event->buffer = buffer_create();
561
562 /* calculate the number of bytes to read without
563 going over the high_water_mark. */
564 #if 1
565 size = MIN(BUFFER_SIZE,
566 sp->si ? sp->si->high_water_mark - (sp->si->water_mark - sp->si->offset)
567 : BUFFER_SIZE);
568 #else
569 size = MIN(BUFFER_SIZE, c->event->nbytes == -1 ? BUFFER_SIZE : c->event->nleft);
570 #endif
571 redo:
572 n = read(c->fd, c->event->buffer->buf, size);
573 if (n < 0 && (errno == EINTR || errno == EAGAIN))
574 {
575 goto redo;
576 }
577 if (n <= 0)
578 {
579 /* End of a single-put flow */
580 if (c->event->nbytes == -1)
581 {
582 spool_done(sp);
583 connection_close(c);
584 }
585 else
586 {
587 event_update(c);
588 }
589 break;
590 }
591
592 /* Add the buffer to the sound's flow. */
593 c->event->buffer->nbytes = n;
594 if (spool_flow_insert(sp, c->event->buffer) < 0)
595 {
596 c->event->buffer = NULL; /* efence */
597 break;
598 }
599 c->event->buffer = NULL;
600 c->event->byte_offset += n;
601 if (c->event->nbytes != -1)
602 {
603 c->event->nleft -= n;
604 }
605
606 /* single-put flows don't terminate here */
607 if (c->event->nbytes != -1 && c->event->nleft <= 0)
608 {
609 c->event->success++;
610 event_update(c);
611 }
612 break;
613
614 case EVENT_NOTIFY:
615 /* Replace the current notify event with a read_command. */
616 e = event_create(EVENT_READ_COMMAND, buffer_create());
617 event_replace(c, e);
618 /* fall fall fall */
619
620 default:
621 /* Read a command. */
622 restart2:
623 n = recv(c->fd, c->event->ptr, c->event->nleft, MSG_PEEK);
624 if (n < 0 && (errno == EINTR || errno == EAGAIN))
625 {
626 goto restart2;
627 }
628 #if 0
629 report(REPORT_DEBUG, "- %s recv %d (%d)\n",
630 inet_ntoa(c->sin.sin_addr),
631 n, c->event->nleft);
632 #endif
633 if (n <= 0)
634 {
635 event_update(c);
636 }
637 else
638 {
639 /*
640 * search for a newline and replace \r and \n with \0
641 */
642 for (i = 0; i < n; i++)
643 {
644 if (c->event->ptr[i] == '\r')
645 {
646 c->event->ptr[i] = '\0';
647 }
648 else if (c->event->ptr[i] == '\n')
649 {
650 c->event->ptr[i] = '\0';
651 c->event->success++;
652 break;
653 }
654 }
655 restart3:
656 n = read(c->fd, buf, i == n ? n : i + 1);
657 if (n < 0 && (errno == EINTR || errno == EAGAIN))
658 {
659 goto restart3;
660 }
661 if (n <= 0)
662 {
663 c->event->success = 0;
664 event_update(c);
665 }
666 else
667 {
668 c->event->ptr += n;
669 c->event->nleft -= n;
670 if (c->event->success || c->event->nleft == 0)
671 {
672 event_update(c);
673 }
674 }
675 }
676 break;
677 }
678 }
679 /*
680 * write events
681 */
682 else if (FD_ISSET(c->fd, &wfds))
683 {
684 c->time = t;
685
686 switch (c->event->type)
687 {
688 case EVENT_CONNECT:
689 event_update(c);
690 break;
691
692 default:
693 again:
694 n = write(c->fd, c->event->ptr, c->event->nleft);
695 #if 0
696 report(REPORT_DEBUG, "- %s write %d (%d)\n",
697 inet_ntoa(c->sin.sin_addr), n,
698 c->event->nleft);
699 #endif
700 if (n <= 0)
701 {
702 if ((errno == EINTR || errno == EAGAIN))
703 {
704 goto again;
705 }
706 event_update(c);
707 }
708 else
709 {
710 c->event->ptr += n;
711 c->event->nleft -= n;
712 c->event->byte_offset += n;
713 if (c->event->nleft == 0)
714 {
715 if (c->event->type == EVENT_WRITE_SOUND)
716 {
717 n = sound_fill(c->event->si, c->event->buffer, 1);
718 if (n > 0)
719 {
720 c->event->nleft = c->event->buffer->nbytes;
721 c->event->ptr = c->event->buffer->buf;
722 c->event->start = c->event->buffer->buf;
723 }
724 else
725 {
726 sound_close(c->event->si);
727 c->event->si = NULL; /* efence */
728 c->event->success++;
729 event_update(c);
730 }
731 }
732 else
733 {
734 c->event->success++;
735 event_update(c);
736 }
737 }
738 }
739 break;
740 }
741 }
742 }
743 }
744
745 /*
746 * close and destroy the connection
747 */
748 #ifdef __STDC__
749 void
750 connection_close(CONNECTION *c)
751 #else
752 void
753 connection_close(c)
754 CONNECTION *c;
755 #endif
756 {
757 EVENT *e, *next;
758 SOUND *s;
759 SPOOL *sp;
760
761 switch (c->type)
762 {
763 case CONNECTION_CLIENT:
764 report(REPORT_NOTICE, "%s client connection closed\n", inet_ntoa(c->sin.sin_addr));
765 break;
766
767 case CONNECTION_SERVER:
768 report(REPORT_NOTICE, "%s server connection closed\n", inet_ntoa(c->sin.sin_addr));
769 break;
770 }
771
772 FD_CLR(c->fd, &read_mask);
773 FD_CLR(c->fd, &write_mask);
774
775 if (c->monitor)
776 {
777 monitor_count--;
778 }
779
780 close(c->fd);
781
782 /*
783 * destroy any events that are left
784 */
785 for (e = c->event; e; e = next)
786 {
787 next = e->next;
788 switch (e->type)
789 {
790 case EVENT_WRITE_FIND:
791 case EVENT_READ_FIND_REPLY:
792 case EVENT_WRITE_GET:
793 case EVENT_READ_GET_REPLY:
794 case EVENT_READ_SOUND:
795 spool_remove(e->sound);
796 sound_delete(e->sound, 1);
797 break;
798
799 case EVENT_READ_FLOW:
800 case EVENT_WAIT_FLOW:
801 case EVENT_PIPE_FLOW:
802 /* Dunno if this should happen: */
803 #if 0
804 sp = spool_find(e->id);
805 s = sp->sound[sp->curr_sound];
806 spool_remove(s);
807 sound_delete(s, 0);
808 #endif
809 break;
810 }
811 event_destroy(e);
812 }
813
814 c->event = NULL;
815 c->ep = &c->event;
816
817 connection_destroy(c);
818 }
819
820 /*
821 * update the fd_sets for the connect
822 */
823 #ifdef __STDC__
824 void
825 connection_update_fdset(CONNECTION *c)
826 #else
827 void
828 connection_update_fdset(c)
829 CONNECTION *c;
830 #endif
831 {
832 FD_CLR(c->fd, &read_mask);
833 FD_CLR(c->fd, &write_mask);
834
835 if (c->event == NULL)
836 {
837 return;
838 }
839
840 switch (c->event->type)
841 {
842 case EVENT_READ_COMMAND:
843 case EVENT_READ_SOUND:
844 case EVENT_READ_CONNECT_REPLY:
845 case EVENT_READ_FIND_REPLY:
846 case EVENT_READ_GET_REPLY:
847 case EVENT_NOTIFY:
848 case EVENT_READ_FLOW:
849 case EVENT_WAIT_MONITOR:
850 FD_SET(c->fd, &read_mask);
851 break;
852
853 case EVENT_WRITE:
854 case EVENT_WRITE_SOUND:
855 case EVENT_WRITE_FIND:
856 case EVENT_WRITE_GET:
857 case EVENT_CONNECT:
858 case EVENT_WRITE_TIMEOUT:
859 case EVENT_WRITE_MONITOR:
860 FD_SET(c->fd, &write_mask);
861 break;
862
863 case EVENT_WAIT_FLOW:
864 case EVENT_PIPE_FLOW:
865 /* nothing */
866 break;
867
868 default:
869 report(REPORT_ERROR, "connection_update_fdset: unknown event type '%d'\n", c->event->type);
870 done(1);
871 }
872 }
873
874 /*
875 * send the connection a reply message
876 */
877 #ifdef __STDC__
878 void
879 connection_reply(CONNECTION *c, char *fmt,...)
880 #else
881 void
882 connection_reply(va_alist)
883 va_dcl
884 #endif
885 {
886 va_list args;
887 EVENT *e;
888 BUFFER *b;
889
890 #ifdef __STDC__
891 va_start(args, fmt);
892 #else
893 CONNECTION *c;
894 char *fmt;
895 va_start(args);
896 c = va_arg(args, CONNECTION *);
897 fmt = va_arg(args, char *);
898 #endif
899
900 b = buffer_create();
901
902 VSNPRINTF(SIZE(b->buf, BUFFER_SIZE), fmt, args);
903 va_end(args);
904 b->nbytes = strlen(b->buf);
905 SNPRINTF(SIZE(b->buf + b->nbytes, BUFFER_SIZE - b->nbytes), "\r\n");
906 b->nbytes += 2;
907
908 e = event_create(EVENT_WRITE, b);
909 event_first(c, e);
910 }
911
912 /*
913 * close all connections
914 */
915 void
916 connection_cleanup()
917 {
918 CONNECTION *c, *c_next;
919
920 for (c = connections; c; c = c_next)
921 {
922 c_next = c->next;
923 connection_close(c);
924 }
925 }
926
927 /*
928 * Check all connections to see if rplayd can enter idle mode.
929 */
930 int
931 connection_idle()
932 {
933 CONNECTION *c;
934 int idle = 1;
935
936 for (c = connections; c && idle; c = c->next)
937 {
938 if (c->event && c->event->type != EVENT_NOTIFY
939 && !c->monitor)
940 {
941 idle = 0;
942 }
943 }
944
945 return idle;
946 }
947
948 /*
949 * timeout all idle connections
950 */
951 void
952 connection_check_timeout()
953 {
954 time_t t;
955 CONNECTION *c, *next;
956
957 if (!rptp_timeout)
958 {
959 return;
960 }
961
962 t = time(0);
963 for (c = connections; c; c = next)
964 {
965 next = c->next;
966 if (t - c->time >= rptp_timeout)
967 {
968 connection_timeout(c);
969 }
970 }
971 }
972
973 /*
974 * timeout an idle connection
975 */
976 #ifdef __STDC__
977 void
978 connection_timeout(CONNECTION *c)
979 #else
980 void
981 connection_timeout(c)
982 CONNECTION *c;
983 #endif
984 {
985 BUFFER *b;
986 EVENT *e;
987
988 /*
989 * Don't timeout clients that are waiting.
990 */
991 if (c->event)
992 {
993 switch (c->event->type)
994 {
995 case EVENT_NOTIFY:
996 case EVENT_WAIT_MONITOR:
997 return;
998 }
999 }
1000
1001 if (c->type == CONNECTION_SERVER)
1002 {
1003 report(REPORT_DEBUG, "%s server connection timeout\n", inet_ntoa(c->sin.sin_addr));
1004 connection_close(c);
1005 }
1006 else
1007 {
1008 b = buffer_create();
1009 SNPRINTF(SIZE(b->buf, BUFFER_SIZE), "%cmessage=\"Connection timed out after %d idle seconds.\"\r\n",
1010 RPTP_TIMEOUT, rptp_timeout);
1011 b->nbytes += strlen(b->buf);
1012 e = event_create(EVENT_WRITE_TIMEOUT, b);
1013 event_replace(c, e);
1014 }
1015 }
1016
1017 /*
1018 * forward the sound request to the next server
1019 */
1020 #ifdef __STDC__
1021 void
1022 connection_server_forward_sound(CONNECTION *c, SOUND *s)
1023 #else
1024 void
1025 connection_server_forward_sound(c, s)
1026 CONNECTION *c;
1027 SOUND *s;
1028 #endif
1029 {
1030 CONNECTION *server;
1031
1032 if (c->server->next)
1033 {
1034 server = connection_server_open(c->server->next, s);
1035 }
1036 else
1037 {
1038 spool_remove(s);
1039 sound_delete(s, 1);
1040 }
1041 }
1042
1043 /*
1044 * forward all events to the next server
1045 */
1046 #ifdef __STDC__
1047 void
1048 connection_server_forward(CONNECTION *c)
1049 #else
1050 void
1051 connection_server_forward(c)
1052 CONNECTION *c;
1053 #endif
1054 {
1055 EVENT *e, *next;
1056 CONNECTION *server;
1057
1058 if (c->server->next)
1059 {
1060 server = connection_server_open(c->server->next, NULL);
1061 event_insert(server, c->event);
1062 }
1063 else
1064 {
1065 for (e = c->event; e; e = next)
1066 {
1067 next = e->next;
1068 switch (e->type)
1069 {
1070 case EVENT_WRITE_FIND:
1071 case EVENT_READ_FIND_REPLY:
1072 case EVENT_WRITE_GET:
1073 case EVENT_READ_GET_REPLY:
1074 case EVENT_READ_SOUND:
1075 spool_remove(e->sound);
1076 sound_delete(e->sound, 1);
1077 break;
1078 }
1079 event_destroy(e);
1080 }
1081 }
1082 c->event = NULL;
1083 c->ep = &c->event;
1084 }
1085
1086 BUFFER *
1087 connection_list_create()
1088 {
1089 CONNECTION *c;
1090 BUFFER *b, *connection_list;
1091 char line[RPTP_MAX_LINE];
1092 char buf[RPTP_MAX_LINE];
1093 int n, length;
1094 time_t t;
1095
1096 b = buffer_create();
1097 connection_list = b;
1098 SNPRINTF(SIZE(b->buf, BUFFER_SIZE), "+message=\"connections\"\r\n");
1099 b->nbytes += strlen(b->buf);
1100
1101 t = time(0);
1102
1103 for (c = connections; c; c = c->next)
1104 {
1105 length = 0;
1106 line[0] = '\0';
1107
1108 SNPRINTF(SIZE(buf, sizeof(buf)), "host=%s", inet_ntoa(c->sin.sin_addr));
1109 strncat(line + length, buf, sizeof(line) - length);
1110 length += strlen(buf);
1111
1112 SNPRINTF(SIZE(buf, sizeof(buf)), " type=");
1113 strncat(line + length, buf, sizeof(line) - length);
1114 length += strlen(buf);
1115
1116 buf[0] = '\0';
1117 switch (c->type)
1118 {
1119 case CONNECTION_SERVER:
1120 SNPRINTF(SIZE(buf, sizeof(buf)), "server");
1121 break;
1122
1123 case CONNECTION_CLIENT:
1124 SNPRINTF(SIZE(buf, sizeof(buf)), "client");
1125 break;
1126 }
1127 strncat(line + length, buf, sizeof(line) - length);
1128 length += strlen(buf);
1129
1130 if (c->event != NULL)
1131 {
1132 SNPRINTF(SIZE(buf, sizeof(buf)), " what=");
1133 strncat(line + length, buf, sizeof(line) - length);
1134 length += strlen(buf);
1135
1136 buf[0] = '\0';
1137 switch (c->event->type)
1138 {
1139 case EVENT_READ_COMMAND:
1140 SNPRINTF(SIZE(buf, sizeof(buf)), "idle");
1141 break;
1142
1143 case EVENT_CONNECT:
1144 case EVENT_READ_CONNECT_REPLY:
1145 SNPRINTF(SIZE(buf, sizeof(buf)), "connect");
1146 break;
1147
1148 case EVENT_WRITE_FIND:
1149 case EVENT_READ_FIND_REPLY:
1150 SNPRINTF(SIZE(buf, sizeof(buf)), "\"find %s\"", c->event->sound->name);
1151 break;
1152
1153 case EVENT_WRITE_GET:
1154 case EVENT_READ_GET_REPLY:
1155 SNPRINTF(SIZE(buf, sizeof(buf)), "\"get %s\"", c->event->sound->name);
1156 break;
1157
1158 case EVENT_READ_SOUND:
1159 if (c->type == CONNECTION_SERVER)
1160 SNPRINTF(SIZE(buf, sizeof(buf)), "\"get %s (%d/%d)\"",
1161 c->event->sound->name,
1162 c->event->byte_offset,
1163 c->event->nbytes);
1164 else
1165 SNPRINTF(SIZE(buf, sizeof(buf)), "\"put %s (%d/%d)\"",
1166 c->event->sound->name,
1167 c->event->byte_offset,
1168 c->event->nbytes);
1169 break;
1170
1171 case EVENT_WRITE:
1172 SNPRINTF(SIZE(buf, sizeof(buf)), "write");
1173 break;
1174
1175 case EVENT_WRITE_SOUND:
1176 SNPRINTF(SIZE(buf, sizeof(buf)), "\"get %s (%d/%d)\"",
1177 c->event->sound->name,
1178 c->event->byte_offset,
1179 c->event->nbytes);
1180 break;
1181
1182 case EVENT_NOTIFY:
1183 if (c->notify_mask)
1184 SNPRINTF(SIZE(buf, sizeof(buf)), "\"notify %08x\"",
1185 c->notify_mask);
1186 else
1187 SNPRINTF(SIZE(buf, sizeof(buf)), "\"wait %08x\"",
1188 c->event->wait_mask);
1189 break;
1190
1191 case EVENT_READ_FLOW:
1192 case EVENT_WAIT_FLOW:
1193 SNPRINTF(SIZE(buf, sizeof(buf)), "\"put #%d (%d/%d)\"",
1194 c->event->id,
1195 c->event->byte_offset,
1196 c->event->nbytes);
1197 break;
1198
1199 case EVENT_PIPE_FLOW:
1200 SNPRINTF(SIZE(buf, sizeof(buf)), "\"pipe #%d\"",
1201 c->event->id);
1202 break;
1203 }
1204 strncat(line + length, buf, sizeof(line) - length);
1205 length += strlen(buf);
1206 }
1207
1208 if (t - c->time)
1209 {
1210 SNPRINTF(SIZE(buf, sizeof(buf)), " idle=%s", time2string(t - c->time));
1211 strncat(line + length, buf, sizeof(line) - length);
1212 length += strlen(buf);
1213 }
1214
1215 if (c->application)
1216 {
1217 SNPRINTF(SIZE(buf, sizeof(buf)), " application=\"%s\"", c->application);
1218 strncat(line + length, buf, sizeof(line) - length);
1219 length += strlen(buf);
1220 }
1221
1222 SNPRINTF(SIZE(buf, sizeof(buf)), "\r\n");
1223 strncat(line + length, buf, sizeof(line) - length);
1224 length += strlen(buf);
1225
1226 if (b->nbytes + length > BUFFER_SIZE)
1227 {
1228 b->next = buffer_create();
1229 b = b->next;
1230 }
1231
1232 strncat(b->buf + b->nbytes, line, BUFFER_SIZE - b->nbytes);
1233 b->nbytes += length;
1234 }
1235
1236 if (b->nbytes + 3 > BUFFER_SIZE)
1237 {
1238 b->next = buffer_create();
1239 b = b->next;
1240 }
1241
1242 strncat(b->buf + b->nbytes, ".\r\n", BUFFER_SIZE - b->nbytes);
1243 b->nbytes += 3;
1244
1245 return connection_list;
1246 }
1247
1248 #ifdef __STDC__
1249 void
1250 connection_notify(CONNECTION *notify_connection, int notify_event,...)
1251 #else
1252 void
1253 connection_notify(va_alist)
1254 va_dcl
1255 #endif
1256 {
1257 static int prev_play = -1, prev_pause = -1, prev_volume = -1;
1258 CONNECTION *c;
1259 EVENT *e;
1260 BUFFER *b;
1261 char buf[RPTP_MAX_LINE];
1262 char head[3], *p;
1263 int new_volume, n, length;
1264 int force_notify = 0;
1265 time_t t;
1266 SPOOL *sp;
1267 va_list args;
1268
1269 #ifdef __STDC__
1270 va_start(args, notify_event);
1271 #else
1272 CONNECTION *notify_connection;
1273 int notify_event;
1274 va_start(args);
1275 notify_connection = va_arg(args, CONNECTION *);
1276 notify_event = va_arg(args, int);
1277 #endif
1278
1279 /* Extract event parameters. */
1280 switch (notify_event)
1281 {
1282 case NOTIFY_VOLUME: /* (int) */
1283 new_volume = va_arg(args, int);
1284 break;
1285
1286 case NOTIFY_PLAY: /* (SPOOL *) */
1287 case NOTIFY_PAUSE:
1288 case NOTIFY_CONTINUE:
1289 case NOTIFY_STOP:
1290 case NOTIFY_SKIP:
1291 case NOTIFY_DONE:
1292 case NOTIFY_FLOW:
1293 case NOTIFY_MODIFY:
1294 case NOTIFY_POSITION:
1295 sp = va_arg(args, SPOOL *);
1296 break;
1297
1298 case NOTIFY_STATE: /* none */
1299 break;
1300
1301 case NOTIFY_LEVEL: /* int */
1302 force_notify = va_arg(args, int);
1303 break;
1304 }
1305
1306 va_end(args);
1307
1308 t = time(0);
1309
1310 c = notify_connection ? notify_connection : connections;
1311 for (; c; c = c->next)
1312 {
1313 if (notify_connection && c != notify_connection)
1314 {
1315 break;
1316 }
1317
1318 if (BIT(c->notify_mask, notify_event)
1319 || (c->event && BIT(c->event->wait_mask, notify_event)))
1320 {
1321 strcpy(buf, "?event=");
1322 length = strlen(buf);
1323
1324 switch (notify_event)
1325 {
1326 case NOTIFY_DONE:
1327 if ((!c->notify_id || c->notify_id == sp->id)
1328 || (c->event && c->event->id == sp->id))
1329 {
1330 n = sp->curr_sound;
1331 if (n >= sp->rp->nsounds)
1332 {
1333 n--;
1334 }
1335 SNPRINTF(SIZE(buf + length, sizeof(buf) - length),
1336 "done id=#%d sound=\"%s\" client-data=\"%s\"",
1337 sp->id, sp->sound[n]->name, sp->curr_attrs->client_data);
1338 }
1339 else
1340 {
1341 continue;
1342 }
1343 break;
1344
1345 case NOTIFY_VOLUME:
1346 SNPRINTF(SIZE(buf + length, sizeof(buf) - length), "volume volume=%d",
1347 new_volume);
1348 break;
1349
1350 case NOTIFY_PLAY:
1351 if ((!c->notify_id || c->notify_id == sp->id)
1352 || (c->event && c->event->id == sp->id))
1353 {
1354 SOUND *s = sp->sound[sp->curr_sound];
1355
1356 SNPRINTF(SIZE(buf + length, sizeof(buf) - length), "play\
1357 id=#%d sound=\"%s\" host=%s volume=%d priority=%d count=%d seconds=%.2f size=%d\
1358 sample-rate=%d channels=%d bits=%g input=%s client-data=\"%s\"",
1359 sp->id,
1360 sp->curr_attrs->sound,
1361 inet_ntoa(sp->sin.sin_addr),
1362 sp->curr_attrs->volume,
1363 sp->rp->priority,
1364 sp->curr_attrs->count,
1365 (float) s->samples / sp->sample_rate,
1366 s->size,
1367 sp->sample_rate,
1368 s->channels,
1369 s->input_precision,
1370 input_to_string(s->type),
1371 sp->curr_attrs->client_data);
1372 }
1373 else
1374 {
1375 continue;
1376 }
1377 break;
1378
1379 case NOTIFY_STOP:
1380 if ((!c->notify_id || c->notify_id == sp->id)
1381 || (c->event && c->event->id == sp->id))
1382 {
1383 SNPRINTF(SIZE(buf + length, sizeof(buf) - length),
1384 "stop id=#%d sound=\"%s\" client-data=\"%s\"",
1385 sp->id, sp->curr_attrs->sound, sp->curr_attrs->client_data);
1386 }
1387 else
1388 {
1389 continue;
1390 }
1391 break;
1392
1393 case NOTIFY_PAUSE:
1394 if ((!c->notify_id || c->notify_id == sp->id)
1395 || (c->event && c->event->id == sp->id))
1396 {
1397 SNPRINTF(SIZE(buf + length, sizeof(buf) - length),
1398 "pause id=#%d sound=\"%s\" client-data=\"%s\"",
1399 sp->id, sp->curr_attrs->sound, sp->curr_attrs->client_data);
1400 }
1401 else
1402 {
1403 continue;
1404 }
1405 break;
1406
1407 case NOTIFY_CONTINUE:
1408 if ((!c->notify_id || c->notify_id == sp->id)
1409 || (c->event && c->event->id == sp->id))
1410 {
1411 SNPRINTF(SIZE(buf + length, sizeof(buf) - length),
1412 "continue id=#%d sound=\"%s\" client-data=\"%s\"",
1413 sp->id, sp->curr_attrs->sound, sp->curr_attrs->client_data);
1414 }
1415 else
1416 {
1417 continue;
1418 }
1419 break;
1420
1421 case NOTIFY_SKIP:
1422 if ((!c->notify_id || c->notify_id == sp->id)
1423 || (c->event && c->event->id == sp->id))
1424 {
1425 SNPRINTF(SIZE(buf + length, sizeof(buf) - length),
1426 "skip id=#%d sound=\"%s\" client-data=\"%s\"",
1427 sp->id, sp->curr_attrs->sound, sp->curr_attrs->client_data);
1428 }
1429 else
1430 {
1431 continue;
1432 }
1433 break;
1434
1435 case NOTIFY_STATE:
1436 SNPRINTF(SIZE(buf + length, sizeof(buf) - length),
1437 "state play=%d pause=%d volume=%d priority-threshold=%d audio-port=%s",
1438 spool_nplaying, spool_npaused,
1439 rplay_audio_volume, rplay_priority_threshold,
1440 audio_port_to_string(rplay_audio_port));
1441 break;
1442
1443 case NOTIFY_FLOW:
1444 if ((!c->notify_id || c->notify_id == sp->id)
1445 || (c->event && c->event->id == sp->id))
1446 {
1447 SNPRINTF(SIZE(buf + length, sizeof(buf) - length),
1448 "flow id=#%d sound=\"%s\" mark=%d low=%d high=%d client-data=\"%s\"",
1449 sp->id,
1450 sp->curr_attrs->sound,
1451 sp->si->water_mark - sp->si->offset,
1452 sp->si->low_water_mark,
1453 sp->si->high_water_mark,
1454 sp->curr_attrs->client_data);
1455 }
1456 else
1457 {
1458 continue;
1459 }
1460 break;
1461
1462 case NOTIFY_MODIFY:
1463 if ((!c->notify_id || c->notify_id == sp->id)
1464 || (c->event && c->event->id == sp->id))
1465 {
1466 SNPRINTF(SIZE(buf + length, sizeof(buf) - length), "modify \
1467 id=#%d count=%d list-count=%d priority=%d sample-rate=%d volume=%d client-data=\"%s\"",
1468 sp->id,
1469 sp->curr_count,
1470 sp->list_count,
1471 sp->rp->priority,
1472 sp->sample_rate,
1473 sp->curr_attrs->volume,
1474 sp->curr_attrs->client_data);
1475 }
1476 else
1477 {
1478 continue;
1479 }
1480 break;
1481
1482 case NOTIFY_LEVEL:
1483 if (c->notify_rate[NOTIFY_RATE_LEVEL].rate)
1484 {
1485 if (!force_notify
1486 && c->notify_rate[NOTIFY_RATE_LEVEL].next > timer_count)
1487 {
1488 continue;
1489 }
1490
1491 c->notify_rate[NOTIFY_RATE_LEVEL].next =
1492 timer_count + c->notify_rate[NOTIFY_RATE_LEVEL].rate;
1493 }
1494
1495 SNPRINTF(SIZE(buf + length, sizeof(buf) - length),
1496 "level volume=%d left=%d right=%d",
1497 rplay_audio_volume,
1498 rplay_audio_left_level,
1499 rplay_audio_right_level);
1500 break;
1501
1502 case NOTIFY_POSITION:
1503 if ((!c->notify_id || c->notify_id == sp->id)
1504 || (c->event && c->event->id == sp->id))
1505 {
1506 SOUND *s = sp->sound[sp->curr_sound];
1507
1508 if (c->notify_rate[NOTIFY_RATE_POSITION].rate)
1509 {
1510 if (c->notify_rate[NOTIFY_RATE_POSITION].next > timer_count)
1511 {
1512 continue;
1513 }
1514
1515 c->notify_rate[NOTIFY_RATE_POSITION].next =
1516 timer_count + c->notify_rate[NOTIFY_RATE_POSITION].rate;
1517 }
1518
1519 SNPRINTF(SIZE(buf + length, sizeof(buf) - length), "position \
1520 id=#%d position=%.2f remain=%.2f seconds=%.2f sample=%d samples=%d client-data=\"%s\"",
1521 sp->id,
1522 sp->sample_rate && s->samples ? sp->sample_index / sp->sample_rate : 0,
1523 sp->sample_rate && s->samples ? ((double) s->samples - sp->sample_index) / sp->sample_rate : 0,
1524 sp->sample_rate && s->samples ? (double) s->samples / sp->sample_rate : 0,
1525 s->samples ? (int) sp->sample_index : 0,
1526 s->samples,
1527 sp->curr_attrs->client_data);
1528 }
1529 else
1530 {
1531 continue;
1532 }
1533 }
1534
1535 length = strlen(buf);
1536 SNPRINTF(SIZE(buf + length, sizeof(buf) - length), "\r\n");
1537 length = strlen(buf);
1538
1539 /* Build the head array with the necessary RPTP headers. */
1540 n = 0;
1541 if (c->event && BIT(c->event->wait_mask, notify_event))
1542 {
1543 head[n++] = RPTP_OK;
1544 }
1545 if (BIT(c->notify_mask, notify_event))
1546 {
1547 head[n++] = RPTP_NOTIFY;
1548 }
1549 head[n] = '\0';
1550
1551 /* Setup write events to send `buf' using each RPTP header. */
1552 for (p = head; *p; p++)
1553 {
1554 buf[0] = *p;
1555
1556 /* Try to append `buf' to the last write event.
1557 This is an attempt use reuse existing write buffers
1558 to improve performance. */
1559 for (e = c->event; e && e->next; e = e->next) ;
1560 if (e && e->type == EVENT_WRITE
1561 && e->buffer->nbytes + length <= BUFFER_SIZE)
1562 {
1563 strcpy(e->buffer->buf + e->buffer->nbytes, buf);
1564 e->buffer->nbytes += length;
1565 e->nbytes += length;
1566 e->nleft += length;
1567 }
1568 else
1569 {
1570 b = buffer_create();
1571 strcpy(b->buf + b->nbytes, buf);
1572 b->nbytes += length;
1573 e = event_create(EVENT_WRITE, b);
1574 if (c->event && c->event->type == EVENT_NOTIFY)
1575 {
1576 event_replace(c, e);
1577 }
1578 else
1579 {
1580 event_insert_before(c, e,
1581 EVENT_READ_COMMAND
1582 | EVENT_READ_SOUND
1583 | EVENT_READ_CONNECT_REPLY
1584 | EVENT_READ_FIND_REPLY
1585 | EVENT_READ_GET_REPLY
1586 | EVENT_READ_FLOW
1587 | EVENT_WAIT_FLOW
1588 | EVENT_PIPE_FLOW);
1589 }
1590 }
1591 }
1592
1593 c->time = t;
1594 }
1595 }
1596
1597 /* Notify state changes. */
1598 switch (notify_event)
1599 {
1600 default:
1601 /* Only notify is the state has _really_ changed. */
1602 if (prev_play != spool_nplaying
1603 || prev_pause != spool_npaused
1604 || prev_volume != rplay_audio_volume
1605 || notify_connection)
1606 {
1607 prev_play = spool_nplaying;
1608 prev_pause = spool_npaused;
1609 prev_volume = rplay_audio_volume;
1610 /* Recursion is your friend. */
1611 connection_notify(notify_connection, NOTIFY_STATE);
1612 }
1613 break;
1614
1615 case NOTIFY_STATE:
1616 break;
1617 }
1618 }
1619
1620 #ifdef __STDC__
1621 void
1622 connection_flow_pause(SPOOL *sp)
1623 #else
1624 void
1625 connection_flow_pause(sp)
1626 SPOOL *sp;
1627 #endif
1628 {
1629 CONNECTION *c;
1630 EVENT *e;
1631
1632 for (c = connections; c; c = c->next)
1633 {
1634 for (e = c->event; e; e = e->next)
1635 {
1636 if (e->type == EVENT_READ_FLOW && e->id == sp->id)
1637 {
1638 e->type = EVENT_WAIT_FLOW;
1639 if (c->event == e)
1640 {
1641 connection_update_fdset(c);
1642 }
1643 break;
1644 }
1645 }
1646 }
1647 }
1648
1649 #ifdef __STDC__
1650 void
1651 connection_flow_continue(SPOOL *sp)
1652 #else
1653 void
1654 connection_flow_continue(sp)
1655 SPOOL *sp;
1656 #endif
1657 {
1658 CONNECTION *c;
1659 EVENT *e;
1660
1661 for (c = connections; c; c = c->next)
1662 {
1663 for (e = c->event; e; e = e->next)
1664 {
1665 if (e->type == EVENT_WAIT_FLOW && e->id == sp->id)
1666 {
1667 e->type = EVENT_READ_FLOW;
1668 if (c->event == e)
1669 {
1670 connection_update_fdset(c);
1671 }
1672 break;
1673 }
1674 }
1675 }
1676 }
1677
1678 #ifdef __STDC__
1679 void
1680 connection_monitor_pause(CONNECTION *c)
1681 #else
1682 void
1683 connection_monitor_pause(c)
1684 CONNECTION *c;
1685 #endif
1686 {
1687 if (c->event && c->event->type == EVENT_WRITE_MONITOR)
1688 {
1689 c->event->type = EVENT_WAIT_MONITOR;
1690 connection_update_fdset(c);
1691 }
1692 }
1693
1694 void
1695 connection_monitor_continue()
1696 {
1697 CONNECTION *c;
1698 EVENT *e;
1699
1700 for (c = connections; c; c = c->next)
1701 {
1702 for (e = c->event; e; e = e->next)
1703 {
1704 if (e->type == EVENT_WAIT_MONITOR)
1705 {
1706 e->type = EVENT_WRITE_MONITOR;
1707 if (c->event == e)
1708 {
1709 connection_update_fdset(c);
1710 }
1711 break;
1712 }
1713 }
1714 }
1715 }
1716
1717 #ifdef __STDC__
1718 void
1719 event_update(CONNECTION *c)
1720 #else
1721 void
1722 event_update(c)
1723 CONNECTION *c;
1724 #endif
1725 {
1726 int n, size;
1727 int fd;
1728 EVENT *e;
1729 time_t t;
1730 char *p;
1731 BUFFER *b;
1732
1733 if (c->event == NULL)
1734 {
1735 return;
1736 }
1737
1738 switch (c->event->type)
1739 {
1740 case EVENT_READ_COMMAND:
1741 if (c->event->success)
1742 {
1743 if (c->event->start[0] != '\0'
1744 && isascii(c->event->start[0]))
1745 {
1746 b = c->event->buffer; /* save the event's buffer */
1747 c->event->buffer = NULL;
1748 event_delete(c, 0);
1749
1750 if (command(c, b->buf) != 0)
1751 {
1752 connection_close(c);
1753 }
1754 buffer_destroy(b);
1755 }
1756 else
1757 {
1758 event_delete(c, 1);
1759 }
1760 }
1761 else
1762 {
1763 connection_close(c);
1764 }
1765 break;
1766
1767 case EVENT_CONNECT:
1768 /*
1769 * check the time to delay between connect attempts
1770 */
1771 t = time(0);
1772 if (t - c->event->time < RPTP_PING_DELAY)
1773 {
1774 break;
1775 }
1776 c->event->time = t;
1777
1778 /*
1779 * check the socket to see if it is connected
1780 */
1781 n = connect(c->fd, (struct sockaddr *) &c->server->sin, sizeof(c->server->sin));
1782 if (n == 0 || errno == EISCONN)
1783 {
1784 /*
1785 * got a connection
1786 */
1787 report(REPORT_NOTICE, "%s server connection established\n", inet_ntoa(c->server->sin.sin_addr));
1788 /*
1789 * prepare to read the connect reply, this must replace the
1790 * connect event
1791 */
1792 e = event_create(EVENT_READ_CONNECT_REPLY, buffer_create());
1793 event_replace(c, e);
1794 }
1795 else
1796 {
1797 /*
1798 * socket is not connected
1799 */
1800 c->event->nconnects++;
1801 report(REPORT_DEBUG, "%s server connection failed attempt #%d\n",
1802 inet_ntoa(c->server->sin.sin_addr),
1803 c->event->nconnects);
1804 if (c->event->nconnects < RPTP_CONNECT_ATTEMPTS)
1805 {
1806 switch (errno)
1807 {
1808 case ECONNREFUSED:
1809 case EINTR:
1810 case EAGAIN:
1811 case EINVAL:
1812 case EPIPE:
1813 /*
1814 * ping the server again
1815 */
1816 connection_server_ping(c);
1817
1818 FD_CLR(c->fd, &read_mask);
1819 FD_CLR(c->fd, &write_mask);
1820 close(c->fd);
1821 c->fd = socket(AF_INET, SOCK_STREAM, 0);
1822 fd_nonblock(c->fd);
1823
1824 /*
1825 * try and connect again
1826 */
1827 connect(c->fd, (struct sockaddr *) &c->server->sin, sizeof(c->server->sin));
1828 connection_update_fdset(c);
1829 return;
1830
1831 default:
1832 break;
1833 }
1834 }
1835
1836 /*
1837 * connection failed, remove connect event and forward all other
1838 * events to the next server
1839 */
1840 report(REPORT_NOTICE, "%s server connection failed\n", inet_ntoa(c->server->sin.sin_addr));
1841 event_delete(c, 1);
1842 connection_server_forward(c);
1843 connection_close(c);
1844 }
1845 break;
1846
1847 case EVENT_READ_CONNECT_REPLY:
1848 if (c->event->success)
1849 {
1850 if (c->event->start[0] == RPTP_OK)
1851 {
1852 event_delete(c, 1);
1853 }
1854 else
1855 {
1856 event_delete(c, 1);
1857 connection_server_forward(c);
1858 connection_close(c);
1859 }
1860 }
1861 else
1862 {
1863 report(REPORT_NOTICE, "cannot read connect reply from %s\n",
1864 inet_ntoa(c->server->sin.sin_addr));
1865 event_delete(c, 1);
1866 connection_server_forward(c);
1867 connection_close(c);
1868 }
1869 break;
1870
1871 case EVENT_WRITE_FIND:
1872 if (c->event->success)
1873 {
1874 e = event_create(EVENT_READ_FIND_REPLY, buffer_create(), c->event->sound);
1875 event_replace(c, e);
1876 }
1877 else
1878 {
1879 connection_server_forward(c);
1880 connection_close(c);
1881 }
1882 break;
1883
1884 case EVENT_READ_FIND_REPLY:
1885 if (c->event->success)
1886 {
1887 switch (c->event->start[0])
1888 {
1889 case RPTP_OK:
1890 if (strchr(c->event->start, '='))
1891 {
1892 size = atoi(rptp_parse(c->event->start, "size"));
1893 }
1894 else
1895 /* old-style */
1896 {
1897 p = strtok(c->event->start, " ");
1898 size = atoi(strtok(NULL, "\r\n"));
1899 }
1900 /*
1901 * see if the sound will fit in the cache
1902 */
1903 if (cache_free(size) < 0)
1904 {
1905 /*
1906 * the sound is too big for the cache
1907 */
1908 spool_remove(c->event->sound);
1909 sound_delete(c->event->sound, 1);
1910 event_delete(c, 1);
1911 break;
1912 }
1913 b = buffer_create();
1914 SNPRINTF(SIZE(b->buf, BUFFER_SIZE), "get %s\r\n", c->event->sound->name); /* old-style */
1915 b->nbytes = strlen(b->buf);
1916 e = event_create(EVENT_WRITE_GET, b, c->event->sound);
1917 event_replace(c, e);
1918 break;
1919
1920 case RPTP_ERROR:
1921 report(REPORT_NOTICE, "%s server does not have %s\n",
1922 inet_ntoa(c->sin.sin_addr), c->event->sound->name);
1923 connection_server_forward_sound(c, c->event->sound);
1924 event_delete(c, 1);
1925 break;
1926
1927 case RPTP_TIMEOUT:
1928 report(REPORT_NOTICE, "%s server connection timed out\n",
1929 inet_ntoa(c->sin.sin_addr));
1930 b = buffer_create();
1931 SNPRINTF(SIZE(b->buf, BUFFER_SIZE), "find %s\r\n", c->event->sound->name); /* old-style */
1932 b->nbytes = strlen(b->buf);
1933 e = event_create(EVENT_WRITE_FIND, b, c->event->sound);
1934 event_replace(c, e);
1935 connection_server_reopen(c);
1936 break;
1937
1938 default:
1939 report(REPORT_ERROR, "event_update: unknown find reply '%c'\n", c->event->start[0]);
1940 done(1);
1941 }
1942 }
1943 else
1944 {
1945 report(REPORT_NOTICE, "cannot read find reply from %s\n", inet_ntoa(c->sin.sin_addr));
1946 b = buffer_create();
1947 SNPRINTF(SIZE(b->buf, BUFFER_SIZE), "find %s\r\n", c->event->sound->name); /* old-style */
1948 b->nbytes = strlen(b->buf);
1949 e = event_create(EVENT_WRITE_FIND, b, c->event->sound);
1950 event_replace(c, e);
1951 connection_server_forward(c);
1952 connection_close(c);
1953 }
1954 break;
1955
1956 case EVENT_WRITE_GET:
1957 if (c->event->success)
1958 {
1959 e = event_create(EVENT_READ_GET_REPLY, buffer_create(), c->event->sound);
1960 event_replace(c, e);
1961 }
1962 else
1963 {
1964 b = buffer_create();
1965 SNPRINTF(SIZE(b->buf, BUFFER_SIZE), "find %s\r\n", c->event->sound->name); /* old-style */
1966 b->nbytes = strlen(b->buf);
1967 e = event_create(EVENT_WRITE_FIND, b, c->event->sound);
1968 event_replace(c, e);
1969 connection_server_forward(c);
1970 connection_close(c);
1971 }
1972 break;
1973
1974 case EVENT_READ_GET_REPLY:
1975 if (c->event->success)
1976 {
1977 char *sound_name;
1978
1979 switch (c->event->start[0])
1980 {
1981 case RPTP_OK:
1982 if (strchr(c->event->start, '='))
1983 {
1984 sound_name = rptp_parse(c->event->start, "sound");
1985 size = atoi(rptp_parse(0, "size"));
1986 }
1987 else
1988 /* old-style */
1989 {
1990 p = strtok(c->event->start, " ");
1991 p++;
1992 sound_name = p;
1993 size = atoi(strtok(NULL, "\r\n"));
1994 }
1995
1996 free((char *) c->event->sound->path);
1997 c->event->sound->path = strdup(cache_name(sound_name));
1998 c->event->sound->name = c->event->sound->path[0] == '/' ?
1999 strrchr(c->event->sound->path, '/') + 1 :
2000 c->event->sound->path;
2001 fd = cache_create(c->event->sound->path, size);
2002 if (fd < 0)
2003 {
2004 spool_remove(c->event->sound);
2005 sound_delete(c->event->sound, 1);
2006 event_delete(c, 1);
2007 }
2008 else
2009 {
2010 e = event_create(EVENT_READ_SOUND, fd, buffer_create(),
2011 size, c->event->sound);
2012 c->event->sound->status = SOUND_NOT_READY;
2013 event_replace(c, e);
2014 }
2015 break;
2016
2017 case RPTP_ERROR:
2018 report(REPORT_NOTICE, "%s server does not have %s\n",
2019 inet_ntoa(c->sin.sin_addr), c->event->sound->name);
2020 connection_server_forward_sound(c, c->event->sound);
2021 event_delete(c, 1);
2022 break;
2023
2024 case RPTP_TIMEOUT:
2025 report(REPORT_NOTICE, "%s server connection timed out\n",
2026 inet_ntoa(c->sin.sin_addr));
2027 b = buffer_create();
2028 SNPRINTF(SIZE(b->buf, BUFFER_SIZE), "get %s\r\n", c->event->sound->name); /* old-style */
2029 b->nbytes = strlen(b->buf);
2030 e = event_create(EVENT_WRITE_GET, b, c->event->sound);
2031 event_replace(c, e);
2032 connection_server_reopen(c);
2033 break;
2034
2035 default:
2036 report(REPORT_ERROR, "event_update: unknown get reply '%c'\n",
2037 c->event->start[0]);
2038 done(1);
2039 }
2040 }
2041 else
2042 {
2043 report(REPORT_NOTICE, "cannot read get reply from %s\n",
2044 inet_ntoa(c->server->sin.sin_addr));
2045 b = buffer_create();
2046 SNPRINTF(SIZE(b->buf, BUFFER_SIZE), "find %s\r\n", c->event->sound->name); /* old-style */
2047 b->nbytes = strlen(b->buf);
2048 e = event_create(EVENT_WRITE_FIND, b, c->event->sound);
2049 event_replace(c, e);
2050 connection_server_forward(c);
2051 connection_close(c);
2052 }
2053 break;
2054
2055 case EVENT_READ_SOUND:
2056 if (c->event->success)
2057 {
2058 close(c->event->fd);
2059 sound_map(c->event->sound);
2060 spool_ready(c->event->sound);
2061 c->event->sound->status = SOUND_READY;
2062 report(REPORT_DEBUG, "sound %s is ready\n", c->event->sound->name);
2063 event_delete(c, 1);
2064 }
2065 else
2066 {
2067 report(REPORT_NOTICE, "cannot read sound %s from %s\n", c->event->sound->name,
2068 inet_ntoa(c->sin.sin_addr));
2069 if (c->type == CONNECTION_SERVER)
2070 {
2071 b = buffer_create();
2072 SNPRINTF(SIZE(b->buf, BUFFER_SIZE), "find %s\r\n", c->event->sound->name); /* old-style */
2073 b->nbytes = strlen(b->buf);
2074 e = event_create(EVENT_WRITE_FIND, b, c->event->sound);
2075 event_replace(c, e);
2076 connection_server_forward(c);
2077 }
2078 connection_close(c);
2079 }
2080 break;
2081
2082 case EVENT_WRITE_SOUND:
2083 if (c->event->success)
2084 {
2085 event_delete(c, 1);
2086 }
2087 else
2088 {
2089 connection_close(c);
2090 }
2091 break;
2092
2093 case EVENT_WRITE:
2094 if (c->event->success)
2095 {
2096 b = c->event->buffer;
2097 c->event->buffer = c->event->buffer->next;
2098 if (b->status == BUFFER_FREE)
2099 {
2100 buffer_destroy(b);
2101 }
2102 if (c->event->buffer)
2103 {
2104 c->event->start = c->event->buffer->buf;
2105 c->event->ptr = c->event->start;
2106 c->event->nleft = c->event->buffer->nbytes;
2107 c->event->nbytes = c->event->nleft;
2108 c->event->success = 0;
2109 }
2110 else
2111 {
2112 event_delete(c, 1);
2113 }
2114 }
2115 else
2116 {
2117 connection_close(c);
2118 }
2119 break;
2120
2121 case EVENT_WRITE_TIMEOUT:
2122 report(REPORT_DEBUG, "%s client connection timeout\n", inet_ntoa(c->sin.sin_addr));
2123 connection_close(c);
2124 break;
2125
2126 case EVENT_READ_FLOW:
2127 case EVENT_WAIT_FLOW:
2128 case EVENT_PIPE_FLOW:
2129 if (c->event->success)
2130 {
2131 event_delete(c, 1);
2132 }
2133 else
2134 {
2135 connection_close(c);
2136 }
2137 break;
2138
2139 case EVENT_NOTIFY:
2140 report(REPORT_DEBUG, "event_update: `%d' update?\n", c->event->type);
2141 break;
2142
2143 case EVENT_WRITE_MONITOR:
2144 if (c->event->success)
2145 {
2146 /* move to the next monitor buffer */
2147 c->event->buffer = c->event->buffer->next;
2148 c->event->start = c->event->buffer->buf;
2149 c->event->ptr = c->event->start;
2150 c->event->nleft = c->event->buffer->nbytes;
2151 c->event->nbytes = c->event->nleft;
2152 c->event->success = 0;
2153
2154 /* need to wait for the buffer to be complete */
2155 if (c->event->buffer == monitor_buffers)
2156 {
2157 connection_monitor_pause(c);
2158 }
2159 }
2160 else
2161 {
2162 connection_close(c);
2163 }
2164 break;
2165
2166 case EVENT_WAIT_MONITOR:
2167 connection_close(c);
2168 break;
2169
2170 default:
2171 report(REPORT_ERROR, "event_update: unknown event `%d'\n", c->event->type);
2172 done(1);
2173 }
2174 }
2175
2176 #ifdef __STDC__
2177 void
2178 event_first(CONNECTION *c, EVENT *e)
2179 #else
2180 void
2181 event_first(c, e)
2182 CONNECTION *c;
2183 EVENT *e;
2184 #endif
2185 {
2186 EVENT *tail;
2187
2188 for (tail = e; tail->next; tail = tail->next) ;
2189
2190 tail->next = c->event;
2191 c->event = e;
2192
2193 if (!c->event->next)
2194 {
2195 c->ep = &c->event->next;
2196 }
2197
2198 connection_update_fdset(c);
2199
2200 #ifdef EVENT_DEBUG
2201 event_print(c, "first");
2202 #endif
2203 }
2204
2205 #ifdef __STDC__
2206 void
2207 event_insert_before(CONNECTION *c, EVENT *e, int mask)
2208 #else
2209 void
2210 event_insert_before(c, e, mask)
2211 CONNECTION *c;
2212 EVENT *e;
2213 int mask;
2214 #endif
2215 {
2216 if (!c->event || (c->event->type & mask))
2217 {
2218 event_first(c, e);
2219 }
2220 else
2221 {
2222 EVENT *tail, *event;
2223
2224 for (tail = e; tail->next; tail = tail->next) ;
2225
2226 for (event = c->event; event->next; event = event->next)
2227 {
2228 if (event->next->type & mask)
2229 {
2230 break;
2231 }
2232 }
2233
2234 tail->next = event->next;
2235 event->next = e;
2236
2237 if (!tail->next)
2238 {
2239 c->ep = &tail->next;
2240 }
2241
2242 if (c->event == e)
2243 {
2244 connection_update_fdset(c);
2245 }
2246
2247 #ifdef EVENT_DEBUG
2248 event_print(c, "before");
2249 #endif
2250 }
2251 }
2252
2253 #ifdef __STDC__
2254 void
2255 event_insert(CONNECTION *c, EVENT *e)
2256 #else
2257 void
2258 event_insert(c, e)
2259 CONNECTION *c;
2260 EVENT *e;
2261 #endif
2262 {
2263 EVENT *tail;
2264
2265 for (tail = e; tail->next; tail = tail->next) ;
2266
2267 *c->ep = e;
2268 c->ep = &tail->next;
2269
2270 if (c->event == e)
2271 {
2272 connection_update_fdset(c);
2273 }
2274
2275 #ifdef EVENT_DEBUG
2276 event_print(c, "insert");
2277 #endif
2278 }
2279
2280 #ifdef __STDC__
2281 void
2282 event_replace(CONNECTION *c, EVENT *e)
2283 #else
2284 void
2285 event_replace(c, e)
2286 CONNECTION *c;
2287 EVENT *e;
2288 #endif
2289 {
2290 if (c->event)
2291 {
2292 e->next = c->event->next;
2293 event_destroy(c->event);
2294 }
2295 c->event = e;
2296 if (c->event->next == NULL)
2297 {
2298 c->ep = &c->event->next;
2299 }
2300 connection_update_fdset(c);
2301
2302 #ifdef EVENT_DEBUG
2303 event_print(c, "replace");
2304 #endif
2305 }
2306
2307 #ifdef __STDC__
2308 void
2309 event_delete(CONNECTION *c, int replace)
2310 #else
2311 void
2312 event_delete(c, replace)
2313 CONNECTION *c;
2314 int replace;
2315 #endif
2316 {
2317 EVENT *e;
2318
2319 if (c->event == NULL)
2320 {
2321 connection_update_fdset(c);
2322 return;
2323 }
2324
2325 e = c->event;
2326 c->event = c->event->next;
2327
2328 event_destroy(e);
2329
2330 if (c->event == NULL)
2331 {
2332 c->ep = &c->event;
2333
2334 /* Replace the empty event with notify or read_command. */
2335 if (replace && c->type == CONNECTION_CLIENT)
2336 {
2337 if (c->notify_mask)
2338 {
2339 e = event_create(EVENT_NOTIFY);
2340 }
2341 else
2342 {
2343 e = event_create(EVENT_READ_COMMAND, buffer_create());
2344 }
2345 event_insert(c, e);
2346 }
2347 }
2348
2349 connection_update_fdset(c);
2350
2351 #ifdef EVENT_DEBUG
2352 event_print(c, "delete");
2353 #endif
2354 }
2355
2356 /*
2357 * EVENT_READ_COMMAND, BUFFER *b
2358 * EVENT_CONNECT
2359 * EVENT_READ_CONNECT_REPLY, BUFFER *b
2360 * EVENT_WRITE_FIND, BUFFER *b, SOUND *sound
2361 * EVENT_READ_FIND_REPLY, BUFFER *b, SOUND *sound
2362 * EVENT_WRITE_GET, BUFFER *b, SOUND *sound
2363 * EVENT_READ_GET_REPLY, BUFFER *b, SOUND *sound
2364 * EVENT_READ_SOUND, int fd, BUFFER *b, int nbytes, SOUND *sound
2365 * EVENT_WRITE, BUFFER *buffer
2366 * EVENT_WRITE_SOUND, SOUND *sound
2367 * EVENT_NOTIFY
2368 * EVENT_READ_FLOW, int spool_id, int nbytes
2369 */
2370 #ifdef __STDC__
2371 EVENT *
2372 event_create(int type,...)
2373 #else
2374 EVENT *
2375 event_create(va_alist)
2376 va_dcl
2377 #endif
2378 {
2379 EVENT *e;
2380 va_list args;
2381
2382 #ifdef __STDC__
2383 va_start(args, type);
2384 #else
2385 int type;
2386 va_start(args);
2387 type = va_arg(args, int);
2388 #endif
2389
2390 e = (EVENT *) malloc(sizeof(EVENT));
2391 if (e == NULL)
2392 {
2393 report(REPORT_ERROR, "event_create: out of memory\n");
2394 done(1);
2395 }
2396
2397 /*
2398 * defaults
2399 */
2400 e->next = NULL;
2401 e->type = type;
2402 e->nbytes = 0;
2403 e->byte_offset = 0;
2404 e->nleft = 0;
2405 e->start = NULL;
2406 e->ptr = NULL;
2407 e->sound = NULL;
2408 e->fd = -1;
2409 e->nconnects = 0;
2410 e->time = 0;
2411 e->success = 0;
2412 e->buffer = NULL;
2413 e->id = 0;
2414 e->wait_mask = NOTIFY_NONE;
2415 e->si = NULL;
2416
2417 switch (e->type)
2418 {
2419 case EVENT_READ_COMMAND:
2420 case EVENT_READ_CONNECT_REPLY:
2421 e->buffer = va_arg(args, BUFFER *);
2422 e->nleft = RPTP_MAX_LINE;
2423 e->nbytes = e->nleft;
2424 e->ptr = e->buffer->buf;
2425 e->start = e->ptr;
2426 break;
2427
2428 case EVENT_CONNECT:
2429 break;
2430
2431 case EVENT_WRITE_FIND:
2432 case EVENT_WRITE_GET:
2433 e->buffer = va_arg(args, BUFFER *);
2434 e->nleft = e->buffer->nbytes;
2435 e->nbytes = e->nleft;
2436 e->ptr = e->buffer->buf;
2437 e->start = e->ptr;
2438 e->sound = va_arg(args, SOUND *);
2439 break;
2440
2441 case EVENT_READ_FIND_REPLY:
2442 case EVENT_READ_GET_REPLY:
2443 e->buffer = va_arg(args, BUFFER *);
2444 e->nleft = RPTP_MAX_LINE;
2445 e->nbytes = e->nleft;
2446 e->ptr = e->buffer->buf;
2447 e->start = e->ptr;
2448 e->sound = va_arg(args, SOUND *);
2449 break;
2450
2451 case EVENT_READ_SOUND:
2452 e->fd = va_arg(args, int);
2453 e->buffer = va_arg(args, BUFFER *);
2454 e->nbytes = va_arg(args, int);
2455 e->nleft = e->nbytes;
2456 e->sound = va_arg(args, SOUND *);
2457 break;
2458
2459 case EVENT_WRITE_SOUND:
2460 e->sound = va_arg(args, SOUND *);
2461 e->buffer = buffer_create();
2462 e->si = sound_open(e->sound, 0);
2463 if (e->si == NULL)
2464 {
2465 event_destroy(e);
2466 return NULL;
2467 }
2468 if (sound_fill(e->si, e->buffer, 1) <= 0)
2469 {
2470 event_destroy(e);
2471 return NULL;
2472 }
2473 e->nbytes = e->sound->size; /* total number of bytes */
2474 e->nleft = e->buffer->nbytes; /* # bytes in the buffer */
2475 e->ptr = e->buffer->buf;
2476 e->start = e->buffer->buf;
2477 break;
2478
2479 case EVENT_WRITE:
2480 case EVENT_WRITE_TIMEOUT:
2481 e->buffer = va_arg(args, BUFFER *);
2482 e->ptr = e->buffer->buf;
2483 e->start = e->ptr;
2484 e->nleft = e->buffer->nbytes;
2485 e->nbytes = e->nleft;
2486 break;
2487
2488 case EVENT_NOTIFY:
2489 break;
2490
2491 case EVENT_READ_FLOW:
2492 e->id = va_arg(args, int);
2493 e->nbytes = va_arg(args, int);
2494 if (e->nbytes == 0)
2495 {
2496 e->nbytes = -1;
2497 }
2498 e->nleft = e->nbytes;
2499 break;
2500
2501 case EVENT_WAIT_FLOW:
2502 /* XXX */
2503 break;
2504
2505 case EVENT_PIPE_FLOW:
2506 e->id = va_arg(args, int);
2507 e->sound = va_arg(args, SOUND *);
2508 break;
2509
2510 case EVENT_WAIT_MONITOR:
2511 e->buffer = monitor_buffers;
2512 e->ptr = e->buffer->buf;
2513 e->start = e->ptr;
2514 e->nleft = e->buffer->nbytes;
2515 e->nbytes = e->nleft;
2516 break;
2517 }
2518
2519 va_end(args);
2520
2521 return e;
2522 }
2523
2524 #ifdef __STDC__
2525 void
2526 event_destroy(EVENT *e)
2527 #else
2528 void
2529 event_destroy(e)
2530 EVENT *e;
2531 #endif
2532 {
2533 if (e->buffer && e->buffer->status == BUFFER_FREE)
2534 {
2535 buffer_destroy(e->buffer);
2536 }
2537 if (e->si)
2538 {
2539 sound_close(e->si);
2540 }
2541 free((char *) e);
2542 }
2543
2544 #ifdef EVENT_DEBUG
2545 static void
2546 event_print(CONNECTION *c, char *message)
2547 {
2548 EVENT *e;
2549
2550 printf("%8s %s (%s) -> ", message, c->application, inet_ntoa(c->sin.sin_addr));
2551 e = c->event;
2552 for (; e; e = e->next)
2553 {
2554 switch (e->type)
2555 {
2556 case EVENT_READ_COMMAND:
2557 printf("read_command ");
2558 break;
2559
2560 case EVENT_CONNECT:
2561 printf("connect ");
2562 break;
2563
2564 case EVENT_READ_CONNECT_REPLY:
2565 printf("read_connect_reply ");
2566 break;
2567
2568 case EVENT_WRITE_FIND:
2569 printf("write_find ");
2570 break;
2571
2572 case EVENT_READ_FIND_REPLY:
2573 printf("read_find_reply ");
2574 break;
2575
2576 case EVENT_WRITE_GET:
2577 printf("write_get ");
2578 break;
2579
2580 case EVENT_READ_GET_REPLY:
2581 printf("read_get_reply ");
2582 break;
2583
2584 case EVENT_READ_SOUND:
2585 printf("read_sound ");
2586 break;
2587
2588 case EVENT_WRITE:
2589 printf("write ");
2590 break;
2591
2592 case EVENT_WRITE_SOUND:
2593 printf("write_sound ");
2594 break;
2595
2596 case EVENT_WRITE_TIMEOUT:
2597 printf("write_timeout ");
2598 break;
2599
2600 case EVENT_NOTIFY:
2601 printf("notify ");
2602 break;
2603
2604 case EVENT_READ_FLOW:
2605 printf("read_flow ");
2606 break;
2607
2608 case EVENT_WAIT_FLOW:
2609 printf("wait_flow ");
2610 break;
2611
2612 case EVENT_PIPE_FLOW:
2613 printf("pipe_flow ");
2614 break;
2615
2616 default:
2617 printf("`%d' ", e->type);
2618 break;
2619 }
2620 }
2621
2622 if (FD_ISSET(c->fd, &write_mask))
2623 {
2624 printf("[write] ");
2625 }
2626 if (FD_ISSET(c->fd, &read_mask))
2627 {
2628 printf("[read] ");
2629 }
2630
2631 printf("\n");
2632 }
2633 #endif