-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): threading epoll IO thread pool
- Loading branch information
Showing
17 changed files
with
428 additions
and
261 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.