From 172f8569679ff277f8c9be98318457de8ac653d0 Mon Sep 17 00:00:00 2001 From: John Date: Tue, 30 May 2023 10:22:27 +0200 Subject: [PATCH] limit number of handled telegrams per loop and wait time for #843 --- src/ebusd/knxhandler.cpp | 21 +++++++++++---------- src/ebusd/knxhandler.h | 3 ++- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/ebusd/knxhandler.cpp b/src/ebusd/knxhandler.cpp index 082421d2..225c9300 100644 --- a/src/ebusd/knxhandler.cpp +++ b/src/ebusd/knxhandler.cpp @@ -451,10 +451,10 @@ void KnxHandler::sendGlobalValue(global_t index, unsigned int value, bool respon } result_t KnxHandler::receiveTelegram(int maxlen, knx_transfer_t* typ, uint8_t *buf, int *recvlen, - knx_addr_t *src, knx_addr_t *dest) { + knx_addr_t *src, knx_addr_t *dest, bool wait) { struct timespec tdiff = { - .tv_sec = 2, - .tv_nsec = 0, + .tv_sec = wait ? 2 : 0, // 2 seconds when waiting + .tv_nsec = wait ? 0 : 1000, // 1 milliseond when not waiting }; if (!m_con->isConnected()) { return RESULT_ERR_GENERIC_IO; @@ -919,18 +919,19 @@ void KnxHandler::run() { knx_addr_t src, dest; knx_transfer_t typ; // APDU data starting with octet 6 according to spec, contains 2 bits of application layer - result_t res = RESULT_OK; - do { - res = receiveTelegram(sizeof(data), &typ, data, &len, &src, &dest); + // limit number of read telegrams in order to give back control to outer loop for checking updates etc + for (int count = 0; count < 10; count++) { + // wait for telegram on first iteration only + result_t res = receiveTelegram(sizeof(data), &typ, data, &len, &src, &dest, count == 0); if (res != RESULT_OK) { if (res == RESULT_ERR_GENERIC_IO) { m_con->close(); } - } else { - needsWait = false; - handleReceivedTelegram(typ, src, dest, len, data); + break; } - } while (res == RESULT_OK); + needsWait = false; + handleReceivedTelegram(typ, src, dest, len, data); + } } if (!m_updatedMessages.empty()) { m_messages->lock(); diff --git a/src/ebusd/knxhandler.h b/src/ebusd/knxhandler.h index 25f81cb5..c2ad961d 100644 --- a/src/ebusd/knxhandler.h +++ b/src/ebusd/knxhandler.h @@ -170,11 +170,12 @@ class KnxHandler : public DataSink, public DataSource, public WaitThread { * @param recvlen pointer to a variable in which to store the actually received length. * @param src pointer to a variable in which to store the source address. * @param dest pointer to a variable in which to store the destination group address. + * @param wait true to wait up to 2 seconds for a new telegram, false to not wait. * @return the result code, either RESULT_OK on success, RESULT_ERR_GENERIC_IO on I/O error (e.g. socket closed), * or RESULT_ERR_TIMEOUT if no data is available. */ result_t receiveTelegram(int maxlen, knx_transfer_t* typ, uint8_t *buf, int *recvlen, knx_addr_t *src, - knx_addr_t *dest); + knx_addr_t *dest, bool wait = true); /** * Handle a received KNX telegram.