api_connection.hpp: Possibly fix previous commit, see https://github.com/cryptonomex/graphene/issues/232

This commit is contained in:
theoreticalbts 2015-08-12 13:35:36 -04:00
parent 3cd9ed4386
commit cb006ba03c

View file

@ -22,7 +22,7 @@ namespace fc {
public: public:
typedef typename std::function<Signature>::result_type result_type; typedef typename std::function<Signature>::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){} :_callback_id(id),_api_connection(con){}
template<typename... Args> template<typename... Args>
@ -30,7 +30,7 @@ namespace fc {
private: private:
uint64_t _callback_id; uint64_t _callback_id;
std::shared_ptr< fc::api_connection > _api_connection; std::weak_ptr< fc::api_connection > _api_connection;
}; };
template<typename R, typename Arg0, typename ... Args> template<typename R, typename Arg0, typename ... Args>
@ -91,11 +91,9 @@ namespace fc {
return _methods[method_id](args); 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(); return _api_connection;
FC_ASSERT( locked, "connection closed" );
return locked;
} }
std::vector<std::string> get_method_names()const std::vector<std::string> get_method_names()const
@ -396,7 +394,11 @@ namespace fc {
template<typename... Args> template<typename... Args>
typename callback_functor<Signature>::result_type callback_functor<Signature>::operator()( Args... args )const typename callback_functor<Signature>::result_type callback_functor<Signature>::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: public:
typedef void result_type; 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){} :_callback_id(id),_api_connection(con){}
void operator()( Args... args )const 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: private:
uint64_t _callback_id; uint64_t _callback_id;
std::shared_ptr< fc::api_connection > _api_connection; std::weak_ptr< fc::api_connection > _api_connection;
}; };
} // namespace detail } // namespace detail