Sign in to follow this  

boost asio read loop

This topic is 3461 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Want to read exactly 8 bytes, no more. I was hoping to automatically do this by giving a container 8 bytes long and let the asio::buffer figure it out, so I don't have to keep track of how many bytes got read. I don't think it likes me giving it an iterator but I can't think of any other way besides tracking the number of bytes myself, reading into a temporary buffer, and then copying into the final buffer. Assuming the second sol'n via asio::read()? I can't figure out how to make it take a parameter of how many bytes max to read. asio::socket::read_some() reference
std::vector<char> packed_dimensions(8);
while (packed_dimensions.size() < packed_dimensions.capacity())
{
	boost::this_thread::interruption_point();
	boost::system::error_code connection_error;

	size_t len = socket.read_some(boost::asio::buffer(packed_dimensions.end()), connection_error);				
	if (connection_error == boost::asio::error::eof) break; // Connection closed cleanly by peer.
	else if (connection_error) throw boost::system::system_error(connection_error);
}




3>.\Snapstream.cpp(211) : error C2665: 'boost::asio::buffer' : none of the 8 overloads could convert all the argument types
3>        C:\Program Files\boost\boost_1_35_0\boost/asio/buffer.hpp(468): could be 'boost::asio::mutable_buffers_1 boost::asio::buffer(const boost::asio::mutable_buffer &)'
3>        C:\Program Files\boost\boost_1_35_0\boost/asio/buffer.hpp(488): or 'boost::asio::const_buffers_1 boost::asio::buffer(const boost::asio::const_buffer &)'
3>        C:\Program Files\boost\boost_1_35_0\boost/asio/buffer.hpp(771): or 'boost::asio::const_buffers_1 boost::asio::buffer(const std::string &)'
3>        while trying to match the argument list '(std::_Vector_iterator<_Ty,_Alloc>)'
3>        with
3>        [
3>            _Ty=char,
3>            _Alloc=std::allocator<char>
3>        ]
3>.\Snapstream.cpp(211) : error C2780: 'size_t boost::asio::basic_stream_socket<Protocol>::read_some(const MutableBufferSequence &)' : expects 1 arguments - 2 provided
3>        with
3>        [
3>            Protocol=boost::asio::ip::tcp
3>        ]
3>        C:\Program Files\boost\boost_1_35_0\boost/asio/basic_stream_socket.hpp(639) : see declaration of 'boost::asio::basic_stream_socket<Protocol>::read_some'
3>        with
3>        [
3>            Protocol=boost::asio::ip::tcp
3>        ]



I thought of something like this
//size_t len = boost::asio::read(socket, boost::asio::buffer(&*bytes.end(), bytes.capacity() - bytes.size()));
but then the vector wouldn't know it got bigger. Is there an approach I could use with boost::array?

Share this post


Link to post
Share on other sites
FWIW this is my working alternative, but it needs a second buffer.

std::vector<char> packed_dimensions(8);
while (packed_dimensions.size() < 8)
{
boost::this_thread::interruption_point();
boost::system::error_code connection_error;
boost::array<char, 8> buf;

size_t len = socket.read_some(boost::asio::buffer(buf), connection_error);
if (connection_error == boost::asio::error::eof) break; // Connection closed cleanly by peer.
else if (connection_error) throw boost::system::system_error(connection_error);

char *b = buf.c_array();
std::for_each(b, b+len, boost::bind(&std::vector<char>::push_back, packed_dimensions, _1));
}




Also fwiw this exact loop is used again to receive lots and lots of data in 4meg chunks.

Share this post


Link to post
Share on other sites
Here's how I'd do it:
using boost::asio::ip::tcp;

void read_n_bytes(tcp::socket & socket, size_t n = 8)
{
std::vector<char> packed_dimensions(n);
boost::system::error_code e;
size_t len = 0;

do {
len += socket.read_some(boost::asio::buffer(&packed_dimensions[len], n-len), e);
} while ( (len < n) && (!e) );

if (len == n) {
std::cout << std::string(packed_dimensions.begin(), packed_dimensions.end()) << std::endl;
} else {
throw std::exception(e.message().c_str());
}
}

int main(int argc, char**argv)
{
try
{
boost::asio::io_service io_service;

tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 80));

for (;;)
{
tcp::socket socket(io_service);
acceptor.accept(socket);

while (true) read_n_bytes(socket);

boost::system::error_code ignored_error;
}
}
catch (std::exception& e)
{
std::cerr << e.what() << std::endl;
}

return 0;
}


Note the different handling of eof condition.

eof can occur at any time. If you simply break out of it, your buffer might contain incomplete data (for example, it read 3 bytes, then encountered eof).

Since read_some returns 0 on error, reading len==n bytes means we successfully completed filling the buffer. If len < n, then we must have exited due to error, and we throw that.

The above demo binds to http port for sake of testing. Also, why vectors? boost::array would be perfect for this.

Share this post


Link to post
Share on other sites

This topic is 3461 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this