Commit 1c279e64151f28e766c509bb1809cadd78ba84eb
1 parent
9ff0a986
chore: use a better coding-style for lambdas
Showing
2 changed files
with
67 additions
and
35 deletions
example/pubsub/main.cpp
| @@ -16,11 +16,18 @@ int main() | @@ -16,11 +16,18 @@ int main() | ||
| 16 | // Create a connection to the local broker. | 16 | // Create a connection to the local broker. |
| 17 | TrueMQTT::Client client("localhost", 1883, "test"); | 17 | TrueMQTT::Client client("localhost", 1883, "test"); |
| 18 | 18 | ||
| 19 | - client.setLogger(TrueMQTT::Client::LogLevel::WARNING, [](TrueMQTT::Client::LogLevel level, std::string_view message) | ||
| 20 | - { std::cout << "Log " << std::string(magic_enum::enum_name(level)) << ": " << message << std::endl; }); | 19 | + client.setLogger( |
| 20 | + TrueMQTT::Client::LogLevel::WARNING, | ||
| 21 | + [](TrueMQTT::Client::LogLevel level, std::string_view message) | ||
| 22 | + { | ||
| 23 | + std::cout << "Log " << std::string(magic_enum::enum_name(level)) << ": " << message << std::endl; | ||
| 24 | + }); | ||
| 21 | client.setPublishQueue(TrueMQTT::Client::PublishQueueType::FIFO, 10); | 25 | client.setPublishQueue(TrueMQTT::Client::PublishQueueType::FIFO, 10); |
| 22 | - client.setErrorCallback([](TrueMQTT::Client::Error error, std::string_view message) | ||
| 23 | - { std::cout << "Error " << std::string(magic_enum::enum_name(error)) << ": " << message << std::endl; }); | 26 | + client.setErrorCallback( |
| 27 | + [](TrueMQTT::Client::Error error, std::string_view message) | ||
| 28 | + { | ||
| 29 | + std::cout << "Error " << std::string(magic_enum::enum_name(error)) << ": " << message << std::endl; | ||
| 30 | + }); | ||
| 24 | client.setLastWill("example/pubsub/lastwill", "example pubsub finished", true); | 31 | client.setLastWill("example/pubsub/lastwill", "example pubsub finished", true); |
| 25 | 32 | ||
| 26 | client.connect(); | 33 | client.connect(); |
| @@ -28,25 +35,40 @@ int main() | @@ -28,25 +35,40 @@ int main() | ||
| 28 | int stop = 0; | 35 | int stop = 0; |
| 29 | 36 | ||
| 30 | // Subscribe to the topic we will be publishing under in a bit. | 37 | // Subscribe to the topic we will be publishing under in a bit. |
| 31 | - client.subscribe("example/pubsub/test/subtest", [&stop](const std::string_view topic, const std::string_view payload) | ||
| 32 | - { | ||
| 33 | - std::cout << "Received message on exact topic " << topic << ": " << payload << std::endl; | ||
| 34 | - stop++; }); | ||
| 35 | - client.subscribe("example/pubsub/test/subtest", [&stop](const std::string_view topic, const std::string_view payload) | ||
| 36 | - { | ||
| 37 | - std::cout << "Received message on exact topic " << topic << " again: " << payload << std::endl; | ||
| 38 | - stop++; }); | ||
| 39 | - client.subscribe("example/pubsub/+/subtest", [&stop](const std::string_view topic, const std::string_view payload) | ||
| 40 | - { | ||
| 41 | - std::cout << "Received message on single wildcard topic " << topic << ": " << payload << std::endl; | ||
| 42 | - stop++; }); | ||
| 43 | - client.subscribe("example/pubsub/test/#", [&stop](const std::string_view topic, const std::string_view payload) | ||
| 44 | - { | ||
| 45 | - std::cout << "Received message on multi wildcard topic " << topic << ": " << payload << std::endl; | ||
| 46 | - stop++; }); | ||
| 47 | - client.subscribe("example/pubsub/test/+", [&stop](const std::string_view topic, const std::string_view payload) | ||
| 48 | - { | ||
| 49 | - /* Never actually called, as we unsubscribe a bit later */ }); | 38 | + client.subscribe( |
| 39 | + "example/pubsub/test/subtest", | ||
| 40 | + [&stop](const std::string_view topic, const std::string_view payload) | ||
| 41 | + { | ||
| 42 | + std::cout << "Received message on exact topic " << topic << ": " << payload << std::endl; | ||
| 43 | + stop++; | ||
| 44 | + }); | ||
| 45 | + client.subscribe( | ||
| 46 | + "example/pubsub/test/subtest", | ||
| 47 | + [&stop](const std::string_view topic, const std::string_view payload) | ||
| 48 | + { | ||
| 49 | + std::cout << "Received message on exact topic " << topic << " again: " << payload << std::endl; | ||
| 50 | + stop++; | ||
| 51 | + }); | ||
| 52 | + client.subscribe( | ||
| 53 | + "example/pubsub/+/subtest", | ||
| 54 | + [&stop](const std::string_view topic, const std::string_view payload) | ||
| 55 | + { | ||
| 56 | + std::cout << "Received message on single wildcard topic " << topic << ": " << payload << std::endl; | ||
| 57 | + stop++; | ||
| 58 | + }); | ||
| 59 | + client.subscribe( | ||
| 60 | + "example/pubsub/test/#", | ||
| 61 | + [&stop](const std::string_view topic, const std::string_view payload) | ||
| 62 | + { | ||
| 63 | + std::cout << "Received message on multi wildcard topic " << topic << ": " << payload << std::endl; | ||
| 64 | + stop++; | ||
| 65 | + }); | ||
| 66 | + client.subscribe( | ||
| 67 | + "example/pubsub/test/+", | ||
| 68 | + [&stop](const std::string_view topic, const std::string_view payload) | ||
| 69 | + { | ||
| 70 | + /* Never actually called, as we unsubscribe a bit later */ | ||
| 71 | + }); | ||
| 50 | 72 | ||
| 51 | client.unsubscribe("example/pubsub/test/+"); | 73 | client.unsubscribe("example/pubsub/test/+"); |
| 52 | 74 |
example/stress/main.cpp
| @@ -16,11 +16,18 @@ int main() | @@ -16,11 +16,18 @@ int main() | ||
| 16 | // Create a connection to the local broker. | 16 | // Create a connection to the local broker. |
| 17 | TrueMQTT::Client client("localhost", 1883, "test"); | 17 | TrueMQTT::Client client("localhost", 1883, "test"); |
| 18 | 18 | ||
| 19 | - client.setLogger(TrueMQTT::Client::LogLevel::WARNING, [](TrueMQTT::Client::LogLevel level, std::string_view message) | ||
| 20 | - { std::cout << "Log " << std::string(magic_enum::enum_name(level)) << ": " << message << std::endl; }); | 19 | + client.setLogger( |
| 20 | + TrueMQTT::Client::LogLevel::WARNING, | ||
| 21 | + [](TrueMQTT::Client::LogLevel level, std::string_view message) | ||
| 22 | + { | ||
| 23 | + std::cout << "Log " << std::string(magic_enum::enum_name(level)) << ": " << message << std::endl; | ||
| 24 | + }); | ||
| 21 | client.setPublishQueue(TrueMQTT::Client::PublishQueueType::FIFO, 100); | 25 | client.setPublishQueue(TrueMQTT::Client::PublishQueueType::FIFO, 100); |
| 22 | - client.setErrorCallback([](TrueMQTT::Client::Error error, std::string_view message) | ||
| 23 | - { std::cout << "Error " << std::string(magic_enum::enum_name(error)) << ": " << message << std::endl; }); | 26 | + client.setErrorCallback( |
| 27 | + [](TrueMQTT::Client::Error error, std::string_view message) | ||
| 28 | + { | ||
| 29 | + std::cout << "Error " << std::string(magic_enum::enum_name(error)) << ": " << message << std::endl; | ||
| 30 | + }); | ||
| 24 | client.setLastWill("test/lastwill", "example pubsub finished", true); | 31 | client.setLastWill("test/lastwill", "example pubsub finished", true); |
| 25 | 32 | ||
| 26 | client.connect(); | 33 | client.connect(); |
| @@ -32,15 +39,18 @@ int main() | @@ -32,15 +39,18 @@ int main() | ||
| 32 | int64_t totalLatency = 0; | 39 | int64_t totalLatency = 0; |
| 33 | 40 | ||
| 34 | // Subscribe to the topic we are going to stress test. | 41 | // Subscribe to the topic we are going to stress test. |
| 35 | - client.subscribe("example/stress/+", [&received, &totalLatency](const std::string_view topic, const std::string_view payload) | ||
| 36 | - { | ||
| 37 | - // Calculate the latency. | ||
| 38 | - auto now = std::chrono::steady_clock::now(); | ||
| 39 | - auto then = std::chrono::time_point<std::chrono::steady_clock>(std::chrono::microseconds(std::stoll(std::string(payload)))); | ||
| 40 | - auto latency = std::chrono::duration_cast<std::chrono::microseconds>(now - then).count(); | 42 | + client.subscribe( |
| 43 | + "example/stress/+", | ||
| 44 | + [&received, &totalLatency](const std::string_view topic, const std::string_view payload) | ||
| 45 | + { | ||
| 46 | + // Calculate the latency. | ||
| 47 | + auto now = std::chrono::steady_clock::now(); | ||
| 48 | + auto then = std::chrono::time_point<std::chrono::steady_clock>(std::chrono::microseconds(std::stoll(std::string(payload)))); | ||
| 49 | + auto latency = std::chrono::duration_cast<std::chrono::microseconds>(now - then).count(); | ||
| 41 | 50 | ||
| 42 | - totalLatency += latency; | ||
| 43 | - received++; }); | 51 | + totalLatency += latency; |
| 52 | + received++; | ||
| 53 | + }); | ||
| 44 | 54 | ||
| 45 | // Send a lot of packets constantly, while telling us when publishing is failing. | 55 | // Send a lot of packets constantly, while telling us when publishing is failing. |
| 46 | // The expected behaviour is that this goes okay for a while, till the broker | 56 | // The expected behaviour is that this goes okay for a while, till the broker |