Added multithreading
This commit is contained in:
51
router.cpp
51
router.cpp
@@ -1,5 +1,6 @@
|
|||||||
#include "router.hpp"
|
#include "router.hpp"
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
|
#include <mutex>
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
|
|
||||||
Router::Router(int port) {
|
Router::Router(int port) {
|
||||||
@@ -14,13 +15,61 @@ int Router::Start() {
|
|||||||
if (err != 0)
|
if (err != 0)
|
||||||
return err;
|
return err;
|
||||||
|
|
||||||
|
StartThreadLoop();
|
||||||
|
|
||||||
err = listen(m_socket, 5);
|
err = listen(m_socket, 5);
|
||||||
if (err != 0)
|
if (err != 0)
|
||||||
return err;
|
return err;
|
||||||
|
|
||||||
std::vector<std::byte> buffer(1024);
|
|
||||||
while (true) {
|
while (true) {
|
||||||
int client = accept(m_socket, nullptr, nullptr);
|
int client = accept(m_socket, nullptr, nullptr);
|
||||||
|
QueueClient(client);
|
||||||
|
}
|
||||||
|
|
||||||
|
StopThreadLoop();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Router::QueueClient(int fd) {
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(m_mutex);
|
||||||
|
m_clients.push(fd);
|
||||||
|
}
|
||||||
|
m_cond.notify_one();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Router::StartThreadLoop() {
|
||||||
|
const uint32_t numThreads = std::thread::hardware_concurrency();
|
||||||
|
for (auto i = 0; i < numThreads; ++i) {
|
||||||
|
m_threads.emplace_back(std::thread(&Router::ThreadLoop, this));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Router::StopThreadLoop() {
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(m_mutex);
|
||||||
|
m_shouldTerminate = true;
|
||||||
|
}
|
||||||
|
m_cond.notify_all();
|
||||||
|
for (std::thread &active_thread : m_threads) {
|
||||||
|
active_thread.join();
|
||||||
|
}
|
||||||
|
m_threads.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Router::ThreadLoop() {
|
||||||
|
std::vector<std::byte> buffer(1024);
|
||||||
|
while (true) {
|
||||||
|
int client;
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(m_mutex);
|
||||||
|
m_cond.wait(lock,
|
||||||
|
[this] { return !m_clients.empty() || m_shouldTerminate; });
|
||||||
|
if (m_shouldTerminate)
|
||||||
|
return;
|
||||||
|
client = m_clients.front();
|
||||||
|
m_clients.pop();
|
||||||
|
}
|
||||||
|
|
||||||
int read = recv(client, buffer.data(), buffer.size(), 0);
|
int read = recv(client, buffer.data(), buffer.size(), 0);
|
||||||
Request req(buffer);
|
Request req(buffer);
|
||||||
Response res = Route(req);
|
Response res = Route(req);
|
||||||
|
|||||||
14
router.hpp
14
router.hpp
@@ -3,9 +3,12 @@
|
|||||||
|
|
||||||
#include "request.hpp"
|
#include "request.hpp"
|
||||||
#include "response.hpp"
|
#include "response.hpp"
|
||||||
|
#include <condition_variable>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <map>
|
#include <map>
|
||||||
|
#include <mutex>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#include <queue>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class Router {
|
class Router {
|
||||||
@@ -15,6 +18,17 @@ private:
|
|||||||
sockaddr_in m_address;
|
sockaddr_in m_address;
|
||||||
Response Route(Request req);
|
Response Route(Request req);
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::mutex m_mutex;
|
||||||
|
std::condition_variable m_cond;
|
||||||
|
std::vector<std::thread> m_threads;
|
||||||
|
std::queue<int> m_clients;
|
||||||
|
bool m_shouldTerminate = false;
|
||||||
|
void StartThreadLoop();
|
||||||
|
void ThreadLoop();
|
||||||
|
void QueueClient(int client);
|
||||||
|
void StopThreadLoop();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Router(int port);
|
Router(int port);
|
||||||
void Handle(std::string pathPattern,
|
void Handle(std::string pathPattern,
|
||||||
|
|||||||
Reference in New Issue
Block a user