123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- #define ENET_IMPLEMENTATION
- #include "common/enet/enet.h"
- #include "common/network/Packet.h"
- Packet::Packet(ENetPacket* packet) : packet(packet), readIndex(0), error(false) {
- }
- Packet::~Packet() {
- enet_packet_destroy(packet);
- }
- bool Packet::hasData() const {
- return readIndex < packet->dataLength;
- }
- bool Packet::hasError() const {
- return error;
- }
- void Packet::read(void* buffer, uint length) {
- if(readIndex + length > packet->dataLength) {
- error = true;
- return;
- }
- memcpy(buffer, packet->data + readIndex, length);
- readIndex += length;
- }
- u8 Packet::readU8() {
- u8 uc;
- read(&uc, sizeof (u8));
- return uc;
- }
- u16 Packet::readU16() {
- u16 us;
- read(&us, sizeof (u16));
- us = ntohs(us);
- return us;
- }
- u32 Packet::readU32() {
- u32 ui;
- read(&ui, sizeof (u32));
- ui = ntohl(ui);
- return ui;
- }
|