Skip to content

Commit 7581ffa

Browse files
committed
Minor fixes in TCP Disconnect to simplify the logic.
Use the available IPAddress and port info in the member PeerAddress in the ActiveConnection object to compare with the passed PeerAddress instead of calling GetPeerInfo() from within TCPEndPoint to fetch those. Log the connection closure in the CloseConnectionInternal function which is also called by CloseActiveConnections() from the TCPBase destructor.
1 parent 005f1b4 commit 7581ffa

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

src/transport/raw/TCP.cpp

+13-24
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,10 @@ void TCPBase::CloseConnectionInternal(ActiveTCPConnectionState * connection, CHI
398398

399399
if (connection->mConnectionState != TCPState::kClosed && connection->mEndPoint)
400400
{
401+
char addrStr[Transport::PeerAddress::kMaxToStringSize];
402+
connection->mPeerAddr.ToString(addrStr);
403+
ChipLogProgress(Inet, "Closing connection with peer %s.", addrStr);
404+
401405
if (err == CHIP_NO_ERROR)
402406
{
403407
connection->mEndPoint->Close();
@@ -616,36 +620,21 @@ CHIP_ERROR TCPBase::TCPConnect(const PeerAddress & address, Transport::AppTCPCon
616620

617621
void TCPBase::TCPDisconnect(const PeerAddress & address)
618622
{
619-
CHIP_ERROR err = CHIP_NO_ERROR;
620623
// Closes an existing connection
621624
for (size_t i = 0; i < mActiveConnectionsSize; i++)
622625
{
623626
if (mActiveConnections[i].IsConnected())
624627
{
625-
Inet::IPAddress ipAddress;
626-
uint16_t port;
627-
Inet::InterfaceId interfaceId;
628-
629-
err = mActiveConnections[i].mEndPoint->GetPeerInfo(&ipAddress, &port);
630-
if (err != CHIP_NO_ERROR)
631-
{
632-
ChipLogError(Inet, "TCPDisconnect: GetPeerInfo error: %" CHIP_ERROR_FORMAT, err.Format());
633-
return;
634-
}
635-
636-
err = mActiveConnections[i].mEndPoint->GetInterfaceId(&interfaceId);
637-
if (err != CHIP_NO_ERROR)
628+
const Inet::IPAddress & ipAddress = mActiveConnections[i].mPeerAddr.GetIPAddress();
629+
uint16_t port = mActiveConnections[i].mPeerAddr.GetPort();
630+
631+
// Ignoring the InterfaceID in the check as it may not have been provided in
632+
// the PeerAddress during connection establishment. The IPAddress and Port
633+
// are the necessary and sufficient set of parameters for searching
634+
// through the connections.
635+
if (ipAddress == address.GetIPAddress() && port == address.GetPort() &&
636+
address.GetTransportType() == Type::kTcp)
638637
{
639-
ChipLogError(Inet, "TCPDisconnect: GetInterfaceId error: %" CHIP_ERROR_FORMAT, err.Format());
640-
return;
641-
}
642-
// if (address == PeerAddress::TCP(ipAddress, port, interfaceId))
643-
if (ipAddress == address.GetIPAddress() && port == address.GetPort())
644-
{
645-
char addrStr[Transport::PeerAddress::kMaxToStringSize];
646-
address.ToString(addrStr);
647-
ChipLogProgress(Inet, "Disconnecting with peer %s.", addrStr);
648-
649638
// NOTE: this leaves the socket in TIME_WAIT.
650639
// Calling Abort() would clean it since SO_LINGER would be set to 0,
651640
// however this seems not to be useful.

0 commit comments

Comments
 (0)