Updates from BitShares FC #22

Closed
nathanielhourt wants to merge 693 commits from dapp-support into latest-fc
Showing only changes of commit 9ce24c2feb - Show all commits

View file

@ -167,7 +167,7 @@ namespace fc { namespace http {
:_ws_connection(con){ :_ws_connection(con){
} }
~websocket_connection_impl() virtual ~websocket_connection_impl()
{ {
} }
@ -432,16 +432,16 @@ namespace fc { namespace http {
typedef websocket_tls_client_type::connection_ptr websocket_tls_client_connection_type; typedef websocket_tls_client_type::connection_ptr websocket_tls_client_connection_type;
using websocketpp::connection_hdl; using websocketpp::connection_hdl;
class websocket_client_impl template<typename T>
class generic_websocket_client_impl
{ {
public: public:
typedef websocket_client_type::message_ptr message_ptr; generic_websocket_client_impl()
websocket_client_impl()
:_client_thread( fc::thread::current() ) :_client_thread( fc::thread::current() )
{ {
_client.clear_access_channels( websocketpp::log::alevel::all ); _client.clear_access_channels( websocketpp::log::alevel::all );
_client.set_message_handler( [&]( connection_hdl hdl, message_ptr msg ){ _client.set_message_handler( [&]( connection_hdl hdl,
typename websocketpp::client<T>::message_ptr msg ){
_client_thread.async( [&](){ _client_thread.async( [&](){
wdump((msg->get_payload())); wdump((msg->get_payload()));
//std::cerr<<"recv: "<<msg->get_payload()<<"\n"; //std::cerr<<"recv: "<<msg->get_payload()<<"\n";
@ -469,74 +469,38 @@ namespace fc { namespace http {
_client.init_asio( &fc::asio::default_io_service() ); _client.init_asio( &fc::asio::default_io_service() );
} }
~websocket_client_impl() virtual ~generic_websocket_client_impl()
{ {
if(_connection ) if( _connection )
{ {
_connection->close(0, "client closed"); _connection->close(0, "client closed");
_connection.reset(); _connection.reset();
_closed->wait();
} }
if( _closed )
_closed->wait();
} }
fc::promise<void>::ptr _connected; fc::promise<void>::ptr _connected;
fc::promise<void>::ptr _closed; fc::promise<void>::ptr _closed;
fc::thread& _client_thread; fc::thread& _client_thread;
websocket_client_type _client; websocketpp::client<T> _client;
websocket_connection_ptr _connection; websocket_connection_ptr _connection;
std::string _uri; std::string _uri;
fc::optional<connection_hdl> _hdl; fc::optional<connection_hdl> _hdl;
}; };
class websocket_client_impl : public generic_websocket_client_impl<asio_with_stub_log>
{};
class websocket_tls_client_impl : public generic_websocket_client_impl<asio_tls_stub_log>
class websocket_tls_client_impl
{ {
public: public:
typedef websocket_tls_client_type::message_ptr message_ptr;
websocket_tls_client_impl( const std::string& ca_filename ) websocket_tls_client_impl( const std::string& ca_filename )
:_client_thread( fc::thread::current() ) : generic_websocket_client_impl()
{ {
// ca_filename has special values: // ca_filename has special values:
// "_none" disables cert checking (potentially insecure!) // "_none" disables cert checking (potentially insecure!)
// "_default" uses default CA's provided by OS // "_default" uses default CA's provided by OS
_client.clear_access_channels( websocketpp::log::alevel::all );
_client.set_message_handler( [&]( connection_hdl hdl, message_ptr msg ){
_client_thread.async( [&](){
wdump((msg->get_payload()));
_connection->on_message( msg->get_payload() );
}).wait();
});
_client.set_close_handler( [=]( connection_hdl hdl ){
if( _connection )
{
try {
_client_thread.async( [&](){
wlog(". ${p}", ("p",uint64_t(_connection.get())));
if( !_shutting_down && !_closed && _connection )
_connection->closed();
_connection.reset();
} ).wait();
} catch ( const fc::exception& e )
{
if( _closed ) _closed->set_exception( e.dynamic_copy_exception() );
}
if( _closed ) _closed->set_value();
}
});
_client.set_fail_handler( [=]( connection_hdl hdl ){
elog( "." );
auto con = _client.get_con_from_hdl(hdl);
auto message = con->get_ec().message();
if( _connection )
_client_thread.async( [&](){ if( _connection ) _connection->closed(); _connection.reset(); } ).wait();
if( _connected && !_connected->ready() )
_connected->set_exception( exception_ptr( new FC_EXCEPTION( exception, "${message}", ("message",message)) ) );
if( _closed )
_closed->set_value();
});
// //
// We need ca_filename to be copied into the closure, as the referenced object might be destroyed by the caller by the time // We need ca_filename to be copied into the closure, as the referenced object might be destroyed by the caller by the time
// tls_init_handler() is called. According to [1], capture-by-value results in the desired behavior (i.e. creation of // tls_init_handler() is called. According to [1], capture-by-value results in the desired behavior (i.e. creation of
@ -569,18 +533,8 @@ namespace fc { namespace http {
return ctx; return ctx;
}); });
_client.init_asio( &fc::asio::default_io_service() );
}
~websocket_tls_client_impl()
{
if(_connection )
{
wlog(".");
_shutting_down = true;
_connection->close(0, "client closed");
_closed->wait();
}
} }
virtual ~websocket_tls_client_impl() {}
std::string get_host()const std::string get_host()const
{ {
@ -600,13 +554,6 @@ namespace fc { namespace http {
ctx->set_verify_callback( boost::asio::ssl::rfc2818_verification( get_host() ) ); ctx->set_verify_callback( boost::asio::ssl::rfc2818_verification( get_host() ) );
} }
bool _shutting_down = false;
fc::promise<void>::ptr _connected;
fc::promise<void>::ptr _closed;
fc::thread& _client_thread;
websocket_tls_client_type _client;
websocket_connection_ptr _connection;
std::string _uri;
}; };