From 5876e74fa32881b41478cd67c5b0895161fbdc9c Mon Sep 17 00:00:00 2001 From: lonkaars Date: Mon, 20 May 2024 11:55:50 +0200 Subject: add socket class + test async prompt messages --- client/CMakeLists.txt | 1 + client/main.cpp | 6 +++++- client/sock.cpp | 29 +++++++++++++++++++++++++++++ client/sock.h | 21 +++++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 client/sock.cpp create mode 100644 client/sock.h diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index d77b65b..e4990d7 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -11,6 +11,7 @@ include(../proto/include.cmake) add_executable(main main.cpp rl.c + sock.cpp ) target_link_libraries(main diff --git a/client/main.cpp b/client/main.cpp index 3d3a68c..6aad0e3 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -3,6 +3,7 @@ #include #include "rl.h" +#include "sock.h" int main(int argc, char** argv) { if (argc < 2) { @@ -11,10 +12,13 @@ int main(int argc, char** argv) { } // parse arguments - char* addr_str = argv[1]; + char* addr = argv[1]; uint16_t port = 9191; if (argc >= 3) port = atoi(argv[2]); + // connect to TCP socket (automatically spawns thread) + PBSocket sock(addr, port); + // enter main CLI (using GNU readline for comfyness) return cli_main(); } diff --git a/client/sock.cpp b/client/sock.cpp new file mode 100644 index 0000000..703ee24 --- /dev/null +++ b/client/sock.cpp @@ -0,0 +1,29 @@ +#include +#include + +#include + +#include "sock.h" +#include "rl.h" + +PBSocket::PBSocket() { + printf("Init PBSocket!\n"); +} + +PBSocket::PBSocket(char* addr, uint16_t port) : PBSocket() { + connect(addr, port); +} + +void PBSocket::connect(char* addr, uint16_t port) { + printf("Connect to %s on port %d\n", addr, port); + + this->_thread = std::thread(&PBSocket::sock_task, this); +} + +void PBSocket::sock_task() { + while(1) { + sleep(3); + rl_printf("Testing asynchronous messages in prompt...\n"); + } +} + diff --git a/client/sock.h b/client/sock.h new file mode 100644 index 0000000..e3d7ec8 --- /dev/null +++ b/client/sock.h @@ -0,0 +1,21 @@ +#pragma once + +#include +#include + +class PBSocket { +public: + PBSocket(char* addr, uint16_t port); + + void connect(char* addr, uint16_t port); + +private: + PBSocket(); + + void sock_task(); + + std::thread _thread; + +}; + + -- cgit v1.2.3