JRTPLIB  3.11.2 (development version)
rtpselect.h
Go to the documentation of this file.
1 /*
2 
3  This file is a part of JRTPLIB
4  Copyright (c) 1999-2017 Jori Liesenborgs
5 
6  Contact: jori.liesenborgs@gmail.com
7 
8  This library was developed at the Expertise Centre for Digital Media
9  (http://www.edm.uhasselt.be), a research center of the Hasselt University
10  (http://www.uhasselt.be). The library is based upon work done for
11  my thesis at the School for Knowledge Technology (Belgium/The Netherlands).
12 
13  Permission is hereby granted, free of charge, to any person obtaining a
14  copy of this software and associated documentation files (the "Software"),
15  to deal in the Software without restriction, including without limitation
16  the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  and/or sell copies of the Software, and to permit persons to whom the
18  Software is furnished to do so, subject to the following conditions:
19 
20  The above copyright notice and this permission notice shall be included
21  in all copies or substantial portions of the Software.
22 
23  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29  IN THE SOFTWARE.
30 
31 */
32 
37 #ifndef RTPSELECT_H
38 
39 #define RTPSELECT_H
40 
41 #include "rtpconfig.h"
42 #include "rtptypes.h"
43 #include "rtperrors.h"
44 #include "rtptimeutilities.h"
45 #include "rtpsocketutil.h"
46 
47 #if defined(RTP_HAVE_WSAPOLL) || defined(RTP_HAVE_POLL)
48 
49 #ifndef RTP_HAVE_WSAPOLL
50 #include <poll.h>
51 #include <errno.h>
52 #endif // !RTP_HAVE_WSAPOLL
53 
54 #include <vector>
55 #include <limits>
56 
57 namespace jrtplib
58 {
59 
60 inline int RTPSelect(const SocketType *sockets, int8_t *readflags, size_t numsocks, RTPTime timeout)
61 {
62  using namespace std;
63 
64  vector<struct pollfd> fds(numsocks);
65 
66  for (size_t i = 0 ; i < numsocks ; i++)
67  {
68  fds[i].fd = sockets[i];
69  fds[i].events = POLLIN;
70  fds[i].revents = 0;
71  readflags[i] = 0;
72  }
73 
74  int timeoutmsec = -1;
75  if (timeout.GetDouble() >= 0)
76  {
77  double dtimeoutmsec = timeout.GetDouble()*1000.0;
78  if (dtimeoutmsec > (numeric_limits<int>::max)()) // parentheses to prevent windows 'max' macro expansion
79  dtimeoutmsec = (numeric_limits<int>::max)();
80 
81  timeoutmsec = (int)dtimeoutmsec;
82  }
83 
84 #ifdef RTP_HAVE_WSAPOLL
85  int status = WSAPoll(&(fds[0]), (ULONG)numsocks, timeoutmsec);
86  if (status < 0)
87  return ERR_RTP_SELECT_ERRORINPOLL;
88 #else
89  int status = poll(&(fds[0]), numsocks, timeoutmsec);
90  if (status < 0)
91  {
92  // We're just going to ignore an EINTR
93  if (errno == EINTR)
94  return 0;
95  return ERR_RTP_SELECT_ERRORINPOLL;
96  }
97 #endif // RTP_HAVE_WSAPOLL
98 
99  if (status > 0)
100  {
101  for (size_t i = 0 ; i < numsocks ; i++)
102  {
103  if (fds[i].revents)
104  readflags[i] = 1;
105  }
106  }
107  return status;
108 }
109 
110 } // end namespace
111 
112 #else
113 
114 #ifndef RTP_SOCKETTYPE_WINSOCK
115 #include <sys/select.h>
116 #include <sys/time.h>
117 #include <sys/types.h>
118 #include <errno.h>
119 #endif // !RTP_SOCKETTYPE_WINSOCK
120 
121 namespace jrtplib
122 {
123 
134 inline int RTPSelect(const SocketType *sockets, int8_t *readflags, size_t numsocks, RTPTime timeout)
135 {
136  struct timeval tv;
137  struct timeval *pTv = 0;
138 
139  if (timeout.GetDouble() >= 0)
140  {
141  tv.tv_sec = (long)timeout.GetSeconds();
142  tv.tv_usec = timeout.GetMicroSeconds();
143  pTv = &tv;
144  }
145 
146  fd_set fdset;
147  FD_ZERO(&fdset);
148  for (size_t i = 0 ; i < numsocks ; i++)
149  {
150 #ifndef RTP_SOCKETTYPE_WINSOCK
151  const int setsize = FD_SETSIZE;
152  // On windows it seems that comparing the socket value to FD_SETSIZE does
153  // not make sense
154  if (sockets[i] >= setsize)
155  return ERR_RTP_SELECT_SOCKETDESCRIPTORTOOLARGE;
156 #endif // RTP_SOCKETTYPE_WINSOCK
157  FD_SET(sockets[i], &fdset);
158  readflags[i] = 0;
159  }
160 
161  int status = select(FD_SETSIZE, &fdset, 0, 0, pTv);
162 #ifdef RTP_SOCKETTYPE_WINSOCK
163  if (status < 0)
164  return ERR_RTP_SELECT_ERRORINSELECT;
165 #else
166  if (status < 0)
167  {
168  // We're just going to ignore an EINTR
169  if (errno == EINTR)
170  return 0;
171  return ERR_RTP_SELECT_ERRORINSELECT;
172  }
173 #endif // RTP_HAVE_WSAPOLL
174 
175  if (status > 0) // some descriptors were set, check them
176  {
177  for (size_t i = 0 ; i < numsocks ; i++)
178  {
179  if (FD_ISSET(sockets[i], &fdset))
180  readflags[i] = 1;
181  }
182  }
183  return status;
184 }
185 
186 } // end namespace
187 
188 #endif // RTP_HAVE_POLL || RTP_HAVE_WSAPOLL
189 
190 #endif // RTPSELECT_H
This class is used to specify wallclock time, delay intervals etc.
Definition: rtptimeutilities.h:86
int64_t GetSeconds() const
Returns the number of seconds stored in this instance.
Definition: rtptimeutilities.h:177
uint32_t GetMicroSeconds() const
Returns the number of microseconds stored in this instance.
Definition: rtptimeutilities.h:182
double GetDouble() const
Returns the time stored in this instance, expressed in units of seconds.
Definition: rtptimeutilities.h:117
int RTPSelect(const SocketType *sockets, int8_t *readflags, size_t numsocks, RTPTime timeout)
Wrapper function around 'select', 'poll' or 'WSAPoll', depending on the availability on your platform...
Definition: rtpselect.h:134