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