代码拉取完成,页面将自动刷新
//
// Created by 22100 on 2024/5/6.
//
#include "EpollDispatcher.h"
#include "exceptionTip.h"
#include <unistd.h>
EpollDispatcher::EpollDispatcher(EventLoop *eventLoop) : Dispatcher(eventLoop) {
m_epfd = epoll_create(10); // 创建epoll根节点
exceptionTip(m_epfd, "epoll_create");
m_events = new struct epoll_event[maxNode];
}
EpollDispatcher::~EpollDispatcher() {
close(m_epfd);
delete [] m_events;
}
int EpollDispatcher::add() {
return epollCtl(EPOLL_CTL_ADD);
}
int EpollDispatcher::remove() {
int ret = epollCtl(EPOLL_CTL_DEL);
m_channel->destroyCallback(const_cast<void*>(m_channel->getArg())); // 丢掉const属性
return ret;
}
int EpollDispatcher::modify() {
return epollCtl(EPOLL_CTL_MOD);
}
int EpollDispatcher::dispatch(int timeout) {
int count = epoll_wait(m_epfd, m_events, maxNode, timeout * 1000);
for (int i = 0; i < count; ++i)
{
int events = m_events[i].events;
int fd = m_events[i].data.fd;
if (events & EPOLLERR || events & EPOLLHUP)
{
// 对方断开了连接, 删除 fd
// epollRemove(Channel, evLoop);
continue;
}
if (events & EPOLLIN)
{
eventLoop->eventActive(fd, (int)FDEvent::ReadEvent);
}
if (events & EPOLLOUT)
{
eventLoop->eventActive(fd, (int)FDEvent::WriteEvent);
}
}
return 0;
}
int EpollDispatcher::epollCtl(int opt) {
struct epoll_event ev{};
int events = 0;
if(m_channel->getEvents() & static_cast<int>(FDEvent::ReadEvent)){ // 读事件
events |= EPOLLIN;
}
if(m_channel->getEvents() & static_cast<int>(FDEvent::WriteEvent)){ // 写事件
events |= EPOLLOUT;
}
ev.data.fd = m_channel->getFd();
ev.events = events;
int ret = epoll_ctl(m_epfd, opt, m_channel->getFd(), &ev);
exceptionTip(ret, "epoll_ctl");
return ret;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。