Fix teardown for TIME_WAIT

fix #1024, fix #1029
This commit is contained in:
Vinnie Falco
2018-02-20 03:33:02 -08:00
parent de8d0b6843
commit 593ccb15cd
7 changed files with 82 additions and 52 deletions

View File

@ -1,3 +1,9 @@
Version 157:
* Fix teardown for TIME_WAIT
--------------------------------------------------------------------------------
Version 156: Version 156:
* Don't use typeid * Don't use typeid

View File

@ -103,9 +103,11 @@ to update to the latest Boost release.
* [issue 1019] Fix fallthrough warnings * [issue 1019] Fix fallthrough warnings
* [issue 1024] Fix teardown for TIME_WAIT
[*API Changes] [*API Changes]
* [issue 1029] Remove unintended public members of * Remove unintended public members of
[link beast.ref.boost__beast__handler_ptr `handler_ptr`]. [link beast.ref.boost__beast__handler_ptr `handler_ptr`].
Actions required: don't call non-public members. Actions required: don't call non-public members.
@ -115,7 +117,7 @@ to update to the latest Boost release.
to manage state can only be moved, not copied. to manage state can only be moved, not copied.
* [link beast.ref.boost__beast__handler_ptr `handler_ptr`] * [link beast.ref.boost__beast__handler_ptr `handler_ptr`]
gives the strong exception guarantee. The signature gives the strong exception guarantee. The constructor signature
for managed objects constructed by `handler_ptr` now receives a for managed objects constructed by `handler_ptr` now receives a
`const` reference to the handler. Actions required: Change the `const` reference to the handler. Actions required: Change the
constructor signature for state objects used with `handler_ptr` constructor signature for state objects used with `handler_ptr`

View File

@ -225,7 +225,7 @@ int main(int argc, char** argv)
std::make_shared<session>(ioc, ctx)->run(host, port, text); std::make_shared<session>(ioc, ctx)->run(host, port, text);
// Run the I/O service. The call will return when // Run the I/O service. The call will return when
// the get operation is complete. // the socket is closed.
ioc.run(); ioc.run();
return EXIT_SUCCESS; return EXIT_SUCCESS;

View File

@ -199,7 +199,7 @@ int main(int argc, char** argv)
std::make_shared<session>(ioc)->run(host, port, text); std::make_shared<session>(ioc)->run(host, port, text);
// Run the I/O service. The call will return when // Run the I/O service. The call will return when
// the get operation is complete. // the socket is closed.
ioc.run(); ioc.run();
return EXIT_SUCCESS; return EXIT_SUCCESS;

View File

@ -137,7 +137,7 @@ int main(int argc, char** argv)
std::placeholders::_1)); std::placeholders::_1));
// Run the I/O service. The call will return when // Run the I/O service. The call will return when
// the get operation is complete. // the socket is closed.
ioc.run(); ioc.run();
return EXIT_SUCCESS; return EXIT_SUCCESS;

View File

@ -119,7 +119,7 @@ int main(int argc, char** argv)
std::placeholders::_1)); std::placeholders::_1));
// Run the I/O service. The call will return when // Run the I/O service. The call will return when
// the get operation is complete. // the socket is closed.
ioc.run(); ioc.run();
return EXIT_SUCCESS; return EXIT_SUCCESS;

View File

