5#define RESIP_POLL_IMPL_SELECT
12#define RESIP_POLL_EXTERN
18#ifdef RESIP_POLL_IMPL_POLL
22#ifdef RESIP_POLL_IMPL_SELECT
27# include <sys/types.h>
32#ifdef RESIP_POLL_EXTERN
39#if !defined(RESIP_POLL_IMPL_POLL) && !defined(RESIP_POLL_IMPL_SELECT) && !defined(RESIP_POLL_EPOLL)
40#error "Define one of RESIP_POLL_IMPL_POLL, RESIP_POLL_IMPL_SELECT, or RESIP_POLL_EPOLL."
53#ifdef RESIP_POLL_IMPL_POLL
54 std::vector<pollfd> _pollFDVector;
56#ifdef RESIP_POLL_IMPL_SELECT
61#ifdef RESIP_POLL_EXTERN
71#ifdef RESIP_POLL_IMPL_SELECT
76#ifdef RESIP_POLL_IMPL_SELECT
78 FD_ZERO(&_writeFDSet);
93 _stateBitMask(isTransport ?
Poll::
FDEntry::rsbmIsTransport : 0),
94 _index((unsigned short)poll->mImpl->_fdEntryVector.size())
96 _poll->mImpl->_fdEntryVector.push_back(
this);
97#ifdef RESIP_POLL_IMPL_POLL
100 pollFD.events = POLLIN;
101 _poll->mImpl->_pollFDVector.push_back(pollFD);
103#ifdef RESIP_POLL_IMPL_SELECT
104 if (_fd >= _poll->mImpl->_maxFDPlus1)
106 _poll->mImpl->_maxFDPlus1 = _fd + 1;
108 FD_SET(_fd, &(_poll->mImpl->_readFDSet));
110#ifdef RESIP_POLL_EXTERN
111 _poll->mImpl->_fdEntryMap.insert(std::pair<int/*FD*/, Poll::FDEntry *>(_fd,
this));
118 vector<Poll::FDEntry *> &fdEntryVector = _poll->mImpl->_fdEntryVector;
119 Poll::FDEntry *lastFDEntry = fdEntryVector[fdEntryVector.size() - 1];
120 lastFDEntry->_index = _index;
121 fdEntryVector[_index] = lastFDEntry;
122 fdEntryVector.pop_back();
123#ifdef RESIP_POLL_IMPL_POLL
124 vector<pollfd> &pollFDVector = _poll->mImpl->_pollFDVector;
125 pollFDVector[_index] = pollFDVector[pollFDVector.size() - 1];
126 pollFDVector.pop_back();
128#ifdef RESIP_POLL_IMPL_SELECT
129 FD_CLR(_fd, &(_poll->mImpl->_readFDSet));
130 FD_CLR(_fd, &(_poll->mImpl->_writeFDSet));
132#ifdef RESIP_POLL_EXTERN
133 _poll->mImpl->_fdEntryMap.erase(_fd);
149#ifdef RESIP_POLL_IMPL_POLL
152#ifdef RESIP_POLL_IMPL_SELECT
153 FD_SET(_fd, &(_poll->mImpl->_writeFDSet));
158#ifdef RESIP_POLL_IMPL_POLL
161#ifdef RESIP_POLL_IMPL_SELECT
162 FD_CLR(_fd, &(_poll->mImpl->_writeFDSet));
172 return (leftFDEntry->
getFD() - rightFDEntry->
getFD());
180 const vector<Poll::FDEntry *> & waitResult)
182 unsigned int lowIndex = 0;
183 unsigned int highIndex = (
unsigned int)waitResult.size();
184 while (lowIndex + 1 < highIndex)
187 unsigned int midIndex = (lowIndex + highIndex) / 2;
188 if (waitResult[midIndex]->_fd > fd)
190 highIndex = midIndex - 1;
210#ifdef RESIP_POLL_EXTERN
212const vector<Poll::FDEntry *> &
213Poll::beforeExternWait()
215 mImpl->_waitResult.clear();
216 return mImpl->_fdEntryVector;
220Poll::setEntryFDStateForExternWait(
int fd,
224 mImpl->_fdEntryMap.find(fd);
225 bool isFDInPoll = (fdEntryIterator != mImpl->_fdEntryMap.end());
230 mImpl->_waitResult.push_back(fdEntry);
235const vector<Poll::FDEntry *> &
236Poll::afterExternWait()
238 return mImpl->_waitResult;
243const vector<Poll::FDEntry *> &
246 mImpl->_waitResult.clear();
247#ifdef RESIP_POLL_IMPL_POLL
248 pollfd *pollFDArray = &(mImpl->_pollFDVector.front());
249 int numReadyFDs = poll(pollFDArray,
250 mImpl->_pollFDVector.size(),
251 timeoutMilliSeconds);
256 for (
unsigned short index = 0; numReadyFDs > 0; ++index)
258 int revents = pollFDArray[index].revents;
262 fdEntry->_stateBitMask |= revents;
263 mImpl->_waitResult.push_back(fdEntry);
267 qsort(&(mImpl->_waitResult.front()),
268 mImpl->_waitResult.size(),
270 reinterpret_cast<int(*)(
const void *,
271 const void *)
>(&Poll::FDEntry::compare));
273#ifdef RESIP_POLL_IMPL_SELECT
274 fd_set readableFDSet = mImpl->_readFDSet;
275 fd_set writableFDSet = mImpl->_writeFDSet;
276 fd_set errorFDSet = mImpl->_readFDSet;
277 timeval timeoutTimeVal;
278 timeoutTimeVal.tv_sec = timeoutMilliSeconds / 1000;
279 timeoutTimeVal.tv_usec = 1000 * (timeoutMilliSeconds % 1000);
280 int numReadyFDs = select(mImpl->_maxFDPlus1,
289 for (
unsigned short index = 0; numReadyFDs > 0; ++index)
292 bool isFDReadable = FD_ISSET(fdEntry->_fd, &readableFDSet);
293 bool isFDWritable = FD_ISSET(fdEntry->_fd, &writableFDSet);
294 bool isFDError = FD_ISSET(fdEntry->_fd, &errorFDSet);
295 if (isFDReadable || isFDWritable || isFDError)
309 mImpl->_waitResult.push_back(fdEntry);
314 return mImpl->_waitResult;
std::vector< Poll::FDEntry * > _fdEntryVector
std::map< int, Poll::FDEntry * > _fdEntryMap
std::vector< Poll::FDEntry * > _waitResult
FDEntry(Poll *poll, bool isTransport, int fd)
unsigned short StateBitMask
void setIsWritePending(bool isWritePending)
This class abstracts the Unix system call "poll".
const std::vector< Poll::FDEntry * > & wait(int timeoutMilliSeconds)
defined(RESIP_POLL_EXTERN) } {
static int findFDInWaitResult(int fd, const std::vector< Poll::FDEntry * > &waitResult)
dcm! – add UnusedChecking(_enum) below;