Repository navigation
Expand file tree
/
Copy pathClient.cpp
More file actions
137 lines (121 loc) · 3.32 KB
/
Copy pathClient.cpp
File metadata and controls
137 lines (121 loc) · 3.32 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Client.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kzhen-cl <marvin@d42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/28 10:43:41 by kzhen-cl #+# #+# */
/* Updated: 2025/08/28 10:43:41 by kzhen-cl ### ########.fr */
/* */
/* ************************************************************************** */
#include "Client.hpp"
const ErrorFdException Client::EFE;
Client::Client(const int &fd) : _fd(fd), _registerLevel(0)
{
if (_fd < 0)
throw (Client::EFE);
for (int i = 0; i < CHPERCL; ++i)
*(_channels + i) = NULL;
}
Client::~Client(void)
{
if (_fd >= 0)
close(_fd);
}
char Client::operator+=(Channel *const ch)
{
int i = -1;
while (++i < CHPERCL)
{
if (*(_channels + i) == ch)
return (0);
if (!*(_channels + i))
break;
}
*(_channels + i) = ch;
return (1);
}
char Client::operator-=(Channel *const ch)
{
for (int i = 0; i < CHPERCL; ++i)
{
if (*(_channels + i) == ch)
{
*(_channels + i) = NULL;
return (1);
}
}
return (0);
}
bool Client::readFromSocket(void)
{
char buffer[512]; // RFC 1459: Max 512 octets incluant CR-LF
ssize_t bytesRead;
if (_buffer.length() > 512)
{
std::cerr << "Buffer overflow attempt from client " << _fd << std::endl;
return false;
}
memset(buffer, 0, sizeof(buffer));
bytesRead = recv(_fd, buffer, sizeof(buffer) - 2, 0); // -2 pour CR-LF
// Lu
if (bytesRead > 0)
{
buffer[bytesRead] = '\0';
appendToBuffer(buffer);
return true;
}
// Client disconnected
else if (bytesRead == 0)
{
std::cout << "Client fd=" << _fd << " déconnecté." << std::endl;
return false;
}
else
{
// Erreur lecture
std::cerr << "Erreur de lecture du client fd=" << _fd << std::endl;
return false;
}
}
void Client::appendToBuffer(const std::string &data)
{
_buffer += data;
}
bool Client::sendMessage(const std::string &message)
{
if (message.empty())
return true;
ssize_t bytesSent = send(_fd, message.c_str(), message.length(), MSG_NOSIGNAL);
// Erreur d'envoi
if (bytesSent < 0)
{
std::cerr << "Erreur lors de l'envoi au client fd=" << _fd << std::endl;
return false;
}
else if (static_cast<size_t>(bytesSent) < message.length())
{
std::cerr << "Envoi partiel au client fd=" << _fd << std::endl;
return false;
}
return true;
}
char Client::cannotJoinNChannels(int n) const
{
if (n > CHPERCL)
return (1);
int i = -1;
while (++i < CHPERCL)
if (*(_channels + i) == NULL)
break ;
return (n > (CHPERCL - i));
}
std::string Client::getSheet(void) const
{
return ((std::string)"+--- USER INFO ---+\n| ["
+ getNick() + "] \n"
+ "|\t- Real Name : " + getRealname() + "\n"
+ "|\t- Username : " + getUsername() + "\n"
+ "+--- USER INFO ---+\n");
}