Fix crash during client disconnect

This commit is contained in:
badaix 2020-01-25 22:20:02 +01:00
parent 08de66fe70
commit 21d259d0af
6 changed files with 19 additions and 47 deletions

View file

@ -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<websocket_session>(std::move(socket_), state_)->run(std::move(req_));
ws_ = make_unique<websocket::stream<beast::tcp_stream>>(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<response_type>(std::forward<decltype(response)>(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);
}));
}