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

Release referenced resources in the destructor for ESP8266SSDP (#5607)

* Release referenced resources in destructor

* Release referenced resources in destructor, corrected for IPV6

* Release referenced resources in destructor, per suggested changes
This commit is contained in:
Mike C 2019-01-21 19:00:19 -08:00 committed by Develo
parent 471dd87b5c
commit d7094f2269
2 changed files with 48 additions and 7 deletions

View File

@ -127,7 +127,7 @@ struct SSDPTimer {
SSDPClass::SSDPClass() : SSDPClass::SSDPClass() :
_server(0), _server(0),
_timer(new SSDPTimer), _timer(0),
_port(80), _port(80),
_ttl(SSDP_MULTICAST_TTL), _ttl(SSDP_MULTICAST_TTL),
_respondToPort(0), _respondToPort(0),
@ -150,27 +150,26 @@ SSDPClass::SSDPClass() :
} }
SSDPClass::~SSDPClass() { SSDPClass::~SSDPClass() {
delete _timer; end();
} }
bool SSDPClass::begin() { bool SSDPClass::begin() {
end();
_pending = false; _pending = false;
if (strcmp(_uuid,"") == 0) { if (strcmp(_uuid,"") == 0) {
uint32_t chipId = ESP.getChipId(); uint32_t chipId = ESP.getChipId();
sprintf(_uuid, "38323636-4558-4dda-9188-cda0e6%02x%02x%02x", sprintf(_uuid, "38323636-4558-4dda-9188-cda0e6%02x%02x%02x",
(uint16_t) ((chipId >> 16) & 0xff), (uint16_t) ((chipId >> 16) & 0xff),
(uint16_t) ((chipId >> 8) & 0xff), (uint16_t) ((chipId >> 8) & 0xff),
(uint16_t) chipId & 0xff ); (uint16_t) chipId & 0xff);
} }
#ifdef DEBUG_SSDP #ifdef DEBUG_SSDP
DEBUG_SSDP.printf("SSDP UUID: %s\n", (char *)_uuid); DEBUG_SSDP.printf("SSDP UUID: %s\n", (char *)_uuid);
#endif #endif
if (_server) { assert(NULL == _server);
_server->unref();
_server = 0;
}
_server = new UdpContext; _server = new UdpContext;
_server->ref(); _server->ref();
@ -199,6 +198,34 @@ bool SSDPClass::begin() {
return true; return true;
} }
void SSDPClass::end() {
if(!_server)
return; // object is zeroed already, nothing to do
#ifdef DEBUG_SSDP
DEBUG_SSDP.printf_P(PSTR("SSDP end ... "));
#endif
// undo all initializations done in begin(), in reverse order
_stopTimer();
_server->disconnect();
IPAddress local = WiFi.localIP();
IPAddress mcast(SSDP_MULTICAST_ADDR);
if (igmp_leavegroup(local, mcast) != ERR_OK ) {
#ifdef DEBUG_SSDP
DEBUG_SSDP.printf_P(PSTR("SSDP failed to leave igmp group\n"));
#endif
}
_server->unref();
_server = 0;
#ifdef DEBUG_SSDP
DEBUG_SSDP.printf_P(PSTR("ok\n"));
#endif
}
void SSDPClass::_send(ssdp_method_t method) { void SSDPClass::_send(ssdp_method_t method) {
char buffer[1460]; char buffer[1460];
IPAddress ip = WiFi.localIP(); IPAddress ip = WiFi.localIP();
@ -461,6 +488,8 @@ void SSDPClass::_onTimerStatic(SSDPClass* self) {
} }
void SSDPClass::_startTimer() { void SSDPClass::_startTimer() {
_stopTimer();
_timer = new SSDPTimer();
ETSTimer* tm = &(_timer->timer); ETSTimer* tm = &(_timer->timer);
const int interval = 1000; const int interval = 1000;
os_timer_disarm(tm); os_timer_disarm(tm);
@ -468,6 +497,16 @@ void SSDPClass::_startTimer() {
os_timer_arm(tm, interval, 1 /* repeat */); os_timer_arm(tm, interval, 1 /* repeat */);
} }
void SSDPClass::_stopTimer() {
if(!_timer)
return;
ETSTimer* tm = &(_timer->timer);
os_timer_disarm(tm);
delete _timer;
_timer = NULL;
}
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SSDP) #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SSDP)
SSDPClass SSDP; SSDPClass SSDP;
#endif #endif

View File

@ -61,6 +61,7 @@ class SSDPClass{
SSDPClass(); SSDPClass();
~SSDPClass(); ~SSDPClass();
bool begin(); bool begin();
void end();
void schema(WiFiClient client); void schema(WiFiClient client);
void setDeviceType(const String& deviceType) { setDeviceType(deviceType.c_str()); } void setDeviceType(const String& deviceType) { setDeviceType(deviceType.c_str()); }
void setDeviceType(const char *deviceType); void setDeviceType(const char *deviceType);
@ -95,6 +96,7 @@ class SSDPClass{
void _send(ssdp_method_t method); void _send(ssdp_method_t method);
void _update(); void _update();
void _startTimer(); void _startTimer();
void _stopTimer();
static void _onTimerStatic(SSDPClass* self); static void _onTimerStatic(SSDPClass* self);
UdpContext* _server; UdpContext* _server;