I'm coding multiple servers who have to listen from multiple clients and process the data asynchronously.
I did a Server base class, that i derive for adding the packethandling functions, as well as some notifiers that i call from my session class.
void TcpSession::handle_read(const boost::system::error_code& e, std::size_t bytes_transferred)
{
if (!e)
{
if (bytes_transferred < 4 || bytes_transferred >= 65535)
return;
ptrServer->NotifyPacketReceive(this, *(MPacketBody*)&buffer_);
socket_.async_read_some(boost::asio::buffer(buffer_),
strand_.wrap(boost::bind(&TcpSession::handle_read, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)));
}
}
My NotifyPacketReceive is in the derived class, it will check for one identifier of the packet and will process the data. All of the functions executed after contains a packet to send, i do it like that
void DerivatedServerClass::Request(TcpSession* session, MPacketBody packet)
// called from DerivatedServerClass::NotifyPacketReceive
{
MOutPacket response(12);
// fill response with data
session->send(response);
}
void TcpSession::send(MOutPacket p)
{
boost::asio::async_write(socket_, boost::asio::buffer(p.packet(), p.size()),
strand_.wrap(boost::bind(&TcpSession::handle_write, shared_from_this(),
boost::asio::placeholders::error)));
}
Problem is that my write function seems to "collapse" with async_write from others sessions, resulting the response is not sent and my client is in infinite waiting state. I tried to apply the "strand"/thread pool like in the HTTP Server 3 Example from Asio documentation, but nothing changed at all.
I also tried the example from here with the Outbox : boost asio async_write : how to not interleaving async_write calls?
Same problem, so i guess it comes from somewhere else. The buffer must remains intact, but my MOutPacket is allocated "on the stack", should i make a pointer to the MOutPacket then call "delete pointer;" after sent the packet ? Is that even possible, and if so, will this not cause problems ?
I don't really know what to do, does anyone got an idea ? Thanks