// Copyright (c) 2017 Computer Vision Center (CVC) at the Universitat Autonoma // de Barcelona (UAB). // // This work is licensed under the terms of the MIT license. // For a copy, see . #pragma once #include #include #include #include namespace carla { namespace streaming { namespace detail { // When in doubt, V4 addresses are returned. struct FullyDefinedEndPoint {}; struct PartiallyDefinedEndPoint {}; template class EndPoint; template class EndPoint { public: explicit EndPoint(boost::asio::ip::basic_endpoint ep) : _endpoint(std::move(ep)) {} auto address() const { return _endpoint.address(); } uint16_t port() const { return _endpoint.port(); } operator boost::asio::ip::basic_endpoint() const { return _endpoint; } private: boost::asio::ip::basic_endpoint _endpoint; }; template class EndPoint { public: explicit EndPoint(uint16_t port) : _port(port) {} uint16_t port() const { return _port; } operator boost::asio::ip::basic_endpoint() const { return {Protocol::v4(), _port}; } private: uint16_t _port; }; } // namespace detail static inline auto make_localhost_address() { return boost::asio::ip::make_address("127.0.0.1"); } static inline auto make_address(const std::string &address) { boost::asio::io_context io_context; boost::asio::ip::tcp::resolver resolver(io_context); boost::asio::ip::tcp::resolver::query query(boost::asio::ip::tcp::v4(), address, "", boost::asio::ip::tcp::resolver::query::canonical_name); boost::asio::ip::tcp::resolver::iterator iter = resolver.resolve(query); boost::asio::ip::tcp::resolver::iterator end; while (iter != end) { boost::asio::ip::tcp::endpoint endpoint = *iter++; return endpoint.address(); } return boost::asio::ip::make_address(address); } template static inline auto make_endpoint(boost::asio::ip::basic_endpoint ep) { return detail::EndPoint{std::move(ep)}; } template static inline auto make_endpoint(const char *address, uint16_t port) { return make_endpoint({make_address(address), port}); } template static inline auto make_endpoint(const std::string &address, uint16_t port) { return make_endpoint(address.c_str(), port); } template static inline auto make_endpoint(uint16_t port) { return detail::EndPoint{port}; } } // namespace streaming } // namespace carla