Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
fc::rpc::websocket_api_connection Class Reference

#include <websocket_api.hpp>

Inheritance diagram for fc::rpc::websocket_api_connection:
Collaboration diagram for fc::rpc::websocket_api_connection:

Public Member Functions

 websocket_api_connection (fc::http::websocket_connection &c)
 
 ~websocket_api_connection ()
 
virtual variant send_call (api_id_type api_id, string method_name, variants args=variants()) override
 
virtual variant send_callback (uint64_t callback_id, variants args=variants()) override
 
virtual void send_notice (uint64_t callback_id, variants args=variants()) override
 
- Public Member Functions inherited from fc::api_connection
 api_connection ()
 
virtual ~api_connection ()
 
template<typename T >
api< Tget_remote_api (api_id_type api_id=0)
 
variant receive_call (api_id_type api_id, const string &method_name, const variants &args=variants()) const
 
variant receive_callback (uint64_t callback_id, const variants &args=variants()) const
 
void receive_notice (uint64_t callback_id, const variants &args=variants()) const
 
template<typename Interface >
api_id_type register_api (const Interface &a)
 
template<typename Signature >
uint64_t register_callback (const std::function< Signature > &cb)
 
std::vector< std::string > get_method_names (api_id_type local_api_id=0) const
 

Protected Member Functions

std::string on_message (const std::string &message, bool send_message=true)
 

Protected Attributes

fc::http::websocket_connection & _connection
 
fc::rpc::state _rpc_state
 

Additional Inherited Members

- Public Attributes inherited from fc::api_connection
fc::signal< void()> closed
 

Detailed Description

Definition at line 10 of file websocket_api.hpp.

Constructor & Destructor Documentation

◆ websocket_api_connection()

fc::rpc::websocket_api_connection::websocket_api_connection ( fc::http::websocket_connection & c)

Definition at line 10 of file websocket_api.cpp.

11 : _connection(c)
12{
13 _rpc_state.add_method( "call", [this]( const variants& args ) -> variant
14 {
15 FC_ASSERT( args.size() == 3 && args[2].is_array() );
16 api_id_type api_id;
17 if( args[0].is_string() )
18 {
19 variants subargs;
20 subargs.push_back( args[0] );
21 variant subresult = this->receive_call( 1, "get_api_by_name", subargs );
22 api_id = subresult.as_uint64();
23 }
24 else
25 api_id = args[0].as_uint64();
26
27 return this->receive_call(
28 api_id,
29 args[1].as_string(),
30 args[2].get_array() );
31 } );
32
33 _rpc_state.add_method( "notice", [this]( const variants& args ) -> variant
34 {
35 FC_ASSERT( args.size() == 2 && args[1].is_array() );
36 this->receive_notice( args[0].as_uint64(), args[1].get_array() );
37 return variant();
38 } );
39
40 _rpc_state.add_method( "callback", [this]( const variants& args ) -> variant
41 {
42 FC_ASSERT( args.size() == 2 && args[1].is_array() );
43 this->receive_callback( args[0].as_uint64(), args[1].get_array() );
44 return variant();
45 } );
46
47 _rpc_state.on_unhandled( [&]( const std::string& method_name, const variants& args )
48 {
49 return this->receive_call( 0, method_name, args );
50 } );
51
52 _connection.on_message_handler( [&]( const std::string& msg ){ on_message(msg,true); } );
53 _connection.on_http_handler( [&]( const std::string& msg ){ return on_message(msg,false); } );
54 _connection.closed.connect( [this](){ closed(); } );
55}
variant receive_callback(uint64_t callback_id, const variants &args=variants()) const
fc::signal< void()> closed
void receive_notice(uint64_t callback_id, const variants &args=variants()) const
variant receive_call(api_id_type api_id, const string &method_name, const variants &args=variants()) const
std::string on_message(const std::string &message, bool send_message=true)
fc::http::websocket_connection & _connection
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
uint32_t api_id_type
Definition api.hpp:46
std::vector< fc::variant > variants
Definition variant.hpp:173
Here is the call graph for this function:

◆ ~websocket_api_connection()

fc::rpc::websocket_api_connection::~websocket_api_connection ( )

Definition at line 6 of file websocket_api.cpp.

7{
8}

Member Function Documentation

◆ on_message()

std::string fc::rpc::websocket_api_connection::on_message ( const std::string & message,
bool send_message = true )
protected

Definition at line 84 of file websocket_api.cpp.

