Socket.hpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 <ableton/util/test/IoService.hpp>
  23. namespace ableton
  24. {
  25. namespace discovery
  26. {
  27. namespace test
  28. {
  29. class Socket
  30. {
  31. public:
  32. Socket(util::test::IoService&) {}
  33. friend void configureUnicastSocket(Socket&, const IpAddressV4&) {}
  34. std::size_t send(const uint8_t* const pData,
  35. const size_t numBytes,
  36. const discovery::UdpEndpoint& to)
  37. {
  38. sentMessages.push_back(
  39. std::make_pair(std::vector<uint8_t>{pData, pData + numBytes}, to));
  40. return numBytes;
  41. }
  42. template <typename Handler>
  43. void receive(Handler handler)
  44. {
  45. mCallback = [handler](const UdpEndpoint& from, const std::vector<uint8_t>& buffer)
  46. { handler(from, begin(buffer), end(buffer)); };
  47. }
  48. template <typename It>
  49. void incomingMessage(const UdpEndpoint& from, It messageBegin, It messageEnd)
  50. {
  51. std::vector<uint8_t> buffer{messageBegin, messageEnd};
  52. mCallback(from, buffer);
  53. }
  54. UdpEndpoint endpoint() const { return UdpEndpoint({}, 0); }
  55. using SentMessage = std::pair<std::vector<uint8_t>, UdpEndpoint>;
  56. std::vector<SentMessage> sentMessages;
  57. private:
  58. using ReceiveCallback =
  59. std::function<void(const UdpEndpoint&, const std::vector<uint8_t>&)>;
  60. ReceiveCallback mCallback;
  61. };
  62. } // namespace test
  63. } // namespace discovery
  64. } // namespace ableton