Reformat code

This commit is contained in:
badaix 2022-12-29 19:10:38 +01:00
parent c6518a4709
commit e1c8250876
32 changed files with 505 additions and 351 deletions

View file

@ -51,32 +51,35 @@ ControlSessionTcp::~ControlSessionTcp()
void ControlSessionTcp::do_read()
{
const std::string delimiter = "\n";
boost::asio::async_read_until(
socket_, streambuf_, delimiter, [this, self = shared_from_this(), delimiter](const std::error_code& ec, std::size_t bytes_transferred) {
if (ec)
{
LOG(ERROR, LOG_TAG) << "Error while reading from control socket: " << ec.message() << "\n";
return;
}
boost::asio::async_read_until(socket_, streambuf_, delimiter,
[this, self = shared_from_this(), delimiter](const std::error_code& ec, std::size_t bytes_transferred)
{
if (ec)
{
LOG(ERROR, LOG_TAG) << "Error while reading from control socket: " << ec.message() << "\n";
return;
}
// Extract up to the first delimiter.
std::string line{buffers_begin(streambuf_.data()), buffers_begin(streambuf_.data()) + bytes_transferred - delimiter.length()};
if (!line.empty())
// Extract up to the first delimiter.
std::string line{buffers_begin(streambuf_.data()), buffers_begin(streambuf_.data()) + bytes_transferred - delimiter.length()};
if (!line.empty())
{
if (line.back() == '\r')
line.resize(line.size() - 1);
// LOG(DEBUG, LOG_TAG) << "received: " << line << "\n";
if ((message_receiver_ != nullptr) && !line.empty())
{
if (line.back() == '\r')
line.resize(line.size() - 1);
// LOG(DEBUG, LOG_TAG) << "received: " << line << "\n";
if ((message_receiver_ != nullptr) && !line.empty())
{
message_receiver_->onMessageReceived(shared_from_this(), line, [this](const std::string& response) {
if (!response.empty())
sendAsync(response);
});
}
message_receiver_->onMessageReceived(shared_from_this(), line,
[this](const std::string& response)
{
if (!response.empty())
sendAsync(response);
});
}
streambuf_.consume(bytes_transferred);
do_read();
});
}
streambuf_.consume(bytes_transferred);
do_read();
});
}
@ -102,7 +105,9 @@ void ControlSessionTcp::stop()
void ControlSessionTcp::sendAsync(const std::string& message)
{
boost::asio::post(strand_, [this, self = shared_from_this(), message]() {
boost::asio::post(strand_,
[this, self = shared_from_this(), message]()
{
messages_.emplace_back(message + "\r\n");
if (messages_.size() > 1)
{
@ -115,7 +120,9 @@ void ControlSessionTcp::sendAsync(const std::string& message)
void ControlSessionTcp::send_next()
{
boost::asio::async_write(socket_, boost::asio::buffer(messages_.front()), [this, self = shared_from_this()](std::error_code ec, std::size_t length) {
boost::asio::async_write(socket_, boost::asio::buffer(messages_.front()),
[this, self = shared_from_this()](std::error_code ec, std::size_t length)
{
messages_.pop_front();
if (ec)
{