I’m using a TCP library with a Server and Client class called Lacewing http://lacewing-project.org/
I have noticed that when I send a client several separate messages, it sometimes bundles them and the only way I can separate them is by parsing them again. When sending binary data, it is very hard to parse though.
Does anyone know why it might do this? I tried the DisableNagling() on both client and server but it still does it.
Here is an example of something I might send:
void ServerCore::loginRequestC( const std::string& userName, const std::string& password )
{
std::cout << "Got request" << std::endl;
ServerPlayer* player = (ServerPlayer*)getServerClient()->Tag;
player->setUsername(userName);
for (size_t i = 0; i < m_players.size(); ++i)
{
if(m_players[i] != player)
{
std::cout << "Telling new about " << m_players[i]->getUsername() << std::endl;
m_enc.playerJoinedS(m_players[i]->getUsername());
player->getClient()->Send(m_enc.getLastMessage().c_str());
}
}
m_enc.loginResultS(true,"");
player->getClient()->Send(m_enc.getLastMessage().c_str());
m_enc.playerJoinedS(userName);
for (size_t i = 0; i < m_players.size(); ++i)
{
if(m_players[i] != player)
m_players[i]->getClient()->Send(m_enc.getLastMessage().c_str());
}
}
So if I intended to have:
MSG_A
MSG_B
MSG_C
It might send:
MSG_A
MSG_BMSG_C
The messages get randomly bundled together. It is not my code that is the problem. I have checked.
Although it is useful to bundle messages, I want to control when it happens, not the library.
Thanks
This is not the library, but the TCP protocol itself that is doing it. TCP socket is a bi-directional stream of bytes. One write might result in multiple reads on the other side, and vise versa.
You have to design you application-level protocol so that it’s easy to split that stream into messages. Common approaches are to use a length prefix or a message delimiter.