Move set_is_blocking method to NativeFd.
GitOrigin-RevId: 09040e5993647fb6626917bd3b07421e9a717af3
This commit is contained in:
parent
fd90bf435e
commit
cfea83b4c5
@ -790,7 +790,7 @@ class CliClient final : public Actor {
|
|||||||
};
|
};
|
||||||
stdin_reader_ = create_actor_on_scheduler<StdinReader>("stdin_reader", stdin_id, actor_shared(this, 1));
|
stdin_reader_ = create_actor_on_scheduler<StdinReader>("stdin_reader", stdin_id, actor_shared(this, 1));
|
||||||
#else
|
#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
|
#ifdef USE_READLINE
|
||||||
deactivate_readline();
|
deactivate_readline();
|
||||||
rl_callback_handler_install(prompt, cb_linehandler);
|
rl_callback_handler_install(prompt, cb_linehandler);
|
||||||
|
@ -1078,10 +1078,6 @@ Status Fd::duplicate(const Fd &from, Fd &to) {
|
|||||||
return Status::Error("Not supported");
|
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)) {
|
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
|
#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
|
} // namespace td
|
||||||
#endif
|
#endif
|
||||||
|
@ -119,8 +119,6 @@ class Fd {
|
|||||||
Result<size_t> write(Slice slice) TD_WARN_UNUSED_RESULT;
|
Result<size_t> write(Slice slice) TD_WARN_UNUSED_RESULT;
|
||||||
Result<size_t> read(MutableSlice 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
|
#if TD_PORT_POSIX
|
||||||
void update_flags_notify(Flags flags);
|
void update_flags_notify(Flags flags);
|
||||||
void clear_flags(Flags flags);
|
void clear_flags(Flags flags);
|
||||||
|
@ -312,7 +312,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(detail::set_native_socket_is_blocking(fd, false));
|
TRY_STATUS(fd.set_is_blocking(false));
|
||||||
auto sock = fd.socket();
|
auto sock = fd.socket();
|
||||||
|
|
||||||
linger ling = {0, 0};
|
linger ling = {0, 0};
|
||||||
|
@ -433,18 +433,6 @@ void SocketFdImplDeleter::operator()(SocketFdImpl *impl) {
|
|||||||
|
|
||||||
#endif
|
#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
|
#if TD_PORT_POSIX
|
||||||
Status get_socket_pending_error(const NativeFd &fd) {
|
Status get_socket_pending_error(const NativeFd &fd) {
|
||||||
int error = 0;
|
int error = 0;
|
||||||
@ -470,7 +458,7 @@ SocketFd::~SocketFd() = default;
|
|||||||
SocketFd::SocketFd(std::unique_ptr<detail::SocketFdImpl> impl) : impl_(impl.release()) {
|
SocketFd::SocketFd(std::unique_ptr<detail::SocketFdImpl> impl) : impl_(impl.release()) {
|
||||||
}
|
}
|
||||||
Result<SocketFd> SocketFd::from_native_fd(NativeFd fd) {
|
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();
|
auto sock = fd.socket();
|
||||||
|
|
||||||
// TODO remove copypaste
|
// TODO remove copypaste
|
||||||
@ -492,10 +480,9 @@ Result<SocketFd> SocketFd::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));
|
||||||
|
|
||||||
auto sock = native_fd.socket();
|
auto sock = native_fd.socket();
|
||||||
|
|
||||||
TRY_STATUS(detail::set_native_socket_is_blocking(native_fd, false));
|
|
||||||
|
|
||||||
#if TD_PORT_POSIX
|
#if TD_PORT_POSIX
|
||||||
int flags = 1;
|
int flags = 1;
|
||||||
#elif TD_PORT_WINDOWS
|
#elif TD_PORT_WINDOWS
|
||||||
|
@ -65,7 +65,6 @@ class SocketFd {
|
|||||||
};
|
};
|
||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
Status set_native_socket_is_blocking(const NativeFd &fd, bool is_blocking);
|
|
||||||
#if TD_PORT_POSIX
|
#if TD_PORT_POSIX
|
||||||
Status get_socket_pending_error(const NativeFd &fd);
|
Status get_socket_pending_error(const NativeFd &fd);
|
||||||
#endif
|
#endif
|
||||||
|
@ -758,10 +758,9 @@ 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(detail::set_native_socket_is_blocking(native_fd, false));
|
|
||||||
auto sock = native_fd.socket();
|
auto sock = native_fd.socket();
|
||||||
|
|
||||||
#if TD_PORT_POSIX
|
#if TD_PORT_POSIX
|
||||||
int flags = 1;
|
int flags = 1;
|
||||||
#elif TD_PORT_WINDOWS
|
#elif TD_PORT_WINDOWS
|
||||||
|
@ -40,8 +40,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]);
|
||||||
detail::set_native_socket_is_blocking(fd_a, false).ensure();
|
fd_a.set_is_blocking(false).ensure();
|
||||||
detail::set_native_socket_is_blocking(fd_b, false).ensure();
|
fd_b.set_is_blocking(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();
|
||||||
|
@ -67,6 +67,18 @@ NativeFd::Raw NativeFd::socket() const {
|
|||||||
}
|
}
|
||||||
#endif
|
#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() {
|
void NativeFd::close() {
|
||||||
if (!*this) {
|
if (!*this) {
|
||||||
return;
|
return;
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
#include "td/utils/common.h"
|
#include "td/utils/common.h"
|
||||||
#include "td/utils/MovableValue.h"
|
#include "td/utils/MovableValue.h"
|
||||||
|
#include "td/utils/Status.h"
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
@ -32,7 +33,9 @@ class NativeFd {
|
|||||||
~NativeFd();
|
~NativeFd();
|
||||||
|
|
||||||
explicit operator bool() const;
|
explicit operator bool() const;
|
||||||
|
|
||||||
static Raw empty_raw();
|
static Raw empty_raw();
|
||||||
|
|
||||||
Raw raw() const;
|
Raw raw() const;
|
||||||
Raw fd() const;
|
Raw fd() const;
|
||||||
#if TD_PORT_WINDOWS
|
#if TD_PORT_WINDOWS
|
||||||
@ -41,6 +44,9 @@ class NativeFd {
|
|||||||
#elif TD_PORT_POSIX
|
#elif TD_PORT_POSIX
|
||||||
Raw socket() const;
|
Raw socket() const;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
Status set_is_blocking(bool is_blocking) const;
|
||||||
|
|
||||||
void close();
|
void close();
|
||||||
Raw release();
|
Raw release();
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user