This commit is contained in:
Nicholas Orlowsky 2023-10-17 13:16:31 -04:00
parent 38359bd957
commit d6eb46d310
No known key found for this signature in database
GPG key ID: BE7DF0188A405E2B
20 changed files with 178 additions and 1020295 deletions

View file

@ -16,6 +16,7 @@ using namespace std;
void log_request_and_response(http_request& req, unique_ptr<http_response>& resp);
constexpr int default_port = 80;
constexpr int max_worker_threads = 128;
int active_threads = 0;
mutex mtx;
@ -26,15 +27,15 @@ void handle_client(anthracite_socket s, file_backend& fb)
http_request req(s);
unique_ptr<http_response> resp = fb.handle_request(req);
log_request_and_response(req, resp);
s.send_message(resp->to_string());
string header = resp->header_to_string();
s.send_message(header);
s.send_message(resp->content());
resp.reset();
s.close_conn();
{
std::lock_guard<std::mutex> lock(mtx);
active_threads--;
}
cv.notify_one();
}
@ -48,14 +49,14 @@ int main(int argc, char** argv)
cout << "Initializing Anthracite" << endl;
anthracite_socket s(port_number);
file_backend fb(true);
file_backend fb(argc > 2 ? argv[2] : "./www");
cout << "Initialization Complete" << endl;
cout << "Listening for HTTP connections on port " << port_number << endl;
for (;;) {
while (true) {
s.wait_for_conn();
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [] { return active_threads < 20; });
cv.wait(lock, [] { return active_threads < max_worker_threads; });
active_threads++;
thread(handle_client, s, ref(fb)).detach();
}