summaryrefslogtreecommitdiff
path: root/src/session.hpp
blob: 9133176161a01567a5fd144c212ed2915cab1c11 (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
#pragma once

#include "FileDescriptor.hpp"
#include "format.hpp"
#include <memory>
#include <string>
#include <thread>

class CSession {
  public:
	CSession(Hyprutils::OS::CFileDescriptor m_sockfd);
	~CSession();

	struct SRecvData {
		char	data[1024] = {0};
		ssize_t dataSize   = sizeof(data);
		bool	good	   = true;
	};

	std::string getName();
	bool		isValid();
	SRecvData	read();
	SRecvData	read(const std::string &msg);
	bool		write(const std::string &msg, eFormatType type);
	template <typename... Args>
	bool write(eFormatType type, std::format_string<Args...> fmt, Args &&...args);
	void run();
	void setSelf(std::pair<std::jthread, std::shared_ptr<CSession>> *self);

  private:
	std::pair<std::jthread, std::shared_ptr<CSession>> *self;
	enum eEventType : std::uint8_t {
		READ,
		WRITE
	};
	Hyprutils::OS::CFileDescriptor m_sockfd;
	std::string					   m_name;
	void						   recvLoop();
	bool						   registerSession();
	void						   onConnect();
	void						   onDisconnect();
	void						   onErrno(eEventType);
	void						   onRecv(const SRecvData &data);
	void						   onSend(const std::string &msg);
	void						   onShutdown();
	friend class CSessionManager;
};