[3882] | 1 | //proto.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2010 |
---|
| 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 (1 /* version */ + 1 /* cmd */ + 2 /* stream */ + 4 /* pos */ + 2 /* datalen */) |
---|
| 39 | int roar_send_message (struct roar_connection * con, struct roar_message * mes, char * data) { |
---|
| 40 | struct roar_vio_calls * vio; |
---|
| 41 | |
---|
| 42 | if ( (vio = roar_get_connection_vio2(con)) == NULL ) |
---|
| 43 | return -1; |
---|
| 44 | |
---|
| 45 | return roar_vsend_message(vio, mes, data); |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | int roar_vsend_message(struct roar_vio_calls * vio, struct roar_message * mes, char * data) { |
---|
| 49 | char buf[_ROAR_MESS_BUF_LEN]; |
---|
| 50 | |
---|
| 51 | roar_errno = ROAR_ERROR_UNKNOWN; |
---|
| 52 | |
---|
| 53 | ROAR_DBG("roar_send_message(*): try to send an request..."); |
---|
| 54 | |
---|
| 55 | buf[0] = _ROAR_MESSAGE_VERSION; |
---|
| 56 | buf[1] = (unsigned char) mes->cmd; |
---|
| 57 | *(uint16_t*)(buf+2) = ROAR_HOST2NET16(mes->stream); |
---|
| 58 | *(uint32_t*)(buf+4) = ROAR_HOST2NET32(mes->pos); |
---|
| 59 | *(uint16_t*)(buf+8) = ROAR_HOST2NET16(mes->datalen); |
---|
| 60 | |
---|
| 61 | if ( roar_vio_write(vio, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) { |
---|
| 62 | roar_errno = ROAR_ERROR_PIPE; |
---|
| 63 | return -1; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | if ( mes->datalen != 0 ) { |
---|
| 67 | if ( roar_vio_write(vio, data == NULL ? mes->data : data, mes->datalen) != mes->datalen ) { |
---|
| 68 | roar_errno = ROAR_ERROR_PIPE; |
---|
| 69 | return -1; |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | roar_errno = ROAR_ERROR_NONE; |
---|
| 74 | |
---|
| 75 | ROAR_DBG("roar_send_message(*) = 0"); |
---|
| 76 | return 0; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | int roar_recv_message (struct roar_connection * con, struct roar_message * mes, char ** data) { |
---|
| 80 | struct roar_vio_calls * vio; |
---|
| 81 | |
---|
| 82 | if ( (vio = roar_get_connection_vio2(con)) == NULL ) |
---|
| 83 | return -1; |
---|
| 84 | |
---|
| 85 | return roar_vrecv_message(vio, mes, data); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | int roar_vrecv_message(struct roar_vio_calls * vio, struct roar_message * mes, char ** data) { |
---|
| 89 | char buf[_ROAR_MESS_BUF_LEN]; |
---|
| 90 | |
---|
| 91 | roar_errno = ROAR_ERROR_UNKNOWN; |
---|
| 92 | |
---|
| 93 | ROAR_DBG("roar_recv_message(*): try to get a response form the server..."); |
---|
| 94 | |
---|
| 95 | if ( mes == NULL ) |
---|
| 96 | return -1; |
---|
| 97 | |
---|
| 98 | if ( data != NULL ) |
---|
| 99 | *data = NULL; |
---|
| 100 | |
---|
| 101 | memset(mes, 0, sizeof(struct roar_message)); |
---|
| 102 | |
---|
| 103 | if ( roar_vio_read(vio, buf, _ROAR_MESS_BUF_LEN) != _ROAR_MESS_BUF_LEN ) { |
---|
| 104 | roar_errno = ROAR_ERROR_PROTO; |
---|
| 105 | return -1; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | ROAR_DBG("roar_recv_message(*): Got a header"); |
---|
| 109 | |
---|
| 110 | if ( buf[0] != _ROAR_MESSAGE_VERSION ) { |
---|
| 111 | roar_errno = ROAR_ERROR_PROTO; |
---|
| 112 | return -1; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | mes->cmd = (unsigned char)buf[1]; |
---|
| 116 | mes->stream = ROAR_NET2HOST16(*(uint16_t*)(buf+2)); |
---|
| 117 | mes->pos = ROAR_NET2HOST32(*(uint32_t*)(buf+4)); |
---|
| 118 | mes->datalen = ROAR_NET2HOST16(*(uint16_t*)(buf+8)); |
---|
| 119 | |
---|
| 120 | if ( (int16_t)mes->stream == -1 ) |
---|
| 121 | mes->stream = -1; |
---|
| 122 | |
---|
| 123 | ROAR_DBG("roar_recv_message(*): command=%i(%s)", mes->cmd, |
---|
| 124 | mes->cmd == ROAR_CMD_OK ? "OK" : (mes->cmd == ROAR_CMD_ERROR ? "ERROR" : "UNKNOWN")); |
---|
| 125 | |
---|
| 126 | if ( mes->datalen == 0 ) { |
---|
| 127 | ROAR_DBG("roar_recv_message(*): no data in this pkg"); |
---|
| 128 | ROAR_DBG("roar_recv_message(*) = 0"); |
---|
| 129 | roar_errno = ROAR_ERROR_NONE; |
---|
| 130 | return 0; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | if ( mes->datalen <= LIBROAR_BUFFER_MSGDATA ) { |
---|
| 134 | if ( roar_vio_read(vio, mes->data, mes->datalen) == mes->datalen ) { |
---|
| 135 | ROAR_DBG("roar_recv_message(*): Got data!"); |
---|
| 136 | ROAR_DBG("roar_recv_message(*) = 0"); |
---|
| 137 | roar_errno = ROAR_ERROR_NONE; |
---|
| 138 | return 0; |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | roar_errno = ROAR_ERROR_PIPE; |
---|
| 142 | return -1; |
---|
| 143 | } else { |
---|
| 144 | if ( data == NULL ) { |
---|
| 145 | roar_errno = ROAR_ERROR_MSGSIZE; |
---|
| 146 | return -1; |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | if ( (*data = malloc(mes->datalen)) == NULL ) { |
---|
| 150 | roar_errno = ROAR_ERROR_NOMEM; |
---|
| 151 | return -1; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | if ( mes->datalen == 0 ) { |
---|
| 155 | roar_errno = ROAR_ERROR_NONE; |
---|
| 156 | return 0; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | if ( roar_vio_read(vio, *data, mes->datalen) == mes->datalen ) { |
---|
| 160 | ROAR_DBG("roar_recv_message(*): Got data!"); |
---|
| 161 | ROAR_DBG("roar_recv_message(*) = 0"); |
---|
| 162 | roar_errno = ROAR_ERROR_NONE; |
---|
| 163 | return 0; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | roar_errno = ROAR_ERROR_PIPE; |
---|
| 167 | return -1; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | // what happened here? |
---|
| 171 | return -1; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | int roar_req (struct roar_connection * con, struct roar_message * mes, char ** data) { |
---|
| 175 | struct roar_vio_calls * vio; |
---|
| 176 | |
---|
| 177 | if ( (vio = roar_get_connection_vio2(con)) == NULL ) |
---|
| 178 | return -1; |
---|
| 179 | |
---|
| 180 | return roar_vreq(vio, mes, data); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | int roar_vreq (struct roar_vio_calls * vio, struct roar_message * mes, char ** data) { |
---|
| 184 | if ( roar_vsend_message(vio, mes, data ? *data : NULL) != 0 ) |
---|
| 185 | return -1; |
---|
| 186 | |
---|
| 187 | if ( data ) |
---|
| 188 | free(*data); |
---|
| 189 | |
---|
| 190 | roar_vio_sync(vio); // we need to do this becasue of ssl/compressed links |
---|
| 191 | |
---|
| 192 | return roar_vrecv_message(vio, mes, data); |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | //ll |
---|