Commit 54a4385b84bc780eae251132cf021fd93fc16730
1 parent
5454f1c4
chore: apply some coding-style suggestions from SonarCloud
Showing
7 changed files
with
82 additions
and
72 deletions
include/TrueMQTT.h
| @@ -132,7 +132,7 @@ namespace TrueMQTT | @@ -132,7 +132,7 @@ namespace TrueMQTT | ||
| 132 | * @note This library doesn't contain a logger, so you need to provide one. | 132 | * @note This library doesn't contain a logger, so you need to provide one. |
| 133 | * If this method is not called, no logging will be done. | 133 | * If this method is not called, no logging will be done. |
| 134 | */ | 134 | */ |
| 135 | - void setLogger(LogLevel log_level, std::function<void(LogLevel, std::string)> logger); | 135 | + void setLogger(LogLevel log_level, const std::function<void(LogLevel, std::string)> &logger) const; |
| 136 | 136 | ||
| 137 | /** | 137 | /** |
| 138 | * @brief Set the last will message on the connection. | 138 | * @brief Set the last will message on the connection. |
| @@ -143,14 +143,14 @@ namespace TrueMQTT | @@ -143,14 +143,14 @@ namespace TrueMQTT | ||
| 143 | * | 143 | * |
| 144 | * @note Cannot be called after \ref connect. | 144 | * @note Cannot be called after \ref connect. |
| 145 | */ | 145 | */ |
| 146 | - void setLastWill(const std::string &topic, const std::string &payload, bool retain); | 146 | + void setLastWill(const std::string &topic, const std::string &payload, bool retain) const; |
| 147 | 147 | ||
| 148 | /** | 148 | /** |
| 149 | * @brief Set the error callback, called when any error occurs. | 149 | * @brief Set the error callback, called when any error occurs. |
| 150 | * | 150 | * |
| 151 | * @param callback The callback to call when an error occurs. | 151 | * @param callback The callback to call when an error occurs. |
| 152 | */ | 152 | */ |
| 153 | - void setErrorCallback(std::function<void(Error, std::string)> callback); | 153 | + void setErrorCallback(const std::function<void(Error, std::string)> &callback) const; |
| 154 | 154 | ||
| 155 | /** | 155 | /** |
| 156 | * @brief Set the publish queue to use. | 156 | * @brief Set the publish queue to use. |
| @@ -160,7 +160,7 @@ namespace TrueMQTT | @@ -160,7 +160,7 @@ namespace TrueMQTT | ||
| 160 | * | 160 | * |
| 161 | * @note Cannot be called after \ref connect. | 161 | * @note Cannot be called after \ref connect. |
| 162 | */ | 162 | */ |
| 163 | - void setPublishQueue(PublishQueueType queue_type, size_t size); | 163 | + void setPublishQueue(PublishQueueType queue_type, size_t size) const; |
| 164 | 164 | ||
| 165 | /** | 165 | /** |
| 166 | * @brief Connect to the broker. | 166 | * @brief Connect to the broker. |
| @@ -174,7 +174,7 @@ namespace TrueMQTT | @@ -174,7 +174,7 @@ namespace TrueMQTT | ||
| 174 | * | 174 | * |
| 175 | * @note Calling connect twice has no effect. | 175 | * @note Calling connect twice has no effect. |
| 176 | */ | 176 | */ |
| 177 | - void connect(); | 177 | + void connect() const; |
| 178 | 178 | ||
| 179 | /** | 179 | /** |
| 180 | * @brief Disconnect from the broker. | 180 | * @brief Disconnect from the broker. |
| @@ -187,7 +187,7 @@ namespace TrueMQTT | @@ -187,7 +187,7 @@ namespace TrueMQTT | ||
| 187 | * moment the connection to the broker is established, and there are messages in the | 187 | * moment the connection to the broker is established, and there are messages in the |
| 188 | * publish queue and/or subscriptions. | 188 | * publish queue and/or subscriptions. |
| 189 | */ | 189 | */ |
| 190 | - void disconnect(); | 190 | + void disconnect() const; |
| 191 | 191 | ||
| 192 | /** | 192 | /** |
| 193 | * @brief Publish a payload on a topic. | 193 | * @brief Publish a payload on a topic. |
| @@ -210,7 +210,7 @@ namespace TrueMQTT | @@ -210,7 +210,7 @@ namespace TrueMQTT | ||
| 210 | * moment the connection to the broker is established, and there are messages in the | 210 | * moment the connection to the broker is established, and there are messages in the |
| 211 | * publish queue and/or subscriptions. | 211 | * publish queue and/or subscriptions. |
| 212 | */ | 212 | */ |
| 213 | - void publish(const std::string &topic, const std::string &payload, bool retain); | 213 | + void publish(const std::string &topic, const std::string &payload, bool retain) const; |
| 214 | 214 | ||
| 215 | /** | 215 | /** |
| 216 | * @brief Subscribe to a topic, and call the callback function when a message arrives. | 216 | * @brief Subscribe to a topic, and call the callback function when a message arrives. |
| @@ -242,7 +242,7 @@ namespace TrueMQTT | @@ -242,7 +242,7 @@ namespace TrueMQTT | ||
| 242 | * moment the connection to the broker is established, and there are messages in the | 242 | * moment the connection to the broker is established, and there are messages in the |
| 243 | * publish queue and/or subscriptions. | 243 | * publish queue and/or subscriptions. |
| 244 | */ | 244 | */ |
| 245 | - void subscribe(const std::string &topic, std::function<void(std::string, std::string)> callback); | 245 | + void subscribe(const std::string &topic, const std::function<void(std::string, std::string)> &callback) const; |
| 246 | 246 | ||
| 247 | /** | 247 | /** |
| 248 | * @brief Unsubscribe from a topic. | 248 | * @brief Unsubscribe from a topic. |
| @@ -258,7 +258,7 @@ namespace TrueMQTT | @@ -258,7 +258,7 @@ namespace TrueMQTT | ||
| 258 | * moment the connection to the broker is established, and there are messages in the | 258 | * moment the connection to the broker is established, and there are messages in the |
| 259 | * publish queue and/or subscriptions. | 259 | * publish queue and/or subscriptions. |
| 260 | */ | 260 | */ |
| 261 | - void unsubscribe(const std::string &topic); | 261 | + void unsubscribe(const std::string &topic) const; |
| 262 | 262 | ||
| 263 | private: | 263 | private: |
| 264 | // Private implementation | 264 | // Private implementation |
src/Client.cpp
| @@ -28,7 +28,7 @@ Client::~Client() | @@ -28,7 +28,7 @@ Client::~Client() | ||
| 28 | this->disconnect(); | 28 | this->disconnect(); |
| 29 | } | 29 | } |
| 30 | 30 | ||
| 31 | -void Client::setLogger(Client::LogLevel log_level, std::function<void(Client::LogLevel, std::string)> logger) | 31 | +void Client::setLogger(Client::LogLevel log_level, const std::function<void(Client::LogLevel, std::string)> &logger) const |
| 32 | { | 32 | { |
| 33 | LOG_TRACE(this->m_impl, "Setting logger to log level " + std::to_string(log_level)); | 33 | LOG_TRACE(this->m_impl, "Setting logger to log level " + std::to_string(log_level)); |
| 34 | 34 | ||
| @@ -38,7 +38,7 @@ void Client::setLogger(Client::LogLevel log_level, std::function<void(Client::Lo | @@ -38,7 +38,7 @@ void Client::setLogger(Client::LogLevel log_level, std::function<void(Client::Lo | ||
| 38 | LOG_DEBUG(this->m_impl, "Log level now on " + std::to_string(this->m_impl->log_level)); | 38 | LOG_DEBUG(this->m_impl, "Log level now on " + std::to_string(this->m_impl->log_level)); |
| 39 | } | 39 | } |
| 40 | 40 | ||
| 41 | -void Client::setLastWill(const std::string &topic, const std::string &payload, bool retain) | 41 | +void Client::setLastWill(const std::string &topic, const std::string &payload, bool retain) const |
| 42 | { | 42 | { |
| 43 | if (this->m_impl->state != Client::Impl::State::DISCONNECTED) | 43 | if (this->m_impl->state != Client::Impl::State::DISCONNECTED) |
| 44 | { | 44 | { |
| @@ -53,14 +53,14 @@ void Client::setLastWill(const std::string &topic, const std::string &payload, b | @@ -53,14 +53,14 @@ void Client::setLastWill(const std::string &topic, const std::string &payload, b | ||
| 53 | this->m_impl->last_will_retain = retain; | 53 | this->m_impl->last_will_retain = retain; |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | -void Client::setErrorCallback(std::function<void(Error, std::string)> callback) | 56 | +void Client::setErrorCallback(const std::function<void(Error, std::string)> &callback) const |
| 57 | { | 57 | { |
| 58 | LOG_TRACE(this->m_impl, "Setting error callback"); | 58 | LOG_TRACE(this->m_impl, "Setting error callback"); |
| 59 | 59 | ||
| 60 | this->m_impl->error_callback = callback; | 60 | this->m_impl->error_callback = callback; |
| 61 | } | 61 | } |
| 62 | 62 | ||
| 63 | -void Client::setPublishQueue(Client::PublishQueueType queue_type, size_t size) | 63 | +void Client::setPublishQueue(Client::PublishQueueType queue_type, size_t size) const |
| 64 | { | 64 | { |
| 65 | if (this->m_impl->state != Client::Impl::State::DISCONNECTED) | 65 | if (this->m_impl->state != Client::Impl::State::DISCONNECTED) |
| 66 | { | 66 | { |
| @@ -74,7 +74,7 @@ void Client::setPublishQueue(Client::PublishQueueType queue_type, size_t size) | @@ -74,7 +74,7 @@ void Client::setPublishQueue(Client::PublishQueueType queue_type, size_t size) | ||
| 74 | this->m_impl->publish_queue_size = size; | 74 | this->m_impl->publish_queue_size = size; |
| 75 | } | 75 | } |
| 76 | 76 | ||
| 77 | -void Client::connect() | 77 | +void Client::connect() const |
| 78 | { | 78 | { |
| 79 | std::scoped_lock lock(this->m_impl->state_mutex); | 79 | std::scoped_lock lock(this->m_impl->state_mutex); |
| 80 | 80 | ||
| @@ -89,7 +89,7 @@ void Client::connect() | @@ -89,7 +89,7 @@ void Client::connect() | ||
| 89 | this->m_impl->connect(); | 89 | this->m_impl->connect(); |
| 90 | } | 90 | } |
| 91 | 91 | ||
| 92 | -void Client::disconnect() | 92 | +void Client::disconnect() const |
| 93 | { | 93 | { |
| 94 | std::scoped_lock lock(this->m_impl->state_mutex); | 94 | std::scoped_lock lock(this->m_impl->state_mutex); |
| 95 | 95 | ||
| @@ -105,7 +105,7 @@ void Client::disconnect() | @@ -105,7 +105,7 @@ void Client::disconnect() | ||
| 105 | this->m_impl->disconnect(); | 105 | this->m_impl->disconnect(); |
| 106 | } | 106 | } |
| 107 | 107 | ||
| 108 | -void Client::publish(const std::string &topic, const std::string &payload, bool retain) | 108 | +void Client::publish(const std::string &topic, const std::string &payload, bool retain) const |
| 109 | { | 109 | { |
| 110 | std::scoped_lock lock(this->m_impl->state_mutex); | 110 | std::scoped_lock lock(this->m_impl->state_mutex); |
| 111 | 111 | ||
| @@ -125,7 +125,7 @@ void Client::publish(const std::string &topic, const std::string &payload, bool | @@ -125,7 +125,7 @@ void Client::publish(const std::string &topic, const std::string &payload, bool | ||
| 125 | } | 125 | } |
| 126 | } | 126 | } |
| 127 | 127 | ||
| 128 | -void Client::subscribe(const std::string &topic, std::function<void(std::string, std::string)> callback) | 128 | +void Client::subscribe(const std::string &topic, const std::function<void(std::string, std::string)> &callback) const |
| 129 | { | 129 | { |
| 130 | std::scoped_lock lock(this->m_impl->state_mutex); | 130 | std::scoped_lock lock(this->m_impl->state_mutex); |
| 131 | 131 | ||
| @@ -143,10 +143,10 @@ void Client::subscribe(const std::string &topic, std::function<void(std::string, | @@ -143,10 +143,10 @@ void Client::subscribe(const std::string &topic, std::function<void(std::string, | ||
| 143 | std::getline(stopic, part, '/'); | 143 | std::getline(stopic, part, '/'); |
| 144 | 144 | ||
| 145 | // Find the root node, and walk down till we find the leaf node. | 145 | // Find the root node, and walk down till we find the leaf node. |
| 146 | - Client::Impl::SubscriptionPart *subscriptions = &this->m_impl->subscriptions.try_emplace(part, Client::Impl::SubscriptionPart()).first->second; | 146 | + Client::Impl::SubscriptionPart *subscriptions = &this->m_impl->subscriptions.try_emplace(part).first->second; |
| 147 | while (std::getline(stopic, part, '/')) | 147 | while (std::getline(stopic, part, '/')) |
| 148 | { | 148 | { |
| 149 | - subscriptions = &subscriptions->children.try_emplace(part, Client::Impl::SubscriptionPart()).first->second; | 149 | + subscriptions = &subscriptions->children.try_emplace(part).first->second; |
| 150 | } | 150 | } |
| 151 | // Add the callback to the leaf node. | 151 | // Add the callback to the leaf node. |
| 152 | subscriptions->callbacks.push_back(callback); | 152 | subscriptions->callbacks.push_back(callback); |
| @@ -158,7 +158,7 @@ void Client::subscribe(const std::string &topic, std::function<void(std::string, | @@ -158,7 +158,7 @@ void Client::subscribe(const std::string &topic, std::function<void(std::string, | ||
| 158 | } | 158 | } |
| 159 | } | 159 | } |
| 160 | 160 | ||
| 161 | -void Client::unsubscribe(const std::string &topic) | 161 | +void Client::unsubscribe(const std::string &topic) const |
| 162 | { | 162 | { |
| 163 | std::scoped_lock lock(this->m_impl->state_mutex); | 163 | std::scoped_lock lock(this->m_impl->state_mutex); |
| 164 | 164 | ||
| @@ -178,11 +178,11 @@ void Client::unsubscribe(const std::string &topic) | @@ -178,11 +178,11 @@ void Client::unsubscribe(const std::string &topic) | ||
| 178 | // Find the root node, and walk down till we find the leaf node. | 178 | // Find the root node, and walk down till we find the leaf node. |
| 179 | std::vector<std::tuple<std::string, Client::Impl::SubscriptionPart *>> reverse; | 179 | std::vector<std::tuple<std::string, Client::Impl::SubscriptionPart *>> reverse; |
| 180 | Client::Impl::SubscriptionPart *subscriptions = &this->m_impl->subscriptions[part]; | 180 | Client::Impl::SubscriptionPart *subscriptions = &this->m_impl->subscriptions[part]; |
| 181 | - reverse.push_back({part, subscriptions}); | 181 | + reverse.emplace_back(part, subscriptions); |
| 182 | while (std::getline(stopic, part, '/')) | 182 | while (std::getline(stopic, part, '/')) |
| 183 | { | 183 | { |
| 184 | subscriptions = &subscriptions->children[part]; | 184 | subscriptions = &subscriptions->children[part]; |
| 185 | - reverse.push_back({part, subscriptions}); | 185 | + reverse.emplace_back(part, subscriptions); |
| 186 | } | 186 | } |
| 187 | // Clear the callbacks in the leaf node. | 187 | // Clear the callbacks in the leaf node. |
| 188 | subscriptions->callbacks.clear(); | 188 | subscriptions->callbacks.clear(); |
| @@ -240,9 +240,9 @@ void Client::Impl::connectionStateChange(bool connected) | @@ -240,9 +240,9 @@ void Client::Impl::connectionStateChange(bool connected) | ||
| 240 | this->sendSubscribe(subscription); | 240 | this->sendSubscribe(subscription); |
| 241 | } | 241 | } |
| 242 | // Flush the publish queue. | 242 | // Flush the publish queue. |
| 243 | - for (auto &message : this->publish_queue) | 243 | + for (const auto &[topic, payload, retain] : this->publish_queue) |
| 244 | { | 244 | { |
| 245 | - this->sendPublish(std::get<0>(message), std::get<1>(message), std::get<2>(message)); | 245 | + this->sendPublish(topic, payload, retain); |
| 246 | } | 246 | } |
| 247 | this->publish_queue.clear(); | 247 | this->publish_queue.clear(); |
| 248 | } | 248 | } |
| @@ -283,13 +283,13 @@ void Client::Impl::toPublishQueue(const std::string &topic, const std::string &p | @@ -283,13 +283,13 @@ void Client::Impl::toPublishQueue(const std::string &topic, const std::string &p | ||
| 283 | } | 283 | } |
| 284 | 284 | ||
| 285 | LOG_TRACE(this, "Adding message to publish queue"); | 285 | LOG_TRACE(this, "Adding message to publish queue"); |
| 286 | - this->publish_queue.push_back({topic, payload, retain}); | 286 | + this->publish_queue.emplace_back(topic, payload, retain); |
| 287 | } | 287 | } |
| 288 | 288 | ||
| 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) | 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) |
| 290 | { | 290 | { |
| 291 | // If we reached the end of the topic, do nothing anymore. | 291 | // If we reached the end of the topic, do nothing anymore. |
| 292 | - if (parts.size() == 0) | 292 | + if (parts.empty()) |
| 293 | { | 293 | { |
| 294 | return; | 294 | return; |
| 295 | } | 295 | } |
| @@ -357,7 +357,7 @@ void Client::Impl::messageReceived(std::string topic, std::string payload) | @@ -357,7 +357,7 @@ void Client::Impl::messageReceived(std::string topic, std::string payload) | ||
| 357 | } | 357 | } |
| 358 | else | 358 | else |
| 359 | { | 359 | { |
| 360 | - for (auto &callback : matching_callbacks) | 360 | + for (const auto &callback : matching_callbacks) |
| 361 | { | 361 | { |
| 362 | callback(topic, payload); | 362 | callback(topic, payload); |
| 363 | } | 363 | } |
src/ClientImpl.h
| @@ -68,14 +68,14 @@ public: | @@ -68,14 +68,14 @@ public: | ||
| 68 | int connection_backoff_max; ///< Maximum time between backoff attempts in seconds. | 68 | int connection_backoff_max; ///< Maximum time between backoff attempts in seconds. |
| 69 | int keep_alive_interval; ///< Interval in seconds between keep-alive messages. | 69 | int keep_alive_interval; ///< Interval in seconds between keep-alive messages. |
| 70 | 70 | ||
| 71 | - Client::LogLevel log_level = Client::LogLevel::NONE; ///< The log level to use. | ||
| 72 | - std::function<void(Client::LogLevel, std::string)> logger = [](Client::LogLevel, std::string) {}; ///< Logger callback. | 71 | + Client::LogLevel log_level = Client::LogLevel::NONE; ///< The log level to use. |
| 72 | + std::function<void(Client::LogLevel, std::string)> logger = std::move([](Client::LogLevel, std::string) { /* empty */ }); ///< Logger callback. | ||
| 73 | 73 | ||
| 74 | std::string last_will_topic = ""; ///< Topic to publish the last will message to. | 74 | std::string last_will_topic = ""; ///< Topic to publish the last will message to. |
| 75 | std::string last_will_payload = ""; ///< Payload of the last will message. | 75 | std::string last_will_payload = ""; ///< Payload of the last will message. |
| 76 | bool last_will_retain = false; ///< Whether to retain the last will message. | 76 | bool last_will_retain = false; ///< Whether to retain the last will message. |
| 77 | 77 | ||
| 78 | - std::function<void(Error, std::string)> error_callback = [](Error, std::string) {}; ///< Error callback. | 78 | + std::function<void(Error, std::string)> error_callback = std::move([](Error, std::string) { /* empty */ }); ///< Error callback. |
| 79 | 79 | ||
| 80 | Client::PublishQueueType publish_queue_type = Client::PublishQueueType::DROP; ///< The type of queue to use for the publish queue. | 80 | Client::PublishQueueType publish_queue_type = Client::PublishQueueType::DROP; ///< The type of queue to use for the publish queue. |
| 81 | size_t publish_queue_size = -1; ///< Size of the publish queue. | 81 | size_t publish_queue_size = -1; ///< Size of the publish queue. |
src/Connection.cpp
| @@ -17,10 +17,10 @@ | @@ -17,10 +17,10 @@ | ||
| 17 | #include <vector> | 17 | #include <vector> |
| 18 | 18 | ||
| 19 | Connection::Connection(TrueMQTT::Client::LogLevel log_level, | 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, | 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, | 24 | const std::string &host, |
| 25 | int port) | 25 | int port) |
| 26 | : log_level(log_level), | 26 | : log_level(log_level), |
| @@ -46,17 +46,17 @@ Connection::~Connection() | @@ -46,17 +46,17 @@ Connection::~Connection() | ||
| 46 | 46 | ||
| 47 | // freeaddrinfo() is one of those functions that doesn't take kind to NULL pointers | 47 | // freeaddrinfo() is one of those functions that doesn't take kind to NULL pointers |
| 48 | // on some platforms. | 48 | // on some platforms. |
| 49 | - if (this->m_host_resolved != NULL) | 49 | + if (this->m_host_resolved != nullptr) |
| 50 | { | 50 | { |
| 51 | freeaddrinfo(this->m_host_resolved); | 51 | freeaddrinfo(this->m_host_resolved); |
| 52 | - this->m_host_resolved = NULL; | 52 | + this->m_host_resolved = nullptr; |
| 53 | } | 53 | } |
| 54 | } | 54 | } |
| 55 | 55 | ||
| 56 | -std::string Connection::addrinfoToString(addrinfo *address) | 56 | +std::string Connection::addrinfoToString(const addrinfo *address) const |
| 57 | { | 57 | { |
| 58 | char host[NI_MAXHOST]; | 58 | char host[NI_MAXHOST]; |
| 59 | - getnameinfo(address->ai_addr, address->ai_addrlen, host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); | 59 | + getnameinfo(address->ai_addr, address->ai_addrlen, host, NI_MAXHOST, nullptr, 0, NI_NUMERICHOST); |
| 60 | 60 | ||
| 61 | return std::string(host); | 61 | return std::string(host); |
| 62 | } | 62 | } |
| @@ -214,7 +214,7 @@ bool Connection::connectToAny() | @@ -214,7 +214,7 @@ bool Connection::connectToAny() | ||
| 214 | FD_SET(socket, &write_fds); | 214 | FD_SET(socket, &write_fds); |
| 215 | } | 215 | } |
| 216 | 216 | ||
| 217 | - int result = select(FD_SETSIZE, NULL, &write_fds, NULL, &timeout); | 217 | + int result = select(FD_SETSIZE, nullptr, &write_fds, nullptr, &timeout); |
| 218 | 218 | ||
| 219 | // Check if there was an error on select(). This is hard to recover from. | 219 | // Check if there was an error on select(). This is hard to recover from. |
| 220 | if (result < 0) | 220 | if (result < 0) |
src/Connection.h
| @@ -24,15 +24,15 @@ class Connection | @@ -24,15 +24,15 @@ class Connection | ||
| 24 | { | 24 | { |
| 25 | public: | 25 | public: |
| 26 | Connection(TrueMQTT::Client::LogLevel log_level, | 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, | 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, | 31 | const std::string &host, |
| 32 | int port); | 32 | int port); |
| 33 | ~Connection(); | 33 | ~Connection(); |
| 34 | 34 | ||
| 35 | - void send(class Packet &packet); | 35 | + void send(class Packet &packet) const; |
| 36 | 36 | ||
| 37 | private: | 37 | private: |
| 38 | // Implemented in Connection.cpp | 38 | // Implemented in Connection.cpp |
| @@ -41,10 +41,10 @@ private: | @@ -41,10 +41,10 @@ private: | ||
| 41 | bool tryNextAddress(); | 41 | bool tryNextAddress(); |
| 42 | void connect(addrinfo *address); | 42 | void connect(addrinfo *address); |
| 43 | bool connectToAny(); | 43 | bool connectToAny(); |
| 44 | - std::string addrinfoToString(addrinfo *address); | 44 | + std::string addrinfoToString(const addrinfo *address) const; |
| 45 | 45 | ||
| 46 | // Implemented in Packet.cpp | 46 | // Implemented in Packet.cpp |
| 47 | - ssize_t recv(char *buffer, size_t length); | 47 | + ssize_t recv(char *buffer, size_t length) const; |
| 48 | bool recvLoop(); | 48 | bool recvLoop(); |
| 49 | void sendConnect(); | 49 | void sendConnect(); |
| 50 | 50 | ||
| @@ -65,8 +65,8 @@ private: | @@ -65,8 +65,8 @@ private: | ||
| 65 | const std::function<void(std::string, std::string)> m_publish_callback; | 65 | const std::function<void(std::string, std::string)> m_publish_callback; |
| 66 | const std::function<void(bool)> m_connection_change_callback; | 66 | const std::function<void(bool)> m_connection_change_callback; |
| 67 | 67 | ||
| 68 | - const std::string &m_host; ///< The hostname or IP address to connect to. | ||
| 69 | - int m_port; ///< The port to connect to. | 68 | + const std::string m_host; ///< The hostname or IP address to connect to. |
| 69 | + int m_port; ///< The port to connect to. | ||
| 70 | 70 | ||
| 71 | State m_state = State::RESOLVING; | 71 | State m_state = State::RESOLVING; |
| 72 | std::thread m_thread; ///< Current thread used to run this connection. | 72 | std::thread m_thread; ///< Current thread used to run this connection. |
src/Log.h
| @@ -22,50 +22,60 @@ | @@ -22,50 +22,60 @@ | ||
| 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 | - if (obj->log_level >= TrueMQTT::Client::LogLevel::ERROR) \ | ||
| 27 | - { \ | ||
| 28 | - obj->logger(TrueMQTT::Client::LogLevel::ERROR, x); \ | 25 | +#define LOG_ERROR(obj, x) \ |
| 26 | + { \ | ||
| 27 | + if (obj->log_level >= TrueMQTT::Client::LogLevel::ERROR) \ | ||
| 28 | + { \ | ||
| 29 | + obj->logger(TrueMQTT::Client::LogLevel::ERROR, x); \ | ||
| 30 | + } \ | ||
| 29 | } | 31 | } |
| 30 | #else | 32 | #else |
| 31 | #define LOG_ERROR(obj, x) | 33 | #define LOG_ERROR(obj, x) |
| 32 | #endif | 34 | #endif |
| 33 | 35 | ||
| 34 | #if MIN_LOGGER_LEVEL >= LOGGER_LEVEL_WARNING | 36 | #if MIN_LOGGER_LEVEL >= LOGGER_LEVEL_WARNING |
| 35 | -#define LOG_WARNING(obj, x) \ | ||
| 36 | - if (obj->log_level >= TrueMQTT::Client::LogLevel::WARNING) \ | ||
| 37 | - { \ | ||
| 38 | - obj->logger(TrueMQTT::Client::LogLevel::WARNING, x); \ | 37 | +#define LOG_WARNING(obj, x) \ |
| 38 | + { \ | ||
| 39 | + if (obj->log_level >= TrueMQTT::Client::LogLevel::WARNING) \ | ||
| 40 | + { \ | ||
| 41 | + obj->logger(TrueMQTT::Client::LogLevel::WARNING, x); \ | ||
| 42 | + } \ | ||
| 39 | } | 43 | } |
| 40 | #else | 44 | #else |
| 41 | #define LOG_WARNING(obj, x) | 45 | #define LOG_WARNING(obj, x) |
| 42 | #endif | 46 | #endif |
| 43 | 47 | ||
| 44 | #if MIN_LOGGER_LEVEL >= LOGGER_LEVEL_INFO | 48 | #if MIN_LOGGER_LEVEL >= LOGGER_LEVEL_INFO |
| 45 | -#define LOG_INFO(obj, x) \ | ||
| 46 | - if (obj->log_level >= TrueMQTT::Client::LogLevel::INFO) \ | ||
| 47 | - { \ | ||
| 48 | - obj->logger(TrueMQTT::Client::LogLevel::INFO, x); \ | 49 | +#define LOG_INFO(obj, x) \ |
| 50 | + { \ | ||
| 51 | + if (obj->log_level >= TrueMQTT::Client::LogLevel::INFO) \ | ||
| 52 | + { \ | ||
| 53 | + obj->logger(TrueMQTT::Client::LogLevel::INFO, x); \ | ||
| 54 | + } \ | ||
| 49 | } | 55 | } |
| 50 | #else | 56 | #else |
| 51 | #define LOG_INFO(obj, x) | 57 | #define LOG_INFO(obj, x) |
| 52 | #endif | 58 | #endif |
| 53 | 59 | ||
| 54 | #if MIN_LOGGER_LEVEL >= LOGGER_LEVEL_DEBUG | 60 | #if MIN_LOGGER_LEVEL >= LOGGER_LEVEL_DEBUG |
| 55 | -#define LOG_DEBUG(obj, x) \ | ||
| 56 | - if (obj->log_level >= TrueMQTT::Client::LogLevel::DEBUG) \ | ||
| 57 | - { \ | ||
| 58 | - obj->logger(TrueMQTT::Client::LogLevel::DEBUG, x); \ | 61 | +#define LOG_DEBUG(obj, x) \ |
| 62 | + { \ | ||
| 63 | + if (obj->log_level >= TrueMQTT::Client::LogLevel::DEBUG) \ | ||
| 64 | + { \ | ||
| 65 | + obj->logger(TrueMQTT::Client::LogLevel::DEBUG, x); \ | ||
| 66 | + } \ | ||
| 59 | } | 67 | } |
| 60 | #else | 68 | #else |
| 61 | #define LOG_DEBUG(obj, x) | 69 | #define LOG_DEBUG(obj, x) |
| 62 | #endif | 70 | #endif |
| 63 | 71 | ||
| 64 | #if MIN_LOGGER_LEVEL >= LOGGER_LEVEL_TRACE | 72 | #if MIN_LOGGER_LEVEL >= LOGGER_LEVEL_TRACE |
| 65 | -#define LOG_TRACE(obj, x) \ | ||
| 66 | - if (obj->log_level >= TrueMQTT::Client::LogLevel::TRACE) \ | ||
| 67 | - { \ | ||
| 68 | - obj->logger(TrueMQTT::Client::LogLevel::TRACE, x); \ | 73 | +#define LOG_TRACE(obj, x) \ |
| 74 | + { \ | ||
| 75 | + if (obj->log_level >= TrueMQTT::Client::LogLevel::TRACE) \ | ||
| 76 | + { \ | ||
| 77 | + obj->logger(TrueMQTT::Client::LogLevel::TRACE, x); \ | ||
| 78 | + } \ | ||
| 69 | } | 79 | } |
| 70 | #else | 80 | #else |
| 71 | #define LOG_TRACE(obj, x) | 81 | #define LOG_TRACE(obj, x) |
src/Packet.cpp
| @@ -65,7 +65,7 @@ public: | @@ -65,7 +65,7 @@ public: | ||
| 65 | 65 | ||
| 66 | void write_string(const std::string &str) | 66 | void write_string(const std::string &str) |
| 67 | { | 67 | { |
| 68 | - write_uint16(str.size()); | 68 | + write_uint16(static_cast<uint16_t>(str.size())); |
| 69 | write(str.c_str(), str.size()); | 69 | write(str.c_str(), str.size()); |
| 70 | } | 70 | } |
| 71 | 71 | ||
| @@ -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) | 124 | +ssize_t 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. |
| @@ -181,7 +181,7 @@ bool Connection::recvLoop() | @@ -181,7 +181,7 @@ bool Connection::recvLoop() | ||
| 181 | return false; | 181 | return false; |
| 182 | } | 182 | } |
| 183 | 183 | ||
| 184 | - Packet::PacketType packet_type = static_cast<Packet::PacketType>(packet_type_raw); | 184 | + auto packet_type = static_cast<Packet::PacketType>(packet_type_raw); |
| 185 | 185 | ||
| 186 | // Read the length of the packet. This is a bit slow to read, as only | 186 | // Read the length of the packet. This is a bit slow to read, as only |
| 187 | // after reading the byte we know if another byte follows. | 187 | // after reading the byte we know if another byte follows. |
| @@ -322,7 +322,7 @@ bool Connection::recvLoop() | @@ -322,7 +322,7 @@ bool Connection::recvLoop() | ||
| 322 | return true; | 322 | return true; |
| 323 | } | 323 | } |
| 324 | 324 | ||
| 325 | -void Connection::send(Packet &packet) | 325 | +void 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(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())); |
| 328 | 328 |