@ -14,6 +14,7 @@
#include <boost/beast/core/type_traits.hpp> #include <boost/beast/core/type_traits.hpp>
#include <boost/asio/associated_allocator.hpp> #include <boost/asio/associated_allocator.hpp>
#include <boost/asio/associated_executor.hpp> #include <boost/asio/associated_executor.hpp>
#include <boost/asio/coroutine.hpp>
#include <boost/asio/handler_continuation_hook.hpp> #include <boost/asio/handler_continuation_hook.hpp>
#include <boost/asio/post.hpp> #include <boost/asio/post.hpp>
#include <memory> #include <memory>
@ -25,7 +26,7 @@ namespace websocket {
namespace detail { namespace detail {
template<class Handler> template<class Handler>
class teardown_tcp_op class teardown_tcp_op : public boost::asio::coroutine
{ {
using socket_type = using socket_type =
boost::asio::ip::tcp::socket; boost::asio::ip::tcp::socket;
@ -33,7 +34,7 @@ class teardown_tcp_op
Handler h_; Handler h_;
socket_type& s_; socket_type& s_;
role_type role_; role_type role_;
int step_ = 0; bool nb_;
public: public:
teardown_tcp_op(teardown_tcp_op&& other) = default; teardown_tcp_op(teardown_tcp_op&& other) = default;
@ -73,65 +74,72 @@ public:
operator()( operator()(
error_code ec = {}, error_code ec = {},
std::size_t bytes_transferred = 0); std::size_t bytes_transferred = 0);
friend
bool asio_handler_is_continuation(teardown_tcp_op* op)
{
using boost::asio::asio_handler_is_continuation;
return op->step_ >= 3 ||
asio_handler_is_continuation(std::addressof(op->h_));
}
}; };
template<class Handler> template<class Handler>
void void
teardown_tcp_op<Handler>:: teardown_tcp_op<Handler>::
operator()(error_code ec, std::size_t) operator()(error_code ec, std::size_t bytes_transferred)
{ {
using boost::asio::buffer; using boost::asio::buffer;
using tcp = boost::asio::ip::tcp; using tcp = boost::asio::ip::tcp;
switch(step_) BOOST_ASIO_CORO_REENTER(*this)
{ {
case 0: nb_ = s_.non_blocking();
s_.non_blocking(true, ec); s_.non_blocking(true, ec);
if(! ec)
{
if(role_ == role_type::server)
s_.shutdown(tcp::socket::shutdown_send, ec);
}
if(ec) if(ec)
{ {
step_ = 1; BOOST_ASIO_CORO_YIELD
return boost::asio::post( boost::asio::post(
s_.get_executor(), s_.get_executor(),
bind_handler(std::move(*this), ec, 0)); bind_handler(std::move(*this), ec, 0));
goto upcall;
} }
step_ = 2; for(;;)
if(role_ == role_type::server)
s_.shutdown(tcp::socket::shutdown_send, ec);
goto do_read;
case 1:
break;
case 2:
step_ = 3; BOOST_FALLTHROUGH;
case 3:
if(ec != boost::asio::error::would_block)
break;
{ {
char buf[2048]; {
s_.read_some( char buf[2048];
boost::asio::buffer(buf), ec); s_.read_some(
boost::asio::buffer(buf), ec);
}
if(ec == boost::asio::error::would_block)
{
BOOST_ASIO_CORO_YIELD
s_.async_wait(
boost::asio::ip::tcp::socket::wait_read,
std::move(*this));
continue;
}
if(ec) if(ec)
{
if(ec != boost::asio::error::eof)
goto upcall;
ec = {};
break; break;
}
if(bytes_transferred == 0)
{
// happens sometimes
break;
}
} }
if(role_ == role_type::client)
do_read: s_.shutdown(tcp::socket::shutdown_send, ec);
return s_.async_wait( if(ec)
boost::asio::ip::tcp::socket::wait_read, goto upcall;
std::move(*this)); s_.close(ec);
upcall:
{
error_code ignored;
s_.non_blocking(nb_, ignored);
}
h_(ec);
} }
if(role_ == role_type::client)
s_.shutdown(tcp::socket::shutdown_send, ec);
s_.close(ec);
h_(ec);
} }
} // detail } // detail
@ -149,17 +157,31 @@ teardown(
if(role == role_type::server) if(role == role_type::server)
socket.shutdown( socket.shutdown(
boost::asio::ip::tcp::socket::shutdown_send, ec); boost::asio::ip::tcp::socket::shutdown_send, ec);
while(! ec) if(ec)
return;
for(;;)
{ {
char buf[8192]; char buf[2048];
auto const n = socket.read_some( auto const bytes_transferred =
buffer(buf), ec); socket.read_some(buffer(buf), ec);
if(! n) if(ec)
{
if(ec != boost::asio::error::eof)
return;
ec = {};
break; break;
}
if(bytes_transferred == 0)
{
// happens sometimes
break;
}
} }
if(role == role_type::client) if(role == role_type::client)
socket.shutdown( socket.shutdown(
boost::asio::ip::tcp::socket::shutdown_send, ec); boost::asio::ip::tcp::socket::shutdown_send, ec);
if(ec)
return;
socket.close(ec); socket.close(ec);
} }