#pragma once #include "logger.hpp" #include #include #include #include #include #include #include namespace tinyrpc { class FdEvent; class Reactor { public: using Task = std::function; enum ReactorType { Main = 1, Sub = 2 }; public: Reactor(ReactorType type = ReactorType::Main); void loop(); void addFdEvent(FdEvent* fdEvent); void delFdEvent(FdEvent* fdEvent); void modFdEvent(FdEvent* fdEvent); void stop(); void rouse(); static Reactor* getReactor(); ~Reactor(); // void addEvent() private: bool addFd(int fd, epoll_event ev); bool delFd(int fd); bool modFd(int fd, epoll_event ev); void addRouseFd(int eventFd); void processAllTasks(); private: int m_epfd {-1}; int m_rousefd{-1}; int m_tid {-1}; // 所属线程的 id bool m_is_stop {false}; bool m_is_looping {false}; ReactorType m_type {ReactorType::Main}; std::unordered_map m_listen_fd_events; std::vector m_tasks; std::mutex m_tasks_mtx; }; }