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.

juce_NetworkServiceDiscovery.cpp 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #if JUCE_ANDROID
  20. extern void acquireMulticastLock();
  21. extern void releaseMulticastLock();
  22. #endif
  23. NetworkServiceDiscovery::Advertiser::Advertiser (const String& serviceTypeUID,
  24. const String& serviceDescription,
  25. int broadcastPortToUse, int connectionPort,
  26. RelativeTime minTimeBetweenBroadcasts)
  27. : Thread ("Discovery_broadcast"),
  28. message (serviceTypeUID), broadcastPort (broadcastPortToUse),
  29. minInterval (minTimeBetweenBroadcasts)
  30. {
  31. message.setAttribute ("id", Uuid().toString());
  32. message.setAttribute ("name", serviceDescription);
  33. message.setAttribute ("address", String());
  34. message.setAttribute ("port", connectionPort);
  35. startThread (2);
  36. }
  37. NetworkServiceDiscovery::Advertiser::~Advertiser()
  38. {
  39. stopThread (2000);
  40. socket.shutdown();
  41. }
  42. void NetworkServiceDiscovery::Advertiser::run()
  43. {
  44. if (! socket.bindToPort (0))
  45. {
  46. jassertfalse;
  47. return;
  48. }
  49. while (! threadShouldExit())
  50. {
  51. sendBroadcast();
  52. wait ((int) minInterval.inMilliseconds());
  53. }
  54. }
  55. void NetworkServiceDiscovery::Advertiser::sendBroadcast()
  56. {
  57. static IPAddress local = IPAddress::local();
  58. for (auto& address : IPAddress::getAllAddresses())
  59. {
  60. if (address == local)
  61. continue;
  62. message.setAttribute ("address", address.toString());
  63. auto broadcastAddress = IPAddress::getInterfaceBroadcastAddress (address);
  64. auto data = message.toString (XmlElement::TextFormat().singleLine().withoutHeader());
  65. socket.write (broadcastAddress.toString(), broadcastPort, data.toRawUTF8(), (int) data.getNumBytesAsUTF8());
  66. }
  67. }
  68. //==============================================================================
  69. NetworkServiceDiscovery::AvailableServiceList::AvailableServiceList (const String& serviceType, int broadcastPort)
  70. : Thread ("Discovery_listen"), serviceTypeUID (serviceType)
  71. {
  72. #if JUCE_ANDROID
  73. acquireMulticastLock();
  74. #endif
  75. socket.bindToPort (broadcastPort);
  76. startThread (2);
  77. }
  78. NetworkServiceDiscovery::AvailableServiceList::~AvailableServiceList()
  79. {
  80. socket.shutdown();
  81. stopThread (2000);
  82. #if JUCE_ANDROID
  83. releaseMulticastLock();
  84. #endif
  85. }
  86. void NetworkServiceDiscovery::AvailableServiceList::run()
  87. {
  88. while (! threadShouldExit())
  89. {
  90. if (socket.waitUntilReady (true, 200) == 1)
  91. {
  92. char buffer[1024];
  93. auto bytesRead = socket.read (buffer, sizeof (buffer) - 1, false);
  94. if (bytesRead > 10)
  95. if (auto xml = parseXML (String (CharPointer_UTF8 (buffer),
  96. CharPointer_UTF8 (buffer + bytesRead))))
  97. if (xml->hasTagName (serviceTypeUID))
  98. handleMessage (*xml);
  99. }
  100. removeTimedOutServices();
  101. }
  102. }
  103. std::vector<NetworkServiceDiscovery::Service> NetworkServiceDiscovery::AvailableServiceList::getServices() const
  104. {
  105. const ScopedLock sl (listLock);
  106. auto listCopy = services;
  107. return listCopy;
  108. }
  109. void NetworkServiceDiscovery::AvailableServiceList::handleAsyncUpdate()
  110. {
  111. if (onChange != nullptr)
  112. onChange();
  113. }
  114. void NetworkServiceDiscovery::AvailableServiceList::handleMessage (const XmlElement& xml)
  115. {
  116. Service service;
  117. service.instanceID = xml.getStringAttribute ("id");
  118. if (service.instanceID.trim().isNotEmpty())
  119. {
  120. service.description = xml.getStringAttribute ("name");
  121. service.address = IPAddress (xml.getStringAttribute ("address"));
  122. service.port = xml.getIntAttribute ("port");
  123. service.lastSeen = Time::getCurrentTime();
  124. handleMessage (service);
  125. }
  126. }
  127. static void sortServiceList (std::vector<NetworkServiceDiscovery::Service>& services)
  128. {
  129. auto compareServices = [] (const NetworkServiceDiscovery::Service& s1,
  130. const NetworkServiceDiscovery::Service& s2)
  131. {
  132. return s1.instanceID < s2.instanceID;
  133. };
  134. std::sort (services.begin(), services.end(), compareServices);
  135. }
  136. void NetworkServiceDiscovery::AvailableServiceList::handleMessage (const Service& service)
  137. {
  138. const ScopedLock sl (listLock);
  139. for (auto& s : services)
  140. {
  141. if (s.instanceID == service.instanceID)
  142. {
  143. if (s.description != service.description
  144. || s.address != service.address
  145. || s.port != service.port)
  146. {
  147. s = service;
  148. triggerAsyncUpdate();
  149. }
  150. s.lastSeen = service.lastSeen;
  151. return;
  152. }
  153. }
  154. services.push_back (service);
  155. sortServiceList (services);
  156. triggerAsyncUpdate();
  157. }
  158. void NetworkServiceDiscovery::AvailableServiceList::removeTimedOutServices()
  159. {
  160. const double timeoutSeconds = 5.0;
  161. auto oldestAllowedTime = Time::getCurrentTime() - RelativeTime::seconds (timeoutSeconds);
  162. const ScopedLock sl (listLock);
  163. auto oldEnd = std::end (services);
  164. auto newEnd = std::remove_if (std::begin (services), oldEnd,
  165. [=] (const Service& s) { return s.lastSeen < oldestAllowedTime; });
  166. if (newEnd != oldEnd)
  167. {
  168. services.erase (newEnd, oldEnd);
  169. triggerAsyncUpdate();
  170. }
  171. }
  172. } // namespace juce