forked from lizhenghn123/CppLanguagePrograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTcpConnection.cpp
More file actions
262 lines (236 loc) · 7.81 KB
/
TcpConnection.cpp
File metadata and controls
262 lines (236 loc) · 7.81 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "TcpConnection.h"
#include "net/Socket.h"
#include "base/Logger.h"
#include "net/EventLoop.h"
#include "net/Channel.h"
NAMESPACE_ZL_NET_START
void defaultConnectionCallback(const TcpConnectionPtr& conn)
{
LOG_INFO("defaultConnectionCallback : [%s]<->[%s] [%s]\n", conn->localAddress().ipPort().c_str(),
conn->peerAddress().ipPort().c_str(), conn->connected() ? "UP" : "DOWN");
}
void defaultMessageCallback(const TcpConnectionPtr& conn, NetBuffer* buf, Timestamp receiveTime)
{
LOG_INFO("defaultMessageCallback : [%d][%s]", conn->fd(), buf->toString().c_str());
}
TcpConnection::TcpConnection(EventLoop* loop, int sockfd, const InetAddress& localAddr, const InetAddress& peerAddr)
: loop_(loop)
, state_(kConnecting)
, localAddr_(localAddr.getSockAddrInet())
, peerAddr_(peerAddr.getSockAddrInet())
{
socket_ = new Socket(sockfd);
socket_->setKeepAlive(true);
socket_->setNoDelay(true);
socket_->setNonBlocking();
channel_ = new Channel(loop, sockfd);
channel_->setReadCallback(std::bind(&TcpConnection::handleRead, this, std::placeholders::_1));
channel_->setWriteCallback(std::bind(&TcpConnection::handleWrite, this));
channel_->setCloseCallback(std::bind(&TcpConnection::handleClose, this));
channel_->setErrorCallback(std::bind(&TcpConnection::handleError, this));
LOG_INFO("TcpConnection::TcpConnection(), [%0x] [%d][%0x][%0x]", this, socket_->fd(), socket_, channel_);
}
TcpConnection::~TcpConnection()
{
LOG_INFO("TcpConnection::~TcpConnection(),[%0x] [%d][%0x][%0x]", this, socket_->fd(), socket_, channel_);
ZL_ASSERT(state_ == kDisconnected)(state_);
Safe_Delete(socket_);
Safe_Delete(channel_);
}
void TcpConnection::send(const void* data, size_t len)
{
if (state_ == kConnected)
{
if (loop_->isInLoopThread())
{
sendInLoop(data, len);
}
else
{
loop_->runInLoop(std::bind(&TcpConnection::sendInLoop, this, data, len));
}
}
}
void TcpConnection::send(const std::string& buffer)
{
send(buffer.data(), buffer.size());
}
void TcpConnection::send(NetBuffer* buffer)
{
if (state_ == kConnected)
{
if (loop_->isInLoopThread())
{
sendInLoop(buffer->peek(), buffer->readableBytes());
buffer->retrieveAll();
}
else
{
loop_->runInLoop(std::bind(&TcpConnection::sendInLoop2, shared_from_this(), buffer->retrieveAllAsString()));
}
}
}
void TcpConnection::sendInLoop(const void* data, size_t len)
{
loop_->assertInLoopThread();
if (state_ == kDisconnected)
{
LOG_WARN("TcpConnection::sendInLoop [%d]disconnected, give up writing", socket_->fd());
return;
}
size_t nwrote = 0;
size_t remaining = len;
bool faultError = false;
// 如果当前连接尚没有注册可写事件(比如直接调用send接口),并且发送缓冲区为空
// 就直接发送数据,发送成功则回调写完成事件;
if (!channel_->isWriting() && outputBuffer_.readableBytes() == 0)
{
nwrote = socket_->send((const char*)data, len);
if (nwrote >= 0)
{
remaining = len - nwrote;
if (remaining == 0 && writeCompleteCallback_)
{
loop_->queueInLoop(std::bind(writeCompleteCallback_, shared_from_this()));
}
}
else // nwrote < 0
{
nwrote = 0;
if (errno != EWOULDBLOCK)
{
LOG_ERROR("TcpConnection::sendInLoop error, fd[%d], error[%d]", socket_->fd(), errno);
if (errno == EPIPE || errno == ECONNRESET)
{
faultError = true;
}
}
}
}
ZL_ASSERT(remaining <= len)(remaining)(len)(socket_->fd());
//如果发送成功且数据尚未发送完毕,则将剩余数据存入缓冲区,并注册该channel上的可写事件
if (!faultError && remaining > 0)
{
outputBuffer_.write(static_cast<const char*>(data) + nwrote, remaining);
if (!channel_->isWriting())
{
channel_->enableWriting();
}
}
}
void TcpConnection::sendInLoop2(const std::string& buffer)
{
sendInLoop(buffer.data(), buffer.size());
}
void TcpConnection::shutdown()
{
if (state_ == kConnected)
{
setState(kDisconnecting);
loop_->runInLoop(std::bind(&TcpConnection::shutdownInLoop, shared_from_this()));
}
}
void TcpConnection::shutdownInLoop()
{
loop_->assertInLoopThread();
if (!channel_->isWriting()) // 如果不再关注可写事件,说明数据发送完毕
{
SocketUtil::shutdownWrite(socket_->fd()); // 仅仅关闭写端,因为可能读端还有数据要读
}
}
void TcpConnection::connectEstablished()
{
loop_->assertInLoopThread();
ZL_ASSERT(state_ == kConnecting)(state_);
setState(kConnected);
channel_->enableReading();
TcpConnectionPtr sp_this(shared_from_this());
connectionCallback_(sp_this);
}
void TcpConnection::connectDestroyed()
{
LOG_INFO("TcpConnection::connectDestroyed fd = %d, state = %d", socket_->fd(), state_);
loop_->assertInLoopThread();
if (state_ == kConnected)
{
setState(kDisconnected);
channel_->disableAll();
TcpConnectionPtr sp_this(shared_from_this());
connectionCallback_(sp_this);
}
//SocketUtil::closeSocket(socket_->fd());
channel_->remove();
}
void TcpConnection::handleRead(Timestamp receiveTime)
{
LOG_INFO("TcpConnection::handleRead fd = %d, state = %d", socket_->fd(), state_);
loop_->assertInLoopThread();
std::string data;
size_t n = socket_->recv(data);
inputBuffer_.write(data);
if (n > 0)
{
messageCallback_(shared_from_this(), &inputBuffer_, receiveTime);
}
else if (n == 0)
{
handleClose();
}
else
{
handleError();
}
}
void TcpConnection::handleWrite()
{
LOG_INFO("TcpConnection::handleWrite fd = %d, state = %d", socket_->fd(), state_);
loop_->assertInLoopThread();
if (channel_->isWriting())
{
size_t n = socket_->send(outputBuffer_.peek(), outputBuffer_.readableBytes());
if (n > 0)
{
outputBuffer_.retrieve(n);
LOG_INFO("TcpConnection::handleWrite fd = %d, send = %d, reserve = %d", socket_->fd(), n, outputBuffer_.readableBytes());
if (outputBuffer_.readableBytes() == 0) // 缓冲区为空,数据发送完毕,删除该channel上的可写事件
{
channel_->disableWriting();
if (writeCompleteCallback_)
{
loop_->queueInLoop(std::bind(writeCompleteCallback_, shared_from_this()));
}
if (state_ == kDisconnecting) // 数据发送完毕,且连接已断开
{
shutdownInLoop(); // 关闭socket的可写
}
}
}
else
{
LOG_ERROR("TcpConnection::handleWrite, send fail fd = %d, state = %d, send = %d", socket_->fd(), state_, n);
if (state_ == kDisconnecting)
{
shutdownInLoop();
}
}
}
else
{
LOG_ERROR("TcpConnection::handleWrite, no more writing, fd = %d, state = %d", socket_->fd(), state_);
}
}
void TcpConnection::handleClose()
{
loop_->assertInLoopThread();
ZL_ASSERT(state_ == kConnected || state_ == kDisconnecting)(state_)(socket_->fd());
setState(kDisconnected);
channel_->disableAll();
connectionCallback_(shared_from_this());
closeCallback_(shared_from_this());
}
void TcpConnection::handleError()
{
int err = SocketUtil::getSocketError(channel_->fd());
LOG_ERROR("TcpConnection::handleError [%d], SO_ERROR = %d", channel_->fd(), err);
}
NAMESPACE_ZL_NET_END