Debian Codegraph @

rplayd/connection.c

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