Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
state.cpp
Go to the documentation of this file.
1#include <fc/rpc/state.hpp>
4
5namespace fc { namespace rpc {
6state::~state()
7{
8 close();
9}
10
11void state::add_method( const fc::string& name, method m )
12{
13 _methods.emplace(std::pair<std::string,method>(name,fc::move(m)));
14}
15
16void state::remove_method( const fc::string& name )
17{
18 _methods.erase(name);
19}
20
21variant state::local_call( const string& method_name, const variants& 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 state::handle_reply( const response& response )
31{
32 auto await = _awaiting.find( response.id );
33 FC_ASSERT( await != _awaiting.end(), "Unknown Response ID: ${id}", ("id",response.id)("response",response) );
34 if( response.result )
35 await->second->set_value( *response.result );
36 else if( response.error )
37 {
38 await->second->set_exception( std::make_exception_ptr( FC_EXCEPTION( exception, "${error}", ("error",response.error->message)("data",response) ) ) );
39 }
40 else
41 await->second->set_value( fc::variant() );
42 _awaiting.erase(await);
43}
44
45request state::start_remote_call( const string& method_name, variants args )
46{
47 request request{ _next_id++, method_name, std::move(args) };
48 _awaiting[*request.id].reset( new boost::fibers::promise<fc::variant>() );
49 return request;
50}
51variant state::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 state::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 state::on_unhandled( const std::function<variant(const string&, const variants&)>& unhandled )
65{
66 _unhandled = unhandled;
67}
68
69} } // namespace fc::rpc
std::string name
stores null, int64, uint64, double, bool, string, std::vector<variant>, and variant_object's.
Definition variant.hpp:191
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
std::vector< fc::variant > variants
Definition variant.hpp:173
unsigned __int64 uint64_t
Definition stdint.h:136