diff --git a/src/inet/EndPointBasis.cpp b/src/inet/EndPointBasis.cpp index ba36b53457c138..1fe33c05b0e35e 100644 --- a/src/inet/EndPointBasis.cpp +++ b/src/inet/EndPointBasis.cpp @@ -41,6 +41,7 @@ void EndPointBasis::InitEndPointBasis(InetLayer & aInetLayer, void * aAppState) #if CHIP_SYSTEM_CONFIG_USE_SOCKETS mSocket = INET_INVALID_SOCKET_FD; mPendingIO.Clear(); + mRequestIO.Clear(); #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS } diff --git a/src/inet/EndPointBasis.h b/src/inet/EndPointBasis.h index 13853de80fb0c4..f495bdef4e76dd 100644 --- a/src/inet/EndPointBasis.h +++ b/src/inet/EndPointBasis.h @@ -95,7 +95,8 @@ class DLL_EXPORT EndPointBasis : public InetLayerBasis #if CHIP_SYSTEM_CONFIG_USE_SOCKETS int mSocket; /**< Encapsulated socket descriptor. */ IPAddressType mAddrType; /**< Protocol family, i.e. IPv4 or IPv6. */ - SocketEvents mPendingIO; /**< Socket event masks */ + SocketEvents mPendingIO; /**< Socket event masks (read/write/error) currently available */ + SocketEvents mRequestIO; /**< Socket event masks (read/write) to wait for */ #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS #if CHIP_SYSTEM_CONFIG_USE_LWIP diff --git a/src/inet/IPEndPointBasis.cpp b/src/inet/IPEndPointBasis.cpp index 81d4add6920fbe..aa4ddcf9726970 100644 --- a/src/inet/IPEndPointBasis.cpp +++ b/src/inet/IPEndPointBasis.cpp @@ -1078,16 +1078,6 @@ INET_ERROR IPEndPointBasis::GetSocket(IPAddressType aAddressType, int aType, int return INET_NO_ERROR; } -SocketEvents IPEndPointBasis::PrepareIO() -{ - SocketEvents res; - - if (mState == kState_Listening && OnMessageReceived != nullptr) - res.SetRead(); - - return res; -} - void IPEndPointBasis::HandlePendingIO(uint16_t aPort) { INET_ERROR lStatus = INET_NO_ERROR; diff --git a/src/inet/IPEndPointBasis.h b/src/inet/IPEndPointBasis.h index 2bf54dd5ffca90..eb76fb12510e0e 100644 --- a/src/inet/IPEndPointBasis.h +++ b/src/inet/IPEndPointBasis.h @@ -93,9 +93,6 @@ class DLL_EXPORT IPEndPointBasis : public EndPointBasis typedef void (*OnMessageReceivedFunct)(IPEndPointBasis * endPoint, chip::System::PacketBufferHandle msg, const IPPacketInfo * pktInfo); - /** The endpoint's message reception event handling function delegate. */ - OnMessageReceivedFunct OnMessageReceived; - /** * @brief Type of reception error event handling function. * @@ -109,9 +106,6 @@ class DLL_EXPORT IPEndPointBasis : public EndPointBasis */ typedef void (*OnReceiveErrorFunct)(IPEndPointBasis * endPoint, INET_ERROR err, const IPPacketInfo * pktInfo); - /** The endpoint's receive error event handling function delegate. */ - OnReceiveErrorFunct OnReceiveError; - INET_ERROR SetMulticastLoopback(IPVersion aIPVersion, bool aLoopback); INET_ERROR JoinMulticastGroup(InterfaceId aInterfaceId, const IPAddress & aAddress); INET_ERROR LeaveMulticastGroup(InterfaceId aInterfaceId, const IPAddress & aAddress); @@ -119,6 +113,12 @@ class DLL_EXPORT IPEndPointBasis : public EndPointBasis protected: void Init(InetLayer * aInetLayer); + /** The endpoint's message reception event handling function delegate. */ + OnMessageReceivedFunct OnMessageReceived; + + /** The endpoint's receive error event handling function delegate. */ + OnReceiveErrorFunct OnReceiveError; + #if CHIP_SYSTEM_CONFIG_USE_LWIP public: static struct netif * FindNetifFromInterfaceId(InterfaceId aInterfaceId); @@ -138,7 +138,6 @@ class DLL_EXPORT IPEndPointBasis : public EndPointBasis INET_ERROR BindInterface(IPAddressType aAddressType, InterfaceId aInterfaceId); INET_ERROR SendMsg(const IPPacketInfo * aPktInfo, chip::System::PacketBufferHandle aBuffer, uint16_t aSendFlags); INET_ERROR GetSocket(IPAddressType aAddressType, int aType, int aProtocol); - SocketEvents PrepareIO(); void HandlePendingIO(uint16_t aPort); #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS diff --git a/src/inet/InetLayer.cpp b/src/inet/InetLayer.cpp index f8a6b58202f1bd..5c9b1fd8570281 100644 --- a/src/inet/InetLayer.cpp +++ b/src/inet/InetLayer.cpp @@ -1108,7 +1108,7 @@ void InetLayer::PrepareSelect(int & nfds, fd_set * readfds, fd_set * writefds, f { RawEndPoint * lEndPoint = RawEndPoint::sPool.Get(*mSystemLayer, i); if ((lEndPoint != nullptr) && lEndPoint->IsCreatedByInetLayer(*this)) - lEndPoint->PrepareIO().SetFDs(lEndPoint->mSocket, nfds, readfds, writefds, exceptfds); + lEndPoint->mRequestIO.SetFDs(lEndPoint->mSocket, nfds, readfds, writefds, exceptfds); } #endif // INET_CONFIG_ENABLE_RAW_ENDPOINT @@ -1117,7 +1117,7 @@ void InetLayer::PrepareSelect(int & nfds, fd_set * readfds, fd_set * writefds, f { TCPEndPoint * lEndPoint = TCPEndPoint::sPool.Get(*mSystemLayer, i); if ((lEndPoint != nullptr) && lEndPoint->IsCreatedByInetLayer(*this)) - lEndPoint->PrepareIO().SetFDs(lEndPoint->mSocket, nfds, readfds, writefds, exceptfds); + lEndPoint->mRequestIO.SetFDs(lEndPoint->mSocket, nfds, readfds, writefds, exceptfds); } #endif // INET_CONFIG_ENABLE_TCP_ENDPOINT @@ -1126,7 +1126,7 @@ void InetLayer::PrepareSelect(int & nfds, fd_set * readfds, fd_set * writefds, f { UDPEndPoint * lEndPoint = UDPEndPoint::sPool.Get(*mSystemLayer, i); if ((lEndPoint != nullptr) && lEndPoint->IsCreatedByInetLayer(*this)) - lEndPoint->PrepareIO().SetFDs(lEndPoint->mSocket, nfds, readfds, writefds, exceptfds); + lEndPoint->mRequestIO.SetFDs(lEndPoint->mSocket, nfds, readfds, writefds, exceptfds); } #endif // INET_CONFIG_ENABLE_UDP_ENDPOINT } diff --git a/src/inet/InetLayerBasis.h b/src/inet/InetLayerBasis.h index 37c3a39bbc1812..d8951ac751e98a 100644 --- a/src/inet/InetLayerBasis.h +++ b/src/inet/InetLayerBasis.h @@ -104,7 +104,7 @@ inline void InetLayerBasis::InitInetLayerBasis(InetLayer & aInetLayer, void * aA class SocketEvents { public: - enum + enum : uint8_t { kRead = 0x01, /**< Bit flag indicating if there is a read event on a socket. */ kWrite = 0x02, /**< Bit flag indicating if there is a write event on a socket. */ diff --git a/src/inet/RawEndPoint.cpp b/src/inet/RawEndPoint.cpp index d8786eaf373cc5..3fc0cbf9367a3d 100644 --- a/src/inet/RawEndPoint.cpp +++ b/src/inet/RawEndPoint.cpp @@ -352,6 +352,10 @@ INET_ERROR RawEndPoint::BindIPv6LinkLocal(InterfaceId intfId, const IPAddress & /** * @brief Prepare the endpoint to receive ICMP messages. * + * @param[in] onMessageReceived The endpoint's message reception event handling function delegate. + * @param[in] onReceiveError The endpoint's receive error event handling function delegate. + * @param[in] appState Application state pointer. + * * @retval INET_NO_ERROR always returned. * * @details @@ -363,7 +367,8 @@ INET_ERROR RawEndPoint::BindIPv6LinkLocal(InterfaceId intfId, const IPAddress & * On LwIP, this method must not be called with the LwIP stack lock * already acquired */ -INET_ERROR RawEndPoint::Listen() +INET_ERROR RawEndPoint::Listen(IPEndPointBasis::OnMessageReceivedFunct onMessageReceived, + IPEndPointBasis::OnReceiveErrorFunct onReceiveError, void * appState) { if (mState == kState_Listening) { @@ -375,6 +380,10 @@ INET_ERROR RawEndPoint::Listen() return INET_ERROR_INCORRECT_STATE; } + OnMessageReceived = onMessageReceived; + OnReceiveError = onReceiveError; + AppState = appState; + #if CHIP_SYSTEM_CONFIG_USE_LWIP // Lock LwIP stack @@ -401,6 +410,11 @@ INET_ERROR RawEndPoint::Listen() mState = kState_Listening; +#if CHIP_SYSTEM_CONFIG_USE_SOCKETS + // Wait for ability to read on this endpoint. + mRequestIO.SetRead(); +#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS + return INET_NO_ERROR; } @@ -453,6 +467,9 @@ void RawEndPoint::Close() // Clear any results from select() that indicate pending I/O for the socket. mPendingIO.Clear(); + // Do not wait for I/O on this endpoint. + mRequestIO.Clear(); + #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS mState = kState_Closed; @@ -995,11 +1012,6 @@ INET_ERROR RawEndPoint::GetSocket(IPAddressType aAddressType) return IPEndPointBasis::GetSocket(aAddressType, lType, lProtocol); } -SocketEvents RawEndPoint::PrepareIO() -{ - return (IPEndPointBasis::PrepareIO()); -} - void RawEndPoint::HandlePendingIO() { if (mState == kState_Listening && OnMessageReceived != nullptr && mPendingIO.IsReadable()) diff --git a/src/inet/RawEndPoint.h b/src/inet/RawEndPoint.h index d806184e8f1b9b..af8bac0cdcc3ca 100644 --- a/src/inet/RawEndPoint.h +++ b/src/inet/RawEndPoint.h @@ -74,7 +74,8 @@ class DLL_EXPORT RawEndPoint : public IPEndPointBasis INET_ERROR BindIPv6LinkLocal(InterfaceId intfId, const IPAddress & addr); INET_ERROR BindInterface(IPAddressType addrType, InterfaceId intfId); InterfaceId GetBoundInterface(); - INET_ERROR Listen(); + INET_ERROR Listen(IPEndPointBasis::OnMessageReceivedFunct onMessageReceived, + IPEndPointBasis::OnReceiveErrorFunct onReceiveError, void * appState = nullptr); INET_ERROR SendTo(const IPAddress & addr, chip::System::PacketBufferHandle && msg, uint16_t sendFlags = 0); INET_ERROR SendTo(const IPAddress & addr, InterfaceId intfId, chip::System::PacketBufferHandle && msg, uint16_t sendFlags = 0); INET_ERROR SendMsg(const IPPacketInfo * pktInfo, chip::System::PacketBufferHandle msg, uint16_t sendFlags = 0); @@ -107,7 +108,6 @@ class DLL_EXPORT RawEndPoint : public IPEndPointBasis #if CHIP_SYSTEM_CONFIG_USE_SOCKETS INET_ERROR GetSocket(IPAddressType addrType); - SocketEvents PrepareIO(); void HandlePendingIO(); #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS }; diff --git a/src/inet/TCPEndPoint.cpp b/src/inet/TCPEndPoint.cpp index 1aa7fbc12e59a8..5075563e7c5632 100644 --- a/src/inet/TCPEndPoint.cpp +++ b/src/inet/TCPEndPoint.cpp @@ -245,7 +245,9 @@ INET_ERROR TCPEndPoint::Bind(IPAddressType addrType, const IPAddress & addr, uin #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS if (res == INET_NO_ERROR) + { State = kState_Bound; + } return res; } @@ -254,10 +256,6 @@ INET_ERROR TCPEndPoint::Listen(uint16_t backlog) { INET_ERROR res = INET_NO_ERROR; -#if CHIP_SYSTEM_CONFIG_USE_SOCKETS - chip::System::Layer & lSystemLayer = SystemLayer(); -#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS - if (State != kState_Bound) return INET_ERROR_INCORRECT_STATE; @@ -278,8 +276,11 @@ INET_ERROR TCPEndPoint::Listen(uint16_t backlog) if (listen(mSocket, backlog) != 0) res = chip::System::MapErrorPOSIX(errno); + // Wait for ability to read on this endpoint. + mRequestIO.SetRead(); + // Wake the thread calling select so that it recognizes the new socket. - lSystemLayer.WakeSelect(); + SystemLayer().WakeSelect(); #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS @@ -493,11 +494,17 @@ INET_ERROR TCPEndPoint::Connect(const IPAddress & addr, uint16_t port, Interface if (conRes == 0) { State = kState_Connected; + // Wait for ability to read on this endpoint. + mRequestIO.SetRead(); if (OnConnectComplete != nullptr) OnConnectComplete(this, INET_NO_ERROR); } else + { State = kState_Connecting; + // Wait for ability to write on this endpoint. + mRequestIO.SetWrite(); + } // Wake the thread calling select so that it recognizes the new socket. lSystemLayer.WakeSelect(); @@ -699,9 +706,17 @@ INET_ERROR TCPEndPoint::Send(System::PacketBufferHandle data, bool push) } if (mSendQueue.IsNull()) + { mSendQueue = std::move(data); +#if CHIP_SYSTEM_CONFIG_USE_SOCKETS + // Wait for ability to write on this endpoint. + mRequestIO.SetWrite(); +#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS + } else + { mSendQueue->AddToEnd(std::move(data)); + } #if CHIP_SYSTEM_CONFIG_USE_LWIP @@ -1318,9 +1333,18 @@ INET_ERROR TCPEndPoint::DriveSending() MarkActive(); if (lenSent < bufLen) + { mSendQueue->ConsumeHead(lenSent); + } else + { mSendQueue.FreeHead(); + if (mSendQueue.IsNull()) + { + // Do not wait for ability to write on this endpoint. + mRequestIO.ClearWrite(); + } + } if (OnDataSent != nullptr) OnDataSent(this, lenSent); @@ -1418,6 +1442,13 @@ void TCPEndPoint::HandleConnectComplete(INET_ERROR err) MarkActive(); State = kState_Connected; + +#if CHIP_SYSTEM_CONFIG_USE_SOCKETS + // Wait for ability to read or write on this endpoint. + mRequestIO.SetRead(); + mRequestIO.SetWrite(); +#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS + if (OnConnectComplete != nullptr) OnConnectComplete(this, INET_NO_ERROR); } @@ -1547,6 +1578,7 @@ INET_ERROR TCPEndPoint::DoClose(INET_ERROR err, bool suppressCallback) if (close(mSocket) != 0 && err == INET_NO_ERROR) err = chip::System::MapErrorPOSIX(errno); mSocket = INET_INVALID_SOCKET_FD; + mRequestIO.Clear(); // Wake the thread calling select so that it recognizes the socket is closed. lSystemLayer.WakeSelect(); @@ -2327,26 +2359,6 @@ INET_ERROR TCPEndPoint::GetSocket(IPAddressType addrType) return INET_NO_ERROR; } -SocketEvents TCPEndPoint::PrepareIO() -{ - SocketEvents ioType; - - // If initiating a new connection... - // OR if connected and there is data to be sent... - // THEN arrange for the kernel to alert us when the socket is ready to be written. - if (State == kState_Connecting || (IsConnected() && !mSendQueue.IsNull())) - ioType.SetWrite(); - - // If listening for incoming connections and the app is ready to receive a connection... - // OR if in a state where receiving is allowed, and the app is ready to receive data... - // THEN arrange for the kernel to alert us when the socket is ready to be read. - if ((State == kState_Listening && OnConnectionReceived != nullptr) || - ((State == kState_Connected || State == kState_SendShutdown) && ReceiveEnabled && OnDataReceived != nullptr)) - ioType.SetRead(); - - return ioType; -} - void TCPEndPoint::HandlePendingIO() { // Prevent the end point from being freed while in the middle of a callback. @@ -2496,7 +2508,8 @@ void TCPEndPoint::ReceiveData() State = kState_ReceiveShutdown; else State = kState_Closing; - + // Do not wait for ability to read on this endpoint. + mRequestIO.ClearRead(); // Call the app's OnPeerClose. if (OnPeerClose != nullptr) OnPeerClose(this); @@ -2593,6 +2606,9 @@ void TCPEndPoint::HandleIncomingConnection() #endif // !INET_CONFIG_ENABLE_IPV4 conEP->Retain(); + // Wait for ability to read on this endpoint. + conEP->mRequestIO.SetRead(); + // Call the app's callback function. OnConnectionReceived(this, conEP, peerAddr, peerPort); } diff --git a/src/inet/TCPEndPoint.h b/src/inet/TCPEndPoint.h index 8c6b921a16d92c..bd379608316dd0 100644 --- a/src/inet/TCPEndPoint.h +++ b/src/inet/TCPEndPoint.h @@ -673,7 +673,6 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis #if CHIP_SYSTEM_CONFIG_USE_SOCKETS INET_ERROR GetSocket(IPAddressType addrType); - SocketEvents PrepareIO(); void HandlePendingIO(); void ReceiveData(); void HandleIncomingConnection(); diff --git a/src/inet/UDPEndPoint.cpp b/src/inet/UDPEndPoint.cpp index 7d7f6b042c5078..196f24df98389f 100644 --- a/src/inet/UDPEndPoint.cpp +++ b/src/inet/UDPEndPoint.cpp @@ -275,6 +275,10 @@ INET_ERROR UDPEndPoint::Bind(IPAddressType addrType, const IPAddress & addr, uin /** * @brief Prepare the endpoint to receive UDP messages. * + * @param[in] onMessageReceived The endpoint's message reception event handling function delegate. + * @param[in] onReceiveError The endpoint's receive error event handling function delegate. + * @param[in] appState Application state pointer. + * * @retval INET_NO_ERROR success: endpoint ready to receive messages. * @retval INET_ERROR_INCORRECT_STATE endpoint is already listening. * @@ -287,7 +291,7 @@ INET_ERROR UDPEndPoint::Bind(IPAddressType addrType, const IPAddress & addr, uin * On LwIP, this method must not be called with the LwIP stack lock * already acquired */ -INET_ERROR UDPEndPoint::Listen() +INET_ERROR UDPEndPoint::Listen(OnMessageReceivedFunct onMessageReceived, OnReceiveErrorFunct onReceiveError, void * appState) { if (mState == kState_Listening) { @@ -299,6 +303,10 @@ INET_ERROR UDPEndPoint::Listen() return INET_ERROR_INCORRECT_STATE; } + OnMessageReceived = onMessageReceived; + OnReceiveError = onReceiveError; + AppState = appState; + #if CHIP_SYSTEM_CONFIG_USE_LWIP // Lock LwIP stack @@ -334,6 +342,11 @@ INET_ERROR UDPEndPoint::Listen() mState = kState_Listening; +#if CHIP_SYSTEM_CONFIG_USE_SOCKETS + // Wait for ability to read on this endpoint. + mRequestIO.SetRead(); +#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS + return INET_NO_ERROR; } @@ -386,6 +399,9 @@ void UDPEndPoint::Close() // Clear any results from select() that indicate pending I/O for the socket. mPendingIO.Clear(); + // Do not wait for I/O on this endpoint. + mRequestIO.Clear(); + #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS #if CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK @@ -885,11 +901,6 @@ INET_ERROR UDPEndPoint::GetSocket(IPAddressType aAddressType) return IPEndPointBasis::GetSocket(aAddressType, lType, lProtocol); } -SocketEvents UDPEndPoint::PrepareIO() -{ - return (IPEndPointBasis::PrepareIO()); -} - void UDPEndPoint::HandlePendingIO() { if (mState == kState_Listening && OnMessageReceived != nullptr && mPendingIO.IsReadable()) diff --git a/src/inet/UDPEndPoint.h b/src/inet/UDPEndPoint.h index d47ed9a18ed3d6..bf1f1b77a8e97d 100644 --- a/src/inet/UDPEndPoint.h +++ b/src/inet/UDPEndPoint.h @@ -56,7 +56,7 @@ class DLL_EXPORT UDPEndPoint : public IPEndPointBasis INET_ERROR BindInterface(IPAddressType addrType, InterfaceId intfId); InterfaceId GetBoundInterface(); uint16_t GetBoundPort(); - INET_ERROR Listen(); + INET_ERROR Listen(OnMessageReceivedFunct onMessageReceived, OnReceiveErrorFunct onReceiveError, void * appState = nullptr); INET_ERROR SendTo(const IPAddress & addr, uint16_t port, chip::System::PacketBufferHandle && msg, uint16_t sendFlags = 0); INET_ERROR SendTo(const IPAddress & addr, uint16_t port, InterfaceId intfId, chip::System::PacketBufferHandle && msg, uint16_t sendFlags = 0); @@ -87,7 +87,6 @@ class DLL_EXPORT UDPEndPoint : public IPEndPointBasis uint16_t mBoundPort; INET_ERROR GetSocket(IPAddressType addrType); - SocketEvents PrepareIO(); void HandlePendingIO(); #endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS }; diff --git a/src/inet/tests/TestInetEndPoint.cpp b/src/inet/tests/TestInetEndPoint.cpp index d8cb6e99ac2ebe..adde8615c83bfc 100644 --- a/src/inet/tests/TestInetEndPoint.cpp +++ b/src/inet/tests/TestInetEndPoint.cpp @@ -373,14 +373,14 @@ static void TestInetEndPointInternal(nlTestSuite * inSuite, void * inContext) // Listen after bind should succeed if the prior bind succeeded. - err = testRaw6EP->Listen(); + err = testRaw6EP->Listen(nullptr /*OnMessageReceived*/, nullptr /*OnReceiveError*/); NL_TEST_ASSERT(inSuite, (didBind && (err == INET_NO_ERROR)) || (!didBind && (err == INET_ERROR_INCORRECT_STATE))); didListen = (err == INET_NO_ERROR); // If the first listen succeeded, then the second listen should be successful. - err = testRaw6EP->Listen(); + err = testRaw6EP->Listen(nullptr /*OnMessageReceived*/, nullptr /*OnReceiveError*/); NL_TEST_ASSERT(inSuite, (didBind && didListen && (err == INET_NO_ERROR)) || (!didBind && (err == INET_ERROR_INCORRECT_STATE))); didListen = (err == INET_NO_ERROR); @@ -412,7 +412,7 @@ static void TestInetEndPointInternal(nlTestSuite * inSuite, void * inContext) #endif // INET_CONFIG_ENABLE_IPV4 // UdpEndPoint special cases to cover the error branch - err = testUDPEP->Listen(); + err = testUDPEP->Listen(nullptr /*OnMessageReceived*/, nullptr /*OnReceiveError*/); NL_TEST_ASSERT(inSuite, err == INET_ERROR_INCORRECT_STATE); err = testUDPEP->Bind(kIPAddressType_Unknown, addr_any, 3000); NL_TEST_ASSERT(inSuite, err == INET_ERROR_WRONG_ADDRESS_TYPE); @@ -428,8 +428,8 @@ static void TestInetEndPointInternal(nlTestSuite * inSuite, void * inContext) InterfaceId id = testUDPEP->GetBoundInterface(); NL_TEST_ASSERT(inSuite, id == intId); - err = testUDPEP->Listen(); - err = testUDPEP->Listen(); + err = testUDPEP->Listen(nullptr /*OnMessageReceived*/, nullptr /*OnReceiveError*/); + err = testUDPEP->Listen(nullptr /*OnMessageReceived*/, nullptr /*OnReceiveError*/); err = testUDPEP->Bind(kIPAddressType_IPv6, addr, 3000, intId); NL_TEST_ASSERT(inSuite, err == INET_ERROR_INCORRECT_STATE); err = testUDPEP->BindInterface(kIPAddressType_IPv6, intId); diff --git a/src/inet/tests/TestInetLayer.cpp b/src/inet/tests/TestInetLayer.cpp index 516abc908b0259..8b009c56a7d1bd 100644 --- a/src/inet/tests/TestInetLayer.cpp +++ b/src/inet/tests/TestInetLayer.cpp @@ -955,9 +955,6 @@ static void StartTest() lStatus = gInet.NewRawEndPoint(lIPVersion, lIPProtocol, &sRawIPEndPoint); INET_FAIL_ERROR(lStatus, "InetLayer::NewRawEndPoint failed"); - sRawIPEndPoint->OnMessageReceived = HandleRawMessageReceived; - sRawIPEndPoint->OnReceiveError = HandleRawReceiveError; - if (IsInterfaceIdPresent(gInterfaceId)) { lStatus = sRawIPEndPoint->BindInterface(lIPAddressType, gInterfaceId); @@ -969,9 +966,6 @@ static void StartTest() lStatus = gInet.NewUDPEndPoint(&sUDPIPEndPoint); INET_FAIL_ERROR(lStatus, "InetLayer::NewUDPEndPoint failed"); - sUDPIPEndPoint->OnMessageReceived = HandleUDPMessageReceived; - sUDPIPEndPoint->OnReceiveError = HandleUDPReceiveError; - if (IsInterfaceIdPresent(gInterfaceId)) { lStatus = sUDPIPEndPoint->BindInterface(lIPAddressType, gInterfaceId); @@ -992,7 +986,7 @@ static void StartTest() INET_FAIL_ERROR(lStatus, "RawEndPoint::SetICMPFilter failed"); } - lStatus = sRawIPEndPoint->Listen(); + lStatus = sRawIPEndPoint->Listen(HandleRawMessageReceived, HandleRawReceiveError); INET_FAIL_ERROR(lStatus, "RawEndPoint::Listen failed"); } else if (gOptFlags & kOptFlagUseUDPIP) @@ -1000,7 +994,7 @@ static void StartTest() lStatus = sUDPIPEndPoint->Bind(lIPAddressType, IPAddress::Any, kUDPPort); INET_FAIL_ERROR(lStatus, "UDPEndPoint::Bind failed"); - lStatus = sUDPIPEndPoint->Listen(); + lStatus = sUDPIPEndPoint->Listen(HandleUDPMessageReceived, HandleUDPReceiveError); INET_FAIL_ERROR(lStatus, "UDPEndPoint::Listen failed"); } else if (gOptFlags & kOptFlagUseTCPIP) diff --git a/src/inet/tests/TestInetLayerMulticast.cpp b/src/inet/tests/TestInetLayerMulticast.cpp index 04a06b6939379c..d49c67c7871cda 100644 --- a/src/inet/tests/TestInetLayerMulticast.cpp +++ b/src/inet/tests/TestInetLayerMulticast.cpp @@ -897,9 +897,6 @@ static void StartTest() lStatus = gInet.NewRawEndPoint(lIPVersion, lIPProtocol, &sRawIPEndPoint); INET_FAIL_ERROR(lStatus, "InetLayer::NewRawEndPoint failed"); - sRawIPEndPoint->OnMessageReceived = HandleRawMessageReceived; - sRawIPEndPoint->OnReceiveError = HandleRawReceiveError; - lStatus = sRawIPEndPoint->Bind(lIPAddressType, lAddress); INET_FAIL_ERROR(lStatus, "RawEndPoint::Bind failed"); @@ -915,7 +912,7 @@ static void StartTest() INET_FAIL_ERROR(lStatus, "RawEndPoint::BindInterface failed"); } - lStatus = sRawIPEndPoint->Listen(); + lStatus = sRawIPEndPoint->Listen(HandleRawMessageReceived, HandleRawReceiveError); INET_FAIL_ERROR(lStatus, "RawEndPoint::Listen failed"); lEndPoint = sRawIPEndPoint; @@ -925,9 +922,6 @@ static void StartTest() lStatus = gInet.NewUDPEndPoint(&sUDPIPEndPoint); INET_FAIL_ERROR(lStatus, "InetLayer::NewUDPEndPoint failed"); - sUDPIPEndPoint->OnMessageReceived = HandleUDPMessageReceived; - sUDPIPEndPoint->OnReceiveError = HandleUDPReceiveError; - lStatus = sUDPIPEndPoint->Bind(lIPAddressType, lAddress, kUDPPort); INET_FAIL_ERROR(lStatus, "UDPEndPoint::Bind failed"); @@ -937,7 +931,7 @@ static void StartTest() INET_FAIL_ERROR(lStatus, "UDPEndPoint::BindInterface failed"); } - lStatus = sUDPIPEndPoint->Listen(); + lStatus = sUDPIPEndPoint->Listen(HandleUDPMessageReceived, HandleUDPReceiveError); INET_FAIL_ERROR(lStatus, "UDPEndPoint::Listen failed"); lEndPoint = sUDPIPEndPoint; diff --git a/src/lib/mdns/minimal/Server.cpp b/src/lib/mdns/minimal/Server.cpp index c6432df0825c15..5212936f2d2614 100644 --- a/src/lib/mdns/minimal/Server.cpp +++ b/src/lib/mdns/minimal/Server.cpp @@ -154,10 +154,7 @@ CHIP_ERROR ServerBase::Listen(chip::Inet::InetLayer * inetLayer, ListenIterator ReturnErrorOnFailure(info->udp->Bind(addressType, chip::Inet::IPAddress::Any, port, interfaceId)); - info->udp->AppState = static_cast(this); - info->udp->OnMessageReceived = OnUdpPacketReceived; - - ReturnErrorOnFailure(info->udp->Listen()); + ReturnErrorOnFailure(info->udp->Listen(OnUdpPacketReceived, nullptr /*OnReceiveError*/, this)); CHIP_ERROR err = JoinMulticastGroup(interfaceId, info->udp, addressType); if (err != CHIP_NO_ERROR) diff --git a/src/transport/raw/UDP.cpp b/src/transport/raw/UDP.cpp index aa7846867e5104..c78da4136a317b 100644 --- a/src/transport/raw/UDP.cpp +++ b/src/transport/raw/UDP.cpp @@ -52,12 +52,10 @@ CHIP_ERROR UDP::Init(UdpListenParameters & params) err = mUDPEndPoint->Bind(params.GetAddressType(), Inet::IPAddress::Any, params.GetListenPort(), params.GetInterfaceId()); SuccessOrExit(err); - err = mUDPEndPoint->Listen(); + err = mUDPEndPoint->Listen(OnUdpReceive, nullptr /*onReceiveError*/, this); SuccessOrExit(err); - mUDPEndPoint->AppState = reinterpret_cast(this); - mUDPEndPoint->OnMessageReceived = OnUdpReceive; - mUDPEndpointType = params.GetAddressType(); + mUDPEndpointType = params.GetAddressType(); mState = State::kInitialized;