dsfml.network.packet

Packets provide a safe and easy way to serialize data, in order to send it over the network using sockets (sf::TcpSocket, sf::UdpSocket).

Packets solve 2 fundamental problems that arise when transferring data over the network:

  • data is interpreted correctly according to the endianness
  • the bounds of the packet are preserved (one send == one receive)

$(PARA The $(U Packet) class provides both input and output modes.)

Members

Classes

Packet
class Packet

Utility class to build blocks of data to transfer over the network.

SfPacket
class SfPacket
Undocumented in source.

Functions

sfPacket_append
void sfPacket_append(sfPacket* packet, void* data, size_t sizeInBytes)

Append data to the end of a packet

sfPacket_canRead
bool sfPacket_canRead(sfPacket* packet)

Test the validity of a packet, for reading

sfPacket_clear
void sfPacket_clear(sfPacket* packet)

Clear a packet

sfPacket_copy
sfPacket* sfPacket_copy(sfPacket* packet)

Create a new packet by copying an existing one

sfPacket_create
sfPacket* sfPacket_create()

Create a new packet

sfPacket_destroy
void sfPacket_destroy(sfPacket* packet)

Destroy a packet

sfPacket_endOfPacket
bool sfPacket_endOfPacket(sfPacket* packet)

Tell if the reading position has reached the end of a packet

sfPacket_getData
const(void)* sfPacket_getData(sfPacket* packet)

Get a pointer to the data contained in a packet

sfPacket_getDataSize
size_t sfPacket_getDataSize(sfPacket* packet)

Get the size of the data contained in a packet

sfPacket_readBool
bool sfPacket_readBool(sfPacket* packet)

Functions to extract data from a packet

sfPacket_readDouble
double sfPacket_readDouble(sfPacket* packet)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfPacket_readFloat
float sfPacket_readFloat(sfPacket* packet)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfPacket_readInt16
short sfPacket_readInt16(sfPacket* packet)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfPacket_readInt32
int sfPacket_readInt32(sfPacket* packet)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfPacket_readInt8
byte sfPacket_readInt8(sfPacket* packet)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfPacket_readUint16
ushort sfPacket_readUint16(sfPacket* packet)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfPacket_readUint32
uint sfPacket_readUint32(sfPacket* packet)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfPacket_readUint8
ubyte sfPacket_readUint8(sfPacket* packet)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfPacket_writeBool
void sfPacket_writeBool(sfPacket* packet, bool )

Functions to insert data into a packet

sfPacket_writeDouble
void sfPacket_writeDouble(sfPacket* packet, double )
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfPacket_writeFloat
void sfPacket_writeFloat(sfPacket* packet, float )
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfPacket_writeInt16
void sfPacket_writeInt16(sfPacket* packet, short )
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfPacket_writeInt32
void sfPacket_writeInt32(sfPacket* packet, int )
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfPacket_writeInt8
void sfPacket_writeInt8(sfPacket* packet, byte )
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfPacket_writeUint16
void sfPacket_writeUint16(sfPacket* packet, ushort )
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfPacket_writeUint32
void sfPacket_writeUint32(sfPacket* packet, uint )
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
sfPacket_writeUint8
void sfPacket_writeUint8(sfPacket* packet, ubyte )
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.

Structs

sfPacket
struct sfPacket
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.

Examples

int x = 24;
string s = "hello";
double d = 5.89;

// Group the variables to send into a packet
auto packet = new Packet();
packet.write(x);
packet.write(s);
packet.write(d);

// Send it over the network (socket is a valid TcpSocket)
socket.send(packet);

////////////////////////////////////////////////////////////////

// Receive the packet at the other end
auto packet = new Packet();
socket.receive(packet);

// Extract the variables contained in the packet
int x;
 s;
double d;
if (packet.read(x) && packet.read(s) && packet.read(d))
{
    // Data extracted successfully...
}

$(PARA Packets also provide an extra feature that allows to apply custom transformations to the data before it is sent, and after it is received. This is typically used to handle automatic compression or encryption of the data. This is achieved by inheriting from sf::Packet, and overriding the onSend and onReceive functions.)

class ZipPacket : Packet
{
    override const(void)[] onSend()
    {
        const(void)[] srcData = getData();

        return MySuperZipFunction(srcData);
    }

    override void onReceive(const(void)[] data)
    {
        const(void)[] dstData = MySuperUnzipFunction(data);

        append(dstData);
    }
}

// Use like regular packets:
auto packet = new ZipPacket();
packet.write(x);
packet.write(s);
packet.write(d);

See Also

$(TCPSOCKET_LINK), $(UDPSOCKET_LINK)

Meta