Move set_is_blocking method to NativeFd.

GitOrigin-RevId: 09040e5993647fb6626917bd3b07421e9a717af3
This commit is contained in:
levlam 2018-09-10 02:08:12 +03:00
parent fd90bf435e
commit cfea83b4c5
10 changed files with 26 additions and 44 deletions

View File

@ -790,7 +790,7 @@ class CliClient final : public Actor {
};
stdin_reader_ = create_actor_on_scheduler<StdinReader>("stdin_reader", stdin_id, actor_shared(this, 1));
#else
detail::set_native_socket_is_blocking(Stdin().get_native_fd(), false).ensure();
Stdin().get_native_fd().set_is_blocking(false).ensure();
#ifdef USE_READLINE
deactivate_readline();
rl_callback_handler_install(prompt, cb_linehandler);

View File

@ -1078,10 +1078,6 @@ Status Fd::duplicate(const Fd &from, Fd &to) {
return Status::Error("Not supported");
}
Status Fd::set_is_blocking(bool is_blocking) {
return detail::set_native_socket_is_blocking(get_native_socket(), is_blocking);
}
Fd::Fd(Type type, Mode mode, HANDLE handle) : mode_(mode), impl_(std::make_shared<FdImpl>(type, handle)) {
}
@ -1117,20 +1113,5 @@ static InitWSA init_wsa;
#endif
namespace detail {
#if TD_PORT_POSIX
Status set_native_socket_is_blocking(int fd, bool is_blocking) {
if (fcntl(fd, F_SETFL, is_blocking ? 0 : O_NONBLOCK) == -1) {
#elif TD_PORT_WINDOWS
Status set_native_socket_is_blocking(SOCKET fd, bool is_blocking) {
u_long mode = is_blocking;
if (ioctlsocket(fd, FIONBIO, &mode) != 0) {
#endif
return OS_SOCKET_ERROR("Failed to change socket flags");
}
return Status::OK();
}
} // namespace detail
} // namespace td
#endif

View File

@ -119,8 +119,6 @@ class Fd {
Result<size_t> write(Slice slice) TD_WARN_UNUSED_RESULT;
Result<size_t> read(MutableSlice slice) TD_WARN_UNUSED_RESULT;
Status set_is_blocking(bool is_blocking);
#if TD_PORT_POSIX
void update_flags_notify(Flags flags);
void clear_flags(Flags flags);

View File

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

View File

@ -433,18 +433,6 @@ void SocketFdImplDeleter::operator()(SocketFdImpl *impl) {
#endif
Status set_native_socket_is_blocking(const NativeFd &fd, bool is_blocking) {
#if TD_PORT_POSIX
if (fcntl(fd.fd(), F_SETFL, is_blocking ? 0 : O_NONBLOCK) == -1) {
#elif TD_PORT_WINDOWS
u_long mode = is_blocking;
if (ioctlsocket(fd.socket(), FIONBIO, &mode) != 0) {
#endif
return OS_SOCKET_ERROR("Failed to change socket flags");
}
return Status::OK();
}
#if TD_PORT_POSIX
Status get_socket_pending_error(const NativeFd &fd) {
int error = 0;
@ -470,7 +458,7 @@ SocketFd::~SocketFd() = default;
SocketFd::SocketFd(std::unique_ptr<detail::SocketFdImpl> impl) : impl_(impl.release()) {
}
Result<SocketFd> SocketFd::from_native_fd(NativeFd fd) {
TRY_STATUS(detail::set_native_socket_is_blocking(fd, false));
TRY_STATUS(fd.set_is_blocking(false));
auto sock = fd.socket();
// TODO remove copypaste
@ -492,10 +480,9 @@ Result<SocketFd> SocketFd::open(const IPAddress &address) {
if (!native_fd) {
return OS_SOCKET_ERROR("Failed to create a socket");
}
TRY_STATUS(native_fd.set_is_blocking(false));
auto sock = native_fd.socket();
TRY_STATUS(detail::set_native_socket_is_blocking(native_fd, false));
#if TD_PORT_POSIX
int flags = 1;
#elif TD_PORT_WINDOWS

View File

@ -65,7 +65,6 @@ class SocketFd {
};
namespace detail {
Status set_native_socket_is_blocking(const NativeFd &fd, bool is_blocking);
#if TD_PORT_POSIX
Status get_socket_pending_error(const NativeFd &fd);
#endif

View File

@ -758,10 +758,9 @@ Result<UdpSocketFd> UdpSocketFd::open(const IPAddress &address) {
if (!native_fd) {
return OS_SOCKET_ERROR("Failed to create a socket");
}
TRY_STATUS(native_fd.set_is_blocking(false));
TRY_STATUS(detail::set_native_socket_is_blocking(native_fd, false));
auto sock = native_fd.socket();
#if TD_PORT_POSIX
int flags = 1;
#elif TD_PORT_WINDOWS

View File

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

View File

@ -67,6 +67,18 @@ NativeFd::Raw NativeFd::socket() const {
}
#endif
Status NativeFd::set_is_blocking(bool is_blocking) const {
#if TD_PORT_POSIX
if (fcntl(fd(), F_SETFL, is_blocking ? 0 : O_NONBLOCK) == -1) {
#elif TD_PORT_WINDOWS
u_long mode = is_blocking;
if (ioctlsocket(socket(), FIONBIO, &mode) != 0) {
#endif
return OS_SOCKET_ERROR("Failed to change socket flags");
}
return Status::OK();
}
void NativeFd::close() {
if (!*this) {
return;

View File

@ -10,6 +10,7 @@
#include "td/utils/common.h"
#include "td/utils/MovableValue.h"
#include "td/utils/Status.h"
namespace td {
@ -32,7 +33,9 @@ class NativeFd {
~NativeFd();
explicit operator bool() const;
static Raw empty_raw();
Raw raw() const;
Raw fd() const;
#if TD_PORT_WINDOWS
@ -41,6 +44,9 @@ class NativeFd {
#elif TD_PORT_POSIX
Raw socket() const;
#endif
Status set_is_blocking(bool is_blocking) const;
void close();
Raw release();