• 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;
    }
    
  • 相关阅读:
    Java中的经典算法之冒泡排序(Bubble Sort)
    Appium环境搭建(Windows版)
    Jenkins安装与配置
    Jenkins+ANT+Jmeter 接口测试的实践(转载)
    bugku_web_phpcms1(未完待续)
    bugku_web_Bugku-cms1(未完待续)
    牛客网 PAT 算法历年真题 1010 : 月饼 (25)
    【测试的艺术】+ 封装
    【数据库】+ powerdesigner
    【Git】+IDEA中无法使用git命令
  • 原文地址:https://www.cnblogs.com/cqvoip/p/8078923.html
Copyright © 2020-2023  润新知