diff --git a/src/Main.cxx b/src/Main.cxx index 66a4c08e1..1b99ce736 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -476,12 +476,15 @@ MainConfigured(const struct options &options, const ConfigData &raw_config) }; #endif +#ifdef HAVE_ZEROCONF + std::unique_ptr zeroconf; try { - ZeroconfInit(raw_config, instance.event_loop); + zeroconf = ZeroconfInit(raw_config, instance.event_loop); } catch (...) { LogError(std::current_exception(), "Zeroconf initialization failed"); } +#endif #ifdef ENABLE_DATABASE if (create_db) { @@ -543,7 +546,9 @@ MainConfigured(const struct options &options, const ConfigData &raw_config) instance.BeginShutdownUpdate(); - ZeroconfDeinit(); +#ifdef HAVE_ZEROCONF + zeroconf.reset(); +#endif instance.BeginShutdownPartitions(); } diff --git a/src/zeroconf/Glue.cxx b/src/zeroconf/Glue.cxx index 4d9b52868..06af7a041 100644 --- a/src/zeroconf/Glue.cxx +++ b/src/zeroconf/Glue.cxx @@ -18,6 +18,7 @@ */ #include "Glue.hxx" +#include "Helper.hxx" #include "config/Data.hxx" #include "config/Option.hxx" #include "Listen.hxx" @@ -53,31 +54,19 @@ static constexpr Domain zeroconf_domain("zeroconf"); #define DEFAULT_ZEROCONF_ENABLED 1 -static int zeroconfEnabled; - -#ifdef HAVE_AVAHI -static std::unique_ptr avahi_helper; -#endif - -#ifdef HAVE_BONJOUR -static std::unique_ptr bonjour_helper; -#endif - -void +std::unique_ptr ZeroconfInit(const ConfigData &config, [[maybe_unused]] EventLoop &loop) { const char *serviceName; - zeroconfEnabled = config.GetBool(ConfigOption::ZEROCONF_ENABLED, - DEFAULT_ZEROCONF_ENABLED); - if (!zeroconfEnabled) - return; + if (!config.GetBool(ConfigOption::ZEROCONF_ENABLED, + DEFAULT_ZEROCONF_ENABLED)) + return nullptr; if (listen_port <= 0) { LogWarning(zeroconf_domain, "No global port, disabling zeroconf"); - zeroconfEnabled = false; - return; + return nullptr; } serviceName = config.GetString(ConfigOption::ZEROCONF_NAME, @@ -96,22 +85,12 @@ ZeroconfInit(const ConfigData &config, [[maybe_unused]] EventLoop &loop) } #ifdef HAVE_AVAHI - avahi_helper = AvahiInit(loop, serviceName, listen_port); + return std::make_unique(AvahiInit(loop, serviceName, + listen_port)); #endif #ifdef HAVE_BONJOUR - bonjour_helper = BonjourInit(loop, serviceName, listen_port); -#endif -} - -void -ZeroconfDeinit() noexcept -{ -#ifdef HAVE_AVAHI - avahi_helper.reset(); -#endif - -#ifdef HAVE_BONJOUR - bonjour_helper.reset(); + return std::make_unique(BonjourInit(loop, serviceName, + listen_port)); #endif } diff --git a/src/zeroconf/Glue.hxx b/src/zeroconf/Glue.hxx index ca379640b..79ea21e19 100644 --- a/src/zeroconf/Glue.hxx +++ b/src/zeroconf/Glue.hxx @@ -20,32 +20,23 @@ #ifndef MPD_ZEROCONF_GLUE_HXX #define MPD_ZEROCONF_GLUE_HXX +#include "Helper.hxx" #include "config.h" +#include + struct ConfigData; class EventLoop; +class ZeroconfHelper; #ifdef HAVE_ZEROCONF /** * Throws on error. */ -void +std::unique_ptr ZeroconfInit(const ConfigData &config, EventLoop &loop); -void -ZeroconfDeinit() noexcept; - -#else /* ! HAVE_ZEROCONF */ - -static inline void -ZeroconfInit(const ConfigData &, EventLoop &) -{} - -static inline void -ZeroconfDeinit() noexcept -{} - #endif /* ! HAVE_ZEROCONF */ #endif diff --git a/src/zeroconf/Helper.cxx b/src/zeroconf/Helper.cxx new file mode 100644 index 000000000..c44f47193 --- /dev/null +++ b/src/zeroconf/Helper.cxx @@ -0,0 +1,30 @@ +/* + * Copyright 2003-2021 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "Helper.hxx" + +#ifdef HAVE_AVAHI +#include "avahi/Helper.hxx" +#endif + +#ifdef HAVE_BONJOUR +#include "Bonjour.hxx" +#endif + +ZeroconfHelper::~ZeroconfHelper() noexcept = default; diff --git a/src/zeroconf/Helper.hxx b/src/zeroconf/Helper.hxx new file mode 100644 index 000000000..7c7a22215 --- /dev/null +++ b/src/zeroconf/Helper.hxx @@ -0,0 +1,47 @@ +/* + * Copyright 2003-2021 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_ZEROCONF_HELPER_HXX +#define MPD_ZEROCONF_HELPER_HXX + +#include "config.h" + +#include + +class AvahiHelper; +class BonjourHelper; + +class ZeroconfHelper final { +#ifdef HAVE_AVAHI + std::unique_ptr helper; +#endif + +#ifdef HAVE_BONJOUR + std::unique_ptr helper; +#endif + +public: + template + ZeroconfHelper(T &&_helper) noexcept + :helper(std::forward(_helper)) {} + + ~ZeroconfHelper() noexcept; +}; + +#endif diff --git a/src/zeroconf/meson.build b/src/zeroconf/meson.build index 621908f1f..92292e475 100644 --- a/src/zeroconf/meson.build +++ b/src/zeroconf/meson.build @@ -36,6 +36,7 @@ if zeroconf_option == 'bonjour' zeroconf = static_library( 'zeroconf_bonjour', 'Glue.cxx', + 'Helper.cxx', 'Bonjour.cxx', include_directories: inc, dependencies: [ @@ -60,6 +61,7 @@ else zeroconf = static_library( 'zeroconf_bonjour', 'Glue.cxx', + 'Helper.cxx', include_directories: inc, dependencies: [ avahi_dep,