Remove unused shared_from_this

This commit is contained in:
badaix 2020-01-08 21:22:56 +01:00
parent a01f61b3c8
commit 7b268b83df
8 changed files with 30 additions and 39 deletions

View file

@ -62,9 +62,8 @@ void PublishAvahi::publish(const std::vector<mDNSService>& services)
void PublishAvahi::poll()
{
auto self = shared_from_this();
timer_.expires_after(std::chrono::milliseconds(50));
timer_.async_wait([self, this](const boost::system::error_code& ec) {
timer_.async_wait([this](const boost::system::error_code& ec) {
if (!ec && (avahi_simple_poll_iterate(simple_poll, 0) == 0))
poll();
});

View file

@ -36,7 +36,7 @@ class PublishAvahi;
#include "publish_mdns.hpp"
class PublishAvahi : public PublishmDNS, public std::enable_shared_from_this<PublishAvahi>
class PublishAvahi : public PublishmDNS
{
public:
PublishAvahi(const std::string& serviceName, boost::asio::io_context& ioc);

View file

@ -239,7 +239,7 @@ int main(int argc, char* argv[])
boost::asio::io_context io_context;
#if defined(HAS_AVAHI) || defined(HAS_BONJOUR)
auto publishZeroConfg = std::make_shared<PublishZeroConf>("Snapcast", io_context);
auto publishZeroConfg = std::make_unique<PublishZeroConf>("Snapcast", io_context);
vector<mDNSService> dns_services;
dns_services.emplace_back("_snapcast._tcp", settings.stream.port);
dns_services.emplace_back("_snapcast-stream._tcp", settings.stream.port);

View file

@ -168,8 +168,7 @@ void StreamSession::send_next()
void StreamSession::sendAsync(shared_const_buffer const_buf, bool send_now)
{
auto self = shared_from_this();
strand_.post([this, self, const_buf, send_now]() {
strand_.post([this, const_buf, send_now]() {
if (send_now)
messages_.push_front(const_buf);
else

View file

@ -166,8 +166,7 @@ void AirplayStream::pipeReadLine()
}
const std::string delimiter = "\n";
auto self = shared_from_this();
boost::asio::async_read_until(*pipe_fd_, streambuf_pipe_, delimiter, [this, self, delimiter](const std::error_code& ec, std::size_t bytes_transferred) {
boost::asio::async_read_until(*pipe_fd_, streambuf_pipe_, delimiter, [this, delimiter](const std::error_code& ec, std::size_t bytes_transferred) {
if (ec)
{
LOG(ERROR, LOG_TAG) << "Error while reading from pipe: " << ec.message() << "\n";

View file

@ -28,7 +28,7 @@ namespace streamreader
{
template <typename ReadStream>
class AsioStream : public PcmStream, public std::enable_shared_from_this<AsioStream<ReadStream>>
class AsioStream : public PcmStream
{
public:
/// ctor. Encoded PCM data is passed to the PipeListener
@ -66,9 +66,8 @@ template <typename ReadStream>
template <typename Timer, typename Rep, typename Period>
void AsioStream<ReadStream>::wait(Timer& timer, const std::chrono::duration<Rep, Period>& duration, std::function<void()> handler)
{
auto self = this->shared_from_this();
timer.expires_after(duration);
timer.async_wait([ self, handler = std::move(handler) ](const boost::system::error_code& ec) {
timer.async_wait([handler = std::move(handler)](const boost::system::error_code& ec) {
if (ec)
{
LOG(ERROR, "AsioStream") << "Error during async wait: " << ec.message() << "\n";
@ -161,9 +160,8 @@ template <typename ReadStream>
void AsioStream<ReadStream>::do_read()
{
// LOG(DEBUG, "AsioStream") << "do_read\n";
auto self = this->shared_from_this();
boost::asio::async_read(
*stream_, boost::asio::buffer(chunk_->payload, chunk_->payloadSize), [this, self](boost::system::error_code ec, std::size_t length) mutable {
*stream_, boost::asio::buffer(chunk_->payload, chunk_->payloadSize), [this](boost::system::error_code ec, std::size_t length) mutable {
if (ec)
{
LOG(ERROR, "AsioStream") << "Error reading message: " << ec.message() << ", length: " << length << "\n";
@ -203,7 +201,7 @@ void AsioStream<ReadStream>::do_read()
if (nextTick_ >= currentTick)
{
read_timer_.expires_after(std::chrono::milliseconds(nextTick_ - currentTick));
read_timer_.async_wait([self, this](const boost::system::error_code& ec) {
read_timer_.async_wait([this](const boost::system::error_code& ec) {
if (ec)
{
LOG(ERROR, "AsioStream") << "Error during async wait: " << ec.message() << "\n";

View file

@ -147,29 +147,27 @@ void ProcessStream::onStderrMsg(const std::string& line)
void ProcessStream::stderrReadLine()
{
const std::string delimiter = "\n";
auto self(shared_from_this());
boost::asio::async_read_until(
*stream_stderr_, streambuf_stderr_, delimiter, [this, self, delimiter](const std::error_code& ec, std::size_t bytes_transferred) {
if (ec)
{
LOG(ERROR, LOG_TAG) << "Error while reading from stderr: " << ec.message() << "\n";
return;
}
boost::asio::async_read_until(*stream_stderr_, streambuf_stderr_, delimiter, [this, delimiter](const std::error_code& ec, std::size_t bytes_transferred) {
if (ec)
{
LOG(ERROR, LOG_TAG) << "Error while reading from stderr: " << ec.message() << "\n";
return;
}
if (watchdog_)
watchdog_->trigger();
if (watchdog_)
watchdog_->trigger();
// Extract up to the first delimiter.
std::string line{buffers_begin(streambuf_stderr_.data()), buffers_begin(streambuf_stderr_.data()) + bytes_transferred - delimiter.length()};
if (!line.empty())
{
if (line.back() == '\r')
line.resize(line.size() - 1);
onStderrMsg(line);
}
streambuf_stderr_.consume(bytes_transferred);
stderrReadLine();
});
// Extract up to the first delimiter.
std::string line{buffers_begin(streambuf_stderr_.data()), buffers_begin(streambuf_stderr_.data()) + bytes_transferred - delimiter.length()};
if (!line.empty())
{
if (line.back() == '\r')
line.resize(line.size() - 1);
onStderrMsg(line);
}
streambuf_stderr_.consume(bytes_transferred);
stderrReadLine();
});
}

View file

@ -68,11 +68,9 @@ void TcpStream::do_connect()
if (!active_)
return;
auto self = shared_from_this();
if (is_server_)
{
acceptor_->async_accept([this, self](boost::system::error_code ec, tcp::socket socket) {
acceptor_->async_accept([this](boost::system::error_code ec, tcp::socket socket) {
if (!ec)
{
LOG(DEBUG) << "New client connection\n";
@ -89,7 +87,7 @@ void TcpStream::do_connect()
{
stream_ = make_unique<tcp::socket>(ioc_);
boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::address::from_string(host_), port_);
stream_->async_connect(endpoint, [self, this](const boost::system::error_code& ec) {
stream_->async_connect(endpoint, [this](const boost::system::error_code& ec) {
if (!ec)
{
LOG(DEBUG) << "Connected\n";