first commit

This commit is contained in:
2025-02-04 19:11:03 +01:00
commit cfeea10634
43 changed files with 2448 additions and 0 deletions

9
include/sp/Types.h Normal file
View File

@@ -0,0 +1,9 @@
#pragma once
#include <cstdint>
namespace sp {
using PeerID = std::uint16_t;
} // namespace sp

View File

@@ -0,0 +1,300 @@
#pragma once
/**
* \file DataBuffer.h
* \brief File containing the sp::DataBuffer class
*/
#include <algorithm>
#include <array>
#include <cassert>
#include <cstdint>
#include <cstring>
#include <string>
#include <sp/common/VarInt.h>
#include <vector>
namespace sp {
/**
* \class DataBuffer
* \brief Class used to manipulate memory
*/
class DataBuffer {
private:
typedef std::vector<std::uint8_t> Data;
Data m_Buffer;
std::size_t m_ReadOffset;
public:
typedef Data::iterator iterator;
typedef Data::const_iterator const_iterator;
typedef Data::reference reference;
typedef Data::const_reference const_reference;
typedef Data::difference_type difference_type;
DataBuffer();
DataBuffer(const DataBuffer& other);
DataBuffer(const DataBuffer& other, difference_type offset);
DataBuffer(DataBuffer&& other);
DataBuffer(const std::string& str);
DataBuffer& operator=(const DataBuffer& other);
DataBuffer& operator=(DataBuffer&& other);
/**
* \brief Append data to the buffer
*/
template <typename T>
void Append(const T& data) {
std::size_t size = sizeof(data);
std::size_t end_pos = m_Buffer.size();
m_Buffer.resize(m_Buffer.size() + size);
std::memcpy(&m_Buffer[end_pos], &data, size);
}
/**
* \brief Append data to the buffer
*/
template <typename T>
DataBuffer& operator<<(const T& data) {
Append(data);
return *this;
}
/**
* \brief Append a string to the buffer
* \warning Don't use it for binary data !
* \param str The string to append
*/
DataBuffer& operator<<(const std::string& str);
/**
* \brief Append data to the buffer from another const buffer
* \param data The buffer to append
*/
DataBuffer& operator<<(const DataBuffer& data);
/**
* \brief Append a vector to the buffer by first writing the size
* \param data The vector to append
*/
template <typename T>
DataBuffer& operator<<(const std::vector<T>& data) {
*this << VarInt{data.size()};
for (const auto& element : data) {
*this << element;
}
return *this;
}
/**
* \brief Append an array to the buffer by first writing the size
* \param data The buffer to append
*/
template <typename T, std::size_t Size>
DataBuffer& operator<<(const std::array<T, Size>& data) {
for (const auto& element : data) {
*this << element;
}
return *this;
}
/**
* \brief Read some data from the buffer and assign to desired variable
*/
template <typename T>
DataBuffer& operator>>(T& data) {
assert(m_ReadOffset + sizeof(T) <= GetSize());
data = *(reinterpret_cast<T*>(&m_Buffer[m_ReadOffset]));
m_ReadOffset += sizeof(T);
return *this;
}
/**
* \brief Read some data from the buffer and assign to the new buffer
* \param data The buffer to assign
*/
DataBuffer& operator>>(DataBuffer& data);
/**
* \brief Read a string from the buffer
* \param str The string to assign in the new buffer
* \warning Don't use it for binary data !
*/
DataBuffer& operator>>(std::string& str);
/**
* \brief Read a vector (size + data) from the buffer
* \pre The vector is assumed to be empty
*/
template <typename T>
DataBuffer& operator>>(std::vector<T>& data) {
VarInt arraySize;
*this >> arraySize;
for (std::size_t i = 0; i < arraySize.GetValue(); i++) {
T newElement;
*this >> newElement;
data.push_back(newElement);
}
return *this;
}
/**
* \brief Read an array from the buffer
*/
template <std::size_t Size, typename T>
DataBuffer& operator>>(std::array<T, Size>& data) {
for (std::size_t i = 0; i < Size; i++) {
T newElement;
*this >> newElement;
data[i] = newElement;
}
return *this;
}
/**
* \brief Write some data to the buffer
* \param buffer The buffer to write
* \param amount The amount of data to write
*/
void WriteSome(const char* buffer, std::size_t amount);
/**
* \brief Write some data to the buffer
* \param buffer The buffer to write
* \param amount The amount of data to write
*/
void WriteSome(const std::uint8_t* buffer, std::size_t amount);
/**
* \brief Read some data from the buffer
* \param buffer The buffer to Read
* \param amount The amount of data from the buffer
*/
void ReadSome(char* buffer, std::size_t amount);
/**
* \brief Read some data from the buffer
* \param buffer The buffer to Read
* \param amount The amount of data from the buffer
*/
void ReadSome(std::uint8_t* buffer, std::size_t amount);
/**
* \brief Read some data from the buffer
* \param buffer The buffer to Read
* \param amount The amount of data from the buffer
*/
void ReadSome(DataBuffer& buffer, std::size_t amount);
/**
* \brief Resize the buffer
* \param size The new size of the buffer
*/
void Resize(std::size_t size) {
m_Buffer.resize(size);
}
/**
* \brief Reserve some space in the buffer
* \param amount The amount of space to reserve
*/
void Reserve(std::size_t amount) {
m_Buffer.reserve(amount);
}
/**
* \brief Clear the buffer
*/
void Clear() {
m_Buffer.clear();
m_ReadOffset = 0;
}
/**
* \brief When the buffer has been read entirely
*/
bool IsFinished() const {
return m_ReadOffset >= m_Buffer.size();
}
/**
* \brief Get the buffer data
*/
std::uint8_t* data() {
return m_Buffer.data();
}
/**
* \brief Get the buffer data
*/
const std::uint8_t* data() const {
return m_Buffer.data();
}
/**
* \brief Get the read offset
*/
std::size_t GetReadOffset() const {
return m_ReadOffset;
}
/**
* \brief Set the read offset
* \param pos The new read offset
*/
void SetReadOffset(std::size_t pos);
/**
* \brief Get the size of the buffer
*/
std::size_t GetSize() const;
/**
* \brief Get the remaining size of the buffer
*/
std::size_t GetRemaining() const;
/**
* \brief Read a file into the buffer
* \param fileName The name of the file to read
*/
bool ReadFile(const std::string& fileName);
/**
* \brief Write a file into the buffer
* \param fileName The name of the file to write to
*/
bool WriteFile(const std::string& fileName) const;
/**
* \brief Allocate the buffer on the heap
* \warning Don't forget to free the data !
*/
std::uint8_t* HeapAllocatedData() const {
std::uint8_t* newBuffer = new std::uint8_t[GetSize()];
std::memcpy(newBuffer, data(), GetSize());
return newBuffer;
}
/**
* \brief Operator == to compare two DataBuffer
*/
bool operator==(const DataBuffer& other) const {
return m_Buffer == other.m_Buffer;
}
iterator begin();
iterator end();
const_iterator begin() const;
const_iterator end() const;
};
/**
* \brief Operator << to write a DataBuffer to an ostream
*/
std::ostream& operator<<(std::ostream& os, const DataBuffer& buffer);
} // namespace sp

View File

@@ -0,0 +1,25 @@
#pragma once
/**
* \file NonCopyable.h
* \brief File containing the sp::NonCopyable class
*/
namespace sp {
/**
* \class NonCopyable
* \brief Class used to make a class non copyable
* \note Inherit from this class privately to make a class non copyable
*/
class NonCopyable {
public:
NonCopyable(const NonCopyable&) = delete;
NonCopyable& operator=(const NonCopyable&) = delete;
protected:
NonCopyable() {}
~NonCopyable() {}
};
} // namespace sp

View File

@@ -0,0 +1,58 @@
#pragma once
/**
* \file VarInt.h
* \brief File containing the sp::VarInt class
*/
#include <cstddef>
#include <cstdint>
namespace sp {
class DataBuffer;
/**
* \class VarInt
* \brief Variable-length format such that smaller numbers use fewer bytes.
*/
class VarInt {
private:
std::uint64_t m_Value;
public:
VarInt() : m_Value(0) {}
/**
* \brief Construct a variable integer from a value
* \param value The value of the variable integer
*/
VarInt(std::uint64_t value) : m_Value(value) {}
/**
* \brief Get the value of the variable integer
*/
std::uint64_t GetValue() const {
return m_Value;
}
/**
* \brief Get the length of the serialized variable integer
*/
std::size_t GetSerializedLength() const;
/**
* \brief Serialize the variable integer
* \param out The buffer to write the serialized variable integer to
* \param var The variable integer to serialize
*/
friend DataBuffer& operator<<(DataBuffer& out, const VarInt& var);
/**
* \brief Deserialize the variable integer
* \param in The buffer to read the serialized variable integer from
* \param var The variable integer to deserialize
*/
friend DataBuffer& operator>>(DataBuffer& in, VarInt& var);
};
} // namespace sp

38
include/sp/misc/Format.h Normal file
View File

@@ -0,0 +1,38 @@
#pragma once
/**
* \file Format.h
* \brief This file contains the definition of the `Format` function.
*/
#include <memory>
#include <stdexcept>
#include <string>
namespace sp {
namespace utils {
/**
* \brief Formats a string using a format string and variadic arguments.
*
* This function takes a format string and a variable number of arguments and returns a formatted string.
* The format string can contain placeholders that will be replaced by the corresponding arguments.
*
* \param format The format string.
* \param args The variadic arguments to be formatted.
* \return The formatted string.
* \throws std::runtime_error if an error occurs during formatting.
*/
template <typename... Args>
std::string Format(const std::string& format, Args... args) {
int size = snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
if (size <= 0) {
throw std::runtime_error("Error during formatting.");
}
std::unique_ptr<char[]> buf(new char[size]);
snprintf(buf.get(), static_cast<std::size_t>(size), format.c_str(), args...);
return std::string(buf.get(), buf.get() + size - 1); // We don't want the '\0' inside
}
} // namespace utils
} // namespace sp

32
include/sp/misc/Log.h Normal file
View File

@@ -0,0 +1,32 @@
#pragma once
/**
* \file Log.h
* \brief File defining log functions
*/
#include <string>
namespace sp {
namespace utils {
/**
* \brief Logs a normal message.
* \param msg The message to be logged.
*/
void LOG(const std::string& msg);
/**
* \brief Logs a normal message in debug mode.
* \param msg The message to be logged.
*/
void LOGD(const std::string& msg);
/**
* \brief Logs an error message.
* \param err The error message to be logged.
*/
void LOGE(const std::string& err);
} // namespace utils
} // namespace sp

70
include/sp/misc/Test.h Normal file
View File

@@ -0,0 +1,70 @@
#pragma once
/**
* \file Test.h
* \brief File containing unit testing utilities
*/
#include <cstdlib>
#include <sp/misc/Log.h>
namespace sp {
namespace test {
/**
* \def SP_TEST_SUCCESSFUL
* \brief Used in tests to indicate that a test was successful
*/
#define SP_TEST_SUCCESSFUL 0
/**
* \def SP_TEST_FAILED
* \brief Used in tests to indicate that a test failed
*/
#define SP_TEST_FAILED 1
#ifndef __FUNCTION_NAME__
#ifdef _WIN32
#define __FUNCTION_NAME__ __FUNCTION__
#else
#define __FUNCTION_NAME__ __PRETTY_FUNCTION__
#endif
#endif
/**
* \def blitz_test_assert
* \param ... The expression to evaluate
* \brief Evaluates the expression and exits the program if not valid.
* \note It works like a basic assert() but also in release mode
*/
#define sp_test_assert(...) \
if (!static_cast<bool>(__VA_ARGS__)) { \
td::test::LogAssert(#__VA_ARGS__, __FILE__, __LINE__, __FUNCTION_NAME__); \
std::exit(SP_TEST_FAILED); \
}
/**
* \def blitz_debug_assert
* \param ... The expression to execute
* \brief Assertion without checks in release mode
* \note The expression is always executed. However, in release, no checks are made !
*/
#ifdef NDEBUG
#define sp_debug_assert(...) __VA_ARGS__
#else
#define sp_debug_assert sp_test_assert
#endif
/**
* \brief Prints an error message associated with a failed assertion
* \param expression The expression that was tested
* \param file The file in which the assertion failed
* \param line The line in the file in which the assertion failed
* \param function The function in which the assertion failed
*/
void LogAssert(const char* expression, const char* file, int line, const char* function);
} // namespace test
} // namespace sp

View File

@@ -0,0 +1,61 @@
#pragma once
/**
* \file PacketDispatcher.h
* \brief File containing the sp::protocol::PacketDispatcher class
*/
#include <sp/common/NonCopyable.h>
#include <sp/protocol/packet/Packets.h>
#include <map>
namespace sp {
namespace protocol {
/**
* \class Dispatcher
* \brief Class used to dispatch things
*/
template <typename T_Enum, typename T_Handler, typename T>
class Dispatcher : private NonCopyable {
private:
std::map<T_Enum, std::vector<T_Handler*>> m_Handlers;
public:
/**
* \brief Constructor
*/
Dispatcher() {}
/**
* \brief Dispatch a packet
* \param packet The packet to dispatch
*/
void Dispatch(const T& packet);
/**
* \brief Register a packet handler
* \param type The packet type
* \param handler The packet handler
*/
void RegisterHandler(T_Enum type, T_Handler& handler);
/**
* \brief Unregister a packet handler
* \param type The packet type
* \param handler The packet handler
*/
void UnregisterHandler(T_Enum type, T_Handler& handler);
/**
* \brief Unregister a packet handler
* \param handler The packet handler
*/
void UnregisterHandler(T_Handler& handler);
};
} // namespace protocol
} // namespace sp
#include "Dispatcher.inl"

View File

@@ -0,0 +1,38 @@
#pragma once
namespace sp {
namespace protocol {
template <typename T_Enum, typename T_Handler, typename T>
void Dispatcher<T_Enum, T_Handler, T>::Dispatch(const T& packet) {
T_Enum type = packet.GetType();
for (auto* handler : m_Handlers[type])
handler->Check(packet);
}
template <typename T_Enum, typename T_Handler, typename T>
void Dispatcher<T_Enum, T_Handler, T>::RegisterHandler(T_Enum type, T_Handler& 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);
}
template <typename T_Enum, typename T_Handler, typename T>
void Dispatcher<T_Enum, T_Handler, T>::UnregisterHandler(T_Enum type, T_Handler& handler) {
m_Handlers[type].erase(std::remove(m_Handlers[type].begin(), m_Handlers[type].end(), &handler), m_Handlers[type].end());
}
template <typename T_Enum, typename T_Handler, typename T>
void Dispatcher<T_Enum, T_Handler, T>::UnregisterHandler(T_Handler& 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());
}
}
} // namespace protocol
} // namespace sp

View File

@@ -0,0 +1,53 @@
// #pragma once
// #include <array>
// #include <string>
// #include <sp/Types.h>
// 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

View File

@@ -0,0 +1,22 @@
#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

View File

@@ -0,0 +1,17 @@
#pragma once
/**
* \file CommandDispatcher.h
* \brief File containing the sp::protocol::CommandDispatcher class
*/
#include <sp/protocol/Dispatcher.h>
#include <sp/protocol/command/Commands.h>
namespace sp {
namespace protocol {
using CommandDispatcher = Dispatcher<CommandType, CommandVisitor, Command>;
} // namespace protocol
} // namespace sp

View File

@@ -0,0 +1,19 @@
#pragma once
#include <memory>
#include <sp/protocol/command/Commands.h>
namespace sp {
namespace protocol {
namespace CommandFactory {
template <typename CommandDerived, typename = typename std::enable_if<std::is_base_of<Command, CommandDerived>::value>::type>
std::shared_ptr<CommandDerived> CreateCommand() {
return std::make_shared<CommandDerived>();
}
const std::shared_ptr<Command>& CreateReadOnlyCommand(CommandType a_Type);
} // namespace CommandFactory
} // namespace protocol
} // namespace sp

View File

@@ -0,0 +1,21 @@
#pragma once
#include <memory>
#include <sp/common/DataBuffer.h>
namespace sp {
namespace protocol {
class Command;
using CommandPtr = std::shared_ptr<Command>;
namespace CommandSerializer {
DataBuffer Serialize(const Command& a_Command);
std::shared_ptr<Command> Deserialize(DataBuffer& a_Data);
} // namespace CommandSerializer
} // namespace protocol
} // namespace sp

View File

@@ -0,0 +1,39 @@
#pragma once
/**
* \file CommandVisitor.h
* \brief File containing the sp::protocol::CommandVisitor class
*/
#include <sp/protocol/command/Commands.h>
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

View File

@@ -0,0 +1,111 @@
#pragma once
/**
* \file Commands.h
* \brief File containing the definitions of the lockstep commands
*/
#include <sp/Types.h>
#include <sp/common/NonCopyable.h>
#include <sp/protocol/command/CommandData.h>
#include <sp/protocol/command/CommandDeclare.h>
#include <memory>
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 <CommandType CT, typename Data>
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<CommandType::CommandName, cdata::CommandName>; \
template class ConcreteCommand<CommandType::CommandName, cdata::CommandName>;
#else
#define DeclareCommand(CommandName, ...) /** Defines the CommandName Command */ \
using CommandName = ConcreteCommand<CommandType::CommandName, cdata::CommandName>;
#endif
DeclareAllCommand()
#undef DeclareCommand
} // namespace commands
using LockStep = std::vector<std::shared_ptr<protocol::Command>>;
} // namespace protocol
} // namespace sp

View File

@@ -0,0 +1,76 @@
// #pragma once
// #include <td/Types.h>
// #include <vector>
// #include <td/protocol/command/Commands.h>
// // 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<PlayerInfo> m_BlueTeam;
// std::vector<PlayerInfo> m_RedTeam;
// // optional, used for players joining mid game
// std::vector<LockStep> m_FirstLocksteps;
// };
// struct LockSteps {
// std::uint16_t m_FirstFrameNumber;
// std::array<LockStep, LOCKSTEP_BUFFER_SIZE> m_LockSteps;
// };
// } // namespace pdata
// } // namespace protocol
// } // namespace sp

View File

@@ -0,0 +1,48 @@
// #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

View File

@@ -0,0 +1,17 @@
#pragma once
/**
* \file PacketDispatcher.h
* \brief File containing the sp::protocol::PacketDispatcher class
*/
#include <sp/protocol/Dispatcher.h>
#include <sp/protocol/packet/Packets.h>
namespace sp {
namespace protocol {
using PacketDispatcher = Dispatcher<PacketType, PacketVisitor, Packet>;
} // namespace protocol
} // namespace sp

View File

@@ -0,0 +1,19 @@
#pragma once
#include <memory>
#include <sp/protocol/packet/Packets.h>
namespace sp {
namespace protocol {
namespace PacketFactory {
template <typename PacketDerived, typename = typename std::enable_if<std::is_base_of<Packet, PacketDerived>::value>::type>
std::unique_ptr<PacketDerived> CreatePacket() {
return std::make_unique<PacketDerived>();
}
const std::unique_ptr<Packet>& CreateReadOnlyPacket(PacketType a_Type);
} // namespace PacketFactory
} // namespace protocol
} // namespace sp

View File

@@ -0,0 +1,21 @@
#pragma once
#include <memory>
#include <sp/common/DataBuffer.h>
namespace sp {
namespace protocol {
class Packet;
using PacketPtr = std::unique_ptr<Packet>;
namespace PacketSerializer {
DataBuffer Serialize(const Packet& a_Packet);
std::unique_ptr<Packet> Deserialize(DataBuffer& a_Data);
} // namespace PacketSerializer
} // namespace protocol
} // namespace sp

View File

@@ -0,0 +1,39 @@
#pragma once
/**
* \file PacketVisitor.h
* \brief File containing the sp::protocol::PacketVisitor class
*/
#include <sp/protocol/packet/Packets.h>
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

View File

@@ -0,0 +1,115 @@
#pragma once
/**
* \file Packets.h
* \brief File containing the definitions of the packets
*/
#include <sp/common/NonCopyable.h>
#include <sp/protocol/packet/PacketData.h>
#include <sp/protocol/packet/PacketDeclare.h>
#include <cstdint>
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 <PacketType PT, typename Data>
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<PacketType::PacketName, pdata::PacketName>; \
template class ConcretePacket<PacketType::PacketName, pdata::PacketName>;
#else
#define DeclarePacket(PacketName, ...) /** Defines the PacketName packet */ \
using PacketName = ConcretePacket<PacketType::PacketName, pdata::PacketName>;
#endif
// DeclareAllPacket()
#undef DeclarePacket
} // namespace packets
} // namespace protocol
} // namespace sp