mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-10 23:56:43 +02:00
Use executors with implicit strand
This commit is contained in:
parent
fc4e863b84
commit
ffa742c49f
18 changed files with 124 additions and 138 deletions
|
@ -1,6 +1,6 @@
|
|||
/***
|
||||
This file is part of snapcast
|
||||
Copyright (C) 2014-2020 Johannes Pohl
|
||||
Copyright (C) 2014-2021 Johannes Pohl
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
|
@ -29,8 +29,8 @@ using namespace streamreader;
|
|||
static constexpr auto LOG_TAG = "StreamSessionTCP";
|
||||
|
||||
|
||||
StreamSessionTcp::StreamSessionTcp(boost::asio::io_context& ioc, StreamMessageReceiver* receiver, tcp::socket&& socket)
|
||||
: StreamSession(ioc, receiver), socket_(std::move(socket))
|
||||
StreamSessionTcp::StreamSessionTcp(StreamMessageReceiver* receiver, tcp::socket&& socket)
|
||||
: StreamSession(socket.get_executor(), receiver), socket_(std::move(socket))
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -80,37 +80,36 @@ std::string StreamSessionTcp::getIP()
|
|||
|
||||
void StreamSessionTcp::read_next()
|
||||
{
|
||||
boost::asio::async_read(socket_, boost::asio::buffer(buffer_, base_msg_size_),
|
||||
boost::asio::bind_executor(strand_, [this, self = shared_from_this()](boost::system::error_code ec, std::size_t length) mutable {
|
||||
if (ec)
|
||||
{
|
||||
LOG(ERROR, LOG_TAG) << "Error reading message header of length " << length << ": " << ec.message() << "\n";
|
||||
messageReceiver_->onDisconnect(this);
|
||||
return;
|
||||
}
|
||||
boost::asio::async_read(
|
||||
socket_, boost::asio::buffer(buffer_, base_msg_size_), [this, self = shared_from_this()](boost::system::error_code ec, std::size_t length) mutable {
|
||||
if (ec)
|
||||
{
|
||||
LOG(ERROR, LOG_TAG) << "Error reading message header of length " << length << ": " << ec.message() << "\n";
|
||||
messageReceiver_->onDisconnect(this);
|
||||
return;
|
||||
}
|
||||
|
||||
baseMessage_.deserialize(buffer_.data());
|
||||
LOG(DEBUG, LOG_TAG) << "getNextMessage: " << baseMessage_.type << ", size: " << baseMessage_.size << ", id: " << baseMessage_.id
|
||||
<< ", refers: " << baseMessage_.refersTo << "\n";
|
||||
if (baseMessage_.type > message_type::kLast)
|
||||
{
|
||||
LOG(ERROR, LOG_TAG) << "unknown message type received: " << baseMessage_.type << ", size: " << baseMessage_.size << "\n";
|
||||
messageReceiver_->onDisconnect(this);
|
||||
return;
|
||||
}
|
||||
else if (baseMessage_.size > msg::max_size)
|
||||
{
|
||||
LOG(ERROR, LOG_TAG) << "received message of type " << baseMessage_.type << " to large: " << baseMessage_.size << "\n";
|
||||
messageReceiver_->onDisconnect(this);
|
||||
return;
|
||||
}
|
||||
baseMessage_.deserialize(buffer_.data());
|
||||
LOG(DEBUG, LOG_TAG) << "getNextMessage: " << baseMessage_.type << ", size: " << baseMessage_.size << ", id: " << baseMessage_.id
|
||||
<< ", refers: " << baseMessage_.refersTo << "\n";
|
||||
if (baseMessage_.type > message_type::kLast)
|
||||
{
|
||||
LOG(ERROR, LOG_TAG) << "unknown message type received: " << baseMessage_.type << ", size: " << baseMessage_.size << "\n";
|
||||
messageReceiver_->onDisconnect(this);
|
||||
return;
|
||||
}
|
||||
else if (baseMessage_.size > msg::max_size)
|
||||
{
|
||||
LOG(ERROR, LOG_TAG) << "received message of type " << baseMessage_.type << " to large: " << baseMessage_.size << "\n";
|
||||
messageReceiver_->onDisconnect(this);
|
||||
return;
|
||||
}
|
||||
|
||||
if (baseMessage_.size > buffer_.size())
|
||||
buffer_.resize(baseMessage_.size);
|
||||
if (baseMessage_.size > buffer_.size())
|
||||
buffer_.resize(baseMessage_.size);
|
||||
|
||||
boost::asio::async_read(
|
||||
socket_, boost::asio::buffer(buffer_, baseMessage_.size),
|
||||
boost::asio::bind_executor(strand_, [this, self](boost::system::error_code ec, std::size_t length) mutable {
|
||||
boost::asio::async_read(socket_, boost::asio::buffer(buffer_, baseMessage_.size),
|
||||
[this, self](boost::system::error_code ec, std::size_t length) mutable {
|
||||
if (ec)
|
||||
{
|
||||
LOG(ERROR, LOG_TAG) << "Error reading message body of length " << length << ": " << ec.message() << "\n";
|
||||
|
@ -123,14 +122,13 @@ void StreamSessionTcp::read_next()
|
|||
if (messageReceiver_ != nullptr)
|
||||
messageReceiver_->onMessageReceived(this, baseMessage_, buffer_.data());
|
||||
read_next();
|
||||
}));
|
||||
}));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
void StreamSessionTcp::sendAsync(const shared_const_buffer& buffer, const WriteHandler& handler)
|
||||
{
|
||||
boost::asio::async_write(socket_, buffer,
|
||||
boost::asio::bind_executor(strand_, [self = shared_from_this(), buffer, handler](boost::system::error_code ec,
|
||||
std::size_t length) { handler(ec, length); }));
|
||||
[self = shared_from_this(), buffer, handler](boost::system::error_code ec, std::size_t length) { handler(ec, length); });
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue