私のプログラムは常にプラットフォーム依存の同期受信を使用していました。これは、タイムアウトまたは受信イベントまで実行をブロックしました。
recv(buf, size, timeout);
ここで、このコードをブーストに置き換えてクロスプラットフォームにしたいと考えています。私は解決策を見つけましたが、それはかなり醜いと思います(単一の関数呼び出しと比較して)。私はこれを書きました:
void IPV4_HOST::recv_timer_handler(const boost::system::error_code & e)
{
if (e.value() == boost::system::errc::success) {
f_recv_timeout = true;
asio_socket.cancel();
}
}
void IPV4_HOST::recv_handler(const boost::system::error_code & , size_t bytes)
{
recv_timer.cancel();
bytes_received = bytes;
}
int IPV4_HOST::receive(void * buf, size_t buf_size, TIME::INTERVAL timeout)
{
f_recv_timeout = false;
bytes_received = 0;
recv_timer.expires_from_now(timeout.get_boost_milli());
recv_timer.async_wait(boost::bind(&IPV4_HOST::recv_timer_handler, this, boost::asio::placeholders::error));
asio_socket.async_read_some(boost::asio::buffer(buf, buf_size),
boost::bind(&IPV4_HOST::recv_handler, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
io_service.run();
if (f_recv_timeout)
throw IO::TimeoutReceiveException();
return bytes_received;
}
私はそれを正しく行っていますか?これを行う簡単な方法はありますか?