forked from bbotfatimezzahra/Webserv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunServer.cpp
More file actions
183 lines (164 loc) · 4.02 KB
/
Copy pathrunServer.cpp
File metadata and controls
183 lines (164 loc) · 4.02 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
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
#include "includes/WebServer.hpp"
int nsig = 0;
void signal_handler(int n){
nsig = 1;
throw MyError("exit");
(void) n;
}
int set_nonBlock(int fd)
{
//getting current status flags of the socket
int flags = fcntl(fd, F_GETFL);
if (flags < 0)
return 0;
//adding the O_NONBLOCK flag to the flags by bit orring
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK | FD_CLOEXEC) == -1)
return 0;
return 1;
}
ServerConfig *WebServer::isServer(int fd)
{
std::vector<ServerConfig>::iterator it;
for ( it = servs.begin(); it != servs.end(); ++it)
{
if (it->getFd() == fd)
return &(*it);
}
return NULL;
}
Connection *WebServer::isClient(int fd)
{
std::vector<Connection>::iterator it;
for ( it = _cons.begin(); it != _cons.end(); ++it)
{
if (it->getClient() == fd)
return &(*it);
}
return NULL;
}
void WebServer::closeAll()
{
int fd;
std::vector<ServerConfig>::iterator it;
for ( it = servs.begin(); it != servs.end(); ++it)
{
fd = it->getFd();
if (fd != -1)
{
epoll_ctl(_epoll_fd, EPOLL_CTL_DEL,fd, NULL);
close(fd);
}
}
std::vector<Connection>::iterator bt;
for ( bt = _cons.begin(); bt != _cons.end(); ++bt)
bt->closeClient(_epoll_fd, "");
if(_epoll_fd != -2)
close(_epoll_fd);
}
void WebServer::checkTimeouts() {
Request::t_status s;
std::vector<Connection>::iterator it = _cons.begin();
while(it != _cons.end()) {
s = it->getRequest().getStatus();
if ((s != Request::COMPLETE && s != Request::ERROR && (time(NULL) - it->getLastActive() >= RECV_TIMEOUT))
|| ((s == Request::COMPLETE || s == Request::ERROR) && time(NULL) - it->getLastActive() >= SEND_TIMEOUT))
{
it->getRequest().setError(408, "Epoll Timeout");
it->sendResponse(_epoll_fd);
it->closeClient(_epoll_fd, "Timeout");
it = _cons.erase(it);
continue;
}
it++;
}
}
void WebServer::runServer()
{
_epoll_fd = epoll_create(1);
int r = 0;
//starting the servers sockets and adding them to epoll
for (std::vector<ServerConfig>::iterator it = servs.begin(); it != servs.end(); ++it)
{
if (!it->start())
{
std::cerr << "Server "<< it->getIp()<<":"<< it->getPort() << " Failed"<<std::endl;
continue;
}
if (it->addToEpoll(_epoll_fd))
r = 1;
else
std::cerr << "Server "<< it->getIp()<<":"<< it->getPort() << " Failed"<<std::endl;
}
if (!r)
{
closeAll();
throw MyError("All Server Initializations Failed Program Cannot Proceed");
}
int fd;
int maxevents = 70;
struct epoll_event events[maxevents];
ServerConfig *server;
Connection *client;
while(1)
{
signal(SIGINT,signal_handler);
r = epoll_wait(_epoll_fd, events, maxevents,0);
if (r == -1)
{
closeAll();
throw MyError(strerror(errno));
}
for (int i = 0; i < r ; i++)
{
fd = events[i].data.fd;
server = isServer(fd);
client = isClient(fd);
if (server)
{
Connection tmp(*server, _epoll_fd);
if (tmp.getClient() != -2)
_cons.push_back(tmp);
}
else if (events[i].events & EPOLLIN && client )
{
client->parseRequest(_epoll_fd);
client->updateLastActive();
if (client->getSendStatus() == Connection::FINISHED)
_cons.erase(std::find(_cons.begin(), _cons.end(), *client));
}
else if (events[i].events & EPOLLOUT && client)
{
client->sendResponse(_epoll_fd);
client->updateLastActive();
if (client->getSendStatus() == Connection::FINISHED)
{
client->closeClient(_epoll_fd,"");
_cons.erase(std::find(_cons.begin(), _cons.end(), *client));
}
}
else
{
if (client)
{
std::cerr << "Client epoll event" << client->getClient()<<std::endl;
client->closeClient(_epoll_fd,"Error Event on Client");
_cons.erase(std::find(_cons.begin(), _cons.end(), *client));
}
else if (server)
{
epoll_ctl(_epoll_fd, EPOLL_CTL_DEL,fd, NULL);
close(fd);
std::cerr << "Server Epoll event" << server->getFd()<<std::endl;
}
// else
// {
// epoll_ctl(_epoll_fd, EPOLL_CTL_DEL,fd, NULL);
// close(fd);
// std::cerr << "Rogue fd" << fd <<std::endl;
// }
}
}
checkTimeouts();
}
return ;
}