I want to listen on a boost::asio::ip::tcp::socket with a timeout. For this, I am using the std::future::wait_for function. Below is my code:
std::optional<boost::asio::ip::tcp::socket> server::listen()
{
boost::asio::ip::tcp::socket sock(m_io_service);
std::future<void> accept_status = m_acceptor.async_accept(
sock, boost::asio::use_future);
if (accept_status.wait_for(std::chrono::seconds(10)) == std::future_status::timeout)
{
// I hope there's no race-condition between
// accepting a connection and calling cancel
m_acceptor.cancel();
std::cerr << "Timeout" << std::endl;
return {};
}
std::cerr << "Accepted a connection" << std::endl;
return {std::move(sock)};
}
This is not working though: the client is able to connect, but I still get a timeout. Which means that the future object and the asynchronous accept function aren't communicating. What am I missing?
I am using Boost version 1.65.
Aucun commentaire:
Enregistrer un commentaire