Repository navigation
Expand file tree
/
Copy pathchannel.cpp
More file actions
71 lines (63 loc) · 1.69 KB
/
Copy pathchannel.cpp
File metadata and controls
71 lines (63 loc) · 1.69 KB
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
#include "channel.hpp"
#include "sys/epoll.h"
#include "Logger.hpp"
#include "eventLoop.hpp"
const int Channel::kNoneEvent = 0;
const int Channel::kReadEvent = EPOLLIN| EPOLLPRI;
const int Channel::kWriteEvent = EPOLLOUT;
Channel::Channel(EventLoop* loop, int fd):_loop(loop), _fd(fd), _events(0), _revents(0), _index(-1), _tied(false){}
Channel::~Channel(){}
//什么时候绑定的呢?? connection连接建立时候绑定
void Channel::tie(const shared_ptr<void>& obj){
_tie = obj;
_tied = true;
}
//通道自己是没有资格去epoll_ctl的,它要让loop去,code
void Channel::update(){
_loop->updateChannel(this);
}
//同理,不能自己删除自己,要让loop去删除自己这个管道 code
void Channel::remove(){
_loop->removeChannel(this);
}
void Channel::handleEvent(TimeStamp reciveTime){
if(_tied){
shared_ptr<void> guard = _tie.lock(); //提升不成功,说明这个管道已经remove掉了
if(guard){
handleEventWithGuard(reciveTime);
}
}else{
handleEventWithGuard(reciveTime);
}
}
void Channel::handleEventWithGuard(TimeStamp reciveTime){
LOG_INFO("channel handleEvent revents:%d\n", _revents);
if ((_revents & EPOLLHUP) && !(_revents & EPOLLIN))
{
if (_closeCallback)
{
_closeCallback();
}
}
if (_revents& EPOLLERR)
{
if (_errorCallback)
{
_errorCallback();
}
}
if (_revents & (EPOLLIN | EPOLLPRI))
{
if (_readCallback)
{
_readCallback(reciveTime);
}
}
if (_revents & EPOLLOUT)
{
if (_writeCallback)
{
_writeCallback();
}
}
}