Skip to content

Commit

Permalink
avoid crash in FlexDMD tables by adding a delay. Seems like a bug in …
Browse files Browse the repository at this point in the history
…AsyncUDP.
  • Loading branch information
mkalkbrenner committed Jan 27, 2025
1 parent 642a3be commit b309ae3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
20 changes: 18 additions & 2 deletions src/ZeDMDWiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,23 @@ bool ZeDMDWiFi::DoConnect(const char* ip)
}

int flag = 1; // Disable Nagle's algorithm
m_tcpConnector->set_option(IPPROTO_TCP, TCP_NODELAY, flag);
if (!m_tcpConnector->set_option(IPPROTO_TCP, TCP_NODELAY, flag))
{
Log("%s", m_tcpConnector->last_error_str().c_str());
}
// Set QoS (IP_TOS)
int qos_value = 0x10; // Low delay DSCP value
if (!m_tcpConnector->set_option(IPPROTO_IP, IP_TOS, qos_value))
{
Log("%s", m_tcpConnector->last_error_str().c_str());
}
m_keepAliveInterval = std::chrono::milliseconds(ZEDMD_WIFI_TCP_KEEP_ALIVE_INTERVAL);
}
else
{
m_udpSocket = new sockpp::udp_socket();
m_udpServer = new sockpp::inet_address(ip, (in_port_t)port);
m_keepAliveInterval = std::chrono::milliseconds(ZEDMD_WIFI_UDP_KEEP_ALIVE_INTERVAL);
}

m_connected = true;
Expand Down Expand Up @@ -347,6 +358,7 @@ bool ZeDMDWiFi::SendChunks(uint8_t* pData, uint16_t size)
if (m_tcpConnector->write_n(pData, size) < 0)
{
Log("TCP stream error: %s", m_tcpConnector->last_error_str().c_str());
m_fullFrameFlag.store(true, std::memory_order_release);
return false;
}
}
Expand All @@ -357,15 +369,19 @@ bool ZeDMDWiFi::SendChunks(uint8_t* pData, uint16_t size)

while (sent < size && !m_stopFlag.load(std::memory_order_relaxed))
{
// std::this_thread::sleep_for(std::chrono::milliseconds(8));
int toSend = ((size - sent) < ZEDMD_WIFI_UDP_CHUNK_SIZE) ? size - sent : ZEDMD_WIFI_UDP_CHUNK_SIZE;
status = m_udpSocket->send_to(&pData[sent], (size_t)toSend, *m_udpServer);
if (status < toSend)
{
Log("UDP stream error: %s", m_udpSocket->last_error_str().c_str());
m_fullFrameFlag.store(true, std::memory_order_release);
return false;
}
sent += status;
// ESP32 crashes if too many packages arrive in a short time, mainly in FlexDMD tables.
// Even if the firmware has some protections and should ignore some packages, it crashes.
// It seems like a bug in AsyncUDP. At the moment, only that delay seems to avoid the crash.
std::this_thread::sleep_for(std::chrono::milliseconds(5));
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/ZeDMDWiFi.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
#include "sockpp/tcp_connector.h"
#include "sockpp/udp_socket.h"

#define ZEDMD_WIFI_KEEP_ALIVE_INTERVAL 100
#define ZEDMD_WIFI_TCP_KEEP_ALIVE_INTERVAL 100
#define ZEDMD_WIFI_UDP_KEEP_ALIVE_INTERVAL 3000
#define ZEDMD_WIFI_UDP_CHUNK_SIZE 1400

class ZeDMDWiFi : public ZeDMDComm
{
public:
ZeDMDWiFi() : ZeDMDComm() { m_keepAliveInterval = std::chrono::milliseconds(ZEDMD_WIFI_KEEP_ALIVE_INTERVAL); }
ZeDMDWiFi() : ZeDMDComm() {}

virtual bool Connect(const char* name_or_ip);
virtual void Disconnect();
Expand Down

0 comments on commit b309ae3

Please sign in to comment.