generated from Persson-dev/Godot-Xmake
38 lines
1.1 KiB
C++
38 lines
1.1 KiB
C++
#include <blitz/protocol/PacketDispatcher.h>
|
|
|
|
#include <algorithm>
|
|
#include <blitz/protocol/PacketHandler.h>
|
|
|
|
namespace blitz {
|
|
namespace protocol {
|
|
|
|
void PacketDispatcher::RegisterHandler(PacketType type, PacketHandler& handler) {
|
|
auto found = std::find(m_Handlers[type].begin(), m_Handlers[type].end(), &handler);
|
|
if (found == m_Handlers[type].end())
|
|
m_Handlers[type].push_back(&handler);
|
|
}
|
|
|
|
void PacketDispatcher::UnregisterHandler(PacketType type, PacketHandler& handler) {
|
|
m_Handlers[type].erase(std::remove(m_Handlers[type].begin(), m_Handlers[type].end(), &handler), m_Handlers[type].end());
|
|
}
|
|
|
|
void PacketDispatcher::UnregisterHandler(PacketHandler& handler) {
|
|
for (auto& pair : m_Handlers) {
|
|
if (pair.second.empty())
|
|
continue;
|
|
|
|
PacketType type = pair.first;
|
|
|
|
m_Handlers[type].erase(std::remove(m_Handlers[type].begin(), m_Handlers[type].end(), &handler), m_Handlers[type].end());
|
|
}
|
|
}
|
|
|
|
void PacketDispatcher::Dispatch(const Packet& packet) {
|
|
PacketType type = packet.GetType();
|
|
for (PacketHandler* handler : m_Handlers[type])
|
|
handler->Check(packet);
|
|
}
|
|
|
|
} // namespace protocol
|
|
} // namespace blitz
|