I've got a Client application which sends packet to the server, and server replies to client everything he sents. The problem is everything works perfectly after 1-2 first packets and then it goes really sloooooow. Need your help
Server code:
void runServer(unsigned short Port)
{
if (!Listener.Listen(Port))
return;
sf::IPAddress Address;
Listener.Accept(Client, &Address);
while(true)
{
sf::Packet Packet;
if (Client.Receive(Packet) == sf::Socket::Done)
Client.Send(Packet);
}
}
Client works on 3 threads.
1 is the main loop -getting events, drawing,pushing packets on queue(which will be sent). This code runs when i push a button
void Internet::queuePacket(sf::Packet* p)
{
{
sf::Lock Lock(sendMutex);
toSend.push(p);
}
}
2 is a loop which observes if there are any packets in queue to send, and it sends it .
void sendingLoop(void* UserData)
{
queue<sf::Packet*>& toSend= *static_cast< queue<sf::Packet*>* >(UserData);
while(runningThread)
{
if(!toSend.empty())
{
sf::Packet* p=NULL;
{
sf::Lock Lock(sendMutex);
p=toSend.front();
toSend.pop();
}
sf::Socket::Status st;
st=Application::instance->internet.Send(*p);
while(st==sf::Socket::NotReady)
{
sf::Sleep(0.01f);
st=Application::instance->internet.Send(*p);
}
delete p;
}
else
sf::Sleep(0.01f);
}
}
3 is a loop receiving packets.
void receivingLoop(void* UserData)
{
sf::SelectorTCP Selector;
Selector.Add(Application::instance->internet);
while(runningThread)
{
if(Selector.Wait(0.5f)>0)
{
sf::SocketTCP socket = Selector.GetSocketReady(0);
sf::Packet pac;
if(socket.Receive(pac)!=sf::Socket::Done)
out<<"Err receiving"<<endl;
sf::Uint8 type;
pac>>type;
switch(type)
{
case OBJECT:
out<<"type=OBJECT"<<endl;
break;
default:
out<<"Unknown packet type"<<endl;
break;
}
}
}
}
Blocking in client app is set to false, and in server to true.
Hlp!:D