@@ -90,6 +90,10 @@ CHIP_ERROR TCPEndPointImplLwIP::ListenImpl(uint16_t backlog)
90
90
CHIP_ERROR err = CHIP_NO_ERROR;
91
91
if (!mPreAllocatedConnectEP )
92
92
{
93
+ // Pre allocate a TCP EndPoint for TCP connection, it will be released under either of the two conditions:
94
+ // - The Listen EndPoint receives a connection and the connection will use this endpoint. The endpoint will be release when
95
+ // the connection is released.
96
+ // - The Listen Endpoint is closed.
93
97
err = GetEndPointManager ().NewEndPoint (&mPreAllocatedConnectEP );
94
98
}
95
99
if (err == CHIP_NO_ERROR)
@@ -446,6 +450,11 @@ void TCPEndPointImplLwIP::DoCloseImpl(CHIP_ERROR err, State oldState)
446
450
}
447
451
}
448
452
453
+ if (mPreAllocatedConnectEP )
454
+ {
455
+ // If the Listen EndPoint has a pre-allocated connect EndPoint, release it for the Retain() in the constructor
456
+ mPreAllocatedConnectEP ->Free ();
457
+ }
449
458
if (mState == State::kClosed )
450
459
{
451
460
mUnackedLength = 0 ;
@@ -816,7 +825,8 @@ err_t TCPEndPointImplLwIP::LwIPHandleIncomingConnection(void * arg, struct tcp_p
816
825
conEP = static_cast <TCPEndPointImplLwIP *>(listenEP->mPreAllocatedConnectEP );
817
826
if (conEP == nullptr )
818
827
{
819
- // The listen endpoint receives a new incomming connection before it pre-allocates a new connection endpoint.
828
+ // The listen endpoint received a new incoming connection before it had a chance to pre-allocates a new connection
829
+ // endpoint.
820
830
err = CHIP_ERROR_BUSY;
821
831
}
822
832
}
@@ -849,8 +859,18 @@ err_t TCPEndPointImplLwIP::LwIPHandleIncomingConnection(void * arg, struct tcp_p
849
859
// Post a callback to the HandleConnectionReceived() function, passing it the new end point.
850
860
listenEP->Retain ();
851
861
conEP->Retain ();
862
+ // Hand over the implied ref from constructing mPreAllocatedConnectEP to
863
+ // ongoing connection.
864
+ listenEP->mPreAllocatedConnectEP = nullptr ;
865
+
852
866
err = lSystemLayer.ScheduleLambda ([listenEP, conEP] {
853
867
listenEP->HandleIncomingConnection (conEP);
868
+ // Pre-allocate another endpoint for next connection if current connection is established
869
+ CHIP_ERROR error = listenEP->GetEndPointManager ().NewEndPoint (&listenEP->mPreAllocatedConnectEP );
870
+ if (error != CHIP_NO_ERROR)
871
+ {
872
+ listenEP->HandleError (error);
873
+ }
854
874
conEP->Release ();
855
875
listenEP->Release ();
856
876
});
@@ -861,24 +881,6 @@ err_t TCPEndPointImplLwIP::LwIPHandleIncomingConnection(void * arg, struct tcp_p
861
881
err = CHIP_ERROR_CONNECTION_ABORTED;
862
882
conEP->Release (); // for the Retain() above
863
883
}
864
- else
865
- {
866
- // Pre-allocate another endpoint for next connection
867
- listenEP->mPreAllocatedConnectEP = nullptr ;
868
- listenEP->Retain ();
869
- err = lSystemLayer.ScheduleLambda ([listenEP]() {
870
- CHIP_ERROR error = listenEP->GetEndPointManager ().NewEndPoint (&listenEP->mPreAllocatedConnectEP );
871
- if (error != CHIP_NO_ERROR)
872
- {
873
- listenEP->HandleError (error);
874
- }
875
- listenEP->Release ();
876
- });
877
- if (err != CHIP_NO_ERROR)
878
- {
879
- listenEP->Release ();
880
- }
881
- }
882
884
}
883
885
884
886
// Otherwise, there was an error accepting the connection, so post a callback to the HandleError function.
0 commit comments