From 51b38a317d58d2983a49a6765bc5e9bc9e474be8 Mon Sep 17 00:00:00 2001 From: cylian charbonnier Date: Sun, 22 Oct 2023 16:11:43 +0200 Subject: [PATCH 1/3] Allow multiple read from IPC --- src/ipc/Socket.cpp | 132 ++++++++++++++++++++++----------------------- 1 file changed, 63 insertions(+), 69 deletions(-) diff --git a/src/ipc/Socket.cpp b/src/ipc/Socket.cpp index 366d0ef..a9ad5fa 100644 --- a/src/ipc/Socket.cpp +++ b/src/ipc/Socket.cpp @@ -15,7 +15,7 @@ void CIPCSocket::initialize() { std::thread([&]() { const auto SOCKET = socket(AF_UNIX, SOCK_STREAM, 0); - + if (SOCKET < 0) { Debug::log(ERR, "Couldn't start the hyprpaper Socket. (1) IPC will not work."); return; @@ -45,86 +45,80 @@ void CIPCSocket::initialize() { char readBuffer[1024] = {0}; - Debug::log(LOG, "hyprpaper socket started at %s (fd: %i)", socketPath.c_str(), SOCKET); - - while(1) { - const auto ACCEPTEDCONNECTION = accept(SOCKET, (sockaddr*)&clientAddress, &clientSize); - - Debug::log(LOG, "Accepted incoming socket connection request on fd %i", ACCEPTEDCONNECTION); - - if (ACCEPTEDCONNECTION < 0) { - Debug::log(ERR, "Couldn't listen on the hyprpaper Socket. (3) IPC will not work."); - break; - } - - std::lock_guard lg(g_pHyprpaper->m_mtTickMutex); - - auto messageSize = read(ACCEPTEDCONNECTION, readBuffer, 1024); - readBuffer[messageSize == 1024 ? 1023 : messageSize] = '\0'; - - std::string request(readBuffer); - - m_szRequest = request; - m_bRequestReady = true; - - g_pHyprpaper->tick(true); - - while (1) { - if (m_bReplyReady) - break; - - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } - - write(ACCEPTEDCONNECTION, m_szReply.c_str(), m_szReply.length()); - - close(ACCEPTEDCONNECTION); - - m_bReplyReady = false; - m_szReply = ""; - } - - close(SOCKET); - }).detach(); + Debug::log(LOG, "hyprpaper socket started at %s (fd: %i)", socketPath.c_str(), SOCKET); + while (1) { + const auto ACCEPTEDCONNECTION = accept(SOCKET, (sockaddr*)&clientAddress, &clientSize); + if (ACCEPTEDCONNECTION < 0) { + Debug::log(ERR, "Couldn't listen on the hyprpaper Socket. (3) IPC will not work."); + break; + }else { + do { + Debug::log(LOG, "Accepted incoming socket connection request on fd %i", ACCEPTEDCONNECTION); + std::lock_guard lg(g_pHyprpaper->m_mtTickMutex); + + auto messageSize = read(ACCEPTEDCONNECTION, readBuffer, 1024); + readBuffer[messageSize == 1024 ? 1023 : messageSize] = '\0'; + if (messageSize == 0) + break; + std::string request(readBuffer); + + m_szRequest = request; + m_bRequestReady = true; + + g_pHyprpaper->tick(true); + while (!m_bReplyReady) { // wait for Hyprpaper to finish processing the request + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + write(ACCEPTEDCONNECTION, m_szReply.c_str(), m_szReply.length()); + m_bReplyReady = false; + m_szReply = ""; + + }while(1); + Debug::log(LOG, "Closing Accepted Connection"); + close(ACCEPTEDCONNECTION); + } + } + + close(SOCKET); + }).detach(); } bool CIPCSocket::mainThreadParseRequest() { - - if (!m_bRequestReady) - return false; - std::string copy = m_szRequest; + if (!m_bRequestReady) + return false; - // now we can work on the copy + std::string copy = m_szRequest; - if (copy == "") - return false; + // now we can work on the copy - Debug::log(LOG, "Received a request: %s", copy.c_str()); + if (copy == "") + return false; + Debug::log(LOG, "Received a request: %s", copy.c_str()); - // parse - if (copy.find("wallpaper") == 0 || copy.find("preload") == 0 || copy.find("unload") == 0) { - g_pConfigManager->parseError = ""; // reset parse error + // parse + if (copy.find("wallpaper") == 0 || copy.find("preload") == 0 || copy.find("unload") == 0) { + g_pConfigManager->parseError = ""; // reset parse error - g_pConfigManager->parseKeyword(copy.substr(0, copy.find_first_of(' ')), copy.substr(copy.find_first_of(' ') + 1)); + g_pConfigManager->parseKeyword(copy.substr(0, copy.find_first_of(' ')), copy.substr(copy.find_first_of(' ') + 1)); - if (!g_pConfigManager->parseError.empty()) { - m_szReply = g_pConfigManager->parseError; - m_bReplyReady = true; - m_bRequestReady = false; - return false; - } - } - else { - m_szReply = "invalid command"; - m_bReplyReady = true; - m_bRequestReady = false; - return false; + if (!g_pConfigManager->parseError.empty()) { + m_szReply = g_pConfigManager->parseError; + m_bReplyReady = true; + m_bRequestReady = false; + return false; } - - m_szReply = "ok"; + } + else { + m_szReply = "invalid command"; m_bReplyReady = true; m_bRequestReady = false; + return false; + } + + m_szReply = "ok"; + m_bReplyReady = true; + m_bRequestReady = false; - return true; + return true; } From bbb5d151496fcd2c475072c471dae4fa110f7c15 Mon Sep 17 00:00:00 2001 From: Charbonnier Cylian Date: Sun, 22 Oct 2023 16:26:44 +0200 Subject: [PATCH 2/3] bring back old indentation --- src/ipc/Socket.cpp | 53 +++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/src/ipc/Socket.cpp b/src/ipc/Socket.cpp index a9ad5fa..008781d 100644 --- a/src/ipc/Socket.cpp +++ b/src/ipc/Socket.cpp @@ -84,41 +84,42 @@ void CIPCSocket::initialize() { } bool CIPCSocket::mainThreadParseRequest() { + + if (!m_bRequestReady) + return false; - if (!m_bRequestReady) - return false; + std::string copy = m_szRequest; - std::string copy = m_szRequest; + // now we can work on the copy - // now we can work on the copy + if (copy == "") + return false; - if (copy == "") - return false; - Debug::log(LOG, "Received a request: %s", copy.c_str()); + Debug::log(LOG, "Received a request: %s", copy.c_str()); - // parse - if (copy.find("wallpaper") == 0 || copy.find("preload") == 0 || copy.find("unload") == 0) { - g_pConfigManager->parseError = ""; // reset parse error + // parse + if (copy.find("wallpaper") == 0 || copy.find("preload") == 0 || copy.find("unload") == 0) { + g_pConfigManager->parseError = ""; // reset parse error - g_pConfigManager->parseKeyword(copy.substr(0, copy.find_first_of(' ')), copy.substr(copy.find_first_of(' ') + 1)); + g_pConfigManager->parseKeyword(copy.substr(0, copy.find_first_of(' ')), copy.substr(copy.find_first_of(' ') + 1)); - if (!g_pConfigManager->parseError.empty()) { - m_szReply = g_pConfigManager->parseError; - m_bReplyReady = true; - m_bRequestReady = false; - return false; + if (!g_pConfigManager->parseError.empty()) { + m_szReply = g_pConfigManager->parseError; + m_bReplyReady = true; + m_bRequestReady = false; + return false; + } + } + else { + m_szReply = "invalid command"; + m_bReplyReady = true; + m_bRequestReady = false; + return false; } - } - else { - m_szReply = "invalid command"; + + m_szReply = "ok"; m_bReplyReady = true; m_bRequestReady = false; - return false; - } - - m_szReply = "ok"; - m_bReplyReady = true; - m_bRequestReady = false; - return true; + return true; } From b9dce51024b838d756c4502099ef288b48dc9a5f Mon Sep 17 00:00:00 2001 From: cylian charbonnier Date: Mon, 23 Oct 2023 19:02:25 +0200 Subject: [PATCH 3/3] format with clang-format --- src/ipc/Socket.cpp | 85 +++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 43 deletions(-) diff --git a/src/ipc/Socket.cpp b/src/ipc/Socket.cpp index 008781d..bd065b1 100644 --- a/src/ipc/Socket.cpp +++ b/src/ipc/Socket.cpp @@ -1,21 +1,21 @@ #include "Socket.hpp" #include "../Hyprpaper.hpp" -#include +#include #include #include #include +#include #include #include #include #include #include -#include void CIPCSocket::initialize() { - std::thread([&]() { + std::thread([&]() { const auto SOCKET = socket(AF_UNIX, SOCK_STREAM, 0); - + if (SOCKET < 0) { Debug::log(ERR, "Couldn't start the hyprpaper Socket. (1) IPC will not work."); return; @@ -45,46 +45,46 @@ void CIPCSocket::initialize() { char readBuffer[1024] = {0}; - Debug::log(LOG, "hyprpaper socket started at %s (fd: %i)", socketPath.c_str(), SOCKET); - while (1) { - const auto ACCEPTEDCONNECTION = accept(SOCKET, (sockaddr*)&clientAddress, &clientSize); - if (ACCEPTEDCONNECTION < 0) { - Debug::log(ERR, "Couldn't listen on the hyprpaper Socket. (3) IPC will not work."); - break; - }else { - do { - Debug::log(LOG, "Accepted incoming socket connection request on fd %i", ACCEPTEDCONNECTION); - std::lock_guard lg(g_pHyprpaper->m_mtTickMutex); - - auto messageSize = read(ACCEPTEDCONNECTION, readBuffer, 1024); - readBuffer[messageSize == 1024 ? 1023 : messageSize] = '\0'; - if (messageSize == 0) - break; - std::string request(readBuffer); - - m_szRequest = request; - m_bRequestReady = true; - - g_pHyprpaper->tick(true); - while (!m_bReplyReady) { // wait for Hyprpaper to finish processing the request - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - } - write(ACCEPTEDCONNECTION, m_szReply.c_str(), m_szReply.length()); - m_bReplyReady = false; - m_szReply = ""; - - }while(1); - Debug::log(LOG, "Closing Accepted Connection"); - close(ACCEPTEDCONNECTION); - } - } - - close(SOCKET); - }).detach(); + Debug::log(LOG, "hyprpaper socket started at %s (fd: %i)", socketPath.c_str(), SOCKET); + while (1) { + const auto ACCEPTEDCONNECTION = accept(SOCKET, (sockaddr*)&clientAddress, &clientSize); + if (ACCEPTEDCONNECTION < 0) { + Debug::log(ERR, "Couldn't listen on the hyprpaper Socket. (3) IPC will not work."); + break; + } else { + do { + Debug::log(LOG, "Accepted incoming socket connection request on fd %i", ACCEPTEDCONNECTION); + std::lock_guard lg(g_pHyprpaper->m_mtTickMutex); + + auto messageSize = read(ACCEPTEDCONNECTION, readBuffer, 1024); + readBuffer[messageSize == 1024 ? 1023 : messageSize] = '\0'; + if (messageSize == 0) + break; + std::string request(readBuffer); + + m_szRequest = request; + m_bRequestReady = true; + + g_pHyprpaper->tick(true); + while (!m_bReplyReady) { // wait for Hyprpaper to finish processing the request + std::this_thread::sleep_for(std::chrono::milliseconds(1)); + } + write(ACCEPTEDCONNECTION, m_szReply.c_str(), m_szReply.length()); + m_bReplyReady = false; + m_szReply = ""; + + } while (1); + Debug::log(LOG, "Closing Accepted Connection"); + close(ACCEPTEDCONNECTION); + } + } + + close(SOCKET); + }).detach(); } bool CIPCSocket::mainThreadParseRequest() { - + if (!m_bRequestReady) return false; @@ -109,8 +109,7 @@ bool CIPCSocket::mainThreadParseRequest() { m_bRequestReady = false; return false; } - } - else { + } else { m_szReply = "invalid command"; m_bReplyReady = true; m_bRequestReady = false;