-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.cpp
369 lines (334 loc) · 9.46 KB
/
Server.cpp
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Server.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rchahban <rchahban@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/29 22:17:18 by rchahban #+# #+# */
/* Updated: 2024/07/23 07:55:59 by rchahban ### ########.fr */
/* */
/* ************************************************************************** */
#include "./Server.hpp"
Server::Server() : port(3000), password("1234"), fd(-1)
{
}
Server::Server(int port, std::string& password)
{
this->port = port;
this->password = password;
this->fd = -1;
}
Server::Server(const Server& original) : port(original.port), password(original.password), fd(-1), clients(original.clients)
{
signal = original.signal;
}
Server& Server::operator=(const Server& original)
{
if (this != &original)
{
this->port = original.port;
this->password = original.password;
this->fd = original.fd;
this->clients = original.clients;
this->signal = false;
}
return *this;
}
Server::~Server()
{
for (std::vector<Client*>::iterator it = clients.begin(); it != clients.end(); ++it) {
delete *it;
}
clients.clear();
for (std::vector<Channel*>::iterator it = channels.begin(); it != channels.end(); ++it) {
delete *it;
}
channels.clear();
}
bool Server::signal = false;
void Server::handleSignal(int signum)
{
(void)signum;
signal = true;
}
void Server::displayChannels()
{
if (!this->channels.size())
{
std::cout << "No channels yet." << std::endl;
return ;
}
std::cout << "Channels:" << std::endl;
for (size_t x = 0; x < this->channels.size(); x++)
{
for (size_t y = 0; y < this->channels[x]->getMembers().size(); y++)
{
std::vector<Client *> members_ = this->channels[x]->getMembers();
std::cout << "Channel: " << this->channels[x]->getName() << " has user (" << members_[y]->getFd() << ")" << std::endl;
}
}
}
void Server::displayClients()
{
if (!this->clients.size())
{
std::cout << "No clients yet." << std::endl;
return ;
}
std::cout << "Clients:" << std::endl;
for (size_t x = 0; x < this->clients.size(); x++)
{
std::cout << "Client <" << this->clients[x]->getUsername() << "> | FD <" << this->clients[x]->getFd() << ">" << std::endl;
}
}
void Server::displayInvitedChannels()
{
if (!this->channels.size())
return ;
std::cout << "Invited Channels:" << std::endl;
for (size_t x = 0; x < this->clients.size(); x++)
{
for (size_t y = 0; y < this->clients[x]->getInvitedChannels().size(); y++)
{
std::vector<Channel *> invitedChannels = this->clients[x]->getInvitedChannels();
std::cout << "Client <" << this->clients[x]->getNickname() << "> is invited to Channel <" << invitedChannels[y]->getName() << ">" << std::endl;
}
}
}
std::string trim(std::string &str)
{
std::string whitespace = " \t";
size_t strBegin = str.find_first_not_of(whitespace);
if (strBegin == std::string::npos)
return "";
size_t strEnd = str.find_last_not_of(whitespace);
size_t strRange = strEnd - strBegin + 1;
return str.substr(strBegin, strRange);
}
void Server::init() {
std::cout << CYAN << "Initializing server..." << std::endl;
std::cout << GREEN <<"Server initialized" << RESET << std::endl;
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == -1) {
std::cout << RED << "Error: Couldn't create unbound socket!" << std::endl;
return ;
}
else
{
setFd(serverSocket);
sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(this->getPort());
serverAddress.sin_addr.s_addr = INADDR_ANY;
int option_value = 1;
if(fcntl(this->fd, F_SETFL, O_NONBLOCK) == -1)
{
std::cout << RED << "Error: Couldn't set socket file descriptor mode to non-blocking!" << std::endl;
return ;
}
if (setsockopt(this->fd, SOL_SOCKET, SO_REUSEADDR, &option_value, sizeof(option_value)) == -1)
{
std::cout << RED << "Error: Couldn't set the socket options!" << std::endl;
return ;
}
else if (bind(this->fd, (struct sockaddr*)&serverAddress, sizeof(serverAddress)) == -1)
{
std::cout << RED << "Error: Couldn't bind a name to the socket!" << std::endl;
return ;
}
else if (listen(this->fd, SOMAXCONN) == -1)
{
std::cout << RED << "Error: Couldn't listen for socket connections!" << std::endl;
return ;
}
struct pollfd pfd;
pfd.fd = this->fd;
pfd.events = POLLIN;
this->fds.push_back(pfd);
while(!signal)
{
if((poll(&fds[0],fds.size(),-1) == -1))
throw(std::runtime_error("poll() faild"));
for (unsigned int x = 0; x < this->fds.size(); x++)
{
if (fds[x].revents && POLLIN)
{
if (fds[x].fd == this->fd)
{
struct sockaddr_in client_addr;
socklen_t addr_size;
addr_size = sizeof(client_addr);
int clientSocket = accept(this->fd, (sockaddr *)&client_addr, &addr_size);
if (clientSocket == -1)
{
std::cout << "accept() failed" << std::endl;
return;
}
if(fcntl(clientSocket, F_SETFL, O_NONBLOCK) == -1)
{
std::cout << RED << "Error: Couldn't set socket file descriptor mode to non-blocking!" << std::endl;
return ;
}
struct pollfd client_fd;
client_fd.fd = clientSocket;
client_fd.events = POLLIN;
client_fd.revents = 0;
Client *client = new Client();
client->setFd(clientSocket);
client->setIpAddress(inet_ntoa(client_addr.sin_addr));
AddToClients(*client);
this->fds.push_back(client_fd);
std::cout << YELLOW <<"New connection! " << "Client fd <" << clientSocket << ">" << std::endl;
}
else
{
char buff[1024] = {0};
memset(buff, 0, sizeof(buff));
ssize_t receivedBytes = recv(fds[x].fd, buff, sizeof(buff), 0);
if(receivedBytes <= 0)
{
std::cout << "Client <" << fds[x].fd << "> has disconnected!" << std::endl;
removeClient(fds[x].fd);
close(fds[x].fd);
removeFd(fds[x].fd);
}
else
{
std::string buff_str(buff);
cmd_parser(buff_str, fds[x].fd);
}
}
}
}
}
removeClients();
removeChannels();
removePollFds();
close(this->fd);
}
}
// GETTERS AND SETTERS
int Server::getPort() {
return this->port;
}
void Server::setPort(int _port) {
this->port = _port;
}
std::string Server::getPassword() {
return this->password;
}
void Server::setPassword(std::string _password) {
this->password = _password;
}
Client* Server::getClient(int _fd) {
for (std::vector<Client *>::iterator it = this->clients.begin(); it != this->clients.end(); ++it)
{
if ((*it)->getFd() == _fd)
return (*it);
}
return NULL;
}
int Server::getFd() {
return this->fd;
}
void Server::setFd(int _fd) {
this->fd = _fd;
}
void Server::AddToClients(Client& client) {
this->clients.push_back(&client);
}
Channel* Server::getChannelByName(std::string name) {
for (std::vector<Channel *>::iterator it = this->channels.begin(); it != this->channels.end(); ++it)
{
if ((*it)->getName() == name)
return *it;
}
return NULL;
}
Client* Server::getClientByName(std::string name) {
for (std::vector<Client *>::iterator it = this->clients.begin(); it != this->clients.end(); ++it)
{
if ((*it)->getNickname() == name)
return (*it);
}
return NULL;
}
void Server::removeClientFromChannels(int _fd)
{
for (size_t i = 0; i < channels.size(); i++)
{
for (size_t j = 0; j < channels[i]->getMembers().size(); j++)
{
if (channels[i]->getMembers()[j]->getFd() == _fd)
{
channels[i]->getMembers().erase(channels[i]->getMembers().begin() + j);
break;
}
}
}
}
void Server::removeClient(int _fd) {
std::vector<Channel *> Channels = this->channels;
for (size_t i = 0; i < channels.size(); ++i)
{
if(clientAlreadyInChannel(_fd, channels[i]->getName()))
channels[i]->removeMember(*getClient(_fd));
}
for(size_t i = 0; i < clients.size(); ++i)
{
if(clients[i]->getFd() == _fd)
{
clients.erase(clients.begin() + i);
}
}
}
bool Server::checkpass(std::string _pass)
{
if (_pass == this->getPassword())
return true;
return false;
}
void Server::sendResponse(std::string response, int fd)
{
if(send(fd, response.c_str(), response.size(), 0) == -1)
std::cerr << "Response send() failed" << std::endl;
}
void Server::removeFd(int _fd)
{
for (std::vector<struct pollfd>::iterator it = this->fds.begin(); it != this->fds.end(); ++it)
{
if ((*it).fd == _fd)
{
this->fds.erase(it);
break;
}
}
}
void Server::removeClients() {
for (size_t i = 0; i < clients.size();) {
for (size_t j = 0; j < clients[i]->getInvitedChannels().size();)
{
delete clients[i]->getInvitedChannels()[j];
clients[i]->getInvitedChannels().erase(clients[i]->getInvitedChannels().begin() + j);
}
delete clients[i];
clients.erase(clients.begin() + i);
}
}
void Server::removeChannels()
{
for (size_t i = 0; i < channels.size(); )
{
delete channels[i];
this->channels.erase(channels.begin() + i);
}
}
void Server::removePollFds()
{
for (int x = 0; fds.size(); )
{
close(fds[x].fd);
this->fds.erase(fds.begin() + x);
}
}