0

我正在调整此示例以检查消息是否接收时间过长。

read_complete被调用,何时read_start完成。但是,我必须检查接收到的数据是否完整并将其解释为不完整,如果在给定时间内没有完全接收到。

这些是我在代码/伪代码中对上面代码的扩展:

void read_complete(const boost::system::error_code& error, size_t bytes_transferred)
{ // the asynchronous read operation has now completed or failed and returned an error
  if (!error)
  { // read completed, so process the data
    cout.write(read_msg_, bytes_transferred); // echo to standard output
    MyOnReceivedData(MyHandle someHandle, numOfBytes);
    read_start(); // start waiting for another asynchronous read again
  }
  else do_close(error);
} 

void MyOnReceivedData(MyHandle someHandle,int numOfBytes){
  ResetPacketTimer();
  \\check if whole packet was received
  if (wholePacket){    
    MyOnReceivedPacket(someHandle);    
  }
}

void MyOnReceivedPacket(MyHandle someHandle){
  cout << "packet"
  clearBuffer(someHandle);   
}

void MyOnPacketTimeout(MyHandle someHandle){
   cout << "we're in";
   if (!bufferEmpty(someHandle)){
     cout << "timeout";
   }
}

void ResetPacketTimer(MyHandle someHandle){
    boost::asio::io_service io;
    boost::asio::deadline_timer t(io, boost::posix_time::milliseconds(10000));

    t.async_wait(boost::bind(&MyOnPacketTimeout, someHandle);
    //this works async, but MyOnPacketTimeout is not called
    boost::thread t2(boost::bind(&boost::asio::io_service::run, &io));
    //this works, but not async - app waits for 10 secs
    //io.run();
}

我确实在这里提出了一个类似的问题How can I setup a deadline_timer in this environment? ,但现在我已经让我的计时器工作了——这只是线程问题。因此,如果这是问题所在,请关闭较旧的问题,但请不要解决这个问题。

我的主要问题是,我该怎么做ResetPacketTimer,以便:

  • 我捕捉到任何花费超过 10 秒的数据包
  • 不要以为我抓住了一个,当在第一个数据包的 10 秒用完之前已经部分接收到另一个数据包时(因此尚未调用clearBuffer )
4

0 回答 0