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:
parent
25d814bdfb
commit
699b06b875
@ -69,6 +69,5 @@ void setup(void){
|
||||
}
|
||||
|
||||
void loop(void){
|
||||
mdns.update();
|
||||
server.handleClient();
|
||||
}
|
||||
|
@ -156,6 +156,5 @@ void setup(void){
|
||||
}
|
||||
|
||||
void loop(void){
|
||||
mdns.update();
|
||||
server.handleClient();
|
||||
}
|
||||
|
@ -42,6 +42,10 @@ License (MIT license):
|
||||
#define TTL_OFFSET 4
|
||||
#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()
|
||||
: _expected(NULL)
|
||||
@ -49,6 +53,7 @@ MDNSResponder::MDNSResponder()
|
||||
, _response(NULL)
|
||||
, _responseLen(0)
|
||||
, _index(0)
|
||||
, _conn(0)
|
||||
{ }
|
||||
|
||||
MDNSResponder::~MDNSResponder() {
|
||||
@ -149,21 +154,37 @@ bool MDNSResponder::begin(const char* domain, IPAddress addr, uint32_t ttlSecond
|
||||
records[IP_OFFSET + 0] = (uint8_t) ipAddress;
|
||||
|
||||
// Open the MDNS socket if it isn't already open.
|
||||
if (!_mdnsConn) {
|
||||
if (!_mdnsConn.beginMulticast(addr, IPAddress(224, 0, 0, 251), 5353)) {
|
||||
if (!_conn) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
_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;
|
||||
}
|
||||
|
||||
void MDNSResponder::update() {
|
||||
if (!_mdnsConn.parsePacket())
|
||||
if (!_conn->next()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Read available data.
|
||||
int n = _mdnsConn.available();
|
||||
int n = _conn->getSize();
|
||||
|
||||
_index = 0;
|
||||
|
||||
@ -172,7 +193,7 @@ void MDNSResponder::update() {
|
||||
#endif
|
||||
// Look for domain name in request and respond with canned response if found.
|
||||
for (int i = 0; i < n; ++i) {
|
||||
uint8_t ch = tolower(_mdnsConn.read());
|
||||
uint8_t ch = tolower(_conn->read());
|
||||
|
||||
#ifdef MDNS_DEBUG
|
||||
String str(ch, 16);
|
||||
@ -191,9 +212,12 @@ void MDNSResponder::update() {
|
||||
Serial.print("responding, i=");
|
||||
Serial.println(i);
|
||||
#endif
|
||||
_mdnsConn.beginPacketMulticast(IPAddress(224, 0, 0, 251), 5353, _localAddr);
|
||||
_mdnsConn.write(_response, _responseLen);
|
||||
_mdnsConn.endPacket();
|
||||
ip_addr_t multicast_addr;
|
||||
multicast_addr.addr = (uint32_t) MDNS_MULTICAST_ADDR;
|
||||
|
||||
_conn->append(reinterpret_cast<const char*>(_response), _responseLen);
|
||||
_conn->send();
|
||||
|
||||
_index = 0;
|
||||
}
|
||||
}
|
||||
|
@ -46,6 +46,9 @@ License (MIT license):
|
||||
#include "ESP8266WiFi.h"
|
||||
#include "WiFiUdp.h"
|
||||
|
||||
|
||||
class UdpContext;
|
||||
|
||||
class MDNSResponder {
|
||||
public:
|
||||
MDNSResponder();
|
||||
@ -63,7 +66,7 @@ private:
|
||||
uint8_t* _response;
|
||||
int _responseLen;
|
||||
// Socket for MDNS communication
|
||||
WiFiUDP _mdnsConn;
|
||||
UdpContext* _conn;
|
||||
// local IP Address
|
||||
IPAddress _localAddr;
|
||||
};
|
||||
|
@ -68,9 +68,6 @@ void setup(void)
|
||||
|
||||
void loop(void)
|
||||
{
|
||||
// Check for any mDNS queries and send responses
|
||||
mdns.update();
|
||||
|
||||
// Check if a client has connected
|
||||
WiFiClient client = server.available();
|
||||
if (!client) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user