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.

InterfaceScanner.hpp 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Copyright 2016, Ableton AG, Berlin. All rights reserved.
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * If you would like to incorporate Link into a proprietary software application,
  17. * please contact <link-devs@ableton.com>.
  18. */
  19. #pragma once
  20. #include <ableton/platforms/asio/AsioWrapper.hpp>
  21. #include <ableton/util/Injected.hpp>
  22. #include <chrono>
  23. #include <vector>
  24. namespace ableton
  25. {
  26. namespace discovery
  27. {
  28. // Callback takes a range of asio::ip:address which is
  29. // guaranteed to be sorted and unique
  30. template <typename Callback, typename IoContext>
  31. class InterfaceScanner
  32. {
  33. public:
  34. using Timer = typename util::Injected<IoContext>::type::Timer;
  35. InterfaceScanner(const std::chrono::seconds period,
  36. util::Injected<Callback> callback,
  37. util::Injected<IoContext> io)
  38. : mPeriod(period)
  39. , mCallback(std::move(callback))
  40. , mIo(std::move(io))
  41. , mTimer(mIo->makeTimer())
  42. {
  43. }
  44. void enable(const bool bEnable)
  45. {
  46. if (bEnable)
  47. {
  48. scan();
  49. }
  50. else
  51. {
  52. mTimer.cancel();
  53. }
  54. }
  55. void scan()
  56. {
  57. using namespace std;
  58. debug(mIo->log()) << "Scanning network interfaces";
  59. // Rescan the hardware for available network interface addresses
  60. vector<asio::ip::address> addrs = mIo->scanNetworkInterfaces();
  61. // Sort and unique them to guarantee consistent comparison
  62. sort(begin(addrs), end(addrs));
  63. addrs.erase(unique(begin(addrs), end(addrs)), end(addrs));
  64. // Pass them to the callback
  65. (*mCallback)(std::move(addrs));
  66. // setup the next scanning
  67. mTimer.expires_from_now(mPeriod);
  68. using ErrorCode = typename Timer::ErrorCode;
  69. mTimer.async_wait([this](const ErrorCode e) {
  70. if (!e)
  71. {
  72. scan();
  73. }
  74. });
  75. }
  76. private:
  77. const std::chrono::seconds mPeriod;
  78. util::Injected<Callback> mCallback;
  79. util::Injected<IoContext> mIo;
  80. Timer mTimer;
  81. };
  82. // Factory function
  83. template <typename Callback, typename IoContext>
  84. InterfaceScanner<Callback, IoContext> makeInterfaceScanner(
  85. const std::chrono::seconds period,
  86. util::Injected<Callback> callback,
  87. util::Injected<IoContext> io)
  88. {
  89. using namespace std;
  90. return {period, move(callback), move(io)};
  91. }
  92. } // namespace discovery
  93. } // namespace ableton