AvahiPoll.cxx 4.13 KB
Newer Older
1
/*
2
 * Copyright 2003-2018 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 * 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/SocketMonitor.hxx"
23
#include "event/TimerEvent.hxx"
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

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:
51
	AvahiWatch(SocketDescriptor _fd, AvahiWatchEvent _event,
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
		   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;
	}

72 73 74
private:
	/* virtual methods from class SocketMonitor */
	bool OnSocketReady(unsigned flags) noexcept {
75
		received = ToAvahiWatchEvent(flags);
76
		callback(this, GetSocket().Get(), received, userdata);
77 78 79 80 81
		received = AvahiWatchEvent(0);
		return true;
	}
};

82 83
static constexpr std::chrono::steady_clock::duration
TimevalToChrono(const timeval &tv)
84
{
85
	return std::chrono::seconds(tv.tv_sec) + std::chrono::microseconds(tv.tv_usec);
86 87
}

88 89 90
struct AvahiTimeout final {
	TimerEvent timer;

91 92 93 94 95 96 97
	const AvahiTimeoutCallback callback;
	void *const userdata;

public:
	AvahiTimeout(const struct timeval *tv,
		     AvahiTimeoutCallback _callback, void *_userdata,
		     EventLoop &_loop)
98
		:timer(_loop, BIND_THIS_METHOD(OnTimeout)),
99 100
		 callback(_callback), userdata(_userdata) {
		if (tv != nullptr)
101
			timer.Schedule(TimevalToChrono(*tv));
102 103 104 105
	}

	static void TimeoutUpdate(AvahiTimeout *t, const struct timeval *tv) {
		if (tv != nullptr)
106
			t->timer.Schedule(TimevalToChrono(*tv));
107
		else
108
			t->timer.Cancel();
109 110 111 112 113 114
	}

	static void TimeoutFree(AvahiTimeout *t) {
		delete t;
	}

115 116
private:
	void OnTimeout() {
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
		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;

137
	return new AvahiWatch(SocketDescriptor(fd), event, callback, userdata,
138 139 140 141 142 143 144 145 146 147 148
			      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);
}