From cb006ba03c9bf67e6e7cf6a4a8387d387aa4bd3c Mon Sep 17 00:00:00 2001 From: theoreticalbts Date: Wed, 12 Aug 2015 13:35:36 -0400 Subject: [PATCH] api_connection.hpp: Possibly fix previous commit, see https://github.com/cryptonomex/graphene/issues/232 --- include/fc/rpc/api_connection.hpp | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/include/fc/rpc/api_connection.hpp b/include/fc/rpc/api_connection.hpp index c7c752f..c06d2d0 100644 --- a/include/fc/rpc/api_connection.hpp +++ b/include/fc/rpc/api_connection.hpp @@ -22,7 +22,7 @@ namespace fc { public: typedef typename std::function::result_type result_type; - callback_functor( std::shared_ptr< fc::api_connection > con, uint64_t id ) + callback_functor( std::weak_ptr< fc::api_connection > con, uint64_t id ) :_callback_id(id),_api_connection(con){} template @@ -30,7 +30,7 @@ namespace fc { private: uint64_t _callback_id; - std::shared_ptr< fc::api_connection > _api_connection; + std::weak_ptr< fc::api_connection > _api_connection; }; template @@ -91,11 +91,9 @@ namespace fc { return _methods[method_id](args); } - std::shared_ptr< fc::api_connection > get_connection() + std::weak_ptr< fc::api_connection > get_connection() { - std::shared_ptr< fc::api_connection > locked = _api_connection.lock(); - FC_ASSERT( locked, "connection closed" ); - return locked; + return _api_connection; } std::vector get_method_names()const @@ -396,7 +394,11 @@ namespace fc { template typename callback_functor::result_type callback_functor::operator()( Args... args )const { - _api_connection->send_callback( _callback_id, fc::variants{ args... } ).template as< result_type >(); + std::shared_ptr< fc::api_connection > locked = _api_connection.lock(); + // TODO: make new exception type for this instead of recycling eof_exception + if( !locked ) + throw fc::eof_exception(); + locked->send_callback( _callback_id, fc::variants{ args... } ).template as< result_type >(); } @@ -406,17 +408,21 @@ namespace fc { public: typedef void result_type; - callback_functor( std::shared_ptr< fc::api_connection > con, uint64_t id ) + callback_functor( std::weak_ptr< fc::api_connection > con, uint64_t id ) :_callback_id(id),_api_connection(con){} void operator()( Args... args )const { - _api_connection->send_notice( _callback_id, fc::variants{ args... } ); + std::shared_ptr< fc::api_connection > locked = _api_connection.lock(); + // TODO: make new exception type for this instead of recycling eof_exception + if( !locked ) + throw fc::eof_exception(); + locked->send_notice( _callback_id, fc::variants{ args... } ); } private: uint64_t _callback_id; - std::shared_ptr< fc::api_connection > _api_connection; + std::weak_ptr< fc::api_connection > _api_connection; }; } // namespace detail