#ifndef __JNET__JSELECTWRITER__ #define __JNET__JSELECTWRITER__ #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 JSelectWriter { public: /** * Default constructor. */ JSelectWriter() {} /** * Get writer mask. * * \return mask */ JFileDescriptorMask& getWriterMask() { return writerMask; } /** * Set writer mask. * * \param file file */ void setWriterMask(const JAbstractFile& file) { writerMask.set(file); } /** * Has writer file. * * \param file file * \return true if file set; else false */ bool hasWriterMask(const JAbstractFile& file) const { return writerMask.has(file); } /** * Reset. */ void reset() { writerMask.reset(true); // hard reset } /** * Select call. * * \param timeout timeout * \return number of valid file descriptors */ int 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 = writerMask.getNumberOfFileDescriptors(); nfds += 1; nfds = ::select(nfds, NULL, &writerMask, NULL, timeout); if (nfds != 0) { if (nfds < 0) { if (errno != EINTR) { THROW(JSelectException, "JSelectWriter(..) failed."); } } } else { writerMask.reset(false); // soft reset } return nfds; } protected: JFileDescriptorMask writerMask; }; } #endif