Add correctly shutting down server and stopping threads

This commit is contained in:
Pablu23
2024-11-13 16:49:49 +01:00
parent a3a5280f17
commit c18d5f993d
8 changed files with 61 additions and 35 deletions

View File

@@ -18,9 +18,11 @@ Router::Router(int port) {
m_address.sin_family = AF_INET;
m_address.sin_port = htons(port);
m_address.sin_addr.s_addr = INADDR_ANY;
m_running = false;
}
int Router::start() {
m_running = true;
int err = bind(m_socket, (struct sockaddr *)&m_address, sizeof(m_address));
if (err != 0)
return err;
@@ -31,14 +33,17 @@ int Router::start() {
if (err != 0)
return err;
while (true) {
while (m_running.load()) {
int client = accept(m_socket, nullptr, nullptr);
queue_client(client);
}
stop_thread_loop();
return 0;
}
void Router::stop() { m_running.store(false); }
void Router::queue_client(int fd) {
{
std::unique_lock<std::mutex> lock(m_mutex);
@@ -83,7 +88,8 @@ void Router::thread_loop() {
recv(client, buffer.data(), buffer.size(), 0);
Request req(buffer);
Response res = Route(req);
res.send(client);
auto payload = res.compile();
send(client, payload.data(), payload.size(), 0);
shutdown(client, SHUT_WR);
while (recv(client, buffer.data(), buffer.size(), 0) > 0) {