头文件duye_epoll.h

/***********************************************************************************
**
* @copyright (c) 2010-2019,  Technology Co., LTD. All Right Reserved.
*
************************************************************************************/
/**
* @file     duye_epoll.h
* @version
* @brief
* @author
* @date     2013-03-29
* @note
*
* 1. 2013-03-29 Created this file
*/#pragma once#include <sys/epoll.h>
#include <stdio.h>
#include <list>
#include <duye_sys.h>
#include <duye_type.h>namespace duye {class EpollEvent {
public:EpollEvent() : m_fd(-1), m_event(0){}EpollEvent(const int32 fd, const uint32 event) : m_fd(fd), m_event(event) {}~EpollEvent() {}/*** @brief get/set fd* @return fd*/int32 fd() const { return m_fd; }/*** @brief get/set events* @return events*/uint32 event() const { return m_event; }/*** @brief is received data, for network event* @return true/false*/bool isRecv() { return m_event & EPOLLIN; }bool isRecv() const { return m_event & EPOLLIN; }/*** @brief is sent data, for network event* @return true/false*/bool isSend() { return m_event & EPOLLOUT; }bool isSend() const { return m_event & EPOLLOUT; }/*** @brief is sent data, for network event* @return true/false*/   bool isClosed() { return (m_event & EPOLLERR) || (m_event & EPOLLHUP); }bool isClosed() const { return (m_event & EPOLLERR) || (m_event & EPOLLHUP); }/*** @brief is disconnection, for network event* @return true/false*/bool isDiscon() { return (m_event == EPOLLERR || m_event == EPOLLHUP) ? true : false; }private:int32   m_fd;   uint32  m_event;
};/*** @brief epoll socket server*/
class Epoll {
public:typedef struct epoll_event SysEvent;typedef std::list<EpollEvent> EventList;public:Epoll();~Epoll();/*** @brief open epoll* @param [in] maxEvent : the number of the max events* @return true/false*/bool open(const uint32 maxEvents = 1024);/*** @brief close epoll* @return true/false*/bool close();   /*** @brief add fd* @param [in] fd : fd* @param [in] epollMode : epoll events(EPOLLIN, EPOLLOUT, EPOLLPRI, EPOLLERR, *      EPOLLHUP, EPOLLET, EPOLLONESHOT), default is EPOLLIN | EPOLLOUT* @return true/false*/      bool addfd(const int32 fd, const uint32 epollMode = EPOLLIN | EPOLLOUT);/*** @brief modify fd* @param [in] fd : fd* @param [in] epollMode : epoll events(EPOLLIN, EPOLLOUT, EPOLLPRI, EPOLLERR, *      EPOLLHUP, EPOLLET, EPOLLONESHOT), default is EPOLLIN | EPOLLOUT* @return true/false*/      bool modfd(const int32 fd, const uint32 epollMode = EPOLLIN | EPOLLOUT);/*** @brief delete fd* @param [in] fd : fd* @return true/false*/bool delfd(const int32 fd);/*** @brief wait event* @param [out] eventList : return event* @param [in] timeout : wait time out, default is -1, indicate block, millisecond* @return true/false*/bool wait(Epoll::EventList& eventList, const uint32 timeout = -1);/*** @brief get last error string* @return error string* @note */     uint8* error(); private:// create epollbool create();private:int32       m_epollfd; uint32      m_maxEvents;SysEvent*   m_sysEvents;Error       m_error;
};}

cpp文件duye_epoll.h

