-
Notifications
You must be signed in to change notification settings - Fork 2
/
Server.cpp
169 lines (164 loc) · 5.2 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
#include <iostream>
#include <unistd.h>
#include <netinet/in.h>
#include <sys/epoll.h>
#include <sys/socket.h>
const unsigned int BUFFER_SIZE = 1024, DEFAULT_PORT = 8080, MAX_EVENTS = 64;
int getNumericValue(std::string value)
{
bool error = 0;
int result = 0;
for (char c : value) {
if (c < 48 || c > 57)
{
error = 1;
break;
}
result *= 10;
result += c - 48;
}
if (error)
{
std::cout << "Wrong input: expected correct digit, not " << value << '\n';
exit(0);
}
return result;
}
void closeAll(int epoll, int curSocket)
{
if (close(epoll) == -1)
{
std::cout << "Error occurred while closing the epoll\n";
exit(-1);
}
if (close(curSocket) == -1)
{
std::cout << "Error occurred while closing the socket\n";
exit(-1);
}
}
int main(int argc, char* argv[], char *envp[])
{
if (argc > 2)
{
std::cout << "Wrong input: just port expected\n";
return 0;
}
int curSocket = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (curSocket == -1)
{
std::cout << "Unable to create a socket\n";
return -1;
}
int epoll = epoll_create1(0);
if (epoll == -1)
{
std::cout << "Unable to create epoll\n";
return -1;
}
int port = DEFAULT_PORT;
if (argc == 2)
{
port = getNumericValue(argv[1]);
}
struct epoll_event curClient, clients[MAX_EVENTS];
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(port);
address.sin_addr.s_addr = INADDR_ANY;
if (bind(curSocket, (sockaddr*) &address, sizeof(address)) == -1)
{
std::cout << "Unable to bind to socket\n";
return -1;
}
if (listen(curSocket, 5) == -1)
{
std::cout << "Unable to set listening for socket\n";
return -1;
}
curClient.events = EPOLLIN;
curClient.data.fd = curSocket;
if (epoll_ctl(epoll, EPOLL_CTL_ADD, curSocket, &curClient) == -1)
{
std::cout << "Unable to add to epoll\n";
closeAll(epoll, curSocket);
return -1;
}
std::cout << "Server is up\n";
while (true)
{
int clientsAmount = epoll_wait(epoll, clients, MAX_EVENTS, -1);
if (clientsAmount == -1)
{
std::cout << "Error occurred during epoll waiting\n";
closeAll(epoll, curSocket);
return -1;
}
for (unsigned int i = 0; i < clientsAmount; ++i)
{
if (clients[i].data.fd == curSocket)
{
struct sockaddr_in client;
socklen_t len = sizeof(client);
int code = accept(curSocket, (sockaddr*) &client, &len);
if (code == -1)
{
std::cout << "Error during receiving request from client\n";
continue;
}
std::cout << "Client \"" << code << "\" connected\n";
curClient.events = EPOLLIN;
curClient.data.fd = code;
if (epoll_ctl(epoll, EPOLL_CTL_ADD, code, &curClient) == -1)
{
std::cout << "Unable to add to epoll\n";
closeAll(epoll, curSocket);
return -1;
}
} else {
char request[BUFFER_SIZE] = {};
int requestSize = recv(clients[i].data.fd, request, BUFFER_SIZE, 0);
if (requestSize == -1)
{
std::cout << "Error occurred while receiving a message\n";
if (epoll_ctl(epoll, EPOLL_CTL_DEL, clients[i].data.fd, nullptr) == -1)
{
std::cout << "Unable to delete from epoll\n";
}
if (close(clients[i].data.fd) == -1)
{
std::cout << "Error occurred on socket closing\n";
}
break;
} else if (requestSize == 0) {
break;
}
int responseSize = 0, tries = 5;
while (responseSize < requestSize)
{
int sended = send(clients[i].data.fd, request + responseSize, requestSize - responseSize, 0);
if (sended == -1)
{
std::cout << "Error occurred on sending the message\n";
--tries;
}
responseSize += sended;
if (tries == 0)
{
std::cout << "Unable to send the message\n";
if (epoll_ctl(epoll, EPOLL_CTL_DEL, clients[i].data.fd, nullptr) == -1)
{
std::cout << "Unable to delete from epoll\n";
}
if (close(clients[i].data.fd) == -1)
{
std::cout << "Error occurred on socket closing\n";
}
break;
}
}
}
}
}
closeAll(epoll, curSocket);
}