Networking First Steps
When tasked with making a game that sends data between computers, the starting point was to create a class for data packets. Data is sent one byte at a time, which means that to send data, the packet needs to be able to take any type and convert it into an array of 1-byte data. It also needs to be able to convert an array of one byte data into a desired type.
class Packet
{
public:
Packet() = default;
~Packet() = default;
const size_t getPacketSize();
const bool endOfPacket();
const bool allDataReceived(size_t _size);
/*
These three methods are used to determine whether
all the data which needs to be transferred in this
has been received.
*/
void clear();
/*
This is the method for removing all data from a packet
this is for when packets need to be re-used.
*/
template <typename T>
Packet& operator << (const T& data)
/*
This operator is used for inserting data of an unknown type into a packet
*/
template <typename T>
Packet& operator >> (T& data)
/*
This operator is used for converting data from a packet into an unknown type
*/
private:
size_t read_pos;
std::vector<enet_uint8> packet_data;