ESA JPIP server  0.1
poll_table.h
Go to the documentation of this file.
1 #ifndef _NET_POLL_TABLE_H_
2 #define _NET_POLL_TABLE_H_
3 
4 
5 #include <vector>
6 #include <poll.h>
7 #include <algorithm>
8 
9 
10 namespace net
11 {
12  using namespace std;
13 
14 
21  struct PollFD: pollfd
22  {
28  PollFD(int vfd, int mask)
29  {
30  fd = vfd;
31  events = mask;
32  revents = 0;
33  }
34 
39  bool operator==(int n)
40  {
41  return (fd == n);
42  }
43  };
44 
45 
54  class PollTable
55  {
56  private:
60  vector<PollFD> fds;
61 
62  public:
64  {
65  }
66 
72  void Add(int fd, int mask)
73  {
74  fds.push_back(PollFD(fd, mask));
75  }
76 
83  int Poll(int timeout = -1)
84  {
85  return poll(&(fds[0]), (int)fds.size(), timeout);
86  }
87 
91  int GetSize() const
92  {
93  return fds.size();
94  }
95 
101  void Remove(int fd)
102  {
103  vector<PollFD>::iterator i = find(fds.begin(), fds.end(), fd);
104  if(i != fds.end()) fds.erase(i);
105  }
106 
112  void RemoveAt(int n)
113  {
114  fds.erase(fds.begin() + n);
115  }
116 
121  {
122  return fds[n];
123  }
124 
125  virtual ~PollTable()
126  {
127  }
128  };
129 
130 }
131 
132 #endif /* _NET_POLL_TABLE_H_ */
This class allows to perfom polls easily over a vector of descriptors.
Definition: poll_table.h:54
int Poll(int timeout=-1)
Peforms a poll over all the descriptors using the associated masks.
Definition: poll_table.h:83
STL namespace.
int GetSize() const
Returns the size of the internal vector.
Definition: poll_table.h:91
PollFD(int vfd, int mask)
Initializes the structure.
Definition: poll_table.h:28
bool operator==(int n)
Returns true if the file descriptor is the same as the given value.
Definition: poll_table.h:39
void Remove(int fd)
Removes an item of the internal vector giving its file descriptor.
Definition: poll_table.h:101
Contains classes to easy the utilization of sockets, specially implemented for UNIX systems...
Definition: address.h:15
PollTable()
Definition: poll_table.h:63
void Add(int fd, int mask)
Adds a new file descriptor and mask to the vector.
Definition: poll_table.h:72
vector< PollFD > fds
Vector with the file descriptors and masks for polling.
Definition: poll_table.h:60
PollFD & operator[](int n)
Indexing operator.
Definition: poll_table.h:120
void RemoveAt(int n)
Remove an item of the internal vector giving its index position.
Definition: poll_table.h:112
virtual ~PollTable()
Definition: poll_table.h:125
Wrapper structure for the structure pollfd used by the kernel poll functions.
Definition: poll_table.h:21