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:
* Don't use typeid

View File

@ -103,9 +103,11 @@ to update to the latest Boost release.
* [issue 1019] Fix fallthrough warnings
* [issue 1024] Fix teardown for TIME_WAIT
[*API Changes]
* [issue 1029] Remove unintended public members of
* Remove unintended public members of
[link beast.ref.boost__beast__handler_ptr `handler_ptr`].
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.
* [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
`const` reference to the handler. Actions required: Change the
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);
// Run the I/O service. The call will return when
// the get operation is complete.
// the socket is closed.
ioc.run();
return EXIT_SUCCESS;

View File

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

View File

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

View File

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

View File

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