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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
#include "libsock/server/client.hpp"
#include "libsock/server/clients.hpp"
#include "libsock/server/server.hpp"
#include "libsock/types.hpp"
#include "misc/FileDescriptor.hpp"
#include <algorithm>
#include <arpa/inet.h>
#include <cstring>
#include <format>
#include <memory>
#include <optional>
#include <sys/socket.h>
using namespace LibSock::Server;
Client::Client(SP<Server> server, SP<Clients> clients, bool track, bool oneShot)
: m_track(track)
, m_oneShot(oneShot) {
pServer = std::move(server);
pClients = std::move(clients);
if (!pClients)
throw std::runtime_error("server:client: ClientManager doesn't exist");
if (!pServer)
throw std::runtime_error("server:client: Server doesn't exist");
m_sockfd = std::make_shared<LibSock::CFileDescriptor>(accept(pServer->getSocket()->get(), reinterpret_cast<sockaddr *>(&m_addr), &m_addrLen)); // if this is in the init list, it will run before
// m_addrLen, so it won't work :/
if (!m_sockfd->isValid())
throw std::runtime_error("server:client: Failed to create socket");
m_ip.resize(INET_ADDRSTRLEN);
inet_ntop(AF_INET, &m_addr.sin_addr, &m_ip[0], INET_ADDRSTRLEN);
m_ip.resize(strlen(m_ip.c_str()));
m_port = ntohs(m_addr.sin_port);
}
Client::~Client() {
#ifdef DEBUG
onDisconnect();
#endif
m_sockfd.reset();
}
void Client::recvLoop() {
while (true) {
auto recvData = read();
if (!recvData->good)
break;
recvData->sanitize();
if (recvData->isEmpty())
continue;
pClients->m_vDatas.push_back({recvData->data, self});
}
}
bool SRecvData::isEmpty() const {
return data.empty() ||
std::ranges::all_of(data, [](char c) { return std::isspace(c); });
}
void SRecvData::sanitize() {
if (!data.empty() && data.back() == '\n')
data.pop_back();
data.erase(std::remove(data.begin(), data.end(), asciiEscape),
data.end()); // don't accept ASCII code, might mess up terminal
// // NOLINT
if (const auto start = data.find_first_not_of(" \t\r\n");
start != std::string::npos) {
const auto end = data.find_last_not_of(" \t\r\n");
data = data.substr(start, end - start + 1);
} else
data.clear();
}
UP<SRecvData> Client::read(std::optional<std::function<std::any(const SRecvData &)>> cb) {
auto recvData = std::make_unique<SRecvData>();
recvData->data.resize(recvData->size);
ssize_t size = recv(m_sockfd->get(), &recvData->data[0], recvData->size, 0);
if (size < 0 || size == 0) {
recvData->good = false;
if (cb)
(*cb)(*recvData);
} else {
recvData->data.resize(size);
recvData->good = true;
}
#ifdef DEBUG
onRecv(*recvData);
#endif
m_szReading.reset();
return recvData;
}
UP<SRecvData> Client::read(const std::string &msg, std::optional<std::function<std::any(const SRecvData &)>> cb) {
write("{}", msg);
m_szReading = msg;
return read(cb);
}
void Client::run() {
for (const auto &chat : pClients->getDatas()) {
write(chat.msg);
recvLoop();
pClients->kick(self);
}
}
bool Client::isValid() {
if (!m_sockfd->isValid())
return false;
int err = 0;
socklen_t size = sizeof(err);
return getsockopt(m_sockfd->get(), SOL_SOCKET, SO_ERROR, &err, &size) >=
0 &&
err == 0;
}
bool Client::write(const std::string &msg, std::optional<std::function<std::any(const SRecvData &)>> cb) {
const auto r = write("{}", msg);
if (cb) {
auto recvData = std::make_unique<SRecvData>();
recvData->data = msg;
recvData->good = r;
(*cb)(*recvData);
}
return r;
}
const std::string &Client::getIp() const { return m_ip; }
template <typename... Args>
bool Client::write(std::format_string<Args...> fmt, Args &&...args) {
std::string msg = std::format(fmt, std::forward<Args>(args)...);
if (m_szReading) {
msg.insert(0, "\r");
msg.append(*m_szReading);
}
if (send(m_sockfd->get(), msg.c_str(), msg.size(), 0) < 0) {
// cb somehow
return false;
}
return true;
}
|