fix: bug in MDNS handler

- service name was basedon hostname which will cause issues with user-defined values
This commit is contained in:
ZanzyTHEbar 2023-03-03 13:43:24 +00:00
parent 173abdf763
commit 1ca7d1f48c

View File

@ -1,38 +1,39 @@
#include "MDNSManager.hpp" #include "MDNSManager.hpp"
MDNSHandler::MDNSHandler(StateManager<MDNSState_e> *stateManager, MDNSHandler::MDNSHandler(StateManager<MDNSState_e>* stateManager,
ProjectConfig *configManager) : stateManager(stateManager), ProjectConfig* configManager)
configManager(configManager) {} : stateManager(stateManager), configManager(configManager) {}
bool MDNSHandler::startMDNS() bool MDNSHandler::startMDNS() {
{ const std::string service = "_openiristracker";
ProjectConfig::MDNSConfig_t *mdnsConfig = configManager->getMDNSConfig(); ProjectConfig::MDNSConfig_t* mdnsConfig = configManager->getMDNSConfig();
if (!MDNS.begin(mdnsConfig->hostname.c_str())) // lowercase only - as this will be the url if (!MDNS.begin(mdnsConfig->hostname
{ .c_str())) // lowercase only - as this will be the url
stateManager->setState(MDNSState_e::MDNSState_Error); {
log_e("Error initializing MDNS"); stateManager->setState(MDNSState_e::MDNSState_Error);
return false; log_e("Error initializing MDNS");
} return false;
}
stateManager->setState(MDNSState_e::MDNSState_Starting); stateManager->setState(MDNSState_e::MDNSState_Starting);
MDNS.addService(("_" + mdnsConfig->hostname).c_str(), "_tcp", 80); MDNS.addService(service.c_str(), "_tcp", 80);
char port[20]; char port[20];
//! Add service needs leading _ on ESP32 implementation for some reason (according to the docs) //! Add service needs leading _ on ESP32 implementation for some reason
MDNS.addServiceTxt(("_" + mdnsConfig->hostname).c_str(), "_tcp", "stream_port", "80"); //! convert int to const char* using a very efficient implemenation of itoa //! (according to the docs)
log_i("MDNS initialized!"); MDNS.addServiceTxt(service.c_str(), "_tcp", "stream_port",
stateManager->setState(MDNSState_e::MDNSState_Started); "80");
return true; log_i("MDNS initialized!");
stateManager->setState(MDNSState_e::MDNSState_Started);
return true;
} }
void MDNSHandler::update(ObserverEvent::Event event) void MDNSHandler::update(ObserverEvent::Event event) {
{ switch (event) {
switch (event) case ObserverEvent::Event::mdnsConfigUpdated:
{ MDNS.end();
case ObserverEvent::Event::mdnsConfigUpdated: startMDNS();
MDNS.end(); break;
startMDNS(); default:
break; break;
default: }
break;
}
} }