forked from L1w-Y/muduo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChannel.cc
More file actions
57 lines (49 loc) · 1.23 KB
/
Copy pathChannel.cc
File metadata and controls
57 lines (49 loc) · 1.23 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
#include "Channel.h"
#include<sys/epoll.h>
#include"EventLoop.h"
#include"Logger.h"
#include"Timestamp.h"
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)
{
}
Channel::~Channel(){
}
void Channel::tie(const std::shared_ptr<void>&obj){
tie_=obj;
tied_=true;
}
void Channel::handleEvent(Timestamp receiveTime){
if(tied_){
auto ptr = tie_.lock();
if(ptr){
handleEventWithGuard(receiveTime);
}
}else{
handleEventWithGuard(receiveTime);
}
}
void Channel::update(){
loop_->updateChannel(this);
}
void Channel::handleEventWithGuard(Timestamp recieveTime){
//关闭写端会触发epollhup事件
if((revents_ & EPOLLHUP)&&!(revents_ & EPOLLIN)){
if(closeCallback_)closeCallback_();
}
if(revents_ & EPOLLERR){
if(errorCallback_)errorCallback_();
}
if(revents_&(EPOLLIN | EPOLLPRI)){
if(readCallback_)readCallback_(recieveTime);
}
if(revents_ & EPOLLOUT){
if(writeCallback_)writeCallback_();
}
}
void Channel::remove(){
loop_->removeChannel(this);
}