api_connection.hpp: Properly lock connection object for callbacks
This commit is contained in:
parent
458b601774
commit
3cd9ed4386
1 changed files with 14 additions and 9 deletions
|
|
@ -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( fc::api_connection& con, uint64_t id )
|
callback_functor( std::shared_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;
|
||||||
fc::api_connection& _api_connection;
|
std::shared_ptr< fc::api_connection > _api_connection;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename R, typename Arg0, typename ... Args>
|
template<typename R, typename Arg0, typename ... Args>
|
||||||
|
|
@ -91,7 +91,12 @@ namespace fc {
|
||||||
return _methods[method_id](args);
|
return _methods[method_id](args);
|
||||||
}
|
}
|
||||||
|
|
||||||
fc::api_connection& get_connection(){ auto tmp = _api_connection.lock(); FC_ASSERT( tmp, "connection closed"); return *tmp; }
|
std::shared_ptr< fc::api_connection > get_connection()
|
||||||
|
{
|
||||||
|
std::shared_ptr< fc::api_connection > locked = _api_connection.lock();
|
||||||
|
FC_ASSERT( locked, "connection closed" );
|
||||||
|
return locked;
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<std::string> get_method_names()const
|
std::vector<std::string> get_method_names()const
|
||||||
{
|
{
|
||||||
|
|
@ -391,7 +396,7 @@ 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 >();
|
_api_connection->send_callback( _callback_id, fc::variants{ args... } ).template as< result_type >();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -401,17 +406,17 @@ namespace fc {
|
||||||
public:
|
public:
|
||||||
typedef void result_type;
|
typedef void result_type;
|
||||||
|
|
||||||
callback_functor( fc::api_connection& con, uint64_t id )
|
callback_functor( std::shared_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... } );
|
_api_connection->send_notice( _callback_id, fc::variants{ args... } );
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint64_t _callback_id;
|
uint64_t _callback_id;
|
||||||
fc::api_connection& _api_connection;
|
std::shared_ptr< fc::api_connection > _api_connection;
|
||||||
};
|
};
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue