mercredi 26 avril 2017

Applying a timeout to UDP call receive_from in ASIO

I have the following bit of ASIO code, that synchronously reads UDP packets. The problem is if no data packets of the given size have arrived in a given time frame (30 seconds) I'd like the recieve_from function to return with some kind of error to specif icy timeout.

for (;;)
{
  boost::array<char, 1000> recv_buf;
  udp::endpoint remote_endpoint;
  asio::error_code error;

  socket.receive_from(asio::buffer(recv_buf),   // <-- require timeout
      remote_endpoint, 0, error);

  if (error && error != asio::error::message_size)
    throw asio::system_error(error);

  std::string message = make_daytime_string();

  asio::error_code ignored_error;
  socket.send_to(asio::buffer(message),
      remote_endpoint, 0, ignored_error);
}

Looking at the documentation non of the UDP oriented calls support a time-out mechanism.

What is the correct way (also portable if possible) for having a time-out with syncronous UDP calls in ASIO?

Aucun commentaire:

Enregistrer un commentaire