xbmc
ZeroconfBrowserAvahi.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/ZeroconfBrowser.h"
12 #include "threads/CriticalSection.h"
13 #include "threads/Thread.h"
14 
15 #include <map>
16 #include <memory>
17 #include <vector>
18 
19 #include <avahi-client/client.h>
20 #include <avahi-client/lookup.h>
21 #include <avahi-common/defs.h>
22 #include <avahi-common/thread-watch.h>
23 
24 //platform specific implementation of zeroconfbrowser interface using avahi
26 {
27  public:
29  ~CZeroconfBrowserAvahi() override;
30 
31  private:
34  bool doAddServiceType(const std::string& fcr_service_type) override;
35  bool doRemoveServiceType(const std::string& fcr_service_type) override;
36 
37  std::vector<CZeroconfBrowser::ZeroconfService> doGetFoundServices() override;
38  bool doResolveService(CZeroconfBrowser::ZeroconfService& fr_service, double f_timeout) override;
40 
43  static void clientCallback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata);
44  static void browseCallback(
45  AvahiServiceBrowser *b,
46  AvahiIfIndex interface,
47  AvahiProtocol protocol,
48  AvahiBrowserEvent event,
49  const char *name,
50  const char *type,
51  const char *domain,
52  AVAHI_GCC_UNUSED AvahiLookupResultFlags flags,
53  void* userdata);
54  static void resolveCallback(
55  AvahiServiceResolver *r,
56  AvahiIfIndex interface,
57  AvahiProtocol protocol,
58  AvahiResolverEvent event,
59  const char *name,
60  const char *type,
61  const char *domain,
62  const char *host_name,
63  const AvahiAddress *address,
64  uint16_t port,
65  AvahiStringList *txt,
66  AvahiLookupResultFlags flags,
67  AVAHI_GCC_UNUSED void* userdata);
68  //helpers
69  bool createClient();
70  static AvahiServiceBrowser* createServiceBrowser(const std::string& fcr_service_type, AvahiClient* fp_client, void* fp_userdata);
71 
72  //shared variables between avahi thread and interface
73  AvahiClient* mp_client = 0 ;
74  AvahiThreadedPoll* mp_poll = 0 ;
75  // tBrowserMap maps service types the corresponding browser
76  typedef std::map<std::string, AvahiServiceBrowser*> tBrowserMap;
77  tBrowserMap m_browsers;
78 
79  // if a browser is in this set, it already sent an ALL_FOR_NOW message
80  // (needed to bundle GUI_MSG_UPDATE_PATH messages
81  std::set<AvahiServiceBrowser*> m_all_for_now_browsers;
82 
83  //this information is needed for avahi to resolve a service,
84  //so unfortunately we'll only be able to resolve services already discovered
85  struct AvahiSpecificInfos
86  {
87  AvahiIfIndex interface;
88  AvahiProtocol protocol;
89  };
90  typedef std::map<CZeroconfBrowser::ZeroconfService, AvahiSpecificInfos> tDiscoveredServices;
91  tDiscoveredServices m_discovered_services;
92  CZeroconfBrowser::ZeroconfService m_resolving_service;
93  CEvent m_resolved_event;
94 };
This is an Event class built from a ConditionVariable.
Definition: Event.h:35
Definition: ZeroconfBrowserAvahi.h:25
CZeroconfBrowserAvahi()
Definition: ZeroconfBrowserAvahi.cpp:39
Definition: ZeroconfBrowser.h:27
this class provides support for zeroconf browsing
Definition: ZeroconfBrowser.h:24