From 8b43d67a73af0b0e574b0375e50216756cfc5d8c Mon Sep 17 00:00:00 2001 From: Miguel Portilla Date: Fri, 24 Mar 2017 17:36:19 -0400 Subject: [PATCH] Cancel websocket timer on failure: This fixes a problem where the timeout timer would not be canceled with some errors. fix #2026 --- src/ripple/server/impl/BasePeer.h | 25 +------------------- src/ripple/server/impl/BaseWSPeer.h | 36 +++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 33 deletions(-) diff --git a/src/ripple/server/impl/BasePeer.h b/src/ripple/server/impl/BasePeer.h index e7eac1ade..ba4efc607 100644 --- a/src/ripple/server/impl/BasePeer.h +++ b/src/ripple/server/impl/BasePeer.h @@ -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 - 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 id{0}; return "##" + std::to_string(++id) + " "; }()) @@ -109,23 +103,6 @@ close() impl().ws_.lowest_layer().close(ec); } -template -template -void -BasePeer:: -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 diff --git a/src/ripple/server/impl/BaseWSPeer.h b/src/ripple/server/impl/BaseWSPeer.h index 24e83309f..124026e5a 100644 --- a/src/ripple/server/impl/BaseWSPeer.h +++ b/src/ripple/server/impl/BaseWSPeer.h @@ -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 ; - using BasePeer::fail; using BasePeer::strand_; - enum - { - // Max seconds without completing a message - timeoutSeconds = 30 - }; - private: friend class BasePeer; @@ -65,6 +58,7 @@ private: bool close_on_timer_ = false; bool ping_active_ = false; beast::websocket::ping_data payload_; + error_code ec_; public: template @@ -172,6 +166,10 @@ protected: void on_timer(error_code ec); + + template + 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 +template +void +BaseWSPeer:: +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