summaryrefslogtreecommitdiff
path: root/IPC.cpp
diff options
context:
space:
mode:
authorBossCode45 <human.cyborg42@gmail.com>2023-09-29 00:55:29 +1300
committerBossCode45 <human.cyborg42@gmail.com>2023-11-12 15:03:19 +1300
commit5dacf6d6d24eab71994e02b6b3c4fc6f6e309958 (patch)
tree204b77d98627d390eb8d6c1a50b767605221d6ec /IPC.cpp
parent6b5c246a431dcaff119833724137f0716fa2a002 (diff)
downloadYATwm-5dacf6d6d24eab71994e02b6b3c4fc6f6e309958.tar.gz
YATwm-5dacf6d6d24eab71994e02b6b3c4fc6f6e309958.zip
feat: IPC works kinda
Still quite buggy, but first version
Diffstat (limited to 'IPC.cpp')
-rw-r--r--IPC.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/IPC.cpp b/IPC.cpp
new file mode 100644
index 0000000..215a15c
--- /dev/null
+++ b/IPC.cpp
@@ -0,0 +1,80 @@
+#include "IPC.h"
+
+#include <cstring>
+#include <sys/socket.h>
+#include <iostream>
+#include <unistd.h>
+
+using std::cout, std::endl;
+
+static const char* path = "/tmp/YATwm.sock";
+
+IPCModule::IPCModule(CommandsModule& commandsModule, Config& cfg, Globals& globals)
+ :commandsModule(commandsModule),
+ cfg(cfg),
+ globals(globals)
+{
+ sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
+ address.sun_family = AF_UNIX;
+ strcpy(address.sun_path, path);
+ unlink(address.sun_path);
+ len = strlen(address.sun_path) + sizeof(address.sun_family);
+
+ if(bind(sockfd, (sockaddr*)&address, len) == -1)
+ {
+ cout << "ERROR" << endl;
+ }
+ cout << "SOCKETED" << endl;
+}
+
+void IPCModule::doListen()
+{
+ if(listen(sockfd, 1) != 0)
+ {
+ cout << "ERROR 2" << endl;
+ return;
+ }
+ if(first)
+ {
+ first = false;
+ return;
+ }
+ cout << "DOLISTEN" << endl;
+ unsigned int socklen = 0;
+ sockaddr_un remote;
+ int newsock = accept(sockfd, (sockaddr*)&remote, &socklen);
+ cout << "LISTENING" << endl;
+ char buffer[256];
+ memset(buffer, 0, 256);
+ read(newsock, buffer, 256);
+ std::string command(buffer);
+ while(command[command.size() - 1] == 0 || command[command.size() - 1] == '\n')
+ command = command.substr(0, command.size() - 1);
+ //cout << '"' << command << '"' << endl;
+ try
+ {
+ commandsModule.runCommand(command);
+ }
+ catch(Err e)
+ {
+ cout << e.code << " " << e.message << endl;
+ }
+ char* message = "RAN COMMAND";
+ send(newsock, message, strlen(message), 0);
+ cout << "RAN COMMAND" << endl;
+ shutdown(newsock, SHUT_RDWR);
+ close(newsock);
+}
+
+void IPCModule::quitIPC()
+{
+ close(sockfd);
+ cout << path << endl;
+}
+
+int IPCModule::getFD()
+{
+ if(sockfd > 0)
+ return sockfd;
+ return -1;
+}