1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-22 21:23:07 +03:00

implement async replies in mDNS library

No need to call mdns.update() from loop() any more.
This commit is contained in:
Ivan Grokhotkov 2015-05-12 17:59:24 +03:00
parent 25d814bdfb
commit 699b06b875
5 changed files with 38 additions and 16 deletions

View File

@ -69,6 +69,5 @@ void setup(void){
} }
void loop(void){ void loop(void){
mdns.update();
server.handleClient(); server.handleClient();
} }

View File

@ -156,6 +156,5 @@ void setup(void){
} }
void loop(void){ void loop(void){
mdns.update();
server.handleClient(); server.handleClient();
} }

View File

@ -42,6 +42,10 @@ License (MIT license):
#define TTL_OFFSET 4 #define TTL_OFFSET 4
#define IP_OFFSET 10 #define IP_OFFSET 10
static const IPAddress MDNS_MULTICAST_ADDR(224, 0, 0, 251);
static const int MDNS_MULTICAST_TTL = 1;
static const int MDNS_PORT = 5353;
MDNSResponder::MDNSResponder() MDNSResponder::MDNSResponder()
: _expected(NULL) : _expected(NULL)
@ -49,6 +53,7 @@ MDNSResponder::MDNSResponder()
, _response(NULL) , _response(NULL)
, _responseLen(0) , _responseLen(0)
, _index(0) , _index(0)
, _conn(0)
{ } { }
MDNSResponder::~MDNSResponder() { MDNSResponder::~MDNSResponder() {
@ -149,21 +154,37 @@ bool MDNSResponder::begin(const char* domain, IPAddress addr, uint32_t ttlSecond
records[IP_OFFSET + 0] = (uint8_t) ipAddress; records[IP_OFFSET + 0] = (uint8_t) ipAddress;
// Open the MDNS socket if it isn't already open. // Open the MDNS socket if it isn't already open.
if (!_mdnsConn) { if (!_conn) {
if (!_mdnsConn.beginMulticast(addr, IPAddress(224, 0, 0, 251), 5353)) { ip_addr_t ifaddr;
ifaddr.addr = (uint32_t) addr;
ip_addr_t multicast_addr;
multicast_addr.addr = (uint32_t) MDNS_MULTICAST_ADDR;
if (igmp_joingroup(&ifaddr, &multicast_addr)!= ERR_OK) {
return false; return false;
} }
}
_conn = new UdpContext;
_conn->ref();
if (!_conn->listen(*IP_ADDR_ANY, MDNS_PORT)) {
return false;
}
_conn->setMulticastInterface(ifaddr);
_conn->setMulticastTTL(MDNS_MULTICAST_TTL);
_conn->onRx(std::bind(&MDNSResponder::update, this));
_conn->connect(multicast_addr, MDNS_PORT);
}
return true; return true;
} }
void MDNSResponder::update() { void MDNSResponder::update() {
if (!_mdnsConn.parsePacket()) if (!_conn->next()) {
return; return;
}
// Read available data. // Read available data.
int n = _mdnsConn.available(); int n = _conn->getSize();
_index = 0; _index = 0;
@ -172,7 +193,7 @@ void MDNSResponder::update() {
#endif #endif
// Look for domain name in request and respond with canned response if found. // Look for domain name in request and respond with canned response if found.
for (int i = 0; i < n; ++i) { for (int i = 0; i < n; ++i) {
uint8_t ch = tolower(_mdnsConn.read()); uint8_t ch = tolower(_conn->read());
#ifdef MDNS_DEBUG #ifdef MDNS_DEBUG
String str(ch, 16); String str(ch, 16);
@ -191,9 +212,12 @@ void MDNSResponder::update() {
Serial.print("responding, i="); Serial.print("responding, i=");
Serial.println(i); Serial.println(i);
#endif #endif
_mdnsConn.beginPacketMulticast(IPAddress(224, 0, 0, 251), 5353, _localAddr); ip_addr_t multicast_addr;
_mdnsConn.write(_response, _responseLen); multicast_addr.addr = (uint32_t) MDNS_MULTICAST_ADDR;
_mdnsConn.endPacket();
_conn->append(reinterpret_cast<const char*>(_response), _responseLen);
_conn->send();
_index = 0; _index = 0;
} }
} }

View File

@ -46,6 +46,9 @@ License (MIT license):
#include "ESP8266WiFi.h" #include "ESP8266WiFi.h"
#include "WiFiUdp.h" #include "WiFiUdp.h"
class UdpContext;
class MDNSResponder { class MDNSResponder {
public: public:
MDNSResponder(); MDNSResponder();
@ -63,7 +66,7 @@ private:
uint8_t* _response; uint8_t* _response;
int _responseLen; int _responseLen;
// Socket for MDNS communication // Socket for MDNS communication
WiFiUDP _mdnsConn; UdpContext* _conn;
// local IP Address // local IP Address
IPAddress _localAddr; IPAddress _localAddr;
}; };

View File

@ -68,9 +68,6 @@ void setup(void)
void loop(void) void loop(void)
{ {
// Check for any mDNS queries and send responses
mdns.update();
// Check if a client has connected // Check if a client has connected
WiFiClient client = server.available(); WiFiClient client = server.available();
if (!client) { if (!client) {