source: roaraudio/libroar/vio.c @ 3769:14c294155cad

Last change on this file since 3769:14c294155cad was 3769:14c294155cad, checked in by phi, 14 years ago

support to accept on sockets

File size: 12.8 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, 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#ifdef ROAR_HAVE_IO_POSIX
39#define _CAN_OPERATE
40#endif
41
42int roar_vio_init_calls (struct roar_vio_calls * calls) {
43#ifdef _CAN_OPERATE
44 if ( calls == NULL )
45  return -1;
46
47 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
48
49/*
50 calls->read  = (ssize_t (*)(int fd, void *buf, size_t count,      void * inst))read;
51 calls->write = (ssize_t (*)(int fd, void *buf, size_t count,      void * inst))write;
52 calls->lseek = (off_t   (*)(int fildes, off_t offset, int whence, void * inst))lseek;
53*/
54
55 calls->read     = roar_vio_basic_read;
56 calls->write    = roar_vio_basic_write;
57 calls->lseek    = roar_vio_basic_lseek;
58 calls->nonblock = roar_vio_basic_nonblock;
59 calls->sync     = roar_vio_basic_sync;
60 calls->ctl      = roar_vio_basic_ctl;
61 calls->close    = roar_vio_basic_close;
62
63 return 0;
64#else
65 return -1;
66#endif
67}
68
69int roar_vio_set_inst (struct roar_vio_calls * vio, void * inst) {
70 if ( vio == NULL )
71  return -1;
72
73 vio->inst = inst;
74
75 return 0;
76}
77
78int roar_vio_set_fh   (struct roar_vio_calls * vio, int fh) {
79 return roar_vio_set_inst(vio, (void*)(ROAR_INSTINT)(fh + 1));
80}
81
82int roar_vio_get_fh   (struct roar_vio_calls * vio) {
83 if ( vio == NULL )
84  return -1;
85
86 return ((int)(ROAR_INSTINT)vio->inst) - 1;
87}
88
89
90ssize_t roar_vio_read (struct roar_vio_calls * vio, void *buf, size_t count) {
91 ROAR_DBG("roar_vio_read(vio=%p, buf=%p, count=%u) = ?", vio, buf, (unsigned int)count);
92
93 if ( vio == NULL )
94  return -1;
95
96 if ( vio->read == NULL )
97  return -1;
98
99 return vio->read(vio, buf, count);
100}
101
102ssize_t roar_vio_write(struct roar_vio_calls * vio, void *buf, size_t count) {
103 ROAR_DBG("roar_vio_write(vio=%p, buf=%p, count=%u) = ?", vio, buf, (unsigned int)count);
104
105 if ( vio == NULL )
106  return -1;
107
108 if ( vio->write == NULL )
109  return -1;
110
111 return vio->write(vio, buf, count);
112}
113
114off_t   roar_vio_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
115 ROAR_DBG("roar_vio_lseek(vio=%p, offset=%u, whence=%i) = ?", vio, (unsigned int)offset, whence);
116
117 if ( vio == NULL )
118  return -1;
119
120 if ( vio->lseek == NULL )
121  return -1;
122
123 return vio->lseek(vio, offset, whence);
124}
125
126int     roar_vio_nonblock(struct roar_vio_calls * vio, int state) {
127 ROAR_DBG("roar_vio_nonblock(vio=%p, state=%i) = ?", vio, state);
128
129 if ( vio == NULL )
130  return -1;
131
132 if ( vio->nonblock == NULL )
133  return -1;
134
135 return vio->nonblock(vio, state);
136}
137
138int     roar_vio_sync    (struct roar_vio_calls * vio) {
139 ROAR_DBG("roar_vio_sync(vio=%p) = ?", vio);
140
141 if ( vio == NULL )
142  return -1;
143
144 if ( vio->sync == NULL )
145  return -1;
146
147 return vio->sync(vio);
148}
149
150int     roar_vio_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
151 ROAR_DBG("roar_vio_ctl(vio=%p, cmd=%i, data=%p) = ?", vio, cmd, data);
152
153 if ( vio == NULL )
154  return -1;
155
156 ROAR_DBG("roar_vio_ctl(vio=%p, cmd=0x%.8x, data=%p): vio->ctl=%p", vio, cmd, data, vio->ctl);
157
158 if ( vio->ctl == NULL )
159  return -1;
160
161 return vio->ctl(vio, cmd, data);
162}
163
164int     roar_vio_close    (struct roar_vio_calls * vio) {
165 ROAR_DBG("roar_vio_close(vio=%p) = ?", vio);
166
167 if ( vio == NULL )
168  return -1;
169
170 if ( vio->close == NULL )
171  return -1;
172
173 return vio->close(vio);
174}
175
176// specal commands:
177int     roar_vio_accept  (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
178 if (dst == NULL || calls == NULL)
179  return -1;
180
181 return roar_vio_ctl(dst, ROAR_VIO_CTL_ACCEPT, calls);
182}
183
184// converters:
185int     roar_vio_open_file     (struct roar_vio_calls * calls, char * filename, int flags, mode_t mode) {
186#ifdef _CAN_OPERATE
187 int fh;
188
189 if ( calls == NULL || filename == NULL )
190  return -1;
191
192#ifdef ROAR_TARGET_WIN32
193 flags |= O_BINARY;
194#endif
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
278int     roar_vio_simple_new_stream_obj (struct roar_vio_calls * calls,
279                                        struct roar_connection * con,
280                                        struct roar_stream * s,
281                                        int rate, int channels, int bits, int codec, int dir) {
282 int fh;
283
284 if ( calls == NULL )
285  return -1;
286
287 if ( (fh = roar_simple_new_stream_obj(con, s, rate, channels, bits, codec, dir)) == -1 )
288  return -1;
289
290 return roar_vio_open_fh_socket(calls, fh);
291}
292
293// VIOs:
294
295// basic
296ssize_t roar_vio_basic_read (struct roar_vio_calls * vio, void *buf, size_t count) {
297#ifdef _CAN_OPERATE
298 return read(roar_vio_get_fh(vio), buf, count);
299#else
300 return -1;
301#endif
302}
303
304ssize_t roar_vio_basic_write(struct roar_vio_calls * vio, void *buf, size_t count) {
305#ifdef _CAN_OPERATE
306 return write(roar_vio_get_fh(vio), buf, count);
307#else
308 return -1;
309#endif
310}
311
312off_t   roar_vio_basic_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
313#ifdef _CAN_OPERATE
314 return lseek(roar_vio_get_fh(vio), offset, whence);
315#else
316 return -1;
317#endif
318}
319
320int     roar_vio_basic_nonblock(struct roar_vio_calls * vio, int state) {
321 if ( roar_socket_nonblock(roar_vio_get_fh(vio), state) == -1 )
322  return -1;
323
324 if ( state == ROAR_SOCKET_NONBLOCK )
325  return 0;
326
327 roar_vio_sync(vio);
328
329 return 0;
330}
331
332int     roar_vio_basic_sync    (struct roar_vio_calls * vio) {
333#ifdef ROAR_FDATASYNC
334 return ROAR_FDATASYNC(roar_vio_get_fh(vio));
335#else
336 return 0;
337#endif
338}
339
340int     roar_vio_basic_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
341 int fh;
342
343 if ( vio == NULL || cmd == -1 )
344  return -1;
345
346 ROAR_DBG("roar_vio_basic_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
347
348 switch (cmd) {
349  case ROAR_VIO_CTL_GET_FH:
350  case ROAR_VIO_CTL_GET_READ_FH:
351  case ROAR_VIO_CTL_GET_WRITE_FH:
352  case ROAR_VIO_CTL_GET_SELECT_FH:
353  case ROAR_VIO_CTL_GET_SELECT_READ_FH:
354  case ROAR_VIO_CTL_GET_SELECT_WRITE_FH:
355    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));
356    *(int*)data = roar_vio_get_fh(vio);
357    return 0;
358   break;
359  case ROAR_VIO_CTL_SET_NOSYNC:
360    vio->sync = NULL;
361    return 0;
362   break;
363  case ROAR_VIO_CTL_ACCEPT:
364    fh = accept(roar_vio_get_fh(vio), NULL, 0);
365    if ( fh == -1 )
366     return -1;
367
368    // most proably a socket.
369    if ( roar_vio_open_fh_socket(data, fh) == -1 ) {
370#ifdef ROAR_TARGET_WIN32
371     closesocket(fh);
372#else
373     close(fh);
374#endif
375     return -1;
376    }
377
378    return 0;
379   break;
380 }
381
382 return -1;
383}
384
385int     roar_vio_basic_close    (struct roar_vio_calls * vio) {
386#ifdef _CAN_OPERATE
387 if ( roar_vio_get_fh(vio) != -1 )
388  return close(roar_vio_get_fh(vio));
389
390 return 0;
391#else
392 return -1;
393#endif
394}
395
396// null
397ssize_t roar_vio_null_rw    (struct roar_vio_calls * vio, void *buf, size_t count) {
398 if ( vio == NULL || buf == NULL )
399  return -1;
400
401 return 0;
402}
403
404int     roar_vio_null_sync    (struct roar_vio_calls * vio) {
405 return 0;
406}
407
408// pass
409int     roar_vio_open_pass    (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
410 if ( calls == NULL )
411  return -1;
412
413 memset((void*)calls, 0, sizeof(struct roar_vio_calls));
414
415 calls->read     = roar_vio_pass_read;
416 calls->write    = roar_vio_pass_write;
417 calls->lseek    = roar_vio_pass_lseek;
418 calls->nonblock = roar_vio_pass_nonblock;
419 calls->sync     = roar_vio_pass_sync;
420 calls->ctl      = roar_vio_pass_ctl;
421 calls->close    = roar_vio_pass_close;
422
423 calls->inst     = dst;
424
425 return 0;
426}
427
428ssize_t roar_vio_pass_read (struct roar_vio_calls * vio, void *buf, size_t count) {
429 return roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count);
430}
431
432ssize_t roar_vio_pass_write(struct roar_vio_calls * vio, void *buf, size_t count) {
433 return roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count);
434}
435
436off_t   roar_vio_pass_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
437 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
438}
439
440int     roar_vio_pass_nonblock(struct roar_vio_calls * vio, int state) {
441 return roar_vio_nonblock((struct roar_vio_calls *) vio->inst, state);
442}
443
444int     roar_vio_pass_sync    (struct roar_vio_calls * vio) {
445 return roar_vio_sync((struct roar_vio_calls *) vio->inst);
446}
447
448int     roar_vio_pass_ctl     (struct roar_vio_calls * vio, int cmd, void * data) {
449 if (vio == NULL || cmd == -1)
450  return -1;
451
452 ROAR_DBG("roar_vio_pass_ctl(vio=%p, cmd=0x%.8x, data=%p) = ?", vio, cmd, data);
453
454 switch (cmd) {
455  case ROAR_VIO_CTL_GET_NEXT:
456    *(struct roar_vio_calls **)data = vio->inst;
457    return 0;
458   break;
459  case ROAR_VIO_CTL_SET_NEXT:
460    vio->inst = *(struct roar_vio_calls **)data;
461    return 0;
462   break;
463 }
464
465 return roar_vio_ctl((struct roar_vio_calls *) vio->inst, cmd, data);
466}
467
468int     roar_vio_pass_close   (struct roar_vio_calls * vio) {
469 return roar_vio_close((struct roar_vio_calls *) vio->inst);
470}
471
472
473// re
474int     roar_vio_open_re (struct roar_vio_calls * calls, struct roar_vio_calls * dst) {
475 if ( roar_vio_open_pass(calls, dst) == -1 )
476  return -1;
477
478 calls->read  = roar_vio_re_read;
479 calls->write = roar_vio_re_write;
480 calls->lseek = roar_vio_re_lseek;
481
482 return 0;
483}
484ssize_t roar_vio_re_read (struct roar_vio_calls * vio, void *buf, size_t count) {
485  size_t len =  0;
486 ssize_t r   = -1;
487
488 if ( vio == NULL )
489  return -1;
490
491 if ( vio->inst == NULL )
492  return -1;
493
494 errno = 0;
495
496 while ( (r = roar_vio_read((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
497  len   += r;
498  buf   += r;
499  count -= r;
500  if ( count == 0 )
501   break;
502 }
503
504 if ( len == 0 && r == -1 )
505  return -1;
506
507 return len;
508}
509
510ssize_t roar_vio_re_write(struct roar_vio_calls * vio, void *buf, size_t count) {
511  size_t len =  0;
512 ssize_t r   = -1;
513
514 if ( vio == NULL )
515  return -1;
516
517 if ( vio->inst == NULL )
518  return -1;
519
520 errno = 0;
521
522 while ( (r = roar_vio_write((struct roar_vio_calls *) vio->inst, buf, count)) > 0 ) {
523  len   += r;
524  buf   += r;
525  count -= r;
526  if ( count == 0 )
527   break;
528 }
529
530 if ( len == 0 && r == -1 )
531  return -1;
532
533 return len;
534}
535
536// TODO: we should do a some more intelgent thing here.
537off_t   roar_vio_re_lseek(struct roar_vio_calls * vio, off_t offset, int whence) {
538 return roar_vio_lseek((struct roar_vio_calls *) vio->inst, offset, whence);
539}
540
541//ll
Note: See TracBrowser for help on using the repository browser.