libfilezilla
aio.cpp

Demonstrats use of aio readers/writersAccepts to filenames. Copies contents of first file into second file, calculatung file hash in the process.

#include <atomic>
class worker : public fz::event_handler
{
public:
worker(fz::logger_interface & logger, fz::event_loop & loop, std::unique_ptr<fz::reader_factory> && in_factory, std::unique_ptr<fz::writer_factory> && out_factory)
, logger_(logger)
, reader_factory_(in_factory)
, writer_factory_(out_factory)
{
if (!buffer_pool_) {
logger_.log(fz::logmsg::error, "Could not init buffer pool");
loop.stop();
return;
}
reader_ = reader_factory_->open(buffer_pool_);
if (!reader_) {
logger_.log(fz::logmsg::error, "Could not open %s", reader_factory_->name());
loop.stop();
return;
}
auto cb = [this](fz::writer_base const*, uint64_t written) {
written_ += written;
};
writer_ = writer_factory_->open(buffer_pool_, 0, cb);
if (!writer_) {
logger_.log(fz::logmsg::error, "Could not open %s", writer_factory_->name());
loop.stop();
return;
}
}
virtual ~worker() {
if (success_) {
logger_.log(fz::logmsg::status, "File copied successfully, wrote %d bytes", written_.load());
logger_.log(fz::logmsg::status, "Hash of data copied is %s", fz::hex_encode<std::string>(hash_.digest()));
}
else {
logger_.log(fz::logmsg::error, "Copy failed.");
}
}
virtual void operator()(fz::event_base const&) override
{
for (size_t i = 0; i < 10; ++i) {
auto [res, b] = reader_->get_buffer(*this);
if (res == fz::aio_result::error) {
event_loop_.stop();
return;
}
if (res != fz::aio_result::ok) {
return;
}
if (!b) {
auto res = writer_->finalize(*this);
if (res == fz::aio_result::wait) {
return;
}
else if (res == fz::aio_result::error) {
event_loop_.stop();
return;
}
success_ = true;
event_loop_.stop();
return;
}
hash_.update(b->get(), b->size());
done_ += b->size();
res = writer_->add_buffer(std::move(b), *this);
if (res == fz::aio_result::wait) {
return;
}
else if (res == fz::aio_result::error) {
event_loop_.stop();
return;
}
}
send_event<fz::aio_buffer_event>(nullptr);
}
fz::reader_factory_holder reader_factory_;
fz::writer_factory_holder writer_factory_;
uint64_t done_{};
fz::hash_accumulator hash_{fz::hash_algorithm::sha1};
fz::aio_buffer_pool buffer_pool_{logger_, 8};
std::unique_ptr<fz::reader_base> reader_;
std::unique_ptr<fz::writer_base> writer_;
std::atomic<uint64_t> written_{};
bool success_{};
};
int main(int argc, char* argv[])
{
if (argc != 3) {
logger.log(fz::logmsg::error, "Pass input and output filename");
return 1;
}
auto in = std::make_unique<fz::file_reader_factory>(fz::to_wstring(argv[1]), pool);
auto out = std::make_unique<fz::file_writer_factory>(fz::to_wstring(argv[2]), pool);
fz::event_loop loop(fz::event_loop::threadless);
worker w(logger, loop, std::move(in), std::move(out));
w.send_event<fz::aio_buffer_event>(nullptr);
loop.run();
return 0;
}
A buffer pool for use with async readers/writers.
Definition: aio.hpp:132
Common base class for all events.
Definition: event.hpp:23
Simple handler for asynchronous event processing.
Definition: event_handler.hpp:55
void remove_handler()
Deactivates handler, removes all pending events and stops all timers for this handler.
virtual void operator()(event_base const &)=0
Called by the event loop in the worker thread with the event to process.
A threaded event loop that supports sending events and timers.
Definition: event_loop.hpp:34
void stop(bool join=false)
Stops the loop.
Accumulator for hashing large amounts of data.
Definition: hash.hpp:26
Abstract interface for logging strings.
Definition: logger.hpp:51
void log(logmsg::type t, String &&fmt, Args &&...args)
Definition: logger.hpp:69
Holder for reader factories.
Definition: reader.hpp:180
This is the recommended event class.
Definition: event.hpp:68
A simple logger that writes to stdout.
Definition: logger.hpp:183
A dumb thread-pool for asynchronous tasks.
Definition: thread_pool.hpp:64
Base class for all writers.
Definition: writer.hpp:28
Definition: writer.hpp:170
Functions to encode/decode strings.
Declares the event_handler class.
A simple threaded event loop for the typesafe event system.
Collection of cryptographic hash and MAC functions.
Interface for logging.
@ status
Generic status messages aimed at the user.
Definition: logger.hpp:18
@ error
Error messages aimed at the user.
Definition: logger.hpp:21
@ ok
Success, proceed.
@ error
Operationf failed.
std::wstring to_wstring(std::string_view const &in)
Converts from std::string in system encoding into std::wstring.
Classes for reading data asynchronously from files.
Classes for writing data asynchronously into files.