87{
88 wdump((message));
89
90 auto handle_error = [&](const auto& e)
91 {
92 wdump((e.to_detail_string()));
93 return e.to_detail_string();
94 };
95
96 try
97 {
98 auto var = fc::json::from_string(message);
99 const auto& var_obj = var.get_object();
100 if( var_obj.contains( "method" ) )
101 {
102 auto call = var.as<fc::rpc::request>();
103 exception_ptr optexcept;
104
105 auto handle_error_inner = [&](const auto& e)
106 {
107 if (!call.id)
108 {
109 return nullptr;
110 }
111
112 return e.dynamic_copy_exception();
113 };
114
115 try
116 {
117 auto result = _rpc_state.local_call( call.method, call.params );
118 if( call.id )
119 {
120 auto reply = fc::json::to_string( response( *call.id, result ) );
121 if( send_message )
122 _connection.send_message( reply );
123 return reply;
124 }
125 }
126 catch ( const std::bad_alloc& )
127 {
128 throw;
129 }
130 catch ( const boost::interprocess::bad_alloc& )
131 {
132 throw;
133 }
134 catch ( const fc::exception& e )
135 {
136 optexcept = handle_error_inner(e);
137 }
138 catch ( const std::exception& e )
139 {
140 optexcept = handle_error_inner(fc::std_exception_wrapper::from_current_exception(e));
141 }
142
143 if( optexcept ) {
144
145 auto reply = fc::json::to_string( response( *call.id, error_object{ 1, optexcept->to_detail_string(), fc::variant(*optexcept)} ) );
146 if( send_message )
147 _connection.send_message( reply );
148
149 return reply;
150 }
151 }
152 else
153 {
154 auto reply = var.as<fc::rpc::response>();
155 _rpc_state.handle_reply( reply );
156 }
157 }
158 catch ( const std::bad_alloc& )
159 {
160 throw;
161 }
162 catch ( const boost::interprocess::bad_alloc& )
163 {
164 throw;
165 }
166 catch ( const fc::exception& e )
167 {
168 return handle_error(e);
169 }
170 catch ( const std::exception& e)
171 {
173 }
174 return string();
175}
Used to generate a useful error report when an exception is thrown.
Definition exception.hpp:58
static string to_string(const variant &v, const yield_function_t &yield, const output_formatting format=output_formatting::stringify_large_ints_and_doubles)
Definition json.cpp:674
static variant from_string(const string &utf8_str, const parse_type ptype=parse_type::legacy_parser, uint32_t max_depth=DEFAULT_MAX_RECURSION_DEPTH)
Definition json.cpp:442
static std_exception_wrapper from_current_exception(const std::exception &e)
T as() const
Definition variant.hpp:327
#define wdump(SEQ)
Definition logger.hpp:160
std::string string
Definition string.hpp:10
std::shared_ptr< exception > exception_ptr
fc::variant call(const std::string &url, const std::string &path, const T &v)
Definition main.cpp:258
Here is the call graph for this function:
Here is the caller graph for this function:

◆ send_call()

variant fc::rpc::websocket_api_connection::send_call ( api_id_type api_id,
string method_name,
variants args = variants() )
overridevirtual

makes calls to the remote server

Implements fc::api_connection.

Definition at line 57 of file websocket_api.cpp.

61{
62 auto request = _rpc_state.start_remote_call( "call", {api_id, std::move(method_name), std::move(args) } );
63 _connection.send_message( fc::json::to_string(request) );
64 return _rpc_state.wait_for_response( *request.id );
65}
Here is the call graph for this function:

◆ send_callback()

variant fc::rpc::websocket_api_connection::send_callback ( uint64_t callback_id,
variants args = variants() )
overridevirtual

Implements fc::api_connection.

Definition at line 67 of file websocket_api.cpp.

70{
71 auto request = _rpc_state.start_remote_call( "callback", {callback_id, std::move(args) } );
72 _connection.send_message( fc::json::to_string(request) );
73 return _rpc_state.wait_for_response( *request.id );
74}
Here is the call graph for this function:

◆ send_notice()

void fc::rpc::websocket_api_connection::send_notice ( uint64_t callback_id,
variants args = variants() )
overridevirtual

Implements fc::api_connection.

Definition at line 76 of file websocket_api.cpp.

79{
80 fc::rpc::request req{ std::optional<uint64_t>(), "notice", {callback_id, std::move(args)}};
81 _connection.send_message( fc::json::to_string(req) );
82}
Here is the call graph for this function:

Member Data Documentation

◆ _connection

fc::http::websocket_connection& fc::rpc::websocket_api_connection::_connection
protected

Definition at line 32 of file websocket_api.hpp.

◆ _rpc_state

fc::rpc::state fc::rpc::websocket_api_connection::_rpc_state
protected

Definition at line 33 of file websocket_api.hpp.


The documentation for this class was generated from the following files: