/* * Packet.h * * Copyright (C) 2008 Matthias Schiffer * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by the * Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along * with this program. If not, see . */ #ifndef MAD_NET_PACKET_H_ #define MAD_NET_PACKET_H_ #include "export.h" #include #include #include #ifdef _WIN32 # include #else # include #endif namespace Mad { namespace Net { class MAD_NET_EXPORT Packet { public: struct Header { boost::uint32_t length; boost::uint16_t requestId; boost::uint16_t reserved; }; protected: Header *rawData; public: Packet(boost::uint16_t requestId, const void *data = 0, boost::uint32_t length = 0); Packet(const Packet &p) { rawData = reinterpret_cast(std::malloc(p.getRawDataLength())); std::memcpy(rawData, p.rawData, p.getRawDataLength()); } virtual ~Packet() { std::free(rawData); } Packet& operator=(const Packet &p); boost::uint16_t getRequestId() const { return ntohs(rawData->requestId); } boost::uint32_t getLength() const { return ntohl(rawData->length); } const boost::uint8_t* getData() const { return reinterpret_cast(rawData)+sizeof(Header); } const boost::uint8_t* getRawData() const { return reinterpret_cast(rawData); } unsigned long getRawDataLength() const { return sizeof(Header) + getLength(); } }; } } #endif /*MAD_NET_PACKET_H_*/