Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Effective SFML networking..  (Read 4135 times)

0 Members and 2 Guests are viewing this topic.

troopson

  • Newbie
  • *
  • Posts: 30
    • View Profile
Effective SFML networking..
« on: July 13, 2011, 12:49:19 pm »
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:
Code: [Select]

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
Code: [Select]

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 .

Code: [Select]
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.
Code: [Select]

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

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Effective SFML networking..
« Reply #1 on: July 13, 2011, 06:32:15 pm »
Can you indent your code properly? It is nearly impossible to read.
Laurent Gomila - SFML developer

troopson

  • Newbie
  • *
  • Posts: 30
    • View Profile
Effective SFML networking..
« Reply #2 on: July 13, 2011, 10:42:41 pm »
Well I thought its kinda well indented :) I changed some ;p
Oh the result differs between a textbox i wrote that.. sort of hard to make it clear ;d Hope you can read it since its the minimal code.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Effective SFML networking..
« Reply #3 on: July 14, 2011, 09:35:40 am »
It's even worse.

What software do you use to type your code in ? Most text editor / IDE have some 'autoindent' / format feature to help you. Very very useful!
SFML / OS X developer

troopson

  • Newbie
  • *
  • Posts: 30
    • View Profile
Effective SFML networking..
« Reply #4 on: July 14, 2011, 08:53:32 pm »
Anyway thats only a few lines to watch.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Effective SFML networking..
« Reply #5 on: July 14, 2011, 09:03:55 pm »
Quote
Anyway thats only a few lines to watch

... or to edit. Since it was only a few lines, I did it. Avoid tabs, use spaces instead. Tabs are the best way to make our code unreadable across editors/viewers. Spaces are consistent.

Which one si slow, the client or the server?
Laurent Gomila - SFML developer

troopson

  • Newbie
  • *
  • Posts: 30
    • View Profile
Effective SFML networking..
« Reply #6 on: July 14, 2011, 10:00:45 pm »
Quote from: "Laurent"
Quote
Anyway thats only a few lines to watch

... or to edit. Since it was only a few lines, I did it. Avoid tabs, use spaces instead. Tabs are the best way to make our code unreadable across editors/viewers. Spaces are consistent.

Which one si slow, the client or the server?

TBH i think client.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Effective SFML networking..
« Reply #7 on: July 15, 2011, 09:04:48 am »
Your code looks good. But it's incomplete so it's hard to figure out what's wrong. You should try to write a complete and minimal example that reproduces your problem.
Laurent Gomila - SFML developer

troopson

  • Newbie
  • *
  • Posts: 30
    • View Profile
Effective SFML networking..
« Reply #8 on: July 15, 2011, 01:08:39 pm »
So...big applications work like this? 3 threads and a selector?Iis that a well projected loop ? Btw in my code i meant i waste 1/3 CPU time for loop that only checks whether there is a message to sent .