Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1826323
  • 博文数量: 286
  • 博客积分: 3713
  • 博客等级: 少校
  • 技术积分: 2275
  • 用 户 组: 普通用户
  • 注册时间: 2012-01-11 09:47
个人简介

http://blog.chinaunix.net/uid/16979052.html

文章分类

全部博文(286)

文章存档

2018年(1)

2017年(16)

2016年(9)

2015年(17)

2014年(15)

2013年(112)

2012年(116)

分类: LINUX

2013-02-22 08:35:55

     1 #ifndef MK_EPOLL_H
     2 #define MK_EPOLL_H
     3
     4 #include
     5
     6 #define MK_EPOLL_READ     0
     7 #define MK_EPOLL_WRITE    1
     8 #define MK_EPOLL_RW       2
     9
    10 /* Epoll timeout is 3 seconds */
    11 #define MK_EPOLL_WAIT_TIMEOUT 3000
    12
    13 #define MK_EPOLL_LEVEL_TRIGGERED 2        /* behavior. default */
    14 #define MK_EPOLL_EDGE_TRIGGERED  EPOLLET
    15
    16 #ifndef EPOLLRDHUP
    17 #define EPOLLRDHUP 0x2000
    18 #endif
    19
    20 typedef struct
    21 {
    22 int(*read) (void *pvoid);
    23 int(*write)(void *pvoid);
    24 int(*error)(void *pvoid);
    25 int(*close)(void *pvoid);
    26 } mk_epoll_handlers;
    27
    28 /* Monkey epoll calls */
    29 mk_epoll_handlers *mk_epoll_set_handlers(void (*read) (void *), void (*write) (void *), void (*error) (void *), void (*close) (void *));
    30 int mk_epoll_create(int max_events);
    31 int mk_epoll_add(int efd, int fd, int mode, int behavior, void *pvoid);
    32 int mk_epoll_del(int efd, int fd);
    33 int mk_epoll_change_mode(int efd, int fd, int mode, int behavior, void *pvoid);
    34 void *mk_epoll_init(int efd, mk_epoll_handlers * handler, int max_events);
    35
    36 #endif
    37 #include "applog.h"
    38 #include "mk_epoll.h"
    39 #include
    40
    41 mk_epoll_handlers *mk_epoll_set_handlers(void (*read) (void *), void (*write) (void *), void (*error) (void *), void (*close) (void *))
    42 {
    43 applog(LOG_INFO, "EPOLL : %s", __FUNCTION__);
    44 mk_epoll_handlers *handler;
    45 handler = malloc(sizeof(mk_epoll_handlers));
    46 handler->read = (void *) read;
    47 handler->write = (void *) write;
    48 handler->error = (void *) error;
    49 handler->close = (void *) close;
    50 return handler;
    51 }
    52 int mk_epoll_create(int max_events)
    53 {
    54 int efd;
    55 efd = epoll_create(max_events);
    56 if(efd == -1)
    57 {
    58 perror("epoll_create");
    59 applog(LOG_ERR, "epoll_create() failed");
    60 }
    61 return efd;
    62 }
    63 void *mk_epoll_init(int efd, mk_epoll_handlers * handler, int max_events)
    64 {
    65 int i, ret = -1;
    66 int num_fds;
    67 struct epoll_event *events = calloc(sizeof(struct epoll_event), max_events);
    68 while (1)
    69 {
    70 ret = -1;
    71 num_fds = epoll_wait(efd, events, max_events, MK_EPOLL_WAIT_TIMEOUT);
    72 if(-1 == num_fds)
    73 break;
    74 for(i=0; i     75 {
    76 if (events[i].events & EPOLLIN)
    77 {
    78 ret = (*handler->read)(events[i].data.ptr);
    79 }
    80 else if (events[i].events & EPOLLOUT)
    81 {
    82 ret = (*handler->write) (events[i].data.ptr);
    83 }
    84 else if (events[i].events & (EPOLLHUP | EPOLLERR | EPOLLRDHUP))
    85 {
    86 ret = (*handler->error) (events[i].data.ptr);
    87 }
    88 if(ret < 0)
    89 {
    90 (*handler->close)(events[i].data.ptr);
    91 }
    92 }
    93 }
    94 applog(LOG_DEBUG, "epoll event error");
    95 free(events);
    96 return NULL;
    97 }
    98 int mk_epoll_add(int efd, int fd, int init_mode, int behavior, void *pvoid)
    99 {
   100 int ret;
   101 struct epoll_event event;
   102 event.events = EPOLLERR | EPOLLHUP | EPOLLRDHUP;
   103 event.data.ptr = pvoid;
   104 if (behavior == MK_EPOLL_EDGE_TRIGGERED)
   105 {
   106 event.events |= EPOLLET;
   107 }
   108 switch (init_mode)
   109 {
   110 case MK_EPOLL_READ:
   111 event.events |= EPOLLIN;
   112 break;
   113 case MK_EPOLL_WRITE:
   114 event.events |= EPOLLOUT;
   115 break;
   116 case MK_EPOLL_RW:
   117 event.events |= EPOLLIN | EPOLLOUT;
   118 break;
   119 }
   120 /* Add to epoll queue */
   121 ret = epoll_ctl(efd, EPOLL_CTL_ADD, fd, &event);
   122 if (ret < 0 && errno != EEXIST)
   123 {
   124 applog(LOG_DEBUG, "[FD %i] epoll_ctl() %s", fd, strerror(errno));
   125 return ret;
   126 }
   127 return ret;
   128 }
   129 int mk_epoll_del(int efd, int fd)
   130 {
   131 int ret;
   132 struct epoll_event event;
   133 ret = epoll_ctl(efd, EPOLL_CTL_DEL, fd, &event);
   134 applog(LOG_DEBUG, "Epoll, removing fd %i from efd %i", fd, efd);
   135 if(ret < 0)
   136 {
   137 applog(LOG_DEBUG, "[FD %i] epoll_ctl() = %i", fd, ret);
   138 perror("epoll_ctl");
   139 }
   140 return ret;
   141 }
   142 int mk_epoll_change_mode(int efd, int fd, int mode, int behavior, void *pvoid)
   143 {
   144 int ret;
   145 struct epoll_event event;
   146 event.events = EPOLLERR | EPOLLHUP;
   147 event.data.ptr = pvoid;
   148 switch (mode)
   149 {
   150 case MK_EPOLL_READ:
   151 applog(LOG_DEBUG, "[FD %i] EPoll changing mode to READ", fd);
   152 event.events |= EPOLLIN;
   153 break;
   154 case MK_EPOLL_WRITE:
   155 applog(LOG_DEBUG, "[FD %i] EPoll changing mode to WRITE", fd);
   156 event.events |= EPOLLOUT;
   157 break;
   158 case MK_EPOLL_RW:
   159 applog(LOG_DEBUG, "[FD %i] Epoll changing mode to READ/WRITE", fd);
   160 event.events |= EPOLLIN | EPOLLOUT;
   161 break;
   162 }
   163 if (behavior == MK_EPOLL_EDGE_TRIGGERED)
   164 {
   165 event.events |= EPOLLET;
   166 }
   167 /* Update epoll fd events */
   168 ret = epoll_ctl(efd, EPOLL_CTL_MOD, fd, &event);
   169 if (ret < 0)
   170 {
   171 applog(LOG_DEBUG, "[FD %i] epoll_ctl() = %i", fd, ret);
   172 perror("epoll_ctl");
   173 }
   174 return ret;
   175 }

阅读(1509) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~