File tree Expand file tree Collapse file tree 2 files changed +32
-4
lines changed Expand file tree Collapse file tree 2 files changed +32
-4
lines changed Original file line number Diff line number Diff line change @@ -143,8 +143,6 @@ MDNSResponder::~MDNSResponder() {
143143}
144144
145145bool MDNSResponder::begin (const char * hostname){
146- // Open the MDNS socket if it isn't already open.
147-
148146 size_t n = strlen (hostname);
149147 if (n > 63 ) { // max size for a single label.
150148 return false ;
@@ -155,15 +153,41 @@ bool MDNSResponder::begin(const char* hostname){
155153 _hostName.toLowerCase ();
156154
157155 // If instance name is not already set copy hostname to instance name
158- if (_instanceName.equals (" " ) ) _instanceName=hostname;
156+ if (_instanceName.equals (" " ) ) _instanceName=hostname;
157+
158+ _gotIPHandler = WiFi.onStationModeGotIP ([this ](const WiFiEventStationModeGotIP& event){
159+ _restart ();
160+ });
161+
162+ _disconnectedHandler = WiFi.onStationModeDisconnected ([this ](const WiFiEventStationModeDisconnected& event) {
163+ _restart ();
164+ });
165+
166+ return _listen ();
167+ }
159168
169+ void MDNSResponder::_restart () {
170+ if (_conn) {
171+ _conn->unref ();
172+ _conn = nullptr ;
173+ }
174+ _listen ();
175+ }
176+
177+ bool MDNSResponder::_listen () {
160178 // Open the MDNS socket if it isn't already open.
161179 if (!_conn) {
162180 uint32_t ourIp = _getOurIp ();
163181 if (ourIp == 0 ){
182+ #ifdef MDNS_DEBUG_RX
183+ Serial.println (" MDNS: no IP address to listen on" );
184+ #endif
164185 return false ;
165186 }
166-
187+ #ifdef MDNS_DEBUG_RX
188+ Serial.print (" MDNS listening on IP: " );
189+ Serial.println (IPAddress (ourIp));
190+ #endif
167191 ip_addr_t ifaddr;
168192 ifaddr.addr = ourIp;
169193 ip_addr_t multicast_addr;
Original file line number Diff line number Diff line change @@ -114,6 +114,8 @@ class MDNSResponder {
114114 struct MDNSQuery * _query;
115115 bool _newQuery;
116116 bool _waitingForAnswers;
117+ WiFiEventHandler _disconnectedHandler;
118+ WiFiEventHandler _gotIPHandler;
117119
118120
119121 uint32_t _getOurIp ();
@@ -125,6 +127,8 @@ class MDNSResponder {
125127 size_t advertiseServices (); // advertise all hosted services
126128 MDNSAnswer* _getAnswerFromIdx (int idx);
127129 int _getNumAnswers ();
130+ bool _listen ();
131+ void _restart ();
128132};
129133
130134extern MDNSResponder MDNS;
You can’t perform that action at this time.
0 commit comments