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

Author Topic: Is this a bug in the network library?  (Read 2899 times)

0 Members and 1 Guest are viewing this topic.

vro

  • Newbie
  • *
  • Posts: 44
    • View Profile
Is this a bug in the network library?
« on: June 04, 2012, 04:36:08 am »
I have a pretty simple UDP client/server going - the client sends updates to the server every 30 milliseconds, so 33.333 times per second. Nothing else is being sent anywhere else in the code (except for an initial connection message and then a disconnection message)

These updates are always 19 bytes, and Packet.getDataSize() confirms this. So 19 bytes 33 times per seconds should be sending out roughly 627 bytes per second.

However Windows resource monitor says my program is consistently sending out 4,000 to 5,000 bytes per second.

Is this some kind of bug, or is sf::Packet actually much much larger than Packet.getDataSize()?
Here is the code that gets called in the game loop:

void Client::sendUpdate()
{
    elapsedTime = elapsedTime + clock.getElapsedTime();
    if (elapsedTime.asMilliseconds() >= tickrate.asMilliseconds()) //tickrate = sf::milliseconds(30) set elsewhere
    {
        elapsedTime = clock.restart();
        sf::Packet sendPacket;
        sf::Uint32 protocolID = 1885697825;
        sf::Uint8 action = UPDATE;
        sendPacket << protocolID << action << localPlayer.id << localPlayer.positionX << localPlayer.positionY << localPlayer.rotation;
        cout << "sendPacket: " << sendPacket.getDataSize() << endl;
        if (socket.send(sendPacket, serverAddress, serverPort) != sf::Socket::Done)
        {
            cerr << "Error sending player input." << endl;
        }
    }
}
 

It's entirely possible of course that I'm just not seeing something I've done wrong, but I've been trying to figure this out for a while now so I thought I'd ask for help.

Also here's a screen cap of the resource monitor and the debug/console outputting the data size of the packet
« Last Edit: June 04, 2012, 09:55:45 am by vro »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Is this a bug in the network library?
« Reply #1 on: June 04, 2012, 08:16:12 am »
sf::Packet only adds 4 extra bytes. The UDP and IP layers add their own header too, but it's not more than a few bytes.

Your timing logic looks wrong, you should dump elapsedTime to the console to check that. I would have written a simpler code to do what you want:
void Client::sendUpdate()
{
    if (clock.getElapsedTime() >= tickrate)
    {
        clock.restart();
        ...

By the way:
(elapsedTime.asMilliseconds() >= tickrate.asMilliseconds()
This can be simplified to
(elapsedTime >= tickrate)

PS: please use the code=cpp tag to format your code blocks.
Laurent Gomila - SFML developer

vro

  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: Is this a bug in the network library?
« Reply #2 on: June 04, 2012, 09:42:57 am »
That fixed it, now it's sending ~700 bytes/second. I knew I did something wrong :/
Thanks Laurent. I love you :p

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Is this a bug in the network library?
« Reply #3 on: June 04, 2012, 09:49:42 am »
I will love you too if you edit your first message to use the code=cpp tag :D
Laurent Gomila - SFML developer

vro

  • Newbie
  • *
  • Posts: 44
    • View Profile
Re: Is this a bug in the network library?
« Reply #4 on: June 04, 2012, 09:56:20 am »
aha k you win

 

anything