From 16c8f0a0523bedcbb49dc80f39a37cb28e231b54 Mon Sep 17 00:00:00 2001 From: Anonymous275 <36374260+Anonymous-275@users.noreply.github.com> Date: Fri, 22 Jul 2022 23:00:25 +0300 Subject: [PATCH 1/2] Small edits --- CMakeLists.txt | 2 +- include/{Http.h => HttpAPI.h} | 0 src/Handler.cpp | 3 ++- src/Launcher.cpp | 3 ++- src/Network/{Http.cpp => HttpAPI.cpp} | 2 +- src/Network/Login.cpp | 2 +- src/Network/Update.cpp | 2 +- 7 files changed, 8 insertions(+), 6 deletions(-) rename include/{Http.h => HttpAPI.h} (100%) rename src/Network/{Http.cpp => HttpAPI.cpp} (99%) diff --git a/CMakeLists.txt b/CMakeLists.txt index ee33217..3daa6bc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,7 +43,7 @@ add_executable(${PROJECT_NAME} #src/gui/Gui.cpp include/Json.h src/gui/gifs.cpp src/gui/gifs.h - src/Network/Http.cpp include/Http.h + src/Network/HttpAPI.cpp include/HttpAPI.h src/Network/Server.cpp include/Server.h src/Network/Login.cpp src/Network/Update.cpp src/Network/Compressor.cpp include/Compressor.h diff --git a/include/Http.h b/include/HttpAPI.h similarity index 100% rename from include/Http.h rename to include/HttpAPI.h diff --git a/src/Handler.cpp b/src/Handler.cpp index e94553f..b82366d 100644 --- a/src/Handler.cpp +++ b/src/Handler.cpp @@ -7,8 +7,9 @@ #include "Memory/Memory.h" #include "Memory/BeamNG.h" #include "Launcher.h" +#include "HttpAPI.h" #include "Logger.h" -#include "Http.h" + void Launcher::HandleIPC(const std::string& Data) { char Code = Data.at(0), SubCode = 0; diff --git a/src/Launcher.cpp b/src/Launcher.cpp index a4f36c6..fb7de09 100644 --- a/src/Launcher.cpp +++ b/src/Launcher.cpp @@ -8,7 +8,7 @@ #include "Launcher.h" #include "Logger.h" #include -#include "Http.h" +#include "HttpAPI.h" #include #include #include @@ -125,6 +125,7 @@ void Launcher::WaitForGame() { std::this_thread::sleep_for(std::chrono::seconds(2)); } LOG(INFO) << "Game process was lost"; + GamePID = 0; } void Launcher::ListenIPC() { diff --git a/src/Network/Http.cpp b/src/Network/HttpAPI.cpp similarity index 99% rename from src/Network/Http.cpp rename to src/Network/HttpAPI.cpp index 5684967..4053092 100644 --- a/src/Network/Http.cpp +++ b/src/Network/HttpAPI.cpp @@ -5,10 +5,10 @@ #define CPPHTTPLIB_OPENSSL_SUPPORT #include #include "Launcher.h" +#include "HttpAPI.h" #include #include "Logger.h" #include -#include "Http.h" #include #include diff --git a/src/Network/Login.cpp b/src/Network/Login.cpp index a8d9e10..cb3f684 100644 --- a/src/Network/Login.cpp +++ b/src/Network/Login.cpp @@ -4,8 +4,8 @@ /// #include "Launcher.h" +#include "HttpAPI.h" #include "Logger.h" -#include "Http.h" #include "Json.h" void UpdateKey(const std::string& newKey){ diff --git a/src/Network/Update.cpp b/src/Network/Update.cpp index f9b29b7..a4bafac 100644 --- a/src/Network/Update.cpp +++ b/src/Network/Update.cpp @@ -4,8 +4,8 @@ /// #include "Launcher.h" +#include "HttpAPI.h" #include "Logger.h" -#include "Http.h" #include "Json.h" VersionParser::VersionParser(const std::string &from_string) { From 2ccd17fb33c24a68bf249d53cda93d266819d4df Mon Sep 17 00:00:00 2001 From: Anonymous275 <36374260+Anonymous-275@users.noreply.github.com> Date: Mon, 25 Jul 2022 10:02:57 +0300 Subject: [PATCH 2/2] Custom thread safe wrapper for std::queue --- include/atomic_queue.h | 56 ++++++++++++++++++++++++++++++++++++++++++ src/Memory/BeamNG.cpp | 11 ++++++--- 2 files changed, 63 insertions(+), 4 deletions(-) create mode 100644 include/atomic_queue.h diff --git a/include/atomic_queue.h b/include/atomic_queue.h new file mode 100644 index 0000000..e1f4b25 --- /dev/null +++ b/include/atomic_queue.h @@ -0,0 +1,56 @@ +// +// Created by Anonymous275 on 24/07/22. +// + +#pragma once +#include +#include + +template +class atomic_queue { +public: + bool try_pop(T& val) { + lock_guard guard(semaphore); + if(queue.empty())return false; + val = queue.front(); + queue.pop(); + full.release(); + return true; + } + + void push(const T& val) { + check_full(); + lock_guard guard(semaphore); + queue.push(val); + } + + size_t size() { + lock_guard guard(semaphore); + return queue.size(); + } + + bool empty() { + lock_guard guard(semaphore); + return queue.empty(); + } +private: + void check_full() { + if(size() >= Size) { + full.acquire(); + } + } +private: + struct lock_guard { + explicit lock_guard(std::binary_semaphore& lock) : lock(lock){ + lock.acquire(); + } + ~lock_guard() { + lock.release(); + } + private: + std::binary_semaphore& lock; + }; + std::binary_semaphore semaphore{1}, full{0}; + std::queue queue{}; +}; + diff --git a/src/Memory/BeamNG.cpp b/src/Memory/BeamNG.cpp index 7ee973c..6dd59c7 100644 --- a/src/Memory/BeamNG.cpp +++ b/src/Memory/BeamNG.cpp @@ -4,11 +4,13 @@ /// -#include "atomic_queue/atomic_queue.h" +#include "atomic_queue.h" #include "Memory/BeamNG.h" #include "Memory/Memory.h" -atomic_queue::AtomicQueue2 AtomicQueue; +//atomic_queue::AtomicQueue2 AtomicQueue; +std::unique_ptr> Queue; + int BeamNG::lua_open_jit_D(lua_State* State) { Memory::Print("Got lua State"); @@ -18,6 +20,7 @@ int BeamNG::lua_open_jit_D(lua_State* State) { } void BeamNG::EntryPoint() { + Queue = std::make_unique>(); auto status = MH_Initialize(); if(status != MH_OK)Memory::Print(std::string("MH Error -> ") + MH_StatusToString(status)); Memory::Print("PID : " + std::to_string(Memory::GetPID())); @@ -55,7 +58,7 @@ int Game(lua_State* L) { int LuaPop(lua_State* L) { std::string MSG; - if (AtomicQueue.try_pop(MSG)) { + if (Queue->try_pop(MSG)) { GELua::lua_push_fstring(L, "%s", MSG.c_str()); return 1; } @@ -82,7 +85,7 @@ void BeamNG::IPCListener() { IPCFromLauncher->receive(); if (!IPCFromLauncher->receive_timed_out()) { TimeOuts = 0; - AtomicQueue.push(IPCFromLauncher->msg()); + Queue->push(IPCFromLauncher->msg()); IPCFromLauncher->confirm_receive(); } else TimeOuts++; }