Skip to content

Commit 2ac0c41

Browse files
committed
[2] Inet: Use constructors for Inet EndPoints
#### Problem `Inet::TCPEndPoint` and `Inet::UDPEndPoint` historically could not use constructors because of `System::ObjectPool` limitations. Incidentally renamed `mAppState` for consistency (it had been in `System::Object` prior to project-chip#11428). This is a step toward project-chip#7715 _Virtualize System and Inet interfaces_, split off to reduce the complexity of an upcoming PR. #### Change overview Convert from `Init()` to constructors. Transitionally, the constructors still call a per-implementation function `InitImpl()`. #### Testing CI; no changes to functionality.
1 parent fe0dfbf commit 2ac0c41

9 files changed

+34
-33
lines changed

src/inet/EndPointBasis.h

+4-20
Original file line numberDiff line numberDiff line change
@@ -38,36 +38,20 @@ class InetLayer;
3838
class DLL_EXPORT EndPointBase
3939
{
4040
public:
41+
EndPointBase(InetLayer & aInetLayer, void * aAppState = nullptr) : AppState(aAppState), mInetLayer(&aInetLayer) {}
42+
virtual ~EndPointBase() = default;
43+
4144
/**
4245
* Returns a reference to the Inet layer object that owns this basis object.
4346
*/
4447
InetLayer & Layer() const { return *mInetLayer; }
4548

46-
/**
47-
* Returns \c true if the basis object was obtained by the specified Inet layer instance.
48-
*
49-
* @note
50-
* Does not check whether the object is actually obtained by the system layer instance associated with the Inet layer
51-
* instance. It merely tests whether \c aInetLayer is the Inet layer instance that was provided to \c InitInetLayerBasis.
52-
*/
5349
bool IsCreatedByInetLayer(const InetLayer & aInetLayer) const { return mInetLayer == &aInetLayer; }
5450

5551
void * AppState;
5652

5753
private:
58-
InetLayer * mInetLayer; /**< Pointer to the InetLayer object that owns this object. */
59-
60-
protected:
61-
virtual ~EndPointBase() = default;
62-
63-
void InitEndPointBasis(InetLayer & aInetLayer, void * aAppState = nullptr)
64-
{
65-
AppState = aAppState;
66-
mInetLayer = &aInetLayer;
67-
InitEndPointBasisImpl();
68-
}
69-
70-
virtual void InitEndPointBasisImpl() = 0;
54+
InetLayer * mInetLayer; /**< InetLayer object that owns this object. */
7155
};
7256

7357
} // namespace Inet

