Commit d502382ea8bbb2fa230c8726a116ce05e5cb0084
1 parent
fdba503b
chore: rework that Connection can use Client::Impl
This avoids copying the pointers from Client::Impl into Connection, which is just administrative work. Now we can access Client::Impl, and have all variables available to us.
Showing
6 changed files
with
134 additions
and
159 deletions
src/Client.cpp
| @@ -8,27 +8,40 @@ | @@ -8,27 +8,40 @@ | ||
| 8 | #include "TrueMQTT.h" | 8 | #include "TrueMQTT.h" |
| 9 | 9 | ||
| 10 | #include "ClientImpl.h" | 10 | #include "ClientImpl.h" |
| 11 | +#include "Connection.h" | ||
| 11 | #include "Log.h" | 12 | #include "Log.h" |
| 12 | 13 | ||
| 13 | #include <sstream> | 14 | #include <sstream> |
| 14 | 15 | ||
| 15 | -using TrueMQTT::Client; | ||
| 16 | - | ||
| 17 | -Client::Client(const std::string &host, int port, const std::string &client_id, int connection_timeout, int connection_backoff_max, int keep_alive_interval) | 16 | +TrueMQTT::Client::Client(const std::string &host, int port, const std::string &client_id, int connection_timeout, int connection_backoff_max, int keep_alive_interval) |
| 18 | { | 17 | { |
| 19 | this->m_impl = std::make_unique<Client::Impl>(host, port, client_id, connection_timeout, connection_backoff_max, keep_alive_interval); | 18 | this->m_impl = std::make_unique<Client::Impl>(host, port, client_id, connection_timeout, connection_backoff_max, keep_alive_interval); |
| 20 | 19 | ||
| 21 | LOG_TRACE(this->m_impl, "Constructor of client called"); | 20 | LOG_TRACE(this->m_impl, "Constructor of client called"); |
| 22 | } | 21 | } |
| 23 | 22 | ||
| 24 | -Client::~Client() | 23 | +TrueMQTT::Client::~Client() |
| 25 | { | 24 | { |
| 26 | LOG_TRACE(this->m_impl, "Destructor of client called"); | 25 | LOG_TRACE(this->m_impl, "Destructor of client called"); |
| 27 | 26 | ||
| 28 | this->disconnect(); | 27 | this->disconnect(); |
| 29 | } | 28 | } |
| 30 | 29 | ||
| 31 | -void Client::setLogger(Client::LogLevel log_level, const std::function<void(Client::LogLevel, std::string)> &logger) const | 30 | +TrueMQTT::Client::Impl::Impl(const std::string &host, int port, const std::string &client_id, int connection_timeout, int connection_backoff_max, int keep_alive_interval) |
| 31 | + : host(host), | ||
| 32 | + port(port), | ||
| 33 | + client_id(client_id), | ||
| 34 | + connection_timeout(connection_timeout), | ||
| 35 | + connection_backoff_max(connection_backoff_max), | ||
| 36 | + keep_alive_interval(keep_alive_interval) | ||
| 37 | +{ | ||
| 38 | +} | ||
| 39 | + | ||
| 40 | +TrueMQTT::Client::Impl::~Impl() | ||
| 41 | +{ | ||
| 42 | +} | ||
| 43 | + | ||
| 44 | +void TrueMQTT::Client::setLogger(Client::LogLevel log_level, const std::function<void(Client::LogLevel, std::string)> &logger) const | ||
| 32 | { | 45 | { |
| 33 | LOG_TRACE(this->m_impl, "Setting logger to log level " + std::to_string(log_level)); | 46 | LOG_TRACE(this->m_impl, "Setting logger to log level " + std::to_string(log_level)); |
| 34 | 47 | ||
| @@ -38,7 +51,7 @@ void Client::setLogger(Client::LogLevel log_level, const std::function<void(Clie | @@ -38,7 +51,7 @@ void Client::setLogger(Client::LogLevel log_level, const std::function<void(Clie | ||
| 38 | LOG_DEBUG(this->m_impl, "Log level now on " + std::to_string(this->m_impl->log_level)); | 51 | LOG_DEBUG(this->m_impl, "Log level now on " + std::to_string(this->m_impl->log_level)); |
| 39 | } | 52 | } |
| 40 | 53 | ||
| 41 | -void Client::setLastWill(const std::string &topic, const std::string &payload, bool retain) const | 54 | +void TrueMQTT::Client::setLastWill(const std::string &topic, const std::string &payload, bool retain) const |
| 42 | { | 55 | { |
| 43 | if (this->m_impl->state != Client::Impl::State::DISCONNECTED) | 56 | if (this->m_impl->state != Client::Impl::State::DISCONNECTED) |
| 44 | { | 57 | { |
| @@ -53,14 +66,14 @@ void Client::setLastWill(const std::string &topic, const std::string &payload, b | @@ -53,14 +66,14 @@ void Client::setLastWill(const std::string &topic, const std::string &payload, b | ||
| 53 | this->m_impl->last_will_retain = retain; | 66 | this->m_impl->last_will_retain = retain; |
| 54 | } | 67 | } |
| 55 | 68 | ||
| 56 | -void Client::setErrorCallback(const std::function<void(Error, std::string)> &callback) const | 69 | +void TrueMQTT::Client::setErrorCallback(const std::function<void(Error, std::string)> &callback) const |
| 57 | { | 70 | { |
| 58 | LOG_TRACE(this->m_impl, "Setting error callback"); | 71 | LOG_TRACE(this->m_impl, "Setting error callback"); |
| 59 | 72 | ||
| 60 | this->m_impl->error_callback = callback; | 73 | this->m_impl->error_callback = callback; |
| 61 | } | 74 | } |
| 62 | 75 | ||
| 63 | -void Client::setPublishQueue(Client::PublishQueueType queue_type, size_t size) const | 76 | +void TrueMQTT::Client::setPublishQueue(Client::PublishQueueType queue_type, size_t size) const |
| 64 | { | 77 | { |
| 65 | if (this->m_impl->state != Client::Impl::State::DISCONNECTED) | 78 | if (this->m_impl->state != Client::Impl::State::DISCONNECTED) |
| 66 | { | 79 | { |
| @@ -74,7 +87,7 @@ void Client::setPublishQueue(Client::PublishQueueType queue_type, size_t size) c | @@ -74,7 +87,7 @@ void Client::setPublishQueue(Client::PublishQueueType queue_type, size_t size) c | ||
| 74 | this->m_impl->publish_queue_size = size; | 87 | this->m_impl->publish_queue_size = size; |
| 75 | } | 88 | } |
| 76 | 89 | ||
| 77 | -void Client::connect() const | 90 | +void TrueMQTT::Client::connect() const |
| 78 | { | 91 | { |
| 79 | std::scoped_lock lock(this->m_impl->state_mutex); | 92 | std::scoped_lock lock(this->m_impl->state_mutex); |
| 80 | 93 | ||
| @@ -89,7 +102,7 @@ void Client::connect() const | @@ -89,7 +102,7 @@ void Client::connect() const | ||
| 89 | this->m_impl->connect(); | 102 | this->m_impl->connect(); |
| 90 | } | 103 | } |
| 91 | 104 | ||
| 92 | -void Client::disconnect() const | 105 | +void TrueMQTT::Client::disconnect() const |
| 93 | { | 106 | { |
| 94 | std::scoped_lock lock(this->m_impl->state_mutex); | 107 | std::scoped_lock lock(this->m_impl->state_mutex); |
| 95 | 108 | ||
| @@ -105,7 +118,7 @@ void Client::disconnect() const | @@ -105,7 +118,7 @@ void Client::disconnect() const | ||
| 105 | this->m_impl->disconnect(); | 118 | this->m_impl->disconnect(); |
| 106 | } | 119 | } |
| 107 | 120 | ||
| 108 | -void Client::publish(const std::string &topic, const std::string &payload, bool retain) const | 121 | +void TrueMQTT::Client::publish(const std::string &topic, const std::string &payload, bool retain) const |
| 109 | { | 122 | { |
| 110 | std::scoped_lock lock(this->m_impl->state_mutex); | 123 | std::scoped_lock lock(this->m_impl->state_mutex); |
| 111 | 124 | ||
| @@ -125,7 +138,7 @@ void Client::publish(const std::string &topic, const std::string &payload, bool | @@ -125,7 +138,7 @@ void Client::publish(const std::string &topic, const std::string &payload, bool | ||
| 125 | } | 138 | } |
| 126 | } | 139 | } |
| 127 | 140 | ||
| 128 | -void Client::subscribe(const std::string &topic, const std::function<void(std::string, std::string)> &callback) const | 141 | +void TrueMQTT::Client::subscribe(const std::string &topic, const std::function<void(std::string, std::string)> &callback) const |
| 129 | { | 142 | { |
| 130 | std::scoped_lock lock(this->m_impl->state_mutex); | 143 | std::scoped_lock lock(this->m_impl->state_mutex); |
| 131 | 144 | ||
| @@ -158,7 +171,7 @@ void Client::subscribe(const std::string &topic, const std::function<void(std::s | @@ -158,7 +171,7 @@ void Client::subscribe(const std::string &topic, const std::function<void(std::s | ||
| 158 | } | 171 | } |
| 159 | } | 172 | } |
| 160 | 173 | ||
| 161 | -void Client::unsubscribe(const std::string &topic) const | 174 | +void TrueMQTT::Client::unsubscribe(const std::string &topic) const |
| 162 | { | 175 | { |
| 163 | std::scoped_lock lock(this->m_impl->state_mutex); | 176 | std::scoped_lock lock(this->m_impl->state_mutex); |
| 164 | 177 | ||
| @@ -216,7 +229,7 @@ void Client::unsubscribe(const std::string &topic) const | @@ -216,7 +229,7 @@ void Client::unsubscribe(const std::string &topic) const | ||
| 216 | } | 229 | } |
| 217 | } | 230 | } |
| 218 | 231 | ||
| 219 | -void Client::Impl::connectionStateChange(bool connected) | 232 | +void TrueMQTT::Client::Impl::connectionStateChange(bool connected) |
| 220 | { | 233 | { |
| 221 | std::scoped_lock lock(this->state_mutex); | 234 | std::scoped_lock lock(this->state_mutex); |
| 222 | 235 | ||
| @@ -253,7 +266,7 @@ void Client::Impl::connectionStateChange(bool connected) | @@ -253,7 +266,7 @@ void Client::Impl::connectionStateChange(bool connected) | ||
| 253 | } | 266 | } |
| 254 | } | 267 | } |
| 255 | 268 | ||
| 256 | -void Client::Impl::toPublishQueue(const std::string &topic, const std::string &payload, bool retain) | 269 | +void TrueMQTT::Client::Impl::toPublishQueue(const std::string &topic, const std::string &payload, bool retain) |
| 257 | { | 270 | { |
| 258 | if (this->state != Client::Impl::State::CONNECTING) | 271 | if (this->state != Client::Impl::State::CONNECTING) |
| 259 | { | 272 | { |
| @@ -286,7 +299,7 @@ void Client::Impl::toPublishQueue(const std::string &topic, const std::string &p | @@ -286,7 +299,7 @@ void Client::Impl::toPublishQueue(const std::string &topic, const std::string &p | ||
| 286 | this->publish_queue.emplace_back(topic, payload, retain); | 299 | this->publish_queue.emplace_back(topic, payload, retain); |
| 287 | } | 300 | } |
| 288 | 301 | ||
| 289 | -void Client::Impl::findSubscriptionMatch(std::vector<std::function<void(std::string, std::string)>> &matching_callbacks, const std::map<std::string, Client::Impl::SubscriptionPart> &subscriptions, std::deque<std::string> &parts) | 302 | +void TrueMQTT::Client::Impl::findSubscriptionMatch(std::vector<std::function<void(std::string, std::string)>> &matching_callbacks, const std::map<std::string, Client::Impl::SubscriptionPart> &subscriptions, std::deque<std::string> &parts) |
| 290 | { | 303 | { |
| 291 | // If we reached the end of the topic, do nothing anymore. | 304 | // If we reached the end of the topic, do nothing anymore. |
| 292 | if (parts.empty()) | 305 | if (parts.empty()) |
| @@ -331,7 +344,7 @@ void Client::Impl::findSubscriptionMatch(std::vector<std::function<void(std::str | @@ -331,7 +344,7 @@ void Client::Impl::findSubscriptionMatch(std::vector<std::function<void(std::str | ||
| 331 | } | 344 | } |
| 332 | } | 345 | } |
| 333 | 346 | ||
| 334 | -void Client::Impl::messageReceived(std::string topic, std::string payload) | 347 | +void TrueMQTT::Client::Impl::messageReceived(std::string topic, std::string payload) |
| 335 | { | 348 | { |
| 336 | LOG_TRACE(this, "Message received on topic '" + topic + "': " + payload); | 349 | LOG_TRACE(this, "Message received on topic '" + topic + "': " + payload); |
| 337 | 350 |
src/ClientImpl.h
| @@ -9,8 +9,6 @@ | @@ -9,8 +9,6 @@ | ||
| 9 | 9 | ||
| 10 | #include "TrueMQTT.h" | 10 | #include "TrueMQTT.h" |
| 11 | 11 | ||
| 12 | -#include "Connection.h" | ||
| 13 | - | ||
| 14 | #include <deque> | 12 | #include <deque> |
| 15 | #include <map> | 13 | #include <map> |
| 16 | #include <mutex> | 14 | #include <mutex> |
| @@ -23,15 +21,8 @@ | @@ -23,15 +21,8 @@ | ||
| 23 | class TrueMQTT::Client::Impl | 21 | class TrueMQTT::Client::Impl |
| 24 | { | 22 | { |
| 25 | public: | 23 | public: |
| 26 | - Impl(const std::string &host, int port, const std::string &client_id, int connection_timeout, int connection_backoff_max, int keep_alive_interval) | ||
| 27 | - : host(host), | ||
| 28 | - port(port), | ||
| 29 | - client_id(client_id), | ||
| 30 | - connection_timeout(connection_timeout), | ||
| 31 | - connection_backoff_max(connection_backoff_max), | ||
| 32 | - keep_alive_interval(keep_alive_interval) | ||
| 33 | - { | ||
| 34 | - } | 24 | + Impl(const std::string &host, int port, const std::string &client_id, int connection_timeout, int connection_backoff_max, int keep_alive_interval); |
| 25 | + ~Impl(); | ||
| 35 | 26 | ||
| 36 | enum State | 27 | enum State |
| 37 | { | 28 | { |
| @@ -84,6 +75,7 @@ public: | @@ -84,6 +75,7 @@ public: | ||
| 84 | std::set<std::string> subscription_topics; ///< Flat list of topics the client is subscribed to. | 75 | std::set<std::string> subscription_topics; ///< Flat list of topics the client is subscribed to. |
| 85 | std::map<std::string, SubscriptionPart> subscriptions; ///< Tree of active subscriptions build up from the parts on the topic. | 76 | std::map<std::string, SubscriptionPart> subscriptions; ///< Tree of active subscriptions build up from the parts on the topic. |
| 86 | 77 | ||
| 78 | + class Connection; | ||
| 87 | std::unique_ptr<Connection> connection; ///< Connection to the broker. | 79 | std::unique_ptr<Connection> connection; ///< Connection to the broker. |
| 88 | uint16_t packet_id = 0; ///< The next packet ID to use. Will overflow on 65535 to 0. | 80 | uint16_t packet_id = 0; ///< The next packet ID to use. Will overflow on 65535 to 0. |
| 89 | }; | 81 | }; |
src/Connection.cpp
| @@ -16,25 +16,13 @@ | @@ -16,25 +16,13 @@ | ||
| 16 | #include <unistd.h> | 16 | #include <unistd.h> |
| 17 | #include <vector> | 17 | #include <vector> |
| 18 | 18 | ||
| 19 | -Connection::Connection(TrueMQTT::Client::LogLevel log_level, | ||
| 20 | - const std::function<void(TrueMQTT::Client::LogLevel, std::string)> &logger, | ||
| 21 | - const std::function<void(TrueMQTT::Client::Error, std::string)> &error_callback, | ||
| 22 | - const std::function<void(std::string, std::string)> &publish_callback, | ||
| 23 | - const std::function<void(bool)> &connection_change_callback, | ||
| 24 | - const std::string &host, | ||
| 25 | - int port) | ||
| 26 | - : log_level(log_level), | ||
| 27 | - logger(std::move(logger)), | ||
| 28 | - m_error_callback(std::move(error_callback)), | ||
| 29 | - m_publish_callback(std::move(publish_callback)), | ||
| 30 | - m_connection_change_callback(std::move(connection_change_callback)), | ||
| 31 | - m_host(host), | ||
| 32 | - m_port(port), | 19 | +TrueMQTT::Client::Impl::Connection::Connection(Client::Impl &impl) |
| 20 | + : m_impl(impl), | ||
| 33 | m_thread(&Connection::run, this) | 21 | m_thread(&Connection::run, this) |
| 34 | { | 22 | { |
| 35 | } | 23 | } |
| 36 | 24 | ||
| 37 | -Connection::~Connection() | 25 | +TrueMQTT::Client::Impl::Connection::~Connection() |
| 38 | { | 26 | { |
| 39 | m_state = State::STOP; | 27 | m_state = State::STOP; |
| 40 | 28 | ||
| @@ -53,7 +41,7 @@ Connection::~Connection() | @@ -53,7 +41,7 @@ Connection::~Connection() | ||
| 53 | } | 41 | } |
| 54 | } | 42 | } |
| 55 | 43 | ||
| 56 | -std::string Connection::addrinfoToString(const addrinfo *address) const | 44 | +std::string TrueMQTT::Client::Impl::Connection::addrinfoToString(const addrinfo *address) const |
| 57 | { | 45 | { |
| 58 | char host[NI_MAXHOST]; | 46 | char host[NI_MAXHOST]; |
| 59 | getnameinfo(address->ai_addr, address->ai_addrlen, host, NI_MAXHOST, nullptr, 0, NI_NUMERICHOST); | 47 | getnameinfo(address->ai_addr, address->ai_addrlen, host, NI_MAXHOST, nullptr, 0, NI_NUMERICHOST); |
| @@ -61,7 +49,7 @@ std::string Connection::addrinfoToString(const addrinfo *address) const | @@ -61,7 +49,7 @@ std::string Connection::addrinfoToString(const addrinfo *address) const | ||
| 61 | return std::string(host); | 49 | return std::string(host); |
| 62 | } | 50 | } |
| 63 | 51 | ||
| 64 | -void Connection::run() | 52 | +void TrueMQTT::Client::Impl::Connection::run() |
| 65 | { | 53 | { |
| 66 | while (true) | 54 | while (true) |
| 67 | { | 55 | { |
| @@ -79,7 +67,7 @@ void Connection::run() | @@ -79,7 +67,7 @@ void Connection::run() | ||
| 79 | break; | 67 | break; |
| 80 | 68 | ||
| 81 | case State::BACKOFF: | 69 | case State::BACKOFF: |
| 82 | - LOG_WARNING(this, "Connection failed; will retry in NNN seconds"); | 70 | + LOG_WARNING(&m_impl, "Connection failed; will retry in NNN seconds"); |
| 83 | 71 | ||
| 84 | // TODO: use the configuration | 72 | // TODO: use the configuration |
| 85 | std::this_thread::sleep_for(std::chrono::seconds(5)); | 73 | std::this_thread::sleep_for(std::chrono::seconds(5)); |
| @@ -102,7 +90,7 @@ void Connection::run() | @@ -102,7 +90,7 @@ void Connection::run() | ||
| 102 | m_socket = INVALID_SOCKET; | 90 | m_socket = INVALID_SOCKET; |
| 103 | } | 91 | } |
| 104 | m_state = State::BACKOFF; | 92 | m_state = State::BACKOFF; |
| 105 | - m_connection_change_callback(false); | 93 | + m_impl.connectionStateChange(false); |
| 106 | } | 94 | } |
| 107 | break; | 95 | break; |
| 108 | } | 96 | } |
| @@ -113,7 +101,7 @@ void Connection::run() | @@ -113,7 +101,7 @@ void Connection::run() | ||
| 113 | } | 101 | } |
| 114 | } | 102 | } |
| 115 | 103 | ||
| 116 | -void Connection::resolve() | 104 | +void TrueMQTT::Client::Impl::Connection::resolve() |
| 117 | { | 105 | { |
| 118 | m_address_current = 0; | 106 | m_address_current = 0; |
| 119 | m_socket = INVALID_SOCKET; | 107 | m_socket = INVALID_SOCKET; |
| @@ -135,10 +123,10 @@ void Connection::resolve() | @@ -135,10 +123,10 @@ void Connection::resolve() | ||
| 135 | // Request the OS to resolve the hostname into an IP address. | 123 | // Request the OS to resolve the hostname into an IP address. |
| 136 | // We do this even if the hostname is already an IP address, as that | 124 | // We do this even if the hostname is already an IP address, as that |
| 137 | // makes for far easier code. | 125 | // makes for far easier code. |
| 138 | - int error = getaddrinfo(m_host.c_str(), std::to_string(m_port).c_str(), &hints, &m_host_resolved); | 126 | + int error = getaddrinfo(m_impl.host.c_str(), std::to_string(m_impl.port).c_str(), &hints, &m_host_resolved); |
| 139 | if (error != 0) | 127 | if (error != 0) |
| 140 | { | 128 | { |
| 141 | - m_error_callback(TrueMQTT::Client::Error::HOSTNAME_LOOKUP_FAILED, std::string(gai_strerror(error))); | 129 | + m_impl.error_callback(TrueMQTT::Client::Error::HOSTNAME_LOOKUP_FAILED, std::string(gai_strerror(error))); |
| 142 | return; | 130 | return; |
| 143 | } | 131 | } |
| 144 | 132 | ||
| @@ -181,12 +169,12 @@ void Connection::resolve() | @@ -181,12 +169,12 @@ void Connection::resolve() | ||
| 181 | 169 | ||
| 182 | #if MIN_LOGGER_LEVEL >= LOGGER_LEVEL_DEBUG | 170 | #if MIN_LOGGER_LEVEL >= LOGGER_LEVEL_DEBUG |
| 183 | // For debugging, print the addresses we resolved into. | 171 | // For debugging, print the addresses we resolved into. |
| 184 | - if (this->log_level >= TrueMQTT::Client::LogLevel::DEBUG) | 172 | + if (m_impl.log_level >= TrueMQTT::Client::LogLevel::DEBUG) |
| 185 | { | 173 | { |
| 186 | - LOG_DEBUG(this, "Resolved hostname '" + m_host + "' to:"); | 174 | + LOG_DEBUG(&m_impl, "Resolved hostname '" + m_impl.host + "' to:"); |
| 187 | for (const addrinfo *res : m_addresses) | 175 | for (const addrinfo *res : m_addresses) |
| 188 | { | 176 | { |
| 189 | - LOG_DEBUG(this, "- " + addrinfoToString(res)); | 177 | + LOG_DEBUG(&m_impl, "- " + addrinfoToString(res)); |
| 190 | } | 178 | } |
| 191 | } | 179 | } |
| 192 | #endif | 180 | #endif |
| @@ -194,7 +182,7 @@ void Connection::resolve() | @@ -194,7 +182,7 @@ void Connection::resolve() | ||
| 194 | // In some odd cases, the list can be empty. This is a fatal error. | 182 | // In some odd cases, the list can be empty. This is a fatal error. |
| 195 | if (m_addresses.empty()) | 183 | if (m_addresses.empty()) |
| 196 | { | 184 | { |
| 197 | - m_error_callback(TrueMQTT::Client::Error::HOSTNAME_LOOKUP_FAILED, ""); | 185 | + m_impl.error_callback(TrueMQTT::Client::Error::HOSTNAME_LOOKUP_FAILED, ""); |
| 198 | return; | 186 | return; |
| 199 | } | 187 | } |
| 200 | 188 | ||
| @@ -205,7 +193,7 @@ void Connection::resolve() | @@ -205,7 +193,7 @@ void Connection::resolve() | ||
| 205 | } | 193 | } |
| 206 | } | 194 | } |
| 207 | 195 | ||
| 208 | -bool Connection::connectToAny() | 196 | +bool TrueMQTT::Client::Impl::Connection::connectToAny() |
| 209 | { | 197 | { |
| 210 | // Check if we have pending attempts. If not, queue a new attempt. | 198 | // Check if we have pending attempts. If not, queue a new attempt. |
| 211 | if (m_sockets.empty()) | 199 | if (m_sockets.empty()) |
| @@ -235,7 +223,7 @@ bool Connection::connectToAny() | @@ -235,7 +223,7 @@ bool Connection::connectToAny() | ||
| 235 | // Check if there was an error on select(). This is hard to recover from. | 223 | // Check if there was an error on select(). This is hard to recover from. |
| 236 | if (result < 0) | 224 | if (result < 0) |
| 237 | { | 225 | { |
| 238 | - LOG_ERROR(this, "select() failed: " + std::string(strerror(errno))); | 226 | + LOG_ERROR(&m_impl, "select() failed: " + std::string(strerror(errno))); |
| 239 | return true; | 227 | return true; |
| 240 | } | 228 | } |
| 241 | 229 | ||
| @@ -261,7 +249,7 @@ bool Connection::connectToAny() | @@ -261,7 +249,7 @@ bool Connection::connectToAny() | ||
| 261 | return true; | 249 | return true; |
| 262 | } | 250 | } |
| 263 | 251 | ||
| 264 | - LOG_ERROR(this, "Connection attempt to broker timed out"); | 252 | + LOG_ERROR(&m_impl, "Connection attempt to broker timed out"); |
| 265 | 253 | ||
| 266 | // Cleanup all sockets. | 254 | // Cleanup all sockets. |
| 267 | for (const auto &socket : m_sockets) | 255 | for (const auto &socket : m_sockets) |
| @@ -287,7 +275,7 @@ bool Connection::connectToAny() | @@ -287,7 +275,7 @@ bool Connection::connectToAny() | ||
| 287 | if (err != 0) | 275 | if (err != 0) |
| 288 | { | 276 | { |
| 289 | // It is in error-state: report about it, and remove it. | 277 | // It is in error-state: report about it, and remove it. |
| 290 | - LOG_ERROR(this, "Could not connect to " + addrinfoToString(m_socket_to_address[*socket_it]) + ": " + std::string(strerror(err))); | 278 | + LOG_ERROR(&m_impl, "Could not connect to " + addrinfoToString(m_socket_to_address[*socket_it]) + ": " + std::string(strerror(err))); |
| 291 | closesocket(*socket_it); | 279 | closesocket(*socket_it); |
| 292 | m_socket_to_address.erase(*socket_it); | 280 | m_socket_to_address.erase(*socket_it); |
| 293 | socket_it = m_sockets.erase(socket_it); | 281 | socket_it = m_sockets.erase(socket_it); |
| @@ -309,7 +297,7 @@ bool Connection::connectToAny() | @@ -309,7 +297,7 @@ bool Connection::connectToAny() | ||
| 309 | } | 297 | } |
| 310 | 298 | ||
| 311 | // We have a connected socket. | 299 | // We have a connected socket. |
| 312 | - LOG_DEBUG(this, "Connected to " + addrinfoToString(m_socket_to_address[socket_connected])); | 300 | + LOG_DEBUG(&m_impl, "Connected to " + addrinfoToString(m_socket_to_address[socket_connected])); |
| 313 | 301 | ||
| 314 | // Close all other pending connections. | 302 | // Close all other pending connections. |
| 315 | for (const auto &socket : m_sockets) | 303 | for (const auto &socket : m_sockets) |
| @@ -326,7 +314,7 @@ bool Connection::connectToAny() | @@ -326,7 +314,7 @@ bool Connection::connectToAny() | ||
| 326 | int nonblocking = 0; | 314 | int nonblocking = 0; |
| 327 | if (ioctl(socket_connected, FIONBIO, &nonblocking) != 0) | 315 | if (ioctl(socket_connected, FIONBIO, &nonblocking) != 0) |
| 328 | { | 316 | { |
| 329 | - LOG_WARNING(this, "Could not set socket to non-blocking; expect performance impact"); | 317 | + LOG_WARNING(&m_impl, "Could not set socket to non-blocking; expect performance impact"); |
| 330 | } | 318 | } |
| 331 | 319 | ||
| 332 | // Only change the state if no disconnect() has been requested in the mean time. | 320 | // Only change the state if no disconnect() has been requested in the mean time. |
| @@ -339,7 +327,7 @@ bool Connection::connectToAny() | @@ -339,7 +327,7 @@ bool Connection::connectToAny() | ||
| 339 | return true; | 327 | return true; |
| 340 | } | 328 | } |
| 341 | 329 | ||
| 342 | -bool Connection::tryNextAddress() | 330 | +bool TrueMQTT::Client::Impl::Connection::tryNextAddress() |
| 343 | { | 331 | { |
| 344 | if (m_address_current >= m_addresses.size()) | 332 | if (m_address_current >= m_addresses.size()) |
| 345 | { | 333 | { |
| @@ -352,13 +340,13 @@ bool Connection::tryNextAddress() | @@ -352,13 +340,13 @@ bool Connection::tryNextAddress() | ||
| 352 | return true; | 340 | return true; |
| 353 | } | 341 | } |
| 354 | 342 | ||
| 355 | -void Connection::connect(addrinfo *address) | 343 | +void TrueMQTT::Client::Impl::Connection::connect(addrinfo *address) |
| 356 | { | 344 | { |
| 357 | // Create a new socket based on the resolved information. | 345 | // Create a new socket based on the resolved information. |
| 358 | SOCKET sock = socket(address->ai_family, address->ai_socktype, address->ai_protocol); | 346 | SOCKET sock = socket(address->ai_family, address->ai_socktype, address->ai_protocol); |
| 359 | if (sock == INVALID_SOCKET) | 347 | if (sock == INVALID_SOCKET) |
| 360 | { | 348 | { |
| 361 | - LOG_ERROR(this, "Could not create new socket"); | 349 | + LOG_ERROR(&m_impl, "Could not create new socket"); |
| 362 | return; | 350 | return; |
| 363 | } | 351 | } |
| 364 | 352 | ||
| @@ -367,18 +355,18 @@ void Connection::connect(addrinfo *address) | @@ -367,18 +355,18 @@ void Connection::connect(addrinfo *address) | ||
| 367 | /* The (const char*) cast is needed for Windows */ | 355 | /* The (const char*) cast is needed for Windows */ |
| 368 | if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (const char *)&flags, sizeof(flags)) != 0) | 356 | if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (const char *)&flags, sizeof(flags)) != 0) |
| 369 | { | 357 | { |
| 370 | - LOG_WARNING(this, "Could not set TCP_NODELAY on socket"); | 358 | + LOG_WARNING(&m_impl, "Could not set TCP_NODELAY on socket"); |
| 371 | } | 359 | } |
| 372 | // Set socket to non-blocking; this allows for multiple connects to be pending. This is | 360 | // Set socket to non-blocking; this allows for multiple connects to be pending. This is |
| 373 | // needed to apply Happy Eyeballs. | 361 | // needed to apply Happy Eyeballs. |
| 374 | int nonblocking = 1; | 362 | int nonblocking = 1; |
| 375 | if (ioctl(sock, FIONBIO, &nonblocking) != 0) | 363 | if (ioctl(sock, FIONBIO, &nonblocking) != 0) |
| 376 | { | 364 | { |
| 377 | - LOG_WARNING(this, "Could not set socket to non-blocking; expect performance impact"); | 365 | + LOG_WARNING(&m_impl, "Could not set socket to non-blocking; expect performance impact"); |
| 378 | } | 366 | } |
| 379 | 367 | ||
| 380 | // Start the actual connection attempt. | 368 | // Start the actual connection attempt. |
| 381 | - LOG_DEBUG(this, "Connecting to " + addrinfoToString(address)); | 369 | + LOG_DEBUG(&m_impl, "Connecting to " + addrinfoToString(address)); |
| 382 | int err = ::connect(sock, address->ai_addr, (int)address->ai_addrlen); | 370 | int err = ::connect(sock, address->ai_addr, (int)address->ai_addrlen); |
| 383 | if (err != 0 && errno != EINPROGRESS) | 371 | if (err != 0 && errno != EINPROGRESS) |
| 384 | { | 372 | { |
| @@ -386,7 +374,7 @@ void Connection::connect(addrinfo *address) | @@ -386,7 +374,7 @@ void Connection::connect(addrinfo *address) | ||
| 386 | // else, something is wrong. Report the error and close the socket. | 374 | // else, something is wrong. Report the error and close the socket. |
| 387 | closesocket(sock); | 375 | closesocket(sock); |
| 388 | 376 | ||
| 389 | - LOG_ERROR(this, "Could not connect to " + addrinfoToString(address) + ": " + std::string(strerror(errno))); | 377 | + LOG_ERROR(&m_impl, "Could not connect to " + addrinfoToString(address) + ": " + std::string(strerror(errno))); |
| 390 | return; | 378 | return; |
| 391 | } | 379 | } |
| 392 | 380 | ||
| @@ -397,13 +385,7 @@ void Connection::connect(addrinfo *address) | @@ -397,13 +385,7 @@ void Connection::connect(addrinfo *address) | ||
| 397 | 385 | ||
| 398 | void TrueMQTT::Client::Impl::connect() | 386 | void TrueMQTT::Client::Impl::connect() |
| 399 | { | 387 | { |
| 400 | - this->connection = std::make_unique<Connection>( | ||
| 401 | - this->log_level, this->logger, this->error_callback, | ||
| 402 | - [this](std::string topic, std::string payload) | ||
| 403 | - { this->messageReceived(std::move(topic), std::move(payload)); }, | ||
| 404 | - [this](bool connected) | ||
| 405 | - { this->connectionStateChange(connected); }, | ||
| 406 | - this->host, this->port); | 388 | + this->connection = std::make_unique<Connection>(*this); |
| 407 | } | 389 | } |
| 408 | 390 | ||
| 409 | void TrueMQTT::Client::Impl::disconnect() | 391 | void TrueMQTT::Client::Impl::disconnect() |
src/Connection.h
| @@ -7,7 +7,7 @@ | @@ -7,7 +7,7 @@ | ||
| 7 | 7 | ||
| 8 | #pragma once | 8 | #pragma once |
| 9 | 9 | ||
| 10 | -#include "TrueMQTT.h" | 10 | +#include "ClientImpl.h" |
| 11 | 11 | ||
| 12 | #include <string> | 12 | #include <string> |
| 13 | #include <map> | 13 | #include <map> |
| @@ -20,19 +20,15 @@ | @@ -20,19 +20,15 @@ | ||
| 20 | #define INVALID_SOCKET -1 | 20 | #define INVALID_SOCKET -1 |
| 21 | #define closesocket close | 21 | #define closesocket close |
| 22 | 22 | ||
| 23 | -class Connection | 23 | +class Packet; |
| 24 | + | ||
| 25 | +class TrueMQTT::Client::Impl::Connection | ||
| 24 | { | 26 | { |
| 25 | public: | 27 | public: |
| 26 | - Connection(TrueMQTT::Client::LogLevel log_level, | ||
| 27 | - const std::function<void(TrueMQTT::Client::LogLevel, std::string)> &logger, | ||
| 28 | - const std::function<void(TrueMQTT::Client::Error, std::string)> &error_callback, | ||
| 29 | - const std::function<void(std::string, std::string)> &publish_callback, | ||
| 30 | - const std::function<void(bool)> &connection_change_callback, | ||
| 31 | - const std::string &host, | ||
| 32 | - int port); | 28 | + Connection(TrueMQTT::Client::Impl &impl); |
| 33 | ~Connection(); | 29 | ~Connection(); |
| 34 | 30 | ||
| 35 | - void send(class Packet &packet) const; | 31 | + void send(Packet &packet) const; |
| 36 | 32 | ||
| 37 | private: | 33 | private: |
| 38 | // Implemented in Connection.cpp | 34 | // Implemented in Connection.cpp |
| @@ -58,15 +54,7 @@ private: | @@ -58,15 +54,7 @@ private: | ||
| 58 | STOP, | 54 | STOP, |
| 59 | }; | 55 | }; |
| 60 | 56 | ||
| 61 | - TrueMQTT::Client::LogLevel log_level; | ||
| 62 | - const std::function<void(TrueMQTT::Client::LogLevel, std::string)> logger; | ||
| 63 | - | ||
| 64 | - const std::function<void(TrueMQTT::Client::Error, std::string)> m_error_callback; | ||
| 65 | - const std::function<void(std::string, std::string)> m_publish_callback; | ||
| 66 | - const std::function<void(bool)> m_connection_change_callback; | ||
| 67 | - | ||
| 68 | - const std::string m_host; ///< The hostname or IP address to connect to. | ||
| 69 | - int m_port; ///< The port to connect to. | 57 | + TrueMQTT::Client::Impl &m_impl; |
| 70 | 58 | ||
| 71 | State m_state = State::RESOLVING; | 59 | State m_state = State::RESOLVING; |
| 72 | std::thread m_thread; ///< Current thread used to run this connection. | 60 | std::thread m_thread; ///< Current thread used to run this connection. |
src/Log.h
| @@ -22,65 +22,65 @@ | @@ -22,65 +22,65 @@ | ||
| 22 | #endif | 22 | #endif |
| 23 | 23 | ||
| 24 | #if MIN_LOGGER_LEVEL >= LOGGER_LEVEL_ERROR | 24 | #if MIN_LOGGER_LEVEL >= LOGGER_LEVEL_ERROR |
| 25 | -#define LOG_ERROR(obj, x) \ | ||
| 26 | - do \ | ||
| 27 | - { \ | ||
| 28 | - if (obj->log_level >= TrueMQTT::Client::LogLevel::ERROR) \ | ||
| 29 | - { \ | ||
| 30 | - obj->logger(TrueMQTT::Client::LogLevel::ERROR, x); \ | ||
| 31 | - } \ | 25 | +#define LOG_ERROR(obj, x) \ |
| 26 | + do \ | ||
| 27 | + { \ | ||
| 28 | + if ((obj)->log_level >= TrueMQTT::Client::LogLevel::ERROR) \ | ||
| 29 | + { \ | ||
| 30 | + (obj)->logger(TrueMQTT::Client::LogLevel::ERROR, x); \ | ||
| 31 | + } \ | ||
| 32 | } while (0) | 32 | } while (0) |
| 33 | #else | 33 | #else |
| 34 | #define LOG_ERROR(obj, x) | 34 | #define LOG_ERROR(obj, x) |
| 35 | #endif | 35 | #endif |
| 36 | 36 | ||
| 37 | #if MIN_LOGGER_LEVEL >= LOGGER_LEVEL_WARNING | 37 | #if MIN_LOGGER_LEVEL >= LOGGER_LEVEL_WARNING |
| 38 | -#define LOG_WARNING(obj, x) \ | ||
| 39 | - do \ | ||
| 40 | - { \ | ||
| 41 | - if (obj->log_level >= TrueMQTT::Client::LogLevel::WARNING) \ | ||
| 42 | - { \ | ||
| 43 | - obj->logger(TrueMQTT::Client::LogLevel::WARNING, x); \ | ||
| 44 | - } \ | 38 | +#define LOG_WARNING(obj, x) \ |
| 39 | + do \ | ||
| 40 | + { \ | ||
| 41 | + if ((obj)->log_level >= TrueMQTT::Client::LogLevel::WARNING) \ | ||
| 42 | + { \ | ||
| 43 | + (obj)->logger(TrueMQTT::Client::LogLevel::WARNING, x); \ | ||
| 44 | + } \ | ||
| 45 | } while (0) | 45 | } while (0) |
| 46 | #else | 46 | #else |
| 47 | #define LOG_WARNING(obj, x) | 47 | #define LOG_WARNING(obj, x) |
| 48 | #endif | 48 | #endif |
| 49 | 49 | ||
| 50 | #if MIN_LOGGER_LEVEL >= LOGGER_LEVEL_INFO | 50 | #if MIN_LOGGER_LEVEL >= LOGGER_LEVEL_INFO |
| 51 | -#define LOG_INFO(obj, x) \ | ||
| 52 | - do \ | ||
| 53 | - { \ | ||
| 54 | - if (obj->log_level >= TrueMQTT::Client::LogLevel::INFO) \ | ||
| 55 | - { \ | ||
| 56 | - obj->logger(TrueMQTT::Client::LogLevel::INFO, x); \ | ||
| 57 | - } \ | 51 | +#define LOG_INFO(obj, x) \ |
| 52 | + do \ | ||
| 53 | + { \ | ||
| 54 | + if ((obj)->log_level >= TrueMQTT::Client::LogLevel::INFO) \ | ||
| 55 | + { \ | ||
| 56 | + (obj)->logger(TrueMQTT::Client::LogLevel::INFO, x); \ | ||
| 57 | + } \ | ||
| 58 | } while (0) | 58 | } while (0) |
| 59 | #else | 59 | #else |
| 60 | #define LOG_INFO(obj, x) | 60 | #define LOG_INFO(obj, x) |
| 61 | #endif | 61 | #endif |
| 62 | 62 | ||
| 63 | #if MIN_LOGGER_LEVEL >= LOGGER_LEVEL_DEBUG | 63 | #if MIN_LOGGER_LEVEL >= LOGGER_LEVEL_DEBUG |
| 64 | -#define LOG_DEBUG(obj, x) \ | ||
| 65 | - do \ | ||
| 66 | - { \ | ||
| 67 | - if (obj->log_level >= TrueMQTT::Client::LogLevel::DEBUG) \ | ||
| 68 | - { \ | ||
| 69 | - obj->logger(TrueMQTT::Client::LogLevel::DEBUG, x); \ | ||
| 70 | - } \ | 64 | +#define LOG_DEBUG(obj, x) \ |
| 65 | + do \ | ||
| 66 | + { \ | ||
| 67 | + if ((obj)->log_level >= TrueMQTT::Client::LogLevel::DEBUG) \ | ||
| 68 | + { \ | ||
| 69 | + (obj)->logger(TrueMQTT::Client::LogLevel::DEBUG, x); \ | ||
| 70 | + } \ | ||
| 71 | } while (0) | 71 | } while (0) |
| 72 | #else | 72 | #else |
| 73 | #define LOG_DEBUG(obj, x) | 73 | #define LOG_DEBUG(obj, x) |
| 74 | #endif | 74 | #endif |
| 75 | 75 | ||
| 76 | #if MIN_LOGGER_LEVEL >= LOGGER_LEVEL_TRACE | 76 | #if MIN_LOGGER_LEVEL >= LOGGER_LEVEL_TRACE |
| 77 | -#define LOG_TRACE(obj, x) \ | ||
| 78 | - do \ | ||
| 79 | - { \ | ||
| 80 | - if (obj->log_level >= TrueMQTT::Client::LogLevel::TRACE) \ | ||
| 81 | - { \ | ||
| 82 | - obj->logger(TrueMQTT::Client::LogLevel::TRACE, x); \ | ||
| 83 | - } \ | 77 | +#define LOG_TRACE(obj, x) \ |
| 78 | + do \ | ||
| 79 | + { \ | ||
| 80 | + if ((obj)->log_level >= TrueMQTT::Client::LogLevel::TRACE) \ | ||
| 81 | + { \ | ||
| 82 | + (obj)->logger(TrueMQTT::Client::LogLevel::TRACE, x); \ | ||
| 83 | + } \ | ||
| 84 | } while (0) | 84 | } while (0) |
| 85 | #else | 85 | #else |
| 86 | #define LOG_TRACE(obj, x) | 86 | #define LOG_TRACE(obj, x) |
src/Packet.cpp
| @@ -121,7 +121,7 @@ public: | @@ -121,7 +121,7 @@ public: | ||
| 121 | uint8_t m_flags; | 121 | uint8_t m_flags; |
| 122 | }; | 122 | }; |
| 123 | 123 | ||
| 124 | -ssize_t Connection::recv(char *buffer, size_t length) const | 124 | +ssize_t TrueMQTT::Client::Impl::Connection::recv(char *buffer, size_t length) const |
| 125 | { | 125 | { |
| 126 | // We idle-check every 100ms if we are requested to stop, as otherwise | 126 | // We idle-check every 100ms if we are requested to stop, as otherwise |
| 127 | // this thread will block till the server disconnects us. | 127 | // this thread will block till the server disconnects us. |
| @@ -142,27 +142,27 @@ ssize_t Connection::recv(char *buffer, size_t length) const | @@ -142,27 +142,27 @@ ssize_t Connection::recv(char *buffer, size_t length) const | ||
| 142 | } | 142 | } |
| 143 | if (m_state == State::STOP) | 143 | if (m_state == State::STOP) |
| 144 | { | 144 | { |
| 145 | - LOG_TRACE(this, "Closing connection as STOP has been requested"); | 145 | + LOG_TRACE(&m_impl, "Closing connection as STOP has been requested"); |
| 146 | return -1; | 146 | return -1; |
| 147 | } | 147 | } |
| 148 | 148 | ||
| 149 | ssize_t res = ::recv(m_socket, buffer, length, 0); | 149 | ssize_t res = ::recv(m_socket, buffer, length, 0); |
| 150 | if (res == 0) | 150 | if (res == 0) |
| 151 | { | 151 | { |
| 152 | - LOG_INFO(this, "Connection closed by peer"); | 152 | + LOG_INFO(&m_impl, "Connection closed by peer"); |
| 153 | return -1; | 153 | return -1; |
| 154 | } | 154 | } |
| 155 | if (res < 0) | 155 | if (res < 0) |
| 156 | { | 156 | { |
| 157 | - LOG_WARNING(this, "Connection read error: " + std::string(strerror(errno))); | 157 | + LOG_WARNING(&m_impl, "Connection read error: " + std::string(strerror(errno))); |
| 158 | return -1; | 158 | return -1; |
| 159 | } | 159 | } |
| 160 | 160 | ||
| 161 | - LOG_TRACE(this, "Received " + std::to_string(res) + " bytes"); | 161 | + LOG_TRACE(&m_impl, "Received " + std::to_string(res) + " bytes"); |
| 162 | return res; | 162 | return res; |
| 163 | } | 163 | } |
| 164 | 164 | ||
| 165 | -bool Connection::recvLoop() | 165 | +bool TrueMQTT::Client::Impl::Connection::recvLoop() |
| 166 | { | 166 | { |
| 167 | uint8_t buffer; | 167 | uint8_t buffer; |
| 168 | 168 | ||
| @@ -177,7 +177,7 @@ bool Connection::recvLoop() | @@ -177,7 +177,7 @@ bool Connection::recvLoop() | ||
| 177 | 177 | ||
| 178 | if (packet_type_raw < 1 || packet_type_raw > 14) | 178 | if (packet_type_raw < 1 || packet_type_raw > 14) |
| 179 | { | 179 | { |
| 180 | - LOG_ERROR(this, "Received invalid packet type (" + std::to_string(packet_type_raw) + ") from broker, closing connection"); | 180 | + LOG_ERROR(&m_impl, "Received invalid packet type (" + std::to_string(packet_type_raw) + ") from broker, closing connection"); |
| 181 | return false; | 181 | return false; |
| 182 | } | 182 | } |
| 183 | 183 | ||
| @@ -203,11 +203,11 @@ bool Connection::recvLoop() | @@ -203,11 +203,11 @@ bool Connection::recvLoop() | ||
| 203 | } | 203 | } |
| 204 | if ((buffer & 0x80) != 0) | 204 | if ((buffer & 0x80) != 0) |
| 205 | { | 205 | { |
| 206 | - LOG_ERROR(this, "Malformed packet length received, closing connection"); | 206 | + LOG_ERROR(&m_impl, "Malformed packet length received, closing connection"); |
| 207 | return false; | 207 | return false; |
| 208 | } | 208 | } |
| 209 | 209 | ||
| 210 | - LOG_TRACE(this, "Received packet of type " + std::string(magic_enum::enum_name(packet_type)) + " with flags " + std::to_string(flags) + " and length " + std::to_string(remaining_length)); | 210 | + LOG_TRACE(&m_impl, "Received packet of type " + std::string(magic_enum::enum_name(packet_type)) + " with flags " + std::to_string(flags) + " and length " + std::to_string(remaining_length)); |
| 211 | 211 | ||
| 212 | // Read the rest of the packet. | 212 | // Read the rest of the packet. |
| 213 | std::vector<uint8_t> data; | 213 | std::vector<uint8_t> data; |
| @@ -235,25 +235,25 @@ bool Connection::recvLoop() | @@ -235,25 +235,25 @@ bool Connection::recvLoop() | ||
| 235 | 235 | ||
| 236 | if (!packet.read_uint8(acknowledge)) | 236 | if (!packet.read_uint8(acknowledge)) |
| 237 | { | 237 | { |
| 238 | - LOG_ERROR(this, "Malformed packet received, closing connection"); | 238 | + LOG_ERROR(&m_impl, "Malformed packet received, closing connection"); |
| 239 | return false; | 239 | return false; |
| 240 | } | 240 | } |
| 241 | if (!packet.read_uint8(return_code)) | 241 | if (!packet.read_uint8(return_code)) |
| 242 | { | 242 | { |
| 243 | - LOG_ERROR(this, "Malformed packet received, closing connection"); | 243 | + LOG_ERROR(&m_impl, "Malformed packet received, closing connection"); |
| 244 | return false; | 244 | return false; |
| 245 | } | 245 | } |
| 246 | 246 | ||
| 247 | - LOG_DEBUG(this, "Received CONNACK with acknowledge " + std::to_string(acknowledge) + " and return code " + std::to_string(return_code)); | 247 | + LOG_DEBUG(&m_impl, "Received CONNACK with acknowledge " + std::to_string(acknowledge) + " and return code " + std::to_string(return_code)); |
| 248 | 248 | ||
| 249 | if (return_code != 0) | 249 | if (return_code != 0) |
| 250 | { | 250 | { |
| 251 | - LOG_ERROR(this, "Broker actively refused our connection"); | 251 | + LOG_ERROR(&m_impl, "Broker actively refused our connection"); |
| 252 | return false; | 252 | return false; |
| 253 | } | 253 | } |
| 254 | 254 | ||
| 255 | m_state = State::CONNECTED; | 255 | m_state = State::CONNECTED; |
| 256 | - m_connection_change_callback(true); | 256 | + m_impl.connectionStateChange(true); |
| 257 | break; | 257 | break; |
| 258 | } | 258 | } |
| 259 | case Packet::PacketType::PUBLISH: | 259 | case Packet::PacketType::PUBLISH: |
| @@ -261,16 +261,16 @@ bool Connection::recvLoop() | @@ -261,16 +261,16 @@ bool Connection::recvLoop() | ||
| 261 | std::string topic; | 261 | std::string topic; |
| 262 | if (!packet.read_string(topic)) | 262 | if (!packet.read_string(topic)) |
| 263 | { | 263 | { |
| 264 | - LOG_ERROR(this, "Malformed packet received, closing connection"); | 264 | + LOG_ERROR(&m_impl, "Malformed packet received, closing connection"); |
| 265 | return false; | 265 | return false; |
| 266 | } | 266 | } |
| 267 | 267 | ||
| 268 | std::string payload; | 268 | std::string payload; |
| 269 | packet.read_remaining(payload); | 269 | packet.read_remaining(payload); |
| 270 | 270 | ||
| 271 | - LOG_DEBUG(this, "Received PUBLISH with topic " + topic + ": " + payload); | 271 | + LOG_DEBUG(&m_impl, "Received PUBLISH with topic " + topic + ": " + payload); |
| 272 | 272 | ||
| 273 | - m_publish_callback(std::move(topic), std::move(payload)); | 273 | + m_impl.messageReceived(std::move(topic), std::move(payload)); |
| 274 | break; | 274 | break; |
| 275 | } | 275 | } |
| 276 | case Packet::PacketType::SUBACK: | 276 | case Packet::PacketType::SUBACK: |
| @@ -280,22 +280,22 @@ bool Connection::recvLoop() | @@ -280,22 +280,22 @@ bool Connection::recvLoop() | ||
| 280 | 280 | ||
| 281 | if (!packet.read_uint16(packet_id)) | 281 | if (!packet.read_uint16(packet_id)) |
| 282 | { | 282 | { |
| 283 | - LOG_ERROR(this, "Malformed packet received, closing connection"); | 283 | + LOG_ERROR(&m_impl, "Malformed packet received, closing connection"); |
| 284 | return false; | 284 | return false; |
| 285 | } | 285 | } |
| 286 | if (!packet.read_uint8(return_code)) | 286 | if (!packet.read_uint8(return_code)) |
| 287 | { | 287 | { |
| 288 | - LOG_ERROR(this, "Malformed packet received, closing connection"); | 288 | + LOG_ERROR(&m_impl, "Malformed packet received, closing connection"); |
| 289 | return false; | 289 | return false; |
| 290 | } | 290 | } |
| 291 | 291 | ||
| 292 | - LOG_DEBUG(this, "Received SUBACK with packet id " + std::to_string(packet_id) + " and return code " + std::to_string(return_code)); | 292 | + LOG_DEBUG(&m_impl, "Received SUBACK with packet id " + std::to_string(packet_id) + " and return code " + std::to_string(return_code)); |
| 293 | 293 | ||
| 294 | if (return_code > 2) | 294 | if (return_code > 2) |
| 295 | { | 295 | { |
| 296 | - LOG_WARNING(this, "Broker refused our subscription"); | 296 | + LOG_WARNING(&m_impl, "Broker refused our subscription"); |
| 297 | // TODO -- Keep track of the topic per ticket | 297 | // TODO -- Keep track of the topic per ticket |
| 298 | - m_error_callback(TrueMQTT::Client::Error::SUBSCRIBE_FAILED, ""); | 298 | + m_impl.error_callback(TrueMQTT::Client::Error::SUBSCRIBE_FAILED, ""); |
| 299 | } | 299 | } |
| 300 | 300 | ||
| 301 | break; | 301 | break; |
| @@ -306,25 +306,25 @@ bool Connection::recvLoop() | @@ -306,25 +306,25 @@ bool Connection::recvLoop() | ||
| 306 | 306 | ||
| 307 | if (!packet.read_uint16(packet_id)) | 307 | if (!packet.read_uint16(packet_id)) |
| 308 | { | 308 | { |
| 309 | - LOG_ERROR(this, "Malformed packet received, closing connection"); | 309 | + LOG_ERROR(&m_impl, "Malformed packet received, closing connection"); |
| 310 | return false; | 310 | return false; |
| 311 | } | 311 | } |
| 312 | 312 | ||
| 313 | - LOG_DEBUG(this, "Received UNSUBACK with packet id " + std::to_string(packet_id)); | 313 | + LOG_DEBUG(&m_impl, "Received UNSUBACK with packet id " + std::to_string(packet_id)); |
| 314 | 314 | ||
| 315 | break; | 315 | break; |
| 316 | } | 316 | } |
| 317 | default: | 317 | default: |
| 318 | - LOG_ERROR(this, "Received unexpected packet type " + std::string(magic_enum::enum_name(packet_type)) + " from broker, closing connection"); | 318 | + LOG_ERROR(&m_impl, "Received unexpected packet type " + std::string(magic_enum::enum_name(packet_type)) + " from broker, closing connection"); |
| 319 | return false; | 319 | return false; |
| 320 | } | 320 | } |
| 321 | 321 | ||
| 322 | return true; | 322 | return true; |
| 323 | } | 323 | } |
| 324 | 324 | ||
| 325 | -void Connection::send(Packet &packet) const | 325 | +void TrueMQTT::Client::Impl::Connection::send(Packet &packet) const |
| 326 | { | 326 | { |
| 327 | - LOG_TRACE(this, "Sending packet of type " + std::string(magic_enum::enum_name(packet.m_packet_type)) + " with flags " + std::to_string(packet.m_flags) + " and length " + std::to_string(packet.m_buffer.size())); | 327 | + LOG_TRACE(&m_impl, "Sending packet of type " + std::string(magic_enum::enum_name(packet.m_packet_type)) + " with flags " + std::to_string(packet.m_flags) + " and length " + std::to_string(packet.m_buffer.size())); |
| 328 | 328 | ||
| 329 | std::vector<uint8_t> buffer; | 329 | std::vector<uint8_t> buffer; |
| 330 | 330 | ||
| @@ -347,19 +347,19 @@ void Connection::send(Packet &packet) const | @@ -347,19 +347,19 @@ void Connection::send(Packet &packet) const | ||
| 347 | // Write header and packet. | 347 | // Write header and packet. |
| 348 | if (::send(m_socket, (char *)buffer.data(), buffer.size(), MSG_NOSIGNAL) < 0) | 348 | if (::send(m_socket, (char *)buffer.data(), buffer.size(), MSG_NOSIGNAL) < 0) |
| 349 | { | 349 | { |
| 350 | - LOG_WARNING(this, "Connection write error: " + std::string(strerror(errno))); | 350 | + LOG_WARNING(&m_impl, "Connection write error: " + std::string(strerror(errno))); |
| 351 | return; | 351 | return; |
| 352 | } | 352 | } |
| 353 | if (::send(m_socket, (char *)packet.m_buffer.data(), packet.m_buffer.size(), MSG_NOSIGNAL) < 0) | 353 | if (::send(m_socket, (char *)packet.m_buffer.data(), packet.m_buffer.size(), MSG_NOSIGNAL) < 0) |
| 354 | { | 354 | { |
| 355 | - LOG_WARNING(this, "Connection write error: " + std::string(strerror(errno))); | 355 | + LOG_WARNING(&m_impl, "Connection write error: " + std::string(strerror(errno))); |
| 356 | return; | 356 | return; |
| 357 | } | 357 | } |
| 358 | } | 358 | } |
| 359 | 359 | ||
| 360 | -void Connection::sendConnect() | 360 | +void TrueMQTT::Client::Impl::Connection::sendConnect() |
| 361 | { | 361 | { |
| 362 | - LOG_TRACE(this, "Sending CONNECT packet"); | 362 | + LOG_TRACE(&m_impl, "Sending CONNECT packet"); |
| 363 | 363 | ||
| 364 | static std::string protocol_name("MQTT"); | 364 | static std::string protocol_name("MQTT"); |
| 365 | static std::string client_id("ClientID"); | 365 | static std::string client_id("ClientID"); |