Boost: ASIO async_write cannot guarantee that all data is sent at one time.

Source: Internet
Author: User

Only by looking at the boost source code can we figure out what happened. First, I write data into the vector, and then use boost: ASIO: buffer to construct the vector into a mutable_buffer_1 object.

Refer to the document's heavy duty form: http://www.boost.org/doc/libs/1_53_0/doc/html/boost_asio/reference/buffer/overload24.html

buffer (24 of 28 overloads)Create a new modifiable buffer that represents the given POD vector.template<    typename PodType,    typename Allocator>mutable_buffers_1 buffer(    std::vector< PodType, Allocator > & data,    std::size_t max_size_in_bytes);Return ValueA mutable_buffers_1 value equivalent to:mutable_buffers_1(    data.size() ? &data[0] : 0,    min(data.size() * sizeof(PodType), max_size_in_bytes));

Note that the last part of the above Code is to explain the Internal principles. It turns out that the size member function of the vector is called, and then compared with the input size, who is small to use. Will it be my vector: The returned size is not 54, but 9.

Soon I added log tracing, which is indeed 9. Why? This must be related to my operations on writing data to a vector. The following is a code snippet:

void ConfigMessage::Write(vector<char>& buffer) {  buffer.assign(9, 0);  // SOH  buffer[0] = 0x01;  // Type  size_t i = 3;  buffer[i++] = 'U';  buffer[i++] = '1';    size_t len = domain.length();  buffer[i++] = static_cast<uint8_t>(len);  memcpy(&buffer[i], domain.c_str(), len);  i += len;  uint16_t temp = Int16ToBigEndian<uint16_t>(port);  memcpy(&buffer[i], &temp, 2);  i += 2;  buffer[i++] = timezone;

I basically use memcpy as a buffer for C to fill in data, so the size Member of the vector cannot correctly reflect the actual data. Therefore, I add a call at the end:

  buffer.resize(size_);

Test again. Solve the problem.

Therefore, when using vector to construct the buffer, be careful with its minimum rule. This problem is caused by my own logic. '

The recursive protection code written in the previous article also has an insurance effect. If you make another mistake one day, at least it can ensure that all data is sent.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.