Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grph 46 #7

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/fc/rpc/websocket_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace fc { namespace rpc {
class websocket_api_connection : public api_connection
{
public:
websocket_api_connection( fc::http::websocket_connection& c );
websocket_api_connection( const std::shared_ptr<fc::http::websocket_connection> &c );
~websocket_api_connection();

virtual variant send_call(
Expand All @@ -29,7 +29,7 @@ namespace fc { namespace rpc {
const std::string& message,
bool send_message = true );

fc::http::websocket_connection& _connection;
std::shared_ptr<fc::http::websocket_connection> _connection;
fc::rpc::state _rpc_state;
};

Expand Down
5 changes: 5 additions & 0 deletions src/rpc/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ void cli::run()
catch ( const fc::exception& e )
{
std::cout << e.to_detail_string() << "\n";

if (e.code() == fc::canceled_exception_code)
{
break;
}
}
}
}
Expand Down
44 changes: 28 additions & 16 deletions src/rpc/websocket_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ websocket_api_connection::~websocket_api_connection()
{
}

websocket_api_connection::websocket_api_connection( fc::http::websocket_connection& c )
websocket_api_connection::websocket_api_connection( const std::shared_ptr<fc::http::websocket_connection>& c )
: _connection(c)
{
FC_ASSERT( c );
_rpc_state.add_method( "call", [this]( const variants& args ) -> variant
{
FC_ASSERT( args.size() == 3 && args[2].is_array() );
Expand Down Expand Up @@ -47,36 +48,47 @@ websocket_api_connection::websocket_api_connection( fc::http::websocket_connecti
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(
api_id_type api_id,
string method_name,
variants args /* = variants() */ )
{
auto request = _rpc_state.start_remote_call( "call", {api_id, std::move(method_name), std::move(args) } );
_connection.send_message( fc::json::to_string(request) );
return _rpc_state.wait_for_response( *request.id );
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(request) );
return _rpc_state.wait_for_response( *request.id );
}
return variant();
}

variant websocket_api_connection::send_callback(
uint64_t callback_id,
variants args /* = variants() */ )
{
auto request = _rpc_state.start_remote_call( "callback", {callback_id, std::move(args) } );
_connection.send_message( fc::json::to_string(request) );
return _rpc_state.wait_for_response( *request.id );
if( _connection )
{
auto request = _rpc_state.start_remote_call( "callback", {callback_id, std::move(args) } );
_connection->send_message( fc::json::to_string(request) );
return _rpc_state.wait_for_response( *request.id );
}
return variant();
}

void websocket_api_connection::send_notice(
uint64_t callback_id,
variants args /* = variants() */ )
{
fc::rpc::request req{ optional<uint64_t>(), "notice", {callback_id, std::move(args)}};
_connection.send_message( fc::json::to_string(req) );
if( _connection )
{
fc::rpc::request req{ optional<uint64_t>(), "notice", {callback_id, std::move(args)}};
_connection->send_message( fc::json::to_string(req) );
}
}

std::string websocket_api_connection::on_message(
Expand Down Expand Up @@ -114,8 +126,8 @@ std::string websocket_api_connection::on_message(
if( call.id )
{
auto reply = fc::json::to_string( response( *call.id, result, "2.0" ) );
if( send_message )
_connection.send_message( reply );
if( send_message && _connection )
_connection->send_message( reply );
return reply;
}
}
Expand All @@ -131,8 +143,8 @@ std::string websocket_api_connection::on_message(
if( optexcept ) {

auto reply = fc::json::to_string( response( *call.id, error_object{ 1, optexcept->to_string(), fc::variant(*optexcept)}, "2.0" ) );
if( send_message )
_connection.send_message( reply );
if( send_message && _connection )
_connection->send_message( reply );

return reply;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,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);
auto wsc = std::make_shared<websocket_api_connection>(c);
auto login = std::make_shared<login_api>();
login->calc = calc_api;
wsc->register_api(fc::api<login_api>(login));
Expand All @@ -74,7 +74,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);
auto apic = std::make_shared<websocket_api_connection>(con);
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) ); } );
Expand Down