-
Notifications
You must be signed in to change notification settings - Fork 157
/
TcpTransport.cpp
320 lines (275 loc) · 10.8 KB
/
TcpTransport.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "TcpTransport.h"
#include <chrono>
#ifndef WIN32
#include <arpa/inet.h> // for sockaddr_in and inet_ntoa...
#include <netinet/tcp.h>
#include <sys/socket.h> // for socket(), bind(), and connect()...
#endif
#include "Logging.h"
#include "TcpRemotingClient.h"
#include "UtilAll.h"
namespace rocketmq {
//<!************************************************************************
TcpTransport::TcpTransport(TcpRemotingClient* pTcpRemointClient,
bool enableSsl,
const std::string& sslPropertyFile,
TcpTransportReadCallback handle)
: m_event(nullptr),
m_tcpConnectStatus(TCP_CONNECT_STATUS_INIT),
m_connectEventLock(),
m_connectEvent(),
m_readCallback(handle),
m_tcpRemotingClient(pTcpRemointClient),
m_enableSsl(enableSsl),
m_sslPropertyFile(sslPropertyFile) {
m_startTime = UtilAll::currentTimeMillis();
}
TcpTransport::~TcpTransport() {
freeBufferEvent();
m_readCallback = nullptr;
}
void TcpTransport::freeBufferEvent() {
// freeBufferEvent is idempotent.
// first, unlink BufferEvent
if (m_event != nullptr) {
m_event->setCallback(nullptr, nullptr, nullptr, nullptr);
}
// then, release BufferEvent
m_event.reset();
}
void TcpTransport::setTcpConnectStatus(TcpConnectStatus connectStatus) {
m_tcpConnectStatus = connectStatus;
}
TcpConnectStatus TcpTransport::getTcpConnectStatus() {
return m_tcpConnectStatus;
}
TcpConnectStatus TcpTransport::waitTcpConnectEvent(int timeoutMillis) {
if (m_tcpConnectStatus == TCP_CONNECT_STATUS_WAIT) {
std::unique_lock<std::mutex> eventLock(m_connectEventLock);
if (!m_connectEvent.wait_for(eventLock, std::chrono::milliseconds(timeoutMillis),
[&] { return m_tcpConnectStatus != TCP_CONNECT_STATUS_WAIT; })) {
LOG_INFO("connect timeout");
}
}
return m_tcpConnectStatus;
}
// internal method
void TcpTransport::setTcpConnectEvent(TcpConnectStatus connectStatus) {
TcpConnectStatus baseStatus = m_tcpConnectStatus.exchange(connectStatus, std::memory_order_relaxed);
if (baseStatus == TCP_CONNECT_STATUS_WAIT) {
// awake waiting thread
m_connectEvent.notify_all();
}
}
u_long TcpTransport::getInetAddr(string& hostname) {
u_long addr = inet_addr(hostname.c_str());
if (INADDR_NONE == addr) {
constexpr size_t length = 128;
struct evutil_addrinfo hints;
struct evutil_addrinfo* answer = NULL;
/* Build the hints to tell getaddrinfo how to act. */
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; /* v4 or v6 is fine. */
// Look up the hostname.
int err = evutil_getaddrinfo(hostname.c_str(), NULL, &hints, &answer);
if (err != 0) {
string info = "Failed to resolve host name(" + hostname + "): " + evutil_gai_strerror(err);
THROW_MQEXCEPTION(MQClientException, info, -1);
}
struct evutil_addrinfo* addressInfo;
for (addressInfo = answer; addressInfo; addressInfo = addressInfo->ai_next) {
char buf[length];
const char* address = NULL;
if (addressInfo->ai_family == AF_INET) {
struct sockaddr_in* sin = (struct sockaddr_in*)addressInfo->ai_addr;
address = evutil_inet_ntop(AF_INET, &sin->sin_addr, buf, length);
} else if (addressInfo->ai_family == AF_INET6) {
struct sockaddr_in6* sin6 = (struct sockaddr_in6*)addressInfo->ai_addr;
address = evutil_inet_ntop(AF_INET6, &sin6->sin6_addr, buf, length);
}
if (address) {
addr = inet_addr(address);
if (addr != INADDR_NONE) {
break;
}
}
}
evutil_freeaddrinfo(answer);
}
return addr;
}
void TcpTransport::disconnect(const string& addr) {
// disconnect is idempotent.
std::lock_guard<std::mutex> lock(m_eventLock);
if (getTcpConnectStatus() != TCP_CONNECT_STATUS_INIT) {
LOG_INFO("disconnect:%s start. event:%p", addr.c_str(), m_event.get());
freeBufferEvent();
setTcpConnectEvent(TCP_CONNECT_STATUS_INIT);
LOG_INFO("disconnect:%s completely", addr.c_str());
}
}
TcpConnectStatus TcpTransport::connect(const string& strServerURL, int timeoutMillis) {
string hostname;
short port;
LOG_DEBUG("connect to [%s].", strServerURL.c_str());
if (!UtilAll::SplitURL(strServerURL, hostname, port)) {
LOG_INFO("connect to [%s] failed, Invalid url.", strServerURL.c_str());
return TCP_CONNECT_STATUS_FAILED;
}
{
std::lock_guard<std::mutex> lock(m_eventLock);
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
try {
sin.sin_addr.s_addr = getInetAddr(hostname);
} catch (const MQClientException& e) {
LOG_INFO("connect to %s failed, %s", strServerURL.c_str(), e.what());
setTcpConnectStatus(TCP_CONNECT_STATUS_FAILED);
return TCP_CONNECT_STATUS_FAILED;
}
sin.sin_port = htons(port);
m_event.reset(EventLoop::GetDefaultEventLoop()->createBufferEvent(-1, BEV_OPT_CLOSE_ON_FREE | BEV_OPT_THREADSAFE,
m_enableSsl, m_sslPropertyFile));
m_event->setCallback(readNextMessageIntCallback, nullptr, eventCallback, shared_from_this());
m_event->setWatermark(EV_READ, 4, 0);
m_event->enable(EV_READ | EV_WRITE);
setTcpConnectStatus(TCP_CONNECT_STATUS_WAIT);
if (m_event->connect((struct sockaddr*)&sin, sizeof(sin)) < 0) {
LOG_INFO("connect to fd:%d failed", m_event->getfd());
freeBufferEvent();
setTcpConnectStatus(TCP_CONNECT_STATUS_FAILED);
return TCP_CONNECT_STATUS_FAILED;
}
}
if (timeoutMillis <= 0) {
LOG_INFO("try to connect to fd:%d, addr:%s", m_event->getfd(), hostname.c_str());
return TCP_CONNECT_STATUS_WAIT;
}
TcpConnectStatus connectStatus = waitTcpConnectEvent(timeoutMillis);
if (connectStatus != TCP_CONNECT_STATUS_SUCCESS) {
LOG_WARN("can not connect to server:%s", strServerURL.c_str());
std::lock_guard<std::mutex> lock(m_eventLock);
freeBufferEvent();
setTcpConnectStatus(TCP_CONNECT_STATUS_FAILED);
return TCP_CONNECT_STATUS_FAILED;
}
return TCP_CONNECT_STATUS_SUCCESS;
}
void TcpTransport::eventCallback(BufferEvent* event, short what, TcpTransport* transport) {
socket_t fd = event->getfd();
LOG_INFO("eventcb: received event:%x on fd:%d", what, fd);
if (what & BEV_EVENT_CONNECTED) {
LOG_INFO("eventcb: connect to fd:%d successfully", fd);
// disable Nagle
int val = 1;
#ifdef WIN32
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<const char*>(&val), sizeof(val));
#else
setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<const void*>(&val), sizeof(val));
#endif
transport->setTcpConnectEvent(TCP_CONNECT_STATUS_SUCCESS);
} else if (what & (BEV_EVENT_ERROR | BEV_EVENT_EOF | BEV_EVENT_READING | BEV_EVENT_WRITING)) {
LOG_INFO("eventcb: received error event cb:%x on fd:%d", what, fd);
// if error, stop callback.
event->setCallback(nullptr, nullptr, nullptr, nullptr);
transport->setTcpConnectEvent(TCP_CONNECT_STATUS_FAILED);
} else {
LOG_ERROR("eventcb: received error event:%d on fd:%d", what, fd);
}
}
void TcpTransport::readNextMessageIntCallback(BufferEvent* event, TcpTransport* transport) {
/* This callback is invoked when there is data to read on bev. */
// protocol: <length> <header length> <header data> <body data>
// 1 2 3 4
// rocketmq protocol contains 4 parts as following:
// 1, big endian 4 bytes int, its length is sum of 2,3 and 4
// 2, big endian 4 bytes int, its length is 3
// 3, use json to serialization data
// 4, application could self-defined binary data
struct evbuffer* input = event->getInput();
while (1) {
struct evbuffer_iovec v[4];
int n = evbuffer_peek(input, 4, NULL, v, sizeof(v) / sizeof(v[0]));
char hdr[4];
char* p = hdr;
size_t needed = 4;
for (int idx = 0; idx < n; idx++) {
if (needed > 0) {
size_t tmp = needed < v[idx].iov_len ? needed : v[idx].iov_len;
memcpy(p, v[idx].iov_base, tmp);
p += tmp;
needed -= tmp;
} else {
break;
}
}
if (needed > 0) {
LOG_DEBUG("too little data received with sum = %d", 4 - needed);
/**
* reset read water mark to 4
*/
event->setWatermark(EV_READ, 4, 0);
return;
}
uint32 totalLenOfOneMsg = *(uint32*)hdr; // first 4 bytes, which indicates 1st part of protocol
uint32 msgLen = ntohl(totalLenOfOneMsg);
size_t recvLen = evbuffer_get_length(input);
if (recvLen >= msgLen + 4) {
LOG_DEBUG("had received all data. msgLen:%d, from:%d, recvLen:%d", msgLen, event->getfd(), recvLen);
} else {
LOG_DEBUG("didn't received whole. msgLen:%d, from:%d, recvLen:%d", msgLen, event->getfd(), recvLen);
/**
* set read water mark to msgLen + 4,wait for receiving whole data
*/
event->setWatermark(EV_READ, msgLen + 4, 0);
return; // consider large data which was not received completely by now
}
if (msgLen > 0) {
MemoryBlock msg(msgLen, true);
event->read(hdr, 4); // skip length field
event->read(msg.getData(), msgLen);
transport->messageReceived(msg, event->getPeerAddrPort());
}
}
}
void TcpTransport::messageReceived(const MemoryBlock& mem, const std::string& addr) {
if (m_readCallback != nullptr) {
m_readCallback(m_tcpRemotingClient, mem, addr);
}
}
bool TcpTransport::sendMessage(const char* pData, size_t len) {
std::lock_guard<std::mutex> lock(m_eventLock);
if (getTcpConnectStatus() != TCP_CONNECT_STATUS_SUCCESS) {
return false;
}
/* NOTE:
do not need to consider large data which could not send by once, as
bufferevent could handle this case;
*/
return m_event != nullptr && m_event->write(pData, len) == 0;
}
const string TcpTransport::getPeerAddrAndPort() {
std::lock_guard<std::mutex> lock(m_eventLock);
return m_event ? m_event->getPeerAddrPort() : "";
}
const uint64_t TcpTransport::getStartTime() const {
return m_startTime;
}
} // namespace rocketmq