source: roaraudio/libroar/basic.c @ 501:985357040570

Last change on this file since 501:985357040570 was 501:985357040570, checked in by phi, 16 years ago

ha! It's working: RoarAudio via DECnet! :), needs cleanup

File size: 6.2 KB
Line 
1//basic.c:
2
3#include "libroar.h"
4
5int roar_connect_raw (char * server) {
6 char user_sock[80];
7 char * roar_server;
8 int i;
9 int port = 0;
10 int fh = -1;
11 int is_decnet = 0;
12
13 if ( server == NULL && (roar_server = getenv("ROAR_SERVER")) != NULL )
14  server = roar_server;
15
16 if ( server == NULL && (i = readlink("/etc/roarserver", user_sock, 79)) != -1 ) {
17   user_sock[i] = 0;
18   server = user_sock;
19 }
20
21 if ( server == NULL || *server == 0 ) {
22  /* connect via defaults */
23
24
25  snprintf(user_sock, 79, "%s/%s", getenv("HOME"), ROAR_DEFAULT_SOCK_USER);
26
27  if ( (fh = roar_socket_connect(user_sock, 0)) != -1 )
28   return fh;
29
30  if ( (fh = roar_socket_connect(ROAR_DEFAULT_SOCK_GLOBAL, 0)) != -1 )
31   return fh;
32
33  if ( (fh = roar_socket_connect(ROAR_DEFAULT_HOST, ROAR_DEFAULT_PORT)) != -1 )
34   return fh;
35
36 } else {
37  /* connect via (char*)server */
38  // find a port:
39  if ( *server != '/' ) { // don't test AF_UNIX sockets for ports
40   for (i = 0; server[i] != 0; i++) {
41    if ( server[i] == ':' ) {
42     if ( server[i+1] == ':' ) { // DECnet, leave unchanged
43      is_decnet = 1;
44      break;
45     }
46
47     port = atoi(server+i+1);
48     server[i] = 0;
49     break;
50    }
51   }
52  }
53
54  if ( port || is_decnet ) {
55   fh = roar_socket_connect(server, port);
56   // restore the original string
57   server[i] = ':';
58  } else {
59   fh = roar_socket_connect(server, ROAR_DEFAULT_PORT);
60  }
61 }
62
63 ROAR_DBG("roar_connect_raw(*) = %i", fh);
64
65 return fh;
66}
67
68int roar_connect    (struct roar_connection * con, char * server) {
69 con->fh = roar_connect_raw(server);
70
71 if ( con->fh == -1 )
72  return -1;
73
74 return 0;
75}
76
77int roar_disconnect (struct roar_connection * con) {
78 struct roar_message m;
79
80 m.datalen = 0;
81 m.stream  = 0;
82 m.pos     = 0;
83 m.cmd     = ROAR_CMD_QUIT;
84
85 roar_req(con, &m, NULL);
86
87 close(con->fh);
88
89 con->fh = -1;
90
91 return 0;
92}
93
94int roar_identify   (struct roar_connection * con, char * name) {
95 struct roar_message mes;
96 pid_t pid;
97 int max_len;
98
99 ROAR_DBG("roar_identify(*): try to identify myself...");
100
101 mes.cmd    = ROAR_CMD_IDENTIFY;
102 mes.stream = 0;
103 mes.pos    = 0;
104
105 ROAR_DBG("roar_identify(*): name=%p", name);
106
107 if ( name == NULL )
108  name = "libroar client";
109
110 ROAR_DBG("roar_identify(*): name=%p", name);
111
112 max_len = strlen(name);
113 ROAR_DBG("roar_identify(*): strlen(name) = %i", max_len);
114
115 if ( max_len > (LIBROAR_BUFFER_MSGDATA - 5) )
116  max_len = LIBROAR_BUFFER_MSGDATA - 5;
117
118 mes.datalen = 5 + max_len;
119 mes.data[0] = 1;
120
121 pid = getpid();
122 *(uint32_t*)(mes.data+1) = ROAR_HOST2NET32(pid);
123 ROAR_DBG("roar_identify(*): pid = %i", pid);
124
125 strncpy(mes.data+5, name, max_len);
126
127 return roar_req(con, &mes, NULL);
128}
129
130#define _ROAR_MESS_BUF_LEN (1 /* version */ + 1 /* cmd */ + 2 /* stream */ + 4 /* pos */ + 2 /* datalen */)
131int roar_send_message (struct roar_connection * con, struct roar_message * mes, char * data) {
132 char buf[_ROAR_MESS_BUF_LEN];
133
134 ROAR_DBG("roar_send_message(*): try to send an request...");
135
136 buf[0] = _ROAR_MESSAGE_VERSION;
137 buf[1] = (unsigned char) mes->cmd;
138 *(uint16_t*)(buf+2) = ROAR_HOST2NET16(mes->stream);
139 *(uint32_t*)(buf+4) = ROAR_HOST2NET32(mes->pos);
140 *(uint16_t*)(buf+8) = ROAR_HOST2NET16(mes->datalen);
141
142 if ( write(con->fh, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN )
143  return -1;
144
145 if ( mes->datalen != 0 )
146  if ( write(con->fh, data == NULL ? mes->data : data, mes->datalen) != mes->datalen )
147   return -1;
148
149 ROAR_DBG("roar_send_message(*) = 0");
150 return 0;
151}
152
153int roar_recv_message (struct roar_connection * con, struct roar_message * mes, char ** data) {
154 char buf[_ROAR_MESS_BUF_LEN];
155/*
156#ifdef ROAR_HAVE_LIBDNET
157 int len;
158#endif
159*/
160
161 ROAR_DBG("roar_recv_message(*): try to get a response form the server...");
162
163 if ( data )
164  *data = NULL;
165
166/*
167#ifdef ROAR_HAVE_LIBDNET
168 if ( (len = read(con->fh, buf, _ROAR_MESS_BUF_LEN)) != _ROAR_MESS_BUF_LEN ) {
169  if ( len != 0 )
170   return -1;
171
172  usleep(2000);
173
174  if ( read(con->fh, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN )
175   return -1;
176  }
177#else
178*/
179 if ( read(con->fh, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN )
180  return -1;
181/*
182#endif
183*/
184
185 ROAR_DBG("roar_recv_message(*): Got a header");
186
187 if ( buf[0] != _ROAR_MESSAGE_VERSION )
188  return -1;
189
190 mes->cmd     = (unsigned char)buf[1];
191 mes->stream  = ROAR_NET2HOST16(*(uint16_t*)(buf+2));
192 mes->pos     = ROAR_NET2HOST32(*(uint32_t*)(buf+4));
193 mes->datalen = ROAR_NET2HOST16(*(uint16_t*)(buf+8));
194
195 ROAR_DBG("roar_recv_message(*): command=%i(%s)", mes->cmd,
196           mes->cmd == ROAR_CMD_OK ? "OK" : (mes->cmd == ROAR_CMD_ERROR ? "ERROR" : "UNKNOWN"));
197
198 if ( mes->datalen == 0 ) {
199  ROAR_DBG("roar_recv_message(*): no data in this pkg");
200  ROAR_DBG("roar_recv_message(*) = 0");
201  return 0;
202 }
203
204 if ( mes->datalen <= LIBROAR_BUFFER_MSGDATA ) {
205  if ( read(con->fh, mes->data, mes->datalen) == mes->datalen ) {
206   ROAR_DBG("roar_recv_message(*): Got data!");
207   ROAR_DBG("roar_recv_message(*) = 0");
208   return 0;
209  }
210  return -1;
211 } else {
212  if ( data == NULL )
213   return -1;
214
215  if ( (*data = malloc(mes->datalen)) == NULL )
216   return -1;
217
218  if ( mes->datalen == 0 )
219   return 0;
220
221  if ( read(con->fh, *data, mes->datalen) == mes->datalen ) {
222   ROAR_DBG("roar_recv_message(*): Got data!");
223   ROAR_DBG("roar_recv_message(*) = 0");
224   return 0;
225  }
226  return -1;
227 }
228
229 return -1;
230}
231
232int roar_req (struct roar_connection * con, struct roar_message * mes, char ** data) {
233 if ( roar_send_message(con, mes, data ? *data : NULL) != 0 )
234  return -1;
235
236 if ( data )
237  free(*data);
238
239 return roar_recv_message(con, mes, data);
240}
241
242int roar_debug_message_print (struct roar_message * mes) {
243 if ( mes == NULL )
244  return -1;
245
246 ROAR_DBG("roar_debug_message_print(*): Command: %i", mes->cmd);
247 ROAR_DBG("roar_debug_message_print(*): Stream : %u", mes->stream);
248 ROAR_DBG("roar_debug_message_print(*): Pos    : %u", mes->pos);
249 ROAR_DBG("roar_debug_message_print(*): Datalen: %i", mes->datalen);
250
251 ROAR_DBG("roar_debug_message_print(*) = 0");
252 return 0;
253}
254
255int roar_debug_audio_info_print (struct roar_audio_info * info) {
256 if ( info == NULL )
257  return -1;
258
259 ROAR_DBG("roar_debug_audio_info_print(*): Rate    : %i", info->rate);
260 ROAR_DBG("roar_debug_audio_info_print(*): Channels: %i", info->channels);
261 ROAR_DBG("roar_debug_audio_info_print(*): Bits    : %i", info->bits);
262 ROAR_DBG("roar_debug_audio_info_print(*): Codec   : %i", info->codec);
263
264 ROAR_DBG("roar_debug_audio_info_print(*) = 0");
265 return 0;
266}
267
268//ll
Note: See TracBrowser for help on using the repository browser.