source: roaraudio/libroar/basic.c @ 4806:988c888752c2

Last change on this file since 4806:988c888752c2 was 4806:988c888752c2, checked in by phi, 13 years ago

Started with support for non-blocking server locating attempts.
Speed up SLP lookup without DA by about 6 sec.

File size: 11.0 KB
RevLine 
[0]1//basic.c:
2
[690]3/*
[4708]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011
[690]5 *
6 *  This file is part of libroar 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 *  libroar 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
[3517]21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
[690]23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
[0]36#include "libroar.h"
37
38int roar_connect_raw (char * server) {
[4806]39 return roar_connect_raw2(server, 0, 0);
40}
41
42int roar_connect_raw2 (char * server, int flags, uint_least32_t timeout) {
[4780]43#ifdef ROAR_HAVE_LIBSLP
[4653]44 struct roar_libroar_config * config = roar_libroar_get_config();
[4780]45#endif
[0]46 char user_sock[80];
47 char * roar_server;
[701]48 int i = 0;
[0]49 int port = 0;
50 int fh = -1;
[501]51 int is_decnet = 0;
[516]52 char * obj = NULL;
[1475]53#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
[1026]54 struct passwd * pwd;
[1393]55#endif
[828]56#ifdef ROAR_HAVE_LIBDNET
57 struct stat decnet_stat;
58#endif
[3372]59#ifdef ROAR_HAVE_LIBX11
60 struct roar_x11_connection * x11con;
61#endif
[4806]62 struct roar_server * list;
63 int workarounds_store;
[0]64
[807]65 roar_errno = ROAR_ERROR_UNKNOWN;
66
[4806]67 if ( timeout != 0 ) {
68  roar_errno = ROAR_ERROR_INVAL;
69  return -1;
70 }
71
72 if ( flags & ROAR_ENUM_FLAG_HARDNONBLOCK )
73  flags |= ROAR_ENUM_FLAG_NONBLOCK;
74
75
[2567]76 if ( server == NULL )
77  server = roar_libroar_get_server();
78
[61]79 if ( server == NULL && (roar_server = getenv("ROAR_SERVER")) != NULL )
[0]80  server = roar_server;
81
[3372]82#ifdef ROAR_HAVE_LIBX11
83 if ( server == NULL ) {
84  if ( (x11con = roar_x11_connect(NULL)) != NULL ) {
85   server = roar_x11_get_prop(x11con, "ROAR_SERVER");
86   roar_x11_disconnect(x11con);
87  }
88 }
89#endif
90
[1436]91#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
[448]92 if ( server == NULL && (i = readlink("/etc/roarserver", user_sock, 79)) != -1 ) {
93   user_sock[i] = 0;
94   server = user_sock;
95 }
[1093]96#endif
[448]97
[3077]98 if ( server != NULL && !strcasecmp(server, "+slp") ) {
[3076]99  server = roar_slp_find_roard(0);
100  if ( server == NULL ) {
101   return -1;
102  }
103 }
104
[61]105 if ( server == NULL || *server == 0 ) {
[0]106  /* connect via defaults */
107
[1764]108#ifdef ROAR_HAVE_UNIX
[1436]109#ifndef ROAR_TARGET_MICROCONTROLLER
[1026]110  roar_server = getenv("HOME");
[1435]111#else
112  roar_server = NULL;
113#endif
[1026]114
115  if ( roar_server == NULL ) {
[1436]116#if !defined(ROAR_TARGET_WIN32) && !defined(ROAR_TARGET_MICROCONTROLLER)
[1026]117   if ( (pwd = getpwuid(getuid())) == NULL ) {
118    roar_server = "/NX-HOME-DIR";
119   } else {
120    roar_server = pwd->pw_dir;
121   }
[1078]122#else
123   roar_server = "/WIN32-SUCKS";
124#endif
[1026]125  }
126
127  snprintf(user_sock, 79, "%s/%s", roar_server, ROAR_DEFAULT_SOCK_USER);
[0]128
129  if ( (fh = roar_socket_connect(user_sock, 0)) != -1 )
130   return fh;
131
[237]132  if ( (fh = roar_socket_connect(ROAR_DEFAULT_SOCK_GLOBAL, 0)) != -1 )
133   return fh;
[1764]134#endif
[237]135
[0]136  if ( (fh = roar_socket_connect(ROAR_DEFAULT_HOST, ROAR_DEFAULT_PORT)) != -1 )
137   return fh;
138
[828]139#ifdef ROAR_HAVE_LIBDNET
140  if ( stat(ROAR_PROC_NET_DECNET, &decnet_stat) == 0 ) {
141   if ( roar_socket_get_local_nodename() ) {
142    snprintf(user_sock, 79, "%s::%s", roar_socket_get_local_nodename(), ROAR_DEFAULT_OBJECT);
[2010]143    if ( (fh = roar_socket_connect(user_sock, ROAR_DEFAULT_NUM)) != -1 )
144     return fh;
[828]145   }
[522]146  }
[828]147#endif
[2007]148
[4109]149  if ( (fh = roar_socket_connect("+abstract", 0)) != -1 )
150   return fh;
151
[2007]152#ifdef ROAR_HAVE_LIBSLP
[4806]153 if ( !(config->workaround.workarounds & ROAR_LIBROAR_CONFIG_WAS_NO_SLP) &&
154      !(flags & ROAR_ENUM_FLAG_NONBLOCK)
155    ) {
156  if ( (server = roar_slp_find_roard(0)) != NULL ) {
[4653]157   if ( (fh = roar_connect_raw(server)) != -1 )
158    return fh;
[2014]159
[4806]160   /* in case we can not connect to the server given this may be a cache problem,
161      we do a new lookup with the cache disabled in this case                     */
162   ROAR_WARN("roar_connect_raw(*): Can not connect to SLP located server, disabling cache");
163   if ( (server = roar_slp_find_roard(1)) != NULL )
164    if ( (fh = roar_connect_raw(server)) != -1 )
165     return fh;
166  }
167 }
168
169 workarounds_store = config->workaround.workarounds;
170 config->workaround.workarounds |= ROAR_LIBROAR_CONFIG_WAS_NO_SLP;
171 list = roar_enum_servers(flags, -1, -1);
172 config->workaround.workarounds = workarounds_store;
173 if ( list != NULL ) {
174  for (i = 0; list[i].server != NULL; i++) {
175   if ( (fh = roar_connect_raw((char*)list[i].server)) != -1 ) {
176    roar_enum_servers_free(list);
[4653]177    return fh;
[4806]178   }
179  }
180  roar_enum_servers_free(list);
[4653]181 }
[2007]182#endif
[522]183
[2014]184 return -1;
185
[0]186 } else {
187  /* connect via (char*)server */
[501]188  // find a port:
189  if ( *server != '/' ) { // don't test AF_UNIX sockets for ports
190   for (i = 0; server[i] != 0; i++) {
191    if ( server[i] == ':' ) {
192     if ( server[i+1] == ':' ) { // DECnet, leave unchanged
193      is_decnet = 1;
[516]194      obj = &server[i+2];
[501]195      break;
196     }
197
198     port = atoi(server+i+1);
199     server[i] = 0;
200     break;
201    }
[0]202   }
203  }
204
[521]205  if ( is_decnet ) {
206    *user_sock = 0;
207   if ( *server == ':' ) {
208    if ( roar_socket_get_local_nodename() )
[1066]209     strncat(user_sock, roar_socket_get_local_nodename(), 6);
[521]210   }
211
[1066]212   strncat(user_sock, server, 79);
[516]213   server = user_sock;
[521]214   if ( *obj == 0 ) {
[1066]215#ifdef DN_MAXOBJL
216    strncat(user_sock, ROAR_DEFAULT_OBJECT, DN_MAXOBJL+2);
217#else
218    ROAR_ERR("roar_connect_raw(*): size of DECnet object unknown.");
219#endif
[521]220   }
[516]221  }
222
[501]223  if ( port || is_decnet ) {
[0]224   fh = roar_socket_connect(server, port);
225   // restore the original string
226   server[i] = ':';
227  } else {
228   fh = roar_socket_connect(server, ROAR_DEFAULT_PORT);
229  }
230 }
231
[807]232 if ( fh == -1 )
233  roar_errno = ROAR_ERROR_CONNREFUSED;
234
[0]235 ROAR_DBG("roar_connect_raw(*) = %i", fh);
236
237 return fh;
238}
239
240int roar_connect    (struct roar_connection * con, char * server) {
[4806]241 return roar_connect2(con, server, 0, 0);
242}
243
244int roar_connect2     (struct roar_connection * con, char * server, int flags, uint_least32_t timeout) {
[1315]245 int fh;
246
247 if ( con == NULL ) {
248  roar_errno = ROAR_ERROR_INVAL;
249  return -1;
250 }
251
[807]252 roar_errno = ROAR_ERROR_UNKNOWN;
[4806]253 fh = roar_connect_raw2(server, flags, timeout);
[0]254
[1315]255 if ( fh == -1 )
[0]256  return -1;
257
[1315]258 return roar_connect_fh(con, fh);
259}
260
261int roar_connect_fh (struct roar_connection * con, int fh) {
262
263 if ( con == NULL || fh == -1 ) {
264  roar_errno = ROAR_ERROR_INVAL;
265  return -1;
266 }
267
268 // specal hack to set an ilegal value used internaly in libroar:
269 if ( fh == -2 )
270  fh = -1;
271
272 memset(con, 0, sizeof(struct roar_connection));
273
[3869]274 con->flags = ROAR_CON_FLAGS_FH;
275 con->__fh  = fh;
276
277 if ( roar_vio_open_fh_socket(&(con->viocon), fh) != -1 )
278  con->flags |= ROAR_CON_FLAGS_VIO;
[1315]279
[807]280 roar_errno = ROAR_ERROR_NONE;
[0]281 return 0;
282}
283
[1660]284int roar_get_connection_fh (struct roar_connection * con) {
[3869]285 roar_debug_warn_sysio("roar_get_connection_fh", "roar_get_connection_vio2", NULL);
[2809]286
[2043]287 if ( con == NULL )
288  return -1;
289
[3869]290 if ( !(con->flags & ROAR_CON_FLAGS_FH) )
291  return -1;
292
[1660]293 return con->__fh;
294}
295
[2043]296int roar_get_connection_vio (struct roar_connection * con, struct roar_vio_calls * vio) {
[3869]297 roar_debug_warn_obsolete("roar_get_connection_vio", "roar_get_connection_vio2", NULL);
298
[2043]299 if ( con == NULL || vio == NULL )
300  return -1;
301
[3869]302 if ( !(con->flags & ROAR_CON_FLAGS_FH) )
303  return -1;
304
[2809]305 return roar_vio_open_fh_socket(vio, con->__fh);
[2043]306}
307
[3869]308struct roar_vio_calls * roar_get_connection_vio2 (struct roar_connection * con) {
309 if ( con == NULL )
310  return NULL;
311
312 if ( con->flags & ROAR_CON_FLAGS_VIO )
313  return &(con->viocon);
314
315// TODO: try to open the VIO.
316
317 return NULL;
318}
319
[0]320int roar_disconnect (struct roar_connection * con) {
[3869]321 struct roar_vio_calls * vio;
[0]322 struct roar_message m;
323
[3875]324 memset(&m, 0, sizeof(m));
325
[0]326 m.datalen = 0;
327 m.stream  = 0;
[133]328 m.pos     = 0;
[0]329 m.cmd     = ROAR_CMD_QUIT;
330
331 roar_req(con, &m, NULL);
332
[3869]333 if ( (vio = roar_get_connection_vio2(con)) != NULL ) {
334  roar_vio_close(vio);
[2809]335 }
[0]336
[1660]337 roar_connect_fh(con, -2);
[0]338
[807]339 roar_errno = ROAR_ERROR_NONE;
340
[0]341 return 0;
342}
343
[3913]344int roar_set_connection_callback(struct roar_connection * con,
345                                 void (*cb)(struct roar_connection * con,
346                                            struct roar_message    * mes,
347                                            void                   * userdata),
348                                 void * userdata) {
349 if ( con == NULL )
350  return -1;
351
352 con->cb       = cb;
353 con->userdata = userdata;
354
355 return 0;
356}
357
[3914]358int roar_sync         (struct roar_connection * con) {
359 // wait for any non-client reqs
360 return roar_wait_msg(con, 0x0000, 0x8000);
361}
362
363int roar_wait_msg     (struct roar_connection * con, int16_t seq, int16_t seqmask) {
364 return -1;
365}
[3913]366
[3836]367int roar_noop         (struct roar_connection * con) {
368 struct roar_message mes;
369
370 if ( con == NULL ) {
371  return -1;
372 }
373
374 memset(&mes, 0, sizeof(mes));
375
376 mes.cmd = ROAR_CMD_NOOP;
377
378 return roar_req(con, &mes, NULL);
379}
380
[0]381int roar_identify   (struct roar_connection * con, char * name) {
382 struct roar_message mes;
383 pid_t pid;
384 int max_len;
385
[807]386 roar_errno = ROAR_ERROR_UNKNOWN;
387
[0]388 ROAR_DBG("roar_identify(*): try to identify myself...");
389
[3875]390 memset(&mes, 0, sizeof(mes));
391
[0]392 mes.cmd    = ROAR_CMD_IDENTIFY;
393 mes.stream = 0;
394 mes.pos    = 0;
395
396 ROAR_DBG("roar_identify(*): name=%p", name);
397
398 if ( name == NULL )
399  name = "libroar client";
400
401 ROAR_DBG("roar_identify(*): name=%p", name);
402
403 max_len = strlen(name);
404 ROAR_DBG("roar_identify(*): strlen(name) = %i", max_len);
405
406 if ( max_len > (LIBROAR_BUFFER_MSGDATA - 5) )
407  max_len = LIBROAR_BUFFER_MSGDATA - 5;
408
409 mes.datalen = 5 + max_len;
410 mes.data[0] = 1;
411
412 pid = getpid();
413 *(uint32_t*)(mes.data+1) = ROAR_HOST2NET32(pid);
414 ROAR_DBG("roar_identify(*): pid = %i", pid);
415
416 strncpy(mes.data+5, name, max_len);
417
418 return roar_req(con, &mes, NULL);
419}
420
421int roar_debug_message_print (struct roar_message * mes) {
422 if ( mes == NULL )
423  return -1;
424
425 ROAR_DBG("roar_debug_message_print(*): Command: %i", mes->cmd);
426 ROAR_DBG("roar_debug_message_print(*): Stream : %u", mes->stream);
427 ROAR_DBG("roar_debug_message_print(*): Pos    : %u", mes->pos);
428 ROAR_DBG("roar_debug_message_print(*): Datalen: %i", mes->datalen);
429
430 ROAR_DBG("roar_debug_message_print(*) = 0");
431 return 0;
432}
433
434int roar_debug_audio_info_print (struct roar_audio_info * info) {
435 if ( info == NULL )
436  return -1;
437
438 ROAR_DBG("roar_debug_audio_info_print(*): Rate    : %i", info->rate);
439 ROAR_DBG("roar_debug_audio_info_print(*): Channels: %i", info->channels);
440 ROAR_DBG("roar_debug_audio_info_print(*): Bits    : %i", info->bits);
441 ROAR_DBG("roar_debug_audio_info_print(*): Codec   : %i", info->codec);
442
443 ROAR_DBG("roar_debug_audio_info_print(*) = 0");
444 return 0;
445}
446
447//ll
Note: See TracBrowser for help on using the repository browser.