[Client] Add PlayerProcessor & WorldProcessor

pull/199/head
Koncord 8 years ago
parent 07c2d4251e
commit a6111b6599

@ -6,6 +6,7 @@
#define OPENMW_LOCALPLAYER_HPP
#include <components/openmw-mp/Base/BasePlayer.hpp>
#include "../mwworld/ptr.hpp"
#include <RakNetTypes.h>
namespace mwmp

@ -1,3 +1,6 @@
#ifndef OPENMW_MWMP_MAIN
#define OPENMW_MWMP_MAIN
#include "../mwworld/ptr.hpp"
#include <boost/program_options.hpp>
#include <components/files/collections.hpp>
@ -54,3 +57,5 @@ namespace mwmp
unsigned short port;
};
}
#endif //OPENMW_MWMP_MAIN

@ -0,0 +1,73 @@
//
// Created by koncord on 04.04.17.
//
#include <boost/foreach.hpp>
#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<PlayerProcessor>(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();
}

@ -0,0 +1,58 @@
//
// Created by koncord on 03.04.17.
//
#ifndef OPENMW_PLAYERPROCESSOR_HPP
#define OPENMW_PLAYERPROCESSOR_HPP
#include <boost/unordered_map.hpp>
#include <boost/shared_ptr.hpp>
#include <components/openmw-mp/Log.hpp>
#include <components/openmw-mp/NetworkMessages.hpp>
#include <components/openmw-mp/Base/BasePacketProcessor.hpp>
#include <components/openmw-mp/Packets/Player/PlayerPacket.hpp>
#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<unsigned char, boost::shared_ptr<PlayerProcessor> > processors_t;
//typedef std::unordered_map<unsigned char, std::unique_ptr<PlayerProcessor> > 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

@ -0,0 +1,60 @@
//
// Created by koncord on 16.04.17.
//
#include <boost/foreach.hpp>
#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<WorldProcesssor>(processor)));
}
LocalPlayer *WorldProcesssor::getLocalPlayer()
{
return Main::get().getLocalPlayer();
}

@ -0,0 +1,50 @@
//
// Created by koncord on 16.04.17.
//
#ifndef OPENMW_WORLDPROCESSSOR_HPP
#define OPENMW_WORLDPROCESSSOR_HPP
#include <boost/unordered_map.hpp>
#include <boost/shared_ptr.hpp>
#include <components/openmw-mp/Log.hpp>
#include <components/openmw-mp/NetworkMessages.hpp>
#include <components/openmw-mp/Base/BasePacketProcessor.hpp>
#include <components/openmw-mp/Packets/World/WorldPacket.hpp>
#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<unsigned char, boost::shared_ptr<WorldProcesssor> > 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
Loading…
Cancel
Save