Game Development Reference
In-Depth Information
Exchanging packets
Until now we have been sending and receiving data in the form of bytes (
char
arrays to be
exact), which is absolutely fine. Things get a bit trickier if we start sending types which are
more than a single byte in memory, such as an
integer
or a
double
.
The first problem comes from the different sizes of the same data types between platforms
and processors. For example, an
integer
is 32-bit on some configurations and 64-bit on
others.
To solve that problem, we use the classes that SFML provides for primitive types, such as
sf::UInt8
(8-bit unsigned),
sf::Int16
(16-bit signed),
sf::Int32
(32-bit signed),
and so on. With these, we can ensure that our data is the same size on all platforms. There
are no custom
float
or
double
types, because they are always the same size (on the
platforms on which SFML is supported).
Another (and more serious) problem comes in the endianness of different operating sys-
tems and different processors - in other words, how are the bytes of a data type interpreted
by the processor.
Big endian
processors expect the most significant byte first, whereas
little endian
family processors place the least significant byte first. If we send a
sf::UInt16
(16-bit) value of '00000000 00000010' (in binary), which on our machine is
2, on another machine it can read as 512 (reading the bytes in the opposite order:
"00000010 00000000").
To avoid that problem, SFML introduces an elegant solution — the
sf::Packet
class.