Jpp  17.2.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JSelect.hh
Go to the documentation of this file.
1 #ifndef __JNET__JSELECT__
2 #define __JNET__JSELECT__
3 
4 #include <algorithm>
5 #include <sys/select.h>
6 #include <limits>
7 #include <errno.h>
8 
9 #include "JLang/JException.hh"
11 #include "JLang/JAbstractFile.hh"
12 #include "JLang/JTimeval.hh"
13 
14 #include "JNet/JSelectReader.hh"
15 #include "JNet/JSelectWriter.hh"
16 
17 
18 /**
19  * \author mdejong
20  */
21 
22 namespace JNET {}
23 namespace JPP { using namespace JNET; }
24 
25 namespace JNET {
26 
27  using JLANG::JTimeval;
29 
30 
31  /**
32  * Wrapper class for select call.
33  */
34  class JSelect :
35  public JSelectReader,
36  public JSelectWriter
37  {
38  public:
39  /**
40  * Default constructor.
41  */
42  JSelect() :
43  JSelectReader(),
45  {}
46 
47 
48  /**
49  * Reset.
50  */
51  void reset()
52  {
53  static_cast<JSelectReader&>(*this).reset();
54  static_cast<JSelectWriter&>(*this).reset();
55  }
56 
57 
58  /**
59  * Infinite wait select call.
60  *
61  * \return true if data can be read or written; else false
62  */
63 
64  bool operator ()()
65  {
66  return this->operator ()(0);
67  }
68 
69 
70  /**
71  * Select call.
72  *
73  * \param timeout timeout
74  * \return true if data can be read or written; else false
75  */
76  bool operator()(JTimeval timeout)
77  {
78  return this->operator ()(static_cast<JTimeval*>(
79  static_cast<void*>(&timeout)));
80  }
81 
82 
83  /**
84  * Infinite wait select call.
85  *
86  * \return number of valid file descriptors
87  */
88  int operator()(JTimeval* timeout)
89  {
90  int nfds = std::max(readerMask.getNumberOfFileDescriptors(),
92 
93  nfds += 1;
94  nfds = ::select(nfds, &readerMask, &writerMask, NULL, timeout->get());
95 
96  if (nfds != 0) {
97 
98  if (nfds < 0) {
99  if (errno != EINTR) {
100  THROW(JSelectException, "JSelect(..) failed.");
101  }
102  }
103 
104  } else {
105 
106  readerMask.reset(false); // soft reset
107  writerMask.reset(false); // soft reset
108  }
109 
110  return nfds;
111  }
112  };
113 }
114 
115 #endif
Exceptions.
Wrapper class for select call.
Definition: JSelect.hh:34
JFileDescriptorMask writerMask
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:696
void reset()
Reset.
bool operator()()
Infinite wait select call.
Definition: JSelect.hh:64
Exception for select call.
Definition: JException.hh:432
Auxiliary class for time values.
Definition: JTimeval.hh:26
void reset()
Reset.
Definition: JSelect.hh:51
void reset(const bool option=true)
Reset mask.
int operator()(JTimeval *timeout)
Infinite wait select call.
Definition: JSelect.hh:88
Wrapper class for select call.
int getNumberOfFileDescriptors() const
Get number of file descriptors.
void reset()
Reset.
bool operator()(JTimeval timeout)
Select call.
Definition: JSelect.hh:76
const timeval * get() const
Get pointer to time value.
Definition: JTimeval.hh:143
JFileDescriptorMask readerMask
JSelect()
Default constructor.
Definition: JSelect.hh:42
Wrapper class for select call.