Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
websocket_api.cpp
Go to the documentation of this file.
1
3
4namespace fc { namespace rpc {
5
9
10websocket_api_connection::websocket_api_connection( fc::http::websocket_connection& c )
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}
56
58 api_id_type api_id,
59 string method_name,
60 variants args /* = variants() */ )
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}
66
68 uint64_t callback_id,
69 variants args /* = variants() */ )
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}
75
77 uint64_t callback_id,
78 variants args /* = variants() */ )
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}
83
85 const std::string& message,
86 bool send_message /* = true */ )
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}
176
177} } // namespace fc::rpc
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
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
std::string on_message(const std::string &message, bool send_message=true)
virtual variant send_call(api_id_type api_id, string method_name, variants args=variants()) override
fc::http::websocket_connection & _connection
virtual variant send_callback(uint64_t callback_id, variants args=variants()) override
websocket_api_connection(fc::http::websocket_connection &c)
virtual void send_notice(uint64_t callback_id, variants args=variants()) override
static std_exception_wrapper from_current_exception(const std::exception &e)
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
uint64_t as_uint64() const
Definition variant.cpp:398
T as() const
Definition variant.hpp:327
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
#define wdump(SEQ)
Definition logger.hpp:160
namespace sysio::chain
Definition authority.cpp:3
std::string string
Definition string.hpp:10
std::shared_ptr< exception > exception_ptr
uint32_t api_id_type
Definition api.hpp:46
std::vector< fc::variant > variants
Definition variant.hpp:173
fc::variant call(const std::string &url, const std::string &path, const T &v)
Definition main.cpp:258
unsigned __int64 uint64_t
Definition stdint.h:136