src/inet/EndPointBasisImplLwIP.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ namespace Inet {
3535
class DLL_EXPORT EndPointImplLwIP : public EndPointBase
3636
{
3737
protected:
38-
// EndPointBase overrides
39-
void InitEndPointBasisImpl() override { mLwIPEndPointType = LwIPEndPointType::Unknown; }
38+
EndPointImplLwIP(InetLayer & inetLayer, void * appState = nullptr) :
39+
EndPointBase(inetLayer, appState), mLwIPEndPointType(LwIPEndPointType::Unknown)
40+
{}
4041

4142
/** Encapsulated LwIP protocol control block */
4243
union

src/inet/EndPointBasisImplNetworkFramework.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace Inet {
3434
class DLL_EXPORT EndPointImplNetworkFramework : public EndPointBase
3535
{
3636
protected:
37-
void InitEndPointBasisImpl() override {}
37+
EndPointImplNetworkFramework(InetLayer & inetLayer, void * appState = nullptr) : EndPointBase(inetLayer, appState) {}
3838

3939
nw_parameters_t mParameters;
4040
IPAddressType mAddrType; /**< Protocol family, i.e. IPv4 or IPv6. */

src/inet/EndPointBasisImplSockets.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ namespace Inet {
3333
class DLL_EXPORT EndPointImplSockets : public EndPointBase
3434
{
3535
protected:
36-
void InitEndPointBasisImpl() override { mSocket = kInvalidSocketFd; }
36+
EndPointImplSockets(InetLayer & inetLayer, void * appState = nullptr) :
37+
EndPointBase(inetLayer, appState), mSocket(kInvalidSocketFd)
38+
{}
3739

3840
static constexpr int kInvalidSocketFd = -1;
3941
int mSocket; /**< Encapsulated socket descriptor. */

src/inet/InetLayer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ CHIP_ERROR InetLayer::NewTCPEndPoint(TCPEndPoint ** retEndPoint)
429429

430430
VerifyOrReturnError(mLayerState.IsInitialized(), CHIP_ERROR_INCORRECT_STATE);
431431

432-
*retEndPoint = TCPEndPoint::sPool.CreateObject();
432+
*retEndPoint = TCPEndPoint::sPool.CreateObject(*this);
433433
if (*retEndPoint == nullptr)
434434
{
435435
ChipLogError(Inet, "%s endpoint pool FULL", "TCP");
@@ -469,7 +469,7 @@ CHIP_ERROR InetLayer::NewUDPEndPoint(UDPEndPoint ** retEndPoint)
469469

470470
VerifyOrReturnError(mLayerState.IsInitialized(), CHIP_ERROR_INCORRECT_STATE);
471471

472-
*retEndPoint = UDPEndPoint::sPool.CreateObject();
472+
*retEndPoint = UDPEndPoint::sPool.CreateObject(*this);
473473
if (*retEndPoint == nullptr)
474474
{
475475
ChipLogError(Inet, "%s endpoint pool FULL", "UDP");

src/inet/TCPEndPoint.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -2596,8 +2596,6 @@ bool TCPEndPoint::IsConnected(State state)
25962596

25972597
void TCPEndPoint::Init(InetLayer * inetLayer)
25982598
{
2599-
InitEndPointBasis(*inetLayer);
2600-
26012599
mReceiveEnabled = true;
26022600

26032601
// Initialize to zero for using system defaults.

src/inet/TCPEndPoint.h

+20-3
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,23 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis, public ReferenceCounted<TCP
7474
friend class TCPTest;
7575

7676
public:
77-
TCPEndPoint() = default;
77+
TCPEndPoint(InetLayer & inetLayer, void * appState = nullptr) :
78+
EndPointBasis(inetLayer, appState), mState(State::kReady), mReceiveEnabled(true),
79+
mConnectTimeoutMsecs(0) // Initialize to zero for using system defaults.
80+
#if INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
81+
,
82+
mUserTimeoutMillis(INET_CONFIG_DEFAULT_TCP_USER_TIMEOUT_MSEC), mUserTimeoutTimerRunning(false)
83+
#if INET_CONFIG_ENABLE_TCP_SEND_IDLE_CALLBACKS
84+
,
85+
mIsTCPSendIdle(true), mTCPSendQueuePollPeriodMillis(INET_CONFIG_TCP_SEND_QUEUE_POLL_INTERVAL_MSEC),
86+
mTCPSendQueueRemainingPollCount(
87+
MaxTCPSendQueuePolls(INET_CONFIG_DEFAULT_TCP_USER_TIMEOUT_MSEC, INET_CONFIG_TCP_SEND_QUEUE_POLL_INTERVAL_MSEC)),
88+
OnTCPSendIdleChanged(nullptr)
89+
#endif // INET_CONFIG_ENABLE_TCP_SEND_IDLE_CALLBACKS
90+
#endif // INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
91+
{
92+
InitImpl();
93+
}
7894

7995
/**
8096
* @brief Bind the endpoint to an interface IP address.
@@ -644,13 +660,14 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis, public ReferenceCounted<TCP
644660
void ScheduleNextTCPUserTimeoutPoll(uint32_t aTimeOut);
645661

646662
#if INET_CONFIG_ENABLE_TCP_SEND_IDLE_CALLBACKS
647-
uint16_t MaxTCPSendQueuePolls(void)
663+
static constexpr uint16_t MaxTCPSendQueuePolls(uint16_t userTimeout, uint16_t pollPeriod)
648664
{
649665
// If the UserTimeout is configured less than or equal to the poll interval,
650666
// return 1 to poll at least once instead of returning zero and timing out
651667
// immediately.
652-
return (mUserTimeoutMillis > mTCPSendQueuePollPeriodMillis) ? (mUserTimeoutMillis / mTCPSendQueuePollPeriodMillis) : 1;
668+
return (userTimeout > pollPeriod) ? (userTimeout / pollPeriod) : 1;
653669
}
670+
uint16_t MaxTCPSendQueuePolls(void) { return MaxTCPSendQueuePolls(mUserTimeoutMillis, mTCPSendQueuePollPeriodMillis); }
654671
#endif // INET_CONFIG_ENABLE_TCP_SEND_IDLE_CALLBACKS
655672

656673
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS

src/inet/UDPEndPoint.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -2030,7 +2030,6 @@ void UDPEndPoint::Close()
20302030

20312031
void UDPEndPoint::Init(InetLayer * inetLayer)
20322032
{
2033-
InitEndPointBasis(*inetLayer);
20342033
InitImpl();
20352034
}
20362035

src/inet/UDPEndPoint.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class UDPEndPointDeletor
6363
class DLL_EXPORT UDPEndPoint : public EndPointBasis, public ReferenceCounted<UDPEndPoint, UDPEndPointDeletor>
6464
{
6565
public:
66-
UDPEndPoint() = default;
66+
UDPEndPoint(InetLayer & inetLayer, void * appState = nullptr) : EndPointBasis(inetLayer, appState) { InitImpl(); }
6767

6868
UDPEndPoint(const UDPEndPoint &) = delete;
6969
UDPEndPoint(UDPEndPoint &&) = delete;

0 commit comments

Comments
 (0)