[General] Move similar functions to BasePacketController

Simplify ContainsPacket and fix GetPacket
sol2-server-rewrite
Koncord 6 years ago
parent 24ba4ae404
commit 6f7771d97e

@ -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

@ -16,15 +16,6 @@
#include "ActorPacketController.hpp"
template <typename T>
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<PacketActorList>(&packets, peer);
@ -42,25 +33,3 @@ mwmp::ActorPacketController::ActorPacketController(RakNet::RakPeerInterface *pee
AddPacket<PacketActorSpeech>(&packets, peer);
AddPacket<PacketActorStatsDynamic>(&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;
}

@ -2,25 +2,15 @@
#define OPENMW_ACTORPACKETCONTROLLER_HPP
#include <RakPeerInterface.h>
#include "../Packets/Actor/ActorPacket.hpp"
#include <unordered_map>
#include <memory>
#include "BasePacketController.hpp"
namespace mwmp
{
class ActorPacketController
class ActorPacketController: public BasePacketController<ActorPacket>
{
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<RakNet::MessageID, std::unique_ptr<ActorPacket> > packets_t;
private:
packets_t packets;
};
}

@ -0,0 +1,48 @@
//
// Created by koncord on 26.02.18.
//
#pragma once
#include <RakNetTypes.h>
#include <unordered_map>
#include <memory>
namespace mwmp
{
template<typename PacketT>
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<RakNet::MessageID, std::unique_ptr<PacketT> > packets_t;
protected:
template <typename T>
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;
};
}

@ -49,15 +49,6 @@
#include "PlayerPacketController.hpp"
template <typename T>
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<PacketDisconnect>(&packets, peer);
@ -110,25 +101,3 @@ mwmp::PlayerPacketController::PlayerPacketController(RakNet::RakPeerInterface *p
AddPacket<PacketPlayerStatsDynamic>(&packets, peer);
AddPacket<PacketPlayerTopic>(&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;
}

@ -2,25 +2,15 @@
#define OPENMW_PLAYERPACKETCONTROLLER_HPP
#include <RakPeerInterface.h>
#include "../Packets/Player/PlayerPacket.hpp"
#include <unordered_map>
#include <memory>
#include "BasePacketController.hpp"
namespace mwmp
{
class PlayerPacketController
class PlayerPacketController: public BasePacketController<PlayerPacket>
{
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<RakNet::MessageID, std::unique_ptr<PlayerPacket> > packets_t;
private:
packets_t packets;
};
}

@ -28,15 +28,6 @@
#include "WorldPacketController.hpp"
template <typename T>
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<PacketObjectAnimPlay>(&packets, peer);
@ -67,25 +58,3 @@ mwmp::WorldPacketController::WorldPacketController(RakNet::RakPeerInterface *pee
AddPacket<PacketScriptGlobalShort>(&packets, peer);
AddPacket<PacketScriptGlobalFloat>(&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;
}

@ -2,25 +2,15 @@
#define OPENMW_WORLDPACKETCONTROLLER_HPP
#include <RakPeerInterface.h>
#include "../Packets/World/WorldPacket.hpp"
#include <unordered_map>
#include <memory>
#include "BasePacketController.hpp"
namespace mwmp
{
class WorldPacketController
class WorldPacketController: public BasePacketController<WorldPacket>
{
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<RakNet::MessageID, std::unique_ptr<WorldPacket> > packets_t;
private:
packets_t packets;
};
}

Loading…
Cancel
Save