diff --git a/include/fc/rpc/json_connection.hpp b/include/fc/rpc/json_connection.hpp index dd8aba3..04cd041 100644 --- a/include/fc/rpc/json_connection.hpp +++ b/include/fc/rpc/json_connection.hpp @@ -29,6 +29,11 @@ namespace fc { namespace rpc { */ future exec(); + bool is_open(); + void close(); + + void set_on_disconnected_callback(std::function callback); + logger get_logger()const; void set_logger( const logger& l ); diff --git a/src/network/tcp_socket.cpp b/src/network/tcp_socket.cpp index 70bb28a..cb96bbb 100644 --- a/src/network/tcp_socket.cpp +++ b/src/network/tcp_socket.cpp @@ -27,7 +27,12 @@ namespace fc { ~impl() { if( _sock.is_open() ) - _sock.close(); + try + { + _sock.close(); + } + catch( ... ) + {} if( _read_in_progress.valid() ) try { diff --git a/src/rpc/json_connection.cpp b/src/rpc/json_connection.cpp index 77a3918..cd1d6b4 100644 --- a/src/rpc/json_connection.cpp +++ b/src/rpc/json_connection.cpp @@ -30,7 +30,7 @@ namespace fc { namespace rpc { boost::unordered_map _named_param_methods; fc::mutex _write_mutex; - //std::function _on_close; + std::function _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 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 { @@ -250,7 +266,7 @@ namespace fc { namespace rpc { my->_out->close(); my->_done.wait(); } - } + } catch ( fc::canceled_exception& ){} // expected exception catch ( fc::eof_exception& ){} // expected exception catch ( fc::exception& e ) @@ -260,13 +276,9 @@ namespace fc { namespace rpc { } } - fc::future json_connection::exec() + void json_connection::set_on_disconnected_callback(std::function 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 )