2018-08-17 15:41:51 +02:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-08-17 15:41:51 +02:00
|
|
|
//
|
|
|
|
// 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/StdStreams.h"
|
|
|
|
|
2018-09-12 05:26:05 +02:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
2019-07-06 13:29:15 +02:00
|
|
|
#include "td/utils/port/detail/Iocp.h"
|
2019-07-23 00:50:12 +02:00
|
|
|
#include "td/utils/port/detail/NativeFd.h"
|
2020-07-21 17:29:39 +02:00
|
|
|
#include "td/utils/port/detail/PollableFd.h"
|
2020-07-21 18:48:01 +02:00
|
|
|
#include "td/utils/port/PollFlags.h"
|
2018-09-13 05:08:49 +02:00
|
|
|
#include "td/utils/port/thread.h"
|
2020-07-14 02:37:04 +02:00
|
|
|
#include "td/utils/ScopeGuard.h"
|
2018-09-12 05:26:05 +02:00
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-09-12 05:26:05 +02:00
|
|
|
|
|
|
|
#include <atomic>
|
2018-09-07 02:41:21 +02:00
|
|
|
|
2018-08-17 15:41:51 +02:00
|
|
|
namespace td {
|
|
|
|
|
2018-09-12 03:53:04 +02:00
|
|
|
#if TD_PORT_POSIX
|
|
|
|
template <int id>
|
|
|
|
static FileFd &get_file_fd() {
|
|
|
|
static FileFd result = FileFd::from_native_fd(NativeFd(id, true));
|
2020-12-24 15:16:56 +01:00
|
|
|
static auto guard = ScopeExit() + [&] {
|
2020-06-26 01:24:13 +02:00
|
|
|
result.move_as_native_fd().release();
|
|
|
|
};
|
2018-09-12 03:53:04 +02:00
|
|
|
return result;
|
2018-08-17 15:41:51 +02:00
|
|
|
}
|
2018-09-12 03:53:04 +02:00
|
|
|
|
2018-08-17 15:41:51 +02:00
|
|
|
FileFd &Stdin() {
|
2018-09-12 03:53:04 +02:00
|
|
|
return get_file_fd<0>();
|
2018-08-17 15:41:51 +02:00
|
|
|
}
|
|
|
|
FileFd &Stdout() {
|
2018-09-12 03:53:04 +02:00
|
|
|
return get_file_fd<1>();
|
2018-08-17 15:41:51 +02:00
|
|
|
}
|
|
|
|
FileFd &Stderr() {
|
2018-09-12 03:53:04 +02:00
|
|
|
return get_file_fd<2>();
|
|
|
|
}
|
2018-08-17 15:41:51 +02:00
|
|
|
#elif TD_PORT_WINDOWS
|
2018-09-12 03:53:04 +02:00
|
|
|
template <DWORD id>
|
|
|
|
static FileFd &get_file_fd() {
|
|
|
|
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
|
|
|
|
static auto handle = GetStdHandle(id);
|
|
|
|
LOG_IF(FATAL, handle == INVALID_HANDLE_VALUE) << "Failed to GetStdHandle " << id;
|
2022-10-03 14:11:23 +02:00
|
|
|
static FileFd result = handle == nullptr ? FileFd() : FileFd::from_native_fd(NativeFd(handle, true));
|
2020-06-26 01:24:13 +02:00
|
|
|
static auto guard = ScopeExit() + [&] {
|
2022-10-03 14:11:23 +02:00
|
|
|
if (handle != nullptr) {
|
|
|
|
result.move_as_native_fd().release();
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
};
|
2018-09-12 03:53:04 +02:00
|
|
|
#else
|
|
|
|
static FileFd result;
|
2018-08-17 15:41:51 +02:00
|
|
|
#endif
|
2018-09-12 03:53:04 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileFd &Stdin() {
|
|
|
|
return get_file_fd<STD_INPUT_HANDLE>();
|
|
|
|
}
|
|
|
|
FileFd &Stdout() {
|
|
|
|
return get_file_fd<STD_OUTPUT_HANDLE>();
|
|
|
|
}
|
|
|
|
FileFd &Stderr() {
|
|
|
|
return get_file_fd<STD_ERROR_HANDLE>();
|
2018-08-17 15:41:51 +02:00
|
|
|
}
|
2018-09-12 03:53:04 +02:00
|
|
|
#endif
|
2018-09-10 18:21:34 +02:00
|
|
|
|
2018-09-12 03:53:04 +02:00
|
|
|
#if TD_PORT_WINDOWS
|
2018-09-10 18:21:34 +02:00
|
|
|
namespace detail {
|
2021-07-04 04:58:54 +02:00
|
|
|
class BufferedStdinImpl final : private Iocp::Callback {
|
2018-09-10 18:21:34 +02:00
|
|
|
public:
|
2018-09-12 03:53:04 +02:00
|
|
|
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
|
2018-09-11 16:48:29 +02:00
|
|
|
BufferedStdinImpl() : info_(NativeFd(GetStdHandle(STD_INPUT_HANDLE), true)) {
|
2019-07-06 13:29:15 +02:00
|
|
|
iocp_ref_ = Iocp::get()->get_ref();
|
2019-09-15 05:19:46 +02:00
|
|
|
read_thread_ = thread([this] { this->read_loop(); });
|
2018-09-10 18:21:34 +02:00
|
|
|
}
|
2018-09-12 03:53:04 +02:00
|
|
|
#else
|
|
|
|
BufferedStdinImpl() {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
#endif
|
2018-09-10 20:45:34 +02:00
|
|
|
BufferedStdinImpl(const BufferedStdinImpl &) = delete;
|
|
|
|
BufferedStdinImpl &operator=(const BufferedStdinImpl &) = delete;
|
|
|
|
BufferedStdinImpl(BufferedStdinImpl &&) = delete;
|
|
|
|
BufferedStdinImpl &operator=(BufferedStdinImpl &&) = delete;
|
2018-09-10 18:21:34 +02:00
|
|
|
~BufferedStdinImpl() {
|
2018-09-11 16:48:29 +02:00
|
|
|
info_.move_as_native_fd().release();
|
2018-09-10 18:21:34 +02:00
|
|
|
}
|
|
|
|
void close() {
|
|
|
|
close_flag_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ChainBufferReader &input_buffer() {
|
|
|
|
return reader_;
|
|
|
|
}
|
|
|
|
|
|
|
|
PollableFdInfo &get_poll_info() {
|
2018-09-11 16:48:29 +02:00
|
|
|
return info_;
|
2018-09-10 18:21:34 +02:00
|
|
|
}
|
|
|
|
const PollableFdInfo &get_poll_info() const {
|
2018-09-11 16:48:29 +02:00
|
|
|
return info_;
|
2018-09-10 18:21:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result<size_t> flush_read(size_t max_read = std::numeric_limits<size_t>::max()) TD_WARN_UNUSED_RESULT {
|
2020-07-21 17:29:39 +02:00
|
|
|
info_.sync_with_poll();
|
2018-09-11 16:48:29 +02:00
|
|
|
info_.clear_flags(PollFlags::Read());
|
2018-09-10 18:21:34 +02:00
|
|
|
reader_.sync_with_writer();
|
|
|
|
return reader_.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-09-11 16:48:29 +02:00
|
|
|
PollableFdInfo info_;
|
2018-09-10 18:21:34 +02:00
|
|
|
ChainBufferWriter writer_;
|
|
|
|
ChainBufferReader reader_ = writer_.extract_reader();
|
2019-09-15 05:19:46 +02:00
|
|
|
thread read_thread_;
|
2018-09-10 18:21:34 +02:00
|
|
|
std::atomic<bool> close_flag_{false};
|
2019-07-06 13:29:15 +02:00
|
|
|
IocpRef iocp_ref_;
|
|
|
|
std::atomic<int> refcnt_{1};
|
2018-09-10 18:21:34 +02:00
|
|
|
|
|
|
|
void read_loop() {
|
|
|
|
while (!close_flag_) {
|
|
|
|
auto slice = writer_.prepare_append();
|
2018-09-11 16:48:29 +02:00
|
|
|
auto r_size = read(slice);
|
|
|
|
if (r_size.is_error()) {
|
|
|
|
LOG(ERROR) << "Stop read stdin loop: " << r_size.error();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
writer_.confirm_append(r_size.ok());
|
2020-12-24 17:17:06 +01:00
|
|
|
inc_refcnt();
|
|
|
|
if (!iocp_ref_.post(0, this, nullptr)) {
|
|
|
|
dec_refcnt();
|
2019-07-06 13:29:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!iocp_ref_.post(0, this, nullptr)) {
|
2020-12-24 18:49:08 +01:00
|
|
|
read_thread_.detach();
|
2019-07-06 13:29:15 +02:00
|
|
|
dec_refcnt();
|
|
|
|
}
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_iocp(Result<size_t> r_size, WSAOVERLAPPED *overlapped) final {
|
2019-09-15 05:19:46 +02:00
|
|
|
info_.add_flags_from_poll(PollFlags::Read());
|
2019-07-06 13:29:15 +02:00
|
|
|
dec_refcnt();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool dec_refcnt() {
|
|
|
|
if (--refcnt_ == 0) {
|
|
|
|
delete this;
|
|
|
|
return true;
|
2018-09-10 18:21:34 +02:00
|
|
|
}
|
2019-07-06 13:29:15 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void inc_refcnt() {
|
|
|
|
CHECK(refcnt_ != 0);
|
|
|
|
refcnt_++;
|
2018-09-10 18:21:34 +02:00
|
|
|
}
|
2018-09-11 16:48:29 +02:00
|
|
|
|
|
|
|
Result<size_t> read(MutableSlice slice) {
|
2018-09-11 18:57:50 +02:00
|
|
|
auto native_fd = info_.native_fd().fd();
|
2018-09-11 16:48:29 +02:00
|
|
|
DWORD bytes_read = 0;
|
|
|
|
auto res = ReadFile(native_fd, slice.data(), narrow_cast<DWORD>(slice.size()), &bytes_read, nullptr);
|
|
|
|
if (res) {
|
|
|
|
return static_cast<size_t>(bytes_read);
|
|
|
|
}
|
2018-10-07 01:47:50 +02:00
|
|
|
return OS_ERROR(PSLICE() << "Read from " << info_.native_fd() << " has failed");
|
2018-09-11 16:48:29 +02:00
|
|
|
}
|
2018-09-10 18:21:34 +02:00
|
|
|
};
|
|
|
|
void BufferedStdinImplDeleter::operator()(BufferedStdinImpl *impl) {
|
2019-07-06 13:29:15 +02:00
|
|
|
// LOG(ERROR) << "Close";
|
2018-09-10 18:21:34 +02:00
|
|
|
impl->close();
|
|
|
|
}
|
|
|
|
} // namespace detail
|
2018-09-12 03:53:04 +02:00
|
|
|
#elif TD_PORT_POSIX
|
2018-09-10 18:21:34 +02:00
|
|
|
namespace detail {
|
|
|
|
class BufferedStdinImpl {
|
|
|
|
public:
|
|
|
|
BufferedStdinImpl() {
|
2018-09-11 15:27:20 +02:00
|
|
|
file_fd_ = FileFd::from_native_fd(NativeFd(Stdin().get_native_fd().fd()));
|
2018-09-10 18:21:34 +02:00
|
|
|
file_fd_.get_native_fd().set_is_blocking(false);
|
|
|
|
}
|
2018-09-10 20:45:34 +02:00
|
|
|
BufferedStdinImpl(const BufferedStdinImpl &) = delete;
|
|
|
|
BufferedStdinImpl &operator=(const BufferedStdinImpl &) = delete;
|
|
|
|
BufferedStdinImpl(BufferedStdinImpl &&) = delete;
|
|
|
|
BufferedStdinImpl &operator=(BufferedStdinImpl &&) = delete;
|
2018-09-10 18:21:34 +02:00
|
|
|
~BufferedStdinImpl() {
|
2019-03-21 23:59:16 +01:00
|
|
|
file_fd_.get_native_fd().set_is_blocking(true);
|
2018-09-10 18:21:34 +02:00
|
|
|
file_fd_.move_as_native_fd().release();
|
|
|
|
}
|
|
|
|
|
|
|
|
ChainBufferReader &input_buffer() {
|
|
|
|
return reader_;
|
|
|
|
}
|
|
|
|
|
|
|
|
PollableFdInfo &get_poll_info() {
|
|
|
|
return file_fd_.get_poll_info();
|
|
|
|
}
|
|
|
|
const PollableFdInfo &get_poll_info() const {
|
|
|
|
return file_fd_.get_poll_info();
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<size_t> flush_read(size_t max_read = std::numeric_limits<size_t>::max()) TD_WARN_UNUSED_RESULT {
|
|
|
|
size_t result = 0;
|
2020-07-21 17:29:39 +02:00
|
|
|
::td::sync_with_poll(*this);
|
|
|
|
while (::td::can_read_local(*this) && max_read) {
|
2021-02-04 13:54:12 +01:00
|
|
|
MutableSlice slice = writer_.prepare_append();
|
|
|
|
slice.truncate(max_read);
|
2018-09-10 18:21:34 +02:00
|
|
|
TRY_RESULT(x, file_fd_.read(slice));
|
|
|
|
slice.truncate(x);
|
|
|
|
writer_.confirm_append(x);
|
|
|
|
result += x;
|
|
|
|
max_read -= x;
|
|
|
|
}
|
|
|
|
if (result) {
|
|
|
|
reader_.sync_with_writer();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
FileFd file_fd_;
|
|
|
|
ChainBufferWriter writer_;
|
|
|
|
ChainBufferReader reader_ = writer_.extract_reader();
|
|
|
|
};
|
|
|
|
void BufferedStdinImplDeleter::operator()(BufferedStdinImpl *impl) {
|
|
|
|
delete impl;
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
#endif
|
2018-09-10 20:45:34 +02:00
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
BufferedStdin::BufferedStdin() : impl_(make_unique<detail::BufferedStdinImpl>().release()) {
|
2018-09-10 18:21:34 +02:00
|
|
|
}
|
2021-10-18 13:36:15 +02:00
|
|
|
BufferedStdin::BufferedStdin(BufferedStdin &&) noexcept = default;
|
|
|
|
BufferedStdin &BufferedStdin::operator=(BufferedStdin &&) noexcept = default;
|
2018-09-10 18:21:34 +02:00
|
|
|
BufferedStdin::~BufferedStdin() = default;
|
|
|
|
|
|
|
|
ChainBufferReader &BufferedStdin::input_buffer() {
|
|
|
|
return impl_->input_buffer();
|
|
|
|
}
|
|
|
|
PollableFdInfo &BufferedStdin::get_poll_info() {
|
|
|
|
return impl_->get_poll_info();
|
|
|
|
}
|
|
|
|
const PollableFdInfo &BufferedStdin::get_poll_info() const {
|
|
|
|
return impl_->get_poll_info();
|
|
|
|
}
|
|
|
|
Result<size_t> BufferedStdin::flush_read(size_t max_read) {
|
|
|
|
return impl_->flush_read(max_read);
|
|
|
|
}
|
|
|
|
|
2018-08-17 15:41:51 +02:00
|
|
|
} // namespace td
|