JRTPLIB  3.11.1
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 #endif // !RTP_HAVE_WSAPOLL
52 
53 #include <vector>
54 #include <limits>
55 
56 namespace jrtplib
57 {
58 
59 inline int RTPSelect(const SocketType *sockets, int8_t *readflags, size_t numsocks, RTPTime timeout)
60 {
61  using namespace std;
62 
63  vector<struct pollfd> fds(numsocks);
64 
65  for (size_t i = 0 ; i < numsocks ; i++)
66  {
67  fds[i].fd = sockets[i];
68  fds[i].events = POLLIN;
69  fds[i].revents = 0;
70  readflags[i] = 0;
71  }
72 
73  int timeoutmsec = -1;
74  if (timeout.GetDouble() >= 0)
75  {
76  double dtimeoutmsec = timeout.GetDouble()*1000.0;
77  if (dtimeoutmsec > (numeric_limits<int>::max)()) // parentheses to prevent windows 'max' macro expansion
78  dtimeoutmsec = (numeric_limits<int>::max)();
79 
80  timeoutmsec = (int)dtimeoutmsec;
81  }
82 
83 #ifdef RTP_HAVE_WSAPOLL
84  int status = WSAPoll(&(fds[0]), (ULONG)numsocks, timeoutmsec);
85 #else
86  int status = poll(&(fds[0]), numsocks, timeoutmsec);
87 #endif // RTP_HAVE_WSAPOLL
88  if (status < 0)
89  return ERR_RTP_SELECT_ERRORINPOLL;
90 
91  if (status > 0)
92  {
93  for (size_t i = 0 ; i < numsocks ; i++)
94  {
95  if (fds[i].revents)
96  readflags[i] = 1;
97  }
98  }
99  return status;
100 }
101 
102 } // end namespace
103 
104 #else
105 
106 #ifndef RTP_SOCKETTYPE_WINSOCK
107 #include <sys/select.h>
108 #include <sys/time.h>
109 #include <sys/types.h>
110 #endif // !RTP_SOCKETTYPE_WINSOCK
111 
112 namespace jrtplib
113 {
114 
125 inline int RTPSelect(const SocketType *sockets, int8_t *readflags, size_t numsocks, RTPTime timeout)
126 {
127  struct timeval tv;
128  struct timeval *pTv = 0;
129 
130  if (timeout.GetDouble() >= 0)
131  {
132  tv.tv_sec = (long)timeout.GetSeconds();
133  tv.tv_usec = timeout.GetMicroSeconds();
134  pTv = &tv;
135  }
136 
137  fd_set fdset;
138  FD_ZERO(&fdset);
139  for (size_t i = 0 ; i < numsocks ; i++)
140  {
141 #ifndef RTP_SOCKETTYPE_WINSOCK
142  const int setsize = FD_SETSIZE;
143  // On windows it seems that comparing the socket value to FD_SETSIZE does
144  // not make sense
145  if (sockets[i] >= setsize)
146  return ERR_RTP_SELECT_SOCKETDESCRIPTORTOOLARGE;
147 #endif // RTP_SOCKETTYPE_WINSOCK
148  FD_SET(sockets[i], &fdset);
149  readflags[i] = 0;
150  }
151 
152  int status = select(FD_SETSIZE, &fdset, 0, 0, pTv);
153  if (status < 0)
154  return ERR_RTP_SELECT_ERRORINSELECT;
155 
156  if (status > 0) // some descriptors were set, check them
157  {
158  for (size_t i = 0 ; i < numsocks ; i++)
159  {
160  if (FD_ISSET(sockets[i], &fdset))
161  readflags[i] = 1;
162  }
163  }
164  return status;
165 }
166 
167 } // end namespace
168 
169 #endif // RTP_HAVE_POLL || RTP_HAVE_WSAPOLL
170 
171 #endif // RTPSELECT_H
uint32_t GetMicroSeconds() const
Returns the number of microseconds stored in this instance.
Definition: rtptimeutilities.h:181
STL namespace.
double GetDouble() const
Returns the time stored in this instance, expressed in units of seconds.
Definition: rtptimeutilities.h:116
Definition: rtpfaketransmitter.h:64
This class is used to specify wallclock time, delay intervals etc.
Definition: rtptimeutilities.h:84
int64_t GetSeconds() const
Returns the number of seconds stored in this instance.
Definition: rtptimeutilities.h:176
int RTPSelect(const SocketType *sockets, int8_t *readflags, size_t numsocks, RTPTime timeout)
Wrapper function around &#39;select&#39;, &#39;poll&#39; or &#39;WSAPoll&#39;, depending on the availability on your platform...
Definition: rtpselect.h:125