2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2018-12-31 20:04:05 +01: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)
|
|
|
|
//
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
2018-08-14 09:42:40 +02:00
|
|
|
#include "td/utils/port/detail/PollableFd.h"
|
2019-07-06 13:29:15 +02:00
|
|
|
#include "td/utils/port/IoSlice.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Slice.h"
|
2019-07-06 13:29:15 +02:00
|
|
|
#include "td/utils/Span.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
// just reads from given reader and writes to given writer
|
|
|
|
template <class FdT>
|
|
|
|
class BufferedFdBase : public FdT {
|
|
|
|
public:
|
|
|
|
BufferedFdBase() = default;
|
2023-09-11 14:55:46 +02:00
|
|
|
explicit BufferedFdBase(FdT &&fd);
|
2018-12-31 20:04:05 +01:00
|
|
|
// TODO: make move constructor and move assignment safer
|
|
|
|
|
|
|
|
Result<size_t> flush_read(size_t max_read = std::numeric_limits<size_t>::max()) TD_WARN_UNUSED_RESULT;
|
|
|
|
Result<size_t> flush_write() TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
bool need_flush_write(size_t at_least = 0) {
|
2020-07-21 17:29:39 +02:00
|
|
|
return ready_for_flush_write() > at_least;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
size_t ready_for_flush_write() {
|
|
|
|
CHECK(write_);
|
|
|
|
write_->sync_with_writer();
|
|
|
|
return write_->size();
|
|
|
|
}
|
2020-07-21 17:29:39 +02:00
|
|
|
void sync_with_poll() {
|
|
|
|
::td::sync_with_poll(*this);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
void set_input_writer(ChainBufferWriter *read) {
|
|
|
|
read_ = read;
|
|
|
|
}
|
|
|
|
void set_output_reader(ChainBufferReader *write) {
|
|
|
|
write_ = write;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
ChainBufferWriter *read_ = nullptr;
|
|
|
|
ChainBufferReader *write_ = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class FdT>
|
2021-07-04 04:58:54 +02:00
|
|
|
class BufferedFd final : public BufferedFdBase<FdT> {
|
2018-12-31 20:04:05 +01:00
|
|
|
using Parent = BufferedFdBase<FdT>;
|
|
|
|
ChainBufferWriter input_writer_;
|
|
|
|
ChainBufferReader input_reader_;
|
|
|
|
ChainBufferWriter output_writer_;
|
|
|
|
ChainBufferReader output_reader_;
|
|
|
|
void init();
|
|
|
|
void init_ptr();
|
|
|
|
|
|
|
|
public:
|
|
|
|
BufferedFd();
|
2023-09-11 14:55:46 +02:00
|
|
|
explicit BufferedFd(FdT &&fd);
|
2021-10-18 13:36:15 +02:00
|
|
|
BufferedFd(BufferedFd &&) noexcept;
|
|
|
|
BufferedFd &operator=(BufferedFd &&) noexcept;
|
2018-12-31 20:04:05 +01:00
|
|
|
BufferedFd(const BufferedFd &) = delete;
|
|
|
|
BufferedFd &operator=(const BufferedFd &) = delete;
|
|
|
|
~BufferedFd();
|
|
|
|
|
|
|
|
void close();
|
2020-06-26 01:24:13 +02:00
|
|
|
|
|
|
|
size_t left_unread() const {
|
2020-06-24 13:47:36 +02:00
|
|
|
return input_reader_.size();
|
|
|
|
}
|
2020-06-26 01:24:13 +02:00
|
|
|
size_t left_unwritten() const {
|
2020-06-24 13:47:36 +02:00
|
|
|
return output_reader_.size();
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
Result<size_t> flush_read(size_t max_read = std::numeric_limits<size_t>::max()) TD_WARN_UNUSED_RESULT;
|
|
|
|
Result<size_t> flush_write() TD_WARN_UNUSED_RESULT;
|
|
|
|
|
|
|
|
// Yep, direct access to buffers. It is IO interface too.
|
|
|
|
ChainBufferReader &input_buffer();
|
|
|
|
ChainBufferWriter &output_buffer();
|
|
|
|
};
|
|
|
|
|
|
|
|
// IMPLEMENTATION
|
|
|
|
|
|
|
|
/*** BufferedFd ***/
|
|
|
|
template <class FdT>
|
2023-09-11 14:55:46 +02:00
|
|
|
BufferedFdBase<FdT>::BufferedFdBase(FdT &&fd) : FdT(std::move(fd)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class FdT>
|
|
|
|
Result<size_t> BufferedFdBase<FdT>::flush_read(size_t max_read) {
|
|
|
|
CHECK(read_);
|
|
|
|
size_t result = 0;
|
2020-07-21 17:29:39 +02:00
|
|
|
while (::td::can_read_local(*this) && max_read) {
|
2021-02-04 13:54:12 +01:00
|
|
|
MutableSlice slice = read_->prepare_append();
|
|
|
|
slice.truncate(max_read);
|
2018-12-31 20:04:05 +01:00
|
|
|
TRY_RESULT(x, FdT::read(slice));
|
|
|
|
slice.truncate(x);
|
|
|
|
read_->confirm_append(x);
|
|
|
|
result += x;
|
|
|
|
max_read -= x;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class FdT>
|
|
|
|
Result<size_t> BufferedFdBase<FdT>::flush_write() {
|
|
|
|
// TODO: sync on demand
|
|
|
|
write_->sync_with_writer();
|
2019-07-06 13:29:15 +02:00
|
|
|
size_t result = 0;
|
2020-07-21 17:29:39 +02:00
|
|
|
while (!write_->empty() && ::td::can_write_local(*this)) {
|
2020-06-22 01:40:05 +02:00
|
|
|
constexpr size_t BUF_SIZE = 20;
|
|
|
|
IoSlice buf[BUF_SIZE];
|
2019-07-06 13:29:15 +02:00
|
|
|
|
|
|
|
auto it = write_->clone();
|
|
|
|
size_t buf_i;
|
2020-06-22 01:40:05 +02:00
|
|
|
for (buf_i = 0; buf_i < BUF_SIZE; buf_i++) {
|
2019-07-06 13:29:15 +02:00
|
|
|
Slice slice = it.prepare_read();
|
|
|
|
if (slice.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf[buf_i] = as_io_slice(slice);
|
|
|
|
it.confirm_read(slice.size());
|
|
|
|
}
|
|
|
|
TRY_RESULT(x, FdT::writev(Span<IoSlice>(buf, buf_i)));
|
|
|
|
write_->advance(x);
|
2018-12-31 20:04:05 +01:00
|
|
|
result += x;
|
|
|
|
}
|
2023-04-04 14:31:40 +02:00
|
|
|
if (result == 0) {
|
|
|
|
if (write_->empty()) {
|
2023-04-04 15:39:20 +02:00
|
|
|
LOG(DEBUG) << "Nothing to write to " << FdT::get_poll_info().native_fd();
|
2023-04-04 14:31:40 +02:00
|
|
|
} else {
|
2023-04-04 15:39:20 +02:00
|
|
|
LOG(DEBUG) << "Can't flush write to " << FdT::get_poll_info().native_fd()
|
|
|
|
<< " with flags = " << FdT::get_poll_info().get_flags_local();
|
2023-04-04 14:31:40 +02:00
|
|
|
}
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*** BufferedFd ***/
|
|
|
|
template <class FdT>
|
|
|
|
void BufferedFd<FdT>::init() {
|
|
|
|
input_reader_ = input_writer_.extract_reader();
|
|
|
|
output_reader_ = output_writer_.extract_reader();
|
|
|
|
init_ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class FdT>
|
|
|
|
void BufferedFd<FdT>::init_ptr() {
|
|
|
|
this->set_input_writer(&input_writer_);
|
|
|
|
this->set_output_reader(&output_reader_);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class FdT>
|
|
|
|
BufferedFd<FdT>::BufferedFd() {
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class FdT>
|
2023-09-11 14:55:46 +02:00
|
|
|
BufferedFd<FdT>::BufferedFd(FdT &&fd) : Parent(std::move(fd)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class FdT>
|
2021-10-18 13:36:15 +02:00
|
|
|
BufferedFd<FdT>::BufferedFd(BufferedFd &&from) noexcept {
|
2018-12-31 20:04:05 +01:00
|
|
|
*this = std::move(from);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class FdT>
|
2021-10-18 13:36:15 +02:00
|
|
|
BufferedFd<FdT> &BufferedFd<FdT>::operator=(BufferedFd &&from) noexcept {
|
2018-12-31 20:04:05 +01:00
|
|
|
FdT::operator=(std::move(static_cast<FdT &>(from)));
|
|
|
|
input_reader_ = std::move(from.input_reader_);
|
|
|
|
input_writer_ = std::move(from.input_writer_);
|
|
|
|
output_reader_ = std::move(from.output_reader_);
|
|
|
|
output_writer_ = std::move(from.output_writer_);
|
|
|
|
init_ptr();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class FdT>
|
|
|
|
BufferedFd<FdT>::~BufferedFd() {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class FdT>
|
|
|
|
void BufferedFd<FdT>::close() {
|
|
|
|
FdT::close();
|
|
|
|
// TODO: clear buffers
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class FdT>
|
|
|
|
Result<size_t> BufferedFd<FdT>::flush_read(size_t max_read) {
|
|
|
|
TRY_RESULT(result, Parent::flush_read(max_read));
|
|
|
|
if (result) {
|
|
|
|
// TODO: faster sync is possible if you owns writer.
|
|
|
|
input_reader_.sync_with_writer();
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(DEBUG) << "Flush read: +" << format::as_size(result) << tag("total", format::as_size(input_reader_.size()));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class FdT>
|
|
|
|
Result<size_t> BufferedFd<FdT>::flush_write() {
|
|
|
|
TRY_RESULT(result, Parent::flush_write());
|
|
|
|
if (result) {
|
2019-02-21 16:58:20 +01:00
|
|
|
LOG(DEBUG) << "Flush write: +" << format::as_size(result) << tag("left", format::as_size(output_reader_.size()));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Yep, direct access to buffers. It is IO interface too.
|
|
|
|
template <class FdT>
|
|
|
|
ChainBufferReader &BufferedFd<FdT>::input_buffer() {
|
|
|
|
return input_reader_;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class FdT>
|
|
|
|
ChainBufferWriter &BufferedFd<FdT>::output_buffer() {
|
|
|
|
return output_writer_;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|