diff --git a/include/sp/protocol/Dispatcher.h b/include/sp/Dispatcher.h similarity index 89% rename from include/sp/protocol/Dispatcher.h rename to include/sp/Dispatcher.h index 8ddfe9e..02a963f 100644 --- a/include/sp/protocol/Dispatcher.h +++ b/include/sp/Dispatcher.h @@ -2,12 +2,12 @@ /** * \file PacketDispatcher.h - * \brief File containing the sp::protocol::PacketDispatcher class + * \brief File containing the td::protocol::PacketDispatcher class */ #include -#include +#include #include namespace sp { @@ -56,6 +56,6 @@ class Dispatcher : private NonCopyable { }; } // namespace protocol -} // namespace sp +} // namespace td #include "Dispatcher.inl" \ No newline at end of file diff --git a/include/sp/protocol/Dispatcher.inl b/include/sp/Dispatcher.inl similarity index 96% rename from include/sp/protocol/Dispatcher.inl rename to include/sp/Dispatcher.inl index 22e4f5d..c7e4a80 100644 --- a/include/sp/protocol/Dispatcher.inl +++ b/include/sp/Dispatcher.inl @@ -28,11 +28,11 @@ void Dispatcher::UnregisterHandler(T_Handler& handler) { if (pair.second.empty()) continue; - PacketType type = pair.first; + auto type = pair.first; m_Handlers[type].erase(std::remove(m_Handlers[type].begin(), m_Handlers[type].end(), &handler), m_Handlers[type].end()); } } } // namespace protocol -} // namespace sp +} // namespace td diff --git a/include/sp/GenericHandler.h b/include/sp/GenericHandler.h new file mode 100644 index 0000000..9e50a37 --- /dev/null +++ b/include/sp/GenericHandler.h @@ -0,0 +1,141 @@ +#pragma once + +#include + +namespace sp +{ + // This class is inspired by https://arobenko.gitbooks.io/comms-protocols-cpp/content/ + + // TCommon is common interface class for all the messages + // TAll is all the message types, that need to be handled, bundled in std::tuple + template + class GenericHandler; + + // Big boy to process packets 20 by 20, preventing needlessly copying vtable many times at each inheritance stage + template + class GenericHandler > : public GenericHandler > + { + using Base = GenericHandler >; + public: + using Base::Handle; // Don't hide all Handle() functions from base classes + virtual void Handle(T1& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T2& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T3& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T4& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T5& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T6& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T7& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T8& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T9& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T10& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T11& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T12& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T13& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T14& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T15& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T16& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T17& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T18& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T19& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T20& msg) { this->Handle(static_cast(msg)); } + }; + + // 10 by 10 + template + class GenericHandler > : public GenericHandler > + { + using Base = GenericHandler >; + public: + using Base::Handle; // Don't hide all Handle() functions from base classes + virtual void Handle(T1& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T2& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T3& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T4& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T5& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T6& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T7& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T8& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T9& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T10& msg) { this->Handle(static_cast(msg)); } + }; + + // 5 by 5 + template + class GenericHandler > : public GenericHandler > + { + using Base = GenericHandler >; + public: + using Base::Handle; // Don't hide all Handle() functions from base classes + virtual void Handle(T1& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T2& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T3& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T4& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T5& msg) { this->Handle(static_cast(msg)); } + }; + + // Deal with rest with 4 types + template + class GenericHandler > + { + public: + virtual ~GenericHandler() {} + virtual void Handle(T1& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T2& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T3& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T4& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(TCommon&) { } //Nothing to do + }; + + // Deal with rest with 3 types + template + class GenericHandler > + { + public: + virtual ~GenericHandler() {} + virtual void Handle(T1& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T2& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T3& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(TCommon&) { } //Nothing to do + }; + + // Deal with rest with 2 types + template + class GenericHandler > + { + public: + virtual ~GenericHandler() {} + virtual void Handle(T1& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(T2& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(TCommon&) { } //Nothing to do + }; + + // Deal with rest with 1 type + template + class GenericHandler > + { + public: + virtual ~GenericHandler() {} + virtual void Handle(T1& msg) { this->Handle(static_cast(msg)); } + virtual void Handle(TCommon&) { } //Nothing to do + }; + + // Deal with rest with 0 type + template + class GenericHandler > + { + public: + virtual ~GenericHandler() {} + virtual void Handle(TCommon&) { } //Nothing to do + }; + +} // sp \ No newline at end of file diff --git a/include/sp/Message.h b/include/sp/Message.h new file mode 100644 index 0000000..e640efd --- /dev/null +++ b/include/sp/Message.h @@ -0,0 +1,38 @@ +#pragma once + +#include +#include + +#include + +namespace sp { +class Handler; + +class Message { + public: + virtual ~Message() {} + + void Write(DataBuffer& buffer) const { + buffer << GetId(); + WriteImpl(buffer); + } + + void Read(const DataBuffer& buffer) { + ReadImpl(buffer); + } + + void Dispatch(Handler& handler) { + DispatchImpl(handler); + } + + virtual int GetId() const = 0; + + virtual std::string_view GetName() const = 0; + + protected: + virtual void DispatchImpl(Handler& handler) = 0; + virtual void WriteImpl(DataBuffer& buffer) const = 0; + virtual void ReadImpl(const DataBuffer& buffer) = 0; +}; + +} // namespace sp \ No newline at end of file diff --git a/include/sp/MessageBase.h b/include/sp/MessageBase.h new file mode 100644 index 0000000..28b8437 --- /dev/null +++ b/include/sp/MessageBase.h @@ -0,0 +1,18 @@ +#pragma once + +#include + +namespace sp { + +template +class MessageBase : public Message { + protected: + virtual void DispatchImpl(Handler& handler) override { + handler.Handle(static_cast(*this)); + } + + virtual void WriteImpl(DataBuffer& buffer) const = 0; + virtual void ReadImpl(const DataBuffer& buffer) = 0; +}; + +} // namespace sp \ No newline at end of file diff --git a/include/sp/Templates.h b/include/sp/Templates.h new file mode 100644 index 0000000..845ee02 --- /dev/null +++ b/include/sp/Templates.h @@ -0,0 +1,47 @@ +#pragma once + +#include + +namespace sp { + +/// @brief Default case, see field_index specialization for implementation details +template typename U, typename = void> +static constexpr size_t field_index = 0; + +/// @brief A templated size_t that counts the number of existing classes U with a "field_name" member +/// @tparam N Current counter for recursion (user should always call it with 0) +/// @tparam T Can be any type, must be different for each field we want to be counted later (used because we can't have a empty +/// template<> specialization nested in a class) +/// @tparam U The templated class that will be searched for match +template typename U> +static constexpr size_t field_index::field_name)>> = 1 + field_index; + +/// @brief Concat multiple tuples in one big tuple +/// @tparam ...input_t Multiple std::tuple types to concat +template +using tuple_cat_t = decltype(std::tuple_cat(std::declval()...)); + +template +constexpr bool tuple_contains_type = false; +template +constexpr bool tuple_contains_type> = std::disjunction_v...>; + +template +constexpr int get_tuple_index = 0; +template +constexpr int get_tuple_index> = 0; +template +constexpr int get_tuple_index> = 1 + get_tuple_index>; + +// Template black magic to loop at compile time +template +void loop_impl(std::index_sequence, LoopBody&& loop_body) { + (loop_body(std::integral_constant{}), ...); +} + +template +void loop(LoopBody&& loop_body) { + loop_impl(std::make_index_sequence{}, std::forward(loop_body)); +} + +} // namespace sp diff --git a/include/sp/common/MacroMap.h b/include/sp/common/MacroMap.h new file mode 100644 index 0000000..e5afacf --- /dev/null +++ b/include/sp/common/MacroMap.h @@ -0,0 +1,54 @@ +/* + * Created by William Swanson in 2012. + * + * I, William Swanson, dedicate this work to the public domain. + * I waive all rights to the work worldwide under copyright law, + * including all related and neighboring rights, + * to the extent allowed by law. + * + * You can copy, modify, distribute and perform the work, + * even for commercial purposes, all without asking permission. + */ + +#ifndef MAP_H_INCLUDED +#define MAP_H_INCLUDED + +#define EVAL0(...) __VA_ARGS__ +#define EVAL1(...) EVAL0(EVAL0(EVAL0(__VA_ARGS__))) +#define EVAL2(...) EVAL1(EVAL1(EVAL1(__VA_ARGS__))) +#define EVAL3(...) EVAL2(EVAL2(EVAL2(__VA_ARGS__))) +#define EVAL4(...) EVAL3(EVAL3(EVAL3(__VA_ARGS__))) +#define EVAL(...) EVAL4(EVAL4(EVAL4(__VA_ARGS__))) + +#define MAP_END(...) +#define MAP_OUT +#define MAP_COMMA , + +#define MAP_GET_END2() 0, MAP_END +#define MAP_GET_END1(...) MAP_GET_END2 +#define MAP_GET_END(...) MAP_GET_END1 +#define MAP_NEXT0(test, next, ...) next MAP_OUT +#define MAP_NEXT1(test, next) MAP_NEXT0(test, next, 0) +#define MAP_NEXT(test, next) MAP_NEXT1(MAP_GET_END test, next) + +#define MAP0(f, x, peek, ...) f(x) MAP_NEXT(peek, MAP1)(f, peek, __VA_ARGS__) +#define MAP1(f, x, peek, ...) f(x) MAP_NEXT(peek, MAP0)(f, peek, __VA_ARGS__) + +#define MAP_LIST_NEXT1(test, next) MAP_NEXT0(test, MAP_COMMA next, 0) +#define MAP_LIST_NEXT(test, next) MAP_LIST_NEXT1(MAP_GET_END test, next) + +#define MAP_LIST0(f, x, peek, ...) f(x) MAP_LIST_NEXT(peek, MAP_LIST1)(f, peek, __VA_ARGS__) +#define MAP_LIST1(f, x, peek, ...) f(x) MAP_LIST_NEXT(peek, MAP_LIST0)(f, peek, __VA_ARGS__) + +/** + * Applies the function macro `f` to each of the remaining parameters. + */ +#define MAP(f, ...) EVAL(MAP1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0)) + +/** + * Applies the function macro `f` to each of the remaining parameters and + * inserts commas between the results. + */ +#define MAP_LIST(f, ...) EVAL(MAP_LIST1(f, __VA_ARGS__, ()()(), ()()(), ()()(), 0)) + +#endif \ No newline at end of file diff --git a/include/sp/protocol/command/CommandData.h b/include/sp/protocol/command/CommandData.h deleted file mode 100644 index 68e68c0..0000000 --- a/include/sp/protocol/command/CommandData.h +++ /dev/null @@ -1,53 +0,0 @@ -// #pragma once - -// #include -// #include -// #include - -// namespace sp { -// namespace protocol { - -// namespace cdata { - - -// struct PlaceTower { -// TowerType m_Type : 4; -// PlayerID m_Placer : 4; -// TowerCoords m_Position; -// }; - -// struct UpgradeTower { -// TowerID m_Tower : 12; -// std::uint8_t m_Upgrade : 4; -// }; - -// struct SpawnTroop { -// EntityType m_Type : 5; -// std::uint8_t m_Level : 3; -// EntityCoords m_Position; -// PlayerID m_Sender; -// }; - -// struct UseItem { -// ShopItem m_Item : 4; -// PlayerID m_User : 4; -// EntityCoords m_Position; -// }; - -// struct TeamChange { -// PlayerID m_Player : 7; -// Team m_NewTeam : 1; -// }; - -// struct PlayerJoin { -// PlayerID m_ID; -// std::string m_Name; -// }; - -// struct End {}; - -// } // namespace cdata - - -// } // namespace protocol -// } // namespace sp diff --git a/include/sp/protocol/command/CommandDeclare.h b/include/sp/protocol/command/CommandDeclare.h deleted file mode 100644 index b08a7f1..0000000 --- a/include/sp/protocol/command/CommandDeclare.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - - -namespace sp { -namespace protocol { - -/** - * \def DeclareAllPacket - * \brief Avoids repetitive operations on commands - */ -#define DeclareAllCommand() \ - DeclareCommand(End) \ - DeclareCommand(PlaceTower) \ - DeclareCommand(PlayerJoin) \ - DeclareCommand(SpawnTroop) \ - DeclareCommand(TeamChange) \ - DeclareCommand(UpgradeTower) \ - DeclareCommand(UseItem) - - -} // namespace protocol -} // namespace sp \ No newline at end of file diff --git a/include/sp/protocol/command/CommandDispatcher.h b/include/sp/protocol/command/CommandDispatcher.h deleted file mode 100644 index aae4d7b..0000000 --- a/include/sp/protocol/command/CommandDispatcher.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -/** - * \file CommandDispatcher.h - * \brief File containing the sp::protocol::CommandDispatcher class - */ - -#include -#include - -namespace sp { -namespace protocol { - -using CommandDispatcher = Dispatcher; - -} // namespace protocol -} // namespace sp diff --git a/include/sp/protocol/command/CommandFactory.h b/include/sp/protocol/command/CommandFactory.h deleted file mode 100644 index b4a91bd..0000000 --- a/include/sp/protocol/command/CommandFactory.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include -#include - -namespace sp { -namespace protocol { -namespace CommandFactory { - -template ::value>::type> -std::shared_ptr CreateCommand() { - return std::make_shared(); -} - -const std::shared_ptr& CreateReadOnlyCommand(CommandType a_Type); - -} // namespace CommandFactory -} // namespace protocol -} // namespace sp diff --git a/include/sp/protocol/command/CommandSerializer.h b/include/sp/protocol/command/CommandSerializer.h deleted file mode 100644 index 3b84a00..0000000 --- a/include/sp/protocol/command/CommandSerializer.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include -#include - -namespace sp { -namespace protocol { - -class Command; - -using CommandPtr = std::shared_ptr; - -namespace CommandSerializer { - -DataBuffer Serialize(const Command& a_Command); - -std::shared_ptr Deserialize(DataBuffer& a_Data); - -} // namespace CommandSerializer -} // namespace protocol -} // namespace sp diff --git a/include/sp/protocol/command/CommandVisitor.h b/include/sp/protocol/command/CommandVisitor.h deleted file mode 100644 index 7908143..0000000 --- a/include/sp/protocol/command/CommandVisitor.h +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once - -/** - * \file CommandVisitor.h - * \brief File containing the sp::protocol::CommandVisitor class - */ - -#include - -namespace sp { -namespace protocol { - -#define DeclareCommand(CommandName, ...) \ - /** This function is called when the packet processed by CommandVisitor::Check is a CommandName */ \ - virtual void Visit(const commands::CommandName&) {} - -/** - * \class CommandVisitor - * \brief This class uses double-dispatch in order to find the real type of a packet - */ -class CommandVisitor : private NonCopyable { - protected: - CommandVisitor() {} - virtual ~CommandVisitor() {} - - public: - /** - * \brief Calls the right CommandVisitor::Visit method corresponding to the real type of the packet - * \param packet the Command to visit - */ - void Check(const Command& packet); - - DeclareAllCommand() -}; - -#undef DeclareCommand - -} // namespace protocol -} // namespace sp diff --git a/include/sp/protocol/command/Commands.h b/include/sp/protocol/command/Commands.h deleted file mode 100644 index feaa058..0000000 --- a/include/sp/protocol/command/Commands.h +++ /dev/null @@ -1,111 +0,0 @@ -#pragma once - -/** - * \file Commands.h - * \brief File containing the definitions of the lockstep commands - */ - -#include -#include -#include -#include -#include - -namespace sp { -namespace protocol { - -class CommandVisitor; - -/** A Command id is 8 bits wide */ -using CommandID = std::uint8_t; - -#define DeclareCommand(CommandName, ...) /** CommandName */ CommandName, - -/** - * \enum CommandType - * \brief Map a Command to an id - */ -enum class CommandType : CommandID { - - DeclareAllCommand() - - /** The number of Commands */ - COMMAND_COUNT -}; - - -#undef DeclareCommand - - -class Command : private NonCopyable { - public: - /** - * \return The real type of the Command - */ - virtual CommandType GetType() const = 0; - - private: - /** Use a CommandVisitor to make double-dispatch possible */ - virtual void Accept(CommandVisitor& a_Visitor) const = 0; - - friend class CommandVisitor; -}; - - - - - -namespace commands { - -/** - * \class ConcreteCommand - * \brief A Command associated with an id and holding data - * \tparam PT The Command type - * \tparam Data The structure holding the data of the Command (in sp::protocol::data namespace) - */ -template -class ConcreteCommand : public Command { - public: - /** The type of the struct holding the data */ - using CommandDataType = Data; - - /** The structure holding the actual data */ - CommandDataType m_Data; - - /** Construct the Command with data of type CommandDataType */ - ConcreteCommand(const CommandDataType& a_Data = {}); - - constexpr CommandType GetType() const override { - return CT; - }; - - private: - void Accept(CommandVisitor& a_Visitor) const override; -}; - - - - - -// define SP_INSTANCIATE_COMMANDS -// before including this file -// if you want to instantiate templates -#ifdef SP_INSTANCIATE_COMMANDS -#define DeclareCommand(CommandName, ...) \ - using CommandName = ConcreteCommand; \ - template class ConcreteCommand; -#else -#define DeclareCommand(CommandName, ...) /** Defines the CommandName Command */ \ - using CommandName = ConcreteCommand; -#endif - -DeclareAllCommand() - -#undef DeclareCommand - -} // namespace commands - -using LockStep = std::vector>; - -} // namespace protocol -} // namespace sp diff --git a/include/sp/protocol/packet/PacketData.h b/include/sp/protocol/packet/PacketData.h deleted file mode 100644 index 13f692b..0000000 --- a/include/sp/protocol/packet/PacketData.h +++ /dev/null @@ -1,76 +0,0 @@ -// #pragma once - -// #include -// #include -// #include - -// // Make it dynamic ? -// #define LOCKSTEP_BUFFER_SIZE 10 - -// namespace sp { -// namespace protocol { - -// struct PlayerInfo { -// PlayerID m_PlayerId; -// std::string m_PlayerName; -// }; - -// struct MapData { - -// }; - -// namespace pdata { - -// /** Client attempts to login (very basic) */ -// struct PlayerLogin { -// std::string m_PlayerName; -// }; - -// /** Server indicates success */ -// struct LoggingSuccess { -// PlayerID m_PlayerId; -// }; - -// /** Player joins the lobby */ -// struct PlayerJoin { -// PlayerInfo m_Player; -// }; - -// /** Player leaves the lobby */ -// struct PlayerLeave { -// PlayerID m_PlayerId; -// }; - -// /** Keep alive */ -// struct KeepAlive { -// std::uint64_t m_KeepAliveId; -// }; - -// /** Can be used by both client and server */ -// struct Disconnect { -// std::string m_Reason; -// }; - -// /** Chat message */ -// struct ChatMessage { -// std::string m_Text; -// }; - -// // TODO: handle players joining in the first second - -// struct BeginGame { -// MapData m_Map; -// std::vector m_BlueTeam; -// std::vector m_RedTeam; -// // optional, used for players joining mid game -// std::vector m_FirstLocksteps; -// }; - -// struct LockSteps { -// std::uint16_t m_FirstFrameNumber; -// std::array m_LockSteps; -// }; - -// } // namespace pdata -// } // namespace protocol -// } // namespace sp diff --git a/include/sp/protocol/packet/PacketDeclare.h b/include/sp/protocol/packet/PacketDeclare.h deleted file mode 100644 index 44e4dce..0000000 --- a/include/sp/protocol/packet/PacketDeclare.h +++ /dev/null @@ -1,48 +0,0 @@ -// #pragma once - -// /** -// * \file PacketDeclare.h -// * \brief Holds the definitions of the packets (but not their content) -// */ - -// namespace sp { -// namespace protocol { - -// /** -// * \enum PacketSender -// * \brief Indicate who should send a packet -// */ -// enum class PacketSenderType { -// /** Sent by clients and server */ -// Both = 1, -// /** Sent by clients to the server */ -// Client, -// /** Sent by server to the clients */ -// Server, -// }; - -// enum class PacketSendType { -// Reliable = 1, -// Unreliable, -// UnreliableOrdered, -// }; - - -// /** -// * \def DeclareAllPacket -// * \brief Avoids repetitive operations on packets -// */ -// #define DeclareAllPacket() \ -// DeclarePacket(ChatMessage, Reliable, Both) \ -// DeclarePacket(BeginGame, Reliable, Server) \ -// DeclarePacket(Disconnect, Reliable, Both) \ -// DeclarePacket(KeepAlive, Reliable, Both) \ -// DeclarePacket(LockSteps, Unreliable, Both) \ -// DeclarePacket(LoggingSuccess, Reliable, Server) \ -// DeclarePacket(PlayerJoin, Reliable, Server) \ -// DeclarePacket(PlayerLeave, Reliable, Server) \ -// DeclarePacket(PlayerLogin, Reliable, Client) \ - - -// } // namespace protocol -// } // namespace sp \ No newline at end of file diff --git a/include/sp/protocol/packet/PacketDispatcher.h b/include/sp/protocol/packet/PacketDispatcher.h deleted file mode 100644 index de04f58..0000000 --- a/include/sp/protocol/packet/PacketDispatcher.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -/** - * \file PacketDispatcher.h - * \brief File containing the sp::protocol::PacketDispatcher class - */ - -#include -#include - -namespace sp { -namespace protocol { - -using PacketDispatcher = Dispatcher; - -} // namespace protocol -} // namespace sp diff --git a/include/sp/protocol/packet/PacketFactory.h b/include/sp/protocol/packet/PacketFactory.h deleted file mode 100644 index fc085e7..0000000 --- a/include/sp/protocol/packet/PacketFactory.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#include -#include - -namespace sp { -namespace protocol { -namespace PacketFactory { - -template ::value>::type> -std::unique_ptr CreatePacket() { - return std::make_unique(); -} - -const std::unique_ptr& CreateReadOnlyPacket(PacketType a_Type); - -} // namespace PacketFactory -} // namespace protocol -} // namespace sp diff --git a/include/sp/protocol/packet/PacketSerializer.h b/include/sp/protocol/packet/PacketSerializer.h deleted file mode 100644 index 972299a..0000000 --- a/include/sp/protocol/packet/PacketSerializer.h +++ /dev/null @@ -1,21 +0,0 @@ -#pragma once - -#include -#include - -namespace sp { -namespace protocol { - -class Packet; - -using PacketPtr = std::unique_ptr; - -namespace PacketSerializer { - -DataBuffer Serialize(const Packet& a_Packet); - -std::unique_ptr Deserialize(DataBuffer& a_Data); - -} // namespace PacketSerializer -} // namespace protocol -} // namespace sp diff --git a/include/sp/protocol/packet/PacketVisitor.h b/include/sp/protocol/packet/PacketVisitor.h deleted file mode 100644 index 1347153..0000000 --- a/include/sp/protocol/packet/PacketVisitor.h +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once - -/** - * \file PacketVisitor.h - * \brief File containing the sp::protocol::PacketVisitor class - */ - -#include - -namespace sp { -namespace protocol { - -#define DeclarePacket(PacketName, ...) \ - /** This function is called when the packet processed by PacketVisitor::Check is a PacketName */ \ - virtual void Visit(const packets::PacketName&) {} - -/** - * \class PacketVisitor - * \brief This class uses double-dispatch in order to find the real type of a packet - */ -class PacketVisitor : private NonCopyable { - protected: - PacketVisitor() {} - virtual ~PacketVisitor() {} - - public: - /** - * \brief Calls the right PacketVisitor::Visit method corresponding to the real type of the packet - * \param packet the Packet to visit - */ - void Check(const Packet& packet); - - DeclareAllPacket() -}; - -#undef DeclarePacket - -} // namespace protocol -} // namespace sp diff --git a/include/sp/protocol/packet/Packets.h b/include/sp/protocol/packet/Packets.h deleted file mode 100644 index 524f9a0..0000000 --- a/include/sp/protocol/packet/Packets.h +++ /dev/null @@ -1,115 +0,0 @@ -#pragma once - -/** - * \file Packets.h - * \brief File containing the definitions of the packets - */ - -#include -#include -#include - -#include - -namespace sp { -namespace protocol { - -class PacketVisitor; - -/** A Packet id is 8 bits wide */ -using PacketID = std::uint8_t; -using PeerID = std::uint16_t; - -#define DeclarePacket(PacketName, ...) /** PacketName */ PacketName, - -/** - * \enum PacketType - * \brief Map a Packet to an id - */ -enum class PacketType : PacketID { - - DeclareAllPacket() - - /** The number of packets */ - PACKET_COUNT -}; - - -#undef DeclarePacket - - -class Packet : private NonCopyable { - public: - /** - * \return The real type of the packet - */ - virtual PacketType GetType() const = 0; - - /** - * \brief The network peer who sent the packet - */ - PeerID m_Sender; - - private: - /** Use a PacketVisitor to make double-dispatch possible */ - virtual void Accept(PacketVisitor& a_Visitor) const = 0; - - friend class PacketVisitor; -}; - - - - - -namespace packets { - -/** - * \class ConcretePacket - * \brief A Packet associated with an id and holding data - * \tparam PT The packet type - * \tparam Data The structure holding the data of the packet (in sp::protocol::data namespace) - */ -template -class ConcretePacket : public Packet { - public: - /** The type of the struct holding the data */ - using PacketDataType = Data; - - /** The structure holding the actual data */ - PacketDataType m_Data; - - /** Construct the packet with data of type PacketDataType */ - ConcretePacket(const PacketDataType& a_Data = {}); - - constexpr PacketType GetType() const override { - return PT; - }; - - private: - void Accept(PacketVisitor& a_Visitor) const override; -}; - - - - - -// define SP_INSTANCIATE_PACKETS -// before including this file -// if you want to instantiate templates -#ifdef SP_INSTANCIATE_PACKETS -#define DeclarePacket(PacketName, ...) \ - using PacketName = ConcretePacket; \ - template class ConcretePacket; -#else -#define DeclarePacket(PacketName, ...) /** Defines the PacketName packet */ \ - using PacketName = ConcretePacket; -#endif - -// DeclareAllPacket() - -#undef DeclarePacket - -} // namespace packets - -} // namespace protocol -} // namespace sp diff --git a/src/main.cpp b/src/main.cpp index c1e10b4..f154b8f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,21 +1,125 @@ #include -// #include -#include -#include -#include -#include -#include -class Test : public sp::protocol::CommandVisitor {}; +#include +#include +#include -int main(int argc, char** argv) { - // Test visitor; - // sp::protocol::packets::ChatMessage chat{{"coucou"}}; - // visitor.Check(chat); +#define PacketClass(className) class className : public sp::MessageBase + +template +class Packet : public sp::MessageBase { + public: + virtual std::string_view GetName() const = 0; + void WriteImpl(sp::DataBuffer& buffer) const = 0; + void ReadImpl(const sp::DataBuffer& buffer) = 0; + int GetId() const = 0; +}; + +template +class Command : public sp::MessageBase { + public: + virtual std::string_view GetName() const = 0; + void WriteImpl(sp::DataBuffer& buffer) const = 0; + void ReadImpl(const sp::DataBuffer& buffer) = 0; + int GetId() const = 0; +}; - td::protocol::commands::UpgradeTower com{{1, 2}}; - std::cout << (unsigned)com.GetType() << std::endl; - td::protocol::CommandDispatcher disptacher; +#define TestPacket(packetName) \ + class packetName : public Packet { \ + public: \ + std::string_view GetName() const { \ + return #packetName; \ + } \ + void WriteImpl(sp::DataBuffer& buffer) const {} \ + void ReadImpl(const sp::DataBuffer& buffer) {} \ + int GetId() const { \ + return 0; \ + } \ + } + +#define TestCommand(commandName) \ + class commandName : public Command { \ + public: \ + std::string_view GetName() const { \ + return #commandName; \ + } \ + void WriteImpl(sp::DataBuffer& buffer) const {} \ + void ReadImpl(const sp::DataBuffer& buffer) {} \ + int GetId() const { \ + return 0; \ + } \ + } + +TestPacket(ChatPacket); +TestPacket(PlayerJoinPacket); +TestPacket(PlayerLeavePacket); + +TestCommand(SpawnTroopCommand); +TestCommand(PlaceTowerCommand); + +class ActualMessage2 : public sp::MessageBase { + std::string_view GetName() const { + return "mesmes"; + } + void WriteImpl(sp::DataBuffer& buffer) const {} + void ReadImpl(const sp::DataBuffer& buffer) {} + int GetId() const { + return 0; + } +}; + +using AllPackets = std::tuple; +using AllCommands = std::tuple; +using AllTests = std::tuple; + +using AllMessages = sp::tuple_cat_t; + +namespace sp { +class Handler : public sp::GenericHandler {}; +} // namespace sp + +using PacketHandler = sp::Handler; +using CommandHandler = sp::Handler; + +class PacketPrinter : public PacketHandler { + public: + void Handle(ChatPacket& packet) override { + std::cout << packet.GetName() << std::endl; + } + + // void Handle(SpawnTroopCommand& cmd) { + // std::cout << "NOOOOOO\n"; + // } +}; + + + +class ActualHandler1 : public sp::Handler { + public: + virtual void Handle(ActualMessage2& msg) override { + std::cout << "Handling ActualMessage2" << std::endl; + } + + virtual void Handle(sp::Message& msg) override { + std::cout << "Common handling function is invoked" << std::endl; + } + + virtual void Handle(ChatPacket& msg) override { + std::cout << "Chat invoked" << std::endl; + } +}; + + +int main() { + std::unique_ptr msg = std::make_unique(); + std::unique_ptr chat = std::make_unique(); + SpawnTroopCommand cmd; + PacketPrinter packetHandler; + PacketHandler pp; + chat->Dispatch(packetHandler); + ActualHandler1 handler; + msg->Dispatch(handler); + chat->Dispatch(handler); return 0; -} +} \ No newline at end of file diff --git a/src/sp/protocol/command/CommandFactory.cpp b/src/sp/protocol/command/CommandFactory.cpp deleted file mode 100644 index ad621d8..0000000 --- a/src/sp/protocol/command/CommandFactory.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include - -#include -#include -#include - -namespace sp { -namespace protocol { -namespace CommandFactory { - -using CommandCreator = std::function()>; - -#define DeclareCommand(CommandName, ...) std::make_shared(), - -static std::array, static_cast(CommandType::COMMAND_COUNT)> Commands = {DeclareAllCommand()}; - -const std::shared_ptr& CreateReadOnlyCommand(CommandType a_Type) { - assert(a_Type < CommandType::COMMAND_COUNT); - return Commands[static_cast(a_Type)]; -} - -} // namespace CommandFactory -} // namespace protocol -} // namespace sp diff --git a/src/sp/protocol/command/CommandSerializer.cpp b/src/sp/protocol/command/CommandSerializer.cpp deleted file mode 100644 index fb3ef49..0000000 --- a/src/sp/protocol/command/CommandSerializer.cpp +++ /dev/null @@ -1,229 +0,0 @@ -#include - -#include -#include - -#include -#include - -namespace sp { -namespace protocol { -namespace CommandSerializer { - -#define DeclareCommand(CommandName, ...) \ - void Visit(const commands::CommandName& a_Command) override { \ - const auto& commandData = a_Command.m_Data; \ - SerializeCommandData(commandData); \ - } \ - \ - void SerializeCommandData(const commands::CommandName::CommandDataType& a_Command); - - - - -class Serializer : public CommandVisitor { - private: - DataBuffer& m_Buffer; - - public: - Serializer(DataBuffer& a_Buffer) : m_Buffer(a_Buffer) {} - - void Serialize(const Command& a_Command) { - m_Buffer << static_cast(a_Command.GetType()); - Check(a_Command); - } - - DeclareAllCommand() -}; - -#undef DeclareCommand - - - - - -#define DeclareCommand(CommandName, ...) \ - void Visit(const commands::CommandName& a_Command) override { \ - auto commandPtr = CommandFactory::CreateCommand(); \ - auto& commandData = commandPtr->m_Data; \ - \ - DeserializeCommandData(commandData); \ - \ - m_Command = std::move(commandPtr); \ - } \ - \ - void DeserializeCommandData(commands::CommandName::CommandDataType& a_Command); - - - -class Deserializer : public CommandVisitor { - private: - DataBuffer& m_Buffer; - CommandPtr m_Command; - - public: - Deserializer(DataBuffer& a_Buffer) : m_Buffer(a_Buffer) {} - - bool Deserialize(const CommandPtr& a_Command) { - try { - Check(*a_Command.get()); - } catch (std::exception& e) { - utils::LOGE(utils::Format("[CommandSerializer::Deserializer] %s", e.what())); - return false; - } - return true; - } - - CommandPtr& GetCommand() { - return m_Command; - } - - DeclareAllCommand() -}; - - - - - -DataBuffer Serialize(const Command& a_Command) { - DataBuffer buffer; - - Serializer serializer(buffer); - serializer.Serialize(a_Command); - - return buffer; -} - -std::shared_ptr Deserialize(DataBuffer& a_Data) { - CommandID commandId; - a_Data >> commandId; - - if (commandId >= static_cast(CommandType::COMMAND_COUNT)) - return nullptr; - - CommandType commandType = CommandType(commandId); - - // for double-dispatch - const CommandPtr& emptyCommand = CommandFactory::CreateReadOnlyCommand(commandType); - - Deserializer deserializer(a_Data); - - if (deserializer.Deserialize(emptyCommand)) { - CommandPtr command = std::move(deserializer.GetCommand()); - return command; - } - - return nullptr; -} - - - - - -//--------------------------------------------- -// Command serializer implementation -//---------------------------------------------- - - - - - -void Serializer::SerializeCommandData(const cdata::End& a_Command) {} - -void Deserializer::DeserializeCommandData(cdata::End& a_Command) {} - - - - - -void Serializer::SerializeCommandData(const cdata::PlaceTower& a_Command) { - m_Buffer << static_cast((static_cast(a_Command.m_Type) << 4 | a_Command.m_Placer & 0xF)) - << a_Command.m_Position; -} - -void Deserializer::DeserializeCommandData(cdata::PlaceTower& a_Command) { - std::uint8_t union1; - m_Buffer >> union1 >> a_Command.m_Position; - a_Command.m_Type = sp::TowerType(union1 >> 4); - a_Command.m_Placer = union1 & 0xF; -} - - - - - -void Serializer::SerializeCommandData(const cdata::PlayerJoin& a_Command) { - m_Buffer << a_Command.m_ID << a_Command.m_Name; -} - -void Deserializer::DeserializeCommandData(cdata::PlayerJoin& a_Command) { - m_Buffer >> a_Command.m_ID >> a_Command.m_Name; -} - - - - -void Serializer::SerializeCommandData(const cdata::SpawnTroop& a_Command) { - m_Buffer << static_cast(static_cast(a_Command.m_Type) << 3 | a_Command.m_Level & 0x7) - << a_Command.m_Position << a_Command.m_Sender; -} - -void Deserializer::DeserializeCommandData(cdata::SpawnTroop& a_Command) { - std::uint8_t union1; - m_Buffer >> union1 >> a_Command.m_Position >> a_Command.m_Sender; - a_Command.m_Type = sp::EntityType(union1 >> 3); - a_Command.m_Level = union1 & 0x7; -} - - - - - -void Serializer::SerializeCommandData(const cdata::TeamChange& a_Command) { - m_Buffer << static_cast(a_Command.m_Player << 1 | static_cast(a_Command.m_NewTeam)); -} - -void Deserializer::DeserializeCommandData(cdata::TeamChange& a_Command) { - std::uint8_t union1; - m_Buffer >> union1; - a_Command.m_Player = union1 >> 1; - a_Command.m_NewTeam = sp::Team(union1 & 1); -} - - - - - -void Serializer::SerializeCommandData(const cdata::UpgradeTower& a_Command) { - m_Buffer << static_cast(a_Command.m_Tower << 4 | a_Command.m_Upgrade & 0xF); -} - -void Deserializer::DeserializeCommandData(cdata::UpgradeTower& a_Command) { - std::uint16_t union1; - m_Buffer >> union1; - a_Command.m_Tower = union1 >> 4; - a_Command.m_Upgrade = union1 & 0xF; -} - - - - - -void Serializer::SerializeCommandData(const cdata::UseItem& a_Command) { - m_Buffer << static_cast(static_cast(a_Command.m_Item) << 4 | a_Command.m_User & 0xF) - << a_Command.m_Position; -} - -void Deserializer::DeserializeCommandData(cdata::UseItem& a_Command) { - std::uint8_t union1; - m_Buffer >> union1 >> a_Command.m_Position; - a_Command.m_Item = sp::ShopItem(union1 >> 4); - a_Command.m_User = union1 & 0xF; -} - - - - -} // namespace CommandSerializer -} // namespace protocol -} // namespace sp diff --git a/src/sp/protocol/command/CommandVisitor.cpp b/src/sp/protocol/command/CommandVisitor.cpp deleted file mode 100644 index d4e6bda..0000000 --- a/src/sp/protocol/command/CommandVisitor.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include - -namespace sp { -namespace protocol { - -void CommandVisitor::Check(const Command& a_Command) { - a_Command.Accept(*this); -} - -} // namespace protocol -} // namespace sp diff --git a/src/sp/protocol/command/Commands.cpp b/src/sp/protocol/command/Commands.cpp deleted file mode 100644 index 48d936d..0000000 --- a/src/sp/protocol/command/Commands.cpp +++ /dev/null @@ -1,18 +0,0 @@ -#define SP_INSTANCIATE_COMMANDS -#include - -#include - -namespace sp { -namespace protocol { - -template -commands::ConcreteCommand::ConcreteCommand(const CommandDataType& a_Data) : m_Data(a_Data) {} - -template -void commands::ConcreteCommand::Accept(CommandVisitor& a_Visitor) const { - a_Visitor.Visit(*this); -} - -} // namespace protocol -} // namespace sp \ No newline at end of file diff --git a/src/sp/protocol/packet/PacketFactory.cpp b/src/sp/protocol/packet/PacketFactory.cpp deleted file mode 100644 index f857a6d..0000000 --- a/src/sp/protocol/packet/PacketFactory.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include - -#include -#include -#include - -namespace sp { -namespace protocol { -namespace PacketFactory { - -using PacketCreator = std::function()>; - -#define DeclarePacket(PacketName, ...) std::make_unique(), - -static std::array, static_cast(PacketType::PACKET_COUNT)> Packets = { - DeclareAllPacket() -}; - -const std::unique_ptr& CreateReadOnlyPacket(PacketType a_Type) { - assert(a_Type < PacketType::PACKET_COUNT); - return Packets[static_cast(a_Type)]; -} - -} // namespace PacketFactory -} // namespace protocol -} // namespace sp diff --git a/src/sp/protocol/packet/PacketSerializer.cpp b/src/sp/protocol/packet/PacketSerializer.cpp deleted file mode 100644 index 42753d8..0000000 --- a/src/sp/protocol/packet/PacketSerializer.cpp +++ /dev/null @@ -1,262 +0,0 @@ -#include - -#include -#include -#include - -#include -#include - -namespace sp { -namespace protocol { -namespace PacketSerializer { - -#define DeclarePacket(PacketName, ...) \ - void Visit(const packets::PacketName& a_Packet) override { \ - const auto& packetData = a_Packet.m_Data; \ - SerializePacketData(packetData); \ - } \ - \ - void SerializePacketData(const packets::PacketName::PacketDataType& a_Packet); - - - - -class Serializer : public PacketVisitor { - private: - DataBuffer& m_Buffer; - - public: - Serializer(DataBuffer& a_Buffer) : m_Buffer(a_Buffer) {} - - void Serialize(const Packet& a_Packet) { - m_Buffer << static_cast(a_Packet.GetType()); - Check(a_Packet); - } - - DeclareAllPacket() -}; - -#undef DeclarePacket - - - - - -#define DeclarePacket(PacketName, ...) \ - void Visit(const packets::PacketName& a_Packet) override { \ - auto packetPtr = PacketFactory::CreatePacket(); \ - auto& packetData = packetPtr->m_Data; \ - \ - DeserializePacketData(packetData); \ - \ - m_Packet = std::move(packetPtr); \ - } \ - \ - void DeserializePacketData(packets::PacketName::PacketDataType& a_Packet); - - - -class Deserializer : public PacketVisitor { - private: - DataBuffer& m_Buffer; - PacketPtr m_Packet; - - public: - Deserializer(DataBuffer& a_Buffer) : m_Buffer(a_Buffer) {} - - bool Deserialize(const PacketPtr& a_Packet) { - try { - Check(*a_Packet.get()); - } catch (std::exception& e) { - utils::LOGE(utils::Format("[PacketSerializer::Deserializer] %s", e.what())); - return false; - } - return true; - } - - PacketPtr& GetPacket() { - return m_Packet; - } - - DeclareAllPacket() -}; - - - - - -DataBuffer Serialize(const Packet& a_Packet) { - DataBuffer buffer; - - Serializer serializer(buffer); - serializer.Serialize(a_Packet); - - return buffer; -} - -std::unique_ptr Deserialize(DataBuffer& a_Data) { - PacketID packetId; - a_Data >> packetId; - - if (packetId >= static_cast(PacketType::PACKET_COUNT)) - return nullptr; - - PacketType packetType = PacketType(packetId); - - // for double-dispatch - const PacketPtr& emptyPacket = PacketFactory::CreateReadOnlyPacket(packetType); - - Deserializer deserializer(a_Data); - - if (deserializer.Deserialize(emptyPacket)) { - PacketPtr packet = std::move(deserializer.GetPacket()); - return packet; - } - - return nullptr; -} - - - - - -//--------------------------------------------- -// Packet serializer implementation -//---------------------------------------------- - - - - - -void Serializer::SerializePacketData(const pdata::PlayerLogin& a_Packet) { - m_Buffer << a_Packet.m_PlayerName; -} - -void Deserializer::DeserializePacketData(pdata::PlayerLogin& a_Packet) { - m_Buffer >> a_Packet.m_PlayerName; -} - - - - - -void Serializer::SerializePacketData(const pdata::LoggingSuccess& a_Packet) { - m_Buffer << a_Packet.m_PlayerId; -} - -void Deserializer::DeserializePacketData(pdata::LoggingSuccess& a_Packet) { - m_Buffer >> a_Packet.m_PlayerId; -} - - - - - -void Serializer::SerializePacketData(const pdata::PlayerJoin& a_Packet) { - m_Buffer << a_Packet.m_Player; -} - -void Deserializer::DeserializePacketData(pdata::PlayerJoin& a_Packet) { - m_Buffer >> a_Packet.m_Player; -} - - - - -void Serializer::SerializePacketData(const pdata::PlayerLeave& a_Packet) { - m_Buffer << a_Packet.m_PlayerId; -} - -void Deserializer::DeserializePacketData(pdata::PlayerLeave& a_Packet) { - m_Buffer >> a_Packet.m_PlayerId; -} - - - - - -void Serializer::SerializePacketData(const pdata::KeepAlive& a_Packet) { - m_Buffer << a_Packet.m_KeepAliveId; -} - -void Deserializer::DeserializePacketData(pdata::KeepAlive& a_Packet) { - m_Buffer >> a_Packet.m_KeepAliveId; -} - - - - - -void Serializer::SerializePacketData(const pdata::Disconnect& a_Packet) { - m_Buffer << a_Packet.m_Reason; -} - -void Deserializer::DeserializePacketData(pdata::Disconnect& a_Packet) { - m_Buffer >> a_Packet.m_Reason; -} - - - - - -void Serializer::SerializePacketData(const pdata::ChatMessage& a_Packet) { - m_Buffer << a_Packet.m_Text; -} - -void Deserializer::DeserializePacketData(pdata::ChatMessage& a_Packet) { - m_Buffer >> a_Packet.m_Text; -} - - - - - -void Serializer::SerializePacketData(const pdata::LockSteps& a_Packet) { - m_Buffer << a_Packet.m_FirstFrameNumber; - for (int i = 0; i < LOCKSTEP_BUFFER_SIZE; i++) { - auto& lockStep = a_Packet.m_LockSteps[i]; - m_Buffer << VarInt{lockStep.size()}; - - for (int j = 0; j < lockStep.size(); i++) { - auto command = lockStep[j]; - m_Buffer << CommandSerializer::Serialize(*command); - } - } -} - -void Deserializer::DeserializePacketData(pdata::LockSteps& a_Packet) { - m_Buffer >> a_Packet.m_FirstFrameNumber; - for (int i = 0; i < LOCKSTEP_BUFFER_SIZE; i++) { - VarInt lockStepSize; - m_Buffer >> lockStepSize; - - for (std::size_t j = 0; j < lockStepSize.GetValue(); i++) { - auto command = CommandSerializer::Deserialize(m_Buffer); - if (command) { - a_Packet.m_LockSteps[i].push_back(command); - } else { - throw std::runtime_error("Failed to deserialise command"); - } - } - } -} - - - - - -void Serializer::SerializePacketData(const pdata::BeginGame& a_Packet) { - m_Buffer << a_Packet.m_Map << a_Packet.m_BlueTeam << a_Packet.m_RedTeam << a_Packet.m_Map; -} - -void Deserializer::DeserializePacketData(pdata::BeginGame& a_Packet) { - m_Buffer >> a_Packet.m_Map >> a_Packet.m_BlueTeam >> a_Packet.m_RedTeam >> a_Packet.m_Map; -} - - - - -} // namespace PacketSerializer -} // namespace protocol -} // namespace sp diff --git a/src/sp/protocol/packet/PacketVisitor.cpp b/src/sp/protocol/packet/PacketVisitor.cpp deleted file mode 100644 index ca7e918..0000000 --- a/src/sp/protocol/packet/PacketVisitor.cpp +++ /dev/null @@ -1,11 +0,0 @@ -#include - -namespace sp { -namespace protocol { - -void PacketVisitor::Check(const Packet& a_Packet) { - a_Packet.Accept(*this); -} - -} // namespace protocol -} // namespace sp diff --git a/src/sp/protocol/packet/Packets.cpp b/src/sp/protocol/packet/Packets.cpp deleted file mode 100644 index 4449807..0000000 --- a/src/sp/protocol/packet/Packets.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#define SP_INSTANCIATE_PACKETS -#include - -#include - -namespace sp { -namespace protocol { - -template -packets::ConcretePacket::ConcretePacket(const PacketDataType& a_Data) : m_Data(a_Data) {} - -template -void packets::ConcretePacket::Accept(PacketVisitor& a_Visitor) const { - a_Visitor.Visit(*this); -} - -#define DeclarePacket(PacketName, packetSendType, packetSenderType) \ - static_assert(static_cast(PacketSendType::packetSendType) && static_cast(PacketSenderType::packetSenderType)); - -DeclareAllPacket() - -} // namespace protocol -} // namespace sp diff --git a/test/sp/protocol/command/CommandFactory_test.cpp b/test/sp/protocol/command/CommandFactory_test.cpp deleted file mode 100644 index d1b12c8..0000000 --- a/test/sp/protocol/command/CommandFactory_test.cpp +++ /dev/null @@ -1,17 +0,0 @@ -// #include - -// #include - -// static int Test() { -// for (std::size_t i = 0; i < static_cast(td::protocol::CommandType::COMMAND_COUNT); i++) { -// td::protocol::CommandType commandType = sp::protocol::CommandType(i); - -// if (td::protocol::CommandFactory::CreateReadOnlyCommand(commandType)->GetType() != commandType) -// return SP_TEST_FAILED; -// } -// return SP_TEST_SUCCESSFUL; -// } - -// int main() { -// return Test(); -// } \ No newline at end of file diff --git a/test/sp/protocol/command/CommandSerializer_test.cpp b/test/sp/protocol/command/CommandSerializer_test.cpp deleted file mode 100644 index 41f59fd..0000000 --- a/test/sp/protocol/command/CommandSerializer_test.cpp +++ /dev/null @@ -1,96 +0,0 @@ -// #include - -// #include -// #include - -// namespace tp = sp::protocol; - -// class Comparator { -// public: -// bool operator()(const tp::cdata::End& left, const tp::cdata::End& right) { -// return true; -// } -// bool operator()(const tp::cdata::PlaceTower& left, const tp::cdata::PlaceTower& right) { -// return left.m_Placer == right.m_Placer && left.m_Position.x == right.m_Position.x && left.m_Position.y == right.m_Position.y && -// left.m_Type == right.m_Type; -// } -// bool operator()(const tp::cdata::PlayerJoin& left, const tp::cdata::PlayerJoin& right) { -// return left.m_ID == right.m_ID && left.m_Name == right.m_Name; -// } -// bool operator()(const tp::cdata::SpawnTroop& left, const tp::cdata::SpawnTroop& right) { -// return left.m_Level == right.m_Level && left.m_Position.x == right.m_Position.x && left.m_Position.y == right.m_Position.y && -// left.m_Sender == right.m_Sender && left.m_Type == right.m_Type; -// } -// bool operator()(const tp::cdata::TeamChange& left, const tp::cdata::TeamChange& right) { -// return left.m_NewTeam == right.m_NewTeam && left.m_Player == right.m_Player; -// } -// bool operator()(const tp::cdata::UpgradeTower& left, const tp::cdata::UpgradeTower& right) { -// return left.m_Tower == right.m_Tower && left.m_Upgrade == right.m_Upgrade; -// } -// bool operator()(const tp::cdata::UseItem& left, const tp::cdata::UseItem& right) { -// return left.m_Item == right.m_Item && left.m_Position.x == right.m_Position.x && left.m_Position.y == right.m_Position.y && -// left.m_User == right.m_User; -// } -// }; - -// template -// static int TestCommand(const Command_T& command) { - -// td::DataBuffer buffer = tp::CommandSerializer::Serialize(command); - -// auto abstractCommand = tp::CommandSerializer::Deserialize(buffer); - -// td_test_assert(abstractCommand); - -// Command_T* command2 = dynamic_cast(abstractCommand.get()); - -// td_test_assert(command2); -// td_test_assert(command.GetType() == command2->GetType()); - -// return Comparator{}(command.m_Data, command2->m_Data); -// } - -// #define DeclareCommand(Command, ...) sp_test_assert(TestCommand({})); - -// static int TestAllCommands() { -// td_test_assert(TestCommand({})); -// td_test_assert(TestCommand({tp::cdata::PlaceTower{ -// td::TowerType::Necromancer, -// 8, -// td::TowerCoords{-50, 69}, -// }})); -// td_test_assert(TestCommand({tp::cdata::PlayerJoin{ -// 4, -// "Persson", -// }})); -// td_test_assert(TestCommand({tp::cdata::SpawnTroop{ -// td::EntityType::Blaze, -// 4, -// td::EntityCoords{td::FpFloat{54}, sp::FpFloat{58}}, -// 2, -// }})); -// td_test_assert(TestCommand({tp::cdata::TeamChange{ -// 7, -// td::Team::Red, -// }})); -// td_test_assert(TestCommand({tp::cdata::UpgradeTower{ -// 69, -// 3, -// }})); -// td_test_assert(TestCommand({tp::cdata::UseItem{ -// td::ShopItem::Freeze, -// 5, -// td::EntityCoords{td::FpFloat{24}, sp::FpFloat{-69}}, -// }})); - -// DeclareAllCommand() - -// return SP_TEST_SUCCESSFUL; -// } - -// int main() { -// return TestAllCommands(); -// } -int main() { - return 0; -} \ No newline at end of file diff --git a/test/sp/protocol/packet/PacketFactory_test.cpp b/test/sp/protocol/packet/PacketFactory_test.cpp deleted file mode 100644 index e9d7306..0000000 --- a/test/sp/protocol/packet/PacketFactory_test.cpp +++ /dev/null @@ -1,17 +0,0 @@ -// #include - -// #include - -// static int Test() { -// for (std::size_t i = 0; i < static_cast(td::protocol::PacketType::PACKET_COUNT); i++) { -// td::protocol::PacketType packetType = sp::protocol::PacketType(i); - -// if (td::protocol::PacketFactory::CreateReadOnlyPacket(packetType)->GetType() != packetType) -// return SP_TEST_FAILED; -// } -// return SP_TEST_SUCCESSFUL; -// } - -// int main() { -// return Test(); -// } \ No newline at end of file diff --git a/test/sp/protocol/packet/PacketSerializer_test.cpp b/test/sp/protocol/packet/PacketSerializer_test.cpp deleted file mode 100644 index 75d5e42..0000000 --- a/test/sp/protocol/packet/PacketSerializer_test.cpp +++ /dev/null @@ -1,36 +0,0 @@ -// #include - -// #include -// #include - -// namespace tp = sp::protocol; - -// template -// static int TestPacket() { -// Packet_T packet; - -// td::DataBuffer buffer = tp::PacketSerializer::Serialize(packet); - -// auto abstractPacket = tp::PacketSerializer::Deserialize(buffer); - -// td_test_assert(abstractPacket); - -// Packet_T* packet2 = dynamic_cast(abstractPacket.get()); - -// td_test_assert(packet2); -// td_test_assert(packet.GetType() == packet2->GetType()); - -// return std::memcmp(&packet.m_Data, &packet2->m_Data, sizeof(Packet_Data_T)); -// } - -// #define DeclarePacket(Packet, ...) TestPacket(); - -// static int TestAllPackets() { -// DeclareAllPacket() - -// return TD_TEST_SUCCESSFUL; -// } - -// int main() { -// return TestAllPackets(); -// } \ No newline at end of file diff --git a/xmake.lua b/xmake.lua index 7f9c2c6..71fed3a 100644 --- a/xmake.lua +++ b/xmake.lua @@ -6,7 +6,7 @@ add_requires("enet6") target("SimpleProtocolLib") add_includedirs("include", {public = true}) - set_kind("static") + set_kind("binary") add_files("src/**.cpp") add_packages("enet6", {public = true})