source: roaraudio/libroar/vio_select.c @ 5271:09aa484b25f4

Last change on this file since 5271:09aa484b25f4 was 5074:d85bc9b1b8d7, checked in by phi, 13 years ago

force non-zero time

File size: 6.9 KB
Line 
1//vio_select.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
38ssize_t roar_vio_select(struct roar_vio_select * vios, size_t len, struct roar_vio_selecttv * rtv, struct roar_vio_selectctl * ctl) {
39#ifdef ROAR_HAVE_SELECT
40 struct timeval tv;
41 fd_set rfds, wfds, efds;
42#endif
43 size_t i;
44#ifdef ROAR_HAVE_SELECT
45 int max_fh = -1;
46#endif
47 int ret;
48#ifdef DEBUG
49 char * name;
50#endif
51
52 ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p) = ?", vios, (long long unsigned int)len, rtv, ctl);
53
54 if ( len == 0 ) {
55  ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p) = 0", vios, (long long unsigned int)len, rtv, ctl);
56  return 0;
57 }
58
59 if ( vios == NULL ) {
60  ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p) = -1", vios, (long long unsigned int)len, rtv, ctl);
61  return -1;
62 }
63
64 // pepaer internal structs:
65 for (i = 0; i < len; i++) {
66  ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p): vios[i=%i].fh=%i", vios, (long long unsigned int)len, rtv, ctl, i, vios[i].fh);
67  if ( vios[i].fh == -1 ) {
68#ifdef DEBUG
69   name = NULL;
70   roar_vio_ctl(vios[i].vio, ROAR_VIO_CTL_GET_NAME, &name);
71   ROAR_DBG("roar_vio_select(*): vios[%i]'s name is: '%s'", i, name);
72#endif
73
74   vios[i].internal.action = ROAR_VIO_SELECT_ACTION_NONE;
75
76   if ( vios[i].eventsq & ROAR_VIO_SELECT_READ ) {
77    if ( roar_vio_ctl(vios[i].vio, ROAR_VIO_CTL_GET_SELECT_READ_FH, &(vios[i].internal.fh[0])) == -1 ) {
78     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_VIOS;
79     vios[i].internal.fh[0]   = -1;
80    } else {
81     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_SELECT;
82    }
83   }
84
85   if ( vios[i].eventsq & ROAR_VIO_SELECT_WRITE ) {
86    if ( roar_vio_ctl(vios[i].vio, ROAR_VIO_CTL_GET_SELECT_WRITE_FH, &(vios[i].internal.fh[1])) == -1 ) {
87     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_VIOS;
88     vios[i].internal.fh[1]   = -1;
89    } else {
90     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_SELECT;
91    }
92   }
93
94   if ( vios[i].eventsq & ROAR_VIO_SELECT_EXCEPT ) {
95    if ( roar_vio_ctl(vios[i].vio, ROAR_VIO_CTL_GET_SELECT_FH, &(vios[i].internal.fh[2])) == -1 ) {
96     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_VIOS;
97     vios[i].internal.fh[2]   = -1;
98    } else {
99     vios[i].internal.action |= ROAR_VIO_SELECT_ACTION_SELECT;
100    }
101   }
102   ROAR_DBG("roar_vio_select(*): vios[%i].internal.fh[] = {%i, %i, %i}", i, vios[i].internal.fh[0], vios[i].internal.fh[1], vios[i].internal.fh[2]);
103  } else {
104   vios[i].internal.action = ROAR_VIO_SELECT_ACTION_SELECT;
105   vios[i].internal.fh[0]  = vios[i].fh;
106   vios[i].internal.fh[1]  = vios[i].fh;
107   vios[i].internal.fh[2]  = vios[i].fh;
108  }
109 }
110
111 // check:
112 for (i = 0; i < len; i++) {
113  if ( !( vios[i].internal.action == 0 || vios[i].internal.action == ROAR_VIO_SELECT_ACTION_SELECT ) ) {
114   // we currently do not support non-select selects.
115   // TODO: Fix this.
116   ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p) = -1", vios, (long long unsigned int)len, rtv, ctl);
117   return -1;
118  }
119 }
120
121#ifdef ROAR_HAVE_SELECT
122 // prepaer fdsets:
123 FD_ZERO(&rfds);
124 FD_ZERO(&wfds);
125 FD_ZERO(&efds);
126
127 for (i = 0; i < len; i++) {
128  if ( vios[i].eventsq & ROAR_VIO_SELECT_READ ) {
129   ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p): vios[i=%i] is READ, fh=%i", vios, (long long unsigned int)len, rtv, ctl, i, vios[i].internal.fh[0]);
130   FD_SET(vios[i].internal.fh[0], &rfds);
131   if ( vios[i].internal.fh[0] > max_fh )
132    max_fh = vios[i].internal.fh[0];
133  }
134
135  if ( vios[i].eventsq & ROAR_VIO_SELECT_WRITE ) {
136   ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p): vios[i=%i] is WRITE, fh=%i", vios, (long long unsigned int)len, rtv, ctl, i, vios[i].internal.fh[1]);
137   FD_SET(vios[i].internal.fh[1], &wfds);
138   if ( vios[i].internal.fh[1] > max_fh )
139    max_fh = vios[i].internal.fh[1];
140  }
141
142  if ( vios[i].eventsq & ROAR_VIO_SELECT_EXCEPT ) {
143   ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p): vios[i=%i] is EXCEPT, fh=%i", vios, (long long unsigned int)len, rtv, ctl, i, vios[i].internal.fh[2]);
144   FD_SET(vios[i].internal.fh[2], &efds);
145   if ( vios[i].internal.fh[2] > max_fh )
146    max_fh = vios[i].internal.fh[2];
147  }
148 }
149
150 // the the select:
151  if ( rtv == NULL ) {
152   tv.tv_sec  = 1024;
153   tv.tv_usec = 0;
154  } else {
155   tv.tv_sec  = rtv->sec;
156   tv.tv_usec = rtv->nsec / 1000;
157   if ( tv.tv_sec == 0 && tv.tv_usec == 0 )
158    tv.tv_usec = 1;
159  }
160
161
162 ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p): Doing select() with max_fh=%i", vios, (long long unsigned int)len, rtv, ctl, max_fh);
163 ret = select(max_fh + 1, &rfds, &wfds, &efds, &tv);
164 ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p): select() returned %i", vios, (long long unsigned int)len, rtv, ctl, ret);
165
166 // ret == -1 -> Error
167 // ret ==  0 -> No data
168 if ( ret < 1 ) {
169  ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p) = %lli", vios, (long long unsigned int)len, rtv, ctl, (long long int)ret);
170  return ret;
171 }
172
173 // set eventsa:
174 for (i = 0; i < len; i++) {
175  vios[i].eventsa = ROAR_VIO_SELECT_NONE;
176
177  if ( vios[i].eventsq & ROAR_VIO_SELECT_READ )
178   if ( FD_ISSET(vios[i].internal.fh[0], &rfds) )
179    vios[i].eventsa |= ROAR_VIO_SELECT_READ;
180
181  if ( vios[i].eventsq & ROAR_VIO_SELECT_WRITE )
182   if ( FD_ISSET(vios[i].internal.fh[1], &wfds) )
183    vios[i].eventsa |= ROAR_VIO_SELECT_WRITE;
184
185  if ( vios[i].eventsq & ROAR_VIO_SELECT_EXCEPT )
186   if ( FD_ISSET(vios[i].internal.fh[2], &efds) )
187    vios[i].eventsa |= ROAR_VIO_SELECT_EXCEPT;
188 }
189
190#else
191 ret = -1;
192#endif
193
194 ROAR_DBG("roar_vio_select(vios=%p, len=%llu, rtv=%p, ctl=%p) = %lli", vios, (long long unsigned int)len, rtv, ctl, (long long int)ret);
195 return ret;
196}
197
198//ll
Note: See TracBrowser for help on using the repository browser.