diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index f9abc1f03..944251845 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -162,7 +162,7 @@ add_component_dir (openmw-mp/Base ) add_component_dir (openmw-mp/Controllers - PlayerPacketController ActorPacketController WorldPacketController + PlayerPacketController ActorPacketController WorldPacketController PacketController ) add_component_dir(openmw-mp/Master diff --git a/components/openmw-mp/Controllers/ActorPacketController.cpp b/components/openmw-mp/Controllers/ActorPacketController.cpp index 29275b75f..6b6b5f456 100644 --- a/components/openmw-mp/Controllers/ActorPacketController.cpp +++ b/components/openmw-mp/Controllers/ActorPacketController.cpp @@ -16,15 +16,6 @@ #include "ActorPacketController.hpp" -template -inline void AddPacket(mwmp::ActorPacketController::packets_t *packets, RakNet::RakPeerInterface *peer) -{ - T *packet = new T(peer); - typedef mwmp::ActorPacketController::packets_t::value_type value_t; - unsigned char packetId = packet->GetPacketID(); - packets->insert(value_t(packetId, value_t::second_type(std::move(packet)))); -} - mwmp::ActorPacketController::ActorPacketController(RakNet::RakPeerInterface *peer) { AddPacket(&packets, peer); @@ -42,25 +33,3 @@ mwmp::ActorPacketController::ActorPacketController(RakNet::RakPeerInterface *pee AddPacket(&packets, peer); AddPacket(&packets, peer); } - - -mwmp::ActorPacket *mwmp::ActorPacketController::GetPacket(RakNet::MessageID id) -{ - return packets[(RakNet::MessageID)id].get(); -} - -void mwmp::ActorPacketController::SetStream(RakNet::BitStream *inStream, RakNet::BitStream *outStream) -{ - for (const auto &packet : packets) - packet.second->SetStreams(inStream, outStream); -} - -bool mwmp::ActorPacketController::ContainsPacket(RakNet::MessageID id) -{ - for (const auto &packet : packets) - { - if (packet.first == id) - return true; - } - return false; -} diff --git a/components/openmw-mp/Controllers/ActorPacketController.hpp b/components/openmw-mp/Controllers/ActorPacketController.hpp index 47cfff0a7..8fb7db11a 100644 --- a/components/openmw-mp/Controllers/ActorPacketController.hpp +++ b/components/openmw-mp/Controllers/ActorPacketController.hpp @@ -2,25 +2,15 @@ #define OPENMW_ACTORPACKETCONTROLLER_HPP -#include #include "../Packets/Actor/ActorPacket.hpp" -#include -#include +#include "BasePacketController.hpp" namespace mwmp { - class ActorPacketController + class ActorPacketController: public BasePacketController { public: ActorPacketController(RakNet::RakPeerInterface *peer); - ActorPacket *GetPacket(RakNet::MessageID id); - void SetStream(RakNet::BitStream *inStream, RakNet::BitStream *outStream); - - bool ContainsPacket(RakNet::MessageID id); - - typedef std::unordered_map > packets_t; - private: - packets_t packets; }; } diff --git a/components/openmw-mp/Controllers/BasePacketController.hpp b/components/openmw-mp/Controllers/BasePacketController.hpp new file mode 100644 index 000000000..a438dfbe1 --- /dev/null +++ b/components/openmw-mp/Controllers/BasePacketController.hpp @@ -0,0 +1,48 @@ +// +// Created by koncord on 26.02.18. +// + +#pragma once + + +#include +#include +#include + +namespace mwmp +{ + template + class BasePacketController + { + public: + bool ContainsPacket(RakNet::MessageID id) const + { + return packets.count(id) == 1; + } + + PacketT *GetPacket(RakNet::MessageID id) + { + return packets.at(id).get(); //operator[] calls rehash() if key not found, that not applicable here. + } + + void SetStream(RakNet::BitStream *inStream, RakNet::BitStream *outStream) + { + for (const auto &packet : packets) + packet.second->SetStreams(inStream, outStream); + } + + typedef std::unordered_map > packets_t; + + protected: + template + inline void static AddPacket(packets_t *packets, RakNet::RakPeerInterface *peer) + { + auto packet = new T(peer); + RakNet::MessageID packetId = packet->GetPacketID(); + packets->emplace(packetId, packet); + } + + protected: + packets_t packets; + }; +} diff --git a/components/openmw-mp/Controllers/PlayerPacketController.cpp b/components/openmw-mp/Controllers/PlayerPacketController.cpp index 654f7d75b..336cca88c 100644 --- a/components/openmw-mp/Controllers/PlayerPacketController.cpp +++ b/components/openmw-mp/Controllers/PlayerPacketController.cpp @@ -49,15 +49,6 @@ #include "PlayerPacketController.hpp" -template -inline void AddPacket(mwmp::PlayerPacketController::packets_t *packets, RakNet::RakPeerInterface *peer) -{ - T *packet = new T(peer); - typedef mwmp::PlayerPacketController::packets_t::value_type value_t; - unsigned char packetId = packet->GetPacketID(); - packets->insert(value_t(packetId, value_t::second_type(std::move(packet)))); -} - mwmp::PlayerPacketController::PlayerPacketController(RakNet::RakPeerInterface *peer) { AddPacket(&packets, peer); @@ -110,25 +101,3 @@ mwmp::PlayerPacketController::PlayerPacketController(RakNet::RakPeerInterface *p AddPacket(&packets, peer); AddPacket(&packets, peer); } - - -mwmp::PlayerPacket *mwmp::PlayerPacketController::GetPacket(RakNet::MessageID id) -{ - return packets[(RakNet::MessageID)id].get(); -} - -void mwmp::PlayerPacketController::SetStream(RakNet::BitStream *inStream, RakNet::BitStream *outStream) -{ - for (const auto &packet : packets) - packet.second->SetStreams(inStream, outStream); -} - -bool mwmp::PlayerPacketController::ContainsPacket(RakNet::MessageID id) -{ - for (const auto &packet : packets) - { - if (packet.first == id) - return true; - } - return false; -} diff --git a/components/openmw-mp/Controllers/PlayerPacketController.hpp b/components/openmw-mp/Controllers/PlayerPacketController.hpp index d143f7df3..afc44c22f 100644 --- a/components/openmw-mp/Controllers/PlayerPacketController.hpp +++ b/components/openmw-mp/Controllers/PlayerPacketController.hpp @@ -2,25 +2,15 @@ #define OPENMW_PLAYERPACKETCONTROLLER_HPP -#include #include "../Packets/Player/PlayerPacket.hpp" -#include -#include +#include "BasePacketController.hpp" namespace mwmp { - class PlayerPacketController + class PlayerPacketController: public BasePacketController { public: PlayerPacketController(RakNet::RakPeerInterface *peer); - PlayerPacket *GetPacket(RakNet::MessageID id); - void SetStream(RakNet::BitStream *inStream, RakNet::BitStream *outStream); - - bool ContainsPacket(RakNet::MessageID id); - - typedef std::unordered_map > packets_t; - private: - packets_t packets; }; } diff --git a/components/openmw-mp/Controllers/WorldPacketController.cpp b/components/openmw-mp/Controllers/WorldPacketController.cpp index 0b07409ad..0cbea8a9e 100644 --- a/components/openmw-mp/Controllers/WorldPacketController.cpp +++ b/components/openmw-mp/Controllers/WorldPacketController.cpp @@ -28,15 +28,6 @@ #include "WorldPacketController.hpp" -template -inline void AddPacket(mwmp::WorldPacketController::packets_t *packets, RakNet::RakPeerInterface *peer) -{ - T *packet = new T(peer); - typedef mwmp::WorldPacketController::packets_t::value_type value_t; - unsigned char packetId = packet->GetPacketID(); - packets->insert(value_t(packetId, value_t::second_type(std::move(packet)))); -} - mwmp::WorldPacketController::WorldPacketController(RakNet::RakPeerInterface *peer) { AddPacket(&packets, peer); @@ -67,25 +58,3 @@ mwmp::WorldPacketController::WorldPacketController(RakNet::RakPeerInterface *pee AddPacket(&packets, peer); AddPacket(&packets, peer); } - - -mwmp::WorldPacket *mwmp::WorldPacketController::GetPacket(RakNet::MessageID id) -{ - return packets[(RakNet::MessageID)id].get(); -} - -void mwmp::WorldPacketController::SetStream(RakNet::BitStream *inStream, RakNet::BitStream *outStream) -{ - for (const auto &packet : packets) - packet.second->SetStreams(inStream, outStream); -} - -bool mwmp::WorldPacketController::ContainsPacket(RakNet::MessageID id) -{ - for (const auto &packet : packets) - { - if (packet.first == id) - return true; - } - return false; -} diff --git a/components/openmw-mp/Controllers/WorldPacketController.hpp b/components/openmw-mp/Controllers/WorldPacketController.hpp index f3fb797aa..46f9c8be4 100644 --- a/components/openmw-mp/Controllers/WorldPacketController.hpp +++ b/components/openmw-mp/Controllers/WorldPacketController.hpp @@ -2,25 +2,15 @@ #define OPENMW_WORLDPACKETCONTROLLER_HPP -#include #include "../Packets/World/WorldPacket.hpp" -#include -#include +#include "BasePacketController.hpp" namespace mwmp { - class WorldPacketController + class WorldPacketController: public BasePacketController { public: WorldPacketController(RakNet::RakPeerInterface *peer); - WorldPacket *GetPacket(RakNet::MessageID id); - void SetStream(RakNet::BitStream *inStream, RakNet::BitStream *outStream); - - bool ContainsPacket(RakNet::MessageID id); - - typedef std::unordered_map > packets_t; - private: - packets_t packets; }; }