Changeset 6027:3c0026227c67 in roaraudio
- Timestamp:
- 07/14/14 11:46:06 (10 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libroar/vio_stream.c
r6026 r6027 155 155 } 156 156 157 static int _roar_simple_new_stream_obj (struct roar_connection * con, struct roar_stream * s, uint32_t rate, uint32_t channels, uint32_t bits, uint32_t codec, int dir, int mixer) { 158 struct roar_libroar_config * config = roar_libroar_get_config(); 159 char file[80] = ""; 160 int fh = -1, listen = -1; 161 static int count = 0; 162 int port = 0; 163 #if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_LIBDNET) 164 int opt = 1; 165 #endif 166 #ifdef ROAR_HAVE_LIBDNET 167 struct dn_naddr *binaddr; 168 #endif 169 #ifdef ROAR_HAVE_IPV4 170 struct sockaddr_in socket_addr; 171 socklen_t len = sizeof(struct sockaddr_in); 172 #else 173 struct sockaddr socket_addr; 174 socklen_t len = sizeof(struct sockaddr); 175 #endif 157 static inline int _roar_simple_new_stream_obj_try_select (struct roar_connection * con, struct roar_stream * s, uint32_t rate, uint32_t channels, uint32_t bits, uint32_t codec, int dir, int mixer, struct roar_sockname * sockname, int * fh) { 176 158 #ifdef ROAR_HAVE_SELECT 177 159 int confh; … … 179 161 struct timeval timeout = {10, 0}; 180 162 struct roar_message mes; 181 #endif 182 #ifdef ROAR_HAVE_UNIX 183 int socks[2]; // for socketpair() 184 #endif 185 struct roar_sockname sockname; 163 int listen; 164 int port = 0; 165 char file[80] = ""; 166 static int count = 0; 186 167 char socketaddr[80]; 187 168 ssize_t socketaddr_len = -1; 188 189 // make valgrind happy 169 #if defined(ROAR_HAVE_IPV4) || defined(ROAR_HAVE_LIBDNET) 170 int opt = 1; 171 #endif 172 #ifdef ROAR_HAVE_LIBDNET 173 struct dn_naddr *binaddr; 174 #endif 175 #ifdef ROAR_HAVE_IPV4 176 struct sockaddr_in socket_addr; 177 socklen_t len = sizeof(struct sockaddr_in); 178 #else 179 struct sockaddr socket_addr; 180 socklen_t len = sizeof(struct sockaddr); 181 #endif 182 183 memset(&mes, 0, sizeof(mes)); 190 184 memset(&socket_addr, 0, sizeof(socket_addr)); 191 #ifdef ROAR_HAVE_SELECT 192 memset(&mes, 0, sizeof(mes)); 193 #endif 194 195 ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = ?", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer); 196 197 if ( config != NULL ) { 198 if ( config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_USE_EXECED ) { 199 return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer); 200 } 201 } 202 203 ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = ?", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer); 204 205 roar_libroar_nowarn(); 206 if ( roar_vio_ctl(roar_get_connection_vio2(con), ROAR_VIO_CTL_GET_SOCKNAME, &sockname) == -1 ) { 207 roar_libroar_warn(); 208 #ifdef ROAR_OS_OPENBSD 209 sockname.type = ROAR_SOCKET_TYPE_UNIX; 210 #else 211 ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = -1", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer); 212 213 return -1; 214 #endif 215 } 216 roar_libroar_warn(); 217 218 ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = ?", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer); 219 220 if ( sockname.type == ROAR_SOCKET_TYPE_DECNET ) { 185 186 if ( sockname->type == ROAR_SOCKET_TYPE_DECNET ) { 221 187 #ifdef ROAR_HAVE_LIBDNET 222 188 if ( roar_socket_get_local_nodename() != NULL && (binaddr = getnodeadd()) != NULL ) { … … 235 201 #ifdef ROAR_HAVE_IPV4 236 202 } else { 237 strncpy(file, sockname .addr, sizeof(file) - 1);203 strncpy(file, sockname->addr, sizeof(file) - 1); 238 204 roar_err_set(ROAR_ERROR_NONE); 239 if ( inet_aton(sockname .addr, &socket_addr.sin_addr) == 0 ) {205 if ( inet_aton(sockname->addr, &socket_addr.sin_addr) == 0 ) { 240 206 roar_err_update(); 241 207 return -1; … … 246 212 } 247 213 248 if ( sockname.type != ROAR_SOCKET_TYPE_UNIX ) { 249 roar_libroar_nowarn(); 250 if ( (listen = roar_socket_listen(sockname.type, file, port)) == -1 ) { 251 roar_libroar_warn(); 252 return -1; 253 } 214 roar_libroar_nowarn(); 215 if ( (listen = roar_socket_listen(sockname->type, file, port)) == -1 ) { 254 216 roar_libroar_warn(); 255 } 256 257 if ( sockname.type == ROAR_SOCKET_TYPE_INET ) { 217 return -1; 218 } 219 roar_libroar_warn(); 220 221 if ( sockname->type == ROAR_SOCKET_TYPE_INET ) { 258 222 #ifdef ROAR_HAVE_IPV4 259 223 setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, (void*)&opt, sizeof(int)); … … 269 233 return -1; 270 234 #endif 271 } else if ( sockname .type == ROAR_SOCKET_TYPE_DECNET ) {235 } else if ( sockname->type == ROAR_SOCKET_TYPE_DECNET ) { 272 236 #ifdef ROAR_HAVE_LIBDNET 273 237 setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)); … … 277 241 } 278 242 279 if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) { 280 return -1; 281 } 282 283 if ( roar_stream_connect(con, s, dir, mixer) == -1 ) { 284 return -1; 285 } 286 287 if ( sockname.type != ROAR_SOCKET_TYPE_UNIX ) { 288 #ifdef ROAR_HAVE_SELECT 289 if ( roar_stream_connect_to_advanced(con, s, 0, 1, 0, sockname.type, socketaddr_len, socketaddr, ROAR_PROTO_NONE, -1, NULL, 0, NULL) != -1 ) { 290 291 FD_ZERO(&fds); 292 FD_SET(listen, &fds); 293 294 confh = roar_get_connection_fh(con); 295 296 if ( confh != -1 ) { 297 FD_SET(confh, &fds); 243 if ( roar_stream_connect_to_advanced(con, s, 0, 1, 0, sockname->type, socketaddr_len, socketaddr, ROAR_PROTO_NONE, -1, NULL, 0, NULL) != -1 ) { 244 245 FD_ZERO(&fds); 246 FD_SET(listen, &fds); 247 248 confh = roar_get_connection_fh(con); 249 250 if ( confh != -1 ) { 251 FD_SET(confh, &fds); 252 } 253 254 if ( select((confh > listen ? confh : listen) + 1, &fds, NULL, NULL, &timeout) < 1 ) { 255 close(listen); 256 257 // we don't need to check the content as we know it failed... 258 if ( roar_recv_message(con, &mes, NULL) == -1 ) 259 return -1; 260 261 if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 ) 262 return -1; 263 264 return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer); 265 } 266 267 if ( FD_ISSET(listen, &fds) ) { 268 if ( (*fh = accept(listen, NULL, NULL)) != -1 ) { 269 /* TODO: FIXME: XXX: errr, do we need any error handling here? */ 298 270 } 299 271 300 if ( select((confh > listen ? confh : listen) + 1, &fds, NULL, NULL, &timeout) < 1 ) { 272 if ( roar_recv_message(con, &mes, NULL) == -1 ) { 273 if ( *fh != -1 ) 274 close(*fh); 275 *fh = -1; 276 } else if ( mes.cmd != ROAR_CMD_OK ) { 277 if ( *fh != -1 ) 278 close(*fh); 279 *fh = -1; 280 } 281 } else { 282 // we don't need to check the content as we know it failed... 283 if ( roar_recv_message(con, &mes, NULL) == -1 ) { 301 284 close(listen); 302 303 // we don't need to check the content as we know it failed...304 if ( roar_recv_message(con, &mes, NULL) == -1 ) 305 return -1;306 285 return -1; 286 } 287 288 if ( mes.cmd != ROAR_CMD_OK ) { 289 close(listen); 307 290 if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 ) 308 291 return -1; 309 292 310 293 return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer); 311 } 312 313 if ( FD_ISSET(listen, &fds) ) { 314 if ( (fh = accept(listen, NULL, NULL)) != -1 ) { 315 /* TODO: FIXME: XXX: errr, do we need any error handling here? */ 316 } 317 318 if ( roar_recv_message(con, &mes, NULL) == -1 ) { 319 if ( fh != -1 ) 320 close(fh); 321 fh = -1; 322 } else if ( mes.cmd != ROAR_CMD_OK ) { 323 if ( fh != -1 ) 324 close(fh); 325 fh = -1; 326 } 327 } else { 328 // we don't need to check the content as we know it failed... 329 if ( roar_recv_message(con, &mes, NULL) == -1 ) { 330 close(listen); 331 return -1; 332 } 333 334 if ( mes.cmd != ROAR_CMD_OK ) { 335 close(listen); 336 if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 ) 337 return -1; 338 339 return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer); 340 } else { // seems like we have a positive reply. So we retry the listen socket: 341 FD_ZERO(&fds); 342 FD_SET(listen, &fds); 343 timeout.tv_sec = 0; 344 timeout.tv_usec = 128000L; 345 fh = -1; 346 if ( select(listen + 1, &fds, NULL, NULL, &timeout) > 0 ) { 347 if ( (fh = accept(listen, NULL, NULL)) == -1 ) { 348 close(listen); 349 if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 ) 350 return -1; 351 352 return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer); 353 } 294 } else { // seems like we have a positive reply. So we retry the listen socket: 295 FD_ZERO(&fds); 296 FD_SET(listen, &fds); 297 timeout.tv_sec = 0; 298 timeout.tv_usec = 128000L; 299 *fh = -1; 300 if ( select(listen + 1, &fds, NULL, NULL, &timeout) > 0 ) { 301 if ( (*fh = accept(listen, NULL, NULL)) == -1 ) { 302 close(listen); 303 if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 ) 304 return -1; 305 306 return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer); 354 307 } 355 308 } 356 309 } 357 310 } 358 359 close(listen); 311 } 312 313 close(listen); 314 315 return 0; 360 316 #else 361 317 roar_err_set(ROAR_ERROR_NOSYS); 362 318 return -1; 363 319 #endif 320 } 321 322 static int _roar_simple_new_stream_obj (struct roar_connection * con, struct roar_stream * s, uint32_t rate, uint32_t channels, uint32_t bits, uint32_t codec, int dir, int mixer) { 323 struct roar_libroar_config * config = roar_libroar_get_config(); 324 int fh = -1; 325 #ifdef ROAR_HAVE_UNIX 326 int socks[2]; // for socketpair() 327 #endif 328 struct roar_sockname sockname; 329 330 ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = ?", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer); 331 332 if ( config != NULL ) { 333 if ( config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_USE_EXECED ) { 334 return roar_simple_new_stream_attachexeced_obj(con, s, rate, channels, bits, codec, dir, mixer); 335 } 336 } 337 338 ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = ?", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer); 339 340 roar_libroar_nowarn(); 341 if ( roar_vio_ctl(roar_get_connection_vio2(con), ROAR_VIO_CTL_GET_SOCKNAME, &sockname) == -1 ) { 342 roar_libroar_warn(); 343 #ifdef ROAR_OS_OPENBSD 344 sockname.type = ROAR_SOCKET_TYPE_UNIX; 345 #else 346 ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = -1", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer); 347 348 return -1; 349 #endif 350 } 351 roar_libroar_warn(); 352 353 ROAR_DBG("_roar_simple_new_stream_obj(con=%p, s=%p, rate=%i, channels=%i, bits=%i, codec=%i, dir=%i, mixer=%i) = ?", con, s, (int)rate, (int)channels, (int)bits, (int)codec, dir, mixer); 354 355 if ( roar_stream_new(s, rate, channels, bits, codec) == -1 ) { 356 return -1; 357 } 358 359 if ( roar_stream_connect(con, s, dir, mixer) == -1 ) { 360 return -1; 361 } 362 363 if ( sockname.type != ROAR_SOCKET_TYPE_UNIX ) { 364 if ( _roar_simple_new_stream_obj_try_select(con, s, rate, channels, bits, codec, dir, mixer, &sockname, &fh) == -1 ) 365 return -1; 364 366 } else { // this is sockname.type == ROAR_SOCKET_TYPE_UNIX 365 367 #ifdef ROAR_HAVE_UNIX
Note: See TracChangeset
for help on using the changeset viewer.