From a6111b65995d2835441bdde2b0f718bb28f5431b Mon Sep 17 00:00:00 2001 From: Koncord Date: Sun, 16 Apr 2017 14:58:40 +0800 Subject: [PATCH] [Client] Add PlayerProcessor & WorldProcessor --- apps/openmw/mwmp/LocalPlayer.hpp | 1 + apps/openmw/mwmp/Main.hpp | 5 ++ apps/openmw/mwmp/PlayerProcessor.cpp | 73 ++++++++++++++++++++++++++++ apps/openmw/mwmp/PlayerProcessor.hpp | 58 ++++++++++++++++++++++ apps/openmw/mwmp/WorldProcesssor.cpp | 60 +++++++++++++++++++++++ apps/openmw/mwmp/WorldProcesssor.hpp | 50 +++++++++++++++++++ 6 files changed, 247 insertions(+) create mode 100644 apps/openmw/mwmp/PlayerProcessor.cpp create mode 100644 apps/openmw/mwmp/PlayerProcessor.hpp create mode 100644 apps/openmw/mwmp/WorldProcesssor.cpp create mode 100644 apps/openmw/mwmp/WorldProcesssor.hpp diff --git a/apps/openmw/mwmp/LocalPlayer.hpp b/apps/openmw/mwmp/LocalPlayer.hpp index 0d12df168..53b0a1e86 100644 --- a/apps/openmw/mwmp/LocalPlayer.hpp +++ b/apps/openmw/mwmp/LocalPlayer.hpp @@ -6,6 +6,7 @@ #define OPENMW_LOCALPLAYER_HPP #include +#include "../mwworld/ptr.hpp" #include namespace mwmp diff --git a/apps/openmw/mwmp/Main.hpp b/apps/openmw/mwmp/Main.hpp index 6592ebc28..808d5707a 100644 --- a/apps/openmw/mwmp/Main.hpp +++ b/apps/openmw/mwmp/Main.hpp @@ -1,3 +1,6 @@ +#ifndef OPENMW_MWMP_MAIN +#define OPENMW_MWMP_MAIN + #include "../mwworld/ptr.hpp" #include #include @@ -54,3 +57,5 @@ namespace mwmp unsigned short port; }; } + +#endif //OPENMW_MWMP_MAIN \ No newline at end of file diff --git a/apps/openmw/mwmp/PlayerProcessor.cpp b/apps/openmw/mwmp/PlayerProcessor.cpp new file mode 100644 index 000000000..a8b790ce4 --- /dev/null +++ b/apps/openmw/mwmp/PlayerProcessor.cpp @@ -0,0 +1,73 @@ +// +// Created by koncord on 04.04.17. +// + +#include +#include "Networking.hpp" +#include "PlayerProcessor.hpp" +#include "DedicatedPlayer.hpp" +#include "LocalPlayer.hpp" +#include "Main.hpp" + +using namespace mwmp; + +PlayerProcessor::processors_t PlayerProcessor::processors; +RakNet::RakNetGUID PlayerProcessor::myGuid; +RakNet::RakNetGUID PlayerProcessor::guid; +RakNet::SystemAddress PlayerProcessor::serverAddr; +bool PlayerProcessor::request; + +void PlayerProcessor::AddProcessor(PlayerProcessor *processor) +{ + BOOST_FOREACH(processors_t::value_type &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(), boost::shared_ptr(processor))); +} + +bool PlayerProcessor::Process(RakNet::Packet &packet) +{ + RakNet::BitStream bsIn(&packet.data[1], packet.length, false); + bsIn.Read(guid); + + PlayerPacket *myPacket = Main::get().getNetworking()->getPlayerPacket(packet.data[0]); + myPacket->SetReadStream(&bsIn); + + /*if(myPacket == 0) + { + // error: packet not found + }*/ + + BOOST_FOREACH(processors_t::value_type &processor, processors) + { + if(processor.first == packet.data[0]) + { + myGuid = Main::get().getLocalPlayer()->guid; + request = packet.length == myPacket->headerSize(); + + BasePlayer *player = 0; + if (guid != myGuid) + player = Players::getPlayer(guid); + else + player = Main::get().getLocalPlayer(); + + if(!request && !processor.second->avoidReading && player != 0) + { + myPacket->setPlayer(player); + myPacket->Read(); + } + + processor.second->Do(*myPacket, player); + return true; + } + } + return false; +} + +LocalPlayer *PlayerProcessor::getLocalPlayer() +{ + return Main::get().getLocalPlayer(); +} \ No newline at end of file diff --git a/apps/openmw/mwmp/PlayerProcessor.hpp b/apps/openmw/mwmp/PlayerProcessor.hpp new file mode 100644 index 000000000..c7d257789 --- /dev/null +++ b/apps/openmw/mwmp/PlayerProcessor.hpp @@ -0,0 +1,58 @@ +// +// Created by koncord on 03.04.17. +// + +#ifndef OPENMW_PLAYERPROCESSOR_HPP +#define OPENMW_PLAYERPROCESSOR_HPP + +#include +#include + +#include +#include +#include +#include +#include "LocalPlayer.hpp" +#include "DedicatedPlayer.hpp" + +namespace mwmp +{ + class PlayerProcessor : public BasePacketProcessor + { + public: + virtual void Do(PlayerPacket &packet, BasePlayer *player) = 0; + + static bool Process(RakNet::Packet &packet); + static void AddProcessor(PlayerProcessor *processor); + static void SetServerAddr(RakNet::SystemAddress addr) + { + serverAddr = addr; + } + + typedef boost::unordered_map > processors_t; + //typedef std::unordered_map > processors_t; + private: + static processors_t processors; + static bool request; + protected: + inline bool isRequest() + { + return request; + } + + inline bool isLocal() + { + return guid == myGuid; + } + + LocalPlayer *getLocalPlayer(); + protected: + static RakNet::RakNetGUID myGuid; + static RakNet::RakNetGUID guid; + static RakNet::SystemAddress serverAddr; + }; +} + + + +#endif //OPENMW_PLAYERPROCESSOR_HPP diff --git a/apps/openmw/mwmp/WorldProcesssor.cpp b/apps/openmw/mwmp/WorldProcesssor.cpp new file mode 100644 index 000000000..fd0aeef2b --- /dev/null +++ b/apps/openmw/mwmp/WorldProcesssor.cpp @@ -0,0 +1,60 @@ +// +// Created by koncord on 16.04.17. +// + +#include +#include "WorldProcesssor.hpp" +#include "Main.hpp" +#include "Networking.hpp" + +using namespace mwmp; + +WorldProcesssor::processors_t WorldProcesssor::processors; +RakNet::RakNetGUID WorldProcesssor::guid; +RakNet::SystemAddress WorldProcesssor::serverAddr; +bool WorldProcesssor::request; + +bool WorldProcesssor::Process(RakNet::Packet &packet, BaseEvent &event) +{ + RakNet::BitStream bsIn(&packet.data[1], packet.length, false); + bsIn.Read(guid); + + WorldPacket *myPacket = Main::get().getNetworking()->getWorldPacket(packet.data[0]); + + myPacket->setEvent(&event); + myPacket->SetReadStream(&bsIn); + + BOOST_FOREACH(processors_t::value_type &processor, processors) + { + if(processor.first == packet.data[0]) + { + request = packet.length == myPacket->headerSize(); + + if(!request && !processor.second->avoidReading) + { + myPacket->Read(); + } + + processor.second->Do(*myPacket, event); + + return true; + } + } + return false; +} + +void WorldProcesssor::AddProcessor(mwmp::WorldProcesssor *processor) +{ + BOOST_FOREACH(processors_t::value_type &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(), boost::shared_ptr(processor))); +} + +LocalPlayer *WorldProcesssor::getLocalPlayer() +{ + return Main::get().getLocalPlayer(); +} diff --git a/apps/openmw/mwmp/WorldProcesssor.hpp b/apps/openmw/mwmp/WorldProcesssor.hpp new file mode 100644 index 000000000..1e0ff0de3 --- /dev/null +++ b/apps/openmw/mwmp/WorldProcesssor.hpp @@ -0,0 +1,50 @@ +// +// Created by koncord on 16.04.17. +// + +#ifndef OPENMW_WORLDPROCESSSOR_HPP +#define OPENMW_WORLDPROCESSSOR_HPP + +#include +#include + +#include +#include +#include +#include +#include "LocalPlayer.hpp" +#include "DedicatedPlayer.hpp" + +namespace mwmp +{ + class WorldProcesssor : public BasePacketProcessor + { + public: + virtual void Do(WorldPacket &packet, BaseEvent &event) = 0; + + static bool Process(RakNet::Packet &packet, BaseEvent &event); + static void AddProcessor(WorldProcesssor *processor); + static void SetServerAddr(RakNet::SystemAddress addr) + { + serverAddr = addr; + } + + typedef boost::unordered_map > processors_t; + + protected: + inline bool isRequest() + { + return request; + } + LocalPlayer *getLocalPlayer(); + protected: + static RakNet::RakNetGUID guid; + static RakNet::SystemAddress serverAddr; + private: + static processors_t processors; + static bool request; + }; +} + + +#endif //OPENMW_WORLDPROCESSSOR_HPP