1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-07-27 18:02:17 +03:00

polledTimeout: add option to use CPU count instead of millis() (#5870)

* polledTimeout: add option to use CPU count instead of millis()

* use more "using" alias

* more c++/clear code, using typename (thanks @devyte)

* rename class name to include unit, introduce timeMax() and check it with assert()

* remove useless defines

* improve api readability, add micro-second unit

* update example

* mock: emulate getCycleCount, add/fix polledTimeout CI test

* + nano-seconds, assert -> message, comments, host test

* allow 0 for timeout (enables immediate timeout, fix division by 0)

* typo, set member instead of local variable

* unify error message

* slight change on checkExpired() allows "never expired"
also removed printed message, add YieldAndDelay, simplify calculations

* remove traces of debug.h/cpp in this PR

* include missing <limits> header

* back to original expired test, introduce boolean _neverExpires, fix reset(), getTimeout() is invalid

* fix expiredOneShot with _timeout==0 check

* reenable getTimeout()

* expose checkExpired with unit conversion

* fix timing comments, move critical code to iram

* add member ::neverExpires and use it where relevant

* improve clarity

* remove exposed checkExpired(), adapt LEAmDNS with equivalent

* add API ::resetToNeverExpires(), use it in LEAmDNS

* remove offending constness from ::flagged() LEAmDNS (due do API fix in PolledTimeout)

* simplify "Fast" base classes

* minor variable rename

* Fix examples

* compliance with good c++ manners

* minor changes for consistency

* add missing const

* expired() and bool() moved to iram

* constexpr compensation computing

* add/update comments

* move neverExpires and alwaysExpired
This commit is contained in:
david gauchard
2019-04-05 15:50:53 +02:00
committed by Develo
parent f0eb5509a0
commit 9a2ed274f3
15 changed files with 344 additions and 92 deletions

View File

@ -268,7 +268,7 @@ void loop(void) {
// Allow MDNS processing
MDNS.update();
static esp8266::polledTimeout::periodic timeout(UPDATE_CYCLE);
static esp8266::polledTimeout::periodicMs timeout(UPDATE_CYCLE);
if (timeout.expired()) {
if (hMDNSService) {

View File

@ -905,12 +905,12 @@ protected:
* stcProbeInformation
*/
struct stcProbeInformation {
enuProbingStatus m_ProbingStatus;
uint8_t m_u8SentCount; // Used for probes and announcements
esp8266::polledTimeout::oneShot m_Timeout; // Used for probes and announcements
//clsMDNSTimeFlag m_TimeFlag; // Used for probes and announcements
bool m_bConflict;
bool m_bTiebreakNeeded;
enuProbingStatus m_ProbingStatus;
uint8_t m_u8SentCount; // Used for probes and announcements
esp8266::polledTimeout::oneShotMs m_Timeout; // Used for probes and announcements
//clsMDNSTimeFlag m_TimeFlag; // Used for probes and announcements
bool m_bConflict;
bool m_bTiebreakNeeded;
MDNSHostProbeFn m_fnHostProbeResultCallback;
MDNSServiceProbeFn m_fnServiceProbeResultCallback;
@ -974,14 +974,14 @@ protected:
const timeoutLevel_t TIMEOUTLEVEL_INTERVAL = 5;
const timeoutLevel_t TIMEOUTLEVEL_FINAL = 100;
uint32_t m_u32TTL;
esp8266::polledTimeout::oneShot m_TTLTimeout;
timeoutLevel_t m_timeoutLevel;
uint32_t m_u32TTL;
esp8266::polledTimeout::oneShotMs m_TTLTimeout;
timeoutLevel_t m_timeoutLevel;
stcTTL(void);
bool set(uint32_t p_u32TTL);
bool flagged(void) const;
bool flagged(void);
bool restart(void);
bool prepareDeletion(void);
@ -1073,14 +1073,14 @@ protected:
#endif
};
stcMDNSServiceQuery* m_pNext;
stcMDNS_RRDomain m_ServiceTypeDomain; // eg. _http._tcp.local
MDNSServiceQueryCallbackFunc m_fnCallback;
bool m_bLegacyQuery;
uint8_t m_u8SentCount;
esp8266::polledTimeout::oneShot m_ResendTimeout;
bool m_bAwaitingAnswers;
stcAnswer* m_pAnswers;
stcMDNSServiceQuery* m_pNext;
stcMDNS_RRDomain m_ServiceTypeDomain; // eg. _http._tcp.local
MDNSServiceQueryCallbackFunc m_fnCallback;
bool m_bLegacyQuery;
uint8_t m_u8SentCount;
esp8266::polledTimeout::oneShotMs m_ResendTimeout;
bool m_bAwaitingAnswers;
stcAnswer* m_pAnswers;
stcMDNSServiceQuery(void);
~stcMDNSServiceQuery(void);

View File

@ -1047,7 +1047,7 @@ bool MDNSResponder::_updateProbeStatus(void) {
m_HostProbeInformation.m_ProbingStatus = ProbingStatus_InProgress;
}
else if ((ProbingStatus_InProgress == m_HostProbeInformation.m_ProbingStatus) && // Probing AND
(m_HostProbeInformation.m_Timeout.checkExpired(millis()))) { // Time for next probe
(m_HostProbeInformation.m_Timeout.expired())) { // Time for next probe
if (MDNS_PROBE_COUNT > m_HostProbeInformation.m_u8SentCount) { // Send next probe
if ((bResult = _sendHostProbe())) {
@ -1059,7 +1059,7 @@ bool MDNSResponder::_updateProbeStatus(void) {
else { // Probing finished
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Done host probing.\n")););
m_HostProbeInformation.m_ProbingStatus = ProbingStatus_Done;
m_HostProbeInformation.m_Timeout.reset(std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max());
m_HostProbeInformation.m_Timeout.resetToNeverExpires();
if (m_HostProbeInformation.m_fnHostProbeResultCallback) {
m_HostProbeInformation.m_fnHostProbeResultCallback(m_pcHostname, true);
}
@ -1071,7 +1071,7 @@ bool MDNSResponder::_updateProbeStatus(void) {
}
} // else: Probing already finished OR waiting for next time slot
else if ((ProbingStatus_Done == m_HostProbeInformation.m_ProbingStatus) &&
(m_HostProbeInformation.m_Timeout.checkExpired(std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max()))) {
(m_HostProbeInformation.m_Timeout.expired())) {
if ((bResult = _announce(true, false))) { // Don't announce services here
++m_HostProbeInformation.m_u8SentCount;
@ -1081,7 +1081,7 @@ bool MDNSResponder::_updateProbeStatus(void) {
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Announcing host (%lu).\n\n"), m_HostProbeInformation.m_u8SentCount););
}
else {
m_HostProbeInformation.m_Timeout.reset(std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max());
m_HostProbeInformation.m_Timeout.resetToNeverExpires();
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Done host announcing.\n\n")););
}
}
@ -1096,7 +1096,7 @@ bool MDNSResponder::_updateProbeStatus(void) {
pService->m_ProbeInformation.m_ProbingStatus = ProbingStatus_InProgress;
}
else if ((ProbingStatus_InProgress == pService->m_ProbeInformation.m_ProbingStatus) && // Probing AND
(pService->m_ProbeInformation.m_Timeout.checkExpired(millis()))) { // Time for next probe
(pService->m_ProbeInformation.m_Timeout.expired())) { // Time for next probe
if (MDNS_PROBE_COUNT > pService->m_ProbeInformation.m_u8SentCount) { // Send next probe
if ((bResult = _sendServiceProbe(*pService))) {
@ -1108,7 +1108,7 @@ bool MDNSResponder::_updateProbeStatus(void) {
else { // Probing finished
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Done service probing %s.%s.%s\n\n"), (pService->m_pcName ?: m_pcHostname), pService->m_pcService, pService->m_pcProtocol););
pService->m_ProbeInformation.m_ProbingStatus = ProbingStatus_Done;
pService->m_ProbeInformation.m_Timeout.reset(std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max());
pService->m_ProbeInformation.m_Timeout.resetToNeverExpires();
if (pService->m_ProbeInformation.m_fnServiceProbeResultCallback) {
pService->m_ProbeInformation.m_fnServiceProbeResultCallback(pService->m_pcName, pService, true);
}
@ -1119,7 +1119,7 @@ bool MDNSResponder::_updateProbeStatus(void) {
}
} // else: Probing already finished OR waiting for next time slot
else if ((ProbingStatus_Done == pService->m_ProbeInformation.m_ProbingStatus) &&
(pService->m_ProbeInformation.m_Timeout.checkExpired(millis()))) {
(pService->m_ProbeInformation.m_Timeout.expired())) {
if ((bResult = _announceService(*pService))) { // Announce service
++pService->m_ProbeInformation.m_u8SentCount;
@ -1129,7 +1129,7 @@ bool MDNSResponder::_updateProbeStatus(void) {
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Announcing service %s.%s.%s (%lu)\n\n"), (pService->m_pcName ?: m_pcHostname), pService->m_pcService, pService->m_pcProtocol, pService->m_ProbeInformation.m_u8SentCount););
}
else {
pService->m_ProbeInformation.m_Timeout.reset(std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max());
pService->m_ProbeInformation.m_Timeout.resetToNeverExpires();
DEBUG_EX_INFO(DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _updateProbeStatus: Done service announcing for %s.%s.%s\n\n"), (pService->m_pcName ?: m_pcHostname), pService->m_pcService, pService->m_pcProtocol););
}
}
@ -1441,13 +1441,13 @@ bool MDNSResponder::_checkServiceQueryCache(void) {
// Resend dynamic service queries, if not already done often enough
if ((!pServiceQuery->m_bLegacyQuery) &&
(MDNS_DYNAMIC_QUERY_RESEND_COUNT > pServiceQuery->m_u8SentCount) &&
(pServiceQuery->m_ResendTimeout.checkExpired(millis()))) {
(pServiceQuery->m_ResendTimeout.expired())) {
if ((bResult = _sendMDNSServiceQuery(*pServiceQuery))) {
++pServiceQuery->m_u8SentCount;
pServiceQuery->m_ResendTimeout.reset((MDNS_DYNAMIC_QUERY_RESEND_COUNT > pServiceQuery->m_u8SentCount)
? (MDNS_DYNAMIC_QUERY_RESEND_DELAY * (pServiceQuery->m_u8SentCount - 1))
: std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max());
: esp8266::polledTimeout::oneShotMs::neverExpires);
}
DEBUG_EX_INFO(
DEBUG_OUTPUT.printf_P(PSTR("[MDNSResponder] _checkServiceQueryCache: %s to resend service query!"), (bResult ? "Succeeded" : "FAILED"));

View File

@ -1159,7 +1159,7 @@ bool MDNSResponder::stcMDNS_RRAnswerGeneric::clear(void) {
MDNSResponder::stcProbeInformation::stcProbeInformation(void)
: m_ProbingStatus(ProbingStatus_WaitingForData),
m_u8SentCount(0),
m_Timeout(std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max()),
m_Timeout(esp8266::polledTimeout::oneShotMs::neverExpires),
m_bConflict(false),
m_bTiebreakNeeded(false),
m_fnHostProbeResultCallback(0),
@ -1173,7 +1173,7 @@ bool MDNSResponder::stcProbeInformation::clear(bool p_bClearUserdata /*= false*/
m_ProbingStatus = ProbingStatus_WaitingForData;
m_u8SentCount = 0;
m_Timeout.reset(std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max());
m_Timeout.resetToNeverExpires();
m_bConflict = false;
m_bTiebreakNeeded = false;
if (p_bClearUserdata) {
@ -1421,7 +1421,7 @@ bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::isOutdated(void) con
*/
MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::stcTTL(void)
: m_u32TTL(0),
m_TTLTimeout(std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max()),
m_TTLTimeout(esp8266::polledTimeout::oneShotMs::neverExpires),
m_timeoutLevel(TIMEOUTLEVEL_UNSET) {
}
@ -1438,7 +1438,7 @@ bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::set(uint32_t p_u32TT
}
else {
m_timeoutLevel = TIMEOUTLEVEL_UNSET; // undef
m_TTLTimeout.reset(std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max());
m_TTLTimeout.resetToNeverExpires();
}
return true;
}
@ -1446,11 +1446,11 @@ bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::set(uint32_t p_u32TT
/*
* MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::flagged
*/
bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::flagged(void) const {
bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::flagged(void) {
return ((m_u32TTL) &&
(TIMEOUTLEVEL_UNSET != m_timeoutLevel) &&
(m_TTLTimeout.checkExpired(millis())));
(m_TTLTimeout.expired()));
}
/*
@ -1468,7 +1468,7 @@ bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::restart(void) {
}
else {
bResult = false;
m_TTLTimeout.reset(std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max());
m_TTLTimeout.resetToNeverExpires();
m_timeoutLevel = TIMEOUTLEVEL_UNSET;
}
return bResult;
@ -1498,7 +1498,7 @@ bool MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::finalTimeoutLevel(vo
*/
unsigned long MDNSResponder::stcMDNSServiceQuery::stcAnswer::stcTTL::timeout(void) const {
uint32_t u32Timeout = std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max();
uint32_t u32Timeout = esp8266::polledTimeout::oneShotMs::neverExpires;
if (TIMEOUTLEVEL_BASE == m_timeoutLevel) { // 80%
u32Timeout = (m_u32TTL * 800); // to milliseconds
@ -1922,7 +1922,7 @@ MDNSResponder::stcMDNSServiceQuery::stcMDNSServiceQuery(void)
m_fnCallback(0),
m_bLegacyQuery(false),
m_u8SentCount(0),
m_ResendTimeout(std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max()),
m_ResendTimeout(esp8266::polledTimeout::oneShotMs::neverExpires),
m_bAwaitingAnswers(true),
m_pAnswers(0) {
@ -1945,7 +1945,7 @@ bool MDNSResponder::stcMDNSServiceQuery::clear(void) {
m_fnCallback = 0;
m_bLegacyQuery = false;
m_u8SentCount = 0;
m_ResendTimeout.reset(std::numeric_limits<esp8266::polledTimeout::oneShot::timeType>::max());
m_ResendTimeout.resetToNeverExpires();
m_bAwaitingAnswers = true;
while (m_pAnswers) {
stcAnswer* pNext = m_pAnswers->m_pNext;