Skip to content

Commit d2beb2d

Browse files
Add upsteam multicast compatibility APIs (earlephilhower#821)
Fixes earlephilhower#747 while remaining ESP8266 compatible
1 parent 7ef44d9 commit d2beb2d

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

libraries/WiFi/src/WiFiUdp.cpp

Lines changed: 15 additions & 2 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) {
41+
WiFiUDP::WiFiUDP() : _ctx(0), _multicast(false) {
4242
WiFiUDP::_add(this);
4343
}
4444

@@ -48,6 +48,7 @@ WiFiUDP::WiFiUDP(const WiFiUDP& other) {
4848
_ctx->ref();
4949
}
5050
WiFiUDP::_add(this);
51+
_multicast = other._multicast;
5152
}
5253

5354
WiFiUDP& WiFiUDP::operator=(const WiFiUDP& rhs) {
@@ -97,6 +98,12 @@ uint8_t WiFiUDP::beginMulticast(IPAddress interfaceAddr, IPAddress multicast, ui
9798
return 1;
9899
}
99100

101+
uint8_t WiFiUDP::beginMulticast(IPAddress addr, uint16_t port) {
102+
auto ret = beginMulticast(IP_ADDR_ANY, addr, port);
103+
_multicast = true;
104+
return ret;
105+
}
106+
100107
/* return number of bytes available in the current packet,
101108
will return zero if parsePacket hasn't been called yet */
102109
int WiFiUDP::available() {
@@ -137,7 +144,13 @@ int WiFiUDP::beginPacket(IPAddress ip, uint16_t port) {
137144
_ctx = new UdpContext;
138145
_ctx->ref();
139146
}
140-
return (_ctx->connect(ip, port)) ? 1 : 0;
147+
auto ret = (_ctx->connect(ip, port)) ? 1 : 0;
148+
if (_multicast) {
149+
_ctx->setMulticastInterface(IP_ADDR_ANY);
150+
_ctx->setMulticastTTL(255);
151+
}
152+
return ret;
153+
141154
}
142155

143156
int WiFiUDP::beginPacketMulticast(IPAddress multicastAddress, uint16_t port,

libraries/WiFi/src/WiFiUdp.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,12 @@ class WiFiUDP : public UDP, public SList<WiFiUDP> {
4444

4545
// initialize, start listening on specified port.
4646
// Returns 1 if successful, 0 if there are no sockets available to use
47-
uint8_t begin(uint16_t port) override;
47+
virtual uint8_t begin(uint16_t port) override;
48+
// initialize, start listening on specified multicast IP address and port. Returns 1 if successful, 0 if there are no sockets available to use
49+
virtual uint8_t beginMulticast(IPAddress, uint16_t) override;
50+
4851
// Finish with the UDP connection
49-
void stop() override;
52+
virtual void stop() override;
5053
// join a multicast group and listen on the given port
5154
uint8_t beginMulticast(IPAddress interfaceAddr, IPAddress multicast, uint16_t port);
5255

@@ -110,4 +113,6 @@ class WiFiUDP : public UDP, public SList<WiFiUDP> {
110113
static void stopAll();
111114
static void stopAllExcept(WiFiUDP * exC);
112115

116+
private:
117+
bool _multicast;
113118
};

0 commit comments

Comments
 (0)