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

Author Topic: Sfml packet questions  (Read 3368 times)

0 Members and 1 Guest are viewing this topic.

Nybble

  • Newbie
  • *
  • Posts: 17
    • View Profile
Sfml packet questions
« on: May 26, 2013, 07:51:32 am »
Hello everyone, I hope this hasn't been asked before I have searched for a while now and could not find the answer to this...

The Sfml::Packet class is pretty simple and I'm trying to do something like what the class does, where I am having trouble is copying unsigned long or long long etc, I was using stf::copy but it was not working properly... I then used memcpy and it works but then I'm having trouble with the position on vector... take a look at this:

This is the Packet& operator >> (const unsigned long & data)
Quote
std::copy(&data, &data+ sizeof(data), std::back_inserter(buffer));

This is the Packet& operator >> (unsigned & long data)
Quote
std::copy(buffer[0] + pos, &buffer[0] + pos + sizeof(data), &data);
        pos += sizeof(dataSize);

this work for small numbers but if I use bigger numbers it will spit random numbers....

If Laurent or anyone that has had some experience with could help me with this, that would be awesome.

the memcpy version works like this
Quote
memcpy(&buffer[0], &data, sizeof(data));

AND:

memcpy(&data, &buffer[0], sizeof(data));

But the problem is when I use the operator it is always getting buffer[0] obviously and I am stuck on how to move the pos variable accordingly, it is really late an I have been at it for a while so I know is probably simple but my brain is blocking common sense right now...

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Sfml packet questions
« Reply #1 on: May 26, 2013, 10:27:07 am »
and I'm trying to do something like what the class does, where I am having trouble is copying unsigned long or long long etc
Could you describe more clearly what you want to achieve? What problem do you have exactly?

This is the Packet& operator >> (const unsigned long & data)
The const is wrong if you want the right operand to be written.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Nybble

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Sfml packet questions
« Reply #2 on: May 26, 2013, 02:07:50 pm »
I want to write unsigned longs, long longs, int etc on to vector<char> and the other ay around.

Nybble

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Sfml packet questions
« Reply #3 on: May 28, 2013, 04:14:31 am »
Well since I got it answered a while back and I remember this thread let me post my answer:

Code: [Select]
vector<char> buffer;
Packet & Packet::operator << (const unsigned int & data)
{
    unsigned char const * p = reinterpret_cast<unsigned char const*>(&data);
    std::copy(p, p + sizeof(data), std::back_inserter(buffer));
    return *this;
}

and to retrieve the data (in this case unsigned int):

Code: [Select]
vector<char> buffer;
Packet& Packet::operator >> (unsigned int & data)
{
    std::copy(&buffer[pos], &buffer[pos] + sizeof(data), reinterpret_cast<char*>(&data));
    pos += sizeof(data);
    return *this;
}

of course you can use templates if you want.

If you are using char arrays this is a better approach:
Code: [Select]
*(uInt32*)(&buffer[position]) = data;

and:

Code: [Select]
data = *(uInt32*)(&buffer[position]);

For anyone that might stumble across this post looking for something similar.

 

anything