#ifndef __JNET__JSELECTREADER__ #define __JNET__JSELECTREADER__ #include #include #include #include "JLang/JException.hh" #include "JLang/JFileDescriptorMask.hh" #include "JLang/JAbstractFile.hh" #include "JLang/JTimeval.hh" /** * \author mdejong */ namespace JNET {} namespace JPP { using namespace JNET; } namespace JNET { using JLANG::JSelectException; using JLANG::JFileDescriptorMask; using JLANG::JAbstractFile; using JLANG::JTimeval; /** * Wrapper class for select call. */ class JSelectReader { public: /** * Default constructor. */ JSelectReader() {} /** * Get reader mask. * * \return mask */ JFileDescriptorMask& getReaderMask() { return readerMask; } /** * Set reader mask. * * \param file file */ void setReaderMask(const JAbstractFile& file) { readerMask.set(file); } /** * Has reader file. * * \param file file * \return true if file set; else false */ bool hasReaderMask(const JAbstractFile& file) const { return readerMask.has(file); } /** * Reset. */ void reset() { readerMask.reset(true); // hard reset } /** * Select call. * * \param timeout timeout * \return number of valid file descriptors */ bool operator()(JTimeval timeout) { return this->operator()(&timeout); } /** * Select call. * * \param timeout timeout * \return number of valid file descriptors */ int operator()(timeval* timeout = NULL) { int nfds = readerMask.getNumberOfFileDescriptors(); nfds += 1; nfds = ::select(nfds, &readerMask, NULL, NULL, timeout); if (nfds != 0) { if (nfds < 0) { if (errno != EINTR) { THROW(JSelectException, "JSelectReader(..) failed."); } } } else { readerMask.reset(false); // soft reset } return nfds; } protected: JFileDescriptorMask readerMask; }; } #endif