Commit 09880564 authored by Max Kellermann's avatar Max Kellermann

ZeroconfBonjour: use SocketMonitor instead of GIOChannel

parent 95c3f57b
...@@ -490,7 +490,7 @@ int mpd_main(int argc, char *argv[]) ...@@ -490,7 +490,7 @@ int mpd_main(int argc, char *argv[])
return EXIT_FAILURE; return EXIT_FAILURE;
} }
ZeroconfInit(); ZeroconfInit(*main_loop);
player_create(&global_partition->pc); player_create(&global_partition->pc);
......
...@@ -21,6 +21,8 @@ ...@@ -21,6 +21,8 @@
#include "ZeroconfBonjour.hxx" #include "ZeroconfBonjour.hxx"
#include "ZeroconfInternal.hxx" #include "ZeroconfInternal.hxx"
#include "Listen.hxx" #include "Listen.hxx"
#include "event/SocketMonitor.hxx"
#include "gcc.h"
#include <glib.h> #include <glib.h>
...@@ -29,8 +31,28 @@ ...@@ -29,8 +31,28 @@
#undef G_LOG_DOMAIN #undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "bonjour" #define G_LOG_DOMAIN "bonjour"
static DNSServiceRef dnsReference; class BonjourMonitor final : public SocketMonitor {
static GIOChannel *bonjour_channel; 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 static void
dnsRegisterCallback(G_GNUC_UNUSED DNSServiceRef sdRef, dnsRegisterCallback(G_GNUC_UNUSED DNSServiceRef sdRef,
...@@ -43,25 +65,16 @@ dnsRegisterCallback(G_GNUC_UNUSED DNSServiceRef sdRef, ...@@ -43,25 +65,16 @@ dnsRegisterCallback(G_GNUC_UNUSED DNSServiceRef sdRef,
if (errorCode != kDNSServiceErr_NoError) { if (errorCode != kDNSServiceErr_NoError) {
g_warning("Failed to register zeroconf service."); g_warning("Failed to register zeroconf service.");
BonjourDeinit(); bonjour_monitor->Cancel();
} else { } else {
g_debug("Registered zeroconf service with name '%s'", name); 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 void
BonjourInit(const char *service_name) BonjourInit(EventLoop &loop, const char *service_name)
{ {
DNSServiceRef dnsReference;
DNSServiceErrorType error = DNSServiceRegister(&dnsReference, DNSServiceErrorType error = DNSServiceRegister(&dnsReference,
0, 0, service_name, 0, 0, service_name,
SERVICE_TYPE, NULL, NULL, SERVICE_TYPE, NULL, NULL,
...@@ -80,21 +93,11 @@ BonjourInit(const char *service_name) ...@@ -80,21 +93,11 @@ BonjourInit(const char *service_name)
return; return;
} }
bonjour_channel = g_io_channel_unix_new(DNSServiceRefSockFD(dnsReference)); bonjour_monitor = new BonjourMonitor(loop, dnsReference);
g_io_add_watch(bonjour_channel, G_IO_IN, bonjour_channel_event, NULL);
} }
void void
BonjourDeinit() BonjourDeinit()
{ {
if (bonjour_channel != NULL) { delete bonjour_monitor;
g_io_channel_unref(bonjour_channel);
bonjour_channel = NULL;
}
if (dnsReference != NULL) {
DNSServiceRefDeallocate(dnsReference);
dnsReference = NULL;
g_debug("Deregistered Zeroconf service.");
}
} }
...@@ -20,8 +20,10 @@ ...@@ -20,8 +20,10 @@
#ifndef MPD_ZEROCONF_BONJOUR_HXX #ifndef MPD_ZEROCONF_BONJOUR_HXX
#define MPD_ZEROCONF_BONJOUR_HXX #define MPD_ZEROCONF_BONJOUR_HXX
class EventLoop;
void void
BonjourInit(const char *service_name); BonjourInit(EventLoop &loop, const char *service_name);
void void
BonjourDeinit(); BonjourDeinit();
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "ZeroconfBonjour.hxx" #include "ZeroconfBonjour.hxx"
#include "conf.h" #include "conf.h"
#include "Listen.hxx" #include "Listen.hxx"
#include "gcc.h"
#include <glib.h> #include <glib.h>
...@@ -36,7 +37,7 @@ ...@@ -36,7 +37,7 @@
static int zeroconfEnabled; static int zeroconfEnabled;
void void
ZeroconfInit() ZeroconfInit(gcc_unused EventLoop &loop)
{ {
const char *serviceName; const char *serviceName;
...@@ -58,7 +59,7 @@ ZeroconfInit() ...@@ -58,7 +59,7 @@ ZeroconfInit()
#endif #endif
#ifdef HAVE_BONJOUR #ifdef HAVE_BONJOUR
BonjourInit(serviceName); BonjourInit(loop, serviceName);
#endif #endif
} }
......
...@@ -22,10 +22,12 @@ ...@@ -22,10 +22,12 @@
#include "check.h" #include "check.h"
class EventLoop;
#ifdef HAVE_ZEROCONF #ifdef HAVE_ZEROCONF
void void
ZeroconfInit(); ZeroconfInit(EventLoop &loop);
void void
ZeroconfDeinit(); ZeroconfDeinit();
...@@ -33,7 +35,7 @@ ZeroconfDeinit(); ...@@ -33,7 +35,7 @@ ZeroconfDeinit();
#else /* ! HAVE_ZEROCONF */ #else /* ! HAVE_ZEROCONF */
static inline void static inline void
ZeroconfInit() ZeroconfInit(EventLoop &)
{} {}
static inline void static inline void
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment