From d1d51938efb4937f0dce271aeb3b702499508cfa Mon Sep 17 00:00:00 2001 From: Doug McLain Date: Sun, 18 Aug 2024 20:28:26 -0400 Subject: [PATCH] Send an unused TCPacket periodically from tcd to urfd as a simple keepalive solution --- reflector/TCPacketDef.h | 2 +- reflector/TCSocket.cpp | 33 ++++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/reflector/TCPacketDef.h b/reflector/TCPacketDef.h index a3681b7..68c251c 100644 --- a/reflector/TCPacketDef.h +++ b/reflector/TCPacketDef.h @@ -17,7 +17,7 @@ #include -enum class ECodecType : std::uint8_t { none = 0, dstar = 1, dmr = 2, c2_1600 = 3, c2_3200 = 4, p25 = 5, usrp = 6 }; +enum class ECodecType : std::uint8_t { none = 0, dstar = 1, dmr = 2, c2_1600 = 3, c2_3200 = 4, p25 = 5, usrp = 6, ping = 0xff }; using STCPacket = struct tcpacket_tag { uint32_t sequence; diff --git a/reflector/TCSocket.cpp b/reflector/TCSocket.cpp index 7f27a9d..93819d8 100644 --- a/reflector/TCSocket.cpp +++ b/reflector/TCSocket.cpp @@ -116,11 +116,18 @@ bool CTCServer::AnyAreClosed() const bool CTCSocket::Send(const STCPacket *packet) { - const auto pos = m_Modules.find(packet->module); + auto pos = m_Modules.find(packet->module); if (pos == std::string::npos) { - std::cerr << "Can't Send() this packet to unconfigured module '" << packet->module << "'" << std::endl; - return true; + if(packet->codec_in == ECodecType::ping) + { + pos = 0; // There is at least one transcoding module, use it to send the ping + } + else + { + std::cerr << "Can't Send() this packet to unconfigured module '" << packet->module << "'" << std::endl; + return true; + } } unsigned count = 0; auto data = (const unsigned char *)packet; @@ -214,7 +221,11 @@ bool CTCServer::Receive(char module, STCPacket *packet, int ms) if (rv) Close(pfds->fd); - return ! rv; + + if(packet->codec_in == ECodecType::ping) + return false; + else + return !rv; } bool CTCServer::Open(const std::string &address, const std::string &modules, uint16_t port) @@ -434,8 +445,12 @@ bool CTCClient::Connect(char module) return false; } -void CTCClient::ReConnect() +void CTCClient::ReConnect() // and sometimes ping { + static std::chrono::system_clock::time_point start = std::chrono::system_clock::now(); + auto now = std::chrono::system_clock::now(); + std::chrono::duration secs = now - start; + for (char m : m_Modules) { if (0 > GetFD(m)) @@ -447,6 +462,14 @@ void CTCClient::ReConnect() } } } + + if(secs.count() > 5.0) + { + STCPacket ping; + ping.codec_in = ECodecType::ping; + Send(&ping); + start = now; + } } void CTCClient::Receive(std::queue> &queue, int ms)