Audio plugin host https://kx.studio/carla
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.

139 lines
5.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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. @tags{Events}
  25. */
  26. struct NetworkServiceDiscovery
  27. {
  28. /** An object which runs a thread to repeatedly broadcast the existence of a
  29. discoverable service.
  30. To use, simply create an instance of an Advertiser and it'll broadcast until
  31. you delete it.
  32. @tags{Events}
  33. */
  34. struct Advertiser : private Thread
  35. {
  36. /** Creates and starts an Advertiser thread, broadcasting with the given properties.
  37. @param serviceTypeUID A user-supplied string to define the type of service this represents
  38. @param serviceDescription A description string that will appear in the Service::description field for clients
  39. @param broadcastPort The port number on which to broadcast the service discovery packets
  40. @param connectionPort The port number that will be sent to appear in the Service::port field
  41. @param minTimeBetweenBroadcasts The interval to wait between sending broadcast messages
  42. */
  43. Advertiser (const String& serviceTypeUID,
  44. const String& serviceDescription,
  45. int broadcastPort,
  46. int connectionPort,
  47. RelativeTime minTimeBetweenBroadcasts = RelativeTime::seconds (1.5));
  48. /** Destructor */
  49. ~Advertiser() override;
  50. private:
  51. XmlElement message;
  52. const int broadcastPort;
  53. const RelativeTime minInterval;
  54. DatagramSocket socket { true };
  55. void run() override;
  56. void sendBroadcast();
  57. };
  58. //==============================================================================
  59. /**
  60. Contains information about a service that has been found on the network.
  61. @see AvailableServiceList, Advertiser
  62. @tags{Events}
  63. */
  64. struct Service
  65. {
  66. String instanceID; /**< A UUID that identifies the particular instance of the Advertiser class. */
  67. String description; /**< The service description as sent by the Advertiser */
  68. IPAddress address; /**< The IP address of the advertiser */
  69. int port; /**< The port number of the advertiser */
  70. Time lastSeen; /**< The time of the last ping received from the advertiser */
  71. };
  72. //==============================================================================
  73. /**
  74. Watches the network for broadcasts from Advertiser objects, and keeps a list of
  75. all the currently active instances.
  76. Just create an instance of AvailableServiceList and it will start listening - you
  77. can register a callback with its onChange member to find out when services
  78. appear/disappear, and you can call getServices() to find out the current list.
  79. @see Service, Advertiser
  80. @tags{Events}
  81. */
  82. struct AvailableServiceList : private Thread,
  83. private AsyncUpdater
  84. {
  85. /** Creates an AvailableServiceList that will bind to the given port number and watch
  86. the network for Advertisers broadcasting the given service type.
  87. This will only detect broadcasts from an Advertiser object with a matching
  88. serviceTypeUID value, and where the broadcastPort matches.
  89. */
  90. AvailableServiceList (const String& serviceTypeUID, int broadcastPort);
  91. /** Destructor */
  92. ~AvailableServiceList() override;
  93. /** A lambda that can be set to receive a callback when the list changes */
  94. std::function<void()> onChange;
  95. /** Returns a list of the currently known services. */
  96. std::vector<Service> getServices() const;
  97. private:
  98. DatagramSocket socket { true };
  99. String serviceTypeUID;
  100. CriticalSection listLock;
  101. std::vector<Service> services;
  102. void run() override;
  103. void handleAsyncUpdate() override;
  104. void handleMessage (const XmlElement&);
  105. void handleMessage (const Service&);
  106. void removeTimedOutServices();
  107. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AvailableServiceList)
  108. };
  109. };
  110. } // namespace juce