|
- #pragma once
-
- #include "select.h"
- #include "../misc/exception.h"
- #include "../conversion/time.h"
-
- namespace cppcore
- {
-
- namespace __impl
- {
- inline uint throw_select_error(int err)
- {
- if (err == EINTR)
- return 0;
- throw error_exception("Error while waiting for select operation", err);
- }
- }
-
- uint select(
- int fd_count,
- fdset& read,
- fdset& write,
- fdset& except)
- {
- auto ret = ::select(fd_count, &read, &write, &except, nullptr);
- if (ret < 0)
- return __impl::throw_select_error(errno);
- return static_cast<uint>(ret);
- }
-
- template<typename T_rep, typename T_period>
- uint select(
- int fd_count,
- fdset& read,
- fdset& write,
- fdset& except,
- std::chrono::duration<T_rep, T_period>& timeout)
- {
- using duration_type = std::chrono::duration<T_rep, T_period>;
-
- auto tmp = duration_cast<timeval>(timeout);
- auto ret = ::select(fd_count, &read, &write, &except, &tmp);
- if (ret < 0)
- return __impl::throw_select_error(errno);
- timeout = duration_cast<duration_type>(tmp);
- return static_cast<uint>(ret);
- }
-
- template<typename T_rep, typename T_period>
- uint select(
- int fd_count,
- fdset& read,
- fdset& write,
- fdset& except,
- const std::chrono::duration<T_rep, T_period>& timeout)
- {
- auto tmp = duration_cast<timeval>(timeout);
- auto ret = ::select(fd_count, &read, &write, &except, &tmp);
- if (ret < 0)
- return __impl::throw_select_error(errno);
- return static_cast<uint>(ret);
- }
-
- }
|