Changeset 6027:3c0026227c67 in roaraudio


Ignore:
Timestamp:
07/14/14 11:46:06 (10 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

some cleanup as well as another set of prepraing for v1 CON_STREAM

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libroar/vio_stream.c

    r6026 r6027  
    155155} 
    156156 
    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 
     157static 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) { 
    176158#ifdef ROAR_HAVE_SELECT 
    177159 int confh; 
     
    179161 struct timeval timeout = {10, 0}; 
    180162 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; 
    186167 char socketaddr[80]; 
    187168 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)); 
    190184 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 ) { 
    221187#ifdef ROAR_HAVE_LIBDNET 
    222188  if ( roar_socket_get_local_nodename() != NULL && (binaddr = getnodeadd()) != NULL ) { 
     
    235201#ifdef ROAR_HAVE_IPV4 
    236202 } else { 
    237   strncpy(file, sockname.addr, sizeof(file) - 1); 
     203  strncpy(file, sockname->addr, sizeof(file) - 1); 
    238204  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 ) { 
    240206   roar_err_update(); 
    241207   return -1; 
     
    246212 } 
    247213 
    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 ) { 
    254216  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 ) { 
    258222#ifdef ROAR_HAVE_IPV4 
    259223  setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, (void*)&opt, sizeof(int)); 
     
    269233  return -1; 
    270234#endif 
    271  } else if ( sockname.type == ROAR_SOCKET_TYPE_DECNET ) { 
     235 } else if ( sockname->type == ROAR_SOCKET_TYPE_DECNET ) { 
    272236#ifdef ROAR_HAVE_LIBDNET 
    273237  setsockopt(listen, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)); 
     
    277241 } 
    278242 
    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? */ 
    298270   } 
    299271 
    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 ) { 
    301284    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); 
    307290    if ( roar_kick(con, ROAR_OT_STREAM, s->id) == -1 ) 
    308291     return -1; 
    309292 
    310293    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); 
    354307     } 
    355308    } 
    356309   } 
    357310  } 
    358  
    359   close(listen); 
     311 } 
     312 
     313 close(listen); 
     314 
     315 return 0; 
    360316#else 
    361317  roar_err_set(ROAR_ERROR_NOSYS); 
    362318  return -1; 
    363319#endif 
     320} 
     321 
     322static 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; 
    364366 } else { // this is sockname.type == ROAR_SOCKET_TYPE_UNIX 
    365367#ifdef ROAR_HAVE_UNIX 
Note: See TracChangeset for help on using the changeset viewer.