-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChannel.cpp
81 lines (68 loc) · 1.67 KB
/
Channel.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
//
// Created by 12096 on 2022/9/9.
//
#include "Channel.h"
#include "EventLoop.h"
#include "Logger.h"
#include <sys/epoll.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)
, tied_(false)
{
}
Channel::~Channel() {
}
//
void Channel::tie(const std::shared_ptr<void> &obj) {
tie_ = obj;
tied_ = true;
}
// 通过Eventloop的Poller模块注册fd的events事件
void Channel::update() {
loop_->updateChannel(this);
}
// 在Eventloop的Channel列表中删除本Channel
void Channel::remove() {
loop_->removeChannel(this);
}
void Channel::handleEvent(TimeStamp receiveTime) {
if (tied_) {
std::shared_ptr<void> guard = tie_.lock();
if (guard) { // 如果tie_指向的内存没有被销毁
handleEventWithGuard(receiveTime);
}
}
else {
handleEventWithGuard(receiveTime);
}
}
void Channel::handleEventWithGuard(TimeStamp receiveTime) {
// LOG_INFO("channel handleEvent revents:%d\n", revents_);
if ((revents_ & EPOLLHUP)) {
if (closeCallback_) {
closeCallback_();
}
}
if (revents_ & EPOLLERR) {
if (errorCallback_) {
errorCallback_();
}
}
if (revents_ & EPOLLIN) {
if (readCallback_) {
readCallback_(receiveTime);
}
}
if (revents_ & EPOLLOUT) {
if (writeCallback_) {
writeCallback_();
}
}
}