Skip to content

Commit

Permalink
Increment used endpoint count during incoming connection.
Browse files Browse the repository at this point in the history
Due to this issue, subsequent incoming connections were failing to
connect as the value was getting decremented from zero resulting in a
check failing.

Also, remove duplicate code for freeing an active connection and move it
into a separate function.
  • Loading branch information
pidarped committed Oct 4, 2023
1 parent a5ea091 commit b88ddd0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/transport/raw/TCP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -502,15 +508,9 @@ void TCPBase::OnPeerClosed(Inet::TCPEndPoint * endPoint)
{
TCPBase * tcp = reinterpret_cast<TCPBase *>(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
Expand Down
4 changes: 4 additions & 0 deletions src/transport/raw/TCP.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit b88ddd0

Please sign in to comment.