When I call async_write() , the remote peer does not receive data until I call async_write() again. For example, I have 3 packages, a , b and c :
SendPacket(a);
This is my code to send:
void Session::SendPacket(packet p) { dword len = p.Lenght(); byte* buffer_send = new byte[len + 4]; //4 cause of the header memcpy(buffer_send + 4, p.GetRaw(), len); // copy everything to the buffer +4, 0-4 is header m_codec.EncodePacket(buffer_send, len); boost::asio::async_write(m_socket, boost::asio::buffer(buffer_send, len + 4), boost::bind(&Session::OnPacketSend, this, len + 4, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, buffer_send)); } void Session::OnPacketSend(int len, const boost::system::error_code &e, size_t bytes_transferred, byte* buf) { // this asynchronously fires when packet data is sent delete[] buf; if (e || bytes_transferred != len) { Stop(); return; } }
And I use it as follows:
packet pp; pp.WriteWord(0); pp.WriteDword(4); pp.WriteWord(0); SendPacket(pp);
In addition, when SendPacket() receives packet by value instead of reference, a failure occurs.
Gr
source share