-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTcpConn.cpp
executable file
·246 lines (242 loc) · 8.77 KB
/
TcpConn.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <poll.h>
#include <fcntl.h>
#include "TcpConn.h"
using namespace std;
using namespace netlib;
namespace netlib{
//执行初始化工作
void TcpConn::attach(EventBase* base, int fd, Ip4Addr local, Ip4Addr peer)
{
if((destPort_<=0 && state_ != State::Invalid) || (destPort_>=0 && state_ != State::Handshaking))
CHEN_LOG(ERROR,"you should use a new TcpConn to attach. state: %d", state_);
base_ = base;
state_ = State::Handshaking;
local_ = local;
peer_ = peer;
if (channel_) { delete channel_; }
channel_ = new Channel(base, fd, kWriteEvent|kReadEvent);
CHEN_LOG(DEBUG,"tcp constructed %s - %s fd: %d",
local_.toString().c_str(),
peer_.toString().c_str(),
fd);
TcpConnPtr con = shared_from_this();
con->channel_->onRead([=] { con->handleRead(con); });
con->channel_->onWrite([=] { con->handleWrite(con); });
}
void TcpConn::connect(EventBase* base, const string& host, short port,
int timeout, const string& localip) {
if(state_ != State::Invalid && state_ != State::Closed && state_ != State::Failed)
CHEN_LOG(ERROR,"current state is bad state to connect. state: %d", state_);
destHost_ = host;
destPort_ = port;
connectTimeout_ = timeout;
connectedTime_ = sysutil::timeMilli();
localIp_ = localip;
Ip4Addr addr(host, port);
int fd = socket(AF_INET, SOCK_STREAM, 0);
if(fd<0)
CHEN_LOG(ERROR,"socket failed");
sysutil::setNonBlock(fd);
int t = sysutil::addFdFlag(fd, FD_CLOEXEC);
if(t != 0)
CHEN_LOG(ERROR,"addFdFlag FD_CLOEXEC failed %d", t);
int r = 0;
if (localip.size()) {
Ip4Addr addr(localip, 0);
r = ::bind(fd,(struct sockaddr *)&addr.getAddr(),sizeof(struct sockaddr));
CHEN_LOG(ERROR,"bind to %s failed error", addr.toString().c_str());
}
if (r == 0) {
r = ::connect(fd, (sockaddr *) &addr.getAddr(), sizeof(sockaddr_in));
if (r != 0 && errno != EINPROGRESS) {
CHEN_LOG(ERROR,"connect to %s error", addr.toString().c_str());
}
}
sockaddr_in local;
socklen_t alen = sizeof(local);
if (r == 0) {
r = getsockname(fd, (sockaddr *) &local, &alen);
if (r < 0) {
CHEN_LOG(ERROR,"getsockname failed ");
}
}
state_ = State::Handshaking;
attach(base, fd, Ip4Addr(local), addr);
if (timeout) {
TcpConnPtr con = shared_from_this();
timeoutId_ = base->runAfter(timeout, [con] {
if (con->getState() == Handshaking) { con->channel_->close(); }
});
}
}
//关闭连接
void TcpConn::close() {
if (channel_) {
TcpConnPtr con = shared_from_this();
getBase()->safeCall([con]{ if (con->channel_) con->channel_->close(); });
}
}
//清理连接资源
void TcpConn::cleanup(const TcpConnPtr& con) {
if (readcb_ && input_.size()) {
CHEN_LOG(INFO,"cleanup --- read");
readcb_(con);
}
if (state_ == State::Handshaking) {
state_ = State::Failed;
} else {
state_ = State::Closed;
}
CHEN_LOG(DEBUG,"tcp closing %s - %s fd %d %d",
local_.toString().c_str(),
peer_.toString().c_str(),
channel_ ? channel_->fd(): -1, errno);
if(timeoutId_ > -1)
getBase()->cancel(timeoutId_);
if (statecb_) {
statecb_(con);
}
// if (reconnectInterval_ >= 0 && !getBase()->exited()) { //reconnect
// reconnect();
// return;
// }
//channel may have hold TcpConnPtr, set channel_ to NULL before delete
readcb_ = writablecb_ = statecb_ = nullptr;
Channel* ch = channel_;
channel_ = NULL;
delete ch;
}
//读取数据并执行相应回调事件
void TcpConn::handleRead(const TcpConnPtr& con) {
if (state_ == State::Handshaking && handleHandshake(con)) {
return;
}
while(state_ == State::Connected) {
input_.makeRoom();
int rd = 0;
if (channel_->fd() >= 0) {
rd = readImp(channel_->fd(), input_.end(), input_.space());
CHEN_LOG(DEBUG,"channel %lld fd %d readed %d bytes", (long long)channel_->id(),
channel_->fd(), rd);
}
if (rd == -1 && errno == EINTR) {
continue;
} else if (rd == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) ) {
if (readcb_ && input_.size()) {
CHEN_LOG(DEBUG,"read zero---");
readcb_(con);
}
break;
} else if (channel_->fd() == -1 || rd == 0 || rd == -1) {
CHEN_LOG(DEBUG,"cleaning conn");
cleanup(con); //说明此时连接已断开
break;
} else { //rd > 0
input_.addSize(rd);
}
}
}
//在初始化TcpConn时状态会被设为Handshaking,读写时发现还是Handshaking就会执行此函数
int TcpConn::handleHandshake(const TcpConnPtr& con) {
if(state_ != Handshaking)
CHEN_LOG(ERROR,"handleHandshaking called when state_=%d", state_);
struct pollfd pfd;
pfd.fd = channel_->fd();
pfd.events = POLLOUT | POLLERR;
int r = poll(&pfd, 1, 0);
if (r == 1 && pfd.revents == POLLOUT) {
channel_->enableReadWrite(true, false);
state_ = State::Connected;
if (state_ == State::Connected) {
connectedTime_ = sysutil::timeMilli();
CHEN_LOG(DEBUG,"tcp connected %s - %s fd %d",
local_.toString().c_str(), peer_.toString().c_str(), channel_->fd());
if (statecb_) {
CHEN_LOG(DEBUG,"call statecb---------------------");
statecb_(con);
}
}
} else {
CHEN_LOG(DEBUG,"poll fd %d return %d revents %d", channel_->fd(), r, pfd.revents);
cleanup(con);
return -1;
}
return 0;
}
void TcpConn::handleWrite(const TcpConnPtr& con) {
if (state_ == State::Handshaking) {
handleHandshake(con);
} else if (state_ == State::Connected) {
ssize_t sended = isend(output_.begin(), output_.size()); //发送缓冲区的数据
output_.consume(sended);
if (output_.empty() && writablecb_) {
writablecb_(con);
}
if (output_.empty() && channel_->writeEnabled()) { // writablecb_ may write something
channel_->enableWrite(false);
}
} else {
CHEN_LOG(ERROR,"handle write unexpected");
}
}
ssize_t TcpConn::isend(const char* buf, size_t len) {
size_t sended = 0;
while (len > sended) {
ssize_t wd = writeImp(channel_->fd(), buf + sended, len - sended);
CHEN_LOG(DEBUG,"channel %lld fd %d write %ld bytes", (long long)channel_->id(), channel_->fd(), wd);
if (wd > 0) {
sended += wd;
continue;
} else if (wd == -1 && errno == EINTR) {
continue;
} else if (wd == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
if (!channel_->writeEnabled()) {
channel_->enableWrite(true);
}
break;
} else {
CHEN_LOG(ERROR,"write error: channel %lld fd %d wd %ld",
(long long)channel_->id(), channel_->fd(), wd);
break;
}
}
return sended;
}
//发送缓冲中的数据
void TcpConn::send(Buffer& buf) {
if (channel_) {
if (channel_->writeEnabled()) { //just full
output_.absorb(buf);
}
if (buf.size()) {
ssize_t sended = isend(buf.begin(), buf.size());
buf.consume(sended);
}
if (buf.size()) {
output_.absorb(buf);
if (!channel_->writeEnabled()) {
channel_->enableWrite(true);
}
}
} else {
CHEN_LOG(WARN,"connection %s - %s closed, but still writing %lu bytes",
local_.toString().c_str(), peer_.toString().c_str(), buf.size());
}
}
//output_为空就直接写fd,写不完就写到缓冲区里
void TcpConn::send(const char* buf, size_t len) {
if (channel_) {
if (output_.empty()) {
ssize_t sended = isend(buf, len);
buf += sended;
len -= sended;
}
if (len) {
output_.append(buf, len);
}
} else {
CHEN_LOG(WARN,"connection %s - %s closed, but still writing %lu bytes",
local_.toString().c_str(), peer_.toString().c_str(), len);
}
}
}