Sign in to follow this  

boost::asio::tcp::socket continuously receives empty packets

This topic is 2627 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

    std::string sBuffer = new std::string;

pConn->Socket().async_read_some(
boost::asio::buffer(*sBuffer, this->m_sValid.length()),
boost::bind(&Gateway::Auth,
this,
sBuffer,
pConn));

In Gateway::Auth this code is called once more if some conditions aren't fulfilled. But always, std::string passed to Gateway::Auth is empty, and Gateway::Auth is called continuously - not only async_read_some returns immediately, but also read handler is called in same moment, without waiting for data to arrive. Am I doing something wrong or is that bug? Oh, even if I send something from other app, string passed to Auth is always empty.

Share this post


Link to post
Share on other sites
void Gateway::Auth(std::string *, Connection::Pointer)
where connection::pointer is shared_ptr. Connection::socket() returns socket and m_sValid is not empty std::string.

Share this post


Link to post
Share on other sites
I'm going to start to agree with Linus on his rants about C++. These samples say absolutely nothing about types or anything else useful regarding the code.

Regardless, purely by handwaiving, my guess would be that the instance of string examined by Auth is not the same instance that is filled by network read_some. ::buffer isn't supposed to make a copy, but it might have something to do with implicit copy being made due to const-ness or some such (can't seem to recall what *foo resolves to).

Why not pass string by reference (keeping a local instead of allocating with new), just like buffer expects it? And why pass it into Auth at all. It doesn't make much sense to have more than one pending read request, so just using class' own instance allocated as value should work. Or perhaps I'm just used to structuring code differently.

Or the problem is somewhere completely else.

Quote:
m_sValid is not empty std::string.
Are you sure? What if you just set the value to over 9000 or leave it out completely.

Share this post


Link to post
Share on other sites
What about setting the size of sBuffer after creating it?
std::string * sBuffer = new std::string;
sBuffer->resize( this->m_sValid.length() );
...
If you look at the code in buffer.hpp:

inline const_buffers_1 buffer(const std::string& data,
std::size_t max_size_in_bytes)
{
return const_buffers_1(
const_buffer(data.data(),
data.size() < max_size_in_bytes
? data.size() : max_size_in_bytes
#if defined(BOOST_ASIO_ENABLE_BUFFER_DEBUGGING)
, detail::buffer_debug_check<std::string::const_iterator>(data.begin())
#endif // BOOST_ASIO_ENABLE_BUFFER_DEBUGGING
));
}



Your buffer would still be an empty buffer since the amount of space in the string is less than the max length you pass in.

Share this post


Link to post
Share on other sites
The string is empty. You are not meant to use the string's memory in that way. You should use c_str which gives you this hint since it is const. Use std::vector instead and make sure it has memory allocated.

Share this post


Link to post
Share on other sites

This topic is 2627 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