Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, I was thinking about this really stupidly. </p> <ul> <li>First the deadline_timer needs to be completely inside the thread I want it to time in. That means it needs to be created inside the thread. </li> <li>Second I need to define the function called in the thread loop and not set it to the io_service::run function. So I made it the udp_server::start function. Inside the start call I create my deadline_timer.</li> </ul> <p>So here is the class:</p> <pre><code> class udp_server { public: udp_server(boost::asio::io_service&amp; io_service, short port) : io_service_(io_service), socket_(io_service_, udp::endpoint(boost::asio::ip::address_v4::from_string(current_address), port))//, // udp::v4() { // start udp receive socket_.async_receive_from( boost::asio::buffer(recv_buf), sender_endpoint_, boost::bind(&amp;udp_server::handle_receive_from, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); send_timer_ = NULL; } ~udp_server(){ io_service_.stop(); if(send_timer_){ send_timer_-&gt;cancel(); delete send_timer_; } } void start(); void startSendTimer(); void handle_send_to(const boost::system::error_code&amp; error, size_t bytes_recvd); void handle_receive_from(const boost::system::error_code&amp; error, size_t bytes_recvd); void handle_send_timer(); void send_timer_restart(); void stop() { io_service_.stop(); } private: boost::asio::io_service&amp; io_service_; udp::socket socket_; udp::endpoint sender_endpoint_; std::vector&lt;udp::endpoint&gt; clientList; udp_buffer recv_buf; boost::asio::deadline_timer* send_timer_; }; </code></pre> <p>Here are the relevant functions:</p> <pre><code> void udp_server::start(){ // startup timer startSendTimer(); // run ioservice io_service_.run(); } void udp_server::startSendTimer(){ // start send timer if(!send_timer_) send_timer_ = new boost::asio::deadline_timer(io_service_, boost::posix_time::milliseconds(500)); send_timer_restart(); } void udp_server::send_timer_restart(){ if(send_timer_){ // restart send timer send_timer_-&gt;expires_from_now(boost::posix_time::milliseconds(500)); send_timer_-&gt;async_wait(boost::bind(&amp;udp_server::handle_send_timer, this)); } } void udp_server::handle_send_timer(){ for(std::vector&lt;udp::endpoint&gt;::iterator itr = clientList.begin(); itr != clientList.end(); ++itr){ socket_.async_send_to( boost::asio::buffer("heart beat", strlen("heart beat")), *itr, boost::bind(&amp;udp_server::handle_send_to, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } send_timer_restart(); } </code></pre> <p>So I was thinking about this all wrong in the first place. I need to define my starting point of where the thread begins execution. The I can create the objects that need to reside in that thread inside the thread. </p> <p>The udp_server is now started like this:</p> <pre><code> static void PluginInit_PostLoadCallback() { _MESSAGE("NetworkPipe: PluginInit_PostLoadCallback called"); if(!g_Interface-&gt;isEditor) { _MESSAGE("NetworkPipe: Starting UDP"); udp_server_ptr = new udp_server(io_service, current_port); udp_thread = new boost::thread(boost::bind(&amp;udp_server::start, udp_server_ptr)); _MESSAGE("NetworkPipe: UDP Started"); NetworkPipeEnable = true; } else { _MESSAGE("NetworkPipe: Running in editor, not starting UDP"); } } </code></pre> <p>The deadline_timer creation occurs within the udp_thread now. Creating the deadline_timer object in the main thread would cause the program to fail to load properly.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload