-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoll.cpp
68 lines (60 loc) · 1.86 KB
/
poll.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "poll.h"
#include "exception.h"
#include "log.h"
#include "message.h"
namespace {
inline struct pollfd make_pollfd(const MessageBase* msg) {
struct pollfd pollfd;
memset(&pollfd, 0, sizeof(pollfd));
pollfd.fd = msg->fd();
pollfd.events = msg->events();
return pollfd;
}
}
void Poll::add(MessageBase* msg) {
if(msg == 0) return;
int fd = msg->fd();
Queue::iterator result = queue_.insert(fd, msg);
outdated_ = true;
}
void Poll::remove(const MessageBase* msg) {
if(msg == 0) return;
boost::iterator_range<Queue::iterator> range = queue_.equal_range(msg->fd());
for(Queue::iterator it = range.begin(); it != range.end(); ++it) {
if(it->second == msg) {
it = queue_.erase(it);
outdated_ = true;
break;
}
}
}
void Poll::perform() {
if(outdated_) {
Pollfd poll;
for(Queue::const_iterator it = queue_.begin(); it != queue_.end(); ++it) {
poll.push_back(make_pollfd(it->second));
}
poll_.swap(poll);
outdated_ = false;
}
while(!(poll_.empty() || poll()));
}
bool Poll::poll() {
Pollfd poll(poll_);
int ready = 0;
CHECK_CALL((ready = ::poll(&poll[0], poll.size(), -1)), "poll");
for(Pollfd::iterator it = poll.begin(); it != poll.end() && ready > 0; ++it) {
if(it->revents == 0) continue;
--ready;
BOOST_FOREACH(Queue::value_type v, queue_.equal_range(it->fd)) {
MessageBase& msg = *v.second;
if((it->revents & POLLERR) == POLLERR) msg.error();
if((it->revents & POLLHUP) == POLLHUP) msg.hangup();
if((it->revents & POLLIN) == POLLIN) msg.read();
if((it->revents & POLLOUT) == POLLOUT) msg.write();
if(msg.is_ready()) ready_.push_back(&msg);
}
it->revents = 0;
}
return !ready_.empty();
}