C++封装一个Epoll类,实现epoll简单使用

该博客主要讲述用C++封装Epoll类以实现epoll的简单使用,涉及后端开发中对Epoll的操作,能帮助开发者更便捷地运用epoll进行相关编程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <vector>
#include <sys/epoll.h>
#include "tcpsocket.hpp"

class Epoll {
    private:
        int _epfd;
    public:
        bool Init() {
            //创建epoll
            
            //接口原型:int epoll_create(int size);
            _epfd = epoll_create(1);
            if (_epfd < 0) {
                perror("epoll create error");
                return false;
            }
            return true;
        }
        bool Add(TcpSocket sock, uint32_t events = 0) {
            sock.SetNonBlock();
            int fd = sock.GetSockFd();
            
            //定义事件
            struct epoll_event ev;
            ev.events = EPOLLIN | events;
            ev.data.fd = fd;
            
            //接口原型:epoll_ctl(int epfd,int op,int fd,struct epoll_event *event);
            int ret = epoll_ctl(_epfd, EPOLL_CTL_ADD, fd, &ev);
            if (ret < 0) {
                perror("epoll ctrl error");
                return false;
            }
            return true;
        }
        bool Del(TcpSocket sock) {
            int fd = sock.GetSockFd();
            int ret = epoll_ctl(_epfd, EPOLL_CTL_DEL, fd, NULL);
            if (ret < 0) {
                perror("epoll ctrl error");
                return false;
            }
            return true;
        }
        bool Wait(std::vector<TcpSocket> &list, int ms_timeout = 3000) {
            //接口原型:int epoll_wait(int epfd, struct epoll_event *events,
            //  int maxevents, int timeout);
            struct epoll_event evs[10];
            int nfds = epoll_wait(_epfd, evs, 10, ms_timeout);
            if (nfds < 0) {
                perror("epoll wait error");
                return false;
            }else if (nfds == 0) {
                std::cout << "epoll wait timeout\n";
                return false;
            }
            for (int i = 0; i < nfds; i++) {
                int fd = evs[i].data.fd;
                TcpSocket sock;
                sock.SetSockFd(fd);
                list.push_back(sock);
            }
            return true;
        }
};

int main(){
    TcpSocket lst_sock;			//listen socket:监听socket
    CHECK_RET(lst_sock.Socket());
    CHECK_RET(lst_sock.Bind("0.0.0.0", 9000));
    CHECK_RET(lst_sock.Listen());

    Epoll epoll;
    CHECK_RET(epoll.Init());
    CHECK_RET(epoll.Add(lst_sock, EPOLLET));
    while(1) {
        std::vector<TcpSocket> list;
        bool ret = epoll.Wait(list);
        if (ret == false) {
            continue;
        }
        for (int i = 0; i < list.size(); i++) {
            if (list[i].GetSockFd() == lst_sock.GetSockFd()) {
                TcpSocket cli_sock;
                lst_sock.Accept(cli_sock);
                epoll.Add(cli_sock, EPOLLET);
            }else {
                std::string buf;
                list[i].Recv(buf);
                std::cout << "client say: " << buf << std::endl;
            }
        }

    }
    lst_sock.Close();
    return 0;
}

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

giturtle

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值