mirror of https://github.com/libp2p/cpp-libp2p.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.1 KiB
42 lines
1.1 KiB
/**
|
|
* Copyright Soramitsu Co., Ltd. All Rights Reserved.
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
*/
|
|
|
|
#include "console_async_reader.hpp"
|
|
|
|
namespace libp2p::protocol::example::utility {
|
|
|
|
ConsoleAsyncReader::ConsoleAsyncReader(boost::asio::io_context &io,
|
|
Handler handler)
|
|
: in_(io, STDIN_FILENO), handler_(std::move(handler)) {
|
|
read();
|
|
}
|
|
|
|
void ConsoleAsyncReader::stop() {
|
|
stopped_ = true;
|
|
}
|
|
|
|
void ConsoleAsyncReader::read() {
|
|
input_.consume(input_.data().size());
|
|
boost::asio::async_read_until(
|
|
in_, input_, "\n",
|
|
[this](const boost::system::error_code &e, std::size_t size) {
|
|
onRead(e, size);
|
|
});
|
|
}
|
|
|
|
void ConsoleAsyncReader::onRead(const boost::system::error_code &e,
|
|
std::size_t size) {
|
|
if (stopped_) {
|
|
return;
|
|
}
|
|
if (!e && size != 0) {
|
|
line_.assign(buffers_begin(input_.data()), buffers_end(input_.data()));
|
|
line_.erase(line_.find_first_of("\r\n"));
|
|
handler_(line_);
|
|
}
|
|
read();
|
|
}
|
|
|
|
} // namespace libp2p::protocol::example::utility
|
|
|