10 #include <sys/timeb.h> 14 # include <winsock2.h> 15 static inline int poll(
struct pollfd *pfd,
int nfds,
int timeout)
17 return WSAPoll(pfd, nfds, timeout);
21 # if defined(_MSC_VER) 23 # define ftime _ftime_s 26 # include <sys/time.h> 58 if (it->getSource() == source)
71 if (it->getSource() == source)
73 it->getMask() = eventMask;
86 const unsigned POLLIN_REQ = POLLIN;
87 const unsigned POLLIN_CHK = (POLLIN | POLLHUP | POLLERR);
88 const unsigned POLLOUT_REQ = POLLOUT;
89 const unsigned POLLOUT_CHK = (POLLOUT | POLLERR);
90 const unsigned POLLEX_REQ = POLLPRI;
91 const unsigned POLLEX_CHK = (POLLPRI | POLLNVAL);
97 int timeout_ms =
static_cast<int>(floor(timeout * 1000.));
103 const unsigned source_cnt =
_sources.size();
104 pollfd fds[source_cnt];
107 SourceList::iterator it;
110 sources[i] = it->getSource();
111 fds[i].fd = sources[i]->
getfd();
114 if (it->getMask() &
ReadableEvent) fds[i].events |= POLLIN_REQ;
115 if (it->getMask() &
WritableEvent) fds[i].events |= POLLOUT_REQ;
116 if (it->getMask() &
Exception) fds[i].events |= POLLEX_REQ;
120 int nEvents = poll(fds, source_cnt, (timeout_ms < 0) ? -1 : timeout_ms);
125 XmlRpcUtil::error(
"Error in XmlRpcDispatch::work: error in poll (%d).", nEvents);
131 for (i=0; i < source_cnt; ++i)
134 pollfd & pfd = fds[i];
135 unsigned newMask = (unsigned) -1;
137 bool readable = (pfd.events & POLLIN_REQ) == POLLIN_REQ;
138 bool writable = (pfd.events & POLLOUT_REQ) == POLLOUT_REQ;
139 bool oob = (pfd.events & POLLEX_REQ) == POLLEX_REQ;
140 if (readable && (pfd.revents & POLLIN_CHK))
142 if (writable && (pfd.revents & POLLOUT_CHK))
144 if (oob && (pfd.revents & POLLEX_CHK))
150 SourceList::iterator thisIt;
153 if(thisIt->getSource() == src)
158 XmlRpcUtil::error(
"Error in XmlRpcDispatch::work: couldn't find source iterator");
166 }
else if (newMask != (
unsigned) -1) {
167 thisIt->getMask() = newMask;
176 for (SourceList::iterator it=closeList.begin(); it!=closeList.end(); ++it) {
211 for (SourceList::iterator it=closeList.begin(); it!=closeList.end(); ++it)
212 it->getSource()->close();
224 return ((
double) tbuff.time + ((
double)tbuff.millitm / 1000.0) +
225 ((
double) tbuff.timezone * 60));
230 return ((
double)sec + (
double)nsec / 1e9);
bool getKeepOpen() const
Return whether the file descriptor should be kept open if it is no longer monitored.
connected/data can be written without blocking
ROSTIME_DECL void ros_steadytime(uint32_t &sec, uint32_t &nsec)
void removeSource(XmlRpcSource *source)
static void error(const char *fmt,...)
Dump error messages somewhere.
void clear()
Clear all sources from the monitored sources list. Sources are closed.
int getfd() const
Return the file descriptor being monitored.
virtual void close()
Close the owned fd. If deleteOnClose was specified at construction, the object is deleted...
XmlRpcDispatch()
Constructor.
An RPC source represents a file descriptor to monitor.
void setSourceEvents(XmlRpcSource *source, unsigned eventMask)
Modify the types of events to watch for on this source.
void addSource(XmlRpcSource *source, unsigned eventMask)
out-of-band data has arrived
std::list< MonitoredSource > SourceList
void exit()
Exit from work routine.
virtual unsigned handleEvent(unsigned eventType)=0
Return true to continue monitoring this source.