Fix crashes when destroying json_connection, tcp_socket

Also, create on_close callback on json_connection, so clients can
know when the connection has failed.
This commit is contained in:
Nathan Hourt 2015-02-12 10:54:18 -05:00
parent 13430fce12
commit de2000795d
3 changed files with 31 additions and 9 deletions

View file

@ -29,6 +29,11 @@ namespace fc { namespace rpc {
*/
future<void> exec();
bool is_open();
void close();
void set_on_disconnected_callback(std::function<void(fc::exception_ptr)> callback);
logger get_logger()const;
void set_logger( const logger& l );

View file

@ -27,7 +27,12 @@ namespace fc {
~impl()
{
if( _sock.is_open() )
try
{
_sock.close();
}
catch( ... )
{}
if( _read_in_progress.valid() )
try
{

View file

@ -30,7 +30,7 @@ namespace fc { namespace rpc {
boost::unordered_map<std::string, json_connection::named_param_method> _named_param_methods;
fc::mutex _write_mutex;
//std::function<void(fc::exception_ptr)> _on_close;
std::function<void(fc::exception_ptr)> _on_close;
logger _logger;
@ -226,6 +226,8 @@ namespace fc { namespace rpc {
void close( fc::exception_ptr e )
{
wlog( "close ${reason}", ("reason", e->to_detail_string() ) );
if( _on_close )
_on_close(e);
for( auto itr = _awaiting.begin(); itr != _awaiting.end(); ++itr )
{
itr->second->set_exception( e->dynamic_copy_exception() );
@ -239,6 +241,20 @@ namespace fc { namespace rpc {
{}
json_connection::~json_connection()
{
close();
}
fc::future<void> json_connection::exec()
{
if( my->_done.valid() )
{
FC_THROW_EXCEPTION( assert_exception, "start should only be called once" );
}
return my->_done = fc::async( [=](){ my->read_loop(); }, "json_connection read_loop" );
}
void json_connection::close()
{
try
{
@ -260,13 +276,9 @@ namespace fc { namespace rpc {
}
}
fc::future<void> json_connection::exec()
void json_connection::set_on_disconnected_callback(std::function<void (exception_ptr)> callback)
{
if( my->_done.valid() )
{
FC_THROW_EXCEPTION( assert_exception, "start should only be called once" );
}
return my->_done = fc::async( [=](){ my->read_loop(); }, "json_connection read_loop" );
my->_on_close = callback;
}
void json_connection::add_method( const fc::string& name, method m )