16
16
*/
17
17
#include " TcpTransport.h"
18
18
19
+ #include < chrono>
20
+
19
21
#ifndef WIN32
20
22
#include < arpa/inet.h> // for sockaddr_in and inet_ntoa...
21
23
#include < netinet/tcp.h>
@@ -65,9 +67,9 @@ TcpConnectStatus TcpTransport::getTcpConnectStatus() {
65
67
}
66
68
67
69
TcpConnectStatus TcpTransport::waitTcpConnectEvent (int timeoutMillis) {
68
- boost ::unique_lock<boost ::mutex> eventLock (m_connectEventLock);
70
+ std ::unique_lock<std ::mutex> eventLock (m_connectEventLock);
69
71
if (m_tcpConnectStatus == TCP_CONNECT_STATUS_WAIT) {
70
- if (! m_connectEvent.timed_wait (eventLock, boost::posix_time ::milliseconds (timeoutMillis))) {
72
+ if (m_connectEvent.wait_for (eventLock, std::chrono ::milliseconds (timeoutMillis)) == std::cv_status::timeout ) {
71
73
LOG_INFO (" connect timeout" );
72
74
}
73
75
}
@@ -76,16 +78,16 @@ TcpConnectStatus TcpTransport::waitTcpConnectEvent(int timeoutMillis) {
76
78
77
79
// internal method
78
80
void TcpTransport::setTcpConnectEvent (TcpConnectStatus connectStatus) {
79
- TcpConnectStatus baseStatus = m_tcpConnectStatus.exchange (connectStatus, boost ::memory_order_relaxed);
81
+ TcpConnectStatus baseStatus = m_tcpConnectStatus.exchange (connectStatus, std ::memory_order_relaxed);
80
82
if (baseStatus == TCP_CONNECT_STATUS_WAIT) {
81
- boost ::unique_lock<boost ::mutex> eventLock (m_connectEventLock);
83
+ std ::unique_lock<std ::mutex> eventLock (m_connectEventLock);
82
84
m_connectEvent.notify_all ();
83
85
}
84
86
}
85
87
86
88
void TcpTransport::disconnect (const string &addr) {
87
89
// disconnect is idempotent.
88
- boost ::lock_guard<boost ::mutex> lock (m_eventLock);
90
+ std ::lock_guard<std ::mutex> lock (m_eventLock);
89
91
if (getTcpConnectStatus () != TCP_CONNECT_STATUS_INIT) {
90
92
LOG_INFO (" disconnect:%s start. event:%p" , addr.c_str (), m_event.get ());
91
93
freeBufferEvent ();
@@ -104,7 +106,7 @@ TcpConnectStatus TcpTransport::connect(const string &strServerURL, int timeoutMi
104
106
// TODO: call DNS for hostname
105
107
106
108
{
107
- boost ::lock_guard<boost ::mutex> lock (m_eventLock);
109
+ std ::lock_guard<std ::mutex> lock (m_eventLock);
108
110
109
111
struct sockaddr_in sin;
110
112
memset (&sin, 0 , sizeof (sin));
@@ -135,7 +137,7 @@ TcpConnectStatus TcpTransport::connect(const string &strServerURL, int timeoutMi
135
137
if (connectStatus != TCP_CONNECT_STATUS_SUCCESS) {
136
138
LOG_WARN (" can not connect to server:%s" , strServerURL.c_str ());
137
139
138
- boost ::lock_guard<boost ::mutex> lock (m_eventLock);
140
+ std ::lock_guard<std ::mutex> lock (m_eventLock);
139
141
freeBufferEvent ();
140
142
setTcpConnectStatus (TCP_CONNECT_STATUS_FAILED);
141
143
return TCP_CONNECT_STATUS_FAILED;
@@ -228,7 +230,7 @@ void TcpTransport::messageReceived(const MemoryBlock &mem, const std::string &ad
228
230
}
229
231
230
232
bool TcpTransport::sendMessage (const char *pData, size_t len) {
231
- boost ::lock_guard<boost ::mutex> lock (m_eventLock);
233
+ std ::lock_guard<std ::mutex> lock (m_eventLock);
232
234
if (getTcpConnectStatus () != TCP_CONNECT_STATUS_SUCCESS) {
233
235
return false ;
234
236
}
@@ -241,7 +243,7 @@ bool TcpTransport::sendMessage(const char *pData, size_t len) {
241
243
}
242
244
243
245
const string TcpTransport::getPeerAddrAndPort () {
244
- boost ::lock_guard<boost ::mutex> lock (m_eventLock);
246
+ std ::lock_guard<std ::mutex> lock (m_eventLock);
245
247
return m_event ? m_event->getPeerAddrPort () : " " ;
246
248
}
247
249
0 commit comments