diff --git a/server/control_session.hpp b/server/control_session.hpp index 2fed18db..3b78f5a0 100644 --- a/server/control_session.hpp +++ b/server/control_session.hpp @@ -26,7 +26,6 @@ #include #include #include -#include #include #include diff --git a/server/control_session_http.cpp b/server/control_session_http.cpp index e7a13319..88d88c13 100644 --- a/server/control_session_http.cpp +++ b/server/control_session_http.cpp @@ -115,9 +115,8 @@ ControlSessionHttp::~ControlSessionHttp() void ControlSessionHttp::start() { - auto self = shared_from_this(); - http::async_read(socket_, buffer_, req_, - boost::asio::bind_executor(strand_, [this, self](boost::system::error_code ec, std::size_t bytes) { on_read(ec, bytes); })); + http::async_read(socket_, buffer_, req_, boost::asio::bind_executor(strand_, [ this, self = shared_from_this() ]( + boost::system::error_code ec, std::size_t bytes) { on_read(ec, bytes); })); } @@ -256,8 +255,7 @@ void ControlSessionHttp::on_read(beast::error_code ec, std::size_t bytes_transfe // Create a WebSocket session by transferring the socket // std::make_shared(std::move(socket_), state_)->run(std::move(req_)); ws_ = make_unique>(std::move(socket_)); - auto self = shared_from_this(); - ws_->async_accept(req_, [this, self](beast::error_code ec) { on_accept_ws(ec); }); + ws_->async_accept(req_, [ this, self = shared_from_this() ](beast::error_code ec) { on_accept_ws(ec); }); LOG(DEBUG) << "websocket upgrade\n"; return; } @@ -271,8 +269,8 @@ void ControlSessionHttp::on_read(beast::error_code ec, std::size_t bytes_transfe auto sp = std::make_shared(std::forward(response)); // Write the response - auto self = this->shared_from_this(); - http::async_write(this->socket_, *sp, boost::asio::bind_executor(strand_, [this, self, sp](beast::error_code ec, std::size_t bytes) { + http::async_write(this->socket_, *sp, + boost::asio::bind_executor(strand_, [ this, self = this->shared_from_this(), sp ](beast::error_code ec, std::size_t bytes) { this->on_write(ec, bytes, sp->need_eof()); })); }); @@ -315,7 +313,7 @@ void ControlSessionHttp::sendAsync(const std::string& message) if (!ws_) return; - strand_.post([this, message]() { + strand_.post([ this, self = shared_from_this(), message ]() { messages_.emplace_back(message); if (messages_.size() > 1) { @@ -331,9 +329,9 @@ void ControlSessionHttp::send_next() if (!ws_) return; - auto self(shared_from_this()); auto message = messages_.front(); - ws_->async_write(boost::asio::buffer(message), boost::asio::bind_executor(strand_, [this, self](std::error_code ec, std::size_t length) { + ws_->async_write(boost::asio::buffer(message), + boost::asio::bind_executor(strand_, [ this, self = shared_from_this() ](std::error_code ec, std::size_t length) { messages_.pop_front(); if (ec) { @@ -374,9 +372,9 @@ void ControlSessionHttp::on_accept_ws(beast::error_code ec) void ControlSessionHttp::do_read_ws() { // Read a message into our buffer - auto self(shared_from_this()); - ws_->async_read( - buffer_, boost::asio::bind_executor(strand_, [this, self](beast::error_code ec, std::size_t bytes_transferred) { on_read_ws(ec, bytes_transferred); })); + ws_->async_read(buffer_, boost::asio::bind_executor(strand_, [ this, self = shared_from_this() ](beast::error_code ec, std::size_t bytes_transferred) { + on_read_ws(ec, bytes_transferred); + })); } diff --git a/server/control_session_tcp.cpp b/server/control_session_tcp.cpp index 72af7046..3a057c78 100644 --- a/server/control_session_tcp.cpp +++ b/server/control_session_tcp.cpp @@ -41,9 +41,9 @@ ControlSessionTcp::~ControlSessionTcp() void ControlSessionTcp::do_read() { const std::string delimiter = "\n"; - auto self(shared_from_this()); boost::asio::async_read_until( - socket_, streambuf_, delimiter, boost::asio::bind_executor(strand_, [this, self, delimiter](const std::error_code& ec, std::size_t bytes_transferred) { + socket_, streambuf_, delimiter, + boost::asio::bind_executor(strand_, [ this, self = shared_from_this(), delimiter ](const std::error_code& ec, std::size_t bytes_transferred) { if (ec) { LOG(ERROR) << "Error while reading from control socket: " << ec.message() << "\n"; @@ -92,7 +92,7 @@ void ControlSessionTcp::stop() void ControlSessionTcp::sendAsync(const std::string& message) { - strand_.post([this, message]() { + strand_.post([ this, self = shared_from_this(), message ]() { messages_.emplace_back(message); if (messages_.size() > 1) { @@ -105,10 +105,9 @@ void ControlSessionTcp::sendAsync(const std::string& message) void ControlSessionTcp::send_next() { - auto self(shared_from_this()); auto message = messages_.front(); boost::asio::async_write(socket_, boost::asio::buffer(message + "\r\n"), - boost::asio::bind_executor(strand_, [this, self](std::error_code ec, std::size_t length) { + boost::asio::bind_executor(strand_, [ this, self = shared_from_this() ](std::error_code ec, std::size_t length) { messages_.pop_front(); if (ec) { diff --git a/server/stream_session.cpp b/server/stream_session.cpp index eb6645f2..71341c15 100644 --- a/server/stream_session.cpp +++ b/server/stream_session.cpp @@ -45,19 +45,8 @@ StreamSession::~StreamSession() void StreamSession::read_next() { - shared_ptr self; - try - { - self = shared_from_this(); - } - catch (const std::bad_weak_ptr& e) - { - LOG(ERROR, LOG_TAG) << "read_next: Error getting shared from this\n"; - return; - } - boost::asio::async_read(socket_, boost::asio::buffer(buffer_, base_msg_size_), - boost::asio::bind_executor(strand_, [this, self](boost::system::error_code ec, std::size_t length) mutable { + 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"; @@ -139,20 +128,9 @@ void StreamSession::stop() void StreamSession::send_next() { - shared_ptr self; - try - { - self = shared_from_this(); - } - catch (const std::bad_weak_ptr& e) - { - LOG(ERROR, LOG_TAG) << "send_next: Error getting shared from this\n"; - return; - } - auto buffer = messages_.front(); - - boost::asio::async_write(socket_, buffer, boost::asio::bind_executor(strand_, [this, self, buffer](boost::system::error_code ec, std::size_t length) { + boost::asio::async_write(socket_, buffer, + boost::asio::bind_executor(strand_, [ this, self = shared_from_this(), buffer ](boost::system::error_code ec, std::size_t length) { messages_.pop_front(); if (ec) { @@ -168,7 +146,7 @@ void StreamSession::send_next() void StreamSession::sendAsync(shared_const_buffer const_buf, bool send_now) { - strand_.post([this, const_buf, send_now]() { + strand_.post([ this, self = shared_from_this(), const_buf, send_now ]() { if (send_now) messages_.push_front(const_buf); else diff --git a/server/stream_session.hpp b/server/stream_session.hpp index 83b037b0..83334534 100644 --- a/server/stream_session.hpp +++ b/server/stream_session.hpp @@ -27,7 +27,6 @@ #include #include #include -#include #include #include #include diff --git a/server/streamreader/pcm_stream.hpp b/server/streamreader/pcm_stream.hpp index 22649bcb..45ed86f8 100644 --- a/server/streamreader/pcm_stream.hpp +++ b/server/streamreader/pcm_stream.hpp @@ -29,7 +29,6 @@ #include #include #include -#include #include