ZeroconfBonjour: use SocketMonitor instead of GIOChannel

This commit is contained in:
Max Kellermann 2013-01-27 22:32:10 +01:00
parent 95c3f57b30
commit 0988056471
5 changed files with 40 additions and 32 deletions

View File

@ -490,7 +490,7 @@ int mpd_main(int argc, char *argv[])
return EXIT_FAILURE;
}
ZeroconfInit();
ZeroconfInit(*main_loop);
player_create(&global_partition->pc);

View File

@ -21,6 +21,8 @@
#include "ZeroconfBonjour.hxx"
#include "ZeroconfInternal.hxx"
#include "Listen.hxx"
#include "event/SocketMonitor.hxx"
#include "gcc.h"
#include <glib.h>
@ -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;
}

View File

@ -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();

View File

@ -23,6 +23,7 @@
#include "ZeroconfBonjour.hxx"
#include "conf.h"
#include "Listen.hxx"
#include "gcc.h"
#include <glib.h>
@ -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
}

View File

@ -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