/************************************************************************************
**
* @copyright (c) 2013-2100,  Technology Co., LTD. All Right Reserved.
*
*************************************************************************************/
/**
* @file     duye_epoll.cpp
* @version
* @brief
* @author
* @date     2013-03-29
* @note
*
* 1. 2013-03-29 Created this file
*/#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <duye_sys.h>
#include <duye_epoll.h>namespace duye {static const int8* DUYE_LOG_PREFIX = "duye.system.epoll";Epoll::Epoll() : m_epollfd(-1), m_maxEvents(0), m_sysEvents(NULL) {m_error.setPrefix(DUYE_LOG_PREFIX);
}Epoll::~Epoll() {close();
}bool Epoll::open(const uint32 maxEvents) {close();m_maxEvents = maxEvents;m_epollfd = epoll_create(m_maxEvents);if (m_epollfd == -1) {ERROR_DUYE_LOG("%s:%d > epoll_create failed \n", __FILE__, __LINE__);return false;}  m_sysEvents = (struct epoll_event*)calloc(m_maxEvents, sizeof(struct epoll_event));if (m_sysEvents == NULL) {ERROR_DUYE_LOG("%s:%d > calloc return NULL \n", __FILE__, __LINE__);return false;}return true;
}bool Epoll::close() {m_maxEvents = 0;if (m_epollfd != -1) {::close(m_epollfd);m_epollfd = -1;}if (m_sysEvents != NULL) {free(m_sysEvents);m_sysEvents = NULL;}    return true;
}bool Epoll::addfd(const int32 fd, const uint32 epollMode) {if (m_epollfd == -1) {ERROR_DUYE_LOG("%s:%d > m_epollfd == -1", __FILE__, __LINE__);return false;}struct epoll_event epollEvent;bzero(&epollEvent, sizeof(struct epoll_event));epollEvent.data.fd = fd;epollEvent.events = epollMode;int32 ret = epoll_ctl(m_epollfd, EPOLL_CTL_ADD, fd, &epollEvent);if (ret != 0) {ERROR_DUYE_LOG("[%s:%d] epoll_ctl() return = %d", __FILE__, __LINE__, ret);return false;}return true;
}bool Epoll::modfd(const int32 fd, const uint32 epollMode) {if (m_epollfd == -1) {ERROR_DUYE_LOG("[%s:%d] m_epollfd == -1", __FILE__, __LINE__);return false;}struct epoll_event epollEvent;bzero(&epollEvent, sizeof(struct epoll_event));epollEvent.data.fd = fd;epollEvent.events = epollMode;return epoll_ctl(m_epollfd, EPOLL_CTL_MOD, fd, &epollEvent) == 0 ? true : false;
}bool Epoll::delfd(const int32 fd) {if (m_epollfd == -1) {ERROR_DUYE_LOG("[%s:%d] m_epollfd == -1", __FILE__, __LINE__);return false;}return epoll_ctl(m_epollfd, EPOLL_CTL_DEL, fd, NULL) == 0 ? true : false;
}bool Epoll::wait(Epoll::EventList& eventList, const uint32 timeout) {if (m_epollfd == -1) {ERROR_DUYE_LOG("[%s:%d] m_epollfd == -1", __FILE__, __LINE__);return false;}int32 event_count = epoll_wait(m_epollfd, m_sysEvents, m_maxEvents, timeout);if (event_count <= 0) {return false;}for (uint32 i = 0; i < (uint32)event_count; i++) {eventList.push_back(EpollEvent(m_sysEvents[i].data.fd, m_sysEvents[i].events));//       if ((m_sysEvents[i].events & EPOLLERR) ||  //           (m_sysEvents[i].events & EPOLLHUP)) {//           ERROR_DUYE_LOG("[%s:%d] epoll error, close fd \n", __FILE__, __LINE__);//           delfd(m_sysEvents[i].data.fd);//           eventList.push_back(EpollEvent(m_sysEvents[i].data.fd, ERROR_FD));//        continue;//       } else if (m_sysEvents[i].events & EPOLLIN) {//           eventList.push_back(EpollEvent(m_sysEvents[i].data.fd, RECV_FD));//       } else if (m_sysEvents[i].events & EPOLLOUT) {//     eventList.push_back(EpollEvent(m_sysEvents[i].data.fd, SEND_FD));// } else {//     eventList.push_back(EpollEvent(m_sysEvents[i].data.fd, m_sysEvents[i].events));// }}return true;
}uint8* Epoll::error() {return m_error.error;
}}

Linux epoll 实现封装相关推荐

  1. 劫起|再谈Linux epoll惊群问题的原因和解决方案

    原作者:dog250,授权发布 重新整理: 极客重生 文章有点长,可以三连收藏慢慢看 缘起 近期排查了一个问题,epoll惊群的问题,起初我并不认为这是惊群导致,因为从现象上看,只是体现了CPU不均衡 ...

  2. 再谈Linux epoll惊群问题的原因和解决方案

    差别是什么?差别只是西装! 缘起 近期排查了一个问题,epoll惊群的问题,起初我并不认为这是惊群导致,因为从现象上看,只是体现了CPU不均衡.一共fork了20个Server进程,在请求负载中等的时 ...

  3. linux epoll监听套接字实例

    linux epoll机制用于IO多路复用,能够同时监听多个接字,使用起来比较简单. 相关接口: #include <sys/epoll.h>int epoll_create(int si ...

  4. linux epoll 开发指南-【ffrpc源码解析】

    linux epoll 开发指南-[ffrpc源码解析] 摘要 关于epoll的问题很早就像写文章讲讲自己的看法,但是由于ffrpc一直没有完工,所以也就拖下来了.Epoll主要在服务器编程中使用,本 ...

  5. Linux epoll的用法

    Linux epoll的用法 epollfd_create函数 #include <sys/epoll.h>int epoll_create (int __size) 参数 含义 __si ...

  6. python网络编程linux pdf_Python网络编程:Linux epoll

    原文地址:http://scotdoyle.com/python-epoll-howto.html 介绍 Python已于2.6版本添加访问Linux epoll库的API.这篇教程使用Python ...

  7. [转] Windows完成端口与Linux epoll技术简介

    Windows完成端口与Linux epoll技术简介 2008-01-03 16:18 WINDOWS完成端口编程1.基本概念 2.WINDOWS完成端口的特点 3.完成端口(Completion ...

  8. 基于linux epoll网络编程细节处理丨epoll原理剖析

    epoll原理剖析以及三握四挥的处理 1. epoll原理详解 2. 连接的创建与断开 3. epoll如何连接细节问题 视频讲解如下,点击观看: 基于linux epoll网络编程细节处理丨epol ...

  9. linux epoll机制

    在linux 没有实现epoll事件驱动机制之前,我们一般选择用select或者poll等IO多路复用的方法来实现并发服务程序.在linux新的内核中,有了一种替换它的机制,就是epoll. sele ...

  10. Windows完成端口与Linux epoll技术简介

    WINDOWS完成端口编程 1 1.基本概念 1 2.WINDOWS完成端口的特点 2 3.完成端口(Completion Ports )相关数据结构和创建 2 4.完成端口线程的工作原理 4 5.W ...

最新文章

  1. ORACLE 计算时间相减间隔
  2. JSValidation 配置文件
  3. AndroidStuido编译release版本apk(非签名apk)
  4. php维持登录,php怎么保持登录状态?
  5. javascript中常用数组方法详细讲解
  6. golang int64转string_(一)Golang从入门到原地起飞
  7. python顺序执行 toggle_pythonkivymd:如何使toggle_nav_drawer()函数工作?
  8. 苹果cmsv10精仿迅播影院2tu风格主题模板
  9. 脚本录制两种模式 HTML-based script和URL-based script模式
  10. html设置窗口最小大小,调整HTML 5画布的大小以适应窗口
  11. 【Java并发性和多线程】如何创建并运行java线程
  12. python经典题库及答案文库_Python经典题库及答案
  13. 面试IT公司的时候,程序员的简历应该写多少个项目经验比较合适?
  14. FilterSecurityInterceptor详解
  15. 更改浏览器语言(firefox, chrome)详细步骤
  16. 网络营销的方案及技巧
  17. SVG defs元素
  18. 【小程序开发】ios中时间显示为NaNNaN
  19. 各个操作系统中怎样设置电脑的颜色保护眼睛
  20. 永恒之蓝MS17-010漏洞复现

热门文章

  1. 支持Linux系统双网卡ARM平台AM3352/AM3354
  2. HTML5 标签大全
  3. 行政区域村级划分数据库_最新行政区划省市区街道乡镇数据库 每月更新版
  4. 计算机被覆盖文件怎么恢复,如何找回被覆盖的文件?恢复被覆盖文件的方法
  5. openg和VS2010的环境配置
  6. sem与seo的区别与联系
  7. 小象学院 零基础Python入门 案例四 52周存钱挑战v_4.0
  8. 联通光纤服务器没有响应怎么办,联通网速不稳定(联通光纤不稳定解决方法)
  9. python 组合优化_python中的多周期投资组合优化
  10. 用户根据短信验证码注册