3 * Copyright (c) 2006 acmesystems.it - john@acmesystems.it
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
19 * Feedback, Bugs... info@acmesystems.it
23 #include <sys/socket.h>
28 #include <sys/socket.h>
35 #define SOCKET_PATH "/tmp/foxmp3"
37 typedef struct _MP3_NIX_SOCKET {
44 static MP3_NIX_SOCKET mp3_nix_socket;
46 int mp3_nix_socket_setup(void){
47 struct sockaddr_un myaddr;
50 FD_ZERO(&mp3_nix_socket.master);
51 FD_ZERO(&mp3_nix_socket.clients);
53 if ((mp3_nix_socket.listener = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
57 if (setsockopt(mp3_nix_socket.listener, SOL_SOCKET,
58 SO_REUSEADDR, &yes, sizeof(int)) == -1) {
62 myaddr.sun_family = AF_UNIX;
63 strcpy(myaddr.sun_path, SOCKET_PATH);
64 unlink(myaddr.sun_path);
65 len = strlen(myaddr.sun_path) + sizeof(myaddr.sun_family);
66 if (bind(mp3_nix_socket.listener, (struct sockaddr *)&myaddr, len) == -1) {
70 if (listen(mp3_nix_socket.listener, 3) == -1) {
74 FD_SET(mp3_nix_socket.listener, &mp3_nix_socket.master);
75 mp3_nix_socket.max = mp3_nix_socket.listener;
80 int mp3_nix_socket_handle(void){
81 struct sockaddr_un remoteaddr;
92 mp3_nix_socket.clients = mp3_nix_socket.master;
94 if (select(mp3_nix_socket.max + 1, &mp3_nix_socket.clients,
95 NULL, NULL, &tv) == -1) {
96 // sometimes the select is interrupted, because of the alarm signal used by the playtime counter
97 //perror("error whilst selecting socket");
100 for(i = 0; i <= mp3_nix_socket.max; i++) {
101 if (FD_ISSET(i, &mp3_nix_socket.clients)) {
102 if (i == mp3_nix_socket.listener) {
103 addrlen = sizeof(remoteaddr);
104 if ((newfd = accept(mp3_nix_socket.listener,
105 (struct sockaddr *)&remoteaddr,
107 perror("error whilst accepting socket");
110 FD_SET(newfd, &mp3_nix_socket.master);
111 if (newfd > mp3_nix_socket.max) {
112 mp3_nix_socket.max = newfd;
114 fcntl(newfd, F_SETFL, O_NONBLOCK);
115 printf("New socket client on %d\n", newfd);
118 if ((nbytes = recv(i, buf, sizeof(buf), 0)) <= 0) {
120 printf("selectserver: socket hung up %d\n", i);
122 FD_CLR(i, &mp3_nix_socket.master);
124 printf("error whilst receiving socket %d\n", i);
128 printf("Got data : %s\n", buf);
129 mp3_parser_incoming(buf,buf_out);
130 if(*buf_out != '\0'){
131 send(i, buf_out, strlen(buf_out), 0);
140 void mp3_nix_socket_write(unsigned char *data, ...){
142 unsigned char t[2048];
145 // clear possible dead sockets
146 mp3_nix_socket_handle();
149 vsprintf(t, data, ap);
151 printf("Sending data --> %s\n", t);
152 for(i = 0; i <= mp3_nix_socket.max; i++) {
153 if (FD_ISSET(i, &mp3_nix_socket.master)) {
154 if (i != mp3_nix_socket.listener) {
155 printf("Sending on socket %d\n", i);
156 send(i, t, strlen(t), 0);
160 printf("Finished sending\n");
163 int mp3_nix_socket_cleanup(void){