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.