Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multiple read from IPC + some refactor #102

Merged
merged 3 commits into from
Oct 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 31 additions & 37 deletions src/ipc/Socket.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
#include "Socket.hpp"
#include "../Hyprpaper.hpp"

#include <netinet/in.h>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/un.h>
#include <unistd.h>
#include <cerrno>

void CIPCSocket::initialize() {
std::thread([&]() {
std::thread([&]() {
const auto SOCKET = socket(AF_UNIX, SOCK_STREAM, 0);

if (SOCKET < 0) {
Expand Down Expand Up @@ -46,50 +46,45 @@ void CIPCSocket::initialize() {
char readBuffer[1024] = {0};

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);

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;
} else {
do {
Debug::log(LOG, "Accepted incoming socket connection request on fd %i", ACCEPTEDCONNECTION);
std::lock_guard<std::mutex> 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);
}

std::lock_guard<std::mutex> 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();
}

bool CIPCSocket::mainThreadParseRequest() {

if (!m_bRequestReady)
return false;

Expand All @@ -114,8 +109,7 @@ bool CIPCSocket::mainThreadParseRequest() {
m_bRequestReady = false;
return false;
}
}
else {
} else {
m_szReply = "invalid command";
m_bReplyReady = true;
m_bRequestReady = false;
Expand Down