forked from lizhenghn123/CppLanguagePrograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChannel.cpp
More file actions
94 lines (83 loc) · 1.9 KB
/
Channel.cpp
File metadata and controls
94 lines (83 loc) · 1.9 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include "net/Channel.h"
#include <sstream>
#include <assert.h>
#include "base/Logger.h"
#include "net/EventLoop.h"
NAMESPACE_ZL_NET_START
Channel::Channel(EventLoop* loop, int fd)
: loop_(loop)
, fd_(fd)
, events_(0)
, revents_(0)
{
}
Channel::~Channel()
{
if (loop_->isInLoopThread())
{
assert(!loop_->hasChannel(this));
}
}
void Channel::update()
{
loop_->updateChannel(this);
}
void Channel::remove()
{
assert(isNoneEvent());
loop_->removeChannel(this);
}
void Channel::handleEvent(Timestamp receiveTime)
{
handleEventWithHold(receiveTime);
}
void Channel::handleEventWithHold(Timestamp receiveTime)
{
if ((revents_ & FDEVENT_HUP) && !(revents_ & FDEVENT_IN))
{
LOG_INFO("Channel::handleEventWithHold closeCallback, fd[%d]", fd_);
if (closeCallback_)
closeCallback_();
}
if (revents_ & FDEVENT_NVAL)
{
LOG_WARN("Channel::handle_event() POLLNVAL, fd[%d]", fd_);
}
if (revents_ & (FDEVENT_ERR | FDEVENT_NVAL))
{
LOG_INFO("Channel::handleEventWithHold closeCallback, fd[%d]", fd_);
if (errorCallback_)
errorCallback_();
}
if (revents_ & kEventRead)
{
if (readCallback_)
readCallback_(receiveTime);
}
if (revents_ & FDEVENT_OUT)
{
if (writeCallback_)
writeCallback_();
}
}
std::string Channel::reventsToString() const
{
std::ostringstream oss;
oss << fd_ << ": ";
if (revents_ & FDEVENT_IN)
oss << "IN ";
if (revents_ & FDEVENT_PRI)
oss << "PRI ";
if (revents_ & FDEVENT_OUT)
oss << "OUT ";
if (revents_ & FDEVENT_HUP)
oss << "HUP ";
if (revents_ & FDEVENT_RDHUP)
oss << "RDHUP ";
if (revents_ & FDEVENT_ERR)
oss << "ERR ";
if (revents_ & FDEVENT_NVAL)
oss << "NVAL ";
return oss.str().c_str();
}
NAMESPACE_ZL_NET_END