Skip to content

Commit

Permalink
[application] simply fdset update
Browse files Browse the repository at this point in the history
  • Loading branch information
Irving-cl committed Sep 3, 2024
1 parent 3874d96 commit eadd970
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 37 deletions.
6 changes: 2 additions & 4 deletions src/backbone_router/nd_proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,12 @@ void NdProxyManager::Update(MainloopContext &aMainloop)
{
if (mIcmp6RawSock >= 0)
{
FD_SET(mIcmp6RawSock, &aMainloop.mReadFdSet);
aMainloop.mMaxFd = std::max(aMainloop.mMaxFd, mIcmp6RawSock);
aMainloop.AddFdToReadSet(mIcmp6RawSock);
}

if (mUnicastNsQueueSock >= 0)
{
FD_SET(mUnicastNsQueueSock, &aMainloop.mReadFdSet);
aMainloop.mMaxFd = std::max(aMainloop.mMaxFd, mUnicastNsQueueSock);
aMainloop.AddFdToReadSet(mUnicastNsQueueSock);
}
}

Expand Down
32 changes: 32 additions & 0 deletions src/common/mainloop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,36 @@ MainloopProcessor::~MainloopProcessor(void)
{
MainloopManager::GetInstance().RemoveMainloopProcessor(this);
}

void MainloopContext::AddFdToReadSet(int aFd)
{
AddFdToSet(aFd, kReadFdSet);
}

void MainloopContext::AddFdToSet(int aFd, uint8_t aFdSetsMask)
{
bool isSet = false;

if (aFdSetsMask & kErrorFdSet)
{
FD_SET(aFd, &mErrorFdSet);
isSet = true;
}
if (aFdSetsMask & kReadFdSet)
{
FD_SET(aFd, &mReadFdSet);
isSet = true;
}
if (aFdSetsMask & kWriteFdSet)
{
FD_SET(aFd, &mWriteFdSet);
isSet = true;
}

if (isSet)
{
mMaxFd = std::max(mMaxFd, aFd);
}
}

} // namespace otbr
25 changes: 24 additions & 1 deletion src/common/mainloop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,30 @@ namespace otbr {
* This type defines the context data for running a mainloop.
*
*/
using MainloopContext = otSysMainloopContext;
class MainloopContext : public otSysMainloopContext
{
public:
static constexpr uint8_t kErrorFdSet = 1 << 0;
static constexpr uint8_t kReadFdSet = 1 << 1;
static constexpr uint8_t kWriteFdSet = 1 << 2;

/**
* This method adds a fd to the read fd set inside the MainloopContext.
*
* @param[in] aFd The fd to add.
*
*/
void AddFdToReadSet(int aFd);

/**
* This method adds a fd to the fd sets inside the MainloopContext.
*
* @param[in] aFd The fd to add.
* @param[in] aFdSetsMask A bitmask indicating which fd sets to add.
*
*/
void AddFdToSet(int aFd, uint8_t aFdSetsMask);
};

/**
* This abstract class defines the interface of a mainloop processor
Expand Down
3 changes: 1 addition & 2 deletions src/common/task_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ TaskRunner::TaskId TaskRunner::Post(Milliseconds aDelay, Task<void> aTask)

void TaskRunner::Update(MainloopContext &aMainloop)
{
FD_SET(mEventFd[kRead], &aMainloop.mReadFdSet);
aMainloop.mMaxFd = std::max(mEventFd[kRead], aMainloop.mMaxFd);
aMainloop.AddFdToReadSet(mEventFd[kRead]);

{
std::lock_guard<std::mutex> _(mTaskQueueMutex);
Expand Down
9 changes: 4 additions & 5 deletions src/dbus/server/dbus_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ void DBusAgent::Update(MainloopContext &aMainloop)
{
unsigned int flags;
int fd;
uint8_t fdSetMask = MainloopContext::kErrorFdSet;

if (dbus_connection_get_dispatch_status(mConnection.get()) == DBUS_DISPATCH_DATA_REMAINS)
{
Expand All @@ -162,17 +163,15 @@ void DBusAgent::Update(MainloopContext &aMainloop)

if (flags & DBUS_WATCH_READABLE)
{
FD_SET(fd, &aMainloop.mReadFdSet);
fdSetMask |= MainloopContext::kReadFdSet;
}

if ((flags & DBUS_WATCH_WRITABLE))
{
FD_SET(fd, &aMainloop.mWriteFdSet);
fdSetMask |= MainloopContext::kWriteFdSet;
}

FD_SET(fd, &aMainloop.mErrorFdSet);

aMainloop.mMaxFd = std::max(aMainloop.mMaxFd, fd);
aMainloop.AddFdToSet(fd, fdSetMask);
}
}

Expand Down
10 changes: 3 additions & 7 deletions src/mdns/mdns_mdnssd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,7 @@ void PublisherMDnsSd::Update(MainloopContext &aMainloop)

assert(fd != -1);

FD_SET(fd, &aMainloop.mReadFdSet);

aMainloop.mMaxFd = std::max(aMainloop.mMaxFd, fd);
aMainloop.AddFdToReadSet(fd);
}

for (const auto &service : mSubscribedServices)
Expand Down Expand Up @@ -419,8 +417,7 @@ void PublisherMDnsSd::DnssdServiceRegistration::Update(MainloopContext &aMainloo
fd = DNSServiceRefSockFD(mServiceRef);
VerifyOrExit(fd != -1);

FD_SET(fd, &aMainloop.mReadFdSet);
aMainloop.mMaxFd = std::max(aMainloop.mMaxFd, fd);
aMainloop.AddFdToReadSet(fd);

exit:
return;
Expand Down Expand Up @@ -1041,8 +1038,7 @@ void PublisherMDnsSd::ServiceRef::Update(MainloopContext &aMainloop) const

fd = DNSServiceRefSockFD(mServiceRef);
assert(fd != -1);
FD_SET(fd, &aMainloop.mReadFdSet);
aMainloop.mMaxFd = std::max(aMainloop.mMaxFd, fd);
aMainloop.AddFdToReadSet(fd);
exit:
return;
}
Expand Down
8 changes: 1 addition & 7 deletions src/ncp/posix/netif.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,7 @@ void Netif::UpdateFdSet(MainloopContext *aContext)
assert(mTunFd >= 0);
assert(mIpFd >= 0);

FD_SET(mTunFd, &aContext->mReadFdSet);
FD_SET(mTunFd, &aContext->mErrorFdSet);

if (mTunFd > aContext->mMaxFd)
{
aContext->mMaxFd = mTunFd;
}
aContext->AddFdToSet(mTunFd, MainloopContext::kErrorFdSet | MainloopContext::kReadFdSet);
}

void Netif::UpdateIp6UnicastAddresses(const std::vector<Ip6AddressInfo> &aAddrInfos)
Expand Down
8 changes: 1 addition & 7 deletions src/openwrt/ubus/otubus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1824,13 +1824,7 @@ void UBusAgent::Update(MainloopContext &aMainloop)
{
VerifyOrExit(otbr::ubus::sUbusEfd != -1);

FD_SET(otbr::ubus::sUbusEfd, &aMainloop.mReadFdSet);

if (aMainloop.mMaxFd < otbr::ubus::sUbusEfd)
{
aMainloop.mMaxFd = otbr::ubus::sUbusEfd;
}

aMainloop.AddFdToReadSet(otbr::ubus::sUbusEfd);
exit:
mThreadMutex.unlock();
return;
Expand Down
3 changes: 1 addition & 2 deletions src/rest/rest_web_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ void RestWebServer::Init(void)

void RestWebServer::Update(MainloopContext &aMainloop)
{
FD_SET(mListenFd, &aMainloop.mReadFdSet);
aMainloop.mMaxFd = std::max(aMainloop.mMaxFd, mListenFd);
aMainloop.AddFdToReadSet(mListenFd);

return;
}
Expand Down
3 changes: 1 addition & 2 deletions src/utils/infra_link_selector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,7 @@ void InfraLinkSelector::Update(MainloopContext &aMainloop)
{
if (mNetlinkSocket != -1)
{
FD_SET(mNetlinkSocket, &aMainloop.mReadFdSet);
aMainloop.mMaxFd = std::max(mNetlinkSocket, aMainloop.mMaxFd);
aMainloop.AddFdToReadSet(mNetlinkSocket);
}
}

Expand Down

0 comments on commit eadd970

Please sign in to comment.