Skip to content

Commit bda12cd

Browse files
WiFiUDP only emit a packet on flush if not ended (earlephilhower#2618)
A flush() on a packet that's already been sent should be a no-op, not send a 0-byte UDP packet. Track when an outgoing packet is dirty and only endPacket()/transmit it when so. Fixes earlephilhower#2617
1 parent ca30518 commit bda12cd

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

libraries/WiFi/src/WiFiUdp.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ template<>
3838
WiFiUDP* SList<WiFiUDP>::_s_first = 0;
3939

4040
/* Constructor */
41-
WiFiUDP::WiFiUDP() : _ctx(0), _multicast(false) {
41+
WiFiUDP::WiFiUDP() : _ctx(0), _multicast(false), _dirty(false) {
4242
WiFiUDP::_add(this);
4343
}
4444

@@ -49,6 +49,7 @@ WiFiUDP::WiFiUDP(const WiFiUDP& other) {
4949
}
5050
WiFiUDP::_add(this);
5151
_multicast = other._multicast;
52+
_dirty = other._dirty;
5253
}
5354

5455
WiFiUDP& WiFiUDP::operator=(const WiFiUDP& rhs) {
@@ -149,6 +150,7 @@ int WiFiUDP::beginPacket(IPAddress ip, uint16_t port) {
149150
_ctx->setMulticastInterface(IP_ADDR_ANY);
150151
_ctx->setMulticastTTL(255);
151152
}
153+
_dirty = false;
152154
return ret;
153155

154156
}
@@ -164,13 +166,15 @@ int WiFiUDP::beginPacketMulticast(IPAddress multicastAddress, uint16_t port,
164166
}
165167
_ctx->setMulticastInterface(interfaceAddress);
166168
_ctx->setMulticastTTL(ttl);
169+
_dirty = false;
167170
return 1;
168171
}
169172

170173
int WiFiUDP::endPacket() {
171174
if (!_ctx) {
172175
return 0;
173176
}
177+
_dirty = false;
174178

175179
return (_ctx->send()) ? 1 : 0;
176180
}
@@ -183,7 +187,7 @@ size_t WiFiUDP::write(const uint8_t *buffer, size_t size) {
183187
if (!_ctx) {
184188
return 0;
185189
}
186-
190+
_dirty = true;
187191
return _ctx->append(reinterpret_cast<const char*>(buffer), size);
188192
}
189193

@@ -224,7 +228,9 @@ int WiFiUDP::peek() {
224228
}
225229

226230
void WiFiUDP::flush() {
227-
endPacket();
231+
if (_dirty) {
232+
endPacket();
233+
}
228234
}
229235

230236
IPAddress WiFiUDP::remoteIP() {

libraries/WiFi/src/WiFiUdp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,5 @@ class WiFiUDP : public UDP, public SList<WiFiUDP> {
115115

116116
private:
117117
bool _multicast;
118+
bool _dirty;
118119
};

0 commit comments

Comments
 (0)