diff --git a/src/transport/raw/TCP.cpp b/src/transport/raw/TCP.cpp index 8e6cae72da4f36..a1b1df1fe45a2d 100644 --- a/src/transport/raw/TCP.cpp +++ b/src/transport/raw/TCP.cpp @@ -325,6 +325,18 @@ CHIP_ERROR TCPBase::ProcessSingleMessage(const PeerAddress & peerAddress, Active return CHIP_NO_ERROR; } +void TCPBase::ReleaseActiveConnection(Inet::TCPEndPoint * endPoint) +{ + for (size_t i = 0; i < mActiveConnectionsSize; i++) + { + if (mActiveConnections[i].mEndPoint == endPoint) + { + mActiveConnections[i].Free(); + mUsedEndPointCount--; + } + } +} + CHIP_ERROR TCPBase::OnTcpReceive(Inet::TCPEndPoint * endPoint, System::PacketBufferHandle && buffer) { Inet::IPAddress ipAddress; @@ -425,15 +437,8 @@ void TCPBase::OnConnectionClosed(Inet::TCPEndPoint * endPoint, CHIP_ERROR err) ChipLogProgress(Inet, "Connection closed."); - for (size_t i = 0; i < tcp->mActiveConnectionsSize; i++) - { - if (tcp->mActiveConnections[i].mEndPoint == endPoint) - { - ChipLogProgress(Inet, "Freeing closed connection."); - tcp->mActiveConnections[i].Free(); - tcp->mUsedEndPointCount--; - } - } + ChipLogProgress(Inet, "Freeing closed connection."); + tcp->ReleaseActiveConnection(endPoint); } void TCPBase::OnConnectionReceived(Inet::TCPEndPoint * listenEndPoint, Inet::TCPEndPoint * endPoint, @@ -449,6 +454,7 @@ void TCPBase::OnConnectionReceived(Inet::TCPEndPoint * listenEndPoint, Inet::TCP if (!tcp->mActiveConnections[i].InUse()) { tcp->mActiveConnections[i].Init(endPoint); + tcp->mUsedEndPointCount++; break; } } @@ -502,15 +508,9 @@ void TCPBase::OnPeerClosed(Inet::TCPEndPoint * endPoint) { TCPBase * tcp = reinterpret_cast(endPoint->mAppState); - for (size_t i = 0; i < tcp->mActiveConnectionsSize; i++) - { - if (tcp->mActiveConnections[i].mEndPoint == endPoint) - { - ChipLogProgress(Inet, "Freeing connection: connection closed by peer"); - tcp->mActiveConnections[i].Free(); - tcp->mUsedEndPointCount--; - } - } + ChipLogProgress(Inet, "Freeing connection: connection closed by peer"); + + tcp->ReleaseActiveConnection(endPoint); } bool TCPBase::HasActiveConnections() const diff --git a/src/transport/raw/TCP.h b/src/transport/raw/TCP.h index 8f90a4242b187f..d9f78be1771b0f 100644 --- a/src/transport/raw/TCP.h +++ b/src/transport/raw/TCP.h @@ -225,6 +225,10 @@ class DLL_EXPORT TCPBase : public Base */ CHIP_ERROR ProcessSingleMessage(const PeerAddress & peerAddress, ActiveConnectionState * state, uint16_t messageSize); + // Release an active connection (corresponding to the passed TCPEndPoint) + // from the pool. + void ReleaseActiveConnection(Inet::TCPEndPoint * endPoint); + // Callback handler for TCPEndPoint. TCP message receive handler. // @see TCPEndpoint::OnDataReceivedFunct static CHIP_ERROR OnTcpReceive(Inet::TCPEndPoint * endPoint, System::PacketBufferHandle && buffer);