-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChannel.cpp
More file actions
145 lines (125 loc) · 3.82 KB
/
Copy pathChannel.cpp
File metadata and controls
145 lines (125 loc) · 3.82 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "Channel.h"
#include "EventLoop.h"
#include <iostream>
#include <unistd.h>
#include <cerrno>
Channel::Channel(int fd, EventLoop *loop)
: fd_(fd)
, loop_(loop)
, events_(0)
{
}
Channel::~Channel() {
if (fd_ >= 0) {
::close(fd_);
fd_ = -1;
}
}
void Channel::setReadCallback(EventCallback cb) { readCallback_ = std::move(cb); }
void Channel::setWriteCallback(EventCallback cb) { writeCallback_ = std::move(cb); }
void Channel::setCloseCallback(EventCallback cb) { closeCallback_ = std::move(cb); }
void Channel::tie(const std::shared_ptr<void>& obj) {
tie_ = obj;
tied_ = true;
}
void Channel::enableReading() {
events_ |= EPOLLIN;
update();
}
void Channel::disableReading() {
events_ &= ~EPOLLIN;
update();
}
void Channel::enableWriting() {
events_ |= EPOLLOUT;
update();
}
void Channel::disableWriting() {
events_ &= ~EPOLLOUT;
update();
}
void Channel::update() {
loop_->updateChannel(this);
}
void Channel::handleEvent(uint32_t revents) {
// 生命周期守卫:回调链中可能触发关闭并从 owner 的 map 中移除本对象,
// 先把 weak_ptr 提升为 shared_ptr,保证 handleEvent 返回前对象不被销毁
std::shared_ptr<void> guard;
if (tied_) {
guard = tie_.lock();
if (!guard) return; // 对象已被销毁,忽略过期事件
}
if (revents & (EPOLLHUP | EPOLLERR)) {
handleClose();
return;
}
if (revents & EPOLLIN) {
if (readCallback_) readCallback_();
}
if (revents & EPOLLOUT) {
if (writeCallback_) writeCallback_();
}
}
void Channel::handleClose() {
if (closeCallback_) {
EventCallback cb;
std::swap(cb, closeCallback_);
if (cb) cb();
}
}
void Channel::sendData(const std::string& data) {
if (data.empty()) return;
// 输出缓冲区还有未发完的数据:新数据必须排队,保证发送顺序
if (!outputBuffer_.empty()) {
outputBuffer_.append(data.data(), data.size());
return;
}
size_t remaining = data.size();
const char* ptr = data.data();
while (remaining > 0) {
ssize_t n = ::send(fd_, ptr, remaining, MSG_NOSIGNAL);
if (n > 0) {
remaining -= static_cast<size_t>(n);
ptr += n;
continue;
}
if (n < 0 && errno == EINTR) continue;
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
// 内核发送缓冲区满:剩余数据进应用层缓冲,注册 EPOLLOUT 等可写事件
outputBuffer_.append(ptr, remaining);
enableWriting();
return;
}
// 真正的错误(EPIPE / ECONNRESET 等)
handleClose();
return;
}
}
void Channel::flushOutput() {
while (!outputBuffer_.empty()) {
ssize_t n = ::send(fd_, outputBuffer_.data(), outputBuffer_.size(), MSG_NOSIGNAL);
if (n > 0) {
outputBuffer_.retrieve(static_cast<size_t>(n));
continue;
}
if (n < 0 && errno == EINTR) continue;
if (n < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
return; // 缓冲区仍满,等下一次 EPOLLOUT
}
handleClose();
return;
}
disableWriting(); // 全部发完,取消对 EPOLLOUT 的关注
if (rejected_ || closeAfterFlush_) {
handleClose(); // 拒绝响应已全部送达,现在才真正关闭
}
}
void Channel::reject(const std::string& response) {
rejected_ = true;
disableReading(); // 冻结输入:摘掉 EPOLLIN,Buffer 从此封顶
sendData(response); // 尽力直发;内核缓冲满时剩余进输出缓冲区
if (outputBuffer().empty()) {
handleClose(); // 已全部发出,立即关闭
}
// 否则等 EPOLLOUT 冲刷完,由 flushOutput 触发 handleClose
}