-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
206 lines (153 loc) · 5.33 KB
/
main.cpp
File metadata and controls
206 lines (153 loc) · 5.33 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "Includes.h"
#include "Client.h"
std::ofstream out_to_file("output.txt");
Cache * cache;
int my_server_socket;
std::vector<Client*> clients;
void init_my_server_socket(unsigned short server_port) {
my_server_socket = socket(PF_INET, SOCK_STREAM, 0);
int one = 1;
setsockopt(my_server_socket, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
if (my_server_socket <= 0) {
perror("Error while creating serverSocket");
exit(EXIT_FAILURE);
}
struct sockaddr_in server_address;
server_address.sin_family = PF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(server_port);
if (bind(my_server_socket, (struct sockaddr *)&server_address, sizeof(server_address))) {
perror("Error while binding");
exit(EXIT_FAILURE);
}
if (listen(my_server_socket, 1024)) {
perror("Error while listen()");
exit(EXIT_FAILURE);
}
}
void accept_incoming_connection() {
struct sockaddr_in client_address;
int address_size = sizeof(sockaddr_in);
int client_socket = accept(my_server_socket, (struct sockaddr *)&client_address, (socklen_t *)&address_size);
if (client_socket <= 0) {
perror("accept");
exit(EXIT_FAILURE);
}
Client * new_client = new Client(client_socket, cache);
clients.push_back(new_client);
}
void delete_finished_clients() {
std::vector<Client*> rest_clients;
for (auto client : clients) {
if (client->is_closed()) {
delete client;
}
else {
int http_socket = client->get_http_socket();
if (-1 != http_socket && client->is_closed_http_socket()) {
fprintf(stderr, "Close http socket\n");
if (close(http_socket)) {
perror("close");
}
client->set_http_socket(-1);
}
rest_clients.push_back(client);
}
}
clients = rest_clients;
}
void start_main_loop() {
bool flag_execute = true;
for ( ; flag_execute ; ) {
fd_set fds_read;
fd_set fds_write;
FD_ZERO(&fds_read);
FD_ZERO(&fds_write);
int max_fd = 0;
FD_SET(my_server_socket, &fds_read);
max_fd = my_server_socket;
for (auto client : clients) {
FD_SET(client->get_my_socket(), &fds_read);
if (client->is_have_data_for_client()) {
FD_SET(client->get_my_socket(), &fds_write);
}
max_fd = std::max(max_fd, client->get_my_socket());
if (client->is_received_get_request() && -1 != client->get_http_socket() && !client->is_closed_http_socket()) {
FD_SET(client->get_http_socket(), &fds_read);
if (client->is_have_data_for_server()) {
FD_SET(client->get_http_socket(), &fds_write);
}
max_fd = std::max(max_fd, client->get_http_socket());
}
}
int activity = select(max_fd + 1, &fds_read, &fds_write, NULL, NULL);
if (activity <= 0) {
perror("select");
continue;
}
if (FD_ISSET(my_server_socket, &fds_read)) {
fprintf(stderr, "Have incoming client connection\n");
accept_incoming_connection();
}
for (auto client : clients) {
if (client->can_recv_from_client() && FD_ISSET(client->get_my_socket(), &fds_read)) {
fprintf(stderr, "Have data from client\n");
client->receive_request_from_client();
}
if (client->is_have_data_for_server() && FD_ISSET(client->get_http_socket(), &fds_write)) {
client->send_request_to_server();
}
if (client->can_recv_from_server() && FD_ISSET(client->get_http_socket(), &fds_read)) {
client->receive_server_response();
}
if (client->is_have_data_for_client() && FD_ISSET(client->get_my_socket(), &fds_write)) {
client->send_answer_to_client();
}
}
for (auto client : clients) {
if (client->can_be_closed()) {
fprintf(stderr, "Can to close client\n");
client->set_closed_correct();
}
}
delete_finished_clients();
}
}
void signal_handle(int sig) {
perror("perror");
fprintf(stderr, "Exit with code %d\n", sig);
delete cache;
for (auto client : clients) {
delete client;
}
fprintf(stderr, "Close my server socket\n");
close(my_server_socket);
exit(EXIT_SUCCESS);
}
int main(int argc, char *argv[]) {
signal(SIGINT, signal_handle);
signal(SIGKILL, signal_handle);
signal(SIGTERM, signal_handle);
signal(SIGSEGV, signal_handle);
// todo GET_OPT!!!
if (2 != argc) {
perror("Wrong number of arguments");
exit(EXIT_FAILURE);
}
unsigned short server_port = (unsigned short)atoi(argv[1]);
if (0 == server_port) {
perror("Wrong port for listening");
exit(EXIT_FAILURE);
}
init_my_server_socket(server_port);
cache = new Cache();
char bad_request[100] = "Bad request\n\0";
start_main_loop();
delete cache;
// close clients
for (auto client : clients) {
delete client;
}
close(my_server_socket);
return 0;
}