summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/chatServer.cpp32
-rw-r--r--src/chatServer.hpp15
-rw-r--r--src/main.cpp16
-rw-r--r--src/sessionManager.cpp2
-rw-r--r--src/sessionManager.hpp2
5 files changed, 59 insertions, 8 deletions
diff --git a/src/chatServer.cpp b/src/chatServer.cpp
new file mode 100644
index 0000000..06f2021
--- /dev/null
+++ b/src/chatServer.cpp
@@ -0,0 +1,32 @@
+#include "chatServer.hpp"
+#include "server.hpp"
+#include "sessionManager.hpp"
+
+CChatServer::CChatServer() = default;
+
+CChatServer::~CChatServer() {
+ cleanup();
+}
+
+void CChatServer::cleanup() {
+ g_pServer.reset();
+ g_pSessionManager.reset();
+}
+
+void CChatServer::initManagers() {
+ g_pServer = std::make_unique<CServer>(8080);
+ g_pSessionManager = std::make_shared<CSessionManager>();
+}
+
+
+void CChatServer::start() {
+ initManagers();
+
+ // will await while waiting for accept() to return a new client
+ std::jthread sessionThread = std::jthread(&CSessionManager::enterLoop, g_pSessionManager);
+ sessionThread.detach();
+
+ while (true) {
+ }
+}
+
diff --git a/src/chatServer.hpp b/src/chatServer.hpp
new file mode 100644
index 0000000..7109593
--- /dev/null
+++ b/src/chatServer.hpp
@@ -0,0 +1,15 @@
+#include <memory>
+
+class CChatServer {
+public:
+ CChatServer();
+ ~CChatServer();
+ void start();
+ void cleanup();
+private:
+ void initManagers();
+ void initServer();
+};
+
+inline std::unique_ptr<CChatServer> g_pChatServer;
+
diff --git a/src/main.cpp b/src/main.cpp
index e7d9780..8b800dd 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,9 +1,13 @@
-#include "server.hpp"
-#include "sessionManager.hpp"
+#include "chatServer.hpp"
+// TODO: ARGC, ARGV. the CServer ctor should take a port number
int main() {
- g_pServer = std::make_unique<CServer>(8080);
- g_pSessionManager = std::make_unique<CSessionManager>();
- g_pSessionManager->enterLoop();
- return 0;
+ g_pChatServer = std::make_unique<CChatServer>();
+ g_pChatServer->start();
+
+ // died
+ g_pChatServer->cleanup();
+ g_pChatServer.reset();
+
+ return 0;
}
diff --git a/src/sessionManager.cpp b/src/sessionManager.cpp
index 290b8c6..e449567 100644
--- a/src/sessionManager.cpp
+++ b/src/sessionManager.cpp
@@ -13,7 +13,7 @@ CSessionManager::~CSessionManager() {
}
std::pair<std::jthread, std::shared_ptr<CSession>> *CSessionManager::newSession(Hyprutils::OS::CFileDescriptor sockfd) {
- std::shared_ptr session = std::make_shared<CSession>(std::move(sockfd));
+ std::shared_ptr session = std::make_unique<CSession>(std::move(sockfd));
const auto instance = &m_vSessions.emplace_back(std::jthread(&CSession::run, session), std::move(session));
instance->second->setSelf(instance);
return instance;
diff --git a/src/sessionManager.hpp b/src/sessionManager.hpp
index 0b447b5..9d1aae8 100644
--- a/src/sessionManager.hpp
+++ b/src/sessionManager.hpp
@@ -20,4 +20,4 @@ class CSessionManager {
private:
std::vector<std::pair<std::jthread, std::shared_ptr<CSession>>> m_vSessions;
};
-inline std::unique_ptr<CSessionManager> g_pSessionManager;
+inline std::shared_ptr<CSessionManager> g_pSessionManager;