Interface.hpp 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/discovery/AsioTypes.hpp>
  21. #include <ableton/util/Log.hpp>
  22. #include <functional>
  23. #include <utility>
  24. #include <vector>
  25. namespace ableton
  26. {
  27. namespace discovery
  28. {
  29. namespace test
  30. {
  31. class Interface
  32. {
  33. public:
  34. Interface() = default;
  35. Interface(UdpEndpoint endpoint)
  36. : mEndpoint(std::move(endpoint))
  37. {
  38. }
  39. void send(const uint8_t* const bytes,
  40. const size_t numBytes,
  41. const UdpEndpoint& endpoint)
  42. {
  43. sentMessages.push_back(
  44. std::make_pair(std::vector<uint8_t>{bytes, bytes + numBytes}, endpoint));
  45. }
  46. template <typename Callback, typename Tag>
  47. void receive(Callback callback, Tag tag)
  48. {
  49. mCallback = [callback, tag](
  50. const UdpEndpoint& from, const std::vector<uint8_t>& buffer)
  51. { callback(tag, from, begin(buffer), end(buffer)); };
  52. }
  53. template <typename Callback>
  54. void receive(Callback callback)
  55. {
  56. mCallback = [callback](const auto& from, const auto& buffer)
  57. { callback(from, begin(buffer), end(buffer)); };
  58. }
  59. template <typename It>
  60. void incomingMessage(const UdpEndpoint& from, It messageBegin, It messageEnd)
  61. {
  62. std::vector<uint8_t> buffer{messageBegin, messageEnd};
  63. mCallback(from, buffer);
  64. }
  65. UdpEndpoint endpoint() const { return mEndpoint; }
  66. using SentMessage = std::pair<std::vector<uint8_t>, UdpEndpoint>;
  67. std::vector<SentMessage> sentMessages;
  68. private:
  69. using ReceiveCallback =
  70. std::function<void(const UdpEndpoint&, const std::vector<uint8_t>&)>;
  71. ReceiveCallback mCallback;
  72. UdpEndpoint mEndpoint;
  73. };
  74. } // namespace test
  75. } // namespace discovery
  76. } // namespace ableton