source: roaraudio/libroar/vio.c @ 1666:ae1d6f0de812

Last change on this file since 1666:ae1d6f0de812 was 1666:ae1d6f0de812, checked in by phi, 15 years ago

sockets do not need any fdatasync() call on posix systems

File size: 14.5 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
163int     roar_vio_putc    (struct roar_vio_calls * vio, char c) {
164 return roar_vio_write(vio, &c, 1);
165}
166
167int     roar_vio_getc    (struct roar_vio_calls * vio) {
168 unsigned char c;
169
170 if ( roar_vio_read(vio, &c, 1) != 1 )
171  return EOF;
172
173 return c;
174}
175
176int     roar_vio_printf(struct roar_vio_calls * vio, const char *format, ...) {
177 va_list ap;
178 int ret;
179 char buf[8192];
180
181 va_start(ap, format);
182 ret = vsnprintf(buf, 8192, format, ap);
183 va_end(ap);
184
185 return roar_vio_write(vio, buf, ret);
186}
187
188// converters:
189int     roar_vio_open_file     (struct roar_vio_calls * calls, char * filename, int flags, mode_t mode) {
190#ifdef _CAN_OPERATE
191 int fh;
192
193 if ( calls == NULL || filename == NULL )
194  return -1;
195
196 if ( (fh = open(filename, flags, mode)) == -1 )
197  return -1;
198
199 if ( roar_vio_open_fh(calls, fh) == -1 ) {
200  close(fh);
201  return -1;
202 }
203
204 return 0;
205#else
206 return -1;
207#endif
208}
209
210int     roar_vio_open_fh       (struct roar_vio_calls * calls, int fh) {
211 if ( calls == NULL )
212  return -1;
213
214 if ( roar_vio_init_calls(calls) == -1 )
215  return -1;
216
217 return roar_vio_set_fh(calls, fh);
218}
219
220int     roar_vio_open_fh_socket(struct roar_vio_calls * calls, int fh) {
221 if ( calls == NULL )
222  return -1;
223
224 if ( roar_vio_open_fh(calls, fh) == -1 )
225  return -1;
226
227 calls->sync = roar_vio_null_sync;
228
229 return 0;
230}
231
232int     roar_vio_open_socket   (struct roar_vio_calls * calls, char * host, int port) {
233 int fh;
234
235 if ( calls == NULL )
236  return -1;
237
238 if ( (fh = roar_socket_connect(host, port)) == -1 )
239  return -1;
240
241 return roar_vio_open_fh_socket(calls, fh);
242}
243
244int     roar_vio_open_socket_listen(struct roar_vio_calls * calls, int type, char * host, int port) {
245 int fh;
246
247 if ( calls == NULL )
248  return -1;
249
250 if ( (fh = roar_socket_listen(type, host, port)) == -1 )
251  return -1;
252
253 return roar_vio_open_fh_socket(calls, fh);
254}
255
256int     roar_vio_simple_stream (struct roar_vio_calls * calls, int rate, int channels, int bits, int codec,
257                                                               char * server, int dir, char * name) {
258 int fh;
259
260 if ( calls == NULL )
261  return -1;
262
263 if ( (fh = roar_simple_stream(rate, channels, bits, codec, server, dir, name)) == -1 )
264  return -1;
265
266 return roar_vio_open_fh_socket(calls, fh);
267}
268
269
270int     roar_vio_open_stdio    (struct roar_vio_calls * calls, FILE * dst) {
271#ifndef ROAR_WITHOUT_VIO_STDIO
272 if ( calls == NULL || dst == NULL )
273  return -1;
274
275 memset(calls, 0, sizeof(struct roar_vio_calls));
276
277 calls->read  = roar_vio_stdio_read;
278 calls->write = roar_vio_stdio_write;
279 calls->lseek = roar_vio_stdio_lseek;
280 calls->sync  = roar_vio_stdio_sync;
281 calls->close = roar_vio_stdio_close;
282
283 calls->inst  = dst;
284
285 return 0;
286#else
287 return -1;
288#endif
289}
290
291FILE *  roar_vio_to_stdio      (struct roar_vio_calls * calls, int flags) {
292#ifdef ROAR_HAVE_FOPENCOOKIE
293 cookie_io_functions_t foc_funcs;
294#endif
295
296 if ( calls == NULL )
297  return NULL;
298
299#if defined(ROAR_HAVE_FOPENCOOKIE)
300 memset(&foc_funcs, 0, sizeof(cookie_io_functions_t));
301
302 foc_funcs.close = roar_vio_to_stdio_close;
303 foc_funcs.read  = roar_vio_to_stdio_read;
304 foc_funcs.write = roar_vio_to_stdio_write;
305
306 return fopencookie((void*) calls, "rw", foc_funcs);
307#elif defined(ROAR_HAVE_FUNOPEN)
308 return funopen((void*) calls, roar_vio_to_stdio_read,  roar_vio_to_stdio_write,
309                               roar_vio_to_stdio_lseek, roar_vio_to_stdio_close);
310#else
311 return NULL;
312#endif
313}
314
315#if defined(ROAR_HAVE_FOPENCOOKIE) || defined(ROAR_HAVE_FUNOPEN)
316int roar_vio_to_stdio_close (void *__cookie) {
317 return roar_vio_close((struct roar_vio_calls *) __cookie);
318}
319
320#if defined(ROAR_HAVE_FOPENCOOKIE)
321__ssize_t roar_vio_to_stdio_read (void *__cookie, char *__buf, size_t __nbytes) {
322#elif defined(ROAR_HAVE_FUNOPEN)
323int roar_vio_to_stdio_read(void *__cookie, char *__buf, int __nbytes) {
324#endif
325 return roar_vio_read((struct roar_vio_calls *) __cookie, __buf, __nbytes);
326}
327
328#if defined(ROAR_HAVE_FOPENCOOKIE)
329__ssize_t roar_vio_to_stdio_write (void *__cookie, __const char *__buf, size_t __n) {
330#elif defined(ROAR_HAVE_FUNOPEN)
331int roar_vio_to_stdio_write(void *__cookie, const char *__buf, int __n) {
332#endif
333 return roar_vio_write((struct roar_vio_calls *) __cookie, (char *) __buf, __n);
334}
335
336#if defined(ROAR_HAVE_FOPENCOOKIE)
337int roar_vio_to_stdio_lseek (void *__cookie, _IO_off64_t *__pos, int __w);
338#elif defined(ROAR_HAVE_FUNOPEN)
339fpos_t roar_vio_to_stdio_lseek(void *__cookie, fpos_t __pos, int __w) {
340 return roar_vio_lseek((struct roar_vio_calls *) __cookie, __pos, __w);
341}
342#endif
343#endif
344
345// VIOs:
346
347// basic
348ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
349#ifdef _CAN_OPERATE
350 return read(roar_vio_get_fh(vio), buf, count);
351#else
352 return -1;
353#endif
354}
355
356ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
357#ifdef _CAN_OPERATE
358 return write(roar_vio_get_fh(vio), buf, count);
359#else
360 return -1;
361#endif
362}
363
364off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
365#ifdef _CAN_OPERATE
366 return lseek(roar_vio_get_fh(vio), offset, whence);
367#else
368 return -1;
369#endif
370}
371
372int     roar_vio_basic_nonblock(struct roar_vio_calls * vio, int state) {
373 if ( roar_socket_nonblock(roar_vio_get_fh(vio), state) == -1 )
374  return -1;
375
376 if ( state == ROAR_SOCKET_NONBLOCK )
377  return 0;
378
379 roar_vio_sync(vio);
380
381 return 0;
382}
383
384int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
385#ifdef ROAR_FDATASYNC
386 return ROAR_FDATASYNC(roar_vio_get_fh(vio));
387#else
388 return 0;
389#endif
390}
391
392int     roar_vio_basic_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
393
394 if ( vio == NULL || cmd == -1 )
395  return -1;
396
397 ROAR_DBG("roar_vio_basic_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
398
399 switch (cmd) {
400  case ROAR_VIO_CTL_GET_FH:
401  case ROAR_VIO_CTL_GET_READ_FH:
402  case ROAR_VIO_CTL_GET_WRITE_FH:
403    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));
404    *(int*)data = roar_vio_get_fh(vio);
405    return 0;
406   break;
407 }
408
409 return -1;
410}
411
412int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
413#ifdef _CAN_OPERATE
414 if ( roar_vio_get_fh(vio) != -1 )
415  return close(roar_vio_get_fh(vio));
416
417 return 0;
418#else
419 return -1;
420#endif
421}
422
423// null
424ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
425 if ( vio == NULL || buf == NULL )
426  return -1;
427
428 return 0;
429}
430
431int     roar_vio_null_sync    (struct roar_vio_calls * vio) {
432 return 0;
433}
434
435// pass
436int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
437 if ( calls == NULL )
438  return -1;
439
440 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
441
442 calls->read     = roar_vio_pass_read;
443 calls->write    = roar_vio_pass_write;
444 calls->lseek    = roar_vio_pass_lseek;
445 calls->nonblock = roar_vio_pass_nonblock;
446 calls->sync     = roar_vio_pass_sync;
447 calls->ctl      = roar_vio_pass_ctl;
448 calls->close    = roar_vio_pass_close;
449
450 calls->inst     = dst;
451
452 return 0;
453}
454
455ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
456 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
457}
458
459ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
460 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
461}
462
463off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
464 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
465}
466
467int     roar_vio_pass_nonblock(struct roar_vio_calls * vio, int state) {
468 return roar_vio_nonblock((struct roar_vio_calls *) vio->inst, state);
469}
470
471int     roar_vio_pass_sync    (struct roar_vio_calls * vio) {
472 return roar_vio_sync((struct roar_vio_calls *) vio->inst);
473}
474
475int     roar_vio_pass_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
476 if (vio == NULL || cmd == -1)
477  return -1;
478
479 ROAR_DBG("roar_vio_pass_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
480
481 switch (cmd) {
482  case ROAR_VIO_CTL_GET_NEXT:
483    *(struct roar_vio_calls **)data = vio->inst;
484    return 0;
485   break;
486  case ROAR_VIO_CTL_SET_NEXT:
487    vio->inst = *(struct roar_vio_calls **)data;
488    return 0;
489   break;
490 }
491
492 return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data);
493}
494
495int     roar_vio_pass_close   (struct roar_vio_calls * vio) {
496 return roar_vio_close((struct roar_vio_calls *) vio->inst);
497}
498
499
500// re
501int     roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
502 if ( roar_vio_open_pass(calls, dst) == -1 )
503  return -1;
504
505 calls->read  = roar_vio_re_read;
506 calls->write = roar_vio_re_write;
507 calls->lseek = roar_vio_re_lseek;
508
509 return 0;
510}
511ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
512  size_t len =  0;
513 ssize_t r   = -1;
514
515 if ( vio == NULL )
516  return -1;
517
518 if ( vio->inst == NULL )
519  return -1;
520
521 errno = 0;
522
523 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
524  len   += r;
525  buf   += r;
526  count -= r;
527  if ( count == 0 )
528   break;
529 }
530
531 if ( len == 0 && r == -1 )
532  return -1;
533
534 return len;
535}
536
537ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
538  size_t len =  0;
539 ssize_t r   = -1;
540
541 if ( vio == NULL )
542  return -1;
543
544 if ( vio->inst == NULL )
545  return -1;
546
547 errno = 0;
548
549 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
550  len   += r;
551  buf   += r;
552  count -= r;
553  if ( count == 0 )
554   break;
555 }
556
557 if ( len == 0 && r == -1 )
558  return -1;
559
560 return len;
561}
562
563// TODO: we should do a some more intelgent thing here.
564off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
565 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
566}
567
568// stdio:
569#ifndef ROAR_WITHOUT_VIO_STDIO
570ssize_t roar_vio_stdio_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
571 return fread(buf, 1, count, (FILE*)(vio->inst));
572}
573
574ssize_t roar_vio_stdio_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
575 return fwrite(buf, 1, count, (FILE*)(vio->inst));
576}
577
578off_t   roar_vio_stdio_lseek   (struct roar_vio_calls * vio, off_t offset, int whence) {
579 if ( fseek((FILE*)(vio->inst), offset, whence) == -1 )
580  return -1;
581
582 return ftell((FILE*)(vio->inst));
583}
584
585int     roar_vio_stdio_sync    (struct roar_vio_calls * vio) {
586 return fflush((FILE*)(vio->inst));
587}
588
589int     roar_vio_stdio_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
590
591 if ( vio == NULL || cmd == -1 )
592  return -1;
593
594 switch (cmd) {
595  case ROAR_VIO_CTL_GET_FH:
596  case ROAR_VIO_CTL_GET_READ_FH:
597  case ROAR_VIO_CTL_GET_WRITE_FH:
598   *(int*)data = fileno((FILE*)(vio->inst));
599    return 0;
600   break;
601 }
602
603 return -1;
604}
605
606int     roar_vio_stdio_close   (struct roar_vio_calls * vio) {
607 return fclose((FILE*)(vio->inst));
608}
609#endif
610
611//ll
Note: See TracBrowser for help on using the repository browser.