Add real NativeFd::set_is_blocking.

GitOrigin-RevId: a44de74e99cbe6161589e0d039f8fcb8b6e339c1
This commit is contained in:
levlam 2018-09-10 17:05:12 +03:00
parent 27b848f5c0
commit 0f4343d542
7 changed files with 24 additions and 18 deletions

View File

@ -19,19 +19,6 @@ Status Fd::duplicate(const Fd &from, Fd &to) {
return Status::OK(); return Status::OK();
} }
Status Fd::set_is_blocking(bool is_blocking) {
auto old_flags = fcntl(fd_, F_GETFL);
if (old_flags == -1) {
return OS_SOCKET_ERROR("Failed to get socket flags");
}
auto new_flags = is_blocking ? old_flags & ~O_NONBLOCK : old_flags | O_NONBLOCK;
if (new_flags != old_flags && fcntl(fd_, F_SETFL, new_flags) == -1) {
return OS_SOCKET_ERROR("Failed to set socket flags");
}
return Status::OK();
}
#endif #endif
#if TD_PORT_WINDOWS #if TD_PORT_WINDOWS

View File

@ -314,7 +314,7 @@ Result<ServerSocketFd> ServerSocketFd::open(int32 port, CSlice addr) {
return OS_SOCKET_ERROR("Failed to create a socket"); return OS_SOCKET_ERROR("Failed to create a socket");
} }
TRY_STATUS(fd.set_is_blocking(false)); TRY_STATUS(fd.set_is_blocking_unsafe(false));
auto sock = fd.socket(); auto sock = fd.socket();
linger ling = {0, 0}; linger ling = {0, 0};

View File

@ -455,7 +455,7 @@ Status get_socket_pending_error(const NativeFd &fd) {
#endif #endif
Status init_socket_options(NativeFd &native_fd) { Status init_socket_options(NativeFd &native_fd) {
TRY_STATUS(native_fd.set_is_blocking(false)); TRY_STATUS(native_fd.set_is_blocking_unsafe(false));
auto sock = native_fd.socket(); auto sock = native_fd.socket();
#if TD_PORT_POSIX #if TD_PORT_POSIX

View File

@ -761,7 +761,7 @@ Result<UdpSocketFd> UdpSocketFd::open(const IPAddress &address) {
if (!native_fd) { if (!native_fd) {
return OS_SOCKET_ERROR("Failed to create a socket"); return OS_SOCKET_ERROR("Failed to create a socket");
} }
TRY_STATUS(native_fd.set_is_blocking(false)); TRY_STATUS(native_fd.set_is_blocking_unsafe(false));
auto sock = native_fd.socket(); auto sock = native_fd.socket();
#if TD_PORT_POSIX #if TD_PORT_POSIX

View File

@ -41,8 +41,8 @@ void EventFdBsd::init() {
auto fd_a = NativeFd(fds[0]); auto fd_a = NativeFd(fds[0]);
auto fd_b = NativeFd(fds[1]); auto fd_b = NativeFd(fds[1]);
fd_a.set_is_blocking(false).ensure(); fd_a.set_is_blocking_unsafe(false).ensure();
fd_b.set_is_blocking(false).ensure(); fd_b.set_is_blocking_unsafe(false).ensure();
in_ = SocketFd::from_native_fd(std::move(fd_a)).move_as_ok(); in_ = SocketFd::from_native_fd(std::move(fd_a)).move_as_ok();
out_ = SocketFd::from_native_fd(std::move(fd_b)).move_as_ok(); out_ = SocketFd::from_native_fd(std::move(fd_b)).move_as_ok();

View File

@ -68,6 +68,23 @@ NativeFd::Raw NativeFd::socket() const {
#endif #endif
Status NativeFd::set_is_blocking(bool is_blocking) const { Status NativeFd::set_is_blocking(bool is_blocking) const {
#if TD_PORT_POSIX
auto old_flags = fcntl(fd(), F_GETFL);
if (old_flags == -1) {
return OS_SOCKET_ERROR("Failed to get socket flags");
}
auto new_flags = is_blocking ? old_flags & ~O_NONBLOCK : old_flags | O_NONBLOCK;
if (new_flags != old_flags && fcntl(fd(), F_SETFL, new_flags) == -1) {
return OS_SOCKET_ERROR("Failed to set socket flags");
}
return Status::OK();
#elif TD_PORT_WINDOWS
return set_is_blocking_unsafe(is_blocking);
#endif
}
Status NativeFd::set_is_blocking_unsafe(bool is_blocking) const {
#if TD_PORT_POSIX #if TD_PORT_POSIX
if (fcntl(fd(), F_SETFL, is_blocking ? 0 : O_NONBLOCK) == -1) { if (fcntl(fd(), F_SETFL, is_blocking ? 0 : O_NONBLOCK) == -1) {
#elif TD_PORT_WINDOWS #elif TD_PORT_WINDOWS

View File

@ -48,6 +48,8 @@ class NativeFd {
Status set_is_blocking(bool is_blocking) const; Status set_is_blocking(bool is_blocking) const;
Status set_is_blocking_unsafe(bool is_blocking) const; // may drop other Fd flags on non-Windows
void close(); void close();
Raw release(); Raw release();