summaryrefslogtreecommitdiff
path: root/src/session.hpp
blob: c3aecc154e151f1ee27d097417ee4fafaf98f125 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#pragma once

#include "FileDescriptor.hpp"
#include "chatManager.hpp"
#include "format.hpp"
#include <memory>
#include <netinet/in.h>
#include <string>
#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();
	};

	const std::string &getName() const;
	const std::string &getIp() const;
	void			   setSelf(std::unique_ptr<std::pair<std::jthread, std::shared_ptr<CSession>>> self);

	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::unique_ptr<std::pair<std::jthread, std::shared_ptr<CSession>>> self;
	Hyprutils::OS::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;

	bool m_isReading = false;
	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;
};