@@ -167,7 +167,7 @@ namespace fc { namespace http {
167
167
:_ws_connection(con){
168
168
}
169
169
170
- ~websocket_connection_impl ()
170
+ virtual ~websocket_connection_impl ()
171
171
{
172
172
}
173
173
@@ -432,16 +432,16 @@ namespace fc { namespace http {
432
432
typedef websocket_tls_client_type::connection_ptr websocket_tls_client_connection_type;
433
433
using websocketpp::connection_hdl;
434
434
435
- class websocket_client_impl
435
+ template <typename T>
436
+ class generic_websocket_client_impl
436
437
{
437
438
public:
438
- typedef websocket_client_type::message_ptr message_ptr;
439
-
440
- websocket_client_impl ()
439
+ generic_websocket_client_impl ()
441
440
:_client_thread( fc::thread::current() )
442
441
{
443
442
_client.clear_access_channels ( websocketpp::log ::alevel::all );
444
- _client.set_message_handler ( [&]( connection_hdl hdl, message_ptr msg ){
443
+ _client.set_message_handler ( [&]( connection_hdl hdl,
444
+ typename websocketpp::client<T>::message_ptr msg ){
445
445
_client_thread.async ( [&](){
446
446
wdump ((msg->get_payload ()));
447
447
// std::cerr<<"recv: "<<msg->get_payload()<<"\n";
@@ -469,74 +469,38 @@ namespace fc { namespace http {
469
469
470
470
_client.init_asio ( &fc::asio::default_io_service () );
471
471
}
472
- ~websocket_client_impl ()
472
+ virtual ~generic_websocket_client_impl ()
473
473
{
474
- if (_connection )
474
+ if ( _connection )
475
475
{
476
476
_connection->close (0 , " client closed" );
477
477
_connection.reset ();
478
- _closed->wait ();
479
478
}
479
+ if ( _closed )
480
+ _closed->wait ();
480
481
}
481
482
fc::promise<void >::ptr _connected;
482
483
fc::promise<void >::ptr _closed;
483
484
fc::thread& _client_thread;
484
- websocket_client_type _client;
485
+ websocketpp::client<T> _client;
485
486
websocket_connection_ptr _connection;
486
487
std::string _uri;
487
488
fc::optional<connection_hdl> _hdl;
488
489
};
489
490
491
+ class websocket_client_impl : public generic_websocket_client_impl <asio_with_stub_log>
492
+ {};
490
493
491
-
492
- class websocket_tls_client_impl
494
+ class websocket_tls_client_impl : public generic_websocket_client_impl <asio_tls_stub_log>
493
495
{
494
496
public:
495
- typedef websocket_tls_client_type::message_ptr message_ptr;
496
-
497
497
websocket_tls_client_impl ( const std::string& ca_filename )
498
- :_client_thread( fc::thread::current() )
498
+ : generic_websocket_client_impl( )
499
499
{
500
500
// ca_filename has special values:
501
501
// "_none" disables cert checking (potentially insecure!)
502
502
// "_default" uses default CA's provided by OS
503
503
504
- _client.clear_access_channels ( websocketpp::log ::alevel::all );
505
- _client.set_message_handler ( [&]( connection_hdl hdl, message_ptr msg ){
506
- _client_thread.async ( [&](){
507
- wdump ((msg->get_payload ()));
508
- _connection->on_message ( msg->get_payload () );
509
- }).wait ();
510
- });
511
- _client.set_close_handler ( [=]( connection_hdl hdl ){
512
- if ( _connection )
513
- {
514
- try {
515
- _client_thread.async ( [&](){
516
- wlog (" . ${p}" , (" p" ,uint64_t (_connection.get ())));
517
- if ( !_shutting_down && !_closed && _connection )
518
- _connection->closed ();
519
- _connection.reset ();
520
- } ).wait ();
521
- } catch ( const fc::exception & e )
522
- {
523
- if ( _closed ) _closed->set_exception ( e.dynamic_copy_exception () );
524
- }
525
- if ( _closed ) _closed->set_value ();
526
- }
527
- });
528
- _client.set_fail_handler ( [=]( connection_hdl hdl ){
529
- elog ( " ." );
530
- auto con = _client.get_con_from_hdl (hdl);
531
- auto message = con->get_ec ().message ();
532
- if ( _connection )
533
- _client_thread.async ( [&](){ if ( _connection ) _connection->closed (); _connection.reset (); } ).wait ();
534
- if ( _connected && !_connected->ready () )
535
- _connected->set_exception ( exception_ptr ( new FC_EXCEPTION ( exception , " ${message}" , (" message" ,message)) ) );
536
- if ( _closed )
537
- _closed->set_value ();
538
- });
539
-
540
504
//
541
505
// We need ca_filename to be copied into the closure, as the referenced object might be destroyed by the caller by the time
542
506
// 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 {
569
533
return ctx;
570
534
});
571
535
572
- _client.init_asio ( &fc::asio::default_io_service () );
573
- }
574
- ~websocket_tls_client_impl ()
575
- {
576
- if (_connection )
577
- {
578
- wlog (" ." );
579
- _shutting_down = true ;
580
- _connection->close (0 , " client closed" );
581
- _closed->wait ();
582
- }
583
536
}
537
+ virtual ~websocket_tls_client_impl () {}
584
538
585
539
std::string get_host ()const
586
540
{
@@ -600,13 +554,6 @@ namespace fc { namespace http {
600
554
ctx->set_verify_callback ( boost::asio::ssl::rfc2818_verification ( get_host () ) );
601
555
}
602
556
603
- bool _shutting_down = false ;
604
- fc::promise<void >::ptr _connected;
605
- fc::promise<void >::ptr _closed;
606
- fc::thread& _client_thread;
607
- websocket_tls_client_type _client;
608
- websocket_connection_ptr _connection;
609
- std::string _uri;
610
557
};
611
558
612
559
0 commit comments