-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
111 lines (105 loc) · 3.4 KB
/
main.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoumni <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 16:35:36 by mkarim #+# #+# */
/* Updated: 2023/09/26 09:05:59 by mmoumni ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
#include <fstream>
#include <string>
#include <signal.h>
#include "./includes/configfile.hpp"
#include "./includes/socket.hpp"
#include "./includes/request.hpp"
void sigpipe_handler(int signum)
{
(void)signum;
}
void server_loop(std::vector<Socket> & sockets, std::vector<pfd> & pfds, ConfigFile & configFile, std::map<int, ConnectSocket> Connections)
{
signal(SIGPIPE, sigpipe_handler);
while (1)
{
poll(&pfds[0], pfds.size(), 0);
for (size_t i = 0; i < pfds.size(); i++)
{
if (i >= sockets.size() && i < pfds.size() && getTimeOfNow() - Connections[pfds[i].fd].timeOut > 5)
{
checkTimeOut(pfds, Connections, configFile, i);
i--;
}
if (pfds[i].revents & POLLIN)
{
if (i < sockets.size() && pfds[i].fd == sockets[i].getSocketId())
pollin(pfds, sockets, Connections, i);
else
{
Connections[pfds[i].fd].timeOut = getTimeOfNow();
Connections[pfds[i].fd].readRequest(configFile);
if (Connections[pfds[i].fd].closed)
{
if (Connections[pfds[i].fd]._response.response_string.size())
sendError(pfds[i].fd, Connections[pfds[i].fd]._response.response_string);
closeConnection(pfds, Connections, i);
i--;
}
}
}
if (pfds[i].revents & POLLOUT)
{
pollout(pfds, Connections, i);
if (Connections[pfds[i].fd].closed)
{
if (Connections[pfds[i].fd]._response.response_string.size())
sendError(pfds[i].fd, Connections[pfds[i].fd]._response.response_string);
closeConnection(pfds, Connections, i);
i--;
}
else if ((Connections[pfds[i].fd].conType && Connections[pfds[i].fd].ReadAvailble))
{
closeConnection(pfds, Connections, i);
i--;
}
}
if (pfds[i].revents & (POLLERR | POLLHUP))
{
pollErrHup(pfds, Connections, i);
i--;
}
}
}
}
void start_server(std::string & _config)
{
ConfigFile configFile;
std::vector<Socket> sockets;
std::vector<pfd> pfds;
std::map<int, ConnectSocket> Connections;
try
{
_config = read_file(_config);
configFile = start_parse_config_file(_config);
sockets = create_sockets(configFile);
listenSocket(sockets);
pfds = create_pfd(sockets);
}
catch(const std::exception & e)
{
std::cerr << e.what() << '\n';
exit(EXIT_FAILURE);
}
server_loop(sockets, pfds, configFile, Connections);
}
int main(int argc, char **argv)
{
if (argc > 2)
return (std::cout << "INVALID ARGUMENTS" << std::endl, 1);
std::string config_file = (argc == 2 ? argv[1] : "./conf-files/config.conf");
start_server(config_file);
return (0);
}