7#if !defined(ASYNC_MQTT_UTIL_HOST_PORT_HPP)
8#define ASYNC_MQTT_UTIL_HOST_PORT_HPP
12#include <boost/lexical_cast.hpp>
13#include <boost/asio/ip/address.hpp>
15#include <async_mqtt/util/move.hpp>
17#include <async_mqtt/util/log.hpp>
21namespace as = boost::asio;
24 explicit host_port(std::string host, std::uint16_t port)
25 :host{force_move(host)},
32inline std::string
to_string(host_port
const& hp) {
33 return hp.host +
':' + std::to_string(hp.port);
36inline std::ostream&
operator<<(std::ostream& s, host_port
const& hp) {
41inline bool operator==(host_port
const& lhs, host_port
const& rhs) {
42 return std::tie(lhs.host, lhs.port) == std::tie(rhs.host, rhs.port);
45inline bool operator!=(host_port
const& lhs, host_port
const& rhs) {
49inline std::optional<host_port> host_port_from_string(std::string_view str) {
51 auto last_colon = str.find_last_of(
':');
52 if (last_colon == std::string::npos)
return std::nullopt;
56 auto port_size = std::size_t(str.size() - (last_colon + 1));
57 port = boost::lexical_cast<std::uint16_t>(
58 std::string_view(&str[last_colon + 1], port_size)
60 str.remove_suffix(port_size + 1);
62 catch (std::exception
const& e) {
63 ASYNC_MQTT_LOG(
"mqtt_api",
error)
64 <<
"invalid host port string:" << str
69 if (str.empty())
return std::nullopt;
70 if (str.front() ==
'[' && str.back() ==
']') {
74 if (str.empty())
return std::nullopt;
77 return host_port{std::string(str), port};
@ error
error level error report such as connection is failed
std::ostream & operator<<(std::ostream &o, mqtt_error v)
output to the stream
std::string to_string(Container< Buffer > const &cbs)
convert buffer sequence to the string
Definition packet_iterator.hpp:68
bool operator==(property_variant const &lhs, property_variant const &rhs)
equal operator