From a0ae9196145373418bdaf3d0fd0bf853a6b383fd Mon Sep 17 00:00:00 2001 From: Ruakij Date: Mon, 22 Nov 2021 14:56:18 +0100 Subject: [PATCH] Removed asyncHandler and creating new threads now once buffer-vector is complete --- handler/asyncHandler.hpp | 17 ----------------- handler/bufHandler.hpp | 16 +++++++++------- helper/exec.hpp | 4 ++-- main.cpp | 4 ++-- 4 files changed, 13 insertions(+), 28 deletions(-) delete mode 100644 handler/asyncHandler.hpp diff --git a/handler/asyncHandler.hpp b/handler/asyncHandler.hpp deleted file mode 100644 index 700db67..0000000 --- a/handler/asyncHandler.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#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(bufCopy, buf); - - // \/ 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 index 04bcd87..c61adb1 100644 --- a/handler/bufHandler.hpp +++ b/handler/bufHandler.hpp @@ -1,22 +1,24 @@ #ifndef C251BA62_6D80_4033_86B6_61F184E6F250 #define C251BA62_6D80_4033_86B6_61F184E6F250 +#include #include #include "textPacketHandler.hpp" using namespace std::string_literals; std::vector buffer; -void bufHandler(char *buf){ +void bufHandler(const char *buf){ // When first char of buf has text (no tab), we got a new packet if(buf[0] != '\t'){ - // Submit the just-read text-packet - if(buffer.size() != 0) textPacketHandler(buffer); - buffer = std::vector(); + // Submit the just-read text-packet in a new thread + if(buffer.size() != 0) { + (void)std::async(std::launch::async, textPacketHandler, buffer); + } + buffer = {std::string(buf)}; } - - // Append part-packet - buffer.push_back(buf); + else + buffer.push_back(std::string(buf)); // Append part-packet } #endif /* C251BA62_6D80_4033_86B6_61F184E6F250 */ diff --git a/helper/exec.hpp b/helper/exec.hpp index f4f7301..a0041a1 100644 --- a/helper/exec.hpp +++ b/helper/exec.hpp @@ -8,8 +8,8 @@ /// @param cmd is the command /// @param handler is the handler(char*)-function /// @return Return-code form command -int exec(const char* cmd, void (*handler)(char*) = nullptr){ - const int buf_size = 256; +int exec(const char* cmd, void (*handler)(const char*) = nullptr){ + const int buf_size = 512; char buf[buf_size]; // Open execution-pipe diff --git a/main.cpp b/main.cpp index 70d0315..401ec68 100644 --- a/main.cpp +++ b/main.cpp @@ -1,7 +1,7 @@ #include #include #include "./helper/exec.hpp" -#include "./handler/asyncHandler.hpp" +#include "./handler/bufHandler.hpp" const std::string tcpdump_baseCmd = "tcpdump -vvv -e -n -X -s0 -i "; @@ -15,7 +15,7 @@ int main(int argc, char *args[]){ exit(1); } - int exitCode = exec(tcpdump_cmd.c_str(), &asyncHandler); + int exitCode = exec(tcpdump_cmd.c_str(), &bufHandler); if(exitCode){ fprintf(stderr, "\ntcpdump exited with non-zero ExitCode: %d\n Something went wrong! Check tcpdump-output for more information.\n", exitCode);