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
RevLine 
[3882]1//proto.c:
2
3/*
[5381]4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2012
[3882]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
[3887]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
[3882]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) {
[3887]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;
[3882]66
[4873]67 roar_err_set(ROAR_ERROR_UNKNOWN);
[3882]68
69 ROAR_DBG("roar_send_message(*): try to send an request...");
70
[3887]71 headerlen = _ROAR_MESS_BUF_LEN;
72
73 buf[0] = mes->version; // first byte is always the version.
[3882]74
[3887]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    // ...
[4951]87    roar_err_set(ROAR_ERROR_NSVERSION);
[3887]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 ) {
[5463]96     *(uint64_t*)(buf+8) = ROAR_HOST2NET64(mes->pos64);
[3887]97     bufptr = buf+16;
98     headerlen          += 4;
99    } else {
[5463]100     *(uint32_t*)(buf+8) = ROAR_HOST2NET32(mes->pos);
[3887]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:
[4951]107    roar_err_set(ROAR_ERROR_BADVERSION);
[3887]108    return -1;
109 }
110
[5270]111 if ( roar_vio_write(vio, buf, headerlen) != (ssize_t)headerlen ) {
[3882]112  return -1;
113 }
114
115 if ( mes->datalen != 0 ) {
[5270]116  if ( roar_vio_write(vio, data == NULL ? mes->data : data, mes->datalen) != (ssize_t)mes->datalen ) {
[3882]117   return -1;
118  }
119 }
120
[3887]121 if ( crclen != 0 ) {
[5270]122  if ( roar_vio_write(vio, crc, crclen) != (ssize_t)crclen ) {
[3887]123   return -1;
124  }
125 }
126
[4873]127 roar_err_clear();
[3882]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) {
[5146]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) {
[3882]138 struct roar_vio_calls * vio;
139
140 if ( (vio = roar_get_connection_vio2(con)) == NULL )
141  return -1;
142
[5146]143 return roar_vrecv_message2(vio, mes, data, errorframe);
[3882]144}
145
146int roar_vrecv_message(struct roar_vio_calls * vio, struct roar_message * mes, char ** data) {
[5146]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) {
[3887]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;
[3882]159
[4873]160 roar_err_set(ROAR_ERROR_UNKNOWN);
[3882]161
[5146]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...");
[3882]165
[5146]166 if ( vio == NULL || mes == NULL ) {
167  roar_err_set(ROAR_ERROR_FAULT);
[3882]168  return -1;
[5146]169 }
[3882]170
171 if ( data != NULL )
172  *data = NULL;
173
174 memset(mes, 0, sizeof(struct roar_message));
175
[3887]176 if ( roar_vio_read(vio, buf, _ROAR_MESS_BUF_LEN_V0) != _ROAR_MESS_BUF_LEN_V0 ) {
[4873]177  roar_err_set(ROAR_ERROR_PROTO);
[3882]178  return -1;
179 }
180
[5146]181 ROAR_DBG("roar_vrecv_message2(*): Got a header");
[3882]182
[3887]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));
[5463]192    mes->pos64   = mes->pos;
[3887]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:
[4951]206    roar_err_set(ROAR_ERROR_NSVERSION);
[3887]207    return -1;
208   break;
[3882]209 }
210
[3887]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 ) {
[4873]216   roar_err_set(ROAR_ERROR_PROTO);
[3887]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));
[5463]225     mes->pos     = mes->pos64 & (uint64_t)0x0FFFFFFFFLLU;
[3887]226     bufptr       = buf+16;
227    } else {
228     mes->pos     = ROAR_NET2HOST32(*(uint32_t*)(buf+8));
[5463]229     mes->pos64   = mes->pos;
[3887]230     bufptr       = buf+12;
231    }
232    mes->datalen = ROAR_NET2HOST16(*(uint16_t*)(bufptr+0));
[5467]233    mes->seq     = ROAR_NET2HOST16(*(uint16_t*)(bufptr+2));
[3887]234   break;
235 }
[3882]236
237 if ( (int16_t)mes->stream == -1 )
238  mes->stream = -1;
239
[5146]240 ROAR_DBG("roar_vrecv_message2(*): command=%i(%s)", mes->cmd,
[3882]241           mes->cmd == ROAR_CMD_OK ? "OK" : (mes->cmd == ROAR_CMD_ERROR ? "ERROR" : "UNKNOWN"));
242
[3887]243// Below we only have data handling, handling of header is finished:
244
[3882]245 if ( mes->datalen == 0 ) {
[5146]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  }
[4873]250  roar_err_clear();
[5146]251  ROAR_DBG("roar_vrecv_message2(*) = 0");
[3882]252  return 0;
253 }
254
255 if ( mes->datalen <= LIBROAR_BUFFER_MSGDATA ) {
[5270]256  if ( roar_vio_read(vio, mes->data, mes->datalen) == (ssize_t)mes->datalen ) {
[5146]257   ROAR_DBG("roar_vrecv_message2(*): Got data!");
[4873]258   roar_err_clear();
[5146]259   if ( mes->cmd == ROAR_CMD_ERROR && errorframe != NULL ) {
[5216]260    if ( roar_err_parsemsg(mes, *data, errorframe) != 0 ) {
[5146]261     roar_err_init(errorframe);
262    }
263   }
264   ROAR_DBG("roar_vrecv_message2(*) = 0");
[3882]265   return 0;
266  }
267
268  return -1;
269 } else {
270  if ( data == NULL ) {
[4873]271   roar_err_set(ROAR_ERROR_MSGSIZE);
[3882]272   return -1;
273  }
274
[5295]275  if ( (*data = roar_mm_malloc(mes->datalen)) == NULL ) {
[4873]276   roar_err_set(ROAR_ERROR_NOMEM);
[3882]277   return -1;
278  }
279
280  if ( mes->datalen == 0 ) {
[4873]281   roar_err_clear();
[5146]282   if ( mes->cmd == ROAR_CMD_ERROR && errorframe != NULL ) {
283    roar_err_init(errorframe);
284   }
[3882]285   return 0;
286  }
287
[5270]288  if ( roar_vio_read(vio, *data, mes->datalen) == (ssize_t)mes->datalen ) {
[5146]289   ROAR_DBG("roar_vrecv_message2(*): Got data!");
290
[4873]291   roar_err_clear();
[5146]292   if ( mes->cmd == ROAR_CMD_ERROR && errorframe != NULL ) {
[5216]293    if ( roar_err_parsemsg(mes, *data, errorframe) != 0 ) {
[5146]294     roar_err_init(errorframe);
295    }
296   }
297   ROAR_DBG("roar_vrecv_message2(*) = 0");
[3882]298   return 0;
299  }
300
[4951]301  // error is still set (by roar_vio_read()).
[3882]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) {
[5146]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) {
[3882]315 struct roar_vio_calls * vio;
316
317 if ( (vio = roar_get_connection_vio2(con)) == NULL )
318  return -1;
319
[5465]320 if ( mes->version == _ROAR_MESSAGE_VERSION )
321  mes->version = con->version;
322
[5467]323 if ( mes->version == 2 ) {
324  mes->seq = roar_message_genseq(con, -1);
325 }
326
[5146]327 return roar_vreq2(vio, mes, data, errorframe);
[3882]328}
329
330int roar_vreq         (struct roar_vio_calls * vio, struct roar_message * mes, char ** data) {
[5146]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) {
[5030]336 if ( roar_vsend_message(vio, mes, data != NULL ? *data : NULL) != 0 )
[3882]337  return -1;
338
[4439]339 if ( data != NULL )
[5295]340  roar_mm_free(*data);
[3882]341
342 roar_vio_sync(vio); // we need to do this becasue of ssl/compressed links
343
[5146]344 return roar_vrecv_message2(vio, mes, data, errorframe);
[3882]345}
346
347//ll
Note: See TracBrowser for help on using the repository browser.