@@ -45,7 +45,8 @@ void manager_sub::on_add_symbol( manager *, price * )
45
45
// manager
46
46
47
47
manager::manager ()
48
- : thost_( PC_RPC_HOST ),
48
+ : wconn_{ nullptr },
49
+ thost_ ( PC_RPC_HOST ),
49
50
rhost_( PC_RPC_HOST ),
50
51
sub_( nullptr ),
51
52
status_( 0 ),
@@ -260,7 +261,12 @@ void manager::teardown()
260
261
261
262
// destroy rpc connections
262
263
hconn_.close ();
263
- wconn_.close ();
264
+ if ( wconn_ ) {
265
+ wconn_->close ();
266
+ delete wconn_;
267
+ wconn_ = nullptr ;
268
+ clnt_.set_ws_conn ( nullptr );
269
+ }
264
270
}
265
271
266
272
bool manager::init ()
@@ -305,15 +311,18 @@ bool manager::init()
305
311
hconn_.set_host ( rhost );
306
312
hconn_.set_net_loop ( &nl_ );
307
313
clnt_.set_http_conn ( &hconn_ );
308
- wconn_.set_port ( wport );
309
- wconn_.set_host ( rhost );
310
- wconn_.set_net_loop ( &nl_ );
311
- clnt_.set_ws_conn ( &wconn_ );
314
+ if ( get_do_ws () ) {
315
+ wconn_ = new ws_connect{};
316
+ wconn_->set_port ( wport );
317
+ wconn_->set_host ( rhost );
318
+ wconn_->set_net_loop ( &nl_ );
319
+ clnt_.set_ws_conn ( wconn_ );
320
+ }
312
321
if ( !hconn_.init () ) {
313
322
return set_err_msg ( hconn_.get_err_msg () );
314
323
}
315
- if ( !wconn_. init () ) {
316
- return set_err_msg ( wconn_. get_err_msg () );
324
+ if ( wconn_ && !wconn_-> init () ) {
325
+ return set_err_msg ( wconn_-> get_err_msg () );
317
326
}
318
327
// connect to pyth_tx server
319
328
if ( do_tx_ ) {
@@ -358,7 +367,7 @@ bool manager::get_is_tx_send() const
358
367
359
368
bool manager::get_is_rpc_send () const
360
369
{
361
- return hconn_.get_is_send () || wconn_. get_is_send ();
370
+ return hconn_.get_is_send () || ( wconn_ && wconn_-> get_is_send () );
362
371
}
363
372
364
373
bool manager::bootstrap ()
@@ -420,7 +429,9 @@ void manager::poll( bool do_wait )
420
429
} else {
421
430
if ( has_status ( PC_PYTH_RPC_CONNECTED ) ) {
422
431
hconn_.poll ();
423
- wconn_.poll ();
432
+ if ( wconn_ ) {
433
+ wconn_->poll ();
434
+ }
424
435
}
425
436
if ( do_tx_ ) {
426
437
tconn_.poll ();
@@ -476,7 +487,7 @@ void manager::poll( bool do_wait )
476
487
// submit new quotes while connected
477
488
if ( has_status ( PC_PYTH_RPC_CONNECTED ) &&
478
489
!hconn_.get_is_err () &&
479
- !wconn_. get_is_err () ) {
490
+ ( !wconn_ || !wconn_-> get_is_err () ) ) {
480
491
poll_schedule ();
481
492
} else {
482
493
reconnect_rpc ();
@@ -506,15 +517,15 @@ void manager::reconnect_rpc()
506
517
if ( hconn_.get_is_wait () ) {
507
518
hconn_.check ();
508
519
}
509
- if ( wconn_. get_is_wait () ) {
510
- wconn_. check ();
520
+ if ( wconn_ && wconn_-> get_is_wait () ) {
521
+ wconn_-> check ();
511
522
}
512
- if ( hconn_.get_is_wait () || wconn_. get_is_wait () ) {
523
+ if ( hconn_.get_is_wait () || ( wconn_ && wconn_-> get_is_wait () ) ) {
513
524
return ;
514
525
}
515
526
516
527
// check for successful (re)connect
517
- if ( !hconn_.get_is_err () && !wconn_. get_is_err () ) {
528
+ if ( !hconn_.get_is_err () && ( !wconn_ || !wconn_-> get_is_err () ) ) {
518
529
PC_LOG_INF ( " rpc_connected" ).end ();
519
530
set_status ( PC_PYTH_RPC_CONNECTED );
520
531
@@ -613,7 +624,9 @@ void manager::reconnect_rpc()
613
624
ctimeout_ = std::min ( ctimeout_, PC_RECONNECT_TIMEOUT );
614
625
wait_conn_ = true ;
615
626
hconn_.init ();
616
- wconn_.init ();
627
+ if ( wconn_ ) {
628
+ wconn_->init ();
629
+ }
617
630
}
618
631
619
632
void manager::log_disconnect ()
@@ -626,11 +639,11 @@ void manager::log_disconnect()
626
639
.end ();
627
640
return ;
628
641
}
629
- if ( wconn_. get_is_err () ) {
642
+ if ( wconn_ && wconn_-> get_is_err () ) {
630
643
PC_LOG_ERR ( " rpc_websocket_reset" )
631
- .add ( " error" , wconn_. get_err_msg () )
644
+ .add ( " error" , wconn_-> get_err_msg () )
632
645
.add ( " host" , rhost_ )
633
- .add ( " port" , wconn_. get_port () )
646
+ .add ( " port" , wconn_-> get_port () )
634
647
.end ();
635
648
return ;
636
649
}
0 commit comments