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

Author Topic: Converting packet to bytes and back?  (Read 9071 times)

0 Members and 1 Guest are viewing this topic.

Honorabl3

  • Newbie
  • *
  • Posts: 9
    • View Profile
Converting packet to bytes and back?
« on: October 31, 2014, 06:33:23 am »
I heard that sending packets in bytes is faster. So as my title ask, how do you convert a packet to bytes and back? I'm using the enet network library to make my game utilize both TCP and UDP. I plan on packing everything I need into a sf::Packet and converting that into bytes then sending that.

Code: [Select]
// Server
sf::Packet packet = bleh;
bleh << variables;
bleh << positions; // Loading the packet up with stuff..
ENetPacket * packet = enet_packet_create (bleh.getData(), bleh.getDataSize() + 1, ENET_PACKET_FLAG_RELIABLE);
enet_peer_send (peer, 0, packet);

// Client
sf::Packet packet;
packet.append(event.packet->data, event.packet->dataLength);

Is that at all near close? If you're confused about the context of the variables, check the enet library I linked to above. I've also provided a UNFINISHED server-sided spoiler of some code if it helps:

(click to show/hide)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Converting packet to bytes and back?
« Reply #1 on: October 31, 2014, 07:55:30 am »
Don't send/receive sf::Packet with ENet. sf::Packet has its own internal protocol, which works well with SFML sockets, but won't be understood by other libraries.
Laurent Gomila - SFML developer

Honorabl3

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Converting packet to bytes and back?
« Reply #2 on: October 31, 2014, 09:06:31 pm »
Don't send/receive sf::Packet with ENet. sf::Packet has its own internal protocol, which works well with SFML sockets, but won't be understood by other libraries.
I really hope you're misunderstanding how I'm doing this. I really REALLY want to use sf::Packet to send stuff  because it's easy to pack and unload variables. I plan on converting the packet into raw bytes so that sending the raw data won't interfere with anything. Can I do it this way? If not, how do you recommend I send data? If I send it as a string, I don't want to bother converting a string to an integer for position stuff.
« Last Edit: October 31, 2014, 09:13:17 pm by Honorabl3 »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Converting packet to bytes and back?
« Reply #3 on: October 31, 2014, 09:22:44 pm »
"converting to bytes" sounds like you want to do serialization.  sf::Packet has absolutely nothing to do with serialization.  It's about sending data over a network in a way that takes care of endianness and other low-level issues for you, and it can only do that if the sf::Packet is the actual thing you send through the sf::Socket or whatever.

If you only need to serialize standard C++ scalar types then std::stringstream should be fine.  That class' interface is exactly what you appear to want sf::Packet's to be.  If you need to serialize user-defined structs and classes, you'll have to look elsewhere (I think boost has something for that).


Edit: I just noticed:
Quote
I heard that sending packets in bytes is faster
What do you mean by this?  All data in a computer is already "in bytes", regardless of what C++ abstraction you're using to manipulate it.

If you're concerned about the speed of sending one type of data versus another, first off I would be extremely surprised if it mattered at all (the amount of data is what I'd worry about), second since ENet is what you're actually using to send it you should look at ENet's documentation/forums/etc for advice on what sort of data is good or bad for performance with that library.
« Last Edit: October 31, 2014, 09:32:02 pm by Ixrec »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
AW: Converting packet to bytes and back?
« Reply #4 on: October 31, 2014, 09:31:59 pm »
If you'd want to use sf::Packet with something else than sfml-network, you'd have to implement the sf::Packet protocol yourself, which is not recommended, since that protocol is an internal detail, which could change any time.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Honorabl3

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Converting packet to bytes and back?
« Reply #5 on: October 31, 2014, 10:09:24 pm »
Thank you for your help, I will be using std::stringstream I guess.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Converting packet to bytes and back?
« Reply #6 on: October 31, 2014, 10:41:06 pm »
Don't guess. Learn how networking works and read the documentation of enet.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Converting packet to bytes and back?
« Reply #7 on: November 01, 2014, 11:06:50 am »
I'm using the enet network library to make my game utilize both TCP and UDP.
The site explicitly states that Enet is a UDP networking library. For TCP you will have to use something else.

Is there a reason why you don't use SFML, do you need certain features?


If you only need to serialize standard C++ scalar types then std::stringstream should be fine.  That class' interface is exactly what you appear to want sf::Packet's to be.
Why std::stringstream? I would serialize data over network as binary data and not text, otherwise you're quickly wasting a lot of bandwidth.

Although you can use stringstreams for binary data, you'll have to use the read() and write() functions, which are not only less convenient than << and >>, but also don't provide a portable binary format.


"converting to bytes" sounds like you want to do serialization.  sf::Packet has absolutely nothing to do with serialization.
If you'd want to use sf::Packet with something else than sfml-network, you'd have to implement the sf::Packet protocol yourself, which is not recommended, since that protocol is an internal detail, which could change any time.
Not necessarily. As far as I've understood Honorabl3, he wants to use sf::Packet because it provides a conversion between formatted data (integers, double, bool, etc) and a byte array. In that way, sf::Packet does provide some kind of serialization, it just does not guarantee anything about the internal format or its stability over different SFML versions. Those limitations, however, are not an inherent obstacle as long as you're aware of them.

When looking at Enet, it seems to provide only low-level packet functions, which forces the user to re-invent the wheel. The idea of using an existing abstraction is thus standing to reason. The sf::Packet class does not provide a sophisticated streaming API, but it has the functions getData(), getSize() and append() which allow conversion between formatted data types and byte arrays.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Honorabl3

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Converting packet to bytes and back?
« Reply #8 on: November 04, 2014, 06:18:42 pm »
I think you understand what I'm trying to do better than anyone here. I am trying to convert the sf::Packet into binary data and send that over enet. But I want to just know how to convert binary data back into an sf::Packet.

Thanks for posting, I almost gave up on this idea.
« Last Edit: November 04, 2014, 07:10:22 pm by Laurent »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Converting packet to bytes and back?
« Reply #9 on: November 04, 2014, 07:19:49 pm »
Apparently you need to re-read my post. That's exactly what I wrote.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Honorabl3

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Converting packet to bytes and back?
« Reply #10 on: November 04, 2014, 07:50:52 pm »
I'm using the enet network library to make my game utilize both TCP and UDP.
The site explicitly states that Enet is a UDP networking library. For TCP you will have to use something else.

Is there a reason why you don't use SFML, do you need certain features?
My bad, I don't need TCP. I'm using UDP because it functions like a TCP connection. It is faster then a TCP connection, and saves a client or w/e onto a .peer class object so that when a UDP connection is received it keeps the connection running.

Apparently you need to re-read my post. That's exactly what I wrote.
Your post doesn't mention converting binary data to sf::Packet?
« Last Edit: November 04, 2014, 07:53:55 pm by Honorabl3 »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Converting packet to bytes and back?
« Reply #11 on: November 04, 2014, 08:40:52 pm »
I'm using the enet network library to make my game utilize both TCP and UDP.
The site explicitly states that Enet is a UDP networking library. For TCP you will have to use something else.

Is there a reason why you don't use SFML, do you need certain features?
My bad, I don't need TCP. I'm using UDP because it functions like a TCP connection. It is faster then a TCP connection, and saves a client or w/e onto a .peer class object so that when a UDP connection is received it keeps the connection running.

UDP and TCP do not function similarly at all, even if most networking libraries give similar APIs for them.  You might want to read a bit more about what they actually are.

The short version is UDP just sends each packet once and prays they arrive, while TCP will set up an actual connection with some other machine, individually keep track of which packets successfully arrived, ensure correct ordering, resending packets as needed, adjusting how many it sends at a time to avoid network congestion, and more.  These extra features are why TCP is "slower", why it requires an additional object representing the connection, and why it's a good default for most applications (where correctness and completeness are what matter).  Even games that use UDP for speed still typically reimplement some subset of these features themselves.


But that aside, his original question was why are you trying to use both SFML and ENet for your networking?  Specifically, what feature is SFML lacking that ENet has?  SFML supports UDP too.

Quote
Apparently you need to re-read my post. That's exactly what I wrote.
Your post doesn't mention converting binary data to sf::Packet?
It did mention getData(), getSize() and append(), which are what you use for that.
« Last Edit: November 04, 2014, 08:47:55 pm by Ixrec »

Honorabl3

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Converting packet to bytes and back?
« Reply #12 on: November 04, 2014, 09:36:10 pm »
Quote from: Ixrec
UDP and TCP do not function similarly at all, even if most networking libraries give similar APIs for them.  You might want to read a bit more about what they actually are.

My bad!!! I mean't enet! Let me post the fixed version below.
Quote
My bad, I don't need TCP. I'm using enet because it functions like a TCP connection. It is faster then a TCP connection, and saves a client or w/e onto a .peer class object so that when a UDP connection is received it keeps the connection running.

But that aside, his original question was why are you trying to use both SFML and ENet for your networking?  Specifically, what feature is SFML lacking that ENet has?  SFML supports UDP too.
I'm using SFML's sf::Packet class because it's very easy to load and unload variables. enet would never send a sf::Packet, it would only send some binary data (which was created from the sf::Packet). Enet allows a client session instead of one simple UDP packet.
« Last Edit: November 04, 2014, 10:52:14 pm by Laurent »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Converting packet to bytes and back?
« Reply #13 on: November 04, 2014, 09:39:16 pm »
Stop using full quotes!!! Only quote relevant bits or don't quote at all if the context is obvious!!! >:(
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Honorabl3

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Converting packet to bytes and back?
« Reply #14 on: November 04, 2014, 10:09:50 pm »
Stop using full quotes!!! Only quote relevant bits or don't quote at all if the context is obvious!!! >:(
Haha I was going to remove some of the quotes, but I had food cooking in the kitchen. I was thinking "No one will complain about it this last time."