#ifndef _ZWFORWARDEPOLL_H_
#define _ZWFORWARDEPOLL_H_

#include "MFSPublic.h"

namespace mfs
{
    struct ZWEpollContent
    {
        int fd;
        std::function<void(int)> readfunc;
        epoll_event event;
    };
    struct ZWEpollThreadContext
    {
        int epollfd;                               // epoll
        std::map<int, ZWEpollContent> readfds;     // read
    };
    /**
     * epoll 处理转发
     */
    class ZWForwardEpoll
    {
        private:
        ZWForwardEpoll(){};
        static ZWForwardEpoll* _instance;

        bool _abort;                                // abort
        bool _running;                              // state
        std::vector<ZWEpollThreadContext> _contexts;
        std::vector<int> _fds;
        
        void runInner(ZWEpollThreadContext*context);                // main loop

        public:
        ~ZWForwardEpoll();
        static ZWForwardEpoll* instance(){return _instance;}

        int run(int count);
        void add(int fd, std::function<void(int)> func);
        void remove(int fd);

        int write(int fd, const char* buf, int length);
    };
}


#endif // _ZWFORWARDEPOLL_H_