summaryrefslogtreecommitdiff
path: root/src/core/session.hpp
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2025-06-12 21:41:41 +0800
committerlittleblack111 <littleblack11111@gmail.com>2025-06-13 20:22:51 +0800
commit6f7b994684032060f0a2d7af82f4f3572293b8d6 (patch)
tree793d328cbcd6761365267550b55db45aee9b21fc /src/core/session.hpp
parent6a51ab4525c59e9fe2bdbd92493019b0a98c6b94 (diff)
internal!: organized internal structure, using cmake now
Diffstat (limited to 'src/core/session.hpp')
-rw-r--r--src/core/session.hpp88
1 files changed, 88 insertions, 0 deletions
diff --git a/src/core/session.hpp b/src/core/session.hpp
new file mode 100644
index 0000000..ca951d1
--- /dev/null
+++ b/src/core/session.hpp
@@ -0,0 +1,88 @@
+#pragma once
+
+#include "../misc/FileDescriptor.hpp"
+#include "chatManager.hpp"
+#include "../debug/format.hpp"
+#include <format>
+#include <memory>
+#include <netinet/in.h>
+#include <optional>
+#include <string>
+#include <sys/socket.h>
+#include <thread>
+
+class CSession {
+ public:
+ CSession();
+ ~CSession();
+
+ struct SRecvData {
+ std::string data;
+ const size_t size = 1024;
+ bool good = false;
+
+ bool isEmpty() const;
+ void sanitize();
+
+ static constexpr char asciiEscape = 0x1B; // 
+ };
+
+ const std::string &getName() const;
+ const std::string &getIp() const;
+ bool isAdmin() const;
+
+ std::unique_ptr<SRecvData> read();
+ std::unique_ptr<SRecvData> read(const std::string &msg, bool bypassDeaf = false);
+ bool write(const std::string &msg);
+ template <typename... Args>
+ bool write(std::format_string<Args...> fmt, Args &&...args);
+ template <typename... Args>
+ bool write(eFormatType type, std::format_string<Args...> fmt, Args &&...args);
+ bool writeChat(const CChatManager::SMessage &msg);
+
+ bool isMuted() const;
+ void setMuted(bool mute);
+ bool isDeaf() const;
+ void setDeaf(bool deaf);
+
+ void run();
+
+ bool isValid();
+
+ private:
+ constexpr static char m_adminIps[1][INET_ADDRSTRLEN] = {"127.0.0.1"};
+
+ enum eEventType : std::uint8_t {
+ READ,
+ WRITE
+ };
+
+ std::weak_ptr<CSession> self;
+ std::shared_ptr<CFileDescriptor> m_sockfd;
+ sockaddr_in m_addr;
+ socklen_t m_addrLen = sizeof(m_addr);
+ std::string m_name;
+ std::string m_ip;
+ int m_port;
+ bool m_isAdmin = false;
+
+ std::optional<std::string> m_szReading = std::nullopt;
+ bool m_bMuted = false;
+ bool m_bDeaf = true; // init as true as we don't want anything during registerSession
+
+ void recvLoop();
+ bool registerSession();
+
+#ifdef DEBUG
+ void onConnect();
+ void onDisconnect();
+#endif
+ void onErrno(eEventType);
+#ifdef DEBUG
+ void onRecv(const SRecvData &data);
+ void onSend(const std::string &msg);
+#endif
+
+ friend class CSessionManager;
+ friend void registerCommands();
+};