GRPH134-Witness High CPU Issue, websocket changes
This commit is contained in:
parent
6096e94e1b
commit
2f79730323
3 changed files with 20 additions and 32 deletions
|
|
@ -10,7 +10,7 @@ namespace fc { namespace rpc {
|
|||
class websocket_api_connection : public api_connection
|
||||
{
|
||||
public:
|
||||
websocket_api_connection( const std::shared_ptr<fc::http::websocket_connection> &c, uint32_t max_conversion_depth );
|
||||
websocket_api_connection( fc::http::websocket_connection& c, uint32_t max_conversion_depth );
|
||||
~websocket_api_connection();
|
||||
|
||||
virtual variant send_call(
|
||||
|
|
@ -29,7 +29,7 @@ namespace fc { namespace rpc {
|
|||
const std::string& message,
|
||||
bool send_message = true );
|
||||
|
||||
std::shared_ptr<fc::http::websocket_connection> _connection;
|
||||
fc::http::websocket_connection& _connection;
|
||||
fc::rpc::state _rpc_state;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -7,10 +7,9 @@ websocket_api_connection::~websocket_api_connection()
|
|||
{
|
||||
}
|
||||
|
||||
websocket_api_connection::websocket_api_connection( const std::shared_ptr<fc::http::websocket_connection>& c, uint32_t max_depth )
|
||||
websocket_api_connection::websocket_api_connection( fc::http::websocket_connection& c, uint32_t max_depth )
|
||||
: api_connection(max_depth),_connection(c)
|
||||
{
|
||||
FC_ASSERT( c );
|
||||
_rpc_state.add_method( "call", [this]( const variants& args ) -> variant
|
||||
{
|
||||
FC_ASSERT( args.size() == 3 && args[2].is_array() );
|
||||
|
|
@ -48,9 +47,9 @@ websocket_api_connection::websocket_api_connection( const std::shared_ptr<fc::ht
|
|||
return this->receive_call( 0, method_name, args );
|
||||
} );
|
||||
|
||||
_connection->on_message_handler( [&]( const std::string& msg ){ on_message(msg,true); } );
|
||||
_connection->on_http_handler( [&]( const std::string& msg ){ return on_message(msg,false); } );
|
||||
_connection->closed.connect( [this](){ closed(); } );
|
||||
_connection.on_message_handler( [&]( const std::string& msg ){ on_message(msg,true); } );
|
||||
_connection.on_http_handler( [&]( const std::string& msg ){ return on_message(msg,false); } );
|
||||
_connection.closed.connect( [this](){ closed(); } );
|
||||
}
|
||||
|
||||
variant websocket_api_connection::send_call(
|
||||
|
|
@ -58,40 +57,29 @@ 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),
|
||||
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();
|
||||
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),
|
||||
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();
|
||||
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<uint64_t>(), "notice", {callback_id, std::move(args)}};
|
||||
_connection->send_message( fc::json::to_string(fc::variant(req, _max_conversion_depth),
|
||||
fc::rpc::request req{ optional<uint64_t>(), "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(
|
||||
|
|
@ -129,8 +117,8 @@ std::string websocket_api_connection::on_message(
|
|||
if( call.id )
|
||||
{
|
||||
auto reply = fc::json::to_string( response( *call.id, result, "2.0" ), fc::json::stringify_large_ints_and_doubles, _max_conversion_depth );
|
||||
if( send_message && _connection )
|
||||
_connection->send_message( reply );
|
||||
if( send_message )
|
||||
_connection.send_message( reply );
|
||||
return reply;
|
||||
}
|
||||
}
|
||||
|
|
@ -147,8 +135,8 @@ std::string websocket_api_connection::on_message(
|
|||
|
||||
auto reply = fc::json::to_string( variant(response( *call.id, error_object{ 1, optexcept->to_string(), fc::variant(*optexcept, _max_conversion_depth)}, "2.0" ), _max_conversion_depth ),
|
||||
fc::json::stringify_large_ints_and_doubles, _max_conversion_depth );
|
||||
if( send_message && _connection )
|
||||
_connection->send_message( reply );
|
||||
if( send_message )
|
||||
_connection.send_message( reply );
|
||||
|
||||
return reply;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ int main( int argc, char** argv )
|
|||
|
||||
fc::http::websocket_server server;
|
||||
server.on_connection([&]( const websocket_connection_ptr& c ){
|
||||
auto wsc = std::make_shared<websocket_api_connection>(c, MAX_DEPTH);
|
||||
auto wsc = std::make_shared<websocket_api_connection>(*c, MAX_DEPTH);
|
||||
auto login = std::make_shared<login_api>();
|
||||
login->calc = calc_api;
|
||||
wsc->register_api(fc::api<login_api>(login));
|
||||
|
|
@ -76,7 +76,7 @@ int main( int argc, char** argv )
|
|||
try {
|
||||
fc::http::websocket_client client;
|
||||
auto con = client.connect( "ws://localhost:8090" );
|
||||
auto apic = std::make_shared<websocket_api_connection>(con, MAX_DEPTH);
|
||||
auto apic = std::make_shared<websocket_api_connection>(*con, MAX_DEPTH);
|
||||
auto remote_login_api = apic->get_remote_api<login_api>();
|
||||
auto remote_calc = remote_login_api->get_calc();
|
||||
remote_calc->on_result( []( uint32_t r ) { elog( "callback result ${r}", ("r",r) ); } );
|
||||
|
|
|
|||
Loading…
Reference in a new issue