This commit is contained in:
Nicholas Orlowsky 2023-10-20 00:27:35 -04:00
parent f1868ceea5
commit dea773366e
No known key found for this signature in database
GPG key ID: BE7DF0188A405E2B
8 changed files with 103 additions and 24 deletions

View file

@ -5,6 +5,7 @@
#include <iostream>
#include <mutex>
#include <netinet/in.h>
#include <span>
#include <sstream>
#include <sys/socket.h>
#include <thread>
@ -16,14 +17,10 @@ void log_request_and_response(http_request& req, std::unique_ptr<http_response>&
constexpr int default_port = 80;
constexpr int max_worker_threads = 128;
int active_threads = 0;
std::mutex mtx;
std::condition_variable cv;
void handle_client(anthracite_socket s, file_backend& fb)
void handle_client(anthracite_socket s, backend& b, std::mutex& thread_wait_mutex, std::condition_variable& thread_wait_condvar, int& active_threads)
{
http_request req(s);
std::unique_ptr<http_response> resp = fb.handle_request(req);
std::unique_ptr<http_response> resp = b.handle_request(req);
log_request_and_response(req, resp);
std::string header = resp->header_to_string();
s.send_message(header);
@ -31,32 +28,37 @@ void handle_client(anthracite_socket s, file_backend& fb)
resp.reset();
s.close_conn();
{
std::lock_guard<std::mutex> lock(mtx);
std::lock_guard<std::mutex> lock(thread_wait_mutex);
active_threads--;
}
cv.notify_one();
thread_wait_condvar.notify_one();
}
int main(int argc, char** argv)
{
auto args = std::span(argv, size_t(argc));
int port_number = default_port;
if (argc > 1) {
port_number = atoi(argv[1]);
port_number = atoi(args[1]);
}
std::cout << "Initializing Anthracite" << std::endl;
anthracite_socket s(port_number);
file_backend fb(argc > 2 ? argv[2] : "./www");
file_backend fb(argc > 2 ? args[2] : "./www");
std::cout << "Initialization Complete" << std::endl;
std::cout << "Listening for HTTP connections on port " << port_number << std::endl;
int active_threads = 0;
std::mutex thread_wait_mutex;
std::condition_variable thread_wait_condvar;
while (true) {
s.wait_for_conn();
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock, [] { return active_threads < max_worker_threads; });
std::unique_lock<std::mutex> lock(thread_wait_mutex);
thread_wait_condvar.wait(lock, [active_threads] { return active_threads < max_worker_threads; });
active_threads++;
std::thread(handle_client, s, std::ref(fb)).detach();
std::thread(handle_client, s, std::ref(fb), std::ref(thread_wait_mutex), std::ref(thread_wait_condvar), std::ref(active_threads)).detach();
}
exit(0);
@ -64,5 +66,5 @@ int main(int argc, char** argv)
void log_request_and_response(http_request& req, std::unique_ptr<http_response>& resp)
{
std::cout << "[" << resp->status_code() << " " + http_status_map.find(resp->status_code())->second + "] " + req.client_ip() + " " + http_reverse_method_map.find(req.method())->second + " " + req.path() << std::endl;
std::cout << "[" << resp->status_code() << " " + http_status_map.find(resp->status_code())->second + "] " + req.client_ip() + " " + http_reverse_method_map.find(req.method())->second + " " + req.path() << std::endl;
}