From 0988056471fefa2ee195a170dcf57771e7926a08 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 27 Jan 2013 22:32:10 +0100 Subject: ZeroconfBonjour: use SocketMonitor instead of GIOChannel --- src/Main.cxx | 2 +- src/ZeroconfBonjour.cxx | 55 ++++++++++++++++++++++++++----------------------- src/ZeroconfBonjour.hxx | 4 +++- src/ZeroconfGlue.cxx | 5 +++-- src/ZeroconfGlue.hxx | 6 ++++-- 5 files changed, 40 insertions(+), 32 deletions(-) (limited to 'src') diff --git a/src/Main.cxx b/src/Main.cxx index 82846201..6f8bc27d 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -490,7 +490,7 @@ int mpd_main(int argc, char *argv[]) return EXIT_FAILURE; } - ZeroconfInit(); + ZeroconfInit(*main_loop); player_create(&global_partition->pc); diff --git a/src/ZeroconfBonjour.cxx b/src/ZeroconfBonjour.cxx index 929ff610..ac58a83e 100644 --- a/src/ZeroconfBonjour.cxx +++ b/src/ZeroconfBonjour.cxx @@ -21,6 +21,8 @@ #include "ZeroconfBonjour.hxx" #include "ZeroconfInternal.hxx" #include "Listen.hxx" +#include "event/SocketMonitor.hxx" +#include "gcc.h" #include @@ -29,8 +31,28 @@ #undef G_LOG_DOMAIN #define G_LOG_DOMAIN "bonjour" -static DNSServiceRef dnsReference; -static GIOChannel *bonjour_channel; +class BonjourMonitor final : public SocketMonitor { + DNSServiceRef service_ref; + +public: + BonjourMonitor(EventLoop &_loop, DNSServiceRef _service_ref) + :SocketMonitor(DNSServiceRefSockFD(_service_ref), _loop), + service_ref(_service_ref) { + ScheduleRead(); + } + + ~BonjourMonitor() { + Steal(); + DNSServiceRefDeallocate(service_ref); + } + +protected: + virtual void OnSocketReady(gcc_unused unsigned flags) override { + DNSServiceProcessResult(service_ref); + } +}; + +static BonjourMonitor *bonjour_monitor; static void dnsRegisterCallback(G_GNUC_UNUSED DNSServiceRef sdRef, @@ -43,25 +65,16 @@ dnsRegisterCallback(G_GNUC_UNUSED DNSServiceRef sdRef, if (errorCode != kDNSServiceErr_NoError) { g_warning("Failed to register zeroconf service."); - BonjourDeinit(); + bonjour_monitor->Cancel(); } else { g_debug("Registered zeroconf service with name '%s'", name); } } -static gboolean -bonjour_channel_event(G_GNUC_UNUSED GIOChannel *source, - G_GNUC_UNUSED GIOCondition condition, - G_GNUC_UNUSED gpointer data) -{ - DNSServiceProcessResult(dnsReference); - - return dnsReference != NULL; -} - void -BonjourInit(const char *service_name) +BonjourInit(EventLoop &loop, const char *service_name) { + DNSServiceRef dnsReference; DNSServiceErrorType error = DNSServiceRegister(&dnsReference, 0, 0, service_name, SERVICE_TYPE, NULL, NULL, @@ -80,21 +93,11 @@ BonjourInit(const char *service_name) return; } - bonjour_channel = g_io_channel_unix_new(DNSServiceRefSockFD(dnsReference)); - g_io_add_watch(bonjour_channel, G_IO_IN, bonjour_channel_event, NULL); + bonjour_monitor = new BonjourMonitor(loop, dnsReference); } void BonjourDeinit() { - if (bonjour_channel != NULL) { - g_io_channel_unref(bonjour_channel); - bonjour_channel = NULL; - } - - if (dnsReference != NULL) { - DNSServiceRefDeallocate(dnsReference); - dnsReference = NULL; - g_debug("Deregistered Zeroconf service."); - } + delete bonjour_monitor; } diff --git a/src/ZeroconfBonjour.hxx b/src/ZeroconfBonjour.hxx index 5d2470eb..d91fe9a0 100644 --- a/src/ZeroconfBonjour.hxx +++ b/src/ZeroconfBonjour.hxx @@ -20,8 +20,10 @@ #ifndef MPD_ZEROCONF_BONJOUR_HXX #define MPD_ZEROCONF_BONJOUR_HXX +class EventLoop; + void -BonjourInit(const char *service_name); +BonjourInit(EventLoop &loop, const char *service_name); void BonjourDeinit(); diff --git a/src/ZeroconfGlue.cxx b/src/ZeroconfGlue.cxx index 3facd576..fd53faa4 100644 --- a/src/ZeroconfGlue.cxx +++ b/src/ZeroconfGlue.cxx @@ -23,6 +23,7 @@ #include "ZeroconfBonjour.hxx" #include "conf.h" #include "Listen.hxx" +#include "gcc.h" #include @@ -36,7 +37,7 @@ static int zeroconfEnabled; void -ZeroconfInit() +ZeroconfInit(gcc_unused EventLoop &loop) { const char *serviceName; @@ -58,7 +59,7 @@ ZeroconfInit() #endif #ifdef HAVE_BONJOUR - BonjourInit(serviceName); + BonjourInit(loop, serviceName); #endif } diff --git a/src/ZeroconfGlue.hxx b/src/ZeroconfGlue.hxx index dcd7b81c..2a291ce2 100644 --- a/src/ZeroconfGlue.hxx +++ b/src/ZeroconfGlue.hxx @@ -22,10 +22,12 @@ #include "check.h" +class EventLoop; + #ifdef HAVE_ZEROCONF void -ZeroconfInit(); +ZeroconfInit(EventLoop &loop); void ZeroconfDeinit(); @@ -33,7 +35,7 @@ ZeroconfDeinit(); #else /* ! HAVE_ZEROCONF */ static inline void -ZeroconfInit() +ZeroconfInit(EventLoop &) {} static inline void -- cgit v1.2.3