Removed asyncHandler and creating new threads now once buffer-vector is complete

OLD_dev
Ruakij 3 years ago
parent f1e8069afd
commit a0ae919614

@ -1,17 +0,0 @@
#ifndef EFFCCB40_3639_4BD4_9649_302F05987909
#define EFFCCB40_3639_4BD4_9649_302F05987909
#include <future>
#include <string.h>
#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 */

@ -1,22 +1,24 @@
#ifndef C251BA62_6D80_4033_86B6_61F184E6F250 #ifndef C251BA62_6D80_4033_86B6_61F184E6F250
#define C251BA62_6D80_4033_86B6_61F184E6F250 #define C251BA62_6D80_4033_86B6_61F184E6F250
#include <future>
#include <string> #include <string>
#include "textPacketHandler.hpp" #include "textPacketHandler.hpp"
using namespace std::string_literals; using namespace std::string_literals;
std::vector<std::string> buffer; std::vector<std::string> buffer;
void bufHandler(char *buf){ void bufHandler(const char *buf){
// When first char of buf has text (no tab), we got a new packet // When first char of buf has text (no tab), we got a new packet
if(buf[0] != '\t'){ if(buf[0] != '\t'){
// Submit the just-read text-packet // Submit the just-read text-packet in a new thread
if(buffer.size() != 0) textPacketHandler(buffer); if(buffer.size() != 0) {
buffer = std::vector<std::string>(); (void)std::async(std::launch::async, textPacketHandler, buffer);
}
buffer = {std::string(buf)};
} }
else
// Append part-packet buffer.push_back(std::string(buf)); // Append part-packet
buffer.push_back(buf);
} }
#endif /* C251BA62_6D80_4033_86B6_61F184E6F250 */ #endif /* C251BA62_6D80_4033_86B6_61F184E6F250 */

@ -8,8 +8,8 @@
/// @param cmd is the command /// @param cmd is the command
/// @param handler is the handler(char*)-function /// @param handler is the handler(char*)-function
/// @return Return-code form command /// @return Return-code form command
int exec(const char* cmd, void (*handler)(char*) = nullptr){ int exec(const char* cmd, void (*handler)(const char*) = nullptr){
const int buf_size = 256; const int buf_size = 512;
char buf[buf_size]; char buf[buf_size];
// Open execution-pipe // Open execution-pipe

@ -1,7 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <string> #include <string>
#include "./helper/exec.hpp" #include "./helper/exec.hpp"
#include "./handler/asyncHandler.hpp" #include "./handler/bufHandler.hpp"
const std::string tcpdump_baseCmd = "tcpdump -vvv -e -n -X -s0 -i "; const std::string tcpdump_baseCmd = "tcpdump -vvv -e -n -X -s0 -i ";
@ -15,7 +15,7 @@ int main(int argc, char *args[]){
exit(1); exit(1);
} }
int exitCode = exec(tcpdump_cmd.c_str(), &asyncHandler); int exitCode = exec(tcpdump_cmd.c_str(), &bufHandler);
if(exitCode){ if(exitCode){
fprintf(stderr, "\ntcpdump exited with non-zero ExitCode: %d\n Something went wrong! Check tcpdump-output for more information.\n", exitCode); fprintf(stderr, "\ntcpdump exited with non-zero ExitCode: %d\n Something went wrong! Check tcpdump-output for more information.\n", exitCode);

Loading…
Cancel
Save