-1
\$\begingroup\$

I'm trying to write a Unity client with C++ game server with TCP socket. When I'm sending game data back and forth with client and server, I'd want to serialize it in someway that can be deserialized on both ends.

Is xml appropriate for this or is there a better method of doing this?

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Does your xml serialization approach meet your needs? If not, in what specific way do you need to improve it? \$\endgroup\$
    – DMGregory
    Aug 17, 2020 at 12:09
  • \$\begingroup\$ XML has a large amount of boilerplate that will increase the size of your packets and can reduce networking performance. However, it's a standard format that's widely recognized by many languages and frameworks. \$\endgroup\$
    – Kevin
    Aug 17, 2020 at 16:46
  • \$\begingroup\$ I was mostly concerned about the packet size and was wondering if there was a way to significantly reduce it or whether it doesn't make too much of a difference \$\endgroup\$
    – James
    Aug 18, 2020 at 3:53
  • \$\begingroup\$ @JamesBAn I don't think XML is the best choice if you want to minimize packet size. Again, it has a lot of boilerplate with all of the open and close tags. As far as whether it "makes too much of a difference", that completely depends on the game. Is it a turn-based game played over a local network? An action shooter played over the internet? How many players at once? How many packets per second? What are the bandwidth limitations of your server? There's no one-size-fits-all answer. \$\endgroup\$
    – Kevin
    Aug 22, 2020 at 1:03
  • \$\begingroup\$ You could start here: stackoverflow.com/questions/48303058/… XML is absolutely worse. And you maybe should read about how to compress numbers. \$\endgroup\$
    – user743414
    Aug 25, 2020 at 12:11

1 Answer 1

0
\$\begingroup\$

XML has the advantage that it is a well-structured format which is both machine-readable and human readable.

But what makes it bad for use in game protocols is that it contains a lot of boilerplate. It will use a large number of bytes to encode relatively little information. Especially considering that game protocols usually consist of mostly numbers being passed around, which XML encodes in ASCII digits.

Which is why most games use binary protocols. That allows to squeeze a lot more information into the same numbers of bytes.

But while binary protocols are efficient, they also have their drawbacks during development. When the meaning of a byte depends on its position in a message, then a small bug can screw up the alignment of all following bytes, make the receiver interpret it completely wrong and cause very weird bugs. Such errors are hard to troubleshoot, because binary messages are difficult to read for human developers. And it is pretty easy to create such bugs, because serialization and deserialization will usually have to be completely self-developed code, and not use a library.

A compromise between the readability and structure of XML and the conciseness of binary protocols can be JSON, by the way. It still encodes everything in ASCII, but it has a lot less boilerplate than XML. It also writes out the key for every value (like XML), which makes it fail more gracefully when there is a bug in the serialization / deserialization.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .