zeroconf: convert to C++

This commit is contained in:
Max Kellermann 2013-01-27 22:18:45 +01:00
parent 068f191c0d
commit 95c3f57b30
9 changed files with 111 additions and 42 deletions

View File

@ -117,7 +117,6 @@ mpd_headers = \
src/uri.h \ src/uri.h \
src/utils.h \ src/utils.h \
src/string_util.h \ src/string_util.h \
src/zeroconf.h src/zeroconf-internal.h \
src/timer.h \ src/timer.h \
src/mpd_error.h src/mpd_error.h
@ -681,14 +680,16 @@ endif
if HAVE_ZEROCONF if HAVE_ZEROCONF
src_mpd_SOURCES += src/zeroconf.c src_mpd_SOURCES += \
src/ZeroconfInternal.hxx \
src/ZeroconfGlue.cxx src/ZeroconfGlue.hxx
if HAVE_AVAHI if HAVE_AVAHI
src_mpd_SOURCES += src/zeroconf-avahi.c src_mpd_SOURCES += src/ZeroconfAvahi.cxx src/ZeroconfAvahi.hxx
endif endif
if HAVE_BONJOUR if HAVE_BONJOUR
src_mpd_SOURCES += src/zeroconf-bonjour.c src_mpd_SOURCES += src/ZeroconfBonjour.cxx src/ZeroconfBonjour.hxx
endif endif
endif endif

View File

