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?
util::net::read_non_blocking(Module::_socket, buffer(buf, 4), 1);
Note that it's *blocking* read with timeout.
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)
Maybe you tried to define the function template in cpp, without explicit specialization. Define your function templates in the header.
Also, what will this code return exacty?
What do you mean by saying "return"? The function attempts to read data to the buffer you suplly, and if the operation doesn't complete in some time-interval, it throws an exception.
participants (2)
-
David Kaplan
-
Igor R