-
Notifications
You must be signed in to change notification settings - Fork 0
/
poller.cpp
executable file
·94 lines (88 loc) · 3.04 KB
/
poller.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "event_base.h"
#include "logging.h"
#include "sysutil.h"
#include <iostream>
#include <set>
#include <string.h>
#include <fcntl.h>
#include "poller.h"
#include "common.h"
namespace netlib {
PollerEpoll::PollerEpoll(){
fd_ = epoll_create1(EPOLL_CLOEXEC); //当调用exec成功后,此fd自动关闭
if(fd_<0)
CHEN_LOG(ERROR,"create epoll error");
CHEN_LOG(DEBUG,"poller epoll %d created", fd_);
}
PollerEpoll::~PollerEpoll() {
CHEN_LOG(DEBUG,"destroying poller %d", fd_);
while (liveChannels_.size()) {
(*liveChannels_.begin())->close();
}
::close(fd_);
CHEN_LOG(DEBUG,"poller %d destroyed", fd_);
}
void PollerEpoll::addChannel(Channel* ch) {
struct epoll_event ev;
memset(&ev, 0, sizeof(ev));
ev.events = ch->events();
ev.data.ptr = ch;
CHEN_LOG(DEBUG,"adding channel %lld fd %d events %d epoll %d",
(long long)ch->id(), ch->fd(), ev.events, fd_);
int r = epoll_ctl(fd_, EPOLL_CTL_ADD, ch->fd(), &ev);
if(r != 0)
CHEN_LOG(ERROR, "epoll_ctl add failed ");
liveChannels_.insert(ch);
}
void PollerEpoll::updateChannel(Channel* ch) {
struct epoll_event ev;
memset(&ev, 0, sizeof(ev));
ev.events = ch->events();
ev.data.ptr = ch;
CHEN_LOG(DEBUG,"modifying channel %lld fd %d events read %d write %d epoll %d",
(long long)ch->id(), ch->fd(), ev.events & POLLIN, ev.events & POLLOUT, fd_);
int r = epoll_ctl(fd_, EPOLL_CTL_MOD, ch->fd(), &ev);
if(r != 0)
CHEN_LOG(ERROR,"epoll_ctl mod failed");
}
void PollerEpoll::removeChannel(Channel* ch) {
CHEN_LOG(DEBUG,"deleting channel %lld fd %d epoll %d",
(long long)ch->id(), ch->fd(), fd_);
liveChannels_.erase(ch);
for (int i = lastActive_; i >= 0; i --) {
if (ch == activeEvs_[i].data.ptr) {
activeEvs_[i].data.ptr = NULL;
break;
}
}
}
void PollerEpoll::loop_once(int waitMs) {
int64_t ticks = sysutil::timeMilli();
lastActive_ = epoll_wait(fd_, activeEvs_, kMaxEvents, waitMs);
int64_t used = sysutil::timeMilli()-ticks;
// CHEN_LOG(DEBUG,"epoll result:%d",lastActive_);
// CHEN_LOG(DEBUG,"epoll wait %d return %d errno %d used %lld millsecond",
// waitMs, lastActive_, errno, (long long)used);
if(lastActive_ == -1 && errno != EINTR)
CHEN_LOG(ERROR, "epoll return error %d %s");
while (--lastActive_ >= 0) {
int i = lastActive_;
Channel* ch = (Channel*)activeEvs_[i].data.ptr;
int events = activeEvs_[i].events;
if (ch) {
if (events & (kReadEvent )) {
CHEN_LOG(DEBUG,"channel %lld fd %d handle read",
(long long)ch->id(), ch->fd());
ch->handleRead();
} else if (events & kWriteEvent) {
CHEN_LOG(DEBUG,"channel %lld fd %d handle write",
(long long)ch->id(), ch->fd());
ch->handleWrite();
} else {
CHEN_LOG(ERROR,"unexpected poller events");
}
}
}
lastActive_ = 0;
}
}