tdlight/tdutils/td/utils/port/detail/NativeFd.cpp
levlam cfea83b4c5 Move set_is_blocking method to NativeFd.
GitOrigin-RevId: 09040e5993647fb6626917bd3b07421e9a717af3
2018-09-10 02:08:12 +03:00

110 lines
2.2 KiB
C++

//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/utils/port/detail/NativeFd.h"
#include "td/utils/format.h"
#include "td/utils/logging.h"
#include "td/utils/Status.h"
#if TD_PORT_POSIX
#include <unistd.h>
#endif
namespace td {
NativeFd::NativeFd(Raw raw) : fd_(raw) {
VLOG(fd) << *this << " create";
}
NativeFd::NativeFd(Raw raw, bool nolog) : fd_(raw) {
}
#if TD_PORT_WINDOWS
NativeFd::NativeFd(SOCKET raw) : fd_(reinterpret_cast<HANDLE>(raw)), is_socket_(true) {
VLOG(fd) << *this << " create";
}
#endif
NativeFd::~NativeFd() {
close();
}
NativeFd::operator bool() const {
return fd_.get() != empty_raw();
}
NativeFd::Raw NativeFd::empty_raw() {
#if TD_PORT_POSIX
return -1;
#elif TD_PORT_WINDOWS
return INVALID_HANDLE_VALUE;
#endif
}
NativeFd::Raw NativeFd::raw() const {
return fd_.get();
}
NativeFd::Raw NativeFd::fd() const {
return raw();
}
#if TD_PORT_WINDOWS
NativeFd::Raw NativeFd::io_handle() const {
return raw();
}
SOCKET NativeFd::socket() const {
CHECK(is_socket_);
return reinterpret_cast<SOCKET>(fd_.get());
}
#elif TD_PORT_POSIX
NativeFd::Raw NativeFd::socket() const {
return raw();
}
#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;
}
VLOG(fd) << *this << " close";
#if TD_PORT_WINDOWS
if (is_socket_ ? closesocket(socket()) : !CloseHandle(io_handle())) {
#elif TD_PORT_POSIX
if (::close(fd()) < 0) {
#endif
auto error = OS_ERROR("Close fd");
LOG(ERROR) << error;
}
fd_ = {};
}
NativeFd::Raw NativeFd::release() {
VLOG(fd) << *this << " release";
auto res = fd_.get();
fd_ = {};
return res;
}
StringBuilder &operator<<(StringBuilder &sb, const NativeFd &fd) {
return sb << tag("fd", fd.raw());
}
} // namespace td