diff --git a/src/terminal_api_driver.cpp b/src/terminal_api_driver.cpp index 980219b..6a7052c 100644 --- a/src/terminal_api_driver.cpp +++ b/src/terminal_api_driver.cpp @@ -15,6 +15,8 @@ typedef boost::property_tree::ptree::path_type json_path; static constexpr const char* DEFAULT_QOS_CLASSES = R"({"rt1":[],"rt2":[],"rt3":[],"cd":[]})"; +// пороговое значение сна +static constexpr int64_t SLEEP_THRESHOLD = 10; // static int calculateSubnetMask(const std::string& subnet_mask) { // int mask = 0; @@ -463,7 +465,7 @@ private: bool checkNeedUpdate(int64_t now) const { if (periodMs < 0) return false; // тут нет смысла спать меньше чем на 20мс, поэтому можно разрешить чтение на некоторое время раньше - return now - lastUpdate >= (periodMs - 20); + return now - lastUpdate >= (periodMs - SLEEP_THRESHOLD); } int64_t getNextUpdate(int64_t now) const { @@ -543,17 +545,19 @@ private: auto now = TIME_NOW(); for (auto& u: updaters) { if (u.checkNeedUpdate(now)) { - // выравнивание таймингов (-20ms...+50ms) - if (u.lastUpdate + u.periodMs + 50 < now) { - u.lastUpdate += u.periodMs; + auto targetTime = u.lastUpdate + u.periodMs; + if (targetTime + SLEEP_THRESHOLD <= now && targetTime - SLEEP_THRESHOLD >= now) { + u.lastUpdate = targetTime; } else { u.lastUpdate = now; } + u.callback(); now = TIME_NOW(); } - - sleepTime = std::min(sleepTime, u.getNextUpdate(now)); + if (u.periodMs >= 0) { + sleepTime = std::min(sleepTime, u.getNextUpdate(now)); + } } if (sleepTime > 0) {