From 72c278014768c6a0f10da7ad0af600f520ebda66 Mon Sep 17 00:00:00 2001 From: mezzode Date: Sun, 10 Nov 2019 17:27:51 -0800 Subject: [PATCH] Add example async client --- CMakeLists.txt | 3 +- CustomClass.cpp | 19 ++++++++++++ exampleClient.h => CustomClass.h | 9 ++++-- exampleAsyncClient.cpp | 52 ++++++++++++++++++++++++++++++++ exampleClient.cpp | 25 +-------------- 5 files changed, 81 insertions(+), 27 deletions(-) create mode 100644 CustomClass.cpp rename exampleClient.h => CustomClass.h (64%) create mode 100644 exampleAsyncClient.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 592a9e9..3b49da4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,5 +4,6 @@ cmake_minimum_required (VERSION 3.8) # Add source to this project's executable. -add_executable (exampleClient "exampleClient.cpp" "exampleClient.h" "client.cpp" "client.h") add_executable (server "server.cpp" "server.h") +add_executable (exampleClient "exampleClient.cpp" "CustomClass.cpp" "CustomClass.h" "client.cpp" "client.h") +add_executable (exampleAsyncClient "exampleAsyncClient.cpp" "CustomClass.cpp" "CustomClass.h" "client.cpp" "client.h") diff --git a/CustomClass.cpp b/CustomClass.cpp new file mode 100644 index 0000000..36bd478 --- /dev/null +++ b/CustomClass.cpp @@ -0,0 +1,19 @@ +#include "CustomClass.h" + +CustomClass::CustomClass(int a, int b) : counterA{ a }, counterB{ b } {} + +void CustomClass::incrementA() { + ++counterA; +} + +void CustomClass::incrementB() { + ++counterB; +} + +int CustomClass::getA() { + return counterA; +} + +int CustomClass::getB() { + return counterB; +} diff --git a/exampleClient.h b/CustomClass.h similarity index 64% rename from exampleClient.h rename to CustomClass.h index 1ee8783..822a071 100644 --- a/exampleClient.h +++ b/CustomClass.h @@ -4,7 +4,7 @@ class CustomClass { public: int counterA; - CustomClass(int a, int b); + CustomClass(int a = 0, int b = 0); void incrementA(); void incrementB(); int getA(); @@ -15,4 +15,9 @@ class CustomClass { template friend void serialize(Archive& archive, CustomClass& m); // external serialize function must be friend to serialize private attribute -}; \ No newline at end of file +}; + +template +void serialize(Archive& archive, CustomClass& m) { + archive(m.counterA, m.counterB); +} diff --git a/exampleAsyncClient.cpp b/exampleAsyncClient.cpp new file mode 100644 index 0000000..fa8f1b6 --- /dev/null +++ b/exampleAsyncClient.cpp @@ -0,0 +1,52 @@ +#include "client.h" +#include "CustomClass.h" + +using namespace std; + +#include + +int main() +{ + cout << "I am the async client." << endl; + + const auto policy{ std::launch::async }; + + // sends + + auto intSend{ std::async(policy, []() { + send("mykeyasync", 101); + }) }; + + auto strSend{ std::async(policy, []() { + send("she", string{ "ra" }); + }) }; + + auto customSend{ std::async(policy, []() { + CustomClass custom{ 42, 81 }; + custom.incrementA(); + custom.incrementB(); + send("mycustomclassasync", custom); + }) }; + + // gets + + auto intGet{ std::async(policy, [&intSend]() { + intSend.get(); // await send success + cout << get("mykeyasync") << endl; + }) }; + + auto strGet{ std::async(policy, [&strSend]() { + strSend.get(); // await send success + cout << get("she") << endl; + }) }; + + auto customGet{ std::async(policy, [&customSend]() { + customSend.get(); // await send success + auto savedCustom{ get("mycustomclassasync") }; + savedCustom.incrementA(); + savedCustom.incrementB(); + cout << savedCustom.getA() << endl << savedCustom.getB() << endl; + }) }; + + getchar(); // wait before closing +} diff --git a/exampleClient.cpp b/exampleClient.cpp index 230ce13..88d75cc 100644 --- a/exampleClient.cpp +++ b/exampleClient.cpp @@ -1,5 +1,5 @@ #include "client.h" -#include "exampleClient.h" +#include "CustomClass.h" using namespace std; @@ -26,26 +26,3 @@ int main() getchar(); // wait before closing } - -CustomClass::CustomClass(int a = 0, int b = 0) : counterA{ a }, counterB{ b } {} - -void CustomClass::incrementA() { - ++counterA; -} - -void CustomClass::incrementB() { - ++counterB; -} - -int CustomClass::getA() { - return counterA; -} - -int CustomClass::getB() { - return counterB; -} - -template -void serialize(Archive& archive, CustomClass& m) { - archive(m.counterA, m.counterB); -}