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

Author Topic: SocketTCP::Receive(Packet&) bug  (Read 3144 times)

0 Members and 2 Guests are viewing this topic.

Billiard26

  • Newbie
  • *
  • Posts: 3
    • View Profile
SocketTCP::Receive(Packet&) bug
« on: March 02, 2010, 11:19:40 am »
I have been experiencing a bug when receiving packets.
I am using a blocking socket.

A few lines into the function ...
Socket::Status SocketTCP::Receive(Packet& packet)
Code: [Select]

    if (myPendingPacketSize < 0)
    {
        Socket::Status status = Receive(reinterpret_cast<char*>(&packetSize), sizeof(packetSize), received);
        if (status != Socket::Done)
            return status;

        packetSize = ntohl(packetSize);
    }


There is no check for if (sizeof(packetSize) == received)
sizeof(packetSize) is only 4 bytes so almost always all 4 bytes will be received on the first return of Receive.

But not ALWAYS. I have debugged the "received" value in this function to rarely come out 1,2, or 3 which causes packetSize to be read incorrectly, reading into the middle of a packet, and messing up all further received packets on that socket.

Something like the following needs to be done when receiving the header of the packet. ( It's similar to what is being done when receiving the packet's data. It was just assumed 4 bytes is always read first try for the header. )

Code: [Select]

    std::size_t Received   = 0;
while (Received < sizeof(PacketSize))
{
std::size_t r;
Socket::Status Status = Receive(reinterpret_cast<char*>(&PacketSize)+Received, sizeof(PacketSize)-Received, r);
if (Status != Socket::Done)
return Status;
Received += r;
}

        PacketSize = ntohl(PacketSize);


When using this modification all of my packets are received correctly 100% of the time. It could be dependent on the OS for who would experience the problem and who wouldn't.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SocketTCP::Receive(Packet&) bug
« Reply #1 on: March 02, 2010, 01:01:12 pm »
Hi

Thanks for your feedback.

I implemented a fix (based on yours but that works for non-blocking sockets too), can you get the latest revision and tell me if it works? I cannot reproduce this particular situation on my computer.
Laurent Gomila - SFML developer

Billiard26

  • Newbie
  • *
  • Posts: 3
    • View Profile
SocketTCP::Receive(Packet&) bug
« Reply #2 on: March 02, 2010, 08:02:30 pm »
The fix looks like it would work with non-blocking sockets and seems to work in my project using blocking sockets.

I was experiencing the problem while transmitting a pretty constant stream of packets over the net (didn't seem to happen locally because the packets are received almost instantly and the buffer never has half a packet, I guess).

Thanks for the quick fix. You should add better joystick with basic rumble support to SFML :P ( at least I cant find SFML documentation for anything like joystick count, buttons/axis/hat count, joystick name )

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SocketTCP::Receive(Packet&) bug
« Reply #3 on: March 02, 2010, 08:33:41 pm »
Quote
The fix looks like it would work with non-blocking sockets and seems to work in my project using blocking sockets.

Great, thanks :)

Quote
You should add better joystick with basic rumble support to SFML

Sorry but I don't know what "rumble" means :lol:
Does it have anything to do with force feedback?

Quote
at least I cant find SFML documentation for anything like joystick count, buttons/axis/hat count, joystick name

The joystick support is not the most complete one, it is focused on what's important. There is nothing to retrieve the actual number of joysticks, buttons, axis or hat count, but there are constants that define max limits for all these values. And there is nothing to read the joystick names.
This part of SFML may be more complete in the future (probably in SFML 2).
Laurent Gomila - SFML developer

Billiard26

  • Newbie
  • *
  • Posts: 3
    • View Profile
SocketTCP::Receive(Packet&) bug
« Reply #4 on: March 02, 2010, 09:04:28 pm »
Quote
Sorry but I don't know what "rumble" means Laughing
Does it have anything to do with force feedback?


Yeah, force feedback. DirectInput force feedback is just so complicated and only for windows. Would be nice to have a simpler interface.

Quote
This part of SFML may be more complete in the future (probably in SFML 2).


Cool. I'd be interested in helping with joystick support for SFML. We could discuss how you want it laid out maybe and I could help jump start improving the joystick functions. I have quite a bit of free time. :lol:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
SocketTCP::Receive(Packet&) bug
« Reply #5 on: March 02, 2010, 11:18:30 pm »
Quote
Yeah, force feedback. DirectInput force feedback is just so complicated and only for windows. Would be nice to have a simpler interface.

That's the problem. As far as I know, force feedback is only available on Windows through DirectX. Not really a good candidate for SFML ;)

Quote
Cool. I'd be interested in helping with joystick support for SFML. We could discuss how you want it laid out maybe and I could help jump start improving the joystick functions. I have quite a bit of free time

Well, I have no idea what the future improvements for joysticks (and input in general) will look like, and I don't plan to think about it at the moment.
Laurent Gomila - SFML developer