source: roaraudio/libroar/vio.c @ 2841:29c0a0293758

Last change on this file since 2841:29c0a0293758 was 2841:29c0a0293758, checked in by phi, 15 years ago

added roar_vio_simple_new_stream_obj()

File size: 12.4 KB
Line 
1//vio.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008, 2009
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, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 *  NOTE for everyone want's to change something and send patches:
24 *  read README and HACKING! There a addition information on
25 *  the license of this document you need to read before you send
26 *  any patches.
27 *
28 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
29 *  or libpulse*:
30 *  The libs libroaresd, libroararts and libroarpulse link this lib
31 *  and are therefore GPL. Because of this it may be illigal to use
32 *  them with any software that uses libesd, libartsc or libpulse*.
33 */
34
35#include "libroar.h"
36
37#ifdef ROAR_HAVE_IO_POSIX
38#define _CAN_OPERATE
39#endif
40
41int roar_vio_init_calls (struct roar_vio_calls * calls) {
42#ifdef _CAN_OPERATE
43 if ( calls == NULL )
44  return -1;
45
46 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
47
48/*
49 calls->read  = (ssize_t (*)(int fd, void *buf, size_t count,      void * inst))read;
50 calls->write = (ssize_t (*)(int fd, void *buf, size_t count,      void * inst))write;
51 calls->lseek = (off_t   (*)(int fildes, off_t offset, int whence, void * inst))lseek;
52*/
53
54 calls->read     = roar_vio_basic_read;
55 calls->write    = roar_vio_basic_write;
56 calls->lseek    = roar_vio_basic_lseek;
57 calls->nonblock = roar_vio_basic_nonblock;
58 calls->sync     = roar_vio_basic_sync;
59 calls->ctl      = roar_vio_basic_ctl;
60 calls->close    = roar_vio_basic_close;
61
62 return 0;
63#else
64 return -1;
65#endif
66}
67
68int roar_vio_set_inst (struct roar_vio_calls * vio, void * inst) {
69 if ( vio == NULL )
70  return -1;
71
72 vio->inst = inst;
73
74 return 0;
75}
76
77int roar_vio_set_fh   (struct roar_vio_calls * vio, int fh) {
78 return roar_vio_set_inst(vio, (void*)(ROAR_INSTINT)(fh + 1));
79}
80
81int roar_vio_get_fh   (struct roar_vio_calls * vio) {
82 if ( vio == NULL )
83  return -1;
84
85 return ((int)(ROAR_INSTINT)vio->inst) - 1;
86}
87
88
89ssize_t roar_vio_read (struct roar_vio_calls * vio, void *buf, size_t count) {
90 ROAR_DBG("roar_vio_read(vio=%p, buf=%p, count=%u) = ?", vio, buf, (unsigned int)count);
91
92 if ( vio == NULL )
93  return -1;
94
95 if ( vio->read == NULL )
96  return -1;
97
98 return vio->read(vio, buf, count);
99}
100
101ssize_t roar_vio_write(struct roar_vio_calls * vio, void *buf, size_t count) {
102 if ( vio == NULL )
103  return -1;
104
105 if ( vio->write == NULL )
106  return -1;
107
108 return vio->write(vio, buf, count);
109}
110
111off_t   roar_vio_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
112 if ( vio == NULL )
113  return -1;
114
115 if ( vio->lseek == NULL )
116  return -1;
117
118 return vio->lseek(vio, offset, whence);
119}
120
121int     roar_vio_nonblock(struct roar_vio_calls * vio, int state) {
122 if ( vio == NULL )
123  return -1;
124
125 if ( vio->nonblock == NULL )
126  return -1;
127
128 return vio->nonblock(vio, state);
129}
130
131int     roar_vio_sync    (struct roar_vio_calls * vio) {
132 if ( vio == NULL )
133  return -1;
134
135 if ( vio->sync == NULL )
136  return -1;
137
138 return vio->sync(vio);
139}
140
141int     roar_vio_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
142 if ( vio == NULL )
143  return -1;
144
145 ROAR_DBG("roar_vio_ctl(vio=%p, cmd=0x%.8x, data=%p): vio->ctl=%p", vio, cmd, data, vio->ctl);
146
147 if ( vio->ctl == NULL )
148  return -1;
149
150 return vio->ctl(vio, cmd, data);
151}
152
153int     roar_vio_close    (struct roar_vio_calls * vio) {
154 if ( vio == NULL )
155  return -1;
156
157 if ( vio->close == NULL )
158  return -1;
159
160 return vio->close(vio);
161}
162
163// converters:
164int     roar_vio_open_file     (struct roar_vio_calls * calls, char * filename, int flags, mode_t mode) {
165#ifdef _CAN_OPERATE
166 int fh;
167
168 if ( calls == NULL || filename == NULL )
169  return -1;
170
171#ifdef ROAR_TARGET_WIN32
172 flags |= O_BINARY;
173#endif
174
175 if ( (fh = open(filename, flags, mode)) == -1 )
176  return -1;
177
178 if ( roar_vio_open_fh(calls, fh) == -1 ) {
179  close(fh);
180  return -1;
181 }
182
183 return 0;
184#else
185 return -1;
186#endif
187}
188
189int     roar_vio_open_fh       (struct roar_vio_calls * calls, int fh) {
190 if ( calls == NULL )
191  return -1;
192
193 if ( roar_vio_init_calls(calls) == -1 )
194  return -1;
195
196 return roar_vio_set_fh(calls, fh);
197}
198
199int     roar_vio_open_fh_socket(struct roar_vio_calls * calls, int fh) {
200 if ( calls == NULL )
201  return -1;
202
203 if ( roar_vio_open_fh(calls, fh) == -1 )
204  return -1;
205
206#ifdef ROAR_TARGET_WIN32
207 calls->read     = roar_vio_winsock_read;
208 calls->write    = roar_vio_winsock_write;
209 calls->nonblock = roar_vio_winsock_nonblock;
210 calls->sync     = roar_vio_winsock_sync;
211 calls->ctl      = roar_vio_winsock_ctl;
212 calls->close    = roar_vio_winsock_close;
213#else
214 calls->sync     = roar_vio_null_sync;
215#endif
216
217 return 0;
218}
219
220int     roar_vio_open_socket   (struct roar_vio_calls * calls, char * host, int port) {
221 int fh;
222
223 if ( calls == NULL )
224  return -1;
225
226 if ( (fh = roar_socket_connect(host, port)) == -1 )
227  return -1;
228
229 return roar_vio_open_fh_socket(calls, fh);
230}
231
232int     roar_vio_open_socket_listen(struct roar_vio_calls * calls, int type, char * host, int port) {
233 int fh;
234
235 if ( calls == NULL )
236  return -1;
237
238 if ( (fh = roar_socket_listen(type, host, port)) == -1 )
239  return -1;
240
241 return roar_vio_open_fh_socket(calls, fh);
242}
243
244int     roar_vio_simple_stream (struct roar_vio_calls * calls, int rate, int channels, int bits, int codec,
245                                                               char * server, int dir, char * name) {
246 int fh;
247
248 if ( calls == NULL )
249  return -1;
250
251 if ( (fh = roar_simple_stream(rate, channels, bits, codec, server, dir, name)) == -1 )
252  return -1;
253
254 return roar_vio_open_fh_socket(calls, fh);
255}
256
257int     roar_vio_simple_new_stream_obj (struct roar_vio_calls * calls,
258                                        struct roar_connection * con,
259                                        struct roar_stream * s,
260                                        int rate, int channels, int bits, int codec, int dir) {
261 int fh;
262
263 if ( calls == NULL )
264  return -1;
265
266 if ( (fh = roar_simple_new_stream_obj(con, s, rate, channels, bits, codec, dir)) == -1 )
267  return -1;
268
269 return roar_vio_open_fh_socket(calls, fh);
270}
271
272// VIOs:
273
274// basic
275ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
276#ifdef _CAN_OPERATE
277 return read(roar_vio_get_fh(vio), buf, count);
278#else
279 return -1;
280#endif
281}
282
283ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
284#ifdef _CAN_OPERATE
285 return write(roar_vio_get_fh(vio), buf, count);
286#else
287 return -1;
288#endif
289}
290
291off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
292#ifdef _CAN_OPERATE
293 return lseek(roar_vio_get_fh(vio), offset, whence);
294#else
295 return -1;
296#endif
297}
298
299int     roar_vio_basic_nonblock(struct roar_vio_calls * vio, int state) {
300 if ( roar_socket_nonblock(roar_vio_get_fh(vio), state) == -1 )
301  return -1;
302
303 if ( state == ROAR_SOCKET_NONBLOCK )
304  return 0;
305
306 roar_vio_sync(vio);
307
308 return 0;
309}
310
311int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
312#ifdef ROAR_FDATASYNC
313 return ROAR_FDATASYNC(roar_vio_get_fh(vio));
314#else
315 return 0;
316#endif
317}
318
319int     roar_vio_basic_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
320
321 if ( vio == NULL || cmd == -1 )
322  return -1;
323
324 ROAR_DBG("roar_vio_basic_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
325
326 switch (cmd) {
327  case ROAR_VIO_CTL_GET_FH:
328  case ROAR_VIO_CTL_GET_READ_FH:
329  case ROAR_VIO_CTL_GET_WRITE_FH:
330    ROAR_DBG("roar_vio_basic_ctl(vio=%p, cmd=ROAR_VIO_CTL_GET_*FH(0x%.8x), data=%p) = 0 // fh=%i", vio, cmd, data, roar_vio_get_fh(vio));
331    *(int*)data = roar_vio_get_fh(vio);
332    return 0;
333   break;
334  case ROAR_VIO_CTL_SET_NOSYNC:
335    vio->sync = NULL;
336    return 0;
337   break;
338 }
339
340 return -1;
341}
342
343int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
344#ifdef _CAN_OPERATE
345 if ( roar_vio_get_fh(vio) != -1 )
346  return close(roar_vio_get_fh(vio));
347
348 return 0;
349#else
350 return -1;
351#endif
352}
353
354#ifdef ROAR_TARGET_WIN32
355ssize_t roar_vio_winsock_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
356 return recv(roar_vio_get_fh(vio), buf, count, 0);
357}
358
359ssize_t roar_vio_winsock_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
360 return send(roar_vio_get_fh(vio), buf, count, 0);
361}
362
363int     roar_vio_winsock_nonblock(struct roar_vio_calls * vio, int state) {
364 return -1;
365}
366int     roar_vio_winsock_sync    (struct roar_vio_calls * vio) {
367 return 0;
368}
369int     roar_vio_winsock_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
370 return -1;
371}
372int     roar_vio_winsock_close   (struct roar_vio_calls * vio) {
373
374 closesocket(roar_vio_get_fh(vio));
375
376 return 0;
377}
378#endif
379
380
381// null
382ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
383 if ( vio == NULL || buf == NULL )
384  return -1;
385
386 return 0;
387}
388
389int     roar_vio_null_sync    (struct roar_vio_calls * vio) {
390 return 0;
391}
392
393// pass
394int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
395 if ( calls == NULL )
396  return -1;
397
398 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
399
400 calls->read     = roar_vio_pass_read;
401 calls->write    = roar_vio_pass_write;
402 calls->lseek    = roar_vio_pass_lseek;
403 calls->nonblock = roar_vio_pass_nonblock;
404 calls->sync     = roar_vio_pass_sync;
405 calls->ctl      = roar_vio_pass_ctl;
406 calls->close    = roar_vio_pass_close;
407
408 calls->inst     = dst;
409
410 return 0;
411}
412
413ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
414 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
415}
416
417ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
418 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
419}
420
421off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
422 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
423}
424
425int     roar_vio_pass_nonblock(struct roar_vio_calls * vio, int state) {
426 return roar_vio_nonblock((struct roar_vio_calls *) vio->inst, state);
427}
428
429int     roar_vio_pass_sync    (struct roar_vio_calls * vio) {
430 return roar_vio_sync((struct roar_vio_calls *) vio->inst);
431}
432
433int     roar_vio_pass_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
434 if (vio == NULL || cmd == -1)
435  return -1;
436
437 ROAR_DBG("roar_vio_pass_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
438
439 switch (cmd) {
440  case ROAR_VIO_CTL_GET_NEXT:
441    *(struct roar_vio_calls **)data = vio->inst;
442    return 0;
443   break;
444  case ROAR_VIO_CTL_SET_NEXT:
445    vio->inst = *(struct roar_vio_calls **)data;
446    return 0;
447   break;
448 }
449
450 return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data);
451}
452
453int     roar_vio_pass_close   (struct roar_vio_calls * vio) {
454 return roar_vio_close((struct roar_vio_calls *) vio->inst);
455}
456
457
458// re
459int     roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
460 if ( roar_vio_open_pass(calls, dst) == -1 )
461  return -1;
462
463 calls->read  = roar_vio_re_read;
464 calls->write = roar_vio_re_write;
465 calls->lseek = roar_vio_re_lseek;
466
467 return 0;
468}
469ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
470  size_t len =  0;
471 ssize_t r   = -1;
472
473 if ( vio == NULL )
474  return -1;
475
476 if ( vio->inst == NULL )
477  return -1;
478
479 errno = 0;
480
481 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
482  len   += r;
483  buf   += r;
484  count -= r;
485  if ( count == 0 )
486   break;
487 }
488
489 if ( len == 0 && r == -1 )
490  return -1;
491
492 return len;
493}
494
495ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
496  size_t len =  0;
497 ssize_t r   = -1;
498
499 if ( vio == NULL )
500  return -1;
501
502 if ( vio->inst == NULL )
503  return -1;
504
505 errno = 0;
506
507 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
508  len   += r;
509  buf   += r;
510  count -= r;
511  if ( count == 0 )
512   break;
513 }
514
515 if ( len == 0 && r == -1 )
516  return -1;
517
518 return len;
519}
520
521// TODO: we should do a some more intelgent thing here.
522off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
523 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
524}
525
526//ll
Note: See TracBrowser for help on using the repository browser.