When I call async_write()
, the remote peer is not receiving data until I call async_write()
again. For example, I have 3 packets, a
, b
, and c
:
SendPacket(a); // the remote side receives nothing
SendPacket(b); // the remote side receives packet a
SendPacket(c); // the remote side receives packet b
This is my code for sending:
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 like this:
packet pp;
pp.WriteWord(0);
pp.WriteDword(4);
pp.WriteWord(0);
SendPacket(pp);
Also, when SendPacket()
accepts packet
by value instead of reference, a crash occurs.
Gr