zeroconf/Bonjour: return a std::unique_ptr<BonjourHelper>
This commit is contained in:
parent
ceb76b6a82
commit
b01ef1b9a6
@ -19,7 +19,6 @@
|
||||
|
||||
#include "Bonjour.hxx"
|
||||
#include "Internal.hxx"
|
||||
#include "event/SocketEvent.hxx"
|
||||
#include "util/Domain.hxx"
|
||||
#include "Log.hxx"
|
||||
#include "util/Compiler.h"
|
||||
@ -32,38 +31,6 @@
|
||||
|
||||
static constexpr Domain bonjour_domain("bonjour");
|
||||
|
||||
class BonjourHelper final {
|
||||
const DNSServiceRef service_ref;
|
||||
|
||||
SocketEvent socket_event;
|
||||
|
||||
public:
|
||||
BonjourHelper(EventLoop &_loop, const char *name, unsigned port);
|
||||
|
||||
~BonjourHelper() {
|
||||
DNSServiceRefDeallocate(service_ref);
|
||||
}
|
||||
|
||||
BonjourHelper(const BonjourHelper &) = delete;
|
||||
BonjourHelper &operator=(const BonjourHelper &) = delete;
|
||||
|
||||
void Cancel() noexcept {
|
||||
socket_event.Cancel();
|
||||
}
|
||||
|
||||
static void Callback(DNSServiceRef sdRef, DNSServiceFlags flags,
|
||||
DNSServiceErrorType errorCode, const char *name,
|
||||
const char *regtype,
|
||||
const char *domain,
|
||||
void *context) noexcept;
|
||||
|
||||
protected:
|
||||
/* virtual methods from class SocketMonitor */
|
||||
void OnSocketReady([[maybe_unused]] unsigned flags) noexcept {
|
||||
DNSServiceProcessResult(service_ref);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* A wrapper for DNSServiceRegister() which returns the DNSServiceRef
|
||||
* and throws on error.
|
||||
@ -96,8 +63,6 @@ BonjourHelper::BonjourHelper(EventLoop &_loop, const char *name, unsigned port)
|
||||
socket_event.ScheduleRead();
|
||||
}
|
||||
|
||||
static BonjourHelper *bonjour_monitor;
|
||||
|
||||
void
|
||||
BonjourHelper::Callback([[maybe_unused]] DNSServiceRef sdRef,
|
||||
[[maybe_unused]] DNSServiceFlags flags,
|
||||
@ -120,14 +85,8 @@ BonjourHelper::Callback([[maybe_unused]] DNSServiceRef sdRef,
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
std::unique_ptr<BonjourHelper>
|
||||
BonjourInit(EventLoop &loop, const char *service_name, unsigned port)
|
||||
{
|
||||
bonjour_monitor = new BonjourHelper(loop, service_name, port);
|
||||
}
|
||||
|
||||
void
|
||||
BonjourDeinit()
|
||||
{
|
||||
delete bonjour_monitor;
|
||||
return std::make_unique<BonjourHelper>(loop, service_name, port);
|
||||
}
|
||||
|
@ -20,15 +20,50 @@
|
||||
#ifndef MPD_ZEROCONF_BONJOUR_HXX
|
||||
#define MPD_ZEROCONF_BONJOUR_HXX
|
||||
|
||||
#include "event/SocketEvent.hxx"
|
||||
|
||||
#include <dns_sd.h>
|
||||
|
||||
#include <memory>
|
||||
|
||||
class EventLoop;
|
||||
|
||||
class BonjourHelper final {
|
||||
const DNSServiceRef service_ref;
|
||||
|
||||
SocketEvent socket_event;
|
||||
|
||||
public:
|
||||
BonjourHelper(EventLoop &_loop, const char *name, unsigned port);
|
||||
|
||||
~BonjourHelper() noexcept {
|
||||
DNSServiceRefDeallocate(service_ref);
|
||||
}
|
||||
|
||||
BonjourHelper(const BonjourHelper &) = delete;
|
||||
BonjourHelper &operator=(const BonjourHelper &) = delete;
|
||||
|
||||
private:
|
||||
void Cancel() noexcept {
|
||||
socket_event.Cancel();
|
||||
}
|
||||
|
||||
static void Callback(DNSServiceRef sdRef, DNSServiceFlags flags,
|
||||
DNSServiceErrorType errorCode, const char *name,
|
||||
const char *regtype,
|
||||
const char *domain,
|
||||
void *context) noexcept;
|
||||
|
||||
/* virtual methods from class SocketMonitor */
|
||||
void OnSocketReady([[maybe_unused]] unsigned flags) noexcept {
|
||||
DNSServiceProcessResult(service_ref);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Throws on error.
|
||||
*/
|
||||
void
|
||||
std::unique_ptr<BonjourHelper>
|
||||
BonjourInit(EventLoop &loop, const char *service_name, unsigned port);
|
||||
|
||||
void
|
||||
BonjourDeinit();
|
||||
|
||||
#endif
|
||||
|
@ -19,7 +19,6 @@
|
||||
|
||||
#include "Glue.hxx"
|
||||
#include "avahi/Init.hxx"
|
||||
#include "Bonjour.hxx"
|
||||
#include "config/Data.hxx"
|
||||
#include "config/Option.hxx"
|
||||
#include "Listen.hxx"
|
||||
@ -27,6 +26,10 @@
|
||||
#include "Log.hxx"
|
||||
#include "util/Compiler.h"
|
||||
|
||||
#ifdef HAVE_BONJOUR
|
||||
#include "Bonjour.hxx"
|
||||
#endif
|
||||
|
||||
#include <climits>
|
||||
|
||||
#include <string.h>
|
||||
@ -49,6 +52,10 @@ static constexpr Domain zeroconf_domain("zeroconf");
|
||||
|
||||
static int zeroconfEnabled;
|
||||
|
||||
#ifdef HAVE_BONJOUR
|
||||
static std::unique_ptr<BonjourHelper> bonjour_helper;
|
||||
#endif
|
||||
|
||||
void
|
||||
ZeroconfInit(const ConfigData &config, [[maybe_unused]] EventLoop &loop)
|
||||
{
|
||||
@ -86,21 +93,21 @@ ZeroconfInit(const ConfigData &config, [[maybe_unused]] EventLoop &loop)
|
||||
#endif
|
||||
|
||||
#ifdef HAVE_BONJOUR
|
||||
BonjourInit(loop, serviceName, listen_port);
|
||||
bonjour_helper = BonjourInit(loop, serviceName, listen_port);
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
ZeroconfDeinit() noexcept
|
||||
{
|
||||
#ifdef HAVE_BONJOUR
|
||||
bonjour_helper.reset();
|
||||
#endif
|
||||
|
||||
if (!zeroconfEnabled)
|
||||
return;
|
||||
|
||||
#ifdef HAVE_AVAHI
|
||||
AvahiDeinit();
|
||||
#endif /* HAVE_AVAHI */
|
||||
|
||||
#ifdef HAVE_BONJOUR
|
||||
BonjourDeinit();
|
||||
#endif
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user