source: roaraudio/libroar/vio_pipe.c @ 5347:1d76e45ebfd1

Last change on this file since 5347:1d76e45ebfd1 was 5300:190af1adf91c, checked in by phi, 12 years ago

first commit to move away from old roar_buffer_add() to roar_buffer_moveinto() (See: #126)

File size: 9.0 KB
Line 
1//vio_pipe.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009-2011
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
38int roar_vio_open_pipe (struct roar_vio_calls * s0, struct roar_vio_calls * s1, int type, int flags) {
39#ifndef ROAR_WITHOUT_VIO_PIPE
40 struct roar_vio_pipe * self;
41 int                    rw = flags & (O_RDONLY|O_WRONLY|O_RDWR);
42
43 if ( s0 == NULL || s1 == NULL )
44  return -1;
45
46 if ( (self = roar_mm_malloc(sizeof(struct roar_vio_pipe))) == NULL )
47  return -1;
48
49 memset(self, 0, sizeof(struct roar_vio_pipe));
50
51 self->refcount = 2;
52 self->flags    = flags;
53
54 if ( type == ROAR_VIO_PIPE_TYPE_AUTO ) {
55#ifdef ROAR_TARGET_WIN32
56  type = ROAR_VIO_PIPE_TYPE_BUFFER;
57#else
58  type = ROAR_VIO_PIPE_TYPE_SOCKET;
59#endif
60 }
61
62 self->type     = type;
63
64 switch (type) {
65  case ROAR_VIO_PIPE_TYPE_BUFFER:
66    // no buffers need to be set up here,
67    // we handle the NULL pointer in the reader and writer func
68    roar_mm_free(self);
69    return -1;
70   break;
71  case ROAR_VIO_PIPE_TYPE_PIPE:
72    self->b.p[0] = self->b.p[1] = self->b.p[2] = self->b.p[3] = -1;
73
74    if ( rw == O_RDWR || rw == O_RDONLY )
75     if ( pipe(self->b.p) == -1 ) {
76      roar_mm_free(self);
77      return -1;
78     }
79    if ( rw == O_RDWR || rw == O_WRONLY )
80     if ( pipe((self->b.p) + 2) == -1 ) {
81      close(self->b.p[0]);
82      close(self->b.p[1]);
83      roar_mm_free(self);
84      return -1;
85     }
86   break;
87#ifdef ROAR_HAVE_UNIX
88  case ROAR_VIO_PIPE_TYPE_SOCKET:
89    if ( socketpair(AF_UNIX, SOCK_STREAM, 0, self->b.p) == -1 ) {
90     roar_mm_free(self);
91     return -1;
92    }
93
94    if ( rw == O_RDONLY ) {
95     ROAR_SHUTDOWN(self->b.p[0], SHUT_WR);
96     ROAR_SHUTDOWN(self->b.p[1], SHUT_RD);
97    } else if ( rw == O_WRONLY ) {
98     ROAR_SHUTDOWN(self->b.p[0], SHUT_RD);
99     ROAR_SHUTDOWN(self->b.p[1], SHUT_WR);
100    }
101   break;
102#endif
103  default:
104    roar_mm_free(self);
105    return -1;
106 }
107
108 roar_vio_pipe_init(s0, self, flags);
109 roar_vio_pipe_init(s1, self, flags);
110
111 self->s0 = s0;
112
113 return 0;
114#else
115 return -1;
116#endif
117}
118
119#ifndef ROAR_WITHOUT_VIO_PIPE
120int roar_vio_pipe_init (struct roar_vio_calls * s,  struct roar_vio_pipe * self, int flags) {
121 int nonblock = ROAR_SOCKET_NONBLOCK;
122
123 if ( s == NULL || self == NULL )
124  return -1;
125
126 memset(s, 0, sizeof(struct roar_vio_calls));
127
128 s->close    = roar_vio_pipe_close;
129 s->read     = roar_vio_pipe_read;
130 s->write    = roar_vio_pipe_write;
131 s->sync     = roar_vio_pipe_sync;
132
133 s->inst = (void*) self;
134
135 if ( flags & O_NONBLOCK ) {
136  roar_vio_pipe_ctl(s, ROAR_VIO_CTL_NONBLOCK, &nonblock);
137 }
138
139 return 0;
140}
141
142int     roar_vio_pipe_close   (struct roar_vio_calls * vio) {
143 struct roar_vio_pipe * self;
144 int                    idx;
145
146 if ( vio == NULL )
147  return -1;
148
149 if ( (self = (struct roar_vio_pipe *)vio->inst) == NULL )
150  return -1;
151
152 self->refcount--;
153
154 switch (self->type) {
155  case ROAR_VIO_PIPE_TYPE_BUFFER:
156    // this will be a bit more complex as we need to change the flags, too.
157   break;
158  case ROAR_VIO_PIPE_TYPE_PIPE:
159   switch (ROAR_VIO_PIPE_S(self, vio)) {
160    case 0:
161      close(self->b.p[0]);
162      close(self->b.p[3]);
163      self->b.p[0] = -1;
164      self->b.p[3] = -1;
165     break;
166    case 1:
167      close(self->b.p[1]);
168      close(self->b.p[2]);
169      self->b.p[1] = -1;
170      self->b.p[2] = -1;
171     break;
172   }
173   break;
174#ifdef ROAR_HAVE_UNIX
175  case ROAR_VIO_PIPE_TYPE_SOCKET:
176    close(self->b.p[idx = ROAR_VIO_PIPE_S(self, vio)]);
177    self->b.p[idx] = -1;
178   break;
179#endif
180 }
181
182 if ( ! self->refcount ) {
183  roar_mm_free(self);
184 }
185
186 vio->inst = NULL;
187 return 0;
188}
189
190int     roar_vio_pipe_sync    (struct roar_vio_calls * vio) {
191 // we may add fdatasync() calls here depending on the type
192 // but in general they should not be needed on pipes.
193 (void)vio;
194 return 0;
195}
196
197int     roar_vio_pipe_ctl     (struct roar_vio_calls * vio, roar_vio_ctl_t cmd, void * data) {
198 struct roar_vio_pipe * self;
199
200 if (vio == NULL || cmd == -1)
201  return -1;
202
203 if ( (self = (struct roar_vio_pipe *)vio->inst) == NULL )
204  return -1;
205
206 switch (cmd) {
207  case ROAR_VIO_CTL_GET_NAME:
208    if ( data == NULL )
209     return -1;
210
211    *(char**)data = "pipe";
212    return 0;
213   break;
214  case ROAR_VIO_CTL_GET_FH:
215  case ROAR_VIO_CTL_GET_SELECT_FH:
216#ifdef ROAR_HAVE_UNIX
217    if ( self->type == ROAR_VIO_PIPE_TYPE_SOCKET ) {
218     *(int*)data = self->b.p[ROAR_VIO_PIPE_S(self,vio)];
219     return 0;
220    } else {
221     return -1;
222    }
223#else
224    return -1;
225#endif
226   break;
227  case ROAR_VIO_CTL_GET_READ_FH:
228  case ROAR_VIO_CTL_GET_SELECT_READ_FH:
229    switch (self->type) {
230#ifdef ROAR_HAVE_UNIX
231     case ROAR_VIO_PIPE_TYPE_SOCKET:
232       *(int*)data = self->b.p[ROAR_VIO_PIPE_S(self,vio)];
233       return 0;
234      break;
235#endif
236     case ROAR_VIO_PIPE_TYPE_PIPE:
237       *(int*)data = self->b.p[ROAR_VIO_PIPE_S(self,vio)*2];
238       return 0;
239      break;
240    }
241  case ROAR_VIO_CTL_GET_WRITE_FH:
242  case ROAR_VIO_CTL_GET_SELECT_WRITE_FH:
243    switch (self->type) {
244#ifdef ROAR_HAVE_UNIX
245     case ROAR_VIO_PIPE_TYPE_SOCKET:
246       *(int*)data = self->b.p[ROAR_VIO_PIPE_S(self,vio)];
247       return 0;
248      break;
249#endif
250     case ROAR_VIO_PIPE_TYPE_PIPE:
251       *(int*)data = self->b.p[(ROAR_VIO_PIPE_SR(self,vio)*2)+1];
252       return 0;
253      break;
254    }
255   break;
256  case ROAR_VIO_CTL_NONBLOCK:
257    switch (self->type) {
258     case ROAR_VIO_PIPE_TYPE_PIPE:
259       if ( roar_socket_nonblock(self->b.p[ROAR_VIO_PIPE_S(self,vio)*2], *(int*)data) == -1 )
260        return -1;
261       return roar_socket_nonblock(self->b.p[(ROAR_VIO_PIPE_SR(self,vio)*2)+1], *(int*)data);
262      break;
263#ifdef ROAR_HAVE_UNIX
264     case ROAR_VIO_PIPE_TYPE_SOCKET:
265       return roar_socket_nonblock(self->b.p[ROAR_VIO_PIPE_S(self,vio)], *(int*)data);
266      break;
267#endif
268    }
269   break;
270 }
271
272 return -1;
273}
274
275
276ssize_t roar_vio_pipe_read    (struct roar_vio_calls * vio, void *buf, size_t count) {
277 struct roar_vio_pipe * self;
278 int                    idx;
279
280 if ( vio == NULL )
281  return -1;
282
283 if ( (self = (struct roar_vio_pipe *)vio->inst) == NULL )
284  return -1;
285
286 switch (self->type) {
287  case ROAR_VIO_PIPE_TYPE_BUFFER:
288    idx = ROAR_VIO_PIPE_S(self,vio);
289
290    if ( (idx == 0 ? O_WRONLY : O_RDONLY) == (self->flags & (O_RDONLY|O_WRONLY|O_RDWR)) ) {
291     raise(SIGPIPE);
292     return -1;
293    }
294
295    if ( self->b.b[idx] == NULL )
296     return 0;
297
298    if ( roar_buffer_shift_out(&(self->b.b[idx]), buf, &count) == -1 )
299     return -1;
300
301    return count;
302   break;
303  case ROAR_VIO_PIPE_TYPE_PIPE:
304    return read(self->b.p[ROAR_VIO_PIPE_S(self,vio)*2], buf, count);
305   break;
306#ifdef ROAR_HAVE_UNIX
307  case ROAR_VIO_PIPE_TYPE_SOCKET:
308    return read(self->b.p[ROAR_VIO_PIPE_S(self,vio)], buf, count);
309   break;
310#endif
311 }
312
313 return -1;
314}
315
316ssize_t roar_vio_pipe_write   (struct roar_vio_calls * vio, void *buf, size_t count) {
317 struct roar_vio_pipe * self;
318 struct roar_buffer   * next;
319 void                 * data;
320 int                    idx;
321
322 if ( vio == NULL )
323  return -1;
324
325 if ( (self = (struct roar_vio_pipe *)vio->inst) == NULL )
326  return -1;
327
328 switch (self->type) {
329  case ROAR_VIO_PIPE_TYPE_BUFFER:
330    if ( self->refcount < 2 ) {
331     raise(SIGPIPE);
332     return -1;
333    }
334
335    idx = ROAR_VIO_PIPE_SR(self,vio);
336
337    if ( (idx == 0 ? O_WRONLY : O_RDONLY) == (self->flags & (O_RDONLY|O_WRONLY|O_RDWR)) ) {
338     raise(SIGPIPE);
339     return -1;
340    }
341
342    if ( roar_buffer_new_data(&next, count, &data) == -1 )
343     return -1;
344
345    memcpy(data, buf, count);
346
347    if ( self->b.b[idx] == NULL ) {
348     self->b.b[idx] = next;
349    } else {
350     if ( roar_buffer_moveinto(self->b.b[idx], &next) == -1 ) {
351      roar_buffer_free(next);
352      return -1;
353     }
354    }
355
356    return count;
357   break;
358  case ROAR_VIO_PIPE_TYPE_PIPE:
359    return write(self->b.p[(ROAR_VIO_PIPE_SR(self,vio)*2)+1], buf, count);
360   break;
361#ifdef ROAR_HAVE_UNIX
362  case ROAR_VIO_PIPE_TYPE_SOCKET:
363    return write(self->b.p[ROAR_VIO_PIPE_S(self,vio)], buf, count);
364   break;
365#endif
366 }
367
368 return -1;
369}
370#endif
371
372//ll
Note: See TracBrowser for help on using the repository browser.