|
Boost Users : |
Subject: [Boost-users] non-blocking read
From: David Kaplan (davkaplan_at_[hidden])
Date: 2010-02-21 10:20:51
I'm trying to implement a non_blocking read as follows (based on some
code on the net):
void util::net::set_result(optional<boost::system::error_code>* a,
boost::system::error_code b)
{
a->reset(b);
}
template <typename MutableBufferSequence>
optional<boost::system::error_code>
util::net::read_non_blocking(ip::tcp::socket *sock, const
MutableBufferSequence& buffers, int timeoutSec)
{
optional<boost::system::error_code> timer_result;
deadline_timer timer(sock->io_service());
timer.expires_from_now(boost::posix_time::seconds(timeoutSec));
timer.async_wait(boost::bind(set_result, &timer_result, _1));
optional<boost::system::error_code> read_result;
async_read(*sock, buffers,
boost::bind(set_result, &read_result, _1));
sock->io_service().reset();
while (sock->io_service().run_one())
{
if (read_result)
timer.cancel();
else if (timer_result) {
sock->cancel();
throw system_error(boost::system::error_code(errc::timed_out,
get_generic_category()));
}
}
return read_result;
}
I call the code as follows:
util::net::read_non_blocking(Module::_socket, buffer(buf, 4), 1);
(_socket is a ptr of type tcp::socket)
However I'm having problems linking:
undefined reference to `boost::optional<boost::system::error_code>
util::net::read_non_blocking<boost::asio::mutable_buffers_1>(boost::asio::basic_stream_socket<boost::asio::ip::tcp,
boost::asio::stream_socket_service<boost::asio::ip::tcp> >*,
boost::asio::mutable_buffers_1 const&, int)
Also, what will this code return exacty?
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net