@ -49,6 +49,7 @@
#include "IOThread.hxx" #include "IOThread.hxx"
#include "fs/Path.hxx" #include "fs/Path.hxx"
#include "PlaylistRegistry.hxx" #include "PlaylistRegistry.hxx"
#include "ZeroconfGlue.hxx"
extern "C" { extern "C" {
#include "daemon.h" #include "daemon.h"
@ -56,7 +57,6 @@ extern "C" {
#include "audio_config.h" #include "audio_config.h"
#include "pcm_resample.h" #include "pcm_resample.h"
#include "decoder_list.h" #include "decoder_list.h"
#include "zeroconf.h"
} }
#include "mpd_error.h" #include "mpd_error.h"
@ -490,7 +490,7 @@ int mpd_main(int argc, char *argv[])
return EXIT_FAILURE; return EXIT_FAILURE;
} }
initZeroconf(); ZeroconfInit();
player_create(&global_partition->pc); player_create(&global_partition->pc);
@ -551,7 +551,7 @@ int mpd_main(int argc, char *argv[])
} }
global_partition->pc.Kill(); global_partition->pc.Kill();
finishZeroconf(); ZeroconfDeinit();
listen_global_finish(); listen_global_finish();
delete client_list; delete client_list;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2003-2011 The Music Player Daemon Project * Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org * http://www.musicpd.org
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -18,7 +18,8 @@
*/ */
#include "config.h" #include "config.h"
#include "zeroconf-internal.h" #include "ZeroconfAvahi.hxx"
#include "ZeroconfInternal.hxx"
#include "Listen.hxx" #include "Listen.hxx"
#include "mpd_error.h" #include "mpd_error.h"
@ -116,7 +117,8 @@ static void avahiRegisterService(AvahiClient * c)
* if that's better. */ * if that's better. */
ret = avahi_entry_group_add_service(avahiGroup, ret = avahi_entry_group_add_service(avahiGroup,
AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC,
0, avahiName, SERVICE_TYPE, NULL, AvahiPublishFlags(0),
avahiName, SERVICE_TYPE, NULL,
NULL, listen_port, NULL); NULL, listen_port, NULL);
if (ret < 0) { if (ret < 0) {
g_warning("Failed to add service %s: %s", SERVICE_TYPE, g_warning("Failed to add service %s: %s", SERVICE_TYPE,
@ -213,7 +215,8 @@ static void avahiClientCallback(AvahiClient * c, AvahiClientState state,
} }
} }
void init_avahi(const char *serviceName) void
AvahiInit(const char *serviceName)
{ {
int error; int error;
g_debug("Initializing interface"); g_debug("Initializing interface");
@ -234,11 +237,12 @@ void init_avahi(const char *serviceName)
if (!avahiClient) { if (!avahiClient) {
g_warning("Failed to create client: %s", g_warning("Failed to create client: %s",
avahi_strerror(error)); avahi_strerror(error));
avahi_finish(); AvahiDeinit();
} }
} }
void avahi_finish(void) void
AvahiDeinit(void)
{ {
g_debug("Shutting down interface"); g_debug("Shutting down interface");

29
src/ZeroconfAvahi.hxx Normal file
View File

@ -0,0 +1,29 @@
/*
* Copyright (C) 2003-2011 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_AVAHI_HXX
#define MPD_ZEROCONF_AVAHI_HXX
void
AvahiInit(const char *service_name);
void
AvahiDeinit();
#endif

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2003-2011 The Music Player Daemon Project * Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org * http://www.musicpd.org
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -18,7 +18,8 @@
*/ */
#include "config.h" #include "config.h"
#include "zeroconf-internal.h" #include "ZeroconfBonjour.hxx"
#include "ZeroconfInternal.hxx"
#include "Listen.hxx" #include "Listen.hxx"
#include <glib.h> #include <glib.h>
@ -42,7 +43,7 @@ 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.");
bonjour_finish(); BonjourDeinit();
} else { } else {
g_debug("Registered zeroconf service with name '%s'", name); g_debug("Registered zeroconf service with name '%s'", name);
} }
@ -58,10 +59,11 @@ bonjour_channel_event(G_GNUC_UNUSED GIOChannel *source,
return dnsReference != NULL; return dnsReference != NULL;
} }
void init_zeroconf_osx(const char *serviceName) void
BonjourInit(const char *service_name)
{ {
DNSServiceErrorType error = DNSServiceRegister(&dnsReference, DNSServiceErrorType error = DNSServiceRegister(&dnsReference,
0, 0, serviceName, 0, 0, service_name,
SERVICE_TYPE, NULL, NULL, SERVICE_TYPE, NULL, NULL,
g_htons(listen_port), 0, g_htons(listen_port), 0,
NULL, NULL,
@ -82,7 +84,8 @@ void init_zeroconf_osx(const char *serviceName)
g_io_add_watch(bonjour_channel, G_IO_IN, bonjour_channel_event, NULL); g_io_add_watch(bonjour_channel, G_IO_IN, bonjour_channel_event, NULL);
} }
void bonjour_finish(void) void
BonjourDeinit()
{ {
if (bonjour_channel != NULL) { if (bonjour_channel != NULL) {
g_io_channel_unref(bonjour_channel); g_io_channel_unref(bonjour_channel);

29
src/ZeroconfBonjour.hxx Normal file
View File

@ -0,0 +1,29 @@
/*
* Copyright (C) 2003-2013 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_BONJOUR_HXX
#define MPD_ZEROCONF_BONJOUR_HXX
void
BonjourInit(const char *service_name);
void
BonjourDeinit();
#endif

View File

@ -18,8 +18,9 @@
*/ */
#include "config.h" #include "config.h"
#include "zeroconf.h" #include "ZeroconfGlue.hxx"
#include "zeroconf-internal.h" #include "ZeroconfAvahi.hxx"
#include "ZeroconfBonjour.hxx"
#include "conf.h" #include "conf.h"
#include "Listen.hxx" #include "Listen.hxx"
@ -34,7 +35,8 @@
static int zeroconfEnabled; static int zeroconfEnabled;
void initZeroconf(void) void
ZeroconfInit()
{ {
const char *serviceName; const char *serviceName;
@ -52,24 +54,25 @@ void initZeroconf(void)
serviceName = config_get_string(CONF_ZEROCONF_NAME, SERVICE_NAME); serviceName = config_get_string(CONF_ZEROCONF_NAME, SERVICE_NAME);
#ifdef HAVE_AVAHI #ifdef HAVE_AVAHI
init_avahi(serviceName); AvahiInit(serviceName);
#endif #endif
#ifdef HAVE_BONJOUR #ifdef HAVE_BONJOUR
init_zeroconf_osx(serviceName); BonjourInit(serviceName);
#endif #endif
} }
void finishZeroconf(void) void
ZeroconfDeinit()
{ {
if (!zeroconfEnabled) if (!zeroconfEnabled)
return; return;
#ifdef HAVE_AVAHI #ifdef HAVE_AVAHI
avahi_finish(); AvahiDeinit();
#endif /* HAVE_AVAHI */ #endif /* HAVE_AVAHI */
#ifdef HAVE_BONJOUR #ifdef HAVE_BONJOUR
bonjour_finish(); BonjourDeinit();
#endif #endif
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2003-2011 The Music Player Daemon Project * Copyright (C) 2003-2013 The Music Player Daemon Project
* http://www.musicpd.org * http://www.musicpd.org
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -17,20 +17,28 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/ */
#ifndef MPD_ZEROCONF_H #ifndef MPD_ZEROCONF_GLUE_HXX
#define MPD_ZEROCONF_H #define MPD_ZEROCONF_GLUE_HXX
#include "check.h" #include "check.h"
#ifdef HAVE_ZEROCONF #ifdef HAVE_ZEROCONF
void initZeroconf(void); void
void finishZeroconf(void); ZeroconfInit();
void
ZeroconfDeinit();
#else /* ! HAVE_ZEROCONF */ #else /* ! HAVE_ZEROCONF */
static void initZeroconf(void) { } static inline void
static void finishZeroconf(void) { } ZeroconfInit()
{}
static inline void
ZeroconfDeinit()
{}
#endif /* ! HAVE_ZEROCONF */ #endif /* ! HAVE_ZEROCONF */

View File

@ -23,12 +23,4 @@
/* The dns-sd service type qualifier to publish */ /* The dns-sd service type qualifier to publish */
#define SERVICE_TYPE "_mpd._tcp" #define SERVICE_TYPE "_mpd._tcp"
void init_avahi(const char *service_name);
void avahi_finish(void);
void init_zeroconf_osx(const char *service_name);
void bonjour_finish(void);
#endif #endif