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.
 
 
 

66 line
1.7 KiB

  1. #pragma once
  2. #include "select.h"
  3. #include "../misc/exception.h"
  4. #include "../conversion/time.h"
  5. namespace cppcore
  6. {
  7. namespace __impl
  8. {
  9. inline uint throw_select_error(int err)
  10. {
  11. if (err == EINTR)
  12. return 0;
  13. throw error_exception("Error while waiting for select operation", err);
  14. }
  15. }
  16. uint select(
  17. int fd_count,
  18. fdset& read,
  19. fdset& write,
  20. fdset& except)
  21. {
  22. auto ret = ::select(fd_count, &read, &write, &except, nullptr);
  23. if (ret < 0)
  24. return __impl::throw_select_error(errno);
  25. return static_cast<uint>(ret);
  26. }
  27. template<typename T_rep, typename T_period>
  28. uint select(
  29. int fd_count,
  30. fdset& read,
  31. fdset& write,
  32. fdset& except,
  33. std::chrono::duration<T_rep, T_period>& timeout)
  34. {
  35. using duration_type = std::chrono::duration<T_rep, T_period>;
  36. auto tmp = duration_cast<timeval>(timeout);
  37. auto ret = ::select(fd_count, &read, &write, &except, &tmp);
  38. if (ret < 0)
  39. return __impl::throw_select_error(errno);
  40. timeout = duration_cast<duration_type>(tmp);
  41. return static_cast<uint>(ret);
  42. }
  43. template<typename T_rep, typename T_period>
  44. uint select(
  45. int fd_count,
  46. fdset& read,
  47. fdset& write,
  48. fdset& except,
  49. const std::chrono::duration<T_rep, T_period>& timeout)
  50. {
  51. auto tmp = duration_cast<timeval>(timeout);
  52. auto ret = ::select(fd_count, &read, &write, &except, &tmp);
  53. if (ret < 0)
  54. return __impl::throw_select_error(errno);
  55. return static_cast<uint>(ret);
  56. }
  57. }