IPacket interface defines the essential methods for creating and managing network packets. It serves as a blueprint for packet implementations, facilitating communication between the server and clients in a networked environment.The IPacket interface is fundamental for ensuring standardized communication across the networked game environment, enabling easy creation, validation, and transmission of data packets.
class IPacket {
using Message = std::vector<uint8_t>;
using Arguments = std::vector<uint8_t>;
public:
virtual ~IPacket() = default;
virtual bool isValid() const = 0;
virtual uint8_t getOpcode() const = 0;
virtual Arguments getArguments() const = 0;
virtual Message toMessage() const = 0;
protected:
using Message = std::vector<uint8_t>;
Represents the complete message of the packet, which includes the operation code and any associated arguments.
using Arguments = std::vector<uint8_t>;
Represents the arguments contained within the packet, stored as a vector of bytes.
virtual ~IPacket() = default;
Virtual destructor to ensure proper cleanup of derived classes.
virtual bool isValid() const = 0;
Checks if the packet is valid and ready to be sent.
true if the packet is valid;false otherwise.virtual uint8_t getOpcode() const = 0;
Retrieves the operation code of the packet.
uint8_t representing a valid operation code (from protocol::Operations::*), or1 if the operation code is invalid.virtual Arguments getArguments() const = 0;
Gets the arguments of the packet.
Arguments object (i.e., std::vector<uint8_t>), which is always at least an empty vector.virtual Message toMessage() const = 0;
Translates the packet into a complete message suitable for UDP sockets.
Message object (i.e., std::vector<uint8_t>) containing the operation code concatenated with the arguments.