You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.6KB

  1. /*
  2. * Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLToolKit(https://github.com/ZLMediaKit/ZLToolKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef ZLMEDIAKIT_EPOLL_H
  11. #define ZLMEDIAKIT_EPOLL_H
  12. #include "wepoll.h"
  13. #include <map>
  14. #include <mutex>
  15. // 屏蔽 EPOLLET
  16. #define EPOLLET 0
  17. namespace toolkit {
  18. // 索引handle
  19. extern std::map<int, HANDLE> s_wepollHandleMap;
  20. extern int s_handleIndex;
  21. extern std::mutex s_handleMtx;
  22. // 屏蔽epoll_create epoll_ctl epoll_wait参数差异
  23. inline int epoll_create(int size) {
  24. HANDLE handle = ::epoll_create(size);
  25. if (!handle) {
  26. return -1;
  27. }
  28. {
  29. std::lock_guard<std::mutex> lck(s_handleMtx);
  30. int idx = ++s_handleIndex;
  31. s_wepollHandleMap[idx] = handle;
  32. return idx;
  33. }
  34. }
  35. inline int epoll_ctl(int ephnd, int op, SOCKET sock, struct epoll_event *ev) {
  36. HANDLE handle;
  37. {
  38. std::lock_guard<std::mutex> lck(s_handleMtx);
  39. handle = s_wepollHandleMap[ephnd];
  40. }
  41. return ::epoll_ctl(handle, op, sock, ev);
  42. }
  43. inline int epoll_wait(int ephnd, struct epoll_event *events, int maxevents, int timeout) {
  44. HANDLE handle;
  45. {
  46. std::lock_guard<std::mutex> lck(s_handleMtx);
  47. handle = s_wepollHandleMap[ephnd];
  48. }
  49. return ::epoll_wait(handle, events, maxevents, timeout);
  50. }
  51. } // namespace toolkit
  52. #endif // ZLMEDIAKIT_EPOLL_H