You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openmw-tes3mp/components/openmw-mp/Controllers/BasePacketController.hpp

49 lines
1.2 KiB
C++

//
// 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;
};
}