The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
5.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. //==============================================================================
  20. /**
  21. Contains classes that implement a simple protocol for broadcasting the availability
  22. and location of a discoverable service on the local network, and for maintaining a
  23. list of known services.
  24. */
  25. struct NetworkServiceDiscovery
  26. {
  27. /** An object which runs a thread to repeatedly broadcast the existence of a
  28. discoverable service.
  29. To use, simply create an instance of an Advertiser and it'll broadcast until
  30. you delete it.
  31. */
  32. struct Advertiser : private Thread
  33. {
  34. /** Creates and starts an Advertiser thread, broadcasting with the given properties.
  35. @param serviceTypeUID A user-supplied string to define the type of service this represents
  36. @param serviceDescription A description string that will appear in the Service::description field for clients
  37. @param broadcastPort The port number on which to broadcast the service discovery packets
  38. @param connectionPort The port number that will be sent to appear in the Service::port field
  39. @param minTimeBetweenBroadcasts The interval to wait between sending broadcast messages
  40. */
  41. Advertiser (const String& serviceTypeUID,
  42. const String& serviceDescription,
  43. int broadcastPort,
  44. int connectionPort,
  45. RelativeTime minTimeBetweenBroadcasts = RelativeTime::seconds (1.5));
  46. /** Destructor */
  47. ~Advertiser();
  48. private:
  49. XmlElement message;
  50. const int broadcastPort;
  51. const RelativeTime minInterval;
  52. DatagramSocket socket { true };
  53. void run() override;
  54. void sendBroadcast();
  55. };
  56. //==============================================================================
  57. /**
  58. Contains information about a service that has been found on the network.
  59. @see AvailableServiceList, Advertiser
  60. */
  61. struct Service
  62. {
  63. String instanceID; /**< A UUID that identifies the particular instance of the Advertiser class. */
  64. String description; /**< The service description as sent by the Advertiser */
  65. IPAddress address; /**< The IP address of the advertiser */
  66. int port; /**< The port number of the advertiser */
  67. Time lastSeen; /**< The time of the last ping received from the advertiser */
  68. };
  69. //==============================================================================
  70. /**
  71. Watches the network for broadcasts from Advertiser objects, and keeps a list of
  72. all the currently active instances.
  73. Just create an instance of AvailableServiceList and it will start listening - you
  74. can register a callback with its onChange member to find out when services
  75. appear/disappear, and you can call getServices() to find out the current list.
  76. @see Service, Advertiser
  77. */
  78. struct AvailableServiceList : private Thread,
  79. private AsyncUpdater
  80. {
  81. /** Creates an AvailableServiceList that will bind to the given port number and watch
  82. the network for Advertisers broadcasting the given service type.
  83. This will only detect broadcasts from an Advertiser object with a matching
  84. serviceTypeUID value, and where the broadcastPort matches.
  85. */
  86. AvailableServiceList (const String& serviceTypeUID, int broadcastPort);
  87. /** Destructor */
  88. ~AvailableServiceList();
  89. /** A lambda that can be set to recieve a callback when the list changes */
  90. std::function<void()> onChange;
  91. /** Returns a list of the currently known services. */
  92. std::vector<Service> getServices() const;
  93. private:
  94. DatagramSocket socket { true };
  95. String serviceTypeUID;
  96. CriticalSection listLock;
  97. std::vector<Service> services;
  98. void run() override;
  99. void handleAsyncUpdate() override;
  100. void handleMessage (const XmlElement&);
  101. void handleMessage (const Service&);
  102. void removeTimedOutServices();
  103. };
  104. };
  105. } // namespace juce