[General] Move AddProcessor to BasePacketProcessor

0.6.1
Koncord 8 years ago
parent d9e98ec771
commit 0cf09adb44

@ -3,24 +3,14 @@
using namespace mwmp; using namespace mwmp;
ActorProcessor::processors_t ActorProcessor::processors; template<class T>
typename BasePacketProcessor<T>::processors_t BasePacketProcessor<T>::processors;
void ActorProcessor::Do(ActorPacket &packet, Player &player, BaseActorList &actorList) void ActorProcessor::Do(ActorPacket &packet, Player &player, BaseActorList &actorList)
{ {
packet.Send(true); packet.Send(true);
} }
void ActorProcessor::AddProcessor(mwmp::ActorProcessor *processor) noexcept
{
for (auto &p : processors)
{
if (processor->packetID == p.first)
throw std::logic_error("processor " + p.second->strPacketID + " already registered. Check " +
processor->className + " and " + p.second->className);
}
processors.insert(processors_t::value_type(processor->GetPacketID(), processor));
}
bool ActorProcessor::Process(RakNet::Packet &packet, BaseActorList &actorList) noexcept bool ActorProcessor::Process(RakNet::Packet &packet, BaseActorList &actorList) noexcept
{ {
// Clear our BaseActorList before loading new data in it // Clear our BaseActorList before loading new data in it

@ -6,25 +6,18 @@
#include <components/openmw-mp/Packets/BasePacket.hpp> #include <components/openmw-mp/Packets/BasePacket.hpp>
#include <components/openmw-mp/Packets/Actor/ActorPacket.hpp> #include <components/openmw-mp/Packets/Actor/ActorPacket.hpp>
#include <components/openmw-mp/NetworkMessages.hpp> #include <components/openmw-mp/NetworkMessages.hpp>
#include <unordered_map>
#include <memory>
#include "Script/Script.hpp" #include "Script/Script.hpp"
#include "Player.hpp" #include "Player.hpp"
namespace mwmp namespace mwmp
{ {
class ActorProcessor : public BasePacketProcessor class ActorProcessor : public BasePacketProcessor<ActorProcessor>
{ {
public: public:
virtual void Do(ActorPacket &packet, Player &player, BaseActorList &actorList); virtual void Do(ActorPacket &packet, Player &player, BaseActorList &actorList);
static bool Process(RakNet::Packet &packet, BaseActorList &actorList) noexcept; static bool Process(RakNet::Packet &packet, BaseActorList &actorList) noexcept;
static void AddProcessor(ActorProcessor *processor) noexcept;
typedef std::unordered_map<unsigned char, std::unique_ptr<ActorProcessor> > processors_t;
private:
static processors_t processors;
}; };
} }

@ -4,22 +4,11 @@
#include "PlayerProcessor.hpp" #include "PlayerProcessor.hpp"
#include "Networking.hpp" #include "Networking.hpp"
//#include <boost/foreach.hpp>
using namespace mwmp; using namespace mwmp;
PlayerProcessor::processors_t PlayerProcessor::processors; template<class T>
typename BasePacketProcessor<T>::processors_t BasePacketProcessor<T>::processors;
void PlayerProcessor::AddProcessor(PlayerProcessor *processor) noexcept
{
for (auto &p : processors)
{
if (processor->packetID == p.first)
throw std::logic_error("processor " + p.second->strPacketID + " already registered. Check " +
processor->className + " and " + p.second->className);
}
processors.insert(processors_t::value_type(processor->GetPacketID(), processor));
}
bool PlayerProcessor::Process(RakNet::Packet &packet) noexcept bool PlayerProcessor::Process(RakNet::Packet &packet) noexcept
{ {

@ -8,24 +8,17 @@
#include <components/openmw-mp/Base/BasePacketProcessor.hpp> #include <components/openmw-mp/Base/BasePacketProcessor.hpp>
#include <components/openmw-mp/Packets/BasePacket.hpp> #include <components/openmw-mp/Packets/BasePacket.hpp>
#include <components/openmw-mp/NetworkMessages.hpp> #include <components/openmw-mp/NetworkMessages.hpp>
#include <unordered_map>
#include <memory>
#include "Player.hpp" #include "Player.hpp"
namespace mwmp namespace mwmp
{ {
class PlayerProcessor : public BasePacketProcessor class PlayerProcessor : public BasePacketProcessor<PlayerProcessor>
{ {
public: public:
virtual void Do(PlayerPacket &packet, Player &player) = 0; virtual void Do(PlayerPacket &packet, Player &player) = 0;
static bool Process(RakNet::Packet &packet) noexcept; static bool Process(RakNet::Packet &packet) noexcept;
static void AddProcessor(PlayerProcessor *processor) noexcept;
typedef std::unordered_map<unsigned char, std::unique_ptr<PlayerProcessor> > processors_t;
private:
static processors_t processors;
}; };
} }

@ -7,24 +7,14 @@
using namespace mwmp; using namespace mwmp;
WorldProcessor::processors_t WorldProcessor::processors; template<class T>
typename BasePacketProcessor<T>::processors_t BasePacketProcessor<T>::processors;
void WorldProcessor::Do(WorldPacket &packet, Player &player, BaseEvent &event) void WorldProcessor::Do(WorldPacket &packet, Player &player, BaseEvent &event)
{ {
packet.Send(true); packet.Send(true);
} }
void WorldProcessor::AddProcessor(mwmp::WorldProcessor *processor) noexcept
{
for (auto &p : processors)
{
if (processor->packetID == p.first)
throw std::logic_error("processor " + p.second->strPacketID + " already registered. Check " +
processor->className + " and " + p.second->className);
}
processors.insert(processors_t::value_type(processor->GetPacketID(), processor));
}
bool WorldProcessor::Process(RakNet::Packet &packet, BaseEvent &event) noexcept bool WorldProcessor::Process(RakNet::Packet &packet, BaseEvent &event) noexcept
{ {
// Clear our BaseEvent before loading new data in it // Clear our BaseEvent before loading new data in it

@ -10,25 +10,18 @@
#include <components/openmw-mp/Packets/BasePacket.hpp> #include <components/openmw-mp/Packets/BasePacket.hpp>
#include <components/openmw-mp/Packets/World/WorldPacket.hpp> #include <components/openmw-mp/Packets/World/WorldPacket.hpp>
#include <components/openmw-mp/NetworkMessages.hpp> #include <components/openmw-mp/NetworkMessages.hpp>
#include <unordered_map>
#include <memory>
#include "Script/Script.hpp" #include "Script/Script.hpp"
#include "Player.hpp" #include "Player.hpp"
namespace mwmp namespace mwmp
{ {
class WorldProcessor : public BasePacketProcessor class WorldProcessor : public BasePacketProcessor<WorldProcessor>
{ {
public: public:
virtual void Do(WorldPacket &packet, Player &player, BaseEvent &event); virtual void Do(WorldPacket &packet, Player &player, BaseEvent &event);
static bool Process(RakNet::Packet &packet, BaseEvent &event) noexcept; static bool Process(RakNet::Packet &packet, BaseEvent &event) noexcept;
static void AddProcessor(WorldProcessor *processor) noexcept;
typedef std::unordered_map<unsigned char, std::unique_ptr<WorldProcessor> > processors_t;
private:
static processors_t processors;
}; };
} }

@ -8,7 +8,8 @@
using namespace mwmp; using namespace mwmp;
ActorProcessor::processors_t ActorProcessor::processors; template<class T>
typename BasePacketProcessor<T>::processors_t BasePacketProcessor<T>::processors;
bool ActorProcessor::Process(RakNet::Packet &packet, ActorList &actorList) bool ActorProcessor::Process(RakNet::Packet &packet, ActorList &actorList)
{ {
@ -43,15 +44,4 @@ bool ActorProcessor::Process(RakNet::Packet &packet, ActorList &actorList)
} }
} }
return false; return false;
} }
void ActorProcessor::AddProcessor(mwmp::ActorProcessor *processor)
{
for (auto &p : processors)
{
if (processor->packetID == p.first)
throw std::logic_error("processor " + p.second->strPacketID + " already registered. Check " +
processor->className + " and " + p.second->className);
}
processors.insert(processors_t::value_type(processor->GetPacketID(), processor));
}

@ -8,25 +8,18 @@
#include <components/openmw-mp/Log.hpp> #include <components/openmw-mp/Log.hpp>
#include <components/openmw-mp/NetworkMessages.hpp> #include <components/openmw-mp/NetworkMessages.hpp>
#include <components/openmw-mp/Packets/Actor/ActorPacket.hpp> #include <components/openmw-mp/Packets/Actor/ActorPacket.hpp>
#include <unordered_map>
#include "../WorldEvent.hpp" #include "../WorldEvent.hpp"
#include "../ActorList.hpp" #include "../ActorList.hpp"
#include "BaseClientPacketProcessor.hpp" #include "BaseClientPacketProcessor.hpp"
namespace mwmp namespace mwmp
{ {
class ActorProcessor : public BaseClientPacketProcessor class ActorProcessor : public BasePacketProcessor<ActorProcessor>, public BaseClientPacketProcessor
{ {
public: public:
virtual void Do(ActorPacket &packet, ActorList &actorList) = 0; virtual void Do(ActorPacket &packet, ActorList &actorList) = 0;
static bool Process(RakNet::Packet &packet, ActorList &actorList); static bool Process(RakNet::Packet &packet, ActorList &actorList);
static void AddProcessor(ActorProcessor *processor);
typedef std::unordered_map<unsigned char, std::unique_ptr<ActorProcessor> > processors_t;
private:
static processors_t processors;
}; };
} }

@ -11,7 +11,7 @@
namespace mwmp namespace mwmp
{ {
class BaseClientPacketProcessor : public BasePacketProcessor class BaseClientPacketProcessor
{ {
public: public:
static void SetServerAddr(RakNet::SystemAddress addr) static void SetServerAddr(RakNet::SystemAddress addr)

@ -8,18 +8,8 @@
using namespace mwmp; using namespace mwmp;
PlayerProcessor::processors_t PlayerProcessor::processors; template<class T>
typename BasePacketProcessor<T>::processors_t BasePacketProcessor<T>::processors;
void PlayerProcessor::AddProcessor(PlayerProcessor *processor)
{
for (auto &p : processors)
{
if (processor->packetID == p.first)
throw std::logic_error("processor " + p.second->strPacketID + " already registered. Check " +
processor->className + " and " + p.second->className);
}
processors.insert(processors_t::value_type(processor->GetPacketID(), processor));
}
bool PlayerProcessor::Process(RakNet::Packet &packet) bool PlayerProcessor::Process(RakNet::Packet &packet)
{ {

@ -8,7 +8,6 @@
#include <components/openmw-mp/Log.hpp> #include <components/openmw-mp/Log.hpp>
#include <components/openmw-mp/NetworkMessages.hpp> #include <components/openmw-mp/NetworkMessages.hpp>
#include <components/openmw-mp/Packets/Player/PlayerPacket.hpp> #include <components/openmw-mp/Packets/Player/PlayerPacket.hpp>
#include <unordered_map>
#include "../LocalPlayer.hpp" #include "../LocalPlayer.hpp"
#include "../DedicatedPlayer.hpp" #include "../DedicatedPlayer.hpp"
#include "../PlayerList.hpp" #include "../PlayerList.hpp"
@ -16,17 +15,12 @@
namespace mwmp namespace mwmp
{ {
class PlayerProcessor : public BaseClientPacketProcessor class PlayerProcessor : public BasePacketProcessor<PlayerProcessor>, public BaseClientPacketProcessor
{ {
public: public:
virtual void Do(PlayerPacket &packet, BasePlayer *player) = 0; virtual void Do(PlayerPacket &packet, BasePlayer *player) = 0;
static bool Process(RakNet::Packet &packet); static bool Process(RakNet::Packet &packet);
static void AddProcessor(PlayerProcessor *processor);
typedef std::unordered_map<unsigned char, std::unique_ptr<PlayerProcessor> > processors_t;
private:
static processors_t processors;
}; };
} }

@ -8,7 +8,8 @@
using namespace mwmp; using namespace mwmp;
WorldProcessor::processors_t WorldProcessor::processors; template<class T>
typename BasePacketProcessor<T>::processors_t BasePacketProcessor<T>::processors;
bool WorldProcessor::Process(RakNet::Packet &packet, WorldEvent &event) bool WorldProcessor::Process(RakNet::Packet &packet, WorldEvent &event)
{ {
@ -42,14 +43,3 @@ bool WorldProcessor::Process(RakNet::Packet &packet, WorldEvent &event)
} }
return false; return false;
} }
void WorldProcessor::AddProcessor(mwmp::WorldProcessor *processor)
{
for (auto &p : processors)
{
if (processor->packetID == p.first)
throw std::logic_error("processor " + p.second->strPacketID + " already registered. Check " +
processor->className + " and " + p.second->className);
}
processors.insert(processors_t::value_type(processor->GetPacketID(), processor));
}

@ -8,7 +8,6 @@
#include <components/openmw-mp/Log.hpp> #include <components/openmw-mp/Log.hpp>
#include <components/openmw-mp/NetworkMessages.hpp> #include <components/openmw-mp/NetworkMessages.hpp>
#include <components/openmw-mp/Packets/World/WorldPacket.hpp> #include <components/openmw-mp/Packets/World/WorldPacket.hpp>
#include <unordered_map>
#include "../WorldEvent.hpp" #include "../WorldEvent.hpp"
#include "../LocalPlayer.hpp" #include "../LocalPlayer.hpp"
#include "../DedicatedPlayer.hpp" #include "../DedicatedPlayer.hpp"
@ -16,18 +15,12 @@
namespace mwmp namespace mwmp
{ {
class WorldProcessor : public BaseClientPacketProcessor class WorldProcessor : public BasePacketProcessor<WorldProcessor>, public BaseClientPacketProcessor
{ {
public: public:
virtual void Do(WorldPacket &packet, WorldEvent &event) = 0; virtual void Do(WorldPacket &packet, WorldEvent &event) = 0;
static bool Process(RakNet::Packet &packet, WorldEvent &event); static bool Process(RakNet::Packet &packet, WorldEvent &event);
static void AddProcessor(WorldProcessor *processor);
typedef std::unordered_map<unsigned char, std::unique_ptr<WorldProcessor> > processors_t;
private:
static processors_t processors;
}; };
} }

@ -6,12 +6,16 @@
#define OPENMW_BASEPACKETPROCESSOR_HPP #define OPENMW_BASEPACKETPROCESSOR_HPP
#include <string> #include <string>
#include <memory>
#include <unordered_map>
#define BPP_INIT(packet_id) packetID = packet_id; strPacketID = #packet_id; className = typeid(this).name(); avoidReading = false; #define BPP_INIT(packet_id) packetID = packet_id; strPacketID = #packet_id; className = typeid(this).name(); avoidReading = false;
template<typename Proccessor>
class BasePacketProcessor class BasePacketProcessor
{ {
public: public:
typedef std::unordered_map<unsigned char, std::unique_ptr<Proccessor>> processors_t;
unsigned char GetPacketID() unsigned char GetPacketID()
{ {
return packetID; return packetID;
@ -26,12 +30,22 @@ public:
return className; return className;
} }
static void AddProcessor(Proccessor *processor)
{
for (auto &p : processors)
{
if (processor->packetID == p.first)
throw std::logic_error("processor " + p.second->strPacketID + " already registered. Check " +
processor->className + " and " + p.second->className);
}
processors.insert(typename processors_t::value_type(processor->GetPacketID(), processor));
}
protected: protected:
unsigned char packetID; unsigned char packetID;
std::string strPacketID; std::string strPacketID;
std::string className; std::string className;
bool avoidReading; bool avoidReading;
static processors_t processors;
}; };
#endif //OPENMW_BASEPACKETPROCESSOR_HPP #endif //OPENMW_BASEPACKETPROCESSOR_HPP

Loading…
Cancel
Save