ZeroconfAvahi: integrate into the EventLoop
Finally fixes the regression by commit c1f4f1fd
.
This commit is contained in:
parent
964aa42b04
commit
d1c401fe7a
@ -724,7 +724,9 @@ src_mpd_SOURCES += \
|
|||||||
src/ZeroconfGlue.cxx src/ZeroconfGlue.hxx
|
src/ZeroconfGlue.cxx src/ZeroconfGlue.hxx
|
||||||
|
|
||||||
if HAVE_AVAHI
|
if HAVE_AVAHI
|
||||||
src_mpd_SOURCES += src/ZeroconfAvahi.cxx src/ZeroconfAvahi.hxx
|
src_mpd_SOURCES += \
|
||||||
|
src/AvahiPoll.cxx src/AvahiPoll.hxx \
|
||||||
|
src/ZeroconfAvahi.cxx src/ZeroconfAvahi.hxx
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if HAVE_BONJOUR
|
if HAVE_BONJOUR
|
||||||
@ -1346,7 +1348,7 @@ test_software_volume_LDADD = \
|
|||||||
|
|
||||||
test_run_avahi_SOURCES = \
|
test_run_avahi_SOURCES = \
|
||||||
src/Log.cxx \
|
src/Log.cxx \
|
||||||
src/ZeroconfAvahi.cxx \
|
src/ZeroconfAvahi.cxx src/AvahiPoll.cxx \
|
||||||
test/ShutdownHandler.cxx test/ShutdownHandler.hxx \
|
test/ShutdownHandler.cxx test/ShutdownHandler.hxx \
|
||||||
test/run_avahi.cxx
|
test/run_avahi.cxx
|
||||||
test_run_avahi_CPPFLAGS = $(AM_CPPFLAGS) \
|
test_run_avahi_CPPFLAGS = $(AM_CPPFLAGS) \
|
||||||
@ -1354,6 +1356,7 @@ test_run_avahi_CPPFLAGS = $(AM_CPPFLAGS) \
|
|||||||
test_run_avahi_LDADD = \
|
test_run_avahi_LDADD = \
|
||||||
libevent.a \
|
libevent.a \
|
||||||
libsystem.a \
|
libsystem.a \
|
||||||
|
$(GLIB_LIBS) \
|
||||||
$(AVAHI_LIBS)
|
$(AVAHI_LIBS)
|
||||||
|
|
||||||
test_run_normalize_SOURCES = test/run_normalize.cxx \
|
test_run_normalize_SOURCES = test/run_normalize.cxx \
|
||||||
|
@ -628,8 +628,8 @@ avahi)
|
|||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
MPD_AUTO_PKG(avahi, AVAHI, [avahi-client avahi-glib],
|
MPD_AUTO_PKG(avahi, AVAHI, [avahi-client],
|
||||||
[avahi client library], [avahi client+glib not found])
|
[avahi client library], [avahi-client not found])
|
||||||
if test x$enable_avahi = xyes; then
|
if test x$enable_avahi = xyes; then
|
||||||
AC_DEFINE([HAVE_AVAHI], 1, [Define to enable Avahi Zeroconf support])
|
AC_DEFINE([HAVE_AVAHI], 1, [Define to enable Avahi Zeroconf support])
|
||||||
with_zeroconf=avahi
|
with_zeroconf=avahi
|
||||||
|
147
src/AvahiPoll.cxx
Normal file
147
src/AvahiPoll.cxx
Normal file
@ -0,0 +1,147 @@
|
|||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
#include "AvahiPoll.hxx"
|
||||||
|
#include "event/Loop.hxx"
|
||||||
|
#include "event/SocketMonitor.hxx"
|
||||||
|
#include "event/TimeoutMonitor.hxx"
|
||||||
|
|
||||||
|
static unsigned
|
||||||
|
FromAvahiWatchEvent(AvahiWatchEvent e)
|
||||||
|
{
|
||||||
|
return (e & AVAHI_WATCH_IN ? SocketMonitor::READ : 0) |
|
||||||
|
(e & AVAHI_WATCH_OUT ? SocketMonitor::WRITE : 0) |
|
||||||
|
(e & AVAHI_WATCH_ERR ? SocketMonitor::ERROR : 0) |
|
||||||
|
(e & AVAHI_WATCH_HUP ? SocketMonitor::HANGUP : 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static AvahiWatchEvent
|
||||||
|
ToAvahiWatchEvent(unsigned e)
|
||||||
|
{
|
||||||
|
return AvahiWatchEvent((e & SocketMonitor::READ ? AVAHI_WATCH_IN : 0) |
|
||||||
|
(e & SocketMonitor::WRITE ? AVAHI_WATCH_OUT : 0) |
|
||||||
|
(e & SocketMonitor::ERROR ? AVAHI_WATCH_ERR : 0) |
|
||||||
|
(e & SocketMonitor::HANGUP ? AVAHI_WATCH_HUP : 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
struct AvahiWatch final : private SocketMonitor {
|
||||||
|
private:
|
||||||
|
const AvahiWatchCallback callback;
|
||||||
|
void *const userdata;
|
||||||
|
|
||||||
|
AvahiWatchEvent received;
|
||||||
|
|
||||||
|
public:
|
||||||
|
AvahiWatch(int _fd, AvahiWatchEvent _event,
|
||||||
|
AvahiWatchCallback _callback, void *_userdata,
|
||||||
|
EventLoop &_loop)
|
||||||
|
:SocketMonitor(_fd, _loop),
|
||||||
|
callback(_callback), userdata(_userdata),
|
||||||
|
received(AvahiWatchEvent(0)) {
|
||||||
|
Schedule(FromAvahiWatchEvent(_event));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void WatchUpdate(AvahiWatch *w, AvahiWatchEvent event) {
|
||||||
|
w->Schedule(FromAvahiWatchEvent(event));
|
||||||
|
}
|
||||||
|
|
||||||
|
static AvahiWatchEvent WatchGetEvents(AvahiWatch *w) {
|
||||||
|
return w->received;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void WatchFree(AvahiWatch *w) {
|
||||||
|
delete w;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual bool OnSocketReady(unsigned flags) {
|
||||||
|
received = ToAvahiWatchEvent(flags);
|
||||||
|
callback(this, Get(), received, userdata);
|
||||||
|
received = AvahiWatchEvent(0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
static constexpr unsigned
|
||||||
|
TimevalToMS(const timeval &tv)
|
||||||
|
{
|
||||||
|
return tv.tv_sec * 1000 + (tv.tv_usec + 500) / 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct AvahiTimeout final : private TimeoutMonitor {
|
||||||
|
private:
|
||||||
|
const AvahiTimeoutCallback callback;
|
||||||
|
void *const userdata;
|
||||||
|
|
||||||
|
public:
|
||||||
|
AvahiTimeout(const struct timeval *tv,
|
||||||
|
AvahiTimeoutCallback _callback, void *_userdata,
|
||||||
|
EventLoop &_loop)
|
||||||
|
:TimeoutMonitor(_loop),
|
||||||
|
callback(_callback), userdata(_userdata) {
|
||||||
|
if (tv != nullptr)
|
||||||
|
Schedule(TimevalToMS(*tv));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void TimeoutUpdate(AvahiTimeout *t, const struct timeval *tv) {
|
||||||
|
if (tv != nullptr)
|
||||||
|
t->Schedule(TimevalToMS(*tv));
|
||||||
|
else
|
||||||
|
t->Cancel();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void TimeoutFree(AvahiTimeout *t) {
|
||||||
|
delete t;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void OnTimeout() {
|
||||||
|
callback(this, userdata);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
MyAvahiPoll::MyAvahiPoll(EventLoop &_loop):event_loop(_loop)
|
||||||
|
{
|
||||||
|
watch_new = WatchNew;
|
||||||
|
watch_update = AvahiWatch::WatchUpdate;
|
||||||
|
watch_get_events = AvahiWatch::WatchGetEvents;
|
||||||
|
watch_free = AvahiWatch::WatchFree;
|
||||||
|
timeout_new = TimeoutNew;
|
||||||
|
timeout_update = AvahiTimeout::TimeoutUpdate;
|
||||||
|
timeout_free = AvahiTimeout::TimeoutFree;
|
||||||
|
}
|
||||||
|
|
||||||
|
AvahiWatch *
|
||||||
|
MyAvahiPoll::WatchNew(const AvahiPoll *api, int fd, AvahiWatchEvent event,
|
||||||
|
AvahiWatchCallback callback, void *userdata) {
|
||||||
|
const MyAvahiPoll &poll = *(const MyAvahiPoll *)api;
|
||||||
|
|
||||||
|
return new AvahiWatch(fd, event, callback, userdata,
|
||||||
|
poll.event_loop);
|
||||||
|
}
|
||||||
|
|
||||||
|
AvahiTimeout *
|
||||||
|
MyAvahiPoll::TimeoutNew(const AvahiPoll *api, const struct timeval *tv,
|
||||||
|
AvahiTimeoutCallback callback, void *userdata) {
|
||||||
|
const MyAvahiPoll &poll = *(const MyAvahiPoll *)api;
|
||||||
|
|
||||||
|
return new AvahiTimeout(tv, callback, userdata,
|
||||||
|
poll.event_loop);
|
||||||
|
}
|
48
src/AvahiPoll.hxx
Normal file
48
src/AvahiPoll.hxx
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
/*
|
||||||
|
* 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_AVAHI_POLL_HXX
|
||||||
|
#define MPD_AVAHI_POLL_HXX
|
||||||
|
|
||||||
|
#include "check.h"
|
||||||
|
#include "Compiler.h"
|
||||||
|
|
||||||
|
#include <avahi-common/watch.h>
|
||||||
|
|
||||||
|
class EventLoop;
|
||||||
|
|
||||||
|
class MyAvahiPoll final : public AvahiPoll {
|
||||||
|
EventLoop &event_loop;
|
||||||
|
|
||||||
|
public:
|
||||||
|
MyAvahiPoll(EventLoop &_loop);
|
||||||
|
|
||||||
|
private:
|
||||||
|
static AvahiWatch *WatchNew(const AvahiPoll *api, int fd,
|
||||||
|
AvahiWatchEvent event,
|
||||||
|
AvahiWatchCallback callback,
|
||||||
|
void *userdata);
|
||||||
|
|
||||||
|
static AvahiTimeout *TimeoutNew(const AvahiPoll *api,
|
||||||
|
const struct timeval *tv,
|
||||||
|
AvahiTimeoutCallback callback,
|
||||||
|
void *userdata);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
@ -19,33 +19,29 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "ZeroconfAvahi.hxx"
|
#include "ZeroconfAvahi.hxx"
|
||||||
|
#include "AvahiPoll.hxx"
|
||||||
#include "ZeroconfInternal.hxx"
|
#include "ZeroconfInternal.hxx"
|
||||||
#include "Listen.hxx"
|
#include "Listen.hxx"
|
||||||
#include "event/Loop.hxx"
|
|
||||||
#include "system/FatalError.hxx"
|
#include "system/FatalError.hxx"
|
||||||
#include "util/Domain.hxx"
|
#include "util/Domain.hxx"
|
||||||
#include "Log.hxx"
|
#include "Log.hxx"
|
||||||
|
|
||||||
#include <glib.h>
|
|
||||||
|
|
||||||
#include <avahi-client/client.h>
|
#include <avahi-client/client.h>
|
||||||
#include <avahi-client/publish.h>
|
#include <avahi-client/publish.h>
|
||||||
|
|
||||||
|
#include <avahi-common/watch.h>
|
||||||
#include <avahi-common/alternative.h>
|
#include <avahi-common/alternative.h>
|
||||||
#include <avahi-common/domain.h>
|
#include <avahi-common/domain.h>
|
||||||
#include <avahi-common/malloc.h>
|
#include <avahi-common/malloc.h>
|
||||||
#include <avahi-common/error.h>
|
#include <avahi-common/error.h>
|
||||||
|
|
||||||
#include <avahi-glib/glib-watch.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
static constexpr Domain avahi_domain("avahi");
|
static constexpr Domain avahi_domain("avahi");
|
||||||
|
|
||||||
static char *avahiName;
|
static char *avahiName;
|
||||||
static int avahiRunning;
|
static int avahiRunning;
|
||||||
#ifndef USE_EPOLL
|
static MyAvahiPoll *avahi_poll;
|
||||||
static AvahiGLibPoll *avahi_glib_poll;
|
|
||||||
#endif
|
|
||||||
static const AvahiPoll *avahi_poll;
|
|
||||||
static AvahiClient *avahiClient;
|
static AvahiClient *avahiClient;
|
||||||
static AvahiEntryGroup *avahiGroup;
|
static AvahiEntryGroup *avahiGroup;
|
||||||
|
|
||||||
@ -246,15 +242,7 @@ AvahiInit(EventLoop &loop, const char *serviceName)
|
|||||||
|
|
||||||
avahiRunning = 1;
|
avahiRunning = 1;
|
||||||
|
|
||||||
#ifdef USE_EPOLL
|
avahi_poll = new MyAvahiPoll(loop);
|
||||||
// TODO
|
|
||||||
(void)loop;
|
|
||||||
if (1==1) return;
|
|
||||||
#else
|
|
||||||
avahi_glib_poll = avahi_glib_poll_new(loop.GetContext(),
|
|
||||||
G_PRIORITY_DEFAULT);
|
|
||||||
avahi_poll = avahi_glib_poll_get(avahi_glib_poll);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
int error;
|
int error;
|
||||||
avahiClient = avahi_client_new(avahi_poll, AVAHI_CLIENT_NO_FAIL,
|
avahiClient = avahi_client_new(avahi_poll, AVAHI_CLIENT_NO_FAIL,
|
||||||
@ -282,14 +270,8 @@ AvahiDeinit(void)
|
|||||||
avahiClient = NULL;
|
avahiClient = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef USE_EPOLL
|
delete avahi_poll;
|
||||||
// TODO
|
avahi_poll = nullptr;
|
||||||
#else
|
|
||||||
if (avahi_glib_poll != NULL) {
|
|
||||||
avahi_glib_poll_free(avahi_glib_poll);
|
|
||||||
avahi_glib_poll = NULL;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
avahi_free(avahiName);
|
avahi_free(avahiName);
|
||||||
avahiName = NULL;
|
avahiName = NULL;
|
||||||
|
Loading…
Reference in New Issue
Block a user