From 1c279e64151f28e766c509bb1809cadd78ba84eb Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Sun, 23 Oct 2022 10:22:23 +0000 Subject: [PATCH] chore: use a better coding-style for lambdas --- example/pubsub/main.cpp | 68 +++++++++++++++++++++++++++++++++++++++++++++----------------------- example/stress/main.cpp | 34 ++++++++++++++++++++++------------ 2 files changed, 67 insertions(+), 35 deletions(-) diff --git a/example/pubsub/main.cpp b/example/pubsub/main.cpp index e8d3f5e..01c9eb7 100644 --- a/example/pubsub/main.cpp +++ b/example/pubsub/main.cpp @@ -16,11 +16,18 @@ int main() // Create a connection to the local broker. TrueMQTT::Client client("localhost", 1883, "test"); - client.setLogger(TrueMQTT::Client::LogLevel::WARNING, [](TrueMQTT::Client::LogLevel level, std::string_view message) - { std::cout << "Log " << std::string(magic_enum::enum_name(level)) << ": " << message << std::endl; }); + client.setLogger( + TrueMQTT::Client::LogLevel::WARNING, + [](TrueMQTT::Client::LogLevel level, std::string_view message) + { + std::cout << "Log " << std::string(magic_enum::enum_name(level)) << ": " << message << std::endl; + }); client.setPublishQueue(TrueMQTT::Client::PublishQueueType::FIFO, 10); - client.setErrorCallback([](TrueMQTT::Client::Error error, std::string_view message) - { std::cout << "Error " << std::string(magic_enum::enum_name(error)) << ": " << message << std::endl; }); + client.setErrorCallback( + [](TrueMQTT::Client::Error error, std::string_view message) + { + std::cout << "Error " << std::string(magic_enum::enum_name(error)) << ": " << message << std::endl; + }); client.setLastWill("example/pubsub/lastwill", "example pubsub finished", true); client.connect(); @@ -28,25 +35,40 @@ int main() int stop = 0; // Subscribe to the topic we will be publishing under in a bit. - client.subscribe("example/pubsub/test/subtest", [&stop](const std::string_view topic, const std::string_view payload) - { - std::cout << "Received message on exact topic " << topic << ": " << payload << std::endl; - stop++; }); - client.subscribe("example/pubsub/test/subtest", [&stop](const std::string_view topic, const std::string_view payload) - { - std::cout << "Received message on exact topic " << topic << " again: " << payload << std::endl; - stop++; }); - client.subscribe("example/pubsub/+/subtest", [&stop](const std::string_view topic, const std::string_view payload) - { - std::cout << "Received message on single wildcard topic " << topic << ": " << payload << std::endl; - stop++; }); - client.subscribe("example/pubsub/test/#", [&stop](const std::string_view topic, const std::string_view payload) - { - std::cout << "Received message on multi wildcard topic " << topic << ": " << payload << std::endl; - stop++; }); - client.subscribe("example/pubsub/test/+", [&stop](const std::string_view topic, const std::string_view payload) - { - /* Never actually called, as we unsubscribe a bit later */ }); + client.subscribe( + "example/pubsub/test/subtest", + [&stop](const std::string_view topic, const std::string_view payload) + { + std::cout << "Received message on exact topic " << topic << ": " << payload << std::endl; + stop++; + }); + client.subscribe( + "example/pubsub/test/subtest", + [&stop](const std::string_view topic, const std::string_view payload) + { + std::cout << "Received message on exact topic " << topic << " again: " << payload << std::endl; + stop++; + }); + client.subscribe( + "example/pubsub/+/subtest", + [&stop](const std::string_view topic, const std::string_view payload) + { + std::cout << "Received message on single wildcard topic " << topic << ": " << payload << std::endl; + stop++; + }); + client.subscribe( + "example/pubsub/test/#", + [&stop](const std::string_view topic, const std::string_view payload) + { + std::cout << "Received message on multi wildcard topic " << topic << ": " << payload << std::endl; + stop++; + }); + client.subscribe( + "example/pubsub/test/+", + [&stop](const std::string_view topic, const std::string_view payload) + { + /* Never actually called, as we unsubscribe a bit later */ + }); client.unsubscribe("example/pubsub/test/+"); diff --git a/example/stress/main.cpp b/example/stress/main.cpp index 7d08727..c1d117f 100644 --- a/example/stress/main.cpp +++ b/example/stress/main.cpp @@ -16,11 +16,18 @@ int main() // Create a connection to the local broker. TrueMQTT::Client client("localhost", 1883, "test"); - client.setLogger(TrueMQTT::Client::LogLevel::WARNING, [](TrueMQTT::Client::LogLevel level, std::string_view message) - { std::cout << "Log " << std::string(magic_enum::enum_name(level)) << ": " << message << std::endl; }); + client.setLogger( + TrueMQTT::Client::LogLevel::WARNING, + [](TrueMQTT::Client::LogLevel level, std::string_view message) + { + std::cout << "Log " << std::string(magic_enum::enum_name(level)) << ": " << message << std::endl; + }); client.setPublishQueue(TrueMQTT::Client::PublishQueueType::FIFO, 100); - client.setErrorCallback([](TrueMQTT::Client::Error error, std::string_view message) - { std::cout << "Error " << std::string(magic_enum::enum_name(error)) << ": " << message << std::endl; }); + client.setErrorCallback( + [](TrueMQTT::Client::Error error, std::string_view message) + { + std::cout << "Error " << std::string(magic_enum::enum_name(error)) << ": " << message << std::endl; + }); client.setLastWill("test/lastwill", "example pubsub finished", true); client.connect(); @@ -32,15 +39,18 @@ int main() int64_t totalLatency = 0; // Subscribe to the topic we are going to stress test. - client.subscribe("example/stress/+", [&received, &totalLatency](const std::string_view topic, const std::string_view payload) - { - // Calculate the latency. - auto now = std::chrono::steady_clock::now(); - auto then = std::chrono::time_point(std::chrono::microseconds(std::stoll(std::string(payload)))); - auto latency = std::chrono::duration_cast(now - then).count(); + client.subscribe( + "example/stress/+", + [&received, &totalLatency](const std::string_view topic, const std::string_view payload) + { + // Calculate the latency. + auto now = std::chrono::steady_clock::now(); + auto then = std::chrono::time_point(std::chrono::microseconds(std::stoll(std::string(payload)))); + auto latency = std::chrono::duration_cast(now - then).count(); - totalLatency += latency; - received++; }); + totalLatency += latency; + received++; + }); // Send a lot of packets constantly, while telling us when publishing is failing. // The expected behaviour is that this goes okay for a while, till the broker -- libgit2 0.21.4