feat: 搭建了部分 epoll_loop 模块
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
#include <epoll/epoll_loop.hpp>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
namespace ouc_server
|
||||
{
|
||||
namespace epoll
|
||||
{
|
||||
|
||||
EpollLoop::EpollLoop()
|
||||
{
|
||||
epoll_fd = epoll_create1(0);
|
||||
}
|
||||
|
||||
EpollLoop::~EpollLoop()
|
||||
{
|
||||
close(epoll_fd);
|
||||
}
|
||||
|
||||
bool EpollLoop::add_fd(int fd, uint32_t event_flags, std::function<void()> callback)
|
||||
{
|
||||
struct epoll_event ev;
|
||||
ev.events = event_flags;
|
||||
ev.data.fd = fd;
|
||||
epoll_ctl(epoll_fd, EPOLL_CTL_ADD, fd, &ev);
|
||||
events_map[fd] = Event{fd, event_flags, callback};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#ifndef INCLUDE_OUC_SERVER_EPOLL_LOOP
|
||||
#define INCLUDE_OUC_SERVER_EPOLL_LOOP
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
#include <unistd.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/epoll.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
namespace ouc_server
|
||||
{
|
||||
namespace epoll
|
||||
{
|
||||
struct Event
|
||||
{
|
||||
int fd;
|
||||
uint32_t events;
|
||||
std::function<void()> callback;
|
||||
};
|
||||
|
||||
class EpollLoop
|
||||
{
|
||||
private:
|
||||
int epoll_fd;
|
||||
std::unordered_map<int, Event> events_map;
|
||||
|
||||
public:
|
||||
EpollLoop();
|
||||
|
||||
~EpollLoop();
|
||||
|
||||
public:
|
||||
bool add_fd(int, uint32_t, std::function<void()>);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#endif // INCLUDE_OUC_SERVER_EPOLL_LOOP
|
||||
Reference in New Issue
Block a user