source: roaraudio/libroar/vio.c @ 1319:acdd2e21951b

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

big steps in direction to vio roar_connection object, roar_*_message() and roar_req() now use temp. VIO objects

File size: 12.2 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
37int roar_vio_init_calls (struct roar_vio_calls * calls) {
38 if ( calls == NULL )
39  return -1;
40
41 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
42
43/*
44 calls->read  = (ssize_t (*)(int fd, void *buf, size_t count,      void * inst))read;
45 calls->write = (ssize_t (*)(int fd, void *buf, size_t count,      void * inst))write;
46 calls->lseek = (off_t   (*)(int fildes, off_t offset, int whence, void * inst))lseek;
47*/
48
49 calls->read     = roar_vio_basic_read;
50 calls->write    = roar_vio_basic_write;
51 calls->lseek    = roar_vio_basic_lseek;
52 calls->nonblock = roar_vio_basic_nonblock;
53 calls->sync     = roar_vio_basic_sync;
54 calls->close    = roar_vio_basic_close;
55
56 return 0;
57}
58
59int roar_vio_set_inst (struct roar_vio_calls * vio, void * inst) {
60 if ( vio == NULL )
61  return -1;
62
63 vio->inst = inst;
64
65 return 0;
66}
67
68int roar_vio_set_fh   (struct roar_vio_calls * vio, int fh) {
69 return roar_vio_set_inst(vio, (void*)(ROAR_INSTINT)(fh + 1));
70}
71
72int roar_vio_get_fh   (struct roar_vio_calls * vio) {
73 if ( vio == NULL )
74  return -1;
75
76 return ((int)(ROAR_INSTINT)vio->inst) - 1;
77}
78
79
80ssize_t roar_vio_read (struct roar_vio_calls * vio, void *buf, size_t count) {
81 ROAR_DBG("roar_vio_read(vio=%p, buf=%p, count=%u) = ?", vio, buf, (unsigned int)count);
82
83 if ( vio == NULL )
84  return -1;
85
86 if ( vio->read == NULL )
87  return -1;
88
89 return vio->read(vio, buf, count);
90}
91
92ssize_t roar_vio_write(struct roar_vio_calls * vio, void *buf, size_t count) {
93 if ( vio == NULL )
94  return -1;
95
96 if ( vio->write == NULL )
97  return -1;
98
99 return vio->write(vio, buf, count);
100}
101
102off_t   roar_vio_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
103 if ( vio == NULL )
104  return -1;
105
106 if ( vio->lseek == NULL )
107  return -1;
108
109 return vio->lseek(vio, offset, whence);
110}
111
112int     roar_vio_nonblock(struct roar_vio_calls * vio, int state) {
113 if ( vio == NULL )
114  return -1;
115
116 if ( vio->nonblock == NULL )
117  return -1;
118
119 return vio->nonblock(vio, state);
120}
121
122int     roar_vio_sync    (struct roar_vio_calls * vio) {
123 if ( vio == NULL )
124  return -1;
125
126 if ( vio->sync == NULL )
127  return -1;
128
129 return vio->sync(vio);
130}
131
132int     roar_vio_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
133 if ( vio == NULL )
134  return -1;
135
136 if ( vio->ctl == NULL )
137  return -1;
138
139 return vio->ctl(vio, cmd, data);
140}
141
142int     roar_vio_close    (struct roar_vio_calls * vio) {
143 if ( vio == NULL )
144  return -1;
145
146 if ( vio->close == NULL )
147  return -1;
148
149 return vio->close(vio);
150}
151
152int     roar_vio_putc    (struct roar_vio_calls * vio, char c) {
153 return roar_vio_write(vio, &c, 1);
154}
155
156int     roar_vio_getc    (struct roar_vio_calls * vio) {
157 unsigned char c;
158
159 if ( roar_vio_read(vio, &c, 1) != 1 )
160  return EOF;
161
162 return c;
163}
164
165// converters:
166int     roar_vio_open_file     (struct roar_vio_calls * calls, char * filename, int flags, mode_t mode) {
167 int fh;
168
169 if ( calls == NULL || filename == NULL )
170  return -1;
171
172 if ( (fh = open(filename, flags, mode)) == -1 )
173  return -1;
174
175 if ( roar_vio_open_fh(calls, fh) == -1 ) {
176  close(fh);
177  return -1;
178 }
179
180 return 0;
181}
182
183int     roar_vio_open_fh       (struct roar_vio_calls * calls, int fh) {
184 if ( calls == NULL )
185  return -1;
186
187 if ( roar_vio_init_calls(calls) == -1 )
188  return -1;
189
190 return roar_vio_set_fh(calls, fh);
191}
192
193int     roar_vio_open_fh_socket(struct roar_vio_calls * calls, int fh) {
194 if ( calls == NULL )
195  return -1;
196
197 return roar_vio_open_fh(calls, fh);
198}
199
200int     roar_vio_open_socket   (struct roar_vio_calls * calls, char * host, int port) {
201 int fh;
202
203 if ( calls == NULL )
204  return -1;
205
206 if ( (fh = roar_socket_connect(host, port)) == -1 )
207  return -1;
208
209 return roar_vio_open_fh_socket(calls, fh);
210}
211
212int     roar_vio_open_socket_listen(struct roar_vio_calls * calls, int type, char * host, int port) {
213 int fh;
214
215 if ( calls == NULL )
216  return -1;
217
218 if ( (fh = roar_socket_listen(type, host, port)) == -1 )
219  return -1;
220
221 return roar_vio_open_fh_socket(calls, fh);
222}
223
224int     roar_vio_simple_stream (struct roar_vio_calls * calls, int rate, int channels, int bits, int codec,
225                                                               char * server, int dir, char * name) {
226 int fh;
227
228 if ( calls == NULL )
229  return -1;
230
231 if ( (fh = roar_simple_stream(rate, channels, bits, codec, server, dir, name)) == -1 )
232  return -1;
233
234 return roar_vio_open_fh_socket(calls, fh);
235}
236
237
238int     roar_vio_open_stdio    (struct roar_vio_calls * calls, FILE * dst) {
239 if ( calls == NULL || dst == NULL )
240  return -1;
241
242 memset(calls, 0, sizeof(struct roar_vio_calls));
243
244 calls->read  = roar_vio_stdio_read;
245 calls->write = roar_vio_stdio_write;
246 calls->lseek = roar_vio_stdio_lseek;
247 calls->sync  = roar_vio_stdio_sync;
248 calls->close = roar_vio_stdio_close;
249
250 calls->inst  = dst;
251
252 return 0;
253}
254
255FILE *  roar_vio_to_stdio      (struct roar_vio_calls * calls, int flags) {
256#ifdef ROAR_HAVE_FOPENCOOKIE
257 cookie_io_functions_t foc_funcs;
258#endif
259
260 if ( calls == NULL )
261  return NULL;
262
263#if defined(ROAR_HAVE_FOPENCOOKIE)
264 memset(&foc_funcs, 0, sizeof(cookie_io_functions_t));
265
266 foc_funcs.close = roar_vio_to_stdio_close;
267 foc_funcs.read  = roar_vio_to_stdio_read;
268 foc_funcs.write = roar_vio_to_stdio_write;
269
270 return fopencookie((void*) calls, "rw", foc_funcs);
271#elif defined(ROAR_HAVE_FUNOPEN)
272 return funopen((void*) calls, roar_vio_to_stdio_read,  roar_vio_to_stdio_write,
273                               roar_vio_to_stdio_lseek, roar_vio_to_stdio_close);
274#else
275 return NULL;
276#endif
277}
278
279#if defined(ROAR_HAVE_FOPENCOOKIE) || defined(ROAR_HAVE_FUNOPEN)
280int roar_vio_to_stdio_close (void *__cookie) {
281 return roar_vio_close((struct roar_vio_calls *) __cookie);
282}
283#endif
284
285#if defined(ROAR_HAVE_FOPENCOOKIE)
286__ssize_t roar_vio_to_stdio_read (void *__cookie, char *__buf, size_t __nbytes) {
287#elif defined(ROAR_HAVE_FUNOPEN)
288int roar_vio_to_stdio_read(void *__cookie, char *__buf, int __nbytes) {
289#endif
290 return roar_vio_read((struct roar_vio_calls *) __cookie, __buf, __nbytes);
291}
292
293#if defined(ROAR_HAVE_FOPENCOOKIE)
294__ssize_t roar_vio_to_stdio_write (void *__cookie, __const char *__buf, size_t __n) {
295#elif defined(ROAR_HAVE_FUNOPEN)
296int roar_vio_to_stdio_write(void *__cookie, const char *__buf, int __n) {
297#endif
298 return roar_vio_write((struct roar_vio_calls *) __cookie, (char *) __buf, __n);
299}
300
301#if defined(ROAR_HAVE_FOPENCOOKIE)
302int roar_vio_to_stdio_lseek (void *__cookie, _IO_off64_t *__pos, int __w);
303#elif defined(ROAR_HAVE_FUNOPEN)
304fpos_t roar_vio_to_stdio_lseek(void *__cookie, fpos_t __pos, int __w) {
305 return roar_vio_lseek((struct roar_vio_calls *) __cookie, __pos, __w);
306}
307#endif
308
309// VIOs:
310
311// basic
312ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
313 return read(roar_vio_get_fh(vio), buf, count);
314}
315
316ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
317 return write(roar_vio_get_fh(vio), buf, count);
318}
319
320off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
321 return lseek(roar_vio_get_fh(vio), offset, whence);
322}
323
324int     roar_vio_basic_nonblock(struct roar_vio_calls * vio, int state) {
325 if ( roar_socket_nonblock(roar_vio_get_fh(vio), state) == -1 )
326  return -1;
327
328 if ( state == ROAR_SOCKET_NONBLOCK )
329  return 0;
330
331 roar_vio_sync(vio);
332
333 return 0;
334}
335
336int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
337 return ROAR_FDATASYNC(roar_vio_get_fh(vio));
338}
339
340int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
341 return close(roar_vio_get_fh(vio));
342}
343
344// null
345ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
346 if ( vio == NULL || buf == NULL )
347  return -1;
348
349 return 0;
350}
351
352// pass
353int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
354 if ( calls == NULL )
355  return -1;
356
357 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
358
359 calls->read     = roar_vio_pass_read;
360 calls->write    = roar_vio_pass_write;
361 calls->lseek    = roar_vio_pass_lseek;
362 calls->nonblock = roar_vio_pass_nonblock;
363 calls->sync     = roar_vio_pass_sync;
364 calls->close    = roar_vio_pass_close;
365
366 calls->inst     = dst;
367
368 return 0;
369}
370
371ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
372 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
373}
374
375ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
376 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
377}
378
379off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
380 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
381}
382
383int     roar_vio_pass_nonblock(struct roar_vio_calls * vio, int state) {
384 return roar_vio_nonblock((struct roar_vio_calls *) vio->inst, state);
385}
386
387int     roar_vio_pass_sync    (struct roar_vio_calls * vio) {
388 return roar_vio_sync((struct roar_vio_calls *) vio->inst);
389}
390
391int     roar_vio_pass_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
392 return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data);
393}
394
395int     roar_vio_pass_close   (struct roar_vio_calls * vio) {
396 return roar_vio_close((struct roar_vio_calls *) vio->inst);
397}
398
399
400// re
401int     roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
402 if ( roar_vio_open_pass(calls, dst) == -1 )
403  return -1;
404
405 calls->read  = roar_vio_re_read;
406 calls->write = roar_vio_re_write;
407 calls->lseek = roar_vio_re_lseek;
408
409 return 0;
410}
411ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
412  size_t len =  0;
413 ssize_t r   = -1;
414
415 if ( vio == NULL )
416  return -1;
417
418 if ( vio->inst == NULL )
419  return -1;
420
421 errno = 0;
422
423 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
424  len   += r;
425  buf   += r;
426  count -= r;
427  if ( count == 0 )
428   break;
429 }
430
431 if ( len == 0 && r == -1 )
432  return -1;
433
434 return len;
435}
436
437ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
438  size_t len =  0;
439 ssize_t r   = -1;
440
441 if ( vio == NULL )
442  return -1;
443
444 if ( vio->inst == NULL )
445  return -1;
446
447 errno = 0;
448
449 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
450  len   += r;
451  buf   += r;
452  count -= r;
453  if ( count == 0 )
454   break;
455 }
456
457 if ( len == 0 && r == -1 )
458  return -1;
459
460 return len;
461}
462
463// TODO: we should do a some more intelgent thing here.
464off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
465 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
466}
467
468// stdio:
469ssize_t roar_vio_stdio_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
470 return fread(buf, 1, count, (FILE*)(vio->inst));
471}
472
473ssize_t roar_vio_stdio_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
474 return fwrite(buf, 1, count, (FILE*)(vio->inst));
475}
476
477off_t   roar_vio_stdio_lseek   (struct roar_vio_calls * vio, off_t offset, int whence) {
478 if ( fseek((FILE*)(vio->inst), offset, whence) == -1 )
479  return -1;
480
481 return ftell((FILE*)(vio->inst));
482}
483
484int     roar_vio_stdio_sync    (struct roar_vio_calls * vio) {
485 return fflush((FILE*)(vio->inst));
486}
487
488int     roar_vio_stdio_close   (struct roar_vio_calls * vio) {
489 return fclose((FILE*)(vio->inst));
490}
491
492//ll
Note: See TracBrowser for help on using the repository browser.