cpoll_internal.H 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * cpoll_internal.H
  3. *
  4. * Created on: 2012-09-14
  5. * Author: xaxaxa
  6. */
  7. /*
  8. This program is free software: you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation, either version 3 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. * */
  19. #ifndef CPOLL_INTERNAL_H_
  20. #define CPOLL_INTERNAL_H_
  21. #include <signal.h>
  22. namespace CP
  23. {
  24. static inline int eventToIndex(Events event) {
  25. if (event == Events::in) return 0;
  26. if (event == Events::out) return 1;
  27. if (event == Events::other) return 2;
  28. return -1;
  29. }
  30. static inline short eventToPoll(Events event) {
  31. switch (event) {
  32. case Events::in:
  33. return POLLIN | POLLPRI | POLLRDHUP;
  34. case Events::out:
  35. return POLLOUT | POLLHUP | POLLERR | POLLNVAL;
  36. default:
  37. break;
  38. }
  39. return 0;
  40. }
  41. static inline short eventsToPoll(Events events) {
  42. short ret = 0;
  43. if ((event_t) events & (event_t) Events::in) ret |= POLLIN | POLLPRI | POLLRDHUP;
  44. if ((event_t) events & (event_t) Events::out) ret |= POLLOUT | POLLHUP | POLLERR | POLLNVAL;
  45. return ret;
  46. }
  47. static inline Events pollToEvents(short events) {
  48. event_t ret = 0;
  49. if (events & (POLLIN | POLLPRI | POLLRDHUP)) ret |= (event_t) Events::in;
  50. if (events & (POLLOUT | POLLHUP | POLLERR | POLLNVAL)) ret |= (event_t) Events::out;
  51. return (Events) ret;
  52. }
  53. static inline uint32_t eventToEPoll(Events event) {
  54. switch (event) {
  55. case Events::in:
  56. return EPOLLIN | EPOLLPRI | EPOLLRDHUP;
  57. case Events::out:
  58. return EPOLLOUT | EPOLLHUP | EPOLLERR;
  59. default:
  60. break;
  61. }
  62. return 0;
  63. }
  64. static inline uint32_t eventsToEPoll(Events events) {
  65. uint32_t ret = 0;
  66. if ((event_t) events & (event_t) Events::in) ret |= EPOLLIN | EPOLLPRI | EPOLLRDHUP;
  67. if ((event_t) events & (event_t) Events::out) ret |= EPOLLOUT | EPOLLHUP | EPOLLERR;
  68. return ret;
  69. }
  70. static inline Events ePollToEvents(uint32_t events) {
  71. event_t ret = 0;
  72. //if EPOLLHUP is in events, return both Events::in and Event::out
  73. if (events & (EPOLLHUP | EPOLLIN | EPOLLPRI | EPOLLRDHUP)) ret |= (event_t) Events::in;
  74. if (events & (EPOLLHUP | EPOLLOUT | EPOLLERR)) ret |= (event_t) Events::out;
  75. return (Events) ret;
  76. }
  77. static inline Events indexToEvent(int index) {
  78. return (Events) (1 << index);
  79. }
  80. static inline void onError(int err_no) {
  81. throw CPollException(err_no);
  82. }
  83. static inline int checkError(int err) {
  84. if (unlikely(unlikely(err < 0) && errno != EINTR && errno != EINPROGRESS)) throw CPollException();
  85. return err;
  86. }
  87. //disable the retarded SIGHUP and SIGPIPE
  88. static void disableSignals() {
  89. struct sigaction sa;
  90. sa.sa_handler = SIG_IGN;
  91. sigemptyset(&sa.sa_mask);
  92. sa.sa_flags = SA_RESTART; /* Restart system calls if
  93. interrupted by handler */
  94. sigaction(SIGHUP, &sa, NULL);
  95. sigaction(SIGPIPE, &sa, NULL);
  96. sa.sa_handler = SIG_DFL;
  97. //cout<<sigaction(SIGSTOP, &sa, NULL)<<endl;
  98. //cout<<errno<<endl;
  99. sigaction(SIGCONT, &sa, NULL);
  100. sigaction(SIGTSTP, &sa, NULL);
  101. sigaction(SIGTTIN, &sa, NULL);
  102. sigaction(SIGTTOU, &sa, NULL);
  103. }
  104. }
  105. #endif /* CPOLL_INTERNAL_H_ */