123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- #define ENET_IMPLEMENTATION
- #include "common/network/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 < static_cast<int>(packet->dataLength);
- }
- bool Packet::hasError() const {
- return error;
- }
- void Packet::read(void* buffer, int length) {
- if(readIndex + length > static_cast<int>(packet->dataLength)) {
- error = true;
- return;
- }
- memcpy(buffer, packet->data + readIndex, length);
- readIndex += length;
- }
- Packet::int8 Packet::read8() {
- int8 uc;
- read(&uc, sizeof (int8));
- return uc;
- }
- Packet::int16 Packet::read16() {
- int16 us;
- read(&us, sizeof (int16));
- us = ntohs(us);
- return us;
- }
- Packet::int32 Packet::read32() {
- int32 ui;
- read(&ui, sizeof (int32));
- ui = ntohl(ui);
- return ui;
- }
|