ipc: Allow multiple read from IPC + some refactor (#102)

* Allow multiple read from IPC

* bring back old indentation

* format with clang-format

---------

Co-authored-by: cylian charbonnier <cylian.charbonnier1@gmail.com>
This commit is contained in:
Charbonnier Cylian 2023-10-23 23:39:38 +02:00 committed by GitHub
parent 1c009491b5
commit d6856adaff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 37 deletions

View File

@ -1,16 +1,16 @@
#include "Socket.hpp" #include "Socket.hpp"
#include "../Hyprpaper.hpp" #include "../Hyprpaper.hpp"
#include <netinet/in.h> #include <cerrno>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <netinet/in.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/un.h> #include <sys/un.h>
#include <unistd.h> #include <unistd.h>
#include <cerrno>
void CIPCSocket::initialize() { void CIPCSocket::initialize() {
std::thread([&]() { std::thread([&]() {
@ -46,42 +46,37 @@ void CIPCSocket::initialize() {
char readBuffer[1024] = {0}; char readBuffer[1024] = {0};
Debug::log(LOG, "hyprpaper socket started at %s (fd: %i)", socketPath.c_str(), SOCKET); Debug::log(LOG, "hyprpaper socket started at %s (fd: %i)", socketPath.c_str(), SOCKET);
while (1) {
while(1) {
const auto ACCEPTEDCONNECTION = accept(SOCKET, (sockaddr*)&clientAddress, &clientSize); const auto ACCEPTEDCONNECTION = accept(SOCKET, (sockaddr*)&clientAddress, &clientSize);
Debug::log(LOG, "Accepted incoming socket connection request on fd %i", ACCEPTEDCONNECTION);
if (ACCEPTEDCONNECTION < 0) { if (ACCEPTEDCONNECTION < 0) {
Debug::log(ERR, "Couldn't listen on the hyprpaper Socket. (3) IPC will not work."); Debug::log(ERR, "Couldn't listen on the hyprpaper Socket. (3) IPC will not work.");
break; break;
} } else {
do {
Debug::log(LOG, "Accepted incoming socket connection request on fd %i", ACCEPTEDCONNECTION);
std::lock_guard<std::mutex> lg(g_pHyprpaper->m_mtTickMutex); std::lock_guard<std::mutex> lg(g_pHyprpaper->m_mtTickMutex);
auto messageSize = read(ACCEPTEDCONNECTION, readBuffer, 1024); auto messageSize = read(ACCEPTEDCONNECTION, readBuffer, 1024);
readBuffer[messageSize == 1024 ? 1023 : messageSize] = '\0'; readBuffer[messageSize == 1024 ? 1023 : messageSize] = '\0';
if (messageSize == 0)
break;
std::string request(readBuffer); std::string request(readBuffer);
m_szRequest = request; m_szRequest = request;
m_bRequestReady = true; m_bRequestReady = true;
g_pHyprpaper->tick(true); g_pHyprpaper->tick(true);
while (!m_bReplyReady) { // wait for Hyprpaper to finish processing the request
while (1) {
if (m_bReplyReady)
break;
std::this_thread::sleep_for(std::chrono::milliseconds(1)); std::this_thread::sleep_for(std::chrono::milliseconds(1));
} }
write(ACCEPTEDCONNECTION, m_szReply.c_str(), m_szReply.length()); write(ACCEPTEDCONNECTION, m_szReply.c_str(), m_szReply.length());
close(ACCEPTEDCONNECTION);
m_bReplyReady = false; m_bReplyReady = false;
m_szReply = ""; m_szReply = "";
} while (1);
Debug::log(LOG, "Closing Accepted Connection");
close(ACCEPTEDCONNECTION);
}
} }
close(SOCKET); close(SOCKET);
@ -114,8 +109,7 @@ bool CIPCSocket::mainThreadParseRequest() {
m_bRequestReady = false; m_bRequestReady = false;
return false; return false;
} }
} } else {
else {
m_szReply = "invalid command"; m_szReply = "invalid command";
m_bReplyReady = true; m_bReplyReady = true;
m_bRequestReady = false; m_bRequestReady = false;