Fix crash during client disconnect

This commit is contained in:
badaix 2020-01-25 22:20:02 +01:00
parent 1f1c00981b
commit a9b042f78e
6 changed files with 19 additions and 47 deletions

View file

@ -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)
{