refactor PacketFactory

This commit is contained in:
2024-04-13 16:13:10 +02:00
parent 33f1c53148
commit e04444125d
3 changed files with 27 additions and 24 deletions

View File

@@ -5,9 +5,13 @@
*/
#include "blitz/protocol/Protocol.h"
#include <memory>
namespace blitz {
namespace protocol {
typedef std::unique_ptr<Packet> PacketPtr;
namespace PacketFactory {
/**
@@ -15,7 +19,7 @@ namespace PacketFactory {
* \param buffer The buffer containing the packet data.
* \return The created packet.
*/
const Packet* CreatePacket(PacketType type, DataBuffer& buffer);
const PacketPtr CreatePacket(PacketType type, DataBuffer& buffer);
} // namespace PacketFactory
} // namespace protocol

View File

@@ -38,8 +38,8 @@ bool Connexion::UpdateSocket() {
protocol::PacketType packetType;
decompressed >> packetType;
const protocol::Packet* packet = protocol::PacketFactory::CreatePacket(packetType, decompressed);
GetDispatcher()->Dispatch(packet);
const protocol::PacketPtr packet = protocol::PacketFactory::CreatePacket(packetType, decompressed);
GetDispatcher()->Dispatch(packet.get());
}
return true;
}

View File

@@ -3,37 +3,36 @@
#include "blitz/protocol/Packets.h"
#include <array>
#include <functional>
#include <map>
#include <memory>
namespace blitz {
namespace protocol {
namespace PacketFactory {
typedef std::unique_ptr<Packet> PacketPtr;
typedef std::function<PacketPtr()> PacketCreator;
static std::array<PacketPtr, static_cast<std::size_t>(PacketType::PACKET_COUNT)> Packets = {
std::make_unique<PlayerLoginPacket>(),
std::make_unique<UpdateHealthPacket>(),
std::make_unique<ConnexionInfoPacket>(),
std::make_unique<PlayerJoinPacket>(),
std::make_unique<PlayerLeavePacket>(),
std::make_unique<PlayerListPacket>(),
std::make_unique<PlayerStatsPacket>(),
std::make_unique<ServerConfigPacket>(),
std::make_unique<ServerTpsPacket>(),
std::make_unique<UpdateGameStatePacket>(),
std::make_unique<KeepAlivePacket>(),
std::make_unique<DisconnectPacket>(),
std::make_unique<ChatPacket>(),
std::make_unique<PlayerPositionAndRotationPacket>(),
std::make_unique<PlayerShootPacket>(),
static std::array<PacketCreator, static_cast<std::size_t>(PacketType::PACKET_COUNT)> Packets = {
[]() -> PacketPtr { return std::make_unique<PlayerLoginPacket>(); },
[]() -> PacketPtr { return std::make_unique<UpdateHealthPacket>(); },
[]() -> PacketPtr { return std::make_unique<ConnexionInfoPacket>(); },
[]() -> PacketPtr { return std::make_unique<PlayerJoinPacket>(); },
[]() -> PacketPtr { return std::make_unique<PlayerLeavePacket>(); },
[]() -> PacketPtr { return std::make_unique<PlayerListPacket>(); },
[]() -> PacketPtr { return std::make_unique<PlayerStatsPacket>(); },
[]() -> PacketPtr { return std::make_unique<ServerConfigPacket>(); },
[]() -> PacketPtr { return std::make_unique<ServerTpsPacket>(); },
[]() -> PacketPtr { return std::make_unique<UpdateGameStatePacket>(); },
[]() -> PacketPtr { return std::make_unique<KeepAlivePacket>(); },
[]() -> PacketPtr { return std::make_unique<DisconnectPacket>(); },
[]() -> PacketPtr { return std::make_unique<ChatPacket>(); },
[]() -> PacketPtr { return std::make_unique<PlayerPositionAndRotationPacket>(); },
[]() -> PacketPtr { return std::make_unique<PlayerShootPacket>(); },
};
const Packet* CreatePacket(PacketType type, DataBuffer& buffer) {
PacketPtr& packet = Packets[static_cast<std::size_t>(type)];
const PacketPtr CreatePacket(PacketType type, DataBuffer& buffer) {
PacketPtr packet = Packets[static_cast<std::size_t>(type)]();
packet->Deserialize(buffer);
return packet.get();
return packet;
}
} // namespace PacketFactory