diff --git a/src/Common.h b/src/Common.h index 3ed0ea5..184c85e 100644 --- a/src/Common.h +++ b/src/Common.h @@ -85,6 +85,29 @@ namespace Utils return buf; } + template + size_t RetrySocketOp(Func func, size_t retries, const char* socketOp) + { + ssize_t ret; + size_t tries = 0; + do + { + ret = func(); + if (ret < 0) + { + // Error + LOG("RetrySocketOp: " << socketOp << " failed with " << ret); + } + else + { + return ret; + } + tries++; + } while (tries < retries); + LOG("RetrySocketOp: Failed after " << retries << "tries"); + return ret; + } + inline std::vector Split(const std::string& str, char delim) { std::stringstream strstr(str); diff --git a/src/Workspaces.cpp b/src/Workspaces.cpp index 619342b..83b72ee 100644 --- a/src/Workspaces.cpp +++ b/src/Workspaces.cpp @@ -90,21 +90,49 @@ namespace Workspaces addr.sun_family = AF_UNIX; memcpy(addr.sun_path, socketPath.c_str(), sizeof(addr.sun_path)); - int ret = connect(hyprSocket, (sockaddr*)&addr, SUN_LEN(&addr)); - ASSERT(ret >= 0, "Couldn't connect to hyprland socket"); - ssize_t written = write(hyprSocket, arg.c_str(), arg.size()); - ASSERT(written >= 0, "Couldn't write to socket"); + int ret = Utils::RetrySocketOp( + [&]() + { + return connect(hyprSocket, (sockaddr*)&addr, SUN_LEN(&addr)); + }, + 5, "connect"); + if (ret < 0) + { + LOG("Couldn't connect to Hyprland socket."); + return ""; + } + + ssize_t written = Utils::RetrySocketOp( + [&]() + { + return write(hyprSocket, arg.c_str(), arg.size()); + }, + 5, "write"); + if (written < 0) + { + LOG("Couldn't write to Hyprland socket."); + return ""; + } char buf[2056]; std::string res; while (true) { - ssize_t bytesRead = read(hyprSocket, buf, sizeof(buf)); + ssize_t bytesRead = Utils::RetrySocketOp( + [&]() + { + return read(hyprSocket, buf, sizeof(buf)); + }, + 5, "read"); if (bytesRead == 0) { break; } - ASSERT(bytesRead >= 0, "Couldn't read"); + if (bytesRead < 0) + { + LOG("Couldn't read from Hyprland socket."); + return ""; + } res += std::string(buf, bytesRead); } close(hyprSocket);