4
0

IpInterface.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/Injected.hpp>
  22. namespace ableton
  23. {
  24. namespace discovery
  25. {
  26. inline UdpEndpoint multicastEndpointV4()
  27. {
  28. return {makeAddress("224.76.78.75"), 20808};
  29. }
  30. inline UdpEndpoint multicastEndpointV6(uint64_t scopeId)
  31. {
  32. // This is a non-permanently-assigned link-local multicast address (RFC4291)
  33. return {makeAddress("ff12::8080%" + std::to_string(scopeId)), 20808};
  34. }
  35. // Type tags for dispatching between unicast and multicast packets
  36. struct MulticastTag
  37. {
  38. };
  39. struct UnicastTag
  40. {
  41. };
  42. template <typename IoContext, std::size_t MaxPacketSize>
  43. class IpInterface
  44. {
  45. public:
  46. using Socket = typename util::Injected<IoContext>::type::template Socket<MaxPacketSize>;
  47. IpInterface(util::Injected<IoContext> io, const IpAddress& addr)
  48. : mIo(std::move(io))
  49. , mMulticastReceiveSocket(mIo->template openMulticastSocket<MaxPacketSize>(addr))
  50. , mSendSocket(mIo->template openUnicastSocket<MaxPacketSize>(addr))
  51. {
  52. }
  53. IpInterface(const IpInterface&) = delete;
  54. IpInterface& operator=(const IpInterface&) = delete;
  55. IpInterface(IpInterface&& rhs)
  56. : mIo(std::move(rhs.mIo))
  57. , mMulticastReceiveSocket(std::move(rhs.mMulticastReceiveSocket))
  58. , mSendSocket(std::move(rhs.mSendSocket))
  59. {
  60. }
  61. std::size_t send(const uint8_t* const pData,
  62. const size_t numBytes,
  63. const UdpEndpoint& to)
  64. {
  65. return mSendSocket.send(pData, numBytes, to);
  66. }
  67. template <typename Handler>
  68. void receive(Handler handler, UnicastTag)
  69. {
  70. mSendSocket.receive(SocketReceiver<UnicastTag, Handler>{std::move(handler)});
  71. }
  72. template <typename Handler>
  73. void receive(Handler handler, MulticastTag)
  74. {
  75. mMulticastReceiveSocket.receive(
  76. SocketReceiver<MulticastTag, Handler>(std::move(handler)));
  77. }
  78. UdpEndpoint endpoint() const { return mSendSocket.endpoint(); }
  79. private:
  80. template <typename Tag, typename Handler>
  81. struct SocketReceiver
  82. {
  83. SocketReceiver(Handler handler)
  84. : mHandler(std::move(handler))
  85. {
  86. }
  87. template <typename It>
  88. void operator()(const UdpEndpoint& from, const It messageBegin, const It messageEnd)
  89. {
  90. mHandler(Tag{}, from, messageBegin, messageEnd);
  91. }
  92. Handler mHandler;
  93. };
  94. util::Injected<IoContext> mIo;
  95. Socket mMulticastReceiveSocket;
  96. Socket mSendSocket;
  97. };
  98. template <std::size_t MaxPacketSize, typename IoContext>
  99. IpInterface<IoContext, MaxPacketSize> makeIpInterface(util::Injected<IoContext> io,
  100. const IpAddress& addr)
  101. {
  102. return {std::move(io), addr};
  103. }
  104. } // namespace discovery
  105. } // namespace ableton