3131#include " OpSendMsg.h"
3232#include " ProducerImpl.h"
3333#include " PulsarApi.pb.h"
34+ #include " ResultUtils.h"
3435#include " Url.h"
3536#include " auth/InitialAuthData.h"
3637#include " checksum/ChecksumProvider.h"
@@ -205,7 +206,7 @@ ClientConnection::ClientConnection(const std::string& logicalAddress, const std:
205206 ctx.load_verify_file (trustCertFilePath);
206207 } else {
207208 LOG_ERROR (trustCertFilePath << " : No such trustCertFile" );
208- close ();
209+ close (ResultAuthenticationError, false );
209210 return ;
210211 }
211212 } else {
@@ -215,7 +216,7 @@ ClientConnection::ClientConnection(const std::string& logicalAddress, const std:
215216
216217 if (!authentication_) {
217218 LOG_ERROR (" Invalid authentication plugin" );
218- close ();
219+ close (ResultAuthenticationError, false );
219220 return ;
220221 }
221222
@@ -229,12 +230,12 @@ ClientConnection::ClientConnection(const std::string& logicalAddress, const std:
229230 tlsPrivateKey = authData->getTlsPrivateKey ();
230231 if (!file_exists (tlsCertificates)) {
231232 LOG_ERROR (tlsCertificates << " : No such tlsCertificates" );
232- close ();
233+ close (ResultAuthenticationError, false );
233234 return ;
234235 }
235236 if (!file_exists (tlsCertificates)) {
236237 LOG_ERROR (tlsCertificates << " : No such tlsCertificates" );
237- close ();
238+ close (ResultAuthenticationError, false );
238239 return ;
239240 }
240241 ctx.use_private_key_file (tlsPrivateKey, boost::asio::ssl::context::pem);
@@ -660,7 +661,7 @@ void ClientConnection::handleRead(const boost::system::error_code& err, size_t b
660661 } else {
661662 LOG_ERROR (cnxString_ << " Read operation failed: " << err.message ());
662663 }
663- close ();
664+ close (ResultDisconnected );
664665 } else if (bytesTransferred < minReadSize) {
665666 // Read the remaining part, use a slice of buffer to write on the next
666667 // region
@@ -718,7 +719,7 @@ void ClientConnection::processIncomingBuffer() {
718719 proto::BaseCommand incomingCmd;
719720 if (!incomingCmd.ParseFromArray (incomingBuffer_.data (), cmdSize)) {
720721 LOG_ERROR (cnxString_ << " Error parsing protocol buffer command" );
721- close ();
722+ close (ResultDisconnected );
722723 return ;
723724 }
724725
@@ -742,7 +743,7 @@ void ClientConnection::processIncomingBuffer() {
742743 << incomingCmd.message ().message_id ().ledgerid () << " , entry id "
743744 << incomingCmd.message ().message_id ().entryid ()
744745 << " ] Error parsing broker entry metadata" );
745- close ();
746+ close (ResultDisconnected );
746747 return ;
747748 }
748749 incomingBuffer_.setReaderIndex (readerIndex + 2 + 4 + brokerEntryMetadataSize);
@@ -760,7 +761,7 @@ void ClientConnection::processIncomingBuffer() {
760761 << incomingCmd.message ().message_id ().ledgerid () //
761762 << " , entry id " << incomingCmd.message ().message_id ().entryid ()
762763 << " ] Error parsing message metadata" );
763- close ();
764+ close (ResultDisconnected );
764765 return ;
765766 }
766767
@@ -991,7 +992,7 @@ void ClientConnection::handleIncomingCommand(BaseCommand& incomingCmd) {
991992
992993 default :
993994 LOG_WARN (cnxString_ << " Received invalid message from server" );
994- close ();
995+ close (ResultDisconnected );
995996 break ;
996997 }
997998 }
@@ -1133,7 +1134,7 @@ void ClientConnection::sendMessage(const std::shared_ptr<SendArguments>& args) {
11331134void ClientConnection::handleSend (const boost::system::error_code& err, const SharedBuffer&) {
11341135 if (err) {
11351136 LOG_WARN (cnxString_ << " Could not send message on connection: " << err << " " << err.message ());
1136- close ();
1137+ close (ResultDisconnected );
11371138 } else {
11381139 sendPendingCommands ();
11391140 }
@@ -1142,7 +1143,7 @@ void ClientConnection::handleSend(const boost::system::error_code& err, const Sh
11421143void ClientConnection::handleSendPair (const boost::system::error_code& err) {
11431144 if (err) {
11441145 LOG_WARN (cnxString_ << " Could not send pair message on connection: " << err << " " << err.message ());
1145- close ();
1146+ close (ResultDisconnected );
11461147 } else {
11471148 sendPendingCommands ();
11481149 }
@@ -1247,7 +1248,7 @@ void ClientConnection::handleKeepAliveTimeout() {
12471248
12481249 if (havePendingPingRequest_) {
12491250 LOG_WARN (cnxString_ << " Forcing connection to close after keep-alive timeout" );
1250- close ();
1251+ close (ResultDisconnected );
12511252 } else {
12521253 // Send keep alive probe to peer
12531254 LOG_DEBUG (cnxString_ << " Sending ping message" );
@@ -1287,7 +1288,14 @@ void ClientConnection::close(Result result, bool detach) {
12871288 }
12881289 state_ = Disconnected;
12891290
1290- closeSocket ();
1291+ if (socket_) {
1292+ boost::system::error_code err;
1293+ socket_->shutdown (boost::asio::socket_base::shutdown_both, err);
1294+ socket_->close (err);
1295+ if (err) {
1296+ LOG_WARN (cnxString_ << " Failed to close socket: " << err.message ());
1297+ }
1298+ }
12911299 if (tlsSocket_) {
12921300 boost::system::error_code err;
12931301 tlsSocket_->lowest_layer ().close (err);
@@ -1326,7 +1334,7 @@ void ClientConnection::close(Result result, bool detach) {
13261334 }
13271335
13281336 lock.unlock ();
1329- if (result != ResultDisconnected && result != ResultRetryable ) {
1337+ if (! isResultRetryable ( result) ) {
13301338 LOG_ERROR (cnxString_ << " Connection closed with " << result);
13311339 } else {
13321340 LOG_INFO (cnxString_ << " Connection disconnected" );
@@ -1473,26 +1481,15 @@ Future<Result, SchemaInfo> ClientConnection::newGetSchema(const std::string& top
14731481 return promise.getFuture ();
14741482}
14751483
1476- void ClientConnection::closeSocket () {
1477- boost::system::error_code err;
1478- if (socket_) {
1479- socket_->shutdown (boost::asio::socket_base::shutdown_both, err);
1480- socket_->close (err);
1481- if (err) {
1482- LOG_WARN (cnxString_ << " Failed to close socket: " << err.message ());
1483- }
1484- }
1485- }
1486-
14871484void ClientConnection::checkServerError (ServerError error) {
14881485 switch (error) {
14891486 case proto::ServerError::ServiceNotReady:
1490- closeSocket ( );
1487+ close (ResultDisconnected );
14911488 break ;
14921489 case proto::ServerError::TooManyRequests:
14931490 // TODO: Implement maxNumberOfRejectedRequestPerConnection like
14941491 // https://github.com/apache/pulsar/pull/274
1495- closeSocket ( );
1492+ close (ResultDisconnected );
14961493 break ;
14971494 default :
14981495 break ;
@@ -1518,7 +1515,7 @@ void ClientConnection::handleSendReceipt(const proto::CommandSendReceipt& sendRe
15181515 if (!producer->ackReceived (sequenceId, messageId)) {
15191516 // If the producer fails to process the ack, we need to close the connection
15201517 // to give it a chance to recover from there
1521- close ();
1518+ close (ResultDisconnected );
15221519 }
15231520 }
15241521 } else {
@@ -1542,12 +1539,12 @@ void ClientConnection::handleSendError(const proto::CommandSendError& error) {
15421539 if (!producer->removeCorruptMessage (sequenceId)) {
15431540 // If the producer fails to remove corrupt msg, we need to close the
15441541 // connection to give it a chance to recover from there
1545- close ();
1542+ close (ResultDisconnected );
15461543 }
15471544 }
15481545 }
15491546 } else {
1550- close ();
1547+ close (ResultDisconnected );
15511548 }
15521549}
15531550
0 commit comments