From e563ca01fe5987592f0532282519ba1907aa8a5a Mon Sep 17 00:00:00 2001 From: Stephane Poirier <stephane-poirier@synchrotron-soleil.fr> Date: Wed, 8 Feb 2023 15:26:35 +0100 Subject: [PATCH] [UDPListener] fixed timout after start that occure if start() is called later after start_undetached() --- include/yat/network/UDPListener.h | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/include/yat/network/UDPListener.h b/include/yat/network/UDPListener.h index 587b3d0..dcdd551 100755 --- a/include/yat/network/UDPListener.h +++ b/include/yat/network/UDPListener.h @@ -161,6 +161,7 @@ public: m_ignored_events = 0; m_expected_events = n; m_mode = n ? UDP_FINITE : UDP_INFINITE; + m_start_tmr.restart(); } //-------------------------------------------------------- @@ -259,12 +260,15 @@ protected: //- input data buffer yat::Socket::Data ib(2048); + //- Effective timout, can be different in order to manage a start while the wait of data. + yat::uint32 udp_tmo_ms = m_cfg.udp_tmo_ms; + //- (almost) infinite reading loop yat::uint32 udp_evt_number = 0; while ( m_go_on ) { //- wait for some input data - if ( sock.wait_input_data(m_cfg.udp_tmo_ms, false) ) + if ( sock.wait_input_data(udp_tmo_ms, false) ) { //- read input data yat::uint32 rb = sock.receive_from(ib); @@ -321,6 +325,21 @@ protected: //- are we running? if ( m_mode == UDP_STANDBY ) continue; + + //- is it too soon? It should be true only once for the first timeout after the start. + double remain_for_true_timeout = m_cfg.udp_tmo_ms - m_start_tmr.elapsed_msec(); + yat::fcout("remain_for_true_timeout={}").arg(remain_for_true_timeout); + if (remain_for_true_timeout > 0) + { + //- it's too soon, the timeout is set to a shorter value to compensate and we exit the iteration. + udp_tmo_ms = remain_for_true_timeout; + continue; + } + else + { + udp_tmo_ms = m_cfg.udp_tmo_ms; + } + //- call 'tmo' callback (if any) if ( ! m_cfg.tmo_callback.is_empty() ) { @@ -345,6 +364,7 @@ private: yat::uint32 m_ignored_events; yat::uint32 m_total_ignored_events; UDPListener::Config m_cfg; + yat::Timer m_start_tmr; }; } //- namespace yat -- GitLab