From b01ef1b9a6f865a2c0fccf19cf36304807a88399 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 24 Feb 2021 13:39:55 +0100 Subject: [PATCH] zeroconf/Bonjour: return a std::unique_ptr --- src/zeroconf/Bonjour.cxx | 45 ++-------------------------------------- src/zeroconf/Bonjour.hxx | 43 ++++++++++++++++++++++++++++++++++---- src/zeroconf/Glue.cxx | 19 +++++++++++------ 3 files changed, 54 insertions(+), 53 deletions(-) diff --git a/src/zeroconf/Bonjour.cxx b/src/zeroconf/Bonjour.cxx index 063177a07..eb2e58adb 100644 --- a/src/zeroconf/Bonjour.cxx +++ b/src/zeroconf/Bonjour.cxx @@ -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 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(loop, service_name, port); } diff --git a/src/zeroconf/Bonjour.hxx b/src/zeroconf/Bonjour.hxx index 3d9684c14..3cb728366 100644 --- a/src/zeroconf/Bonjour.hxx +++ b/src/zeroconf/Bonjour.hxx @@ -20,15 +20,50 @@ #ifndef MPD_ZEROCONF_BONJOUR_HXX #define MPD_ZEROCONF_BONJOUR_HXX +#include "event/SocketEvent.hxx" + +#include + +#include + 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 BonjourInit(EventLoop &loop, const char *service_name, unsigned port); -void -BonjourDeinit(); - #endif diff --git a/src/zeroconf/Glue.cxx b/src/zeroconf/Glue.cxx index 0038819ea..9bb070a27 100644 --- a/src/zeroconf/Glue.cxx +++ b/src/zeroconf/Glue.cxx @@ -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 #include @@ -49,6 +52,10 @@ static constexpr Domain zeroconf_domain("zeroconf"); static int zeroconfEnabled; +#ifdef HAVE_BONJOUR +static std::unique_ptr 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 }