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

Author Topic: Public extract() function for sf::Packet  (Read 2835 times)

0 Members and 1 Guest are viewing this topic.

Marukyu

  • Newbie
  • *
  • Posts: 36
    • View Profile
Public extract() function for sf::Packet
« on: April 26, 2012, 12:54:49 pm »
Hello,

I have recently switched from TCP/UDP mixed to UDP only networking code. The protocol I am designing for my project will have to be as optimized as possible, as ~20 packets per second will be transferred between server and client. I use strings quite frequently in the protocol, but when I checked out the way SFML handles strings, I saw that it actually stores 4 bytes for the string length in the packet, despite UDP having a maximum packet size of slightly less than Uint16's max value. Because I send quite a few strings per packet, ranging from 2 to around 10, the difference of bytes per second compared to if SFML would use 2-byte length markers for strings does add up to quite a bit (few hundred bytes per second), and when doing networking, memory optimization is highly important.

I thought I would just write a "ShortString" class that works just like a string, but has its max length capped at UDP's maximum packet size, and when appended to a packet using the bitshift operator overloads, the packet would just use sf::Uint16 for length rather than sf::Uint32. Appending the string works using the public append() function, but I can't find a way to extract the string efficiently. The internal method for extracting a string relies on a private class variable, so the only way to do it would be extracting the string char by char, which is obviously unoptimized and slow. So, I would like to propose a public function for sf::Packet that takes a length (the read position would be moved by that amount) and returns a const void * or const char * to "getData() + m_readPos" before the read position was moved. The function could be called "const char * sf::Packet::extract(std::size_t lengthInBytes)". This would effectively give the developers more control about extracting custom data types from packets, as the append function already makes it possible to insert any kind of data.

tl;dr: something like this:
const char * sf::Packet::extract(std::size_t lengthInBytes)
{
    if (!checkSize(lengthInBytes))
        return NULL;

    const char * ret = getData() + m_readPos;
    m_readPos += lengthInBytes;
    return ret;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Public extract() function for sf::Packet
« Reply #1 on: April 26, 2012, 01:02:12 pm »
Hi

Please check the task tracker before posting.

https://github.com/SFML/SFML/issues/112
Laurent Gomila - SFML developer

Marukyu

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: Public extract() function for sf::Packet
« Reply #2 on: April 26, 2012, 01:03:48 pm »
Ah, sorry, I searched the forums only, forgot that the tracker was for suggestions too. :p