-
Notifications
You must be signed in to change notification settings - Fork 23
/
serve.cpp
177 lines (145 loc) · 4.34 KB
/
serve.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
#include <arpa/inet.h>
#include <errno.h>
#include <netdb.h>
#include <netinet/in.h>
#include <signal.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <chrono>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <mutex>
#include <queue>
#include <sstream>
#include <string>
#include <thread>
#include <vector>
#define PORT "3000"
#define BACKLOG 10
std::string toHex(const int num) {
std::stringstream stream;
stream << std::hex << num;
return stream.str();
}
class client_info {
using timepoint = std::chrono::time_point<std::chrono::steady_clock>;
public:
int sockfd;
timepoint last_updated;
client_info(int fd, timepoint t) : sockfd(fd), last_updated(t){};
};
std::vector<int> clientfds;
std::mutex fd_queue_mutex;
int sendall(const int sockfd, const std::string& data) {
size_t bytes_sent{}, result{};
while (bytes_sent < data.size()) {
result = send(sockfd, data.data() + bytes_sent, data.size() - bytes_sent, MSG_NOSIGNAL);
if (result <= 0) break;
bytes_sent -= result;
}
return result;
}
bool inline process(const int sockfd) {
static const std::string data = "1\r\nf\r\n";
return sendall(sockfd, data) > 0;
}
void keep_connections_open() {
using namespace std::chrono;
std::queue<client_info> clients;
while (true) {
const auto now = steady_clock::now();
if (!clientfds.empty()) {
std::scoped_lock lock(fd_queue_mutex);
for (auto& clientfd : clientfds) {
clients.emplace(clientfd, now);
}
std::cerr << "Added " << clientfds.size() << " client to queue. Total client count: " << clients.size() << ".\n";
clientfds.clear();
}
if (clients.empty()) {
std::this_thread::sleep_for(10s);
continue;
}
if (clients.front().last_updated + 10s > now) {
std::this_thread::sleep_until(clients.front().last_updated + 10s);
}
bool processed = process(clients.front().sockfd);
if (processed) {
clients.emplace(clients.front().sockfd, now);
} else {
close(clients.front().sockfd);
}
clients.pop();
}
}
std::string readFile(std::string filename) {
std::ifstream file(filename, std::ios::binary);
std::string content((std::istreambuf_iterator<char>(file)),
(std::istreambuf_iterator<char>()));
return content;
}
std::string buildResponse() {
std::string response =
"HTTP/1.1 200 OK\r\n"
"Content-Type: image/jpeg\r\n"
"Cache-Control: no-store\r\n"
"X-Accel-Buffering: no\r\n"
"Transfer-Encoding: chunked\r\n"
"\r\n";
auto image = readFile("public/lisa.jpg");
response += toHex(image.size());
response += "\r\n";
response += image;
response += "\r\n";
return response;
}
int main() {
std::cout << "Starting server..\n";
int status, serverfd;
addrinfo hints, *res;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ((status = getaddrinfo(NULL, PORT, &hints, &res))) {
std::cout << "getaddrinfo error " << gai_strerror(status) << std::endl;
exit(1);
}
if ((serverfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) {
perror("server: socket");
exit(1);
}
if (bind(serverfd, res->ai_addr, res->ai_addrlen) == -1) {
perror("server: bind");
exit(1);
}
if (listen(serverfd, BACKLOG) == -1) {
perror("server: listen");
exit(1);
}
std::thread t(keep_connections_open);
auto response = buildResponse();
sockaddr_storage client_addr;
socklen_t sin_size;
while (true) {
sin_size = sizeof(client_addr);
int clientfd = accept(serverfd, reinterpret_cast<sockaddr*>(&client_addr), &sin_size);
if (clientfd == -1) {
perror("server: accept");
continue;
}
if (sendall(clientfd, response) > 0) {
std::scoped_lock lock(fd_queue_mutex);
clientfds.push_back(clientfd);
} else {
close(clientfd);
}
}
freeaddrinfo(res);
return 0;
}