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 
20 #pragma once
21 
22 #include <ableton/platforms/asio/AsioService.hpp>
23 #include <ableton/util/Injected.hpp>
24 
25 namespace ableton
26 {
27 namespace discovery
28 {
29 
multicastEndpoint()30 inline asio::ip::udp::endpoint multicastEndpoint()
31 {
32   return {asio::ip::address::from_string("224.76.78.75"), 20808};
33 }
34 
35 // Type tags for dispatching between unicast and multicast packets
36 struct MulticastTag
37 {
38 };
39 struct UnicastTag
40 {
41 };
42 
43 template <typename IoContext, std::size_t MaxPacketSize>
44 class IpV4Interface
45 {
46 public:
47   using Socket = typename util::Injected<IoContext>::type::template Socket<MaxPacketSize>;
48 
IpV4Interface(util::Injected<IoContext> io,const asio::ip::address_v4 & addr)49   IpV4Interface(util::Injected<IoContext> io, const asio::ip::address_v4& addr)
50     : mIo(std::move(io))
51     , mMulticastReceiveSocket(mIo->template openMulticastSocket<MaxPacketSize>(addr))
52     , mSendSocket(mIo->template openUnicastSocket<MaxPacketSize>(addr))
53   {
54   }
55 
56   IpV4Interface(const IpV4Interface&) = delete;
57   IpV4Interface& operator=(const IpV4Interface&) = delete;
58 
IpV4Interface(IpV4Interface && rhs)59   IpV4Interface(IpV4Interface&& rhs)
60     : mIo(std::move(rhs.mIo))
61     , mMulticastReceiveSocket(std::move(rhs.mMulticastReceiveSocket))
62     , mSendSocket(std::move(rhs.mSendSocket))
63   {
64   }
65 
66 
send(const uint8_t * const pData,const size_t numBytes,const asio::ip::udp::endpoint & to)67   std::size_t send(
68     const uint8_t* const pData, const size_t numBytes, const asio::ip::udp::endpoint& to)
69   {
70     return mSendSocket.send(pData, numBytes, to);
71   }
72 
73   template <typename Handler>
receive(Handler handler,UnicastTag)74   void receive(Handler handler, UnicastTag)
75   {
76     mSendSocket.receive(SocketReceiver<UnicastTag, Handler>{std::move(handler)});
77   }
78 
79   template <typename Handler>
receive(Handler handler,MulticastTag)80   void receive(Handler handler, MulticastTag)
81   {
82     mMulticastReceiveSocket.receive(
83       SocketReceiver<MulticastTag, Handler>(std::move(handler)));
84   }
85 
endpoint() const86   asio::ip::udp::endpoint endpoint() const
87   {
88     return mSendSocket.endpoint();
89   }
90 
91 private:
92   template <typename Tag, typename Handler>
93   struct SocketReceiver
94   {
SocketReceiverableton::discovery::IpV4Interface::SocketReceiver95     SocketReceiver(Handler handler)
96       : mHandler(std::move(handler))
97     {
98     }
99 
100     template <typename It>
operator ()ableton::discovery::IpV4Interface::SocketReceiver101     void operator()(
102       const asio::ip::udp::endpoint& from, const It messageBegin, const It messageEnd)
103     {
104       mHandler(Tag{}, from, messageBegin, messageEnd);
105     }
106 
107     Handler mHandler;
108   };
109 
110   util::Injected<IoContext> mIo;
111   Socket mMulticastReceiveSocket;
112   Socket mSendSocket;
113 };
114 
115 template <std::size_t MaxPacketSize, typename IoContext>
makeIpV4Interface(util::Injected<IoContext> io,const asio::ip::address_v4 & addr)116 IpV4Interface<IoContext, MaxPacketSize> makeIpV4Interface(
117   util::Injected<IoContext> io, const asio::ip::address_v4& addr)
118 {
119   return {std::move(io), addr};
120 }
121 
122 } // namespace discovery
123 } // namespace ableton
124