From 3655fe148b438ae0a7515b5e37f5c64135aa8361 Mon Sep 17 00:00:00 2001 From: abitmore Date: Wed, 3 Apr 2019 12:05:40 -0400 Subject: [PATCH] Minor code refactory --- src/rpc/websocket_api.cpp | 50 +++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/src/rpc/websocket_api.cpp b/src/rpc/websocket_api.cpp index 8902004..d1dd997 100644 --- a/src/rpc/websocket_api.cpp +++ b/src/rpc/websocket_api.cpp @@ -11,7 +11,7 @@ websocket_api_connection::websocket_api_connection( const std::shared_ptr variant { FC_ASSERT( args.size() == 3 && args[2].is_array() ); @@ -59,43 +59,41 @@ variant websocket_api_connection::send_call( string method_name, variants args /* = variants() */ ) { - if( _connection ) - { - auto request = _rpc_state.start_remote_call( "call", { api_id, std::move(method_name), std::move(args) } ); - _connection->send_message( fc::json::to_string( fc::variant( request, _max_conversion_depth ), - fc::json::stringify_large_ints_and_doubles, - _max_conversion_depth ) ); - return _rpc_state.wait_for_response( *request.id ); - } - return variant(); // TODO return an error + if( !_connection ) // defensive check + return variant(); // TODO return an error? + + auto request = _rpc_state.start_remote_call( "call", { api_id, std::move(method_name), std::move(args) } ); + _connection->send_message( fc::json::to_string( fc::variant( request, _max_conversion_depth ), + fc::json::stringify_large_ints_and_doubles, + _max_conversion_depth ) ); + return _rpc_state.wait_for_response( *request.id ); } variant websocket_api_connection::send_callback( uint64_t callback_id, variants args /* = variants() */ ) { - if( _connection ) - { - auto request = _rpc_state.start_remote_call( "callback", { callback_id, std::move(args) } ); - _connection->send_message( fc::json::to_string( fc::variant( request, _max_conversion_depth ), - fc::json::stringify_large_ints_and_doubles, - _max_conversion_depth ) ); - return _rpc_state.wait_for_response( *request.id ); - } - return variant(); // TODO return an error + if( !_connection ) // defensive check + return variant(); // TODO return an error? + + auto request = _rpc_state.start_remote_call( "callback", { callback_id, std::move(args) } ); + _connection->send_message( fc::json::to_string( fc::variant( request, _max_conversion_depth ), + fc::json::stringify_large_ints_and_doubles, + _max_conversion_depth ) ); + return _rpc_state.wait_for_response( *request.id ); } void websocket_api_connection::send_notice( uint64_t callback_id, variants args /* = variants() */ ) { - if( _connection ) - { - fc::rpc::request req{ optional(), "notice", { callback_id, std::move(args) } }; - _connection->send_message( fc::json::to_string( fc::variant( req, _max_conversion_depth ), - fc::json::stringify_large_ints_and_doubles, - _max_conversion_depth ) ); - } + if( !_connection ) // defensive check + return; + + fc::rpc::request req{ optional(), "notice", { callback_id, std::move(args) } }; + _connection->send_message( fc::json::to_string( fc::variant( req, _max_conversion_depth ), + fc::json::stringify_large_ints_and_doubles, + _max_conversion_depth ) ); } std::string websocket_api_connection::on_message(