@@ -111,7 +111,7 @@ struct SSDPTimer {
111
111
112
112
SSDPClass::SSDPClass () :
113
113
_server(0 ),
114
- _timer(new SSDPTimer ),
114
+ _timer(0 ),
115
115
_port(80 ),
116
116
_ttl(SSDP_MULTICAST_TTL),
117
117
_respondToPort(0 ),
@@ -134,30 +134,40 @@ _notify_time(0)
134
134
}
135
135
136
136
SSDPClass::~SSDPClass (){
137
- delete _timer;
137
+ end ();
138
+ }
139
+
140
+ void SSDPClass::end (){
141
+ if (!_server) {
142
+ return ;
143
+ }
144
+ #ifdef DEBUG_SSDP
145
+ DEBUG_SSDP.printf_P (PSTR (" SSDP end ... " ));
146
+ #endif
147
+ // undo all initializations done in begin(), in reverse order
148
+ _stopTimer ();
149
+ _server->stop ();
150
+ delete (_server);
151
+ _server = 0 ;
138
152
}
139
153
140
154
141
155
bool SSDPClass::begin (){
142
156
_pending = false ;
143
-
157
+ end ();
144
158
uint32_t chipId = ((uint16_t ) (ESP.getEfuseMac () >> 32 ));
145
159
sprintf (_uuid, " 38323636-4558-4dda-9188-cda0e6%02x%02x%02x" ,
146
160
(uint16_t ) ((chipId >> 16 ) & 0xff ),
147
161
(uint16_t ) ((chipId >> 8 ) & 0xff ),
148
162
(uint16_t ) chipId & 0xff );
149
-
163
+ assert (nullptr == _server);
164
+ _server = new WiFiUDP;
150
165
#ifdef DEBUG_SSDP
151
166
DEBUG_SSDP.printf (" SSDP UUID: %s\n " , (char *)_uuid);
152
167
#endif
153
168
154
- if (_server) {
155
- delete (_server);
156
- _server = 0 ;
157
- }
158
169
159
170
_server = new WiFiUDP;
160
-
161
171
if (!(_server->beginMulticast (IPAddress (SSDP_MULTICAST_ADDR), SSDP_PORT))) {
162
172
#ifdef DEBUG_SSDP
163
173
DEBUG_SSDP.println (" Error begin" );
@@ -234,7 +244,7 @@ void SSDPClass::schema(WiFiClient client){
234
244
235
245
void SSDPClass::_update (){
236
246
int nbBytes =0 ;
237
- char * packetBuffer = NULL ;
247
+ char * packetBuffer = nullptr ;
238
248
239
249
if (!_pending && _server) {
240
250
ssdp_method_t method = NONE;
@@ -430,13 +440,25 @@ void SSDPClass::_onTimerStatic(SSDPClass* self) {
430
440
}
431
441
432
442
void SSDPClass::_startTimer () {
443
+ _stopTimer ();
444
+ _timer= new SSDPTimer ();
433
445
ETSTimer* tm = &(_timer->timer );
434
446
const int interval = 1000 ;
435
447
ets_timer_disarm (tm );
436
448
ets_timer_setfn (tm , reinterpret_cast <ETSTimerFunc*>(&SSDPClass::_onTimerStatic), reinterpret_cast <void *>(this ));
437
449
ets_timer_arm (tm , interval, 1 /* repeat */ );
438
450
}
439
451
452
+ void SSDPClass::_stopTimer () {
453
+ if (!_timer){
454
+ return ;
455
+ }
456
+ ETSTimer* tm = &(_timer->timer );
457
+ ets_timer_disarm (tm );
458
+ delete _timer;
459
+ _timer = nullptr ;
460
+ }
461
+
440
462
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SSDP)
441
463
SSDPClass SSDP;
442
464
#endif
0 commit comments