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(); future<void> exec();
bool is_open();
void close();
void set_on_disconnected_callback(std::function<void(fc::exception_ptr)> callback);
logger get_logger()const; logger get_logger()const;
void set_logger( const logger& l ); void set_logger( const logger& l );

View file

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

View file

@ -30,7 +30,7 @@ namespace fc { namespace rpc {
boost::unordered_map<std::string, json_connection::named_param_method> _named_param_methods; boost::unordered_map<std::string, json_connection::named_param_method> _named_param_methods;
fc::mutex _write_mutex; fc::mutex _write_mutex;
//std::function<void(fc::exception_ptr)> _on_close; std::function<void(fc::exception_ptr)> _on_close;
logger _logger; logger _logger;
@ -226,6 +226,8 @@ namespace fc { namespace rpc {
void close( fc::exception_ptr e ) void close( fc::exception_ptr e )
{ {
wlog( "close ${reason}", ("reason", e->to_detail_string() ) ); wlog( "close ${reason}", ("reason", e->to_detail_string() ) );
if( _on_close )
_on_close(e);
for( auto itr = _awaiting.begin(); itr != _awaiting.end(); ++itr ) for( auto itr = _awaiting.begin(); itr != _awaiting.end(); ++itr )
{ {
itr->second->set_exception( e->dynamic_copy_exception() ); itr->second->set_exception( e->dynamic_copy_exception() );
@ -239,6 +241,20 @@ namespace fc { namespace rpc {
{} {}
json_connection::~json_connection() 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 try
{ {
@ -250,7 +266,7 @@ namespace fc { namespace rpc {
my->_out->close(); my->_out->close();
my->_done.wait(); my->_done.wait();
} }
} }
catch ( fc::canceled_exception& ){} // expected exception catch ( fc::canceled_exception& ){} // expected exception
catch ( fc::eof_exception& ){} // expected exception catch ( fc::eof_exception& ){} // expected exception
catch ( fc::exception& e ) catch ( fc::exception& e )
@ -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() ) my->_on_close = callback;
{
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::add_method( const fc::string& name, method m ) void json_connection::add_method( const fc::string& name, method m )