diff --git a/tdutils/td/utils/port/detail/Epoll.h b/tdutils/td/utils/port/detail/Epoll.h index 1266f490..e99383c9 100644 --- a/tdutils/td/utils/port/detail/Epoll.h +++ b/tdutils/td/utils/port/detail/Epoll.h @@ -12,6 +12,7 @@ #include "td/utils/common.h" #include "td/utils/List.h" +#include "td/utils/port/detail/NativeFd.h" #include "td/utils/port/detail/PollableFd.h" #include "td/utils/port/PollBase.h" #include "td/utils/port/PollFlags.h" diff --git a/tdutils/td/utils/port/detail/KQueue.h b/tdutils/td/utils/port/detail/KQueue.h index d054ed36..d1e60fbf 100644 --- a/tdutils/td/utils/port/detail/KQueue.h +++ b/tdutils/td/utils/port/detail/KQueue.h @@ -12,6 +12,7 @@ #include "td/utils/common.h" #include "td/utils/List.h" +#include "td/utils/port/detail/NativeFd.h" #include "td/utils/port/detail/PollableFd.h" #include "td/utils/port/PollBase.h" #include "td/utils/port/PollFlags.h" diff --git a/tdutils/td/utils/port/detail/NativeFd.cpp b/tdutils/td/utils/port/detail/NativeFd.cpp index b36cb55d..2cf00195 100644 --- a/tdutils/td/utils/port/detail/NativeFd.cpp +++ b/tdutils/td/utils/port/detail/NativeFd.cpp @@ -16,8 +16,8 @@ #endif #if TD_FD_DEBUG -#include #include +#include #endif namespace td { @@ -26,7 +26,7 @@ namespace td { class FdSet { public: void on_create_fd(NativeFd::Fd fd) { - CHECK(fd >= 0); + CHECK(is_valid(fd)); if (is_stdio(fd)) { return; } @@ -38,7 +38,7 @@ class FdSet { } void on_release_fd(NativeFd::Fd fd) { - CHECK(fd >= 0); + CHECK(is_valid(fd)); if (is_stdio(fd)) { return; } @@ -46,7 +46,7 @@ class FdSet { } Status validate(NativeFd::Fd fd) { - if (fd < 0) { + if (!is_valid(fd)) { return Status::Error(PSLICE() << "Invalid fd: " << fd); } if (is_stdio(fd)) { @@ -60,7 +60,7 @@ class FdSet { } void on_close_fd(NativeFd::Fd fd) { - CHECK(fd >= 0); + CHECK(is_valid(fd)); if (is_stdio(fd)) { return; } @@ -75,8 +75,20 @@ class FdSet { std::mutex mutex_; std::set fds_; - bool is_stdio(NativeFd::Fd fd) { + bool is_stdio(NativeFd::Fd fd) const { +#if TD_PORT_WINDOWS + return fd == STD_INPUT_HANDLE || fd == STD_OUTPUT_HANDLE || fd == STD_ERROR_HANDLE; +#else return fd >= 0 && fd <= 2; +#endif + } + + bool is_valid(NativeFd::Fd fd) const { +#if TD_PORT_WINDOWS + return fd != INVALID_HANDLE_VALUE; +#else + return fd >= 0; +#endif } }; @@ -88,6 +100,7 @@ FdSet &get_fd_set() { } // namespace #endif + Status NativeFd::validate() const { #if TD_FD_DEBUG return get_fd_set().validate(fd_.get()); @@ -114,7 +127,9 @@ NativeFd::NativeFd(Socket socket) : fd_(reinterpret_cast(socket)), is_socket VLOG(fd) << *this << " create"; } #endif + NativeFd &NativeFd::operator=(NativeFd &&from) { + CHECK(this != &from); close(); fd_ = std::move(from.fd_); #if TD_PORT_WINDOWS