File tree 2 files changed +32
-4
lines changed
2 files changed +32
-4
lines changed Original file line number Diff line number Diff line change @@ -143,8 +143,6 @@ MDNSResponder::~MDNSResponder() {
143
143
}
144
144
145
145
bool MDNSResponder::begin (const char * hostname){
146
- // Open the MDNS socket if it isn't already open.
147
-
148
146
size_t n = strlen (hostname);
149
147
if (n > 63 ) { // max size for a single label.
150
148
return false ;
@@ -155,15 +153,41 @@ bool MDNSResponder::begin(const char* hostname){
155
153
_hostName.toLowerCase ();
156
154
157
155
// 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
+ }
159
168
169
+ void MDNSResponder::_restart () {
170
+ if (_conn) {
171
+ _conn->unref ();
172
+ _conn = nullptr ;
173
+ }
174
+ _listen ();
175
+ }
176
+
177
+ bool MDNSResponder::_listen () {
160
178
// Open the MDNS socket if it isn't already open.
161
179
if (!_conn) {
162
180
uint32_t ourIp = _getOurIp ();
163
181
if (ourIp == 0 ){
182
+ #ifdef MDNS_DEBUG_RX
183
+ Serial.println (" MDNS: no IP address to listen on" );
184
+ #endif
164
185
return false ;
165
186
}
166
-
187
+ #ifdef MDNS_DEBUG_RX
188
+ Serial.print (" MDNS listening on IP: " );
189
+ Serial.println (IPAddress (ourIp));
190
+ #endif
167
191
ip_addr_t ifaddr;
168
192
ifaddr.addr = ourIp;
169
193
ip_addr_t multicast_addr;
Original file line number Diff line number Diff line change @@ -114,6 +114,8 @@ class MDNSResponder {
114
114
struct MDNSQuery * _query;
115
115
bool _newQuery;
116
116
bool _waitingForAnswers;
117
+ WiFiEventHandler _disconnectedHandler;
118
+ WiFiEventHandler _gotIPHandler;
117
119
118
120
119
121
uint32_t _getOurIp ();
@@ -125,6 +127,8 @@ class MDNSResponder {
125
127
size_t advertiseServices (); // advertise all hosted services
126
128
MDNSAnswer* _getAnswerFromIdx (int idx);
127
129
int _getNumAnswers ();
130
+ bool _listen ();
131
+ void _restart ();
128
132
};
129
133
130
134
extern MDNSResponder MDNS;
You can’t perform that action at this time.
0 commit comments