/
isachkov
/
crypto_server
Обзор
Документация
Войти
/
isachkov
/
crypto_server
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
include/crypto_chat/executor.hpp
55 строк
1 KB
Ivan Sachkov
CryptoServer
15 май 2024, 20:26
15 май 2024, 20:26
9de0f1f
Код
Авторство
О чём код?
#pragma once #include <sys/epoll.h> #include <function2/function2.hpp> #include <vector> #include <optional> class Executor { public: using Descriptor = int; // In a "production" grade system vector has to be replaced with a // pooled continuous storage covered with a buffer abstraction using OnRead = fu2::function<void(std::vector<std::uint8_t>)>; using OnListen = fu2::function<void()>; using OnDisconnect = fu2::function<void(Descriptor)>; Executor() = default; Executor(const Executor&) = delete; Executor(Executor&&) = delete; Executor& operator=(const Executor&) = delete; Executor& operator=(Executor&&) = delete; // epoll-state management methods [[nodiscard]] bool init(); [[nodiscard]] bool on_read(Descriptor, OnRead); [[nodiscard]] bool on_listen(Descriptor, OnListen); void on_disconnect(OnDisconnect); void run(); void stop(); private: using EventFlags = std::uint32_t; int epoll_fd_{-1}; bool running_{true}; bool add(Descriptor, EventFlags); void remove(Descriptor); void process_listen(epoll_event&); void process_read(epoll_event&); bool is_listener_event(epoll_event&); std::unordered_map<Descriptor, OnRead> read_handlers_; std::optional<Descriptor> listen_fd_; OnListen on_listen_; OnDisconnect on_disconnect_; };