source: roaraudio/roard/network.c @ 3981:0dda28251f72

Last change on this file since 3981:0dda28251f72 was 3981:0dda28251f72, checked in by phi, 14 years ago

activate rplay

File size: 4.6 KB
Line 
1//network.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010
5 *
6 *  This file is part of roard a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  RoarAudio is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "roard.h"
27
28#ifdef ROAR_SUPPORT_LISTEN
29
30#ifdef ROAR_BROKEN_PEERCRED
31#undef SO_PEERCRED
32#endif
33
34#ifdef ROAR_HAVE_SELECT
35#define _CAN_OPERATE
36#endif
37
38int net_check_listen  (void) {
39 struct roar_vio_selecttv rtv;
40 struct roar_vio_select   sv[ROAR_MAX_LISTEN_SOCKETS];
41 size_t num = 0;
42 ssize_t ret;
43 int i;
44
45 for (i = 0; i < ROAR_MAX_LISTEN_SOCKETS; i++) {
46  if ( g_listen[i].used ) {
47   ROAR_VIO_SELECT_SETVIO(&(sv[num]), &(g_listen[i].sock), ROAR_VIO_SELECT_READ);
48   sv[num].ud.si = i;
49   num++;
50  }
51 }
52
53 if ( num == 0 )
54  return 0;
55
56 rtv.sec  = 0;
57 rtv.nsec = 1000;
58
59 if ( (ret = roar_vio_select(sv, num, &rtv, NULL)) > 0 ) {
60  for (i = 0; i < num; i++) {
61   if ( sv[i].eventsa & ROAR_VIO_SELECT_READ ) {
62    if ( net_get_new_client(&(g_listen[sv[i].ud.si])) == -1 )
63     return -1;
64   }
65  }
66 }
67
68 return ret;
69}
70
71int net_get_new_client (struct roard_listen * lsock) {
72 int fh;
73 int client;
74 int socket;
75 struct roar_client * c;
76 struct roar_vio_calls    vio;
77 struct sockaddr_storage  addr;
78 socklen_t                addrlen = sizeof(addr);
79
80 if ( roar_vio_ctl(&(lsock->sock), ROAR_VIO_CTL_GET_FH, &socket) == -1 ) {
81  // next is needed for winsock:
82  if ( roar_vio_ctl(&(lsock->sock), ROAR_VIO_CTL_GET_SELECT_FH, &socket) == -1 ) {
83   ROAR_DBG("net_get_new_client(void) = -1 // can not find any acceptable socket to accept() on");
84   return -1;
85  }
86 }
87
88 fh = accept(socket, (struct sockaddr*)&addr, &addrlen);
89
90 ROAR_DBG("net_get_new_client(void): fh = %i", fh);
91
92#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
93 if ( lsock->proto == ROAR_PROTO_RSOUND ) {
94  client = emul_rsound_on_connect(fh, lsock);
95  switch (client) {
96   case -1: return -1; break;
97   case -2: return  0; break;
98   default: // TODO: write error handling
99     clients_get(client, &c);
100     fh = c->fh;
101    break;
102  }
103 } else {
104#endif
105
106 client = clients_new();
107
108 if ( client == -1 ) {
109  ROAR_DBG("net_get_new_client(void) = -1 // can not create new client");
110  close(fh);
111  return -1;
112 }
113
114 if ( clients_set_fh(client, fh) == -1 ) {
115  ROAR_ERR("net_get_new_client(void): Can not set client's fh");
116
117  clients_delete(client);
118  close(fh);
119
120  ROAR_DBG("net_get_new_client(void) = -1");
121  return -1;
122 }
123#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
124 }
125#endif
126
127 if ( clients_get(client, &c) != -1 ) {
128  if ( roar_nnode_free(&(c->nnode)) == -1 )
129   return -1;
130
131  if ( roar_nnode_new_from_sockaddr(&(c->nnode), (struct sockaddr*)&addr, addrlen) == -1 )
132   return -1;
133 }
134
135 ROAR_DBG("net_get_new_client(*): proto=0x%.4x", lsock->proto);
136
137 if ( clients_set_proto(client, lsock->proto) == -1 ) {
138  ROAR_WARN("net_get_new_client(*): Setting proto(0x%.4x) of client %i failed.", lsock->proto, client);
139  return -1;
140 }
141
142 switch (lsock->proto) {
143  case ROAR_PROTO_ROARAUDIO:
144    // nothing needed to be done here
145   break;
146#ifndef ROAR_WITHOUT_DCOMP_EMUL_ESD
147#ifdef ROAR_HAVE_H_ESD
148  case ROAR_PROTO_ESOUND:
149    ROAR_DBG("net_get_new_client(*): execing ESD CONNECT command");
150
151    if ( roar_vio_open_fh_socket(&vio, fh) == -1 )
152     return -1;
153
154    ROAR_DBG("net_get_new_client(*): creating VIO OK");
155
156    if ( emul_esd_exec_command(client, ESD_PROTO_CONNECT, &vio) == -1 )
157     return -1;
158
159    ROAR_DBG("net_get_new_client(*): CONNECT execed sucessfully");
160   break;
161#endif
162#endif
163#ifndef ROAR_WITHOUT_DCOMP_EMUL_SIMPLE
164  case ROAR_PROTO_SIMPLE:
165    if ( emul_simple_on_connect(client, lsock) == -1 )
166     return -1;
167   break;
168#endif
169#ifndef ROAR_WITHOUT_DCOMP_EMUL_RSOUND
170  case ROAR_PROTO_RSOUND: // nothing to do here.
171   break;
172#endif
173#ifndef ROAR_WITHOUT_DCOMP_EMUL_RPLAY
174  case ROAR_PROTO_RPLAY: // nothing to do here.
175   break;
176#endif
177  default:
178    // OS independiend code to close the socket:
179    if ( roar_vio_open_fh_socket(&vio, fh) == -1 )
180     return -1;
181    roar_vio_close(&vio);
182    return -1;
183   break;
184 }
185
186// close(fh);
187
188 return 0;
189}
190
191#endif
192
193//ll
Note: See TracBrowser for help on using the repository browser.