diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index a5574e106..d0212861a 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -148,8 +148,8 @@ add_component_dir (version add_component_dir (openmw-mp Log PacketsController - Packets/PlayerPacket Packets/PacketBaseInfo Packets/PacketPosition Packets/PacketEquipment Packets/PacketAttack - Packets/PacketDynamicStats Packets/PacketCell Packets/PacketDrawState Packets/PacketChatMessage + Packets/BasePacket Packets/PlayerPacket Packets/PacketBaseInfo Packets/PacketPosition Packets/PacketEquipment + Packets/PacketAttack Packets/PacketDynamicStats Packets/PacketCell Packets/PacketDrawState Packets/PacketChatMessage Packets/PacketCharGen Packets/PacketAttribute Packets/PacketSkill Packets/PacketLevel Packets/PacketHandshake Packets/PacketGUIBoxes Packets/PacketClass Packets/PacketTime) diff --git a/components/openmw-mp/Packets/BasePacket.cpp b/components/openmw-mp/Packets/BasePacket.cpp new file mode 100644 index 000000000..408b95c7e --- /dev/null +++ b/components/openmw-mp/Packets/BasePacket.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include "BasePacket.hpp" + +using namespace mwmp; + +BasePacket::BasePacket(RakNet::RakPeerInterface *peer) +{ + packetID = 0; + priority = HIGH_PRIORITY; + reliability = RELIABLE_ORDERED; + this->peer = peer; +} + +BasePacket::~BasePacket() +{ + +} + +void BasePacket::SetReadStream(RakNet::BitStream *bitStream) +{ + bsRead = bitStream; +} + +void BasePacket::SetSendStream(RakNet::BitStream *bitStream) +{ + bsSend = bitStream; +} + +void BasePacket::SetStreams(RakNet::BitStream *inStream, RakNet::BitStream *outStream) +{ + if (inStream != 0) + bsRead = inStream; + if (outStream != 0) + bsSend = outStream; +} \ No newline at end of file diff --git a/components/openmw-mp/Packets/BasePacket.hpp b/components/openmw-mp/Packets/BasePacket.hpp new file mode 100644 index 000000000..88b963734 --- /dev/null +++ b/components/openmw-mp/Packets/BasePacket.hpp @@ -0,0 +1,93 @@ +#ifndef OPENMW_BASEPACKET_HPP +#define OPENMW_BASEPACKET_HPP + +#include +#include +#include +#include +#include + + +namespace mwmp +{ + class BasePacket + { + public: + BasePacket(RakNet::RakPeerInterface *peer); + + ~BasePacket(); + + void SetReadStream(RakNet::BitStream *bitStream); + void SetSendStream(RakNet::BitStream *bitStream); + void SetStreams(RakNet::BitStream *inStream, RakNet::BitStream *outStream); + + static size_t headerSize() + { + return (size_t)(1 + RakNet::RakNetGUID::size()); // packetID + RakNetGUID (uint64_t) + } + + unsigned char GetPacketID() + { + return packetID; + } + + protected: + template + void RW(templateType &data, unsigned int size, bool write) + { + if (write) + bs->Write(data, size); + else + bs->Read(data, size); + } + + template + void RW(templateType &data, bool write) + { + if (write) + bs->Write(data); + else + bs->Read(data); + } + + void RW(bool &data, bool write) + { + char _data; + if (write) + { + _data = data; + bs->Write(_data); + } + else + { + bs->Read(_data); + data = _data; + } + } + + + void RW(std::string &str, bool write) + { + if (write) + { + RakNet::RakString rstr(str.c_str()); + bs->Write(rstr); + } + else + { + RakNet::RakString rstr; + bs->Read(rstr); + str = rstr.C_String(); + } + } + + protected: + unsigned char packetID; + PacketReliability reliability; + PacketPriority priority; + RakNet::BitStream *bsRead, *bsSend, *bs; + RakNet::RakPeerInterface *peer; + }; +} + +#endif //OPENMW_BASEPACKET_HPP diff --git a/components/openmw-mp/Packets/PlayerPacket.cpp b/components/openmw-mp/Packets/PlayerPacket.cpp index 08ef332af..823be9ab7 100644 --- a/components/openmw-mp/Packets/PlayerPacket.cpp +++ b/components/openmw-mp/Packets/PlayerPacket.cpp @@ -17,7 +17,7 @@ void PlayerPacket::Packet(RakNet::BitStream *bs, BasePlayer *player, bool send) } } -PlayerPacket::PlayerPacket(RakNet::RakPeerInterface *peer) +PlayerPacket::PlayerPacket(RakNet::RakPeerInterface *peer) : BasePacket(peer) { packetID = 0; priority = HIGH_PRIORITY; @@ -49,16 +49,6 @@ void PlayerPacket::Read(BasePlayer *player) Packet(bsRead, player, false); } -void PlayerPacket::SetReadStream(RakNet::BitStream *bitStream) -{ - bsRead = bitStream; -} - -void PlayerPacket::SetSendStream(RakNet::BitStream *bitStream) -{ - bsSend = bitStream; -} - void PlayerPacket::RequestData(RakNet::RakNetGUID player) { bsSend->ResetWritePointer(); @@ -66,11 +56,3 @@ void PlayerPacket::RequestData(RakNet::RakNetGUID player) bsSend->Write(player); peer->Send(bsSend, HIGH_PRIORITY, RELIABLE_ORDERED, 0, player, false); } - -void PlayerPacket::SetStreams(RakNet::BitStream *inStream, RakNet::BitStream *outStream) -{ - if (inStream != 0) - bsRead = inStream; - if (outStream != 0) - bsSend = outStream; -} \ No newline at end of file diff --git a/components/openmw-mp/Packets/PlayerPacket.hpp b/components/openmw-mp/Packets/PlayerPacket.hpp index fe45f1e23..9281baf87 100644 --- a/components/openmw-mp/Packets/PlayerPacket.hpp +++ b/components/openmw-mp/Packets/PlayerPacket.hpp @@ -7,10 +7,12 @@ #include #include +#include "BasePacket.hpp" + namespace mwmp { - class PlayerPacket + class PlayerPacket : public BasePacket { public: PlayerPacket(RakNet::RakPeerInterface *peer); @@ -25,10 +27,6 @@ namespace mwmp virtual void RequestData(RakNet::RakNetGUID player); - void SetReadStream(RakNet::BitStream *bitStream); - void SetSendStream(RakNet::BitStream *bitStream); - void SetStreams(RakNet::BitStream *inStream, RakNet::BitStream *outStream); - static size_t headerSize() { return (size_t)(1 + RakNet::RakNetGUID::size()); // packetID + RakNetGUID (uint64_t) @@ -39,65 +37,8 @@ namespace mwmp return packetID; } - protected: - template - void RW(templateType &data, unsigned int size, bool write) - { - if (write) - bs->Write(data, size); - else - bs->Read(data, size); - } - - template - void RW(templateType &data, bool write) - { - if (write) - bs->Write(data); - else - bs->Read(data); - } - - void RW(bool &data, bool write) - { - char _data; - if (write) - { - _data = data; - bs->Write(_data); - } - else - { - bs->Read(_data); - data = _data; - } - } - - - void RW(std::string &str, bool write) - { - if (write) - { - RakNet::RakString rstr(str.c_str()); - bs->Write(rstr); - } - else - { - RakNet::RakString rstr; - bs->Read(rstr); - str = rstr.C_String(); - } - } - protected: BasePlayer *player; - unsigned char packetID; - PacketReliability reliability; - PacketPriority priority; - - private: - RakNet::BitStream *bsRead, *bsSend, *bs; - RakNet::RakPeerInterface *peer; }; } diff --git a/components/openmw-mp/PacketsController.cpp b/components/openmw-mp/PacketsController.cpp index e27f33c4d..19f82f938 100644 --- a/components/openmw-mp/PacketsController.cpp +++ b/components/openmw-mp/PacketsController.cpp @@ -9,7 +9,7 @@ #include "Packets/PacketClass.hpp" #include "Packets/PacketPosition.hpp" #include "Packets/PacketBaseInfo.hpp" -#include "components/openmw-mp/Packets/PacketEquipment.hpp" +#include "Packets/PacketEquipment.hpp" #include "Packets/PacketAttack.hpp" #include "Packets/PacketDynamicStats.hpp" #include "Packets/PacketResurrect.hpp"