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

Author Topic: Identifying types of packets  (Read 3100 times)

0 Members and 1 Guest are viewing this topic.

dixondean25

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Identifying types of packets
« on: August 08, 2014, 05:46:45 pm »
I was going to post here http://en.sfml-dev.org/forums/index.php?topic=3172.0, but it's too old so I thought I'd start a new topic.

Basically the original poster wanted to check the type of packet he sent before he opened it on the receiving end, and his idea was to put a header at the beginning of each packet to correctly identify how to proceed with opening it and storing it correctly.

Laurent replied with this code for the sender and receiver.

Sending:
// sending
sf::Packet packet;
packet << header << ... specific data ... ;
socket.Send(packet);

Receiving:
// receiving
sf::Packet packet;
socket.Receive(packet);

sf::Uint8 header;
packet >> header;

switch (header)
{
    case ConnectionRequest:
        packet >> ... specific data ...;
        connect(...);
        break;

    case MoveObject:
        packet >> ... specific data ...;
        moveObject(...);
        break;

    ...
}

I think this is a great idea, but I wanted to make sure with you guys if you all think it's the right way to do this before I start.

Also, on the receiving side, inside the switch, each case has:
packet >> ...specific data...;

I'm wondering if that"specific data" could be a user type?

For example:

struct Character
{
    sf::Uint8 age;
    std::string name;
    float height;
};

sf::Packet& operator <<(sf::Packet& packet, const Character& character)
{
    return packet << character.age << character.name << character.height;
}

sf::Packet& operator >>(sf::Packet& packet, Character& character)
{
    return packet >> character.age >> character.name >> character.height;
}

struct Enemy
{
        sf::Uint16 health;
        sf::Uint16 power;
        sf::Uint8 type;
};

sf::Packet& operator <<(sf::Packet& packet, const Enemy& enemy)
{
    return packet << enemy.health << enemy.power << enemy.type;
}

sf::Packet& operator >>(sf::Packet& packet, Enemy& enemy)
{
    return packet >> enemy.health >> enemy.power >> enemy.type;
}

// receiving
sf::Packet packet;
socket.Receive(packet);

sf::Uint8 header;
packet >> header;

switch (header)
{
    case isCharacter:
                Character billy;
        packet >> billy;
        break;

    case isEnemy:
                Enemy boss;
        packet >> boss;
        break;

    ...
}

I guess what I'm asking is if the header value could be separate from the user type?
« Last Edit: August 08, 2014, 05:48:26 pm by dixondean25 »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Identifying types of packets
« Reply #1 on: August 08, 2014, 05:59:45 pm »
Quote
I think this is a great idea, but I wanted to make sure with you guys if you all think it's the right way to do this before I start.
It's dead simple and it works as expected, so I think you can safely assume that this is a good way to do it. It may even be the only way to solve such a simple problem ;)

Quote
I'm wondering if that"specific data" could be a user type?
Why not? It can be anything that you want.

Quote
I guess what I'm asking is if the header value could be separate from the user type?
This is the case in the code that you show.
Laurent Gomila - SFML developer

dixondean25

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: Identifying types of packets
« Reply #2 on: August 08, 2014, 06:19:02 pm »
Okay so the sending side could look like this?

// sending
sf::Packet packet;
sf::Uint8 header = 5;
packet << header;

Character sally;
sally.age = 20;
sally.name = "Sally";
sally.height = 6;

packet << sally;

socket.send(packet);

and it will just insert the new character after the header?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Identifying types of packets
« Reply #3 on: August 08, 2014, 07:07:19 pm »
Yes. That's how it works.

Why don't you start writing something, with the doc and tutorials to help you, instead of asking if every single thing that you plan to write will be ok? Don't be afraid to try things ;)
Laurent Gomila - SFML developer

dixondean25

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: Identifying types of packets
« Reply #4 on: August 08, 2014, 07:43:36 pm »
I usually do that, but I'm new to networking and I just want to have a good system  :)

Also, can I pass a packet as a parameter? I'm getting errors trying to pass a packet as a parameter.

I'm thinking I might have to do something with getData() function, can you help me out?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Identifying types of packets
« Reply #5 on: August 09, 2014, 03:00:15 pm »
Quote
I usually do that, but I'm new to networking and I just want to have a good system
Nobody can design and write a good system when he starts something new ;)
It's more beneficial and rewarding to write a good system after trial & error (you learn a lot from that), rather than blindly doing what forum people told you to do.

Quote
Also, can I pass a packet as a parameter?
Yes.

Quote
I'm getting errors trying to pass a packet as a parameter.
Error messages, please.
Laurent Gomila - SFML developer