diff --git a/src/fne/HostFNE.cpp b/src/fne/HostFNE.cpp index 40b171b2..58165d33 100644 --- a/src/fne/HostFNE.cpp +++ b/src/fne/HostFNE.cpp @@ -15,6 +15,7 @@ #include "common/Log.h" #include "common/StopWatch.h" #include "common/Thread.h" +#include "common/ThreadFunc.h" #include "network/fne/TagDMRData.h" #include "network/fne/TagP25Data.h" #include "network/fne/TagNXDNData.h" @@ -180,6 +181,21 @@ int HostFNE::run() StopWatch stopWatch; stopWatch.start(); + // setup network loop threads + ThreadFunc networkLoop([&, this]() { + if (g_killed) + return; + + if (m_network != nullptr) { + while (!g_killed) { + m_network->processNetwork(); + Thread::sleep(5); + } + } + }); + networkLoop.run(); + networkLoop.setName("dvmfne:network-loop"); + // main execution loop while (!g_killed) { uint32_t ms = stopWatch.elapsed(); diff --git a/src/fne/network/FNENetwork.cpp b/src/fne/network/FNENetwork.cpp index 1d617444..1aaf25ee 100644 --- a/src/fne/network/FNENetwork.cpp +++ b/src/fne/network/FNENetwork.cpp @@ -142,10 +142,9 @@ void FNENetwork::setPresharedKey(const uint8_t* presharedKey) } /// -/// Updates the timer by the passed number of milliseconds. +/// Process a data frames from the network. /// -/// -void FNENetwork::clock(uint32_t ms) +void FNENetwork::processNetwork() { if (m_status != NET_STAT_MST_RUNNING) { return; @@ -153,44 +152,6 @@ void FNENetwork::clock(uint32_t ms) uint64_t now = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); - m_maintainenceTimer.clock(ms); - if (m_maintainenceTimer.isRunning() && m_maintainenceTimer.hasExpired()) { - // check to see if any peers have been quiet (no ping) longer than allowed - std::vector peersToRemove = std::vector(); - for (auto peer : m_peers) { - uint32_t id = peer.first; - FNEPeerConnection* connection = peer.second; - if (connection != nullptr) { - if (connection->connected()) { - uint64_t dt = connection->lastPing() + (m_host->m_pingTime * m_host->m_maxMissedPings); - if (dt < now) { - LogInfoEx(LOG_NET, "PEER %u timed out, dt = %u, now = %u", id, dt, now); - peersToRemove.push_back(id); - } - } - } - } - - // remove any peers - for (uint32_t peerId : peersToRemove) { - FNEPeerConnection* connection = m_peers[peerId]; - m_peers.erase(peerId); - if (connection != nullptr) { - delete connection; - } - - erasePeerAffiliations(peerId); - } - - // roll the RTP timestamp if no call is in progress - if (!m_callInProgress) { - frame::RTPHeader::resetStartTime(); - m_frameQueue->clearTimestamps(); - } - - m_maintainenceTimer.start(); - } - sockaddr_storage address; uint32_t addrLen; frame::RTPHeader rtpHeader; @@ -738,6 +699,57 @@ void FNENetwork::clock(uint32_t ms) return; } +/// +/// Updates the timer by the passed number of milliseconds. +/// +/// +void FNENetwork::clock(uint32_t ms) +{ + if (m_status != NET_STAT_MST_RUNNING) { + return; + } + + uint64_t now = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); + + m_maintainenceTimer.clock(ms); + if (m_maintainenceTimer.isRunning() && m_maintainenceTimer.hasExpired()) { + // check to see if any peers have been quiet (no ping) longer than allowed + std::vector peersToRemove = std::vector(); + for (auto peer : m_peers) { + uint32_t id = peer.first; + FNEPeerConnection* connection = peer.second; + if (connection != nullptr) { + if (connection->connected()) { + uint64_t dt = connection->lastPing() + (m_host->m_pingTime * m_host->m_maxMissedPings); + if (dt < now) { + LogInfoEx(LOG_NET, "PEER %u timed out, dt = %u, now = %u", id, dt, now); + peersToRemove.push_back(id); + } + } + } + } + + // remove any peers + for (uint32_t peerId : peersToRemove) { + FNEPeerConnection* connection = m_peers[peerId]; + m_peers.erase(peerId); + if (connection != nullptr) { + delete connection; + } + + erasePeerAffiliations(peerId); + } + + // roll the RTP timestamp if no call is in progress + if (!m_callInProgress) { + frame::RTPHeader::resetStartTime(); + m_frameQueue->clearTimestamps(); + } + + m_maintainenceTimer.start(); + } +} + /// /// Opens connection to the network. /// @@ -899,6 +911,8 @@ void* FNENetwork::threadedACLUpdate(void* arg) /// void FNENetwork::writeWhitelistRIDs(uint32_t peerId) { + uint64_t now = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); + // send radio ID white/black lists std::vector ridWhitelist; @@ -954,6 +968,8 @@ void FNENetwork::writeWhitelistRIDs(uint32_t peerId) writePeerCommand(peerId, { NET_FUNC_MASTER, NET_MASTER_SUBFUNC_WL_RID }, payload, bufSize, true); } + + connection->lastPing(now); } } @@ -963,6 +979,8 @@ void FNENetwork::writeWhitelistRIDs(uint32_t peerId) /// void FNENetwork::writeBlacklistRIDs(uint32_t peerId) { + uint64_t now = std::chrono::duration_cast(std::chrono::system_clock::now().time_since_epoch()).count(); + // send radio ID blacklist std::vector ridBlacklist; @@ -1018,6 +1036,8 @@ void FNENetwork::writeBlacklistRIDs(uint32_t peerId) writePeerCommand(peerId, { NET_FUNC_MASTER, NET_MASTER_SUBFUNC_BL_RID }, payload, bufSize, true); } + + connection->lastPing(now); } } diff --git a/src/fne/network/FNENetwork.h b/src/fne/network/FNENetwork.h index c66d536d..603b2467 100644 --- a/src/fne/network/FNENetwork.h +++ b/src/fne/network/FNENetwork.h @@ -187,6 +187,9 @@ namespace network /// Sets endpoint preshared encryption key. void setPresharedKey(const uint8_t* presharedKey); + /// Process a data frames from the network. + void processNetwork(); + /// Updates the timer by the passed number of milliseconds. void clock(uint32_t ms) override;