diff --git a/lib/rfspace/rfspace_source_c.cc b/lib/rfspace/rfspace_source_c.cc index f9c587d..74708c6 100644 --- a/lib/rfspace/rfspace_source_c.cc +++ b/lib/rfspace/rfspace_source_c.cc @@ -475,6 +475,16 @@ rfspace_source_c::rfspace_source_c (const std::string &args) set_sample_rate( 240000 ); set_bandwidth( 0 ); } + + /* start TCP keepalive thread */ + if ( RFSPACE_NETSDR == _radio || + RFSPACE_SDR_IP == _radio || + RFSPACE_CLOUDIQ == _radio ) + { + _run_tcp_keepalive_task = true; + _thread = gr::thread::thread( boost::bind(&rfspace_source_c::tcp_keepalive_task, this) ); + } + #if 0 std::cerr << "sample_rates: " << get_sample_rates().to_pp_string() << std::endl; std::cerr << "sample rate: " << (uint32_t)get_sample_rate() << std::endl; @@ -505,6 +515,12 @@ rfspace_source_c::~rfspace_source_c () _thread.join(); } + else + { + _run_tcp_keepalive_task = false; + _thread.interrupt(); + _thread.join(); + } close(_usb); @@ -580,6 +596,8 @@ bool rfspace_source_c::transaction( const unsigned char *cmd, size_t size, } else { + boost::mutex::scoped_lock lock(_tcp_lock); + #ifdef USE_ASIO _t.write_some( boost::asio::buffer(cmd, size) ); @@ -730,6 +748,22 @@ void rfspace_source_c::usb_read_task() } } +/* send periodic status requests to keep TCP connection alive */ +void rfspace_source_c::tcp_keepalive_task() +{ + std::vector< unsigned char > response; + unsigned char status_pkt[] = { 0x04, 0x20, 0x05, 0x00 }; + + if ( -1 == _tcp ) + return; + + while ( _run_tcp_keepalive_task ) + { + boost::this_thread::sleep_for(boost::chrono::seconds(60)); + transaction( status_pkt, sizeof(status_pkt), response ); + } +} + bool rfspace_source_c::start() { _sequence = 0; diff --git a/lib/rfspace/rfspace_source_c.h b/lib/rfspace/rfspace_source_c.h index 7d89ddc..c656063 100644 --- a/lib/rfspace/rfspace_source_c.h +++ b/lib/rfspace/rfspace_source_c.h @@ -129,6 +129,7 @@ private: /* functions */ std::vector< unsigned char > &response ); void usb_read_task(); + void tcp_keepalive_task(); private: /* members */ enum radio_type @@ -162,6 +163,8 @@ private: /* members */ gr::thread::thread _thread; bool _run_usb_read_task; + bool _run_tcp_keepalive_task; + boost::mutex _tcp_lock; boost::circular_buffer *_fifo; boost::mutex _fifo_lock;