-
Notifications
You must be signed in to change notification settings - Fork 0
/
Channel.cc
48 lines (41 loc) · 1.21 KB
/
Channel.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
#include "Channel.h"
#include "EventLoop.h"
#include "logging/Logging.h"
#include <poll.h>
using namespace muduo;
const int Channel::kNoneEvent = 0;
const int Channel::kReadEvent = POLLIN | POLLPRI;
const int Channel::kWriteEvent = POLLOUT;
Channel::Channel(EventLoop *loop, int fd)
: loop_(loop), fd_(fd), events_(0), revents_(0), index_(-1),
eventHanding_(false) {}
Channel::~Channel() { assert(!eventHanding_); }
void Channel::update() { loop_->updateChannel(this); }
void Channel::handleEvent(Timestamp receiveTime) {
eventHanding_ = true;
if (revents_ & POLLNVAL) {
LOG_WARN << "Channel::handle_event() POLLNVAL";
}
if (revents_ & (POLLERR | POLLNVAL)) {
if (errorCallback_) {
errorCallback_();
}
}
if ((revents_ & POLLHUP) && !(revents_ & POLLIN)) {
LOG_WARN << "Channel::handle_event() POLLHUP";
if (closeCallback_) {
closeCallback_();
}
}
if (revents_ & (POLLIN | POLLPRI | POLLRDHUP)) {
if (readCallback_) {
readCallback_(receiveTime);
}
}
if (revents_ & POLLOUT) {
if (writeCallback_) {
writeCallback_();
}
}
eventHanding_ = false;
}