Cancel websocket timer on failure:

This fixes a problem where the timeout timer
would not be canceled with some errors.
fix #2026
This commit is contained in:
Miguel Portilla
2017-03-24 17:36:19 -04:00
committed by seelabs
parent 128f7cefb1
commit 8b43d67a73
2 changed files with 28 additions and 33 deletions

View File

@@ -50,7 +50,6 @@ protected:
boost::asio::io_service::work work_;
boost::asio::io_service::strand strand_;
error_code ec_;
public:
BasePeer(Port const& port, Handler& handler,
@@ -61,11 +60,6 @@ public:
void
close() override;
protected:
template<class String>
void
fail(error_code ec, String const& what);
private:
Impl&
impl()
@@ -87,7 +81,7 @@ BasePeer(Port const& port, Handler& handler,
, remote_address_(remote_address)
, sink_(journal.sink(),
[]
{
{
static std::atomic<unsigned> id{0};
return "##" + std::to_string(++id) + " ";
}())
@@ -109,23 +103,6 @@ close()
impl().ws_.lowest_layer().close(ec);
}
template<class Handler, class Impl>
template<class String>
void
BasePeer<Handler, Impl>::
fail(error_code ec, String const& what)
{
assert(strand_.running_in_this_thread());
if(! ec_ &&
ec != boost::asio::error::operation_aborted)
{
ec_ = ec;
JLOG(j_.trace()) <<
what << ": " << ec.message();
impl().ws_.lowest_layer().close(ec);
}
}
} // ripple
#endif

View File

@@ -42,15 +42,8 @@ protected:
using error_code = boost::system::error_code;
using endpoint_type = boost::asio::ip::tcp::endpoint;
using waitable_timer = boost::asio::basic_waitable_timer <clock_type>;
using BasePeer<Handler, Impl>::fail;
using BasePeer<Handler, Impl>::strand_;
enum
{
// Max seconds without completing a message
timeoutSeconds = 30
};
private:
friend class BasePeer<Handler, Impl>;
@@ -65,6 +58,7 @@ private:
bool close_on_timer_ = false;
bool ping_active_ = false;
beast::websocket::ping_data payload_;
error_code ec_;
public:
template<class Body, class Headers>
@@ -172,6 +166,10 @@ protected:
void
on_timer(error_code ec);
template<class String>
void
fail(error_code ec, String const& what);
};
//------------------------------------------------------------------------------
@@ -447,9 +445,10 @@ on_timer(error_code ec)
return;
if(! ec)
{
if(! close_on_timer_ || !ping_active_)
if(! close_on_timer_ || ! ping_active_)
{
start_timer();
close_on_timer_ = true;
ping_active_ = true;
// cryptographic is probably overkill..
beast::rngfill(payload_.begin(),
@@ -460,7 +459,7 @@ on_timer(error_code ec)
impl().shared_from_this(),
std::placeholders::_1)));
JLOG(this->j_.trace()) <<
"sent pong";
"sent ping";
return;
}
ec = boost::system::errc::make_error_code(
@@ -469,6 +468,25 @@ on_timer(error_code ec)
fail(ec, "timer");
}
template<class Handler, class Impl>
template<class String>
void
BaseWSPeer<Handler, Impl>::
fail(error_code ec, String const& what)
{
assert(strand_.running_in_this_thread());
cancel_timer();
if(! ec_ &&
ec != boost::asio::error::operation_aborted)
{
ec_ = ec;
JLOG(this->j_.trace()) <<
what << ": " << ec.message();
impl().ws_.lowest_layer().close(ec);
}
}
} // ripple
#endif