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.

Socket.hpp 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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/AsioWrapper.hpp>
  21. #include <functional>
  22. namespace ableton
  23. {
  24. namespace test
  25. {
  26. namespace serial_io
  27. {
  28. struct Socket
  29. {
  30. using SendFn = std::function<std::size_t(
  31. const uint8_t* const, const size_t, const asio::ip::udp::endpoint&)>;
  32. struct ReceiveHandler
  33. {
  34. template <typename It>
  35. void operator()(const asio::ip::udp::endpoint& from, const It begin, const It end)
  36. {
  37. std::vector<uint8_t> buffer{begin, end};
  38. mReceive(from, buffer);
  39. }
  40. std::function<const asio::ip::udp::endpoint&, const std::vector<uint8_t>&> mReceive;
  41. };
  42. using ReceiveFn = std::function<void(ReceiveHandler)>;
  43. Socket(SendFn sendFn, ReceiveFn receiveFn)
  44. : mSendFn(std::move(sendFn))
  45. , mReceiveFn(std::move(receiveFn))
  46. {
  47. }
  48. std::size_t send(
  49. const uint8_t* const pData, const size_t numBytes, const asio::ip::udp::endpoint& to)
  50. {
  51. return mSendFn(pData, numBytes, to);
  52. }
  53. template <typename Handler>
  54. void receive(Handler handler)
  55. {
  56. mReceiveFn(ReceiveHandler{
  57. [handler](const asio::ip::udp::endpoint& from, const std::vector<uint8_t>& buffer) {
  58. handler(from, begin(buffer), end(buffer));
  59. }});
  60. }
  61. asio::ip::udp::endpoint endpoint() const
  62. {
  63. return asio::ip::udp::endpoint({}, 0);
  64. }
  65. SendFn mSendFn;
  66. ReceiveFn mReceiveFn;
  67. };
  68. } // namespace test
  69. } // namespace platforms
  70. } // namespace ableton