ANNA Suite  2020b
Multipurpose development suite for Telco applications
Poll.hpp
Go to the documentation of this file.
1 // ANNA - Anna is Not Nothingness Anymore //
2 // //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
4 // //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7 
8 
9 #ifndef anna_comm_internal_Poll_hpp
10 #define anna_comm_internal_Poll_hpp
11 
12 #include <unistd.h>
13 #include <sys/time.h>
14 #include <limits.h>
15 
16 #include <anna/config/defines.hpp>
19 #include <anna/core/functions.hpp>
20 
21 namespace anna {
22 
23 namespace comm {
24 
31 class Poll {
32 public:
33  Poll() : a_ptrTimeout(NULL), a_minfd(INT_MAX), a_maxfd(0) {
34  FD_ZERO(&a_fdmask);
35  FD_ZERO(&a_fdset);
36  }
37 
38  void setTimeout(const Millisecond &timeout) ;
39 
40  void waitMessage() noexcept(false);
41  int fetch() ;
42  bool isEmpty() const { return a_pollr <= 0; }
43  void clear() { a_maxfd = 0; a_minfd = INT_MAX; FD_ZERO(&a_fdmask); FD_ZERO(&a_fdset);}
44  void insert(const int fd) ;
45  void erase(const int fd) ;
46 
47 private:
48  fd_set a_fdmask;
49  fd_set a_fdset;
50  timeval* a_ptrTimeout;
51  int a_minfd;
52  int a_maxfd;
53  int a_pollr;
54  int a_ifd;
55  timeval a_timeout;
56 
57  static int select(const int maxfd, fd_set* fdset, timeval* timeout)
58  noexcept(false) {
59  int result;
60 
61  do {
62  result = ::select(maxfd + 1, fdset, NULL, NULL, timeout);
63  } while(result == -1 && errno == EINTR);
64 
65  if(result == -1) {
66  int xerrno = errno;
67  throw RuntimeException("Error en anna::comm::Poll::select", xerrno, ANNA_FILE_LOCATION);
68  }
69 
70  return result;
71  }
72 
73  static int min(const Millisecond &n1, const Millisecond &n2) { return (n1 < n2) ? n1 : n2; }
74 };
75 
76 }
77 }
78 
79 #endif
void setTimeout(const Millisecond &timeout)
Definition: Millisecond.hpp:24
void waitMessage() noexcept(false)
void insert(const int fd)
void clear()
Definition: Poll.hpp:43
void erase(const int fd)
Definition: Poll.hpp:31
bool isEmpty() const
Definition: Poll.hpp:42
Poll()
Definition: Poll.hpp:33
Definition: app.hpp:12
#define ANNA_FILE_LOCATION
Definition: defines.hpp:23
Definition: RuntimeException.hpp:23