zeroconf/AvahiPoll: move to lib/avahi/

This commit is contained in:
Max Kellermann
2021-02-22 13:50:02 +01:00
parent 00a1731085
commit c0b9339d31
6 changed files with 32 additions and 14 deletions

139
src/zeroconf/avahi/Poll.cxx Normal file
View File

@@ -0,0 +1,139 @@
/*
* Copyright 2003-2021 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 "Poll.hxx"
#include "event/SocketEvent.hxx"
#include "event/CoarseTimerEvent.hxx"
#include "time/Convert.hxx"
static unsigned
FromAvahiWatchEvent(AvahiWatchEvent e)
{
return (e & AVAHI_WATCH_IN ? SocketEvent::READ : 0) |
(e & AVAHI_WATCH_OUT ? SocketEvent::WRITE : 0);
}
static AvahiWatchEvent
ToAvahiWatchEvent(unsigned e)
{
return AvahiWatchEvent((e & SocketEvent::READ ? AVAHI_WATCH_IN : 0) |
(e & SocketEvent::WRITE ? AVAHI_WATCH_OUT : 0) |
(e & SocketEvent::ERROR ? AVAHI_WATCH_ERR : 0) |
(e & SocketEvent::HANGUP ? AVAHI_WATCH_HUP : 0));
}
struct AvahiWatch final {
SocketEvent event;
const AvahiWatchCallback callback;
void *const userdata;
AvahiWatchEvent received;
public:
AvahiWatch(SocketDescriptor _fd, AvahiWatchEvent _event,
AvahiWatchCallback _callback, void *_userdata,
EventLoop &_loop)
:event(_loop, BIND_THIS_METHOD(OnSocketReady), _fd),
callback(_callback), userdata(_userdata),
received(AvahiWatchEvent(0)) {
event.Schedule(FromAvahiWatchEvent(_event));
}
static void WatchUpdate(AvahiWatch *w, AvahiWatchEvent event) {
w->event.Schedule(FromAvahiWatchEvent(event));
}
static AvahiWatchEvent WatchGetEvents(AvahiWatch *w) {
return w->received;
}
static void WatchFree(AvahiWatch *w) {
delete w;
}
private:
void OnSocketReady(unsigned flags) noexcept {
received = ToAvahiWatchEvent(flags);
callback(this, event.GetSocket().Get(), received, userdata);
received = AvahiWatchEvent(0);
}
};
struct AvahiTimeout final {
CoarseTimerEvent timer;
const AvahiTimeoutCallback callback;
void *const userdata;
public:
AvahiTimeout(const struct timeval *tv,
AvahiTimeoutCallback _callback, void *_userdata,
EventLoop &_loop)
:timer(_loop, BIND_THIS_METHOD(OnTimeout)),
callback(_callback), userdata(_userdata) {
if (tv != nullptr)
timer.Schedule(ToSteadyClockDuration(*tv));
}
static void TimeoutUpdate(AvahiTimeout *t, const struct timeval *tv) {
if (tv != nullptr)
t->timer.Schedule(ToSteadyClockDuration(*tv));
else
t->timer.Cancel();
}
static void TimeoutFree(AvahiTimeout *t) {
delete t;
}
private:
void OnTimeout() noexcept {
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(SocketDescriptor(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);
}

View File

@@ -0,0 +1,45 @@
/*
* Copyright 2003-2021 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 <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

View File

@@ -0,0 +1,21 @@
libavahi_client = dependency('avahi-client', required: get_option('zeroconf') == 'avahi')
if not libavahi_client.found()
avahi_dep = dependency('', required: false)
subdir_done()
endif
avahi = static_library(
'avahi',
'Poll.cxx',
include_directories: inc,
dependencies: [
libavahi_client,
],
)
avahi_dep = declare_dependency(
link_with: avahi,
dependencies: [
event_dep,
],
)