better example

This commit is contained in:
2025-02-18 19:09:40 +01:00
parent baa52d3baa
commit 2eab50932f
4 changed files with 45 additions and 23 deletions

View File

@@ -10,11 +10,7 @@ enum KeepAlivePacketFields {
};
using KeepAliveFields = std::tuple<
std::uint64_t, //<- KeepAliveId
sp::BitField<std::uint16_t,
sp::Field<std::uint16_t, 12>, //<- m_Tower
sp::Field<std::uint8_t, 4> //<- m_Upgrade
>
std::uint64_t //<- KeepAliveId
>;
DeclarePacket(KeepAlive){
@@ -24,12 +20,4 @@ DeclarePacket(KeepAlive){
std::uint64_t GetKeepAliveId() const {
return GetField<KeepAlive>();
}
std::uint16_t GetTowerId() const {
return GetField<TestAlignField>().GetField<0>();
}
std::uint8_t GetTowerUpgrade() const {
return GetField<TestAlignField>().GetField<1>();
}
};

View File

@@ -3,13 +3,15 @@
enum PacketId {
KeepAlive = 0,
Disconnect,
UpgradeTower,
};
#include <examples/KeepAlivePacket.h>
#include <examples/DisconnectPacket.h>
#include <examples/UpgradeTowerPacket.h>
// they must be in the same order !
using AllPackets = std::tuple<KeepAlivePacket, DisconnectPacket>;
// they must be in the same order as in the enum !
using AllPackets = std::tuple<KeepAlivePacket, DisconnectPacket, UpgradeTowerPacket>;
#include <sp/default/DefaultPacketHandler.h>
#include <sp/default/DefaultPacketFactory.h>

View File

@@ -0,0 +1,30 @@
#pragma once
#include <sp/default/DefaultPacket.h>
#include <sp/protocol/Field.h>
#include <sp/protocol/MessageBase.h>
enum UpgradeTowerPacketFields {
m_Tower = 0,
m_Upgrade,
};
using UpgradeTowerFields = std::tuple<
sp::BitField<std::uint16_t,
sp::Field<std::uint16_t, 12>, //<- m_Tower
sp::Field<std::uint8_t, 4> //<- m_Upgrade
>
>;
DeclarePacket(UpgradeTower){
public:
PacketConstructor(UpgradeTower)
std::uint16_t GetTowerId() const {
return GetField<0>().GetField<m_Tower>();
}
std::uint8_t GetTowerUpgrade() const {
return GetField<0>().GetField<m_Upgrade>();
}
};

View File

@@ -11,12 +11,17 @@ class KeepAliveHandler : public sp::PacketHandler {
void Handle(DisconnectPacket& packet) {
std::cout << "Disconnect handled !\n";
}
// TODO: add constness
void Handle(UpgradeTowerPacket& packet) {
std::cout << "UpgradeTower handled !\n";
}
};
int main() {
auto keepAlive = std::make_unique<KeepAlivePacket>(69, std::make_tuple(666, 9));
auto upgradeTower = std::make_unique<UpgradeTowerPacket>(std::make_tuple(666, 9));
sp::PacketMessage* msg = keepAlive.get();
sp::PacketMessage* msg = upgradeTower.get();
KeepAliveHandler handler;
msg->Dispatch(handler);
@@ -27,13 +32,10 @@ int main() {
std::uint8_t msgId;
buffer >> msgId;
auto keepAlive2 = std::make_unique<KeepAlivePacket>();
keepAlive2->Read(buffer);
auto upgradeTower2 = std::make_unique<UpgradeTowerPacket>();
upgradeTower2->Read(buffer);
std::cout << "KeepAlive2 : " << keepAlive2->GetKeepAliveId() << "\n";
keepAlive2->GetField<TestAlignField>().GetField<0>();
std::cout << "Test : " << (unsigned) keepAlive2->GetTowerId() << "\n";
std::cout << "Test : " << (unsigned) upgradeTower2->GetTowerId() << "\n";
sp::PacketFactory factory;
auto packet = factory.CreateMessage(msgId);