封装的EPOLL 模型

  1. #pragma once   
  2. #include <errno.h>   
  3. #include <sys/epoll.h>   
  4. #include <sys/types.h>   
  5. #include <sys/socket.h>   
  6. #include <fcntl.h>   
  7. #include <unistd.h>   
  8. #include <string.h>   
  9. #include "log.h"   
  10.   
  11. #ifndef EPOLL_SIZE   
  12. #define EPOLL_SIZE 4096   
  13. #define EPOLL_CONTEXT_SIZE (4096+256)   
  14. #endif   
  15.   
  16. typedef void * EPollerContext;   
  17. class Epoller   
  18. {   
  19. public:   
  20. Epoller(){}   
  21. virtual ~Epoller(){}   
  22. bool create()   
  23. {   
  24.  memset(_contexts,0,sizeof(_contexts));   
  25.  _handle=epoll_create(EPOLL_SIZE);   
  26.  if(_handle==-1)   
  27.  {   
  28.   glog.log(__FUNCTION__,"Epoll create error,errno is %d",errno);   
  29.   return false;   
  30.  }   
  31.  else  
  32.   return true;   
  33. }   
  34. void handle_events()   
  35. {   
  36.  for(int i=0;i<_lastntfd;i++)   
  37.  {   
  38.   on_event(_events[i].data.fd,_events[i].events);   
  39.  }   
  40. }   
  41. int get_online_users()   
  42. {   
  43.  int result=0;   
  44.  for(int i=0;i<EPOLL_CONTEXT_SIZE;i++)   
  45.  {   
  46.   if(_contexts[i]!=0)   
  47.    result++;   
  48.  }   
  49.  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值