2016-10-17 15:47:16 +00:00
|
|
|
#ifndef OPENMW_BASEPACKET_HPP
|
|
|
|
#define OPENMW_BASEPACKET_HPP
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <RakNetTypes.h>
|
|
|
|
#include <BitStream.h>
|
|
|
|
#include <PacketPriority.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace mwmp
|
|
|
|
{
|
|
|
|
class BasePacket
|
|
|
|
{
|
|
|
|
public:
|
2018-07-02 15:19:39 +00:00
|
|
|
explicit BasePacket(RakNet::RakPeerInterface *peer);
|
2016-10-17 15:47:16 +00:00
|
|
|
|
2018-07-02 15:19:39 +00:00
|
|
|
virtual ~BasePacket() = default;
|
2016-10-17 15:47:16 +00:00
|
|
|
|
2017-03-06 09:44:08 +00:00
|
|
|
virtual void Packet(RakNet::BitStream *bs, bool send);
|
2017-07-31 10:57:57 +00:00
|
|
|
virtual uint32_t Send(bool toOtherPlayers = true);
|
|
|
|
virtual uint32_t Send(RakNet::AddressOrGUID destination);
|
2017-03-06 09:44:08 +00:00
|
|
|
virtual void Read();
|
|
|
|
|
|
|
|
void setGUID(RakNet::RakNetGUID guid);
|
|
|
|
RakNet::RakNetGUID getGUID();
|
2017-03-05 08:01:42 +00:00
|
|
|
|
2016-10-17 15:47:16 +00:00
|
|
|
void SetReadStream(RakNet::BitStream *bitStream);
|
|
|
|
void SetSendStream(RakNet::BitStream *bitStream);
|
|
|
|
void SetStreams(RakNet::BitStream *inStream, RakNet::BitStream *outStream);
|
2017-07-31 10:57:57 +00:00
|
|
|
virtual uint32_t RequestData(RakNet::RakNetGUID guid);
|
2016-10-17 15:47:16 +00:00
|
|
|
|
2018-07-02 15:19:39 +00:00
|
|
|
static inline uint32_t headerSize()
|
2016-10-17 15:47:16 +00:00
|
|
|
{
|
2018-07-02 15:19:39 +00:00
|
|
|
return static_cast<uint32_t>(1 + RakNet::RakNetGUID::size()); // packetID + RakNetGUID (uint64_t)
|
2016-10-17 15:47:16 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 15:19:39 +00:00
|
|
|
uint8_t GetPacketID() const
|
2016-10-17 15:47:16 +00:00
|
|
|
{
|
|
|
|
return packetID;
|
|
|
|
}
|
|
|
|
|
2018-07-02 18:06:52 +00:00
|
|
|
bool isPacketValid() const
|
|
|
|
{
|
|
|
|
return packetValid;
|
|
|
|
}
|
|
|
|
|
2016-10-17 15:47:16 +00:00
|
|
|
protected:
|
|
|
|
template<class templateType>
|
2018-07-02 18:28:20 +00:00
|
|
|
bool RW(templateType &data, uint32_t size, bool write)
|
2016-10-17 15:47:16 +00:00
|
|
|
{
|
|
|
|
if (write)
|
|
|
|
bs->Write(data, size);
|
|
|
|
else
|
2018-07-02 18:28:20 +00:00
|
|
|
return bs->Read(data, size);
|
|
|
|
return true;
|
2016-10-17 15:47:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class templateType>
|
2018-07-02 18:28:20 +00:00
|
|
|
bool RW(templateType &data, bool write, bool compress = 0)
|
2016-10-17 15:47:16 +00:00
|
|
|
{
|
|
|
|
if (write)
|
2017-05-28 06:56:51 +00:00
|
|
|
{
|
2017-05-31 05:37:11 +00:00
|
|
|
if (compress)
|
2017-05-28 06:56:51 +00:00
|
|
|
bs->WriteCompressed(data);
|
|
|
|
else
|
|
|
|
bs->Write(data);
|
2018-07-02 18:28:20 +00:00
|
|
|
return true;
|
2017-05-28 06:56:51 +00:00
|
|
|
}
|
2016-10-17 15:47:16 +00:00
|
|
|
else
|
2017-05-28 06:56:51 +00:00
|
|
|
{
|
2017-05-31 05:37:11 +00:00
|
|
|
if (compress)
|
2018-07-02 18:28:20 +00:00
|
|
|
return bs->ReadCompressed(data);
|
2017-05-28 06:56:51 +00:00
|
|
|
else
|
2018-07-02 18:28:20 +00:00
|
|
|
return bs->Read(data);
|
2017-05-28 06:56:51 +00:00
|
|
|
}
|
2016-10-17 15:47:16 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 18:28:20 +00:00
|
|
|
bool RW(bool &data, bool write)
|
2016-10-17 15:47:16 +00:00
|
|
|
{
|
|
|
|
if (write)
|
2017-06-02 13:03:29 +00:00
|
|
|
bs->Write(data);
|
2016-10-17 15:47:16 +00:00
|
|
|
else
|
2018-07-02 18:28:20 +00:00
|
|
|
return bs->Read(data);
|
|
|
|
return true;
|
2016-10-17 15:47:16 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 17:12:59 +00:00
|
|
|
const static uint32_t maxStrSize = 64 * 1024; // 64 KiB
|
|
|
|
|
2018-07-02 18:28:20 +00:00
|
|
|
bool RW(std::string &str, bool write, bool compress = false, std::string::size_type maxSize = maxStrSize)
|
2016-10-17 15:47:16 +00:00
|
|
|
{
|
2018-07-02 18:28:20 +00:00
|
|
|
bool res = true;
|
2016-10-17 15:47:16 +00:00
|
|
|
if (write)
|
|
|
|
{
|
2017-05-31 05:37:11 +00:00
|
|
|
if (compress)
|
2018-07-02 15:19:39 +00:00
|
|
|
RakNet::RakString::SerializeCompressed(str.substr(0, maxSize).c_str(), bs); // todo: remove extra copy of string
|
2017-05-28 06:56:51 +00:00
|
|
|
else
|
2017-09-03 10:53:45 +00:00
|
|
|
{
|
|
|
|
RakNet::RakString rstr;
|
2018-07-02 15:19:39 +00:00
|
|
|
rstr.AppendBytes(str.c_str(), str.size() > maxSize ? maxSize : str.size());
|
2017-05-28 06:56:51 +00:00
|
|
|
bs->Write(rstr);
|
2017-09-03 10:53:45 +00:00
|
|
|
}
|
2016-10-17 15:47:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
RakNet::RakString rstr;
|
2017-05-31 05:37:11 +00:00
|
|
|
if (compress)
|
2018-07-02 18:28:20 +00:00
|
|
|
res = rstr.DeserializeCompressed(bs);
|
2017-05-28 06:56:51 +00:00
|
|
|
else
|
2018-07-02 18:28:20 +00:00
|
|
|
res = bs->Read(rstr);
|
2018-07-02 15:19:39 +00:00
|
|
|
|
2018-07-02 18:28:20 +00:00
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
rstr.Truncate(rstr.GetLength() > maxSize ? maxSize : rstr.GetLength());
|
|
|
|
str = rstr.C_String();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
str = std::string();
|
2016-10-17 15:47:16 +00:00
|
|
|
}
|
2018-07-02 18:28:20 +00:00
|
|
|
return res;
|
2016-10-17 15:47:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2018-07-02 15:19:39 +00:00
|
|
|
uint8_t packetID;
|
2016-10-17 15:47:16 +00:00
|
|
|
PacketReliability reliability;
|
|
|
|
PacketPriority priority;
|
2018-07-02 15:19:39 +00:00
|
|
|
int8_t orderChannel;
|
2016-10-17 15:47:16 +00:00
|
|
|
RakNet::BitStream *bsRead, *bsSend, *bs;
|
|
|
|
RakNet::RakPeerInterface *peer;
|
2017-03-06 09:44:08 +00:00
|
|
|
RakNet::RakNetGUID guid;
|
2018-07-02 18:06:52 +00:00
|
|
|
bool packetValid;
|
2016-10-17 15:47:16 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif //OPENMW_BASEPACKET_HPP
|