xbmc
ZeroconfAvahi.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "network/Zeroconf.h"
12 
13 #include <map>
14 #include <memory>
15 #include <string>
16 #include <vector>
17 
18 #include <avahi-client/client.h>
19 #include <avahi-client/publish.h>
20 #include <avahi-common/defs.h>
21 
22 struct AvahiThreadedPoll;
23 
24 class CZeroconfAvahi : public CZeroconf
25 {
26 public:
28  ~CZeroconfAvahi() override;
29 
30 protected:
31  //implement base CZeroConf interface
32  bool doPublishService(const std::string& fcr_identifier,
33  const std::string& fcr_type,
34  const std::string& fcr_name,
35  unsigned int f_port,
36  const std::vector<std::pair<std::string, std::string> >& txt) override;
37 
38  bool doForceReAnnounceService(const std::string& fcr_identifier) override;
39  bool doRemoveService(const std::string& fcr_ident) override;
40 
41  void doStop() override;
42 
43 private:
45  static void clientCallback(AvahiClient* fp_client, AvahiClientState f_state, void*);
47  static void groupCallback(AvahiEntryGroup *fp_group, AvahiEntryGroupState f_state, void *);
48  //shutdown callback; works around a problem in avahi < 0.6.24 see destructor for details
49  static void shutdownCallback(AvahiTimeout *fp_e, void *);
50 
53  bool createClient();
54 
55  //don't access stuff below without stopping the client thread
56  //see http://avahi.org/wiki/RunningAvahiClientAsThread
57  //and use struct ScopedEventLoopBlock
58 
59  //helper struct for holding information about creating a service / AvahiEntryGroup
60  //we have to hold that as it's needed to recreate the service
61  struct ServiceInfo;
62  typedef std::map<std::string, std::shared_ptr<ServiceInfo> > tServiceMap;
63 
64  //goes through a list of todos and publishs them (takes the client a param, as it might be called from
65  // from the callbacks)
66  void updateServices(AvahiClient* fp_client);
67  //helper that actually does the work of publishing
68  void addService(const tServiceMap::mapped_type& fp_service_info, AvahiClient* fp_client);
69 
70  AvahiClient* mp_client = 0;
71  AvahiThreadedPoll* mp_poll = 0;
72 
73  //this holds all published and unpublished services including info on howto create them
74  tServiceMap m_services;
75 
76  //2 variables below are needed for workaround of avahi bug (see destructor for details)
77  bool m_shutdown = false;
78  pthread_t m_thread_id = 0;
79 };
helper to store information on howto create an avahi-group to publish
Definition: ZeroconfAvahi.cpp:44
CZeroconfAvahi()
Definition: ZeroconfAvahi.cpp:59
this class provides support for zeroconf while the different zeroconf implementations have asynchrono...
Definition: Zeroconf.h:28
~CZeroconfAvahi() override
Definition: ZeroconfAvahi.cpp:81
Definition: ZeroconfAvahi.h:24