diff options
Diffstat (limited to 'src/Net/Packet.h')
-rw-r--r-- | src/Net/Packet.h | 40 |
1 files changed, 23 insertions, 17 deletions
diff --git a/src/Net/Packet.h b/src/Net/Packet.h index dc62cb7..567c9c3 100644 --- a/src/Net/Packet.h +++ b/src/Net/Packet.h @@ -20,30 +20,36 @@ #ifndef MAD_NET_PACKET_H_ #define MAD_NET_PACKET_H_ +#include "export.h" + #include <cstdlib> #include <cstring> -#include <netinet/in.h> -#include <stdint.h> +#include <boost/cstdint.hpp> + +#ifdef _WIN32 +# include <winsock2.h> +#else +# include <netinet/in.h> +#endif namespace Mad { namespace Net { -class Packet { +class MAD_NET_EXPORT Packet { public: - struct Data { - uint16_t requestId; - uint16_t length; - uint8_t data[0]; + struct Header { + boost::uint16_t requestId; + boost::uint16_t length; }; protected: - Data *rawData; + Header *rawData; public: - Packet(uint16_t requestId, const void *data = 0, uint16_t length = 0); + Packet(boost::uint16_t requestId, const void *data = 0, boost::uint16_t length = 0); Packet(const Packet &p) { - rawData = (Data*)std::malloc(p.getRawDataLength()); + rawData = reinterpret_cast<Header*>(std::malloc(p.getRawDataLength())); std::memcpy(rawData, p.rawData, p.getRawDataLength()); } @@ -53,24 +59,24 @@ class Packet { Packet& operator=(const Packet &p); - uint16_t getRequestId() const { + boost::uint16_t getRequestId() const { return ntohs(rawData->requestId); } - uint16_t getLength() const { + boost::uint16_t getLength() const { return ntohs(rawData->length); } - const uint8_t* getData() const { - return rawData->data; + const boost::uint8_t* getData() const { + return reinterpret_cast<boost::uint8_t*>(rawData)+sizeof(Header); } - const Data* getRawData() const { - return rawData; + const boost::uint8_t* getRawData() const { + return reinterpret_cast<boost::uint8_t*>(rawData); } unsigned long getRawDataLength() const { - return sizeof(Data) + ntohs(rawData->length); + return sizeof(Header) + ntohs(rawData->length); } }; |