Skip to content

Commit

Permalink
Adjust use of strand to work better with composite asio operations. r…
Browse files Browse the repository at this point in the history
…eferences zaphoyd#459

particularly affected TLS servers and connection close behavior
  • Loading branch information
Peter Thorson committed Feb 8, 2016
1 parent b76422a commit ee343fb
Showing 1 changed file with 65 additions and 39 deletions.
104 changes: 65 additions & 39 deletions websocketpp/transport/asio/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,21 +464,12 @@ class connection : public config::socket_type::socket_con_type {
if (config::enable_multithreading) {
m_strand = lib::make_shared<lib::asio::io_service::strand>(
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);
Expand Down Expand Up @@ -876,15 +867,28 @@ class connection : public config::socket_type::socket_con_type {
"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,
Expand Down Expand Up @@ -935,14 +939,25 @@ class connection : public config::socket_type::socket_con_type {

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<buffer> const & bufs, write_handler handler) {
Expand All @@ -960,14 +975,25 @@ class connection : public config::socket_type::socket_con_type {

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
Expand Down

0 comments on commit ee343fb

Please sign in to comment.