From ee343fb282e2ad0b2b46fb2bf4d830547ac52aa3 Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Mon, 8 Feb 2016 07:31:06 -0500 Subject: [PATCH] Adjust use of strand to work better with composite asio operations. references #459 particularly affected TLS servers and connection close behavior --- websocketpp/transport/asio/connection.hpp | 104 ++++++++++++++-------- 1 file changed, 65 insertions(+), 39 deletions(-) diff --git a/websocketpp/transport/asio/connection.hpp b/websocketpp/transport/asio/connection.hpp index b9f92ef..8121598 100644 --- a/websocketpp/transport/asio/connection.hpp +++ b/websocketpp/transport/asio/connection.hpp @@ -464,21 +464,12 @@ protected: if (config::enable_multithreading) { m_strand = lib::make_shared( lib::ref(*io_service)); - - m_async_read_handler = m_strand->wrap(lib::bind( - &type::handle_async_read, get_shared(),lib::placeholders::_1, - lib::placeholders::_2)); - - m_async_write_handler = m_strand->wrap(lib::bind( - &type::handle_async_write, get_shared(),lib::placeholders::_1, - lib::placeholders::_2)); - } else { - m_async_read_handler = lib::bind(&type::handle_async_read, - get_shared(), lib::placeholders::_1, lib::placeholders::_2); - - m_async_write_handler = lib::bind(&type::handle_async_write, - get_shared(), lib::placeholders::_1, lib::placeholders::_2); } + m_async_read_handler = lib::bind(&type::handle_async_read, + get_shared(), lib::placeholders::_1, lib::placeholders::_2); + + m_async_write_handler = lib::bind(&type::handle_async_write, + get_shared(), lib::placeholders::_1, lib::placeholders::_2); lib::error_code ec = socket_con_type::init_asio(io_service, m_strand, m_is_server); @@ -876,15 +867,28 @@ protected: "asio con async_read_at_least called with bad handler"); } - lib::asio::async_read( - socket_con_type::get_socket(), - lib::asio::buffer(buf,len), - lib::asio::transfer_at_least(num_bytes), - make_custom_alloc_handler( - m_read_handler_allocator, - m_async_read_handler - ) - ); + if (config::enable_multithreading) { + lib::asio::async_read( + socket_con_type::get_socket(), + lib::asio::buffer(buf,len), + lib::asio::transfer_at_least(num_bytes), + m_strand->wrap( + make_custom_alloc_handler( + m_read_handler_allocator, + m_async_read_handler)) + ); + } else { + lib::asio::async_read( + socket_con_type::get_socket(), + lib::asio::buffer(buf,len), + lib::asio::transfer_at_least(num_bytes), + make_custom_alloc_handler( + m_read_handler_allocator, + m_async_read_handler + ) + ); + } + } void handle_async_read(lib::asio::error_code const & ec, @@ -935,14 +939,25 @@ protected: m_write_handler = handler; - lib::asio::async_write( - socket_con_type::get_socket(), - m_bufs, - make_custom_alloc_handler( - m_write_handler_allocator, - m_async_write_handler - ) - ); + if (config::enable_multithreading) { + lib::asio::async_write( + socket_con_type::get_socket(), + m_bufs, + m_strand->wrap( + make_custom_alloc_handler( + m_write_handler_allocator, + m_async_write_handler)) + ); + } else { + lib::asio::async_write( + socket_con_type::get_socket(), + m_bufs, + make_custom_alloc_handler( + m_write_handler_allocator, + m_async_write_handler + ) + ); + } } void async_write(std::vector const & bufs, write_handler handler) { @@ -960,14 +975,25 @@ protected: m_write_handler = handler; - lib::asio::async_write( - socket_con_type::get_socket(), - m_bufs, - make_custom_alloc_handler( - m_write_handler_allocator, - m_async_write_handler - ) - ); + if (config::enable_multithreading) { + lib::asio::async_write( + socket_con_type::get_socket(), + m_bufs, + m_strand->wrap( + make_custom_alloc_handler( + m_write_handler_allocator, + m_async_write_handler)) + ); + } else { + lib::asio::async_write( + socket_con_type::get_socket(), + m_bufs, + make_custom_alloc_handler( + m_write_handler_allocator, + m_async_write_handler + ) + ); + } } /// Async write callback