From 35edc6ca6ef834fac4d92309cb2ed1b0c49f2c14 Mon Sep 17 00:00:00 2001 From: Ruakij Date: Mon, 22 Nov 2021 09:08:25 +0100 Subject: [PATCH] Created basic chained handlers --- handler/asyncHandler.hpp | 16 ++++++++++++++++ handler/bufHandler.hpp | 22 ++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 handler/asyncHandler.hpp create mode 100644 handler/bufHandler.hpp diff --git a/handler/asyncHandler.hpp b/handler/asyncHandler.hpp new file mode 100644 index 0000000..4b4a7fb --- /dev/null +++ b/handler/asyncHandler.hpp @@ -0,0 +1,16 @@ +#ifndef EFFCCB40_3639_4BD4_9649_302F05987909 +#define EFFCCB40_3639_4BD4_9649_302F05987909 + +#include +#include +#include "bufHandler.hpp" + +void asyncHandler(char *buf){ + // Create a copy of buf for our thread + char bufCopy[265]; + strcpy(buf, bufCopy); + + // \/ Surpress unused warning + (void)std::async(std::launch::async, bufHandler, bufCopy); + } +#endif /* EFFCCB40_3639_4BD4_9649_302F05987909 */ diff --git a/handler/bufHandler.hpp b/handler/bufHandler.hpp new file mode 100644 index 0000000..e648671 --- /dev/null +++ b/handler/bufHandler.hpp @@ -0,0 +1,22 @@ +#ifndef C251BA62_6D80_4033_86B6_61F184E6F250 +#define C251BA62_6D80_4033_86B6_61F184E6F250 + +#include +#include "textPacketHandler.hpp" + +using namespace std::string_literals; + +std::string buffer = ""; +void bufHandler(char *buf){ + // When first char of buf has text (no space), we got a new packet + if(buf[0] != ' '){ + // Submit the just-read text-packet + textPacketHandler(buffer); + buffer = buf; + }else{ + // Append part-packet + buffer += "\n"s + buf; + } +} + +#endif /* C251BA62_6D80_4033_86B6_61F184E6F250 */