[e131] Drain all queued packets per loop iteration (#14133)

This commit is contained in:
J. Nick Koston
2026-02-19 20:35:42 -06:00
committed by GitHub
parent c1265a9490
commit afbc45bf32
2 changed files with 14 additions and 21 deletions
+5 -21
View File
@@ -70,27 +70,12 @@ void E131Component::loop() {
E131Packet packet; E131Packet packet;
int universe = 0; int universe = 0;
uint8_t buf[1460]; uint8_t buf[1460];
ssize_t len;
#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS) // Drain all queued packets so multi-universe frames are applied
ssize_t len = this->socket_->read(buf, sizeof(buf)); // atomically before the light writes. Without this, each universe
if (len == -1) { // packet would trigger a separate full-strip write causing tearing.
return; while ((len = this->read_(buf, sizeof(buf))) > 0) {
}
if (!this->packet_(buf, (size_t) len, universe, packet)) {
ESP_LOGV(TAG, "Invalid packet received of size %d.", (int) len);
return;
}
if (!this->process_(universe, packet)) {
ESP_LOGV(TAG, "Ignored packet for %d universe of size %d.", universe, packet.count);
}
#elif defined(USE_SOCKET_IMPL_LWIP_TCP)
while (auto packet_size = this->udp_.parsePacket()) {
auto len = this->udp_.read(buf, sizeof(buf));
if (len <= 0)
continue;
if (!this->packet_(buf, (size_t) len, universe, packet)) { if (!this->packet_(buf, (size_t) len, universe, packet)) {
ESP_LOGV(TAG, "Invalid packet received of size %d.", (int) len); ESP_LOGV(TAG, "Invalid packet received of size %d.", (int) len);
continue; continue;
@@ -100,7 +85,6 @@ void E131Component::loop() {
ESP_LOGV(TAG, "Ignored packet for %d universe of size %d.", universe, packet.count); ESP_LOGV(TAG, "Ignored packet for %d universe of size %d.", universe, packet.count);
} }
} }
#endif
} }
void E131Component::add_effect(E131AddressableLightEffect *light_effect) { void E131Component::add_effect(E131AddressableLightEffect *light_effect) {
+9
View File
@@ -46,6 +46,15 @@ class E131Component : public esphome::Component {
void set_method(E131ListenMethod listen_method) { this->listen_method_ = listen_method; } void set_method(E131ListenMethod listen_method) { this->listen_method_ = listen_method; }
protected: protected:
inline ssize_t read_(uint8_t *buf, size_t len) {
#if defined(USE_SOCKET_IMPL_BSD_SOCKETS) || defined(USE_SOCKET_IMPL_LWIP_SOCKETS)
return this->socket_->read(buf, len);
#elif defined(USE_SOCKET_IMPL_LWIP_TCP)
if (!this->udp_.parsePacket())
return -1;
return this->udp_.read(buf, len);
#endif
}
bool packet_(const uint8_t *data, size_t len, int &universe, E131Packet &packet); bool packet_(const uint8_t *data, size_t len, int &universe, E131Packet &packet);
bool process_(int universe, const E131Packet &packet); bool process_(int universe, const E131Packet &packet);
bool join_igmp_groups_(); bool join_igmp_groups_();