-
Notifications
You must be signed in to change notification settings - Fork 0
/
Poller.cc
92 lines (84 loc) · 2.41 KB
/
Poller.cc
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
#include"./Poller.h"
#include"./Channel.h"
#include"./EventLoop.h"
#include<poll.h>
#include<glog/logging.h>
Poller::Poller(EventLoop *loop)
:loop_(loop)
{
}
void Poller::poll(ActivityChannels *activityChannels)
{
int nEvents=::poll(&(*(pollfds_.begin())),pollfds_.size(),-1);
if(nEvents>0)
{
fillActivityChannels(nEvents,activityChannels);
}
else if(nEvents<0)
{
LOG(ERROR)<<"error in Poller::poll"<<std::endl;
}
}
/*
找出有活动的fd 填入channel
*/
void Poller::fillActivityChannels(int nEvents,ActivityChannels *activityChannels)
{
for(PollFdList::const_iterator c_it=pollfds_.begin();
c_it!=pollfds_.end();
c_it++){
if(c_it->revents)
{
ChannelMap::const_iterator ChannelMap_it=Channelmap_.find(c_it->fd);
assert(ChannelMap_it!=Channelmap_.end());
Channel *channel=ChannelMap_it->second;
assert(channel->fd_==c_it->fd);
channel->setRevents(c_it->revents);
activityChannels->push_back(channel);
}
}
}
/*
如果是新的channel 将它加入到map中 //
否则将map中的channel更新内容
*/
void Poller::updateChanels(Channel * channel)
{
if(channel->index<0)//new one 加入map和监听集struct pollfd
{
assert(Channelmap_.find(channel->fd_)==Channelmap_.end());
Channelmap_[channel->fd_]=channel;
struct pollfd pfd;
pfd.fd=channel->fd_;
pfd.revents=0;
pfd.events=static_cast<short>(channel->getEvents());
pollfds_.push_back(pfd);
channel->index=pollfds_.size()-1;
}
else
{
assert(Channelmap_.find(channel->fd_)!=Channelmap_.end());
assert(Channelmap_[channel->fd_]==channel);
Channelmap_[channel->fd_]=channel;
int idx=channel->index;
struct pollfd &pfd=pollfds_[idx];
pfd.events=static_cast<short>(channel->getEvents());
pfd.revents=0;
}
}
Poller::~Poller()
{
}
void Poller::removeChannel(Channel * channel)
{
assert(loop_->isInLoopThread());
assert(Channelmap_.find(channel->fd_)!=Channelmap_.end());
assert(Channelmap_[channel->fd_]==channel);
//从map里面删除
int idx=channel->index;
assert(idx>=0&&idx<pollfds_.size());
assert(Channelmap_.erase(channel->fd_)==1);
//从pollfdlist里面删除
std::iter_swap(pollfds_.begin()+idx,pollfds_.end());
pollfds_.pop_back();
}