pxEpoll.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #ifndef _PX_EPOLL_H_
  2. #define _PX_EPOLL_H_
  3. #include <sys/epoll.h>
  4. #include "pxTools.h"
  5. #define TIME_UNIT 1000 //0.001S
  6. typedef struct pxEpoll
  7. {
  8. int clientfd;
  9. int serverfd;
  10. void (*recvCallBack)(struct pxEpoll *epoll);
  11. void (*failCallBack)(struct pxEpoll *epoll);
  12. void (*connSuccess)(struct pxEpoll *epoll);
  13. void *param;
  14. }PXEPOLL_S;
  15. typedef void EPOLLCB(PXEPOLL_S*);
  16. typedef struct pxTimer
  17. {
  18. int num;
  19. int multiTime;
  20. int interval;//单位为毫米
  21. PXEPOLL_S *epoll;
  22. void (*timerCallBack)(struct pxTimer *timer);
  23. void *param;
  24. LISTNODE_S node;
  25. }PXTIMER_S;
  26. typedef void TIMERCB(PXTIMER_S *);
  27. int epollInit(char *threadName);
  28. //若add失败,需手动关闭fd,
  29. //若没有指定failCB,param必须仅该epoll使用且是堆内存数据。触发失败会自动释放
  30. //若指定failCB则需要在failCB里删除epoll,关闭fd,释放param
  31. PXEPOLL_S* epollAdd(int epollfd,int fd,EPOLLCB *recvCB,EPOLLCB *failCB,void *param);
  32. //del成功后需手动释放param和关闭fd
  33. int epollDel(PXEPOLL_S* epoll);
  34. PXTIMER_S* timerAdd(int epollfd,int num,int interval,int multiTime,TIMERCB *timeoutCB,void *param);
  35. void timerDel(PXTIMER_S *entry);
  36. // void timerDelByNum(int timerID);
  37. int epollMain(int epollfd,char *threadName);
  38. #endif