Skip to content

Commit

Permalink
feat(core): threading epoll IO thread pool
Browse files Browse the repository at this point in the history
  • Loading branch information
scyth3-c committed Aug 25, 2024
1 parent 0ed7115 commit 73ee053
Show file tree
Hide file tree
Showing 17 changed files with 428 additions and 261 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ file(GLOB_RECURSE SOURCES
)

file(GLOB_RECURSE HEADERS
"${CMAKE_SOURCE_DIR}/include/vibe/*.h")

"${CMAKE_SOURCE_DIR}/include/vibe/*.h"
)
add_library(vibe STATIC ${SOURCES} ${HEADERS})

install(TARGETS vibe
Expand Down
213 changes: 0 additions & 213 deletions include/vibe/main_process.h

This file was deleted.

61 changes: 61 additions & 0 deletions include/vibe/request/io.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// Created by owl on 19/08/24.
//

#ifndef IO_H
#define IO_H

#include <memory>
#include <sys/epoll.h>
#include <netinet/in.h>
#include <unordered_map>
#include <atomic>

#include "../util/enums.h"
#include "../util/parameter_proccess.h"

#include "../routes.hpp"
#include "request.hpp"
#include "../util/nterminal.h"
#include "../sockets.h"
#include "../threading/thread_pool.h"

using std::make_shared, std::vector, std::unique_ptr;

using RoutesMap = std::unordered_map<string, std::unique_ptr<listen_routes>>;
/*
* RequestIO for Server socket class, if you want implement other Server, you should create other RequestIO for the implementation
*/
class RequestIO {

private:
shared_ptr<std::vector<epoll_event>> events;
shared_ptr<RoutesMap> routes;
unique_ptr<int> file_descriptor;
unique_ptr<int> epoll_fd;
shared_ptr<Server> connection;

shared_ptr<threading::ThreadPool> thread_pool_;

size_t threads_{4};

void Process(int, epoll_event&) const;
void ProcessFileDescriptor(int, int) const;

public:

RequestIO(const shared_ptr<vector<epoll_event>>&,
const shared_ptr<RoutesMap> &,
int &,
int &,
const shared_ptr<Server>&);


void Dispatch(int id, epoll_event &event) const;
void SetThreads(size_t size);

static bool TimeGuard(const RoutesMap::const_iterator & itr);
static void ExecuteRoute(const shared_ptr<Server> &instance, const shared_ptr<RoutesMap> &routes);
};

#endif //IO_H
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <algorithm>
#include <memory>
#include "parameters.hpp"
#include "sysprocess.h"
#include "../util/sysprocess.h"

using std::string;
using std::vector;
Expand Down
84 changes: 84 additions & 0 deletions include/vibe/request/router_epoll.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//
// Created by scythe on 5/07/23.
//

#ifndef MAIN_PROCESS_H
#define MAIN_PROCESS_H

#include <stdexcept>
#include <memory>
#include <sys/epoll.h>
#include <unistd.h>
#include <unordered_map>

#include "../util/enums.h"
#include "../util/parameter_proccess.h"

#include "../routes.hpp"
#include "../util/nterminal.h"
#include "io.h"

constexpr int BUFFER = enums::neo::eSize::BUFFER;
constexpr int SESSION = enums::neo::eSize::SESSION;
constexpr int INIT_MAX_EVENTS = 10;

namespace workers {
using RoutesMap = std::unordered_map<string, std::unique_ptr<listen_routes>>;

template<class T>
class RouterEpoll {

int epoll_fd;
shared_ptr<std::vector<epoll_event>> events;
shared_ptr<T> connection;
shared_ptr<RequestIO> request_t;

public:

explicit RouterEpoll(const shared_ptr<T> &conn) :
epoll_fd(epoll_create1(0)),
events(make_shared<vector<epoll_event>>(INIT_MAX_EVENTS)),
connection(conn)
{}

auto getMainProcess(const shared_ptr<RoutesMap> &_routes){

connection->on();
int file_descriptor = connection->getDescription();

if(Server::setNonblocking(file_descriptor) == MG_ERROR)
close(file_descriptor);

if (epoll_fd == -1)
throw std::range_error(VB_EPOLL_RANGE);


epoll_event event{};
event.events = EPOLLIN;
event.data.fd = file_descriptor;

if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, file_descriptor, &event) == VB_NVALUE) {
close(epoll_fd);
throw std::range_error(VB_EPOLL_CTL);
}

request_t = make_shared<RequestIO>(events, _routes, file_descriptor, epoll_fd, connection);

while (static_cast<bool>(enums::neo::eStatus::START)){
try {
const int notice = epoll_wait(epoll_fd, events->data(), INIT_MAX_EVENTS, VB_NVALUE);
request_t->Dispatch(notice, event);
}
catch(const std::exception& e) {
terminal(e.what());
close(epoll_fd);
if(close(file_descriptor) < VB_OK)
throw std::range_error(VB_MAIN_THREAD);
}
}
}
};
}


#endif //MAIN_PROCESS_H
2 changes: 1 addition & 1 deletion include/vibe/routes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <future>


#include "util/request.hpp"
#include "request/request.hpp"

#include "util/basic_render.h"
#include "util/cpp_reader.h"
Expand Down
Loading

0 comments on commit 73ee053

Please sign in to comment.