• epoll c++封装


    #ifndef _BFC_EPOLL_FLOW_HPP_
    #define _BFC_EPOLL_FLOW_HPP_
    
    #include <string.h>
    #include <errno.h>
    #include <sys/epoll.h>
    #include <assert.h>
    #include <string>
    #include <stdexcept>
    #include <iostream>
    
    /*
    EPOLLIN :表示对应的文件描述符可以读
    EPOLLOUT:表示对应的文件描述符可以写
    EPOLLPRI:表示对应的文件描述符有紧急的数据可读
    EPOLLERR:表示对应的文件描述符发生错误
    EPOLLHUP:表示对应的文件描述符被挂断
    EPOLLET: 表示对应的文件描述符有事件发生
    */
    
    class CEPollFlow
    {
    public:
    CEPollFlow();
    ~CEPollFlow();
    
    int Create(int iMaxFD);
    //int Wait(int iTimeMs);
    
    int Wait(int iTimeMs);
    int GetEvents(long long &llKey, unsigned int &iEvent);
    
    int Add(int iFd,long long llKey,int iFlag);
    int Modify(int iFd,long long llKey,int iFlag);
    int Del(int iFd);
    
    private:
    int Ctl(int iFd,long long llKey,int iEpollAction, int iFlag);
    
    int m_iEpollFD;
    epoll_event* m_pEpollEvents;
    int m_iMaxFD;
    
    int m_iEventNum;
    int m_iCurrEvtIdx;
    };
    
    #endif // _BFC_EPOLL_FLOW_HPP_
    
    
    #include "bfc_epoll_flow.h"
    
    CEPollFlow::CEPollFlow()
    {
    m_iEpollFD = -1;
    m_pEpollEvents = NULL;
    }
    
    CEPollFlow::~CEPollFlow()
    {
    if(m_pEpollEvents)
    {
    delete [] m_pEpollEvents;
    }
    
    if(m_iEpollFD >= 0)
    {
    close(m_iEpollFD);
    }
    }
    
    int CEPollFlow::Create(int iMaxFD)
    {
    m_iMaxFD = iMaxFD;
    m_iEpollFD = epoll_create(iMaxFD);
    if(m_iEpollFD < 0)
    {
    return -1;
    }
    
    m_pEpollEvents = new epoll_event[iMaxFD];
    
    return 0;
    }
    
    int CEPollFlow::Wait(int iTimeMs)
    {
    m_iEventNum = epoll_wait(m_iEpollFD, m_pEpollEvents, m_iMaxFD, iTimeMs);
    m_iCurrEvtIdx = 0;
    
    return m_iEventNum;
    }
    
    int CEPollFlow::Add(int iFd, long long llKey, int iFlag)
    {
    return Ctl(iFd,llKey,EPOLL_CTL_ADD,iFlag);
    }
    
    int CEPollFlow::Del(int iFd)
    {
    return Ctl(iFd, 0, EPOLL_CTL_DEL, 0);
    }
    
    int CEPollFlow::Modify(int iFd, long long llKey, int iFlag)
    {
    return Ctl(iFd, llKey, EPOLL_CTL_MOD, iFlag);
    }
    
    int CEPollFlow::GetEvents(long long &llKey, unsigned int &iEvent)
    {
    if(m_iCurrEvtIdx >= m_iEventNum)
    {
    return 0;
    }
    
    epoll_event* curr_event = &m_pEpollEvents[m_iCurrEvtIdx++];
    llKey = curr_event->data.u64;
    iEvent = curr_event->events;
    
    return 1;
    }
    
    int CEPollFlow::Ctl(int iFd,long long llKey,int iEpollAction, int iFlag)
    {
    epoll_event evt;
    evt.events = iFlag;
    evt.data.u64 = llKey;
    
    int ret = epoll_ctl(m_iEpollFD, iEpollAction, iFd, &evt);
    if(ret < 0)
    {
    return -1;
    }
    
    return 0;
    }
    
  • 相关阅读:
    Ubuntu12.04配置永久静态ip地址
    linux shell 命令获取字符串/文件的MD5值
    设置VMware中Kali 共享文件夹
    tomcat的一些优化及报错
    用Toad for Oracle创建数据库表空间和用户
    linux远程Windows服务器
    linux查询及添加用户
    AJAX
    Cookie和Session
    Django-中间件
  • 原文地址:https://www.cnblogs.com/cqvoip/p/8078923.html
Copyright © 2020-2023  润新知