diff options
Diffstat (limited to 'src/zeroconf/avahi')
-rw-r--r-- | src/zeroconf/avahi/Init.cxx | 71 | ||||
-rw-r--r-- | src/zeroconf/avahi/Init.hxx | 31 | ||||
-rw-r--r-- | src/zeroconf/avahi/meson.build | 1 |
3 files changed, 103 insertions, 0 deletions
diff --git a/src/zeroconf/avahi/Init.cxx b/src/zeroconf/avahi/Init.cxx new file mode 100644 index 000000000..873ee8155 --- /dev/null +++ b/src/zeroconf/avahi/Init.cxx @@ -0,0 +1,71 @@ +/* + * 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 "Init.hxx" +#include "../Internal.hxx" +#include "Client.hxx" +#include "ConnectionListener.hxx" +#include "ErrorHandler.hxx" +#include "Publisher.hxx" +#include "Service.hxx" +#include "util/RuntimeError.hxx" +#include "Log.hxx" + +#include <avahi-common/domain.h> + +class AvahiGlue final : Avahi::ErrorHandler { +public: + Avahi::Client client; + Avahi::Publisher publisher; + + AvahiGlue(EventLoop &event_loop, + const char *name, std::forward_list<Avahi::Service> services) + :client(event_loop, *this), + publisher(client, name, std::move(services), *this) + { + } + + /* virtual methods from class Avahi::ErrorHandler */ + bool OnAvahiError(std::exception_ptr e) noexcept override { + LogError(e); + return true; + } +}; + +static AvahiGlue *avahi_glue; + +void +AvahiInit(EventLoop &loop, const char *serviceName, unsigned port) +{ + if (!avahi_is_valid_service_name(serviceName)) + throw FormatRuntimeError("Invalid zeroconf_name \"%s\"", serviceName); + + std::forward_list<Avahi::Service> services; + services.emplace_front(AVAHI_IF_UNSPEC, + AVAHI_PROTO_UNSPEC, + SERVICE_TYPE, port); + + avahi_glue = new AvahiGlue(loop, serviceName, std::move(services)); +} + +void +AvahiDeinit() +{ + delete avahi_glue; +} diff --git a/src/zeroconf/avahi/Init.hxx b/src/zeroconf/avahi/Init.hxx new file mode 100644 index 000000000..684098426 --- /dev/null +++ b/src/zeroconf/avahi/Init.hxx @@ -0,0 +1,31 @@ +/* + * 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_ZEROCONF_AVAHI_HXX +#define MPD_ZEROCONF_AVAHI_HXX + +class EventLoop; + +void +AvahiInit(EventLoop &loop, const char *service_name, unsigned port); + +void +AvahiDeinit(); + +#endif diff --git a/src/zeroconf/avahi/meson.build b/src/zeroconf/avahi/meson.build index d13b55201..e6427adf4 100644 --- a/src/zeroconf/avahi/meson.build +++ b/src/zeroconf/avahi/meson.build @@ -6,6 +6,7 @@ endif avahi = static_library( 'avahi', + 'Init.cxx', 'Client.cxx', 'Error.cxx', 'Poll.cxx', |