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/discovery/NetworkByteStreamSerializable.hpp> 23 #include <ableton/platforms/asio/AsioWrapper.hpp> 24 25 namespace ableton 26 { 27 namespace link 28 { 29 30 struct MeasurementEndpointV4 31 { 32 static const std::int32_t key = 'mep4'; 33 static_assert(key == 0x6d657034, "Unexpected byte order"); 34 35 // Model the NetworkByteStreamSerializable concept sizeInByteStream(const MeasurementEndpointV4 mep)36 friend std::uint32_t sizeInByteStream(const MeasurementEndpointV4 mep) 37 { 38 return discovery::sizeInByteStream( 39 static_cast<std::uint32_t>(mep.ep.address().to_v4().to_ulong())) 40 + discovery::sizeInByteStream(mep.ep.port()); 41 } 42 43 template <typename It> toNetworkByteStream(const MeasurementEndpointV4 mep,It out)44 friend It toNetworkByteStream(const MeasurementEndpointV4 mep, It out) 45 { 46 return discovery::toNetworkByteStream(mep.ep.port(), 47 discovery::toNetworkByteStream( 48 static_cast<std::uint32_t>(mep.ep.address().to_v4().to_ulong()), std::move(out))); 49 } 50 51 template <typename It> fromNetworkByteStreamableton::link::MeasurementEndpointV452 static std::pair<MeasurementEndpointV4, It> fromNetworkByteStream(It begin, It end) 53 { 54 using namespace std; 55 auto addrRes = 56 discovery::Deserialize<std::uint32_t>::fromNetworkByteStream(move(begin), end); 57 auto portRes = discovery::Deserialize<std::uint16_t>::fromNetworkByteStream( 58 move(addrRes.second), end); 59 return make_pair(MeasurementEndpointV4{{asio::ip::address_v4{move(addrRes.first)}, 60 move(portRes.first)}}, 61 move(portRes.second)); 62 } 63 64 asio::ip::udp::endpoint ep; 65 }; 66 67 } // namespace link 68 } // namespace ableton 69