source: roaraudio/libroar/vio.c @ 1760:da54b58b39ba

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

socket api differs on win32

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