Changeset 5368:1c35d975f8fe in roaraudio for libroar


Ignore:
Timestamp:
12/20/11 22:17:27 (12 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

converted roar_connect_raw() to a static function, mad it a more universal API, including using VIO more directly

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libroar/basic.c

    r5365 r5368  
    3636#include "libroar.h" 
    3737 
    38 int roar_connect_raw (const char * server, int flags, uint_least32_t timeout) { 
     38static int _connect_server(struct roar_connection * con, const char * server, int type, int flags, uint_least32_t timeout) { 
     39#if defined(ROAR_HAVE_STAT) && defined(ROAR_HAVE_H_SYS_STAT) 
     40 struct stat sockstat; 
     41#endif 
     42 const char * obj = NULL; 
     43 char user_sock[128]; 
     44 int is_decnet = 0; 
     45 int port = 0; 
     46 int i = 0; 
     47 int fh = -1; 
     48 int err; 
     49 
     50 if ( con == NULL || server == NULL ) { 
     51  roar_err_set(ROAR_ERROR_FAULT); 
     52  return -1; 
     53 } 
     54 
     55 if ( !strcmp(server, "+invalid") ) { 
     56  roar_err_set(ROAR_ERROR_CANCELED); 
     57  return -1; 
     58 } else if ( !strncmp(server, "+dstr=", 6) ) { 
     59  if ( roar_vio_open_dstr_simple(con->viocon, server+6, ROAR_VIOF_READWRITE) == -1 ) 
     60   return -1; 
     61  con->flags |= ROAR_CON_FLAGS_VIO; 
     62  return 0; 
     63 } 
     64 
     65 strncpy(user_sock, server, sizeof(user_sock)-1); 
     66 user_sock[sizeof(user_sock)-1] = 0; 
     67 
     68 
     69 if ( *user_sock != '/' ) { // don't test AF_UNIX sockets for ports 
     70  for (i = 0; user_sock[i] != 0; i++) { 
     71   if ( user_sock[i] == ':' ) { 
     72    if ( user_sock[i+1] == ':' ) { // DECnet, leave unchanged 
     73     is_decnet = 1; 
     74     obj = &user_sock[i+2]; 
     75     break; 
     76    } 
     77 
     78    port = atoi(&(user_sock[i+1])); 
     79    user_sock[i] = 0; 
     80    break; 
     81   } 
     82  } 
     83 } 
     84 
     85 if ( is_decnet ) { 
     86  if ( *user_sock == ':' ) { 
     87   if ( roar_socket_get_local_nodename() != NULL ) { 
     88    strncpy(user_sock, roar_socket_get_local_nodename(), sizeof(user_sock)-1); 
     89    user_sock[sizeof(user_sock)-1] = 0; 
     90    roar_mm_strlcat(user_sock, server, sizeof(user_sock)-1); 
     91    user_sock[sizeof(user_sock)-1] = 0; 
     92    obj  = strstr(user_sock, "::"); 
     93    obj += 2; 
     94   } 
     95  } 
     96 
     97  if ( *obj == 0 ) { 
     98#ifdef DN_MAXOBJL 
     99   roar_mm_strlcat(user_sock, ROAR_DEFAULT_OBJECT, sizeof(user_sock)-1); 
     100   user_sock[sizeof(user_sock)-1] = 0; 
     101#else 
     102   ROAR_ERR("roar_connect_raw(*): size of DECnet object unknown."); 
     103#endif 
     104  } 
     105   ROAR_DBG("roar_connect_raw(*): user_sock='%s'", user_sock); 
     106 } 
     107 
     108 if ( port || is_decnet ) { 
     109  fh = roar_socket_connect(user_sock, port); 
     110  // restore the original string 
     111  user_sock[i] = ':'; 
     112 } else { 
     113#if defined(ROAR_HAVE_STAT) && defined(ROAR_HAVE_H_SYS_STAT) 
     114  if ( user_sock[0] == '/' ) { 
     115   if ( stat(user_sock, &sockstat) == 0 ) { 
     116    if ( S_ISCHR(sockstat.st_mode) ) { 
     117     return open(user_sock, O_RDWR|O_NOCTTY, 0666); 
     118    } 
     119   } 
     120  } 
     121#endif 
     122  fh = roar_socket_connect(user_sock, ROAR_DEFAULT_PORT); 
     123 } 
     124 
     125 if ( fh == -1 ) 
     126  return -1; 
     127 
     128 if ( roar_vio_open_fh_socket(con->viocon, fh) == -1 ) { 
     129  err = roar_error; 
     130#ifdef ROAR_TARGET_WIN32 
     131  closesocket(fh); 
     132#else 
     133  close(fh); 
     134#endif 
     135  roar_error = err; 
     136  return -1; 
     137 } else { 
     138  con->flags |= ROAR_CON_FLAGS_VIO; 
     139 } 
     140 
     141 roar_err_set(ROAR_ERROR_NONE); 
     142 return 0; 
     143} 
     144 
     145static int roar_connect_raw (struct roar_connection * con, const char * server, int flags, uint_least32_t timeout) { 
    39146#ifdef ROAR_HAVE_LIBSLP 
    40147 struct roar_libroar_config * config = roar_libroar_get_config(); 
     
    43150 char * roar_server; 
    44151 int i = 0; 
    45  int port = 0; 
    46  int fh = -1; 
    47  int is_decnet = 0; 
    48  const char * obj = NULL; 
    49152#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) 
    50153 struct passwd * pwd; 
     
    60163 int workarounds_store; 
    61164#endif 
    62 #if defined(ROAR_HAVE_STAT) && defined(ROAR_HAVE_H_SYS_STAT) 
    63  struct stat sockstat; 
    64 #endif 
    65165 
    66166 roar_err_set(ROAR_ERROR_UNKNOWN); 
     
    75175 
    76176 
    77  if ( server == NULL ) 
     177 if ( server == NULL || *server == 0 ) 
    78178  server = roar_libroar_get_server(); 
    79179 
    80  if ( server == NULL && (roar_server = getenv("ROAR_SERVER")) != NULL ) 
    81   server = roar_server; 
     180 if ( server == NULL || *server == 0 ) 
     181  server = getenv("ROAR_SERVER"); 
    82182 
    83183#ifdef ROAR_HAVE_LIBX11 
    84  if ( server == NULL ) { 
     184 if ( server == NULL || *server == 0 ) { 
    85185  if ( (x11con = roar_x11_connect(NULL)) != NULL ) { 
    86186   server = roar_x11_get_prop(x11con, "ROAR_SERVER"); 
     
    91191 
    92192#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER) 
    93  if ( server == NULL && (i = readlink("/etc/roarserver", user_sock, sizeof(user_sock)-1)) != -1 ) { 
     193 if ( (server == NULL || *server == 0) && (i = readlink("/etc/roarserver", user_sock, sizeof(user_sock)-1)) != -1 ) { 
    94194   user_sock[i] = 0; 
    95195   server = user_sock; 
     
    129229  user_sock[sizeof(user_sock)-1] = 0; 
    130230 
    131   if ( (fh = roar_socket_connect(user_sock, 0)) != -1 ) 
    132    return fh; 
    133  
    134   if ( (fh = roar_socket_connect(ROAR_DEFAULT_SOCK_GLOBAL, 0)) != -1 ) 
    135    return fh; 
    136 #endif 
    137  
    138   if ( (fh = roar_socket_connect(ROAR_DEFAULT_HOST, ROAR_DEFAULT_PORT)) != -1 ) 
    139    return fh; 
     231  if ( _connect_server(con, user_sock, ROAR_SOCKET_TYPE_UNIX, flags, timeout) == 0 ) 
     232   return 0; 
     233 
     234  if ( _connect_server(con, ROAR_DEFAULT_SOCK_GLOBAL, ROAR_SOCKET_TYPE_UNIX, flags, timeout) == 0 ) 
     235   return 0; 
     236#endif 
     237 
     238  if ( _connect_server(con, ROAR_DEFAULT_HOSTPORT, ROAR_SOCKET_TYPE_TCP, flags, timeout) == 0 ) 
     239   return 0; 
    140240 
    141241#ifdef ROAR_HAVE_LIBDNET 
     
    143243   if ( roar_socket_get_local_nodename() ) { 
    144244    snprintf(user_sock, 79, "%s::%s", roar_socket_get_local_nodename(), ROAR_DEFAULT_OBJECT); 
    145     if ( (fh = roar_socket_connect(user_sock, ROAR_DEFAULT_NUM)) != -1 ) 
    146      return fh; 
     245    if ( _connect_server(con, user_sock, ROAR_SOCKET_TYPE_DECNET, flags, timeout) == 0 ) 
     246     return 0; 
    147247   } 
    148248  } 
    149249#endif 
    150250 
    151   if ( (fh = roar_socket_connect("+abstract", 0)) != -1 ) 
    152    return fh; 
     251  if ( _connect_server(con, "+abstract", -1, flags, timeout) == 0 ) 
     252   return 0; 
    153253 
    154254#ifdef ROAR_HAVE_LIBSLP 
     
    157257    ) { 
    158258  if ( (server = roar_slp_find_roard(0)) != NULL ) { 
    159    if ( (fh = roar_connect_raw(server, 0, 0)) != -1 ) 
    160     return fh; 
     259   if ( _connect_server(con, server, -1, 0, 0) == 0 ) 
     260    return 0; 
    161261 
    162262   /* in case we can not connect to the server given this may be a cache problem, 
     
    164264   ROAR_WARN("roar_connect_raw(*): Can not connect to SLP located server, disabling cache"); 
    165265   if ( (server = roar_slp_find_roard(1)) != NULL ) 
    166     if ( (fh = roar_connect_raw(server, 0, 0)) != -1 ) 
    167      return fh; 
     266    if ( _connect_server(con, server, -1, 0, 0) == 0 ) 
     267     return 0; 
    168268  } 
    169269 } 
     
    175275 if ( list != NULL ) { 
    176276  for (i = 0; list[i].server != NULL; i++) { 
    177    if ( (fh = roar_connect_raw(list[i].server, 0, 0)) != -1 ) { 
     277   if ( _connect_server(con, list[i].server, -1, 0, 0) == 0 ) { 
    178278    roar_enum_servers_free(list); 
    179     return fh; 
     279    return 0; 
    180280   } 
    181281  } 
     
    188288 } else { 
    189289  /* connect via (char*)server */ 
    190   // find a port: 
    191  
    192   if ( !strcmp(server, "+invalid") ) { 
    193    roar_err_set(ROAR_ERROR_CANCELED); 
    194    return -1; 
    195   } 
    196  
    197   strncpy(user_sock, server, sizeof(user_sock)-1); 
    198   user_sock[sizeof(user_sock)-1] = 0; 
    199  
    200   if ( *user_sock != '/' ) { // don't test AF_UNIX sockets for ports 
    201    for (i = 0; user_sock[i] != 0; i++) { 
    202     if ( user_sock[i] == ':' ) { 
    203      if ( user_sock[i+1] == ':' ) { // DECnet, leave unchanged 
    204       is_decnet = 1; 
    205       obj = &user_sock[i+2]; 
    206       break; 
    207      } 
    208  
    209      port = atoi(&(user_sock[i+1])); 
    210      user_sock[i] = 0; 
    211      break; 
    212     } 
    213    } 
    214   } 
    215  
    216   if ( is_decnet ) { 
    217    if ( *user_sock == ':' ) { 
    218     if ( roar_socket_get_local_nodename() != NULL ) { 
    219      strncpy(user_sock, roar_socket_get_local_nodename(), sizeof(user_sock)-1); 
    220      user_sock[sizeof(user_sock)-1] = 0; 
    221      roar_mm_strlcat(user_sock, server, sizeof(user_sock)-1); 
    222      user_sock[sizeof(user_sock)-1] = 0; 
    223      obj  = strstr(user_sock, "::"); 
    224      obj += 2; 
    225     } 
    226    } 
    227  
    228    if ( *obj == 0 ) { 
    229 #ifdef DN_MAXOBJL 
    230     roar_mm_strlcat(user_sock, ROAR_DEFAULT_OBJECT, sizeof(user_sock)-1); 
    231     user_sock[sizeof(user_sock)-1] = 0; 
    232 #else 
    233     ROAR_ERR("roar_connect_raw(*): size of DECnet object unknown."); 
    234 #endif 
    235    } 
    236     ROAR_DBG("roar_connect_raw(*): user_sock='%s'", user_sock); 
    237   } 
    238  
    239   if ( port || is_decnet ) { 
    240    fh = roar_socket_connect(user_sock, port); 
    241    // restore the original string 
    242    user_sock[i] = ':'; 
    243   } else { 
    244 #if defined(ROAR_HAVE_STAT) && defined(ROAR_HAVE_H_SYS_STAT) 
    245    if ( user_sock[0] == '/' ) { 
    246     if ( stat(user_sock, &sockstat) == 0 ) { 
    247      if ( S_ISCHR(sockstat.st_mode) ) { 
    248       return open(user_sock, O_RDWR|O_NOCTTY, 0666); 
    249      } 
    250     } 
    251    } 
    252 #endif 
    253    fh = roar_socket_connect(user_sock, ROAR_DEFAULT_PORT); 
    254   } 
    255  } 
    256  
    257  ROAR_DBG("roar_connect_raw(*) = %i", fh); 
    258  
    259  return fh; 
     290  if ( _connect_server(con, server, -1, flags, timeout) == 0 ) 
     291   return 0; 
     292 } 
     293 
     294 roar_err_set(ROAR_ERROR_NODEV); 
     295 ROAR_DBG("roar_connect_raw(*) = -1 // error=NODEV"); 
     296 return -1; 
    260297} 
    261298 
    262299int roar_connect     (struct roar_connection * con, const char * server, int flags, uint_least32_t timeout) { 
    263  int fh; 
    264  int err; 
    265  
    266300 if ( con == NULL ) { 
    267301  roar_err_set(ROAR_ERROR_FAULT); 
     
    269303 } 
    270304 
     305 if ( roar_connect_none(con) == -1 ) 
     306  return -1; 
     307 
    271308 roar_err_set(ROAR_ERROR_UNKNOWN); 
    272  fh = roar_connect_raw(server, flags, timeout); 
    273  
    274  if ( fh == -1 ) 
    275   return -1; 
    276  
    277  if ( roar_connect_fh(con, fh) == -1 ) { 
    278   err = roar_error; 
    279 #ifdef ROAR_TARGET_WIN32 
    280   closesocket(fh); 
    281 #else 
    282   close(fh); 
    283 #endif 
    284   roar_error = err; 
    285   return -1; 
    286  } 
     309 if ( roar_connect_raw(con, server, flags, timeout) == -1 ) 
     310  return -1; 
    287311 
    288312 if ( server != NULL ) { 
Note: See TracChangeset for help on using the changeset viewer.