仿Muduo库实现高并发服务器——事件监控Poller模块

Poller模块在整个项目的使用

下面代码是对I/O复用接口函数的基本使用。

回顾上篇文章:事件监控管理模块

这个模块是将触发事件的描述符,给到外面,让外面去做对应的处理。

cpp 复制代码
#define MAX_EPOLLEVENTS 1024
class Poller {
    private:
        int _epfd;
        struct epoll_event _evs[MAX_EPOLLEVENTS];
        std::unordered_map<int, Channel *> _channels;
    private:
        //对epoll的直接操作
        void Update(Channel *channel, int op) {
            // int epoll_ctl(int epfd, int op,  int fd,  struct epoll_event *ev);
            int fd = channel->Fd();
            struct epoll_event ev;
            ev.data.fd = fd;
            ev.events = channel->Events();
            int ret = epoll_ctl(_epfd, op, fd, &ev);
            if (ret < 0) {
                ERR_LOG("EPOLLCTL FAILED!");
            }
            return;
        }
        //判断一个Channel是否已经添加了事件监控
        bool HasChannel(Channel *channel) {
            auto it = _channels.find(channel->Fd());
            if (it == _channels.end()) {
                return false;
            }
            return true;
        }
    public:
        Poller() {
            _epfd = epoll_create(MAX_EPOLLEVENTS);
            if (_epfd < 0) {
                ERR_LOG("EPOLL CREATE FAILED!!");
                abort();//退出程序
            }
        }
        //添加或修改监控事件
        void UpdateEvent(Channel *channel) {
            bool ret = HasChannel(channel);
            if (ret == false) {
                //不存在则添加
                _channels.insert(std::make_pair(channel->Fd(), channel));
                return Update(channel, EPOLL_CTL_ADD);
            }
            return Update(channel, EPOLL_CTL_MOD);
        }
        //移除监控
        void RemoveEvent(Channel *channel) {
            auto it = _channels.find(channel->Fd());
            if (it != _channels.end()) {
                _channels.erase(it);
            }
            Update(channel, EPOLL_CTL_DEL);
        }
        //开始监控,返回活跃连接
        void Poll(std::vector<Channel*> *active) {
            // int epoll_wait(int epfd, struct epoll_event *evs, int maxevents, int timeout)
            int nfds = epoll_wait(_epfd, _evs, MAX_EPOLLEVENTS, -1);
            if (nfds < 0) {
                if (errno == EINTR) {
                    return ;
                }
                ERR_LOG("EPOLL WAIT ERROR:%s\n", strerror(errno));
                abort();//退出程序
            }
            for (int i = 0; i < nfds; i++) {
                auto it = _channels.find(_evs[i].data.fd);
                assert(it != _channels.end());
                it->second->SetREvents(_evs[i].events);//设置实际就绪的事件
                active->push_back(it->second);
            }
            return;
        }
};
相关推荐
阿巴~阿巴~几秒前
基于UDP协议的英汉翻译服务系统:从网络通信到字典查询的完整机制
linux·服务器·网络·网络协议·udp协议·套接字绑定·英汉翻译服务系统
阿巴~阿巴~4 分钟前
简易回声服务器实现与网络测试指南
linux·服务器·网络·udp协议·网络测试·udp套接字编程
君以思为故3 小时前
认识Linux -- 进程概念
linux·服务器
weixin_537765804 小时前
【负载均衡】LVS DR模式详解
服务器·负载均衡·lvs
LoneEon4 小时前
告别手动操作:用 Ansible 统一管理你的 Ubuntu 服务器集群
运维·服务器·ansible
摘星|5 小时前
架设一台NFS服务器,并按照以下要求配置
linux·运维·服务器
做运维的阿瑞6 小时前
Linux环境变量持久化完全指南
linux·运维·服务器
天才奇男子6 小时前
从零开始搭建Linux Web服务器
linux·服务器·前端
Wang's Blog7 小时前
MySQL: 服务器性能优化全面指南:参数配置与数据库设计的最佳实践
服务器·数据库·mysql
顾安r7 小时前
11.14 脚本网页 迷宫逃离
服务器·javascript·游戏·flask·html