mirror of
https://github.com/sle118/squeezelite-esp32.git
synced 2025-12-11 14:07:11 +03:00
make it fit in allocated space
This commit is contained in:
@@ -2,12 +2,14 @@
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <memory>
|
||||
|
||||
namespace bell {
|
||||
|
||||
class MDNSService {
|
||||
public:
|
||||
static void* registerService(
|
||||
public:
|
||||
virtual ~MDNSService() { }
|
||||
static std::unique_ptr<MDNSService> registerService(
|
||||
const std::string &serviceName,
|
||||
const std::string &serviceType,
|
||||
const std::string &serviceProto,
|
||||
@@ -15,7 +17,7 @@ class MDNSService {
|
||||
int servicePort,
|
||||
const std::map<std::string, std::string> txtData
|
||||
);
|
||||
static void unregisterService(void* service);
|
||||
virtual void unregisterService() = 0;
|
||||
};
|
||||
|
||||
} // namespace bell
|
||||
@@ -4,11 +4,20 @@
|
||||
|
||||
using namespace bell;
|
||||
|
||||
class implMDNSService : public MDNSService {
|
||||
private:
|
||||
DNSServiceRef* service;
|
||||
|
||||
public:
|
||||
implMDNSService(DNSServiceRef* service) : service(service) { }
|
||||
void unregisterService() { DNSServiceRefDeallocate(*service); }
|
||||
};
|
||||
|
||||
/**
|
||||
* MacOS implementation of MDNSService.
|
||||
* @see https://developer.apple.com/documentation/dnssd/1804733-dnsserviceregister
|
||||
**/
|
||||
void* MDNSService::registerService(
|
||||
std::unique_ptr<MDNSService> MDNSService::registerService(
|
||||
const std::string& serviceName,
|
||||
const std::string& serviceType,
|
||||
const std::string& serviceProto,
|
||||
@@ -37,9 +46,5 @@ void* MDNSService::registerService(
|
||||
NULL /* context */
|
||||
);
|
||||
TXTRecordDeallocate(&txtRecord);
|
||||
return ref;
|
||||
}
|
||||
|
||||
void MDNSService::unregisterService(void* ref) {
|
||||
DNSServiceRefDeallocate((DNSServiceRef)ref);
|
||||
return std::make_unique<implMDNSService>(ref);
|
||||
}
|
||||
@@ -5,11 +5,22 @@
|
||||
|
||||
using namespace bell;
|
||||
|
||||
class implMDNSService : public MDNSService {
|
||||
private:
|
||||
const std::string type;
|
||||
const std::string proto;
|
||||
void unregisterService() { mdns_service_remove(type.c_str(), proto.c_str()); }
|
||||
|
||||
public:
|
||||
implMDNSService(std::string type, std::string proto) : type(type), proto(proto) { };
|
||||
};
|
||||
|
||||
/**
|
||||
* ESP32 implementation of MDNSService
|
||||
* @see https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/protocols/mdns.html
|
||||
**/
|
||||
void* MDNSService::registerService(
|
||||
|
||||
std::unique_ptr<MDNSService> MDNSService::registerService(
|
||||
const std::string& serviceName,
|
||||
const std::string& serviceType,
|
||||
const std::string& serviceProto,
|
||||
@@ -35,5 +46,5 @@ void* MDNSService::registerService(
|
||||
txtItems.size() /* num_items */
|
||||
);
|
||||
|
||||
return NULL;
|
||||
return std::make_unique<implMDNSService>(serviceType, serviceProto);
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
#include <avahi-common/alternative.h>
|
||||
#include <avahi-common/simple-watch.h>
|
||||
#elif !defined(BELL_DISABLE_AVAHI)
|
||||
#define BELL_DISABLE_AVAHI
|
||||
#define BELL_DISABLE_AVAHI
|
||||
#endif
|
||||
|
||||
#include "mdnssvc.h"
|
||||
@@ -21,19 +21,55 @@
|
||||
using namespace bell;
|
||||
|
||||
#ifndef BELL_DISABLE_AVAHI
|
||||
static AvahiClient *avahiClient = NULL;
|
||||
static AvahiSimplePoll *avahiPoll = NULL;
|
||||
static void groupHandler(AvahiEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata) { }
|
||||
#endif
|
||||
|
||||
static in_addr_t host = INADDR_ANY;
|
||||
static struct mdnsd *mdnsServer;
|
||||
class implMDNSService : public MDNSService {
|
||||
private:
|
||||
#ifndef BELL_DISABLE_AVAHI
|
||||
AvahiEntryGroup *avahiGroup;
|
||||
#endif
|
||||
struct mdns_service* service;
|
||||
|
||||
public:
|
||||
#ifndef BELL_DISABLE_AVAHI
|
||||
static AvahiClient *avahiClient;
|
||||
static AvahiSimplePoll *avahiPoll;
|
||||
#endif
|
||||
static struct mdnsd* mdnsServer;
|
||||
static in_addr_t host;
|
||||
|
||||
implMDNSService(struct mdns_service* service) : service(service) { };
|
||||
#ifndef BELL_DISABLE_AVAHI
|
||||
implMDNSService(AvahiEntryGroup *avahiGroup) : avahiGroup(avahiGroup) { };
|
||||
#endif
|
||||
void unregisterService();
|
||||
};
|
||||
|
||||
struct mdnsd* implMDNSService::mdnsServer = NULL;
|
||||
in_addr_t implMDNSService::host = INADDR_ANY;
|
||||
#ifndef BELL_DISABLE_AVAHI
|
||||
AvahiClient* implMDNSService::avahiClient = NULL;
|
||||
AvahiSimplePoll* implMDNSService::avahiPoll = NULL;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Linux implementation of MDNSService using avahi.
|
||||
* @see https://www.avahi.org/doxygen/html/
|
||||
**/
|
||||
void* MDNSService::registerService(
|
||||
|
||||
void implMDNSService::unregisterService() {
|
||||
#ifndef BELL_DISABLE_AVAHI
|
||||
if (avahiGroup) {
|
||||
avahi_entry_group_free(avahiGroup);
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
mdns_service_remove(implMDNSService::mdnsServer, service);
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<MDNSService> MDNSService::registerService(
|
||||
const std::string& serviceName,
|
||||
const std::string& serviceType,
|
||||
const std::string& serviceProto,
|
||||
@@ -43,22 +79,22 @@ void* MDNSService::registerService(
|
||||
) {
|
||||
#ifndef BELL_DISABLE_AVAHI
|
||||
// try avahi first if available
|
||||
if (!avahiPoll) {
|
||||
avahiPoll = avahi_simple_poll_new();
|
||||
if (!implMDNSService::avahiPoll) {
|
||||
implMDNSService::avahiPoll = avahi_simple_poll_new();
|
||||
}
|
||||
|
||||
if (avahiPoll && !avahiClient) {
|
||||
avahiClient = avahi_client_new(avahi_simple_poll_get(avahiPoll),
|
||||
if (implMDNSService::avahiPoll && !implMDNSService::avahiClient) {
|
||||
implMDNSService::avahiClient = avahi_client_new(avahi_simple_poll_get(implMDNSService::avahiPoll),
|
||||
AvahiClientFlags(0), NULL, NULL, NULL);
|
||||
}
|
||||
|
||||
|
||||
AvahiEntryGroup *avahiGroup;
|
||||
|
||||
if (avahiClient &&
|
||||
(avahiGroup = avahi_entry_group_new(avahiClient, groupHandler, NULL)) == NULL) {
|
||||
if (implMDNSService::avahiClient &&
|
||||
(avahiGroup = avahi_entry_group_new(implMDNSService::avahiClient, groupHandler, NULL)) == NULL) {
|
||||
BELL_LOG(error, "MDNS", "cannot create service %s", serviceName.c_str());
|
||||
}
|
||||
|
||||
|
||||
if (avahiGroup) {
|
||||
AvahiStringList* avahiTxt = NULL;
|
||||
|
||||
@@ -80,7 +116,7 @@ void* MDNSService::registerService(
|
||||
avahi_entry_group_free(avahiGroup);
|
||||
} else {
|
||||
BELL_LOG(info, "MDNS", "using avahi for %s", serviceName.c_str());
|
||||
return avahiGroup;
|
||||
return std::make_unique<implMDNSService>(avahiGroup);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -92,39 +128,39 @@ void* MDNSService::registerService(
|
||||
if (serviceHost.size()) {
|
||||
struct hostent *h = gethostbyname(serviceHost.c_str());
|
||||
if (h) {
|
||||
memcpy(&host, h->h_addr_list[0], 4);
|
||||
memcpy(&implMDNSService::host, h->h_addr_list[0], 4);
|
||||
}
|
||||
}
|
||||
|
||||
// try go guess ifaddr if we have nothing as listening to INADDR_ANY usually does not work
|
||||
if (host == INADDR_ANY && getifaddrs(&ifaddr) != -1) {
|
||||
if (implMDNSService::host == INADDR_ANY && getifaddrs(&ifaddr) != -1) {
|
||||
for (struct ifaddrs* ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
|
||||
if (ifa->ifa_addr == NULL || ifa->ifa_addr->sa_family != AF_INET ||
|
||||
!(ifa->ifa_flags & IFF_UP) || !(ifa->ifa_flags & IFF_MULTICAST) ||
|
||||
(ifa->ifa_flags & IFF_LOOPBACK)) continue;
|
||||
|
||||
host = ((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr;
|
||||
implMDNSService::host = ((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr;
|
||||
break;
|
||||
}
|
||||
freeifaddrs(ifaddr);
|
||||
}
|
||||
|
||||
if (!mdnsServer) {
|
||||
if (!implMDNSService::mdnsServer) {
|
||||
char hostname[256];
|
||||
struct in_addr addr;
|
||||
|
||||
// it's the same, but who knows..
|
||||
addr.s_addr = host;
|
||||
addr.s_addr = implMDNSService::host;
|
||||
gethostname(hostname, sizeof(hostname));
|
||||
|
||||
mdnsServer = mdnsd_start(addr, false);
|
||||
implMDNSService::mdnsServer = mdnsd_start(addr, false);
|
||||
|
||||
if (mdnsServer) {
|
||||
mdnsd_set_hostname(mdnsServer, hostname, addr);
|
||||
if (implMDNSService::mdnsServer) {
|
||||
mdnsd_set_hostname(implMDNSService::mdnsServer, hostname, addr);
|
||||
}
|
||||
}
|
||||
|
||||
if (mdnsServer) {
|
||||
if (implMDNSService::mdnsServer) {
|
||||
std::vector<const char*> txt;
|
||||
std::vector<std::unique_ptr<std::string>> txtStr;
|
||||
|
||||
@@ -137,25 +173,17 @@ void* MDNSService::registerService(
|
||||
txt.push_back(NULL);
|
||||
std::string type(serviceType + "." + serviceProto + ".local");
|
||||
|
||||
BELL_LOG(info, "MDNS", "using build-in mDNS for %s", serviceName.c_str());
|
||||
struct mdns_service* mdnsService = mdnsd_register_svc(mdnsServer, serviceName.c_str(),
|
||||
BELL_LOG(info, "MDNS", "using built-in mDNS for %s", serviceName.c_str());
|
||||
struct mdns_service* mdnsService = mdnsd_register_svc(implMDNSService::mdnsServer, serviceName.c_str(),
|
||||
type.c_str(), servicePort, NULL, txt.data());
|
||||
if (mdnsService) {
|
||||
return mdnsService;
|
||||
auto service = mdnsd_register_svc(implMDNSService::mdnsServer, serviceName.c_str(),
|
||||
type.c_str(), servicePort, NULL, txt.data());
|
||||
|
||||
return std::make_unique<implMDNSService>(service);
|
||||
}
|
||||
}
|
||||
|
||||
BELL_LOG(error, "MDNS", "cannot start any mDNS listener for %s", serviceName.c_str());
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void MDNSService::unregisterService(void* service) {
|
||||
#ifndef BELL_DISABLE_AVAHI
|
||||
if (avahiClient) {
|
||||
avahi_entry_group_free((AvahiEntryGroup*)service);
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
mdns_service_remove(mdnsServer, (mdns_service*)service);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,13 +17,23 @@
|
||||
|
||||
using namespace bell;
|
||||
|
||||
static struct mdnsd *mdnsService;
|
||||
class implMDNSService : public MDNSService {
|
||||
private:
|
||||
struct mdns_service* service;
|
||||
void unregisterService(void) { mdns_service_remove(implMDNSService::mdnsServer, service); };
|
||||
|
||||
public:
|
||||
static struct mdnsd* mdnsServer;
|
||||
implMDNSService(struct mdns_service* service) : service(service) { };
|
||||
};
|
||||
|
||||
/**
|
||||
* Win32 implementation of MDNSService
|
||||
**/
|
||||
|
||||
void* MDNSService::registerService(
|
||||
struct mdnsd* implMDNSService::mdnsServer = NULL;
|
||||
|
||||
std::unique_ptr<MDNSService> MDNSService::registerService(
|
||||
const std::string& serviceName,
|
||||
const std::string& serviceType,
|
||||
const std::string& serviceProto,
|
||||
@@ -31,7 +41,7 @@ void* MDNSService::registerService(
|
||||
int servicePort,
|
||||
const std::map<std::string, std::string> txtData
|
||||
) {
|
||||
if (!mdnsService) {
|
||||
if (!implMDNSService::mdnsServer) {
|
||||
char hostname[128];
|
||||
gethostname(hostname, sizeof(hostname));
|
||||
|
||||
@@ -49,14 +59,14 @@ void* MDNSService::registerService(
|
||||
if (adapter->FirstGatewayAddress && unicast->Address.lpSockaddr->sa_family == AF_INET) {
|
||||
host = (struct sockaddr_in*)unicast->Address.lpSockaddr;
|
||||
BELL_LOG(info, "mdns", "mDNS on interface %s", inet_ntoa(host->sin_addr));
|
||||
mdnsService = mdnsd_start(host->sin_addr, false);
|
||||
implMDNSService::mdnsServer = mdnsd_start(host->sin_addr, false);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assert(mdnsService);
|
||||
mdnsd_set_hostname(mdnsService, hostname, host->sin_addr);
|
||||
assert(implMDNSService::mdnsServer);
|
||||
mdnsd_set_hostname(implMDNSService::mdnsServer, hostname, host->sin_addr);
|
||||
free(adapters);
|
||||
}
|
||||
|
||||
@@ -71,9 +81,9 @@ void* MDNSService::registerService(
|
||||
txt.push_back(NULL);
|
||||
|
||||
std::string type(serviceType + "." + serviceProto + ".local");
|
||||
return mdnsd_register_svc(mdnsService, serviceName.c_str(), type.c_str(), servicePort, NULL, txt.data());
|
||||
}
|
||||
|
||||
void MDNSService::unregisterService(void* service) {
|
||||
mdns_service_remove(mdnsService, (mdns_service*)service);
|
||||
auto service = mdnsd_register_svc(implMDNSService::mdnsServer, serviceName.c_str(),
|
||||
type.c_str(), servicePort, NULL, txt.data());
|
||||
|
||||
return std::make_unique<implMDNSService>(service);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user