From 083f0734fa23f3b6318fede66cf3d74d0b6d2d6d Mon Sep 17 00:00:00 2001 From: Jochen Vothknecht <jochen3120@gmail.com> Date: Wed, 13 Apr 2022 08:35:47 +0200 Subject: [PATCH] Implemented rate-limiting feature for udp packet logging (untested atm) --- qthing/network/udp.cpp | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/qthing/network/udp.cpp b/qthing/network/udp.cpp index 0c9a227..6c1d9f3 100644 --- a/qthing/network/udp.cpp +++ b/qthing/network/udp.cpp @@ -6,12 +6,16 @@ #include "lwip/err.h" #include "lwip/sockets.h" #include "lwip/sys.h" +#include "qthing/util.hpp" #include "qthing/qthing_legacy.hpp" static const char* TAG = "udp-server"; static const in_port_t PORT = 4213; +// rate-limits positive udp log messages; time in milliseconds +static const uint16_t loggingTimeout = 500; // TODO: make configurable?! + static std::map<std::string, qthing::udpPacketCallback> packetCallbackMap; void qthing::addUDPPacketCallback(std::string magicString, qthing::udpPacketCallback callback) { @@ -58,6 +62,11 @@ static void udp_server_task(void* pvParameters) { ESP_LOGI(TAG, "Socket bound"); ESP_LOGI(TAG, "Waiting for data"); + // This is for the rate limited logging only + uint32_t loggingBytes = 0; + uint16_t loggingPackets = 0; + uint32_t loggingLastMillis = qthing::millis(); + while (true) { struct sockaddr_in6 sourceAddr; // Large enough for both IPv4 or IPv6 socklen_t socklen = sizeof(sourceAddr); @@ -72,7 +81,22 @@ static void udp_server_task(void* pvParameters) { } // Data received else { - ESP_LOGI(TAG, "Received %d bytes from %s", len, qthing::to_string(sourceAddr).c_str()); + + if (loggingTimeout == 0) { + ESP_LOGI(TAG, "Received %d bytes from %s", len, qthing::to_string(sourceAddr).c_str()); + } else { + loggingBytes += len; + loggingPackets += 1; + + if (qthing::millis() - loggingLastMillis > loggingTimeout) { + + ESP_LOGI(TAG, "Received %d bytes in %d packets", loggingBytes, loggingPackets); + + loggingBytes = 0; + loggingPackets = 0; + loggingLastMillis = qthing::millis(); + } + } std::string payload_str(rx_buffer); int16_t lenBestMatch = -1; -- GitLab