source: roaraudio/libroar/proto.c @ 5467:30f989510eb4

Last change on this file since 5467:30f989510eb4 was 5467:30f989510eb4, checked in by phi, 12 years ago

make use of seq-numbers in v2 messages

File size: 10.5 KB
Line 
1//proto.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012
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
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
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
36#include "libroar.h"
37
38#define _ROAR_MESS_BUF_LEN_V0  (1 /* version */ + 1 /* cmd */ + 2 /* stream */ + 4 /* pos */ + 2 /* datalen */)
39#define _ROAR_MESS_BUF_LEN_V1  (1 /* version */ + 1 /* cmd */ + 2 /* stream */ + 4 /* pos */ + 2 /* datalen */ + \
40                                1 /* flags */)
41#define _ROAR_MESS_BUF_LEN_V2  (1 /* version */ + 1 /* cmd */ + 2 /* stream */ + 8 /* pos */ + 2 /* datalen */ + \
42                                4 /* flags */   + 2 /* seq */)
43#define _ROAR_MESS_BUF_LEN     _ROAR_MESS_BUF_LEN_V0
44#define _ROAR_MESS_BUF_LEN_MAX ROAR_MAX3(_ROAR_MESS_BUF_LEN_V0, _ROAR_MESS_BUF_LEN_V1, _ROAR_MESS_BUF_LEN_V2)
45
46#define _ROAR_MESS_CRC_LEN_V0  0 /* No CRC */
47#define _ROAR_MESS_CRC_LEN_V1  1
48#define _ROAR_MESS_CRC_LEN_V2  4
49#define _ROAR_MESS_CRC_LEN_MAX ROAR_MAX3(_ROAR_MESS_CRC_LEN_V0, _ROAR_MESS_CRC_LEN_V1, _ROAR_MESS_CRC_LEN_V2)
50
51int roar_send_message (struct roar_connection * con, struct roar_message * mes, char * data) {
52 struct roar_vio_calls * vio;
53
54 if ( (vio = roar_get_connection_vio2(con)) == NULL )
55  return -1;
56
57 return roar_vsend_message(vio, mes, data);
58}
59
60int roar_vsend_message(struct roar_vio_calls * vio, struct roar_message * mes, char *  data) {
61 char buf[_ROAR_MESS_BUF_LEN_MAX];
62 char crc[_ROAR_MESS_CRC_LEN_MAX];
63 size_t headerlen = 0;
64 size_t crclen    = 0;
65 char * bufptr;
66
67 roar_err_set(ROAR_ERROR_UNKNOWN);
68
69 ROAR_DBG("roar_send_message(*): try to send an request...");
70
71 headerlen = _ROAR_MESS_BUF_LEN;
72
73 buf[0] = mes->version; // first byte is always the version.
74
75 switch (mes->version) {
76  case 0:
77    buf[1]              = (unsigned char) mes->cmd;
78    *(uint16_t*)(buf+2) = ROAR_HOST2NET16(mes->stream);
79    *(uint32_t*)(buf+4) = ROAR_HOST2NET32(mes->pos);
80    *(uint16_t*)(buf+8) = ROAR_HOST2NET16(mes->datalen);
81    headerlen           = _ROAR_MESS_BUF_LEN_V0;
82   break;
83  case 1:
84    buf[1]              = (unsigned char) (mes->flags & 0xFF);
85    buf[2]              = (unsigned char)  mes->cmd;
86    // ...
87    roar_err_set(ROAR_ERROR_NSVERSION);
88    return -1;
89   break;
90  case 2:
91    headerlen           = 16;
92    buf[1]              = (unsigned char) mes->cmd;
93    *(uint16_t*)(buf+2) = ROAR_HOST2NET16(mes->stream);
94    *(uint32_t*)(buf+4) = ROAR_HOST2NET32(mes->flags);
95    if ( mes->flags & ROAR_MF_LSPOS ) {
96     *(uint64_t*)(buf+8) = ROAR_HOST2NET64(mes->pos64);
97     bufptr = buf+16;
98     headerlen          += 4;
99    } else {
100     *(uint32_t*)(buf+8) = ROAR_HOST2NET32(mes->pos);
101     bufptr = buf+12;
102    }
103    *(uint16_t*)(bufptr+0) = ROAR_HOST2NET16(mes->datalen);
104    *(uint16_t*)(bufptr+2) = ROAR_HOST2NET16(mes->seq);
105   break;
106  default:
107    roar_err_set(ROAR_ERROR_BADVERSION);
108    return -1;
109 }
110
111 if ( roar_vio_write(vio, buf, headerlen) != (ssize_t)headerlen ) {
112  return -1;
113 }
114
115 if ( mes->datalen != 0 ) {
116  if ( roar_vio_write(vio, data == NULL ? mes->data : data, mes->datalen) != (ssize_t)mes->datalen ) {
117   return -1;
118  }
119 }
120
121 if ( crclen != 0 ) {
122  if ( roar_vio_write(vio, crc, crclen) != (ssize_t)crclen ) {
123   return -1;
124  }
125 }
126
127 roar_err_clear();
128
129 ROAR_DBG("roar_send_message(*) = 0");
130 return 0;
131}
132
133int roar_recv_message (struct roar_connection * con, struct roar_message * mes, char ** data) {
134 return roar_recv_message2(con, mes, data, NULL);
135}
136int roar_recv_message2 (struct roar_connection * con, struct roar_message * mes, char ** data,
137                        struct roar_error_frame * errorframe) {
138 struct roar_vio_calls * vio;
139
140 if ( (vio = roar_get_connection_vio2(con)) == NULL )
141  return -1;
142
143 return roar_vrecv_message2(vio, mes, data, errorframe);
144}
145
146int roar_vrecv_message(struct roar_vio_calls * vio, struct roar_message * mes, char ** data) {
147 return roar_vrecv_message2(vio, mes, data, NULL);
148}
149
150int roar_vrecv_message2(struct roar_vio_calls  * vio, struct roar_message * mes, char ** data,
151                        struct roar_error_frame * errorframe) {
152 // TODO: add CRC support.
153 char buf[_ROAR_MESS_BUF_LEN_MAX];
154// char crc[_ROAR_MESS_CRC_LEN_MAX];
155 size_t headerlen = 0;
156// size_t crclen    = 0;
157 size_t needlen;
158 char * bufptr;
159
160 roar_err_set(ROAR_ERROR_UNKNOWN);
161
162 ROAR_DBG("roar_vrecv_message2(vio=%p, mes=%p, data=%p, errorframe=%p) = ?", vio, mes, data, errorframe);
163
164 ROAR_DBG("roar_vrecv_message2(*): try to get a response form the server...");
165
166 if ( vio == NULL || mes == NULL ) {
167  roar_err_set(ROAR_ERROR_FAULT);
168  return -1;
169 }
170
171 if ( data != NULL )
172  *data = NULL;
173
174 memset(mes, 0, sizeof(struct roar_message));
175
176 if ( roar_vio_read(vio, buf, _ROAR_MESS_BUF_LEN_V0) != _ROAR_MESS_BUF_LEN_V0 ) {
177  roar_err_set(ROAR_ERROR_PROTO);
178  return -1;
179 }
180
181 ROAR_DBG("roar_vrecv_message2(*): Got a header");
182
183 mes->version = buf[0];
184 headerlen    = _ROAR_MESS_BUF_LEN_V0;
185
186 switch (mes->version) {
187  case 0:
188    // we already have all data, so create the struct
189    mes->cmd     = (unsigned char)buf[1];
190    mes->stream  = ROAR_NET2HOST16(*(uint16_t*)(buf+2));
191    mes->pos     = ROAR_NET2HOST32(*(uint32_t*)(buf+4));
192    mes->pos64   = mes->pos;
193    mes->datalen = ROAR_NET2HOST16(*(uint16_t*)(buf+8));
194   break;
195  case 2:
196    mes->cmd     = (unsigned char)buf[1];
197    mes->stream  = ROAR_NET2HOST16(*(uint16_t*)(buf+2));
198    mes->flags   = ROAR_NET2HOST32(*(uint32_t*)(buf+4));
199    if ( mes->flags & ROAR_MF_LSPOS ) {
200     headerlen = 20;
201    } else {
202     headerlen = 16;
203    }
204   break;
205  default:
206    roar_err_set(ROAR_ERROR_NSVERSION);
207    return -1;
208   break;
209 }
210
211 // check specal case where headerlen < _ROAR_MESS_BUF_LEN_V0, this means that we need to put
212 // some data we read as header into the data part of the message.
213 if ( headerlen > _ROAR_MESS_BUF_LEN_V0 ) {
214  needlen = headerlen - _ROAR_MESS_BUF_LEN_V0;
215  if ( roar_vio_read(vio, buf+_ROAR_MESS_BUF_LEN_V0, needlen) != (ssize_t)needlen ) {
216   roar_err_set(ROAR_ERROR_PROTO);
217   return -1;
218  }
219 }
220
221 switch (mes->version) {
222  case 2:
223    if ( mes->flags & ROAR_MF_LSPOS ) {
224     mes->pos64   = ROAR_NET2HOST32(*(uint64_t*)(buf+8));
225     mes->pos     = mes->pos64 & (uint64_t)0x0FFFFFFFFLLU;
226     bufptr       = buf+16;
227    } else {
228     mes->pos     = ROAR_NET2HOST32(*(uint32_t*)(buf+8));
229     mes->pos64   = mes->pos;
230     bufptr       = buf+12;
231    }
232    mes->datalen = ROAR_NET2HOST16(*(uint16_t*)(bufptr+0));
233    mes->seq     = ROAR_NET2HOST16(*(uint16_t*)(bufptr+2));
234   break;
235 }
236
237 if ( (int16_t)mes->stream == -1 )
238  mes->stream = -1;
239
240 ROAR_DBG("roar_vrecv_message2(*): command=%i(%s)", mes->cmd,
241           mes->cmd == ROAR_CMD_OK ? "OK" : (mes->cmd == ROAR_CMD_ERROR ? "ERROR" : "UNKNOWN"));
242
243// Below we only have data handling, handling of header is finished:
244
245 if ( mes->datalen == 0 ) {
246  ROAR_DBG("roar_vrecv_message2(*): no data in this pkg");
247  if ( mes->cmd == ROAR_CMD_ERROR && errorframe != NULL ) {
248   roar_err_init(errorframe);
249  }
250  roar_err_clear();
251  ROAR_DBG("roar_vrecv_message2(*) = 0");
252  return 0;
253 }
254
255 if ( mes->datalen <= LIBROAR_BUFFER_MSGDATA ) {
256  if ( roar_vio_read(vio, mes->data, mes->datalen) == (ssize_t)mes->datalen ) {
257   ROAR_DBG("roar_vrecv_message2(*): Got data!");
258   roar_err_clear();
259   if ( mes->cmd == ROAR_CMD_ERROR && errorframe != NULL ) {
260    if ( roar_err_parsemsg(mes, *data, errorframe) != 0 ) {
261     roar_err_init(errorframe);
262    }
263   }
264   ROAR_DBG("roar_vrecv_message2(*) = 0");
265   return 0;
266  }
267
268  return -1;
269 } else {
270  if ( data == NULL ) {
271   roar_err_set(ROAR_ERROR_MSGSIZE);
272   return -1;
273  }
274
275  if ( (*data = roar_mm_malloc(mes->datalen)) == NULL ) {
276   roar_err_set(ROAR_ERROR_NOMEM);
277   return -1;
278  }
279
280  if ( mes->datalen == 0 ) {
281   roar_err_clear();
282   if ( mes->cmd == ROAR_CMD_ERROR && errorframe != NULL ) {
283    roar_err_init(errorframe);
284   }
285   return 0;
286  }
287
288  if ( roar_vio_read(vio, *data, mes->datalen) == (ssize_t)mes->datalen ) {
289   ROAR_DBG("roar_vrecv_message2(*): Got data!");
290
291   roar_err_clear();
292   if ( mes->cmd == ROAR_CMD_ERROR && errorframe != NULL ) {
293    if ( roar_err_parsemsg(mes, *data, errorframe) != 0 ) {
294     roar_err_init(errorframe);
295    }
296   }
297   ROAR_DBG("roar_vrecv_message2(*) = 0");
298   return 0;
299  }
300
301  // error is still set (by roar_vio_read()).
302  return -1;
303 }
304
305 // what happened here?
306 return -1;
307}
308
309int roar_req (struct roar_connection * con, struct roar_message * mes, char ** data) {
310 return roar_req2(con, mes, data, NULL);
311}
312
313int roar_req2          (struct roar_connection * con, struct roar_message * mes, char ** data,
314                        struct roar_error_frame * errorframe) {
315 struct roar_vio_calls * vio;
316
317 if ( (vio = roar_get_connection_vio2(con)) == NULL )
318  return -1;
319
320 if ( mes->version == _ROAR_MESSAGE_VERSION )
321  mes->version = con->version;
322
323 if ( mes->version == 2 ) {
324  mes->seq = roar_message_genseq(con, -1);
325 }
326
327 return roar_vreq2(vio, mes, data, errorframe);
328}
329
330int roar_vreq         (struct roar_vio_calls * vio, struct roar_message * mes, char ** data) {
331 return roar_vreq2(vio, mes, data, NULL);
332}
333
334int roar_vreq2         (struct roar_vio_calls  * vio, struct roar_message * mes, char ** data,
335                        struct roar_error_frame * errorframe) {
336 if ( roar_vsend_message(vio, mes, data != NULL ? *data : NULL) != 0 )
337  return -1;
338
339 if ( data != NULL )
340  roar_mm_free(*data);
341
342 roar_vio_sync(vio); // we need to do this becasue of ssl/compressed links
343
344 return roar_vrecv_message2(vio, mes, data, errorframe);
345}
346
347//ll
Note: See TracBrowser for help on using the repository browser.