Skip to content

Commit a1967b9

Browse files
committed
implement async replies in mDNS library
No need to call mdns.update() from loop() any more.
1 parent ae2bc12 commit a1967b9

File tree

5 files changed

+38
-16
lines changed

5 files changed

+38
-16
lines changed

hardware/esp8266com/esp8266/libraries/ESP8266WebServer/examples/HelloServer/HelloServer.ino

-1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,5 @@ void setup(void){
6969
}
7070

7171
void loop(void){
72-
mdns.update();
7372
server.handleClient();
7473
}

hardware/esp8266com/esp8266/libraries/ESP8266WebServer/examples/SDWebServer/SDWebServer.ino

-1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,5 @@ void setup(void){
156156
}
157157

158158
void loop(void){
159-
mdns.update();
160159
server.handleClient();
161160
}

hardware/esp8266com/esp8266/libraries/ESP8266mDNS/ESP8266mDNS.cpp

+34-10
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,18 @@ License (MIT license):
4242
#define TTL_OFFSET 4
4343
#define IP_OFFSET 10
4444

45+
static const IPAddress MDNS_MULTICAST_ADDR(224, 0, 0, 251);
46+
static const int MDNS_MULTICAST_TTL = 1;
47+
static const int MDNS_PORT = 5353;
48+
4549

4650
MDNSResponder::MDNSResponder()
4751
: _expected(NULL)
4852
, _expectedLen(0)
4953
, _response(NULL)
5054
, _responseLen(0)
5155
, _index(0)
56+
, _conn(0)
5257
{ }
5358

5459
MDNSResponder::~MDNSResponder() {
@@ -149,21 +154,37 @@ bool MDNSResponder::begin(const char* domain, IPAddress addr, uint32_t ttlSecond
149154
records[IP_OFFSET + 0] = (uint8_t) ipAddress;
150155

151156
// Open the MDNS socket if it isn't already open.
152-
if (!_mdnsConn) {
153-
if (!_mdnsConn.beginMulticast(addr, IPAddress(224, 0, 0, 251), 5353)) {
157+
if (!_conn) {
158+
ip_addr_t ifaddr;
159+
ifaddr.addr = (uint32_t) addr;
160+
ip_addr_t multicast_addr;
161+
multicast_addr.addr = (uint32_t) MDNS_MULTICAST_ADDR;
162+
163+
if (igmp_joingroup(&ifaddr, &multicast_addr)!= ERR_OK) {
154164
return false;
155165
}
156-
}
157166

167+
_conn = new UdpContext;
168+
_conn->ref();
169+
170+
if (!_conn->listen(*IP_ADDR_ANY, MDNS_PORT)) {
171+
return false;
172+
}
173+
_conn->setMulticastInterface(ifaddr);
174+
_conn->setMulticastTTL(MDNS_MULTICAST_TTL);
175+
_conn->onRx(std::bind(&MDNSResponder::update, this));
176+
_conn->connect(multicast_addr, MDNS_PORT);
177+
}
158178
return true;
159179
}
160180

161181
void MDNSResponder::update() {
162-
if (!_mdnsConn.parsePacket())
163-
return;
182+
if (!_conn->next()) {
183+
return;
184+
}
164185

165186
// Read available data.
166-
int n = _mdnsConn.available();
187+
int n = _conn->getSize();
167188

168189
_index = 0;
169190

@@ -172,7 +193,7 @@ void MDNSResponder::update() {
172193
#endif
173194
// Look for domain name in request and respond with canned response if found.
174195
for (int i = 0; i < n; ++i) {
175-
uint8_t ch = tolower(_mdnsConn.read());
196+
uint8_t ch = tolower(_conn->read());
176197

177198
#ifdef MDNS_DEBUG
178199
String str(ch, 16);
@@ -191,9 +212,12 @@ void MDNSResponder::update() {
191212
Serial.print("responding, i=");
192213
Serial.println(i);
193214
#endif
194-
_mdnsConn.beginPacketMulticast(IPAddress(224, 0, 0, 251), 5353, _localAddr);
195-
_mdnsConn.write(_response, _responseLen);
196-
_mdnsConn.endPacket();
215+
ip_addr_t multicast_addr;
216+
multicast_addr.addr = (uint32_t) MDNS_MULTICAST_ADDR;
217+
218+
_conn->append(reinterpret_cast<const char*>(_response), _responseLen);
219+
_conn->send();
220+
197221
_index = 0;
198222
}
199223
}

hardware/esp8266com/esp8266/libraries/ESP8266mDNS/ESP8266mDNS.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ License (MIT license):
4646
#include "ESP8266WiFi.h"
4747
#include "WiFiUdp.h"
4848

49+
50+
class UdpContext;
51+
4952
class MDNSResponder {
5053
public:
5154
MDNSResponder();
@@ -63,7 +66,7 @@ class MDNSResponder {
6366
uint8_t* _response;
6467
int _responseLen;
6568
// Socket for MDNS communication
66-
WiFiUDP _mdnsConn;
69+
UdpContext* _conn;
6770
// local IP Address
6871
IPAddress _localAddr;
6972
};

hardware/esp8266com/esp8266/libraries/ESP8266mDNS/examples/mDNS_Web_Server/mDNS_Web_Server.ino

-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@ void setup(void)
6868

6969
void loop(void)
7070
{
71-
// Check for any mDNS queries and send responses
72-
mdns.update();
73-
7471
// Check if a client has connected
7572
WiFiClient client = server.available();
7673
if (!client) {

0 commit comments

Comments
 (0)