4141#include " auth/InitialAuthData.h"
4242#include " checksum/ChecksumProvider.h"
4343
44+ #ifdef USE_ASIO
45+ #include < asio/connect.hpp>
46+ #include < asio/ssl/host_name_verification.hpp>
47+ #else
48+ #include < boost/asio/connect.hpp>
49+ #include < boost/asio/ssl/host_name_verification.hpp>
50+ #endif
51+
4452DECLARE_LOG_OBJECT ()
4553
4654using namespace ASIO::ip;
@@ -170,13 +178,7 @@ ClientConnection::ClientConnection(const std::string& logicalAddress, const std:
170178 executor_(executor),
171179 resolver_(executor_->createTcpResolver ()),
172180 socket_(executor_->createSocket ()),
173- #if defined(USE_ASIO) || BOOST_VERSION >= 107000
174181 strand_(ASIO::make_strand(executor_->getIOService ().get_executor())),
175- #elif BOOST_VERSION >= 106600
176- strand_ (executor_->getIOService ().get_executor()),
177- #else
178- strand_ (executor_->getIOService ()),
179- #endif
180182 logicalAddress_(logicalAddress),
181183 physicalAddress_(physicalAddress),
182184 cnxString_(" [<none> -> " + physicalAddress + " ] " ),
@@ -266,7 +268,7 @@ ClientConnection::ClientConnection(const std::string& logicalAddress, const std:
266268 if (!clientConfiguration.isTlsAllowInsecureConnection () && clientConfiguration.isValidateHostName ()) {
267269 LOG_DEBUG (" Validating hostname for " << serviceUrl.host () << " :" << serviceUrl.port ());
268270 std::string urlHost = isSniProxy_ ? proxyUrl.host () : serviceUrl.host ();
269- tlsSocket_->set_verify_callback (ASIO::ssl::rfc2818_verification (urlHost));
271+ tlsSocket_->set_verify_callback (ASIO::ssl::host_name_verification (urlHost));
270272 }
271273
272274 LOG_DEBUG (" TLS SNI Host: " << serviceUrl.host ());
@@ -309,7 +311,7 @@ void ClientConnection::handlePulsarConnected(const proto::CommandConnected& cmdC
309311 // Only send keep-alive probes if the broker supports it
310312 keepAliveTimer_ = executor_->createDeadlineTimer ();
311313 if (keepAliveTimer_) {
312- keepAliveTimer_->expires_from_now (std::chrono::seconds (keepAliveIntervalInSeconds_));
314+ keepAliveTimer_->expires_after (std::chrono::seconds (keepAliveIntervalInSeconds_));
313315 auto weakSelf = weak_from_this ();
314316 keepAliveTimer_->async_wait ([weakSelf](const ASIO_ERROR&) {
315317 auto self = weakSelf.lock ();
@@ -354,7 +356,7 @@ void ClientConnection::startConsumerStatsTimer(std::vector<uint64_t> consumerSta
354356 // If the close operation has reset the consumerStatsRequestTimer_ then the use_count will be zero
355357 // Check if we have a timer still before we set the request timer to pop again.
356358 if (consumerStatsRequestTimer_) {
357- consumerStatsRequestTimer_->expires_from_now (operationsTimeout_);
359+ consumerStatsRequestTimer_->expires_after (operationsTimeout_);
358360 auto weakSelf = weak_from_this ();
359361 consumerStatsRequestTimer_->async_wait ([weakSelf, consumerStatsRequests](const ASIO_ERROR& err) {
360362 auto self = weakSelf.lock ();
@@ -394,7 +396,7 @@ typedef ASIO::detail::socket_option::integer<IPPROTO_TCP, TCP_KEEPIDLE> tcp_keep
394396 * if async_connect without any error, connected_ would be set to true
395397 * at this point the connection is deemed valid to be used by clients of this class
396398 */
397- void ClientConnection::handleTcpConnected (const ASIO_ERROR& err, tcp::resolver::iterator endpointIterator ) {
399+ void ClientConnection::handleTcpConnected (const ASIO_ERROR& err, const tcp::endpoint& endpoint ) {
398400 if (!err) {
399401 std::stringstream cnxStringStream;
400402 try {
@@ -479,38 +481,13 @@ void ClientConnection::handleTcpConnected(const ASIO_ERROR& err, tcp::resolver::
479481 } else {
480482 handleHandshake (ASIO_SUCCESS);
481483 }
482- } else if (endpointIterator != tcp::resolver::iterator ()) {
483- LOG_WARN (cnxString_ << " Failed to establish connection: " << err.message ());
484- // The connection failed. Try the next endpoint in the list.
485- ASIO_ERROR closeError;
486- socket_->close (closeError); // ignore the error of close
487- if (closeError) {
488- LOG_WARN (cnxString_ << " Failed to close socket: " << err.message ());
489- }
490- connectTimeoutTask_->stop ();
491- ++endpointIterator;
492- if (endpointIterator != tcp::resolver::iterator ()) {
493- LOG_DEBUG (cnxString_ << " Connecting to " << endpointIterator->endpoint () << " ..." );
494- connectTimeoutTask_->start ();
495- tcp::endpoint endpoint = *endpointIterator;
496- auto weakSelf = weak_from_this ();
497- socket_->async_connect (endpoint, [weakSelf, endpointIterator](const ASIO_ERROR& err) {
498- auto self = weakSelf.lock ();
499- if (self) {
500- self->handleTcpConnected (err, endpointIterator);
501- }
502- });
503- } else {
504- if (err == ASIO::error::operation_aborted) {
505- // TCP connect timeout, which is not retryable
506- close ();
507- } else {
508- close (ResultRetryable);
509- }
510- }
511484 } else {
512485 LOG_ERROR (cnxString_ << " Failed to establish connection: " << err.message ());
513- close (ResultRetryable);
486+ if (err == ASIO::error::operation_aborted) {
487+ close ();
488+ } else {
489+ close (ResultRetryable);
490+ }
514491 }
515492}
516493
@@ -603,18 +580,18 @@ void ClientConnection::tcpConnectAsync() {
603580 }
604581
605582 LOG_DEBUG (cnxString_ << " Resolving " << service_url.host () << " :" << service_url.port ());
606- tcp::resolver::query query (service_url. host (), std::to_string (service_url. port ()));
583+
607584 auto weakSelf = weak_from_this ();
608- resolver_->async_resolve (query ,
609- [weakSelf](const ASIO_ERROR& err, const tcp::resolver::iterator& iterator ) {
585+ resolver_->async_resolve (service_url. host (), std::to_string (service_url. port ()) ,
586+ [weakSelf](const ASIO_ERROR& err, tcp::resolver::results_type results ) {
610587 auto self = weakSelf.lock ();
611588 if (self) {
612- self->handleResolve (err, iterator );
589+ self->handleResolve (err, results );
613590 }
614591 });
615592}
616593
617- void ClientConnection::handleResolve (const ASIO_ERROR& err, const tcp::resolver::iterator& endpointIterator ) {
594+ void ClientConnection::handleResolve (ASIO_ERROR err, const tcp::resolver::results_type& results ) {
618595 if (err) {
619596 std::string hostUrl = isSniProxy_ ? cnxString_ : proxyServiceUrl_;
620597 LOG_ERROR (hostUrl << " Resolve error: " << err << " : " << err.message ());
@@ -641,23 +618,13 @@ void ClientConnection::handleResolve(const ASIO_ERROR& err, const tcp::resolver:
641618 }
642619 ptr->connectTimeoutTask_ ->stop ();
643620 });
644-
645- LOG_DEBUG (cnxString_ << " Connecting to " << endpointIterator->endpoint () << " ..." );
646621 connectTimeoutTask_->start ();
647- if (endpointIterator != tcp::resolver::iterator ()) {
648- LOG_DEBUG (cnxString_ << " Resolved hostname " << endpointIterator->host_name () //
649- << " to " << endpointIterator->endpoint ());
650- socket_->async_connect (*endpointIterator, [weakSelf, endpointIterator](const ASIO_ERROR& err) {
651- auto self = weakSelf.lock ();
652- if (self) {
653- self->handleTcpConnected (err, endpointIterator);
654- }
655- });
656- } else {
657- LOG_WARN (cnxString_ << " No IP address found" );
658- close ();
659- return ;
660- }
622+ ASIO::async_connect (*socket_, results, [weakSelf](const ASIO_ERROR& err, const tcp::endpoint& endpoint) {
623+ auto self = weakSelf.lock ();
624+ if (self) {
625+ self->handleTcpConnected (err, endpoint);
626+ }
627+ });
661628}
662629
663630void ClientConnection::readNextCommand () {
@@ -1061,7 +1028,7 @@ void ClientConnection::newLookup(const SharedBuffer& cmd, uint64_t requestId,
10611028 LookupRequestData requestData;
10621029 requestData.promise = promise;
10631030 requestData.timer = executor_->createDeadlineTimer ();
1064- requestData.timer ->expires_from_now (operationsTimeout_);
1031+ requestData.timer ->expires_after (operationsTimeout_);
10651032 auto weakSelf = weak_from_this ();
10661033 requestData.timer ->async_wait ([weakSelf, requestData](const ASIO_ERROR& ec) {
10671034 auto self = weakSelf.lock ();
@@ -1201,7 +1168,7 @@ Future<Result, ResponseData> ClientConnection::sendRequestWithId(const SharedBuf
12011168
12021169 PendingRequestData requestData;
12031170 requestData.timer = executor_->createDeadlineTimer ();
1204- requestData.timer ->expires_from_now (operationsTimeout_);
1171+ requestData.timer ->expires_after (operationsTimeout_);
12051172 auto weakSelf = weak_from_this ();
12061173 requestData.timer ->async_wait ([weakSelf, requestData](const ASIO_ERROR& ec) {
12071174 auto self = weakSelf.lock ();
@@ -1256,7 +1223,7 @@ void ClientConnection::handleKeepAliveTimeout() {
12561223 // be zero And we do not attempt to dereference the pointer.
12571224 Lock lock (mutex_);
12581225 if (keepAliveTimer_) {
1259- keepAliveTimer_->expires_from_now (std::chrono::seconds (keepAliveIntervalInSeconds_));
1226+ keepAliveTimer_->expires_after (std::chrono::seconds (keepAliveIntervalInSeconds_));
12601227 auto weakSelf = weak_from_this ();
12611228 keepAliveTimer_->async_wait ([weakSelf](const ASIO_ERROR&) {
12621229 auto self = weakSelf.lock ();
@@ -1318,12 +1285,12 @@ void ClientConnection::close(Result result, bool detach) {
13181285 numOfPendingLookupRequest_ = 0 ;
13191286
13201287 if (keepAliveTimer_) {
1321- keepAliveTimer_-> cancel ( );
1288+ cancelTimer (*keepAliveTimer_ );
13221289 keepAliveTimer_.reset ();
13231290 }
13241291
13251292 if (consumerStatsRequestTimer_) {
1326- consumerStatsRequestTimer_-> cancel ( );
1293+ cancelTimer (*consumerStatsRequestTimer_ );
13271294 consumerStatsRequestTimer_.reset ();
13281295 }
13291296
@@ -1435,7 +1402,7 @@ Future<Result, GetLastMessageIdResponse> ClientConnection::newGetLastMessageId(u
14351402 LastMessageIdRequestData requestData;
14361403 requestData.promise = promise;
14371404 requestData.timer = executor_->createDeadlineTimer ();
1438- requestData.timer ->expires_from_now (operationsTimeout_);
1405+ requestData.timer ->expires_after (operationsTimeout_);
14391406 auto weakSelf = weak_from_this ();
14401407 requestData.timer ->async_wait ([weakSelf, requestData](const ASIO_ERROR& ec) {
14411408 auto self = weakSelf.lock ();
@@ -1483,7 +1450,7 @@ Future<Result, SchemaInfo> ClientConnection::newGetSchema(const std::string& top
14831450 lock.unlock ();
14841451
14851452 auto weakSelf = weak_from_this ();
1486- timer->expires_from_now (operationsTimeout_);
1453+ timer->expires_after (operationsTimeout_);
14871454 timer->async_wait ([this , weakSelf, requestId](const ASIO_ERROR& ec) {
14881455 auto self = weakSelf.lock ();
14891456 if (!self) {
@@ -1570,7 +1537,7 @@ void ClientConnection::handleSuccess(const proto::CommandSuccess& success) {
15701537 lock.unlock ();
15711538
15721539 requestData.promise .setValue ({});
1573- requestData.timer -> cancel ( );
1540+ cancelTimer (* requestData.timer );
15741541 }
15751542}
15761543
@@ -1582,7 +1549,8 @@ void ClientConnection::handlePartitionedMetadataResponse(
15821549 Lock lock (mutex_);
15831550 auto it = pendingLookupRequests_.find (partitionMetadataResponse.request_id ());
15841551 if (it != pendingLookupRequests_.end ()) {
1585- it->second .timer ->cancel ();
1552+ cancelTimer (*it->second .timer );
1553+
15861554 LookupDataResultPromisePtr lookupDataPromise = it->second .promise ;
15871555 pendingLookupRequests_.erase (it);
15881556 numOfPendingLookupRequest_--;
@@ -1661,7 +1629,7 @@ void ClientConnection::handleLookupTopicRespose(
16611629 Lock lock (mutex_);
16621630 auto it = pendingLookupRequests_.find (lookupTopicResponse.request_id ());
16631631 if (it != pendingLookupRequests_.end ()) {
1664- it->second .timer -> cancel ( );
1632+ cancelTimer (* it->second .timer );
16651633 LookupDataResultPromisePtr lookupDataPromise = it->second .promise ;
16661634 pendingLookupRequests_.erase (it);
16671635 numOfPendingLookupRequest_--;
@@ -1739,7 +1707,7 @@ void ClientConnection::handleProducerSuccess(const proto::CommandProducerSuccess
17391707 data.topicEpoch = boost::none;
17401708 }
17411709 requestData.promise .setValue (data);
1742- requestData.timer -> cancel ( );
1710+ cancelTimer (* requestData.timer );
17431711 }
17441712 }
17451713}
@@ -1759,7 +1727,7 @@ void ClientConnection::handleError(const proto::CommandError& error) {
17591727 lock.unlock ();
17601728
17611729 requestData.promise .setFailed (result);
1762- requestData.timer -> cancel ( );
1730+ cancelTimer (* requestData.timer );
17631731 } else {
17641732 PendingGetLastMessageIdRequestsMap::iterator it =
17651733 pendingGetLastMessageIdRequests_.find (error.request_id ());
@@ -2052,8 +2020,8 @@ void ClientConnection::unsafeRemovePendingRequest(long requestId) {
20522020 auto it = pendingRequests_.find (requestId);
20532021 if (it != pendingRequests_.end ()) {
20542022 it->second .promise .setFailed (ResultDisconnected);
2055- ASIO_ERROR ec ;
2056- it-> second . timer -> cancel (ec);
2023+ cancelTimer (*it-> second . timer ) ;
2024+
20572025 pendingRequests_.erase (it);
20582026 }
20592027}
0 commit comments