Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
bstate.cpp
Go to the documentation of this file.
1#include <fc/rpc/bstate.hpp>
4
5namespace fc { namespace rpc {
6bstate::~bstate()
7{
8 close();
9}
10
11void bstate::add_method( const fc::string& name, method m )
12{
13 _methods.emplace(std::pair<std::string,method>(name,fc::move(m)));
14}
15
16void bstate::remove_method( const fc::string& name )
17{
18 _methods.erase(name);
19}
20
21result_type bstate::local_call( const string& method_name, const params_type& args )
22{
23 auto method_itr = _methods.find(method_name);
24 if( method_itr == _methods.end() && _unhandled )
25 return _unhandled( method_name, args );
26 FC_ASSERT( method_itr != _methods.end(), "Unknown Method: ${name}", ("name",method_name) );
27 return method_itr->second(args);
28}
29
30void bstate::handle_reply( const bresponse& bresponse )
31{
32 auto await = _awaiting.find( bresponse.id );
33 FC_ASSERT( await != _awaiting.end(), "Unknown Response ID: ${id}", ("id",bresponse.id)("bresponse",bresponse) );
34 if( bresponse.result )
35 await->second->set_value( *bresponse.result );
36 else if( bresponse.error )
37 {
38 await->second->set_exception( std::make_exception_ptr( FC_EXCEPTION( exception, "${error}", ("error",bresponse.error->message)("data",bresponse) ) ) );
39 }
40 else
41 await->second->set_value( params_type() );
42 _awaiting.erase(await);
43}
44
45brequest bstate::start_remote_call( const string& method_name, params_type args )
46{
47 brequest brequest{ _next_id++, method_name, std::move(args) };
48 _awaiting[*brequest.id].reset( new boost::fibers::promise<result_type>() );
49 return brequest;
50}
51result_type bstate::wait_for_response( uint64_t request_id )
52{
53 auto itr = _awaiting.find(request_id);
54 FC_ASSERT( itr != _awaiting.end() );
55 auto fut = itr->second->get_future();
56 return fut.get();
57}
58void bstate::close()
59{
60 for( auto& item : _awaiting )
61 item.second->set_exception( std::make_exception_ptr( FC_EXCEPTION( eof_exception, "connection closed" )) );
62 _awaiting.clear();
63}
64void bstate::on_unhandled( const std::function<result_type(const string&, const params_type&)>& unhandled )
65{
66 _unhandled = unhandled;
67}
68
69} } // namespace fc::rpc
std::string name
Defines exception's used by fc.
#define FC_EXCEPTION(EXCEPTION_TYPE, FORMAT,...)
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
void close(T *e, websocketpp::connection_hdl hdl)
namespace sysio::chain
Definition authority.cpp:3
std::string string
Definition string.hpp:10
unsigned __int64 uint64_t
Definition stdint.h:136