1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- #ifndef PACKET_H
- #define PACKET_H
- #include "network/ENet.h"
- #include "utils/StringBuffer.h"
- #include "utils/Types.h"
- class InPacket {
- ENetPacket* packet;
- unsigned int index;
- friend class Server;
- InPacket(ENetPacket* packet);
- bool read(void* buffer, unsigned int length);
- public:
- bool readU8(uint8& u);
- bool readU16(uint16& u);
- bool readU32(uint32& u);
- bool readS8(int8& s);
- bool readS16(int16& s);
- bool readS32(int32& s);
- template<int N>
- bool readString(StringBuffer<N>& s) {
- uint16 end;
- if(readU16(end)) {
- return true;
- }
- s.clear();
- for(unsigned int i = 0; i < end; i++) {
- int8 c;
- if(readS8(c)) {
- return true;
- }
- s.append(c);
- }
- return false;
- }
- };
- class OutPacket {
- ENetPacket* packet;
- unsigned int index;
- friend class Client;
- public:
- OutPacket(unsigned int size, int flags);
- ~OutPacket();
- OutPacket(const OutPacket& other);
- OutPacket(OutPacket&& other);
- OutPacket& operator=(OutPacket other);
- void writeU8(uint8 u);
- void writeU16(uint16 u);
- void writeU32(uint32 u);
- void writeS8(int8 s);
- void writeS16(int16 s);
- void writeS32(int32 s);
- template<int N>
- void writeString(const StringBuffer<N>& s) {
- uint16 end = s.getLength() > 65535 ? 65535 : s.getLength();
- writeU16(end);
- for(unsigned int i = 0; i < end; i++) {
- writeS8(s[i]);
- }
- }
- private:
- void write(const void* buffer, unsigned int length);
- };
- #endif
|