diff --git a/include/fc/network/http/websocket.hpp b/include/fc/network/http/websocket.hpp index fbc1c92..5c253aa 100644 --- a/include/fc/network/http/websocket.hpp +++ b/include/fc/network/http/websocket.hpp @@ -4,6 +4,7 @@ #include #include #include +#include namespace fc { namespace http { namespace detail { @@ -23,6 +24,8 @@ namespace fc { namespace http { void set_session_data( fc::any d ){ _session_data = std::move(d); } fc::any& get_session_data() { return _session_data; } + + fc::signal closed; private: fc::any _session_data; std::function _on_message; diff --git a/include/fc/rpc/api_connection.hpp b/include/fc/rpc/api_connection.hpp index 1f1c6f5..32258e7 100644 --- a/include/fc/rpc/api_connection.hpp +++ b/include/fc/rpc/api_connection.hpp @@ -80,7 +80,7 @@ namespace fc { variant call( const string& name, const variants& args ) { auto itr = _by_name.find(name); - FC_ASSERT( itr != _by_name.end() ); + FC_ASSERT( itr != _by_name.end(), "no method with name '${name}'", ("name",name)("api",_by_name) ); return call( itr->second, args ); } @@ -220,6 +220,7 @@ namespace fc { return _local_callbacks.size() - 1; } + fc::signal closed; private: std::vector< std::unique_ptr > _local_apis; std::map< uint64_t, api_id_type > _handle_to_id; diff --git a/include/fc/rpc/cli.hpp b/include/fc/rpc/cli.hpp index 2af3553..a25e424 100644 --- a/include/fc/rpc/cli.hpp +++ b/include/fc/rpc/cli.hpp @@ -75,7 +75,7 @@ namespace fc { namespace rpc { } catch ( const fc::exception& e ) { - edump((e.to_detail_string())); + std::cout << e.to_detail_string() << "\n"; } } } diff --git a/include/fc/rpc/websocket_api.hpp b/include/fc/rpc/websocket_api.hpp index fa0158b..202a2cb 100644 --- a/include/fc/rpc/websocket_api.hpp +++ b/include/fc/rpc/websocket_api.hpp @@ -37,6 +37,7 @@ namespace fc { namespace rpc { }); _connection.on_message_handler( [&]( const std::string& msg ){ on_message(msg); } ); + _connection.closed.connect( [this](){ closed(); } ); } virtual variant send_call( api_id_type api_id, @@ -59,6 +60,7 @@ namespace fc { namespace rpc { _connection.send_message( fc::json::to_string(req) ); } + protected: void on_message( const std::string& message ) { diff --git a/src/network/http/websocket.cpp b/src/network/http/websocket.cpp index 777e76b..5987027 100644 --- a/src/network/http/websocket.cpp +++ b/src/network/http/websocket.cpp @@ -73,7 +73,8 @@ namespace fc { namespace http { virtual void send_message( const std::string& message )override { - _ws_connection->send( message ); + auto ec = _ws_connection->send( message ); + FC_ASSERT( !ec, "websocket send failed: ${msg}", ("msg",ec.message() ) ); } virtual void close( int64_t code, const std::string& reason )override { @@ -158,13 +159,15 @@ namespace fc { namespace http { }).wait(); }); _client.set_close_handler( [=]( connection_hdl hdl ){ - _client_thread.async( [&](){ _connection.reset(); } ).wait(); + if( _connection ) + _client_thread.async( [&](){ if( _connection ) _connection->closed(); _connection.reset(); } ).wait(); if( _closed ) _closed->set_value(); }); _client.set_fail_handler( [=]( connection_hdl hdl ){ auto con = _client.get_con_from_hdl(hdl); auto message = con->get_ec().message(); - _client_thread.async( [&](){ _connection.reset(); } ).wait(); + if( _connection ) + _client_thread.async( [&](){ if( _connection ) _connection->closed(); _connection.reset(); } ).wait(); if( _connected && !_connected->ready() ) _connected->set_exception( exception_ptr( new FC_EXCEPTION( exception, "${message}", ("message",message)) ) ); if( _closed ) diff --git a/src/rpc/state.cpp b/src/rpc/state.cpp index b5cb95f..7d4063d 100644 --- a/src/rpc/state.cpp +++ b/src/rpc/state.cpp @@ -35,7 +35,7 @@ void state::handle_reply( const response& response ) await->second->set_value( *response.result ); else if( response.error ) { - await->second->set_exception( exception_ptr(new FC_EXCEPTION( exception, "${error}", ("error",*response.error) ) ) ); + await->second->set_exception( exception_ptr(new FC_EXCEPTION( exception, "${error}", ("error",response.error->message)("data",response) ) ) ); } else await->second->set_value( fc::variant() );