diff --git a/include/examples/DisconnectPacket.h b/include/examples/DisconnectPacket.h new file mode 100644 index 0000000..6a69371 --- /dev/null +++ b/include/examples/DisconnectPacket.h @@ -0,0 +1,13 @@ +#pragma once + +#include +#include +#include + +enum DisconnectPacketFields { + Reason = 0 +}; + +using DisconnectFields = std::tuple; + +DeclarePacket(Disconnect); \ No newline at end of file diff --git a/include/examples/PacketExample.h b/include/examples/PacketExample.h index c679026..02e18fc 100644 --- a/include/examples/PacketExample.h +++ b/include/examples/PacketExample.h @@ -1,11 +1,15 @@ #pragma once enum PacketId { - KeepAlive + KeepAlive = 0, + Disconnect, }; #include +#include -using AllPackets = std::tuple; +// they must be in the same order ! +using AllPackets = std::tuple; -#include \ No newline at end of file +#include +#include \ No newline at end of file diff --git a/include/sp/default/DefaultPacketFactory.h b/include/sp/default/DefaultPacketFactory.h new file mode 100644 index 0000000..8f7e4bd --- /dev/null +++ b/include/sp/default/DefaultPacketFactory.h @@ -0,0 +1,7 @@ +#pragma once + +#include + +namespace sp { +using PacketFactory = sp::MessageFactory; +} // namespace sp diff --git a/include/sp/protocol/MessageFactory.h b/include/sp/protocol/MessageFactory.h new file mode 100644 index 0000000..eaea45b --- /dev/null +++ b/include/sp/protocol/MessageFactory.h @@ -0,0 +1,65 @@ +#pragma once + +#include +#include +#include +#include + +namespace sp { + +namespace details { + +template +using ArrayType = std::vector(void)>>; + + + +template +struct ArrayFiller {}; + +template +struct ArrayFiller> { + static void ArrayAppend(details::ArrayType& array) { + ArrayFiller::ArrayAppend(array); + } +}; + +template +struct ArrayFiller { + static void ArrayAppend(details::ArrayType& array) { + ArrayFiller::ArrayAppend(array); + ArrayFiller::ArrayAppend(array); + } +}; + +template +struct ArrayFiller { + static void ArrayAppend(details::ArrayType& array) { + array.push_back([]() -> std::unique_ptr { return std::make_unique(); }); + } +}; + +} // namespace details + +template +class MessageFactory { + public: + using IdType = typename TBase::MsgIdType; + + MessageFactory() { + details::ArrayFiller::ArrayAppend(m_Factory); + } + + std::unique_ptr CreateMessage(IdType id) { + if (id >= m_Factory.size()) + return nullptr; + return m_Factory.at(id)(); + } + + private: + details::ArrayType m_Factory; +}; + + + +} // namespace sp diff --git a/src/main.cpp b/src/main.cpp index cf70156..18d0cb0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,6 +7,10 @@ class KeepAliveHandler : public sp::PacketHandler { void Handle(KeepAlivePacket& packet) { std::cout << "KeepAlive handled !\n"; } + + void Handle(DisconnectPacket& packet) { + std::cout << "Disconnect handled !\n"; + } }; int main() { @@ -24,7 +28,15 @@ int main() { std::cout << "KeepAlive2 : " << keepAlive2->GetField() << "\n"; - //TODO: write ID and factory + //TODO: write ID + sp::PacketFactory factory; + auto packet = factory.CreateMessage(Disconnect); + if (packet == nullptr) { + std::cout << "Mauvais ID !\n"; + return 1; + } + std::cout << (unsigned) packet->GetId() << std::endl; + packet->Dispatch(handler); return 0; } \ No newline at end of file