Compare commits
9 Commits
v2.2.1
...
e16ad84865
| Author | SHA1 | Date | |
|---|---|---|---|
| e16ad84865 | |||
| c1e6409c18 | |||
| 3ad18cacf6 | |||
| 2eab50932f | |||
| baa52d3baa | |||
| d924685e0c | |||
| 044b12cdec | |||
| e39f8de898 | |||
| 0b28bde25b |
@@ -10,4 +10,11 @@ enum DisconnectPacketFields {
|
|||||||
|
|
||||||
using DisconnectFields = std::tuple<std::string /*Reason*/>;
|
using DisconnectFields = std::tuple<std::string /*Reason*/>;
|
||||||
|
|
||||||
DeclarePacket(Disconnect);
|
DeclarePacket(Disconnect){
|
||||||
|
public:
|
||||||
|
PacketConstructor(Disconnect)
|
||||||
|
|
||||||
|
const std::string& GetReason() const {
|
||||||
|
return GetField<Reason>();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|||||||
@@ -5,9 +5,19 @@
|
|||||||
#include <sp/protocol/MessageBase.h>
|
#include <sp/protocol/MessageBase.h>
|
||||||
|
|
||||||
enum KeepAlivePacketFields {
|
enum KeepAlivePacketFields {
|
||||||
KeepAliveId = 0
|
KeepAliveId = 0,
|
||||||
|
TestAlignField = 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
using KeepAliveFields = std::tuple<std::uint64_t /*KeepAliveId*/>;
|
using KeepAliveFields = std::tuple<
|
||||||
|
std::uint64_t //<- KeepAliveId
|
||||||
|
>;
|
||||||
|
|
||||||
DeclarePacket(KeepAlive);
|
DeclarePacket(KeepAlive){
|
||||||
|
public:
|
||||||
|
PacketConstructor(KeepAlive)
|
||||||
|
|
||||||
|
std::uint64_t GetKeepAliveId() const {
|
||||||
|
return GetField<KeepAlive>();
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -3,13 +3,15 @@
|
|||||||
enum PacketId {
|
enum PacketId {
|
||||||
KeepAlive = 0,
|
KeepAlive = 0,
|
||||||
Disconnect,
|
Disconnect,
|
||||||
|
UpgradeTower,
|
||||||
};
|
};
|
||||||
|
|
||||||
#include <examples/KeepAlivePacket.h>
|
#include <examples/KeepAlivePacket.h>
|
||||||
#include <examples/DisconnectPacket.h>
|
#include <examples/DisconnectPacket.h>
|
||||||
|
#include <examples/UpgradeTowerPacket.h>
|
||||||
|
|
||||||
// they must be in the same order !
|
// they must be in the same order as in the enum !
|
||||||
using AllPackets = std::tuple<KeepAlivePacket, DisconnectPacket>;
|
using AllPackets = std::tuple<KeepAlivePacket, DisconnectPacket, UpgradeTowerPacket>;
|
||||||
|
|
||||||
#include <sp/default/DefaultPacketHandler.h>
|
#include <sp/default/DefaultPacketHandler.h>
|
||||||
#include <sp/default/DefaultPacketFactory.h>
|
#include <sp/default/DefaultPacketFactory.h>
|
||||||
30
include/examples/UpgradeTowerPacket.h
Normal file
30
include/examples/UpgradeTowerPacket.h
Normal 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>();
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -261,13 +261,13 @@ class DataBuffer {
|
|||||||
* \brief Read a file into the buffer
|
* \brief Read a file into the buffer
|
||||||
* \param fileName The name of the file to read
|
* \param fileName The name of the file to read
|
||||||
*/
|
*/
|
||||||
bool ReadFile(const std::string& fileName);
|
void ReadFile(const std::string& fileName);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Write a file into the buffer
|
* \brief Write a file into the buffer
|
||||||
* \param fileName The name of the file to write to
|
* \param fileName The name of the file to write to
|
||||||
*/
|
*/
|
||||||
bool WriteFile(const std::string& fileName) const;
|
void WriteFile(const std::string& fileName) const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Allocate the buffer on the heap
|
* \brief Allocate the buffer on the heap
|
||||||
|
|||||||
@@ -1,54 +0,0 @@
|
|||||||
/*
|
|
||||||
* 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
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
#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
|
|
||||||
@@ -13,15 +13,17 @@ using PacketMessage = Message<option::MsgIdType<std::uint8_t>, // add id() opera
|
|||||||
option::Handler<PacketHandler> // add dispatch() operation
|
option::Handler<PacketHandler> // add dispatch() operation
|
||||||
>;
|
>;
|
||||||
|
|
||||||
#define DeclarePacket(packetName) \
|
#define PacketConstructor(packetName) \
|
||||||
class packetName##Packet : public sp::MessageBase<sp::PacketMessage, sp::option::StaticNumIdImpl<packetName>, \
|
packetName##Packet() {} \
|
||||||
sp::option::DispatchImpl<packetName##Packet>, sp::option::FieldsImpl<packetName##Fields>> { \
|
template <typename... Args> \
|
||||||
public: \
|
packetName##Packet(Args... args) { \
|
||||||
packetName##Packet() {} \
|
Construct(args...); \
|
||||||
template <typename... Args> \
|
|
||||||
packetName##Packet(Args... args) { \
|
|
||||||
Construct(args...); \
|
|
||||||
} \
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define DeclarePacket(packetName) \
|
||||||
|
class packetName##Packet : public sp::MessageBase<sp::PacketMessage, sp::option::StaticNumIdImpl<packetName>, \
|
||||||
|
sp::option::DispatchImpl<packetName##Packet>, sp::option::FieldsImpl<packetName##Fields>>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace sp
|
} // namespace sp
|
||||||
|
|||||||
@@ -1,38 +0,0 @@
|
|||||||
#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
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
#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
|
|
||||||
@@ -1,70 +0,0 @@
|
|||||||
#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
|
|
||||||
@@ -40,6 +40,11 @@ class BitField {
|
|||||||
return std::get<FIndex>(this->GetFields()).GetValue();
|
return std::get<FIndex>(this->GetFields()).GetValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <std::size_t FIndex>
|
||||||
|
const auto& GetField() const {
|
||||||
|
return std::get<FIndex>(this->GetFields()).GetValue();
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
template <int IOffset, typename... T, std::enable_if_t<IOffset >= sizeof...(T), bool> = true>
|
template <int IOffset, typename... T, std::enable_if_t<IOffset >= sizeof...(T), bool> = true>
|
||||||
void Apply(const std::tuple<T...>& args) {}
|
void Apply(const std::tuple<T...>& args) {}
|
||||||
|
|||||||
@@ -23,26 +23,26 @@ namespace sp
|
|||||||
using Base = GenericHandler<TCommon, std::tuple<TRest...> >;
|
using Base = GenericHandler<TCommon, std::tuple<TRest...> >;
|
||||||
public:
|
public:
|
||||||
using Base::Handle; // Don't hide all Handle() functions from base classes
|
using Base::Handle; // Don't hide all Handle() functions from base classes
|
||||||
virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T1& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T2& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T3& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T3& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T4& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T4& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T5& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T5& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T6& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T6& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T7& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T7& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T8& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T8& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T9& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T9& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T10& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T10& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T11& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T11& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T12& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T12& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T13& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T13& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T14& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T14& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T15& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T15& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T16& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T16& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T17& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T17& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T18& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T18& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T19& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T19& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T20& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T20& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
};
|
};
|
||||||
|
|
||||||
// 10 by 10
|
// 10 by 10
|
||||||
@@ -55,16 +55,16 @@ namespace sp
|
|||||||
using Base = GenericHandler<TCommon, std::tuple<TRest...> >;
|
using Base = GenericHandler<TCommon, std::tuple<TRest...> >;
|
||||||
public:
|
public:
|
||||||
using Base::Handle; // Don't hide all Handle() functions from base classes
|
using Base::Handle; // Don't hide all Handle() functions from base classes
|
||||||
virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T1& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T2& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T3& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T3& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T4& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T4& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T5& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T5& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T6& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T6& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T7& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T7& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T8& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T8& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T9& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T9& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T10& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T10& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
};
|
};
|
||||||
|
|
||||||
// 5 by 5
|
// 5 by 5
|
||||||
@@ -76,11 +76,11 @@ namespace sp
|
|||||||
using Base = GenericHandler<TCommon, std::tuple<TRest...> >;
|
using Base = GenericHandler<TCommon, std::tuple<TRest...> >;
|
||||||
public:
|
public:
|
||||||
using Base::Handle; // Don't hide all Handle() functions from base classes
|
using Base::Handle; // Don't hide all Handle() functions from base classes
|
||||||
virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T1& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T2& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T3& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T3& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T4& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T4& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T5& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T5& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
};
|
};
|
||||||
|
|
||||||
// Deal with rest with 4 types
|
// Deal with rest with 4 types
|
||||||
@@ -89,11 +89,11 @@ namespace sp
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~GenericHandler() {}
|
virtual ~GenericHandler() {}
|
||||||
virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T1& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T2& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T3& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T3& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T4& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T4& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(TCommon&) { } //Nothing to do
|
virtual void Handle(const TCommon&) { } //Nothing to do
|
||||||
};
|
};
|
||||||
|
|
||||||
// Deal with rest with 3 types
|
// Deal with rest with 3 types
|
||||||
@@ -102,10 +102,10 @@ namespace sp
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~GenericHandler() {}
|
virtual ~GenericHandler() {}
|
||||||
virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T1& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T2& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T3& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T3& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(TCommon&) { } //Nothing to do
|
virtual void Handle(const TCommon&) { } //Nothing to do
|
||||||
};
|
};
|
||||||
|
|
||||||
// Deal with rest with 2 types
|
// Deal with rest with 2 types
|
||||||
@@ -114,9 +114,9 @@ namespace sp
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~GenericHandler() {}
|
virtual ~GenericHandler() {}
|
||||||
virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T1& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T2& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(TCommon&) { } //Nothing to do
|
virtual void Handle(const TCommon&) { } //Nothing to do
|
||||||
};
|
};
|
||||||
|
|
||||||
// Deal with rest with 1 type
|
// Deal with rest with 1 type
|
||||||
@@ -125,8 +125,8 @@ namespace sp
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~GenericHandler() {}
|
virtual ~GenericHandler() {}
|
||||||
virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
virtual void Handle(const T1& msg) { this->Handle(static_cast<const TCommon&>(msg)); }
|
||||||
virtual void Handle(TCommon&) { } //Nothing to do
|
virtual void Handle(const TCommon&) { } //Nothing to do
|
||||||
};
|
};
|
||||||
|
|
||||||
// Deal with rest with 0 type
|
// Deal with rest with 0 type
|
||||||
@@ -135,7 +135,7 @@ namespace sp
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~GenericHandler() {}
|
virtual ~GenericHandler() {}
|
||||||
virtual void Handle(TCommon&) { } //Nothing to do
|
virtual void Handle(const TCommon&) { } //Nothing to do
|
||||||
};
|
};
|
||||||
|
|
||||||
} // sp
|
} // sp
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
// Inspired by
|
// Inspired by
|
||||||
// https://alex-robenko.gitbook.io/comms-protocols-cpp
|
// https://alex-robenko.gitbook.io/comms-protocols-cpp
|
||||||
|
|
||||||
#include <sp/protocol/MessageInterfaceBuilder.h>
|
#include <sp/protocol/message/MessageInterfaceBuilder.h>
|
||||||
|
|
||||||
namespace sp {
|
namespace sp {
|
||||||
|
|
||||||
|
|||||||
@@ -114,6 +114,11 @@ class MessageImplFieldsBase : public TBase {
|
|||||||
return std::get<FIndex>(GetFields()).GetValue();
|
return std::get<FIndex>(GetFields()).GetValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <std::size_t FIndex>
|
||||||
|
const auto& GetField() const {
|
||||||
|
return std::get<FIndex>(GetFields()).GetValue();
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
AllFields m_Fields;
|
AllFields m_Fields;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,278 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <sp/protocol/Options.h>
|
|
||||||
#include <sp/common/ByteSwapping.h>
|
|
||||||
|
|
||||||
namespace sp {
|
|
||||||
namespace details {
|
|
||||||
|
|
||||||
template <typename... TOptions>
|
|
||||||
struct MessageInterfaceParsedOptions {};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct MessageInterfaceParsedOptions<> {
|
|
||||||
static const bool HasMsgIdType = false;
|
|
||||||
static const bool HasLittleEndian = false;
|
|
||||||
static const bool HasReadOperations = false;
|
|
||||||
static const bool HasWriteOperations = false;
|
|
||||||
static const bool HasWriteId = false;
|
|
||||||
static const bool HasHandler = false;
|
|
||||||
static const bool HasValid = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template <typename T, typename... TOptions>
|
|
||||||
struct MessageInterfaceParsedOptions<option::MsgIdType<T>, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
|
||||||
static const bool HasMsgIdType = true;
|
|
||||||
using MsgIdType = T;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename... TOptions>
|
|
||||||
struct MessageInterfaceParsedOptions<option::LittleEndian, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
|
||||||
static const bool HasLittleEndian = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename... TOptions>
|
|
||||||
struct MessageInterfaceParsedOptions<option::ReadOperations, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
|
||||||
static const bool HasReadOperations = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename... TOptions>
|
|
||||||
struct MessageInterfaceParsedOptions<option::WriteOperations, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
|
||||||
static const bool HasWriteOperations = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename... TOptions>
|
|
||||||
struct MessageInterfaceParsedOptions<option::WriteId, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
|
||||||
static const bool HasWriteId = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T, typename... TOptions>
|
|
||||||
struct MessageInterfaceParsedOptions<option::Handler<T>, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
|
||||||
static const bool HasHandler = true;
|
|
||||||
using HandlerType = option::Handler<T>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename... TOptions>
|
|
||||||
struct MessageInterfaceParsedOptions<option::ValidCheckInterface, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
|
||||||
static const bool HasValid = true;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ID retrieval chunk
|
|
||||||
template <typename TBase, typename TId>
|
|
||||||
class MessageInterfaceIdTypeBase : public TBase {
|
|
||||||
public:
|
|
||||||
using MsgIdType = TId;
|
|
||||||
MsgIdType GetId() const {
|
|
||||||
return GetIdImpl();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
virtual MsgIdType GetIdImpl() const = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Big endian serialisation chunk
|
|
||||||
template <typename TBase>
|
|
||||||
class MessageInterfaceBigEndian : public TBase {
|
|
||||||
protected:
|
|
||||||
template <typename T>
|
|
||||||
void ReadData(T& value, DataBuffer& buffer) {
|
|
||||||
buffer >> value;
|
|
||||||
FromNetwork(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
void WriteData(T value, DataBuffer& buffer) {
|
|
||||||
ToNetwork(value);
|
|
||||||
buffer << value;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Little endian serialisation chunk
|
|
||||||
template <typename TBase>
|
|
||||||
class MessageInterfaceLittleEndian : public TBase {
|
|
||||||
protected:
|
|
||||||
template <typename T>
|
|
||||||
void ReadData(T& value, DataBuffer& buffer) {
|
|
||||||
buffer >> value;
|
|
||||||
TrySwapBytes(value);
|
|
||||||
FromNetwork(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
void WriteData(const T& value, DataBuffer& buffer) {
|
|
||||||
ToNetwork(value);
|
|
||||||
TrySwapBytes(value);
|
|
||||||
buffer << value;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Read functionality chunk
|
|
||||||
template <typename TBase>
|
|
||||||
class MessageInterfaceReadBase : public TBase {
|
|
||||||
public:
|
|
||||||
void Read(DataBuffer& buffer) {
|
|
||||||
return ReadImpl(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
virtual void ReadImpl(DataBuffer& buffer) = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Write functionality chunk
|
|
||||||
template <typename TBase>
|
|
||||||
class MessageInterfaceWriteBase : public TBase {
|
|
||||||
public:
|
|
||||||
void Write(DataBuffer& buffer) {
|
|
||||||
WriteImpl(buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
virtual void WriteImpl(DataBuffer& buffer) = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Handler functionality chunk
|
|
||||||
template <typename TBase, typename THandler>
|
|
||||||
class MessageInterfaceHandlerBase : public TBase {
|
|
||||||
public:
|
|
||||||
using HandlerType = typename THandler::HandlerT;
|
|
||||||
|
|
||||||
void Dispatch(HandlerType& handler) {
|
|
||||||
DispatchImpl(handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
virtual void DispatchImpl(HandlerType& handler) = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Validity functionality chunk
|
|
||||||
template <typename TBase>
|
|
||||||
class MessageInterfaceValidityBase : public TBase {
|
|
||||||
public:
|
|
||||||
bool Valid() const {
|
|
||||||
return ValidImpl();
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
virtual bool ValidImpl() const = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Writing id functionality chunk
|
|
||||||
template <typename TBase>
|
|
||||||
class MessageInterfaceWriteIdBase : public TBase {
|
|
||||||
public:
|
|
||||||
void Write(DataBuffer& buffer) {
|
|
||||||
this->WriteData(this->GetId(), buffer);
|
|
||||||
this->WriteImpl(buffer);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Build message Id
|
|
||||||
template <typename TBase, typename TParsedOptions, bool THasMsgIdType>
|
|
||||||
struct MessageInterfaceProcessMsgId;
|
|
||||||
|
|
||||||
template <typename TBase, typename TParsedOptions>
|
|
||||||
struct MessageInterfaceProcessMsgId<TBase, TParsedOptions, true> {
|
|
||||||
using Type = MessageInterfaceIdTypeBase<TBase, typename TParsedOptions::MsgIdType>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename TBase, typename TParsedOptions>
|
|
||||||
struct MessageInterfaceProcessMsgId<TBase, TParsedOptions, false> {
|
|
||||||
using Type = TBase;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Build endianess
|
|
||||||
template <typename TBase, bool THasLittleEndian>
|
|
||||||
struct MessageInterfaceProcessEndian;
|
|
||||||
|
|
||||||
template <typename TBase>
|
|
||||||
struct MessageInterfaceProcessEndian<TBase, true> {
|
|
||||||
using Type = MessageInterfaceLittleEndian<TBase>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename TBase>
|
|
||||||
struct MessageInterfaceProcessEndian<TBase, false> {
|
|
||||||
using Type = MessageInterfaceBigEndian<TBase>;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Build read
|
|
||||||
template <typename TBase, bool THasRead>
|
|
||||||
struct MessageInterfaceProcessRead;
|
|
||||||
|
|
||||||
template <typename TBase>
|
|
||||||
struct MessageInterfaceProcessRead<TBase, true> {
|
|
||||||
using Type = MessageInterfaceReadBase<TBase>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename TBase>
|
|
||||||
struct MessageInterfaceProcessRead<TBase, false> {
|
|
||||||
using Type = TBase;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Build write
|
|
||||||
template <typename TBase, bool THasWrite>
|
|
||||||
struct MessageInterfaceProcessWrite;
|
|
||||||
|
|
||||||
template <typename TBase>
|
|
||||||
struct MessageInterfaceProcessWrite<TBase, true> {
|
|
||||||
using Type = MessageInterfaceWriteBase<TBase>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename TBase>
|
|
||||||
struct MessageInterfaceProcessWrite<TBase, false> {
|
|
||||||
using Type = TBase;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Build handler
|
|
||||||
template <typename TBase, typename TParsedOptions, bool THasHandler>
|
|
||||||
struct MessageInterfaceProcessHandler;
|
|
||||||
|
|
||||||
template <typename TBase, typename TParsedOptions>
|
|
||||||
struct MessageInterfaceProcessHandler<TBase, TParsedOptions, true> {
|
|
||||||
using Type = MessageInterfaceHandlerBase<TBase, typename TParsedOptions::HandlerType>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename TBase, typename TParsedOptions>
|
|
||||||
struct MessageInterfaceProcessHandler<TBase, TParsedOptions, false> {
|
|
||||||
using Type = TBase;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Build valid
|
|
||||||
template <typename TBase, bool THasValid>
|
|
||||||
struct MessageInterfaceProcessValid;
|
|
||||||
|
|
||||||
template <typename TBase>
|
|
||||||
struct MessageInterfaceProcessValid<TBase, true> {
|
|
||||||
using Type = MessageInterfaceValidityBase<TBase>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename TBase>
|
|
||||||
struct MessageInterfaceProcessValid<TBase, false> {
|
|
||||||
using Type = TBase;
|
|
||||||
};
|
|
||||||
|
|
||||||
// Build id writing
|
|
||||||
template <typename TBase, bool THasValid>
|
|
||||||
struct MessageInterfaceProcessWriteId;
|
|
||||||
|
|
||||||
template <typename TBase>
|
|
||||||
struct MessageInterfaceProcessWriteId<TBase, true> {
|
|
||||||
using Type = MessageInterfaceWriteIdBase<TBase>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename TBase>
|
|
||||||
struct MessageInterfaceProcessWriteId<TBase, false> {
|
|
||||||
using Type = TBase;
|
|
||||||
};
|
|
||||||
} // namespace details
|
|
||||||
} // namespace sp
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <sp/protocol/MessageInterfaces.h>
|
#include <sp/protocol/message/MessageInterfaces.h>
|
||||||
|
|
||||||
namespace sp {
|
namespace sp {
|
||||||
namespace details {
|
namespace details {
|
||||||
@@ -33,7 +33,7 @@ struct MessageInterfaceBuilder {
|
|||||||
// add write id functionality if write id and write was provided
|
// add write id functionality if write id and write was provided
|
||||||
using Base7 = typename MessageInterfaceProcessWriteId<Base6, ParsedOptions::HasWriteId && ParsedOptions::HasWriteOperations>::Type;
|
using Base7 = typename MessageInterfaceProcessWriteId<Base6, ParsedOptions::HasWriteId && ParsedOptions::HasWriteOperations>::Type;
|
||||||
|
|
||||||
// The last Base6 must be taken as final type.
|
// The last Base7 must be taken as final type.
|
||||||
using Type = Base7;
|
using Type = Base7;
|
||||||
};
|
};
|
||||||
|
|
||||||
105
include/sp/protocol/message/MessageInterfaceProcess.h
Normal file
105
include/sp/protocol/message/MessageInterfaceProcess.h
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
namespace details {
|
||||||
|
|
||||||
|
// Build message Id
|
||||||
|
template <typename TBase, typename TParsedOptions, bool THasMsgIdType>
|
||||||
|
struct MessageInterfaceProcessMsgId;
|
||||||
|
|
||||||
|
template <typename TBase, typename TParsedOptions>
|
||||||
|
struct MessageInterfaceProcessMsgId<TBase, TParsedOptions, true> {
|
||||||
|
using Type = MessageInterfaceIdTypeBase<TBase, typename TParsedOptions::MsgIdType>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TBase, typename TParsedOptions>
|
||||||
|
struct MessageInterfaceProcessMsgId<TBase, TParsedOptions, false> {
|
||||||
|
using Type = TBase;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Build endianess
|
||||||
|
template <typename TBase, bool THasLittleEndian>
|
||||||
|
struct MessageInterfaceProcessEndian;
|
||||||
|
|
||||||
|
template <typename TBase>
|
||||||
|
struct MessageInterfaceProcessEndian<TBase, true> {
|
||||||
|
using Type = MessageInterfaceLittleEndian<TBase>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TBase>
|
||||||
|
struct MessageInterfaceProcessEndian<TBase, false> {
|
||||||
|
using Type = MessageInterfaceBigEndian<TBase>;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Build read
|
||||||
|
template <typename TBase, bool THasRead>
|
||||||
|
struct MessageInterfaceProcessRead;
|
||||||
|
|
||||||
|
template <typename TBase>
|
||||||
|
struct MessageInterfaceProcessRead<TBase, true> {
|
||||||
|
using Type = MessageInterfaceReadBase<TBase>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TBase>
|
||||||
|
struct MessageInterfaceProcessRead<TBase, false> {
|
||||||
|
using Type = TBase;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Build write
|
||||||
|
template <typename TBase, bool THasWrite>
|
||||||
|
struct MessageInterfaceProcessWrite;
|
||||||
|
|
||||||
|
template <typename TBase>
|
||||||
|
struct MessageInterfaceProcessWrite<TBase, true> {
|
||||||
|
using Type = MessageInterfaceWriteBase<TBase>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TBase>
|
||||||
|
struct MessageInterfaceProcessWrite<TBase, false> {
|
||||||
|
using Type = TBase;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Build handler
|
||||||
|
template <typename TBase, typename TParsedOptions, bool THasHandler>
|
||||||
|
struct MessageInterfaceProcessHandler;
|
||||||
|
|
||||||
|
template <typename TBase, typename TParsedOptions>
|
||||||
|
struct MessageInterfaceProcessHandler<TBase, TParsedOptions, true> {
|
||||||
|
using Type = MessageInterfaceHandlerBase<TBase, typename TParsedOptions::HandlerType>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TBase, typename TParsedOptions>
|
||||||
|
struct MessageInterfaceProcessHandler<TBase, TParsedOptions, false> {
|
||||||
|
using Type = TBase;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Build valid
|
||||||
|
template <typename TBase, bool THasValid>
|
||||||
|
struct MessageInterfaceProcessValid;
|
||||||
|
|
||||||
|
template <typename TBase>
|
||||||
|
struct MessageInterfaceProcessValid<TBase, true> {
|
||||||
|
using Type = MessageInterfaceValidityBase<TBase>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TBase>
|
||||||
|
struct MessageInterfaceProcessValid<TBase, false> {
|
||||||
|
using Type = TBase;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Build id writing
|
||||||
|
template <typename TBase, bool THasValid>
|
||||||
|
struct MessageInterfaceProcessWriteId;
|
||||||
|
|
||||||
|
template <typename TBase>
|
||||||
|
struct MessageInterfaceProcessWriteId<TBase, true> {
|
||||||
|
using Type = MessageInterfaceWriteIdBase<TBase>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TBase>
|
||||||
|
struct MessageInterfaceProcessWriteId<TBase, false> {
|
||||||
|
using Type = TBase;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
|
} // namespace sp
|
||||||
7
include/sp/protocol/message/MessageInterfaces.h
Normal file
7
include/sp/protocol/message/MessageInterfaces.h
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <sp/protocol/message/MessageOptions.h>
|
||||||
|
|
||||||
|
#include <sp/protocol/message/MessageInterfacesOptions.h>
|
||||||
|
#include <sp/protocol/message/MessageInterfacesImpl.h>
|
||||||
|
#include <sp/protocol/message/MessageInterfaceProcess.h>
|
||||||
119
include/sp/protocol/message/MessageInterfacesImpl.h
Normal file
119
include/sp/protocol/message/MessageInterfacesImpl.h
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <sp/common/ByteSwapping.h>
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
namespace details {
|
||||||
|
|
||||||
|
|
||||||
|
// ID retrieval chunk
|
||||||
|
template <typename TBase, typename TId>
|
||||||
|
class MessageInterfaceIdTypeBase : public TBase {
|
||||||
|
public:
|
||||||
|
using MsgIdType = TId;
|
||||||
|
MsgIdType GetId() const {
|
||||||
|
return GetIdImpl();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual MsgIdType GetIdImpl() const = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Big endian serialisation chunk
|
||||||
|
template <typename TBase>
|
||||||
|
class MessageInterfaceBigEndian : public TBase {
|
||||||
|
protected:
|
||||||
|
template <typename T>
|
||||||
|
void ReadData(T& value, DataBuffer& buffer) {
|
||||||
|
buffer >> value;
|
||||||
|
FromNetwork(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void WriteData(T value, DataBuffer& buffer) {
|
||||||
|
ToNetwork(value);
|
||||||
|
buffer << value;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Little endian serialisation chunk
|
||||||
|
template <typename TBase>
|
||||||
|
class MessageInterfaceLittleEndian : public TBase {
|
||||||
|
protected:
|
||||||
|
template <typename T>
|
||||||
|
void ReadData(T& value, DataBuffer& buffer) {
|
||||||
|
buffer >> value;
|
||||||
|
TrySwapBytes(value);
|
||||||
|
FromNetwork(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void WriteData(const T& value, DataBuffer& buffer) {
|
||||||
|
ToNetwork(value);
|
||||||
|
TrySwapBytes(value);
|
||||||
|
buffer << value;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Read functionality chunk
|
||||||
|
template <typename TBase>
|
||||||
|
class MessageInterfaceReadBase : public TBase {
|
||||||
|
public:
|
||||||
|
void Read(DataBuffer& buffer) {
|
||||||
|
return ReadImpl(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void ReadImpl(DataBuffer& buffer) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Write functionality chunk
|
||||||
|
template <typename TBase>
|
||||||
|
class MessageInterfaceWriteBase : public TBase {
|
||||||
|
public:
|
||||||
|
void Write(DataBuffer& buffer) {
|
||||||
|
WriteImpl(buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void WriteImpl(DataBuffer& buffer) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Handler functionality chunk
|
||||||
|
template <typename TBase, typename THandler>
|
||||||
|
class MessageInterfaceHandlerBase : public TBase {
|
||||||
|
public:
|
||||||
|
using HandlerType = typename THandler::HandlerT;
|
||||||
|
|
||||||
|
void Dispatch(HandlerType& handler) {
|
||||||
|
DispatchImpl(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void DispatchImpl(HandlerType& handler) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Validity functionality chunk
|
||||||
|
template <typename TBase>
|
||||||
|
class MessageInterfaceValidityBase : public TBase {
|
||||||
|
public:
|
||||||
|
bool Valid() const {
|
||||||
|
return ValidImpl();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual bool ValidImpl() const = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Writing id functionality chunk
|
||||||
|
template <typename TBase>
|
||||||
|
class MessageInterfaceWriteIdBase : public TBase {
|
||||||
|
public:
|
||||||
|
void Write(DataBuffer& buffer) {
|
||||||
|
this->WriteData(this->GetId(), buffer);
|
||||||
|
this->WriteImpl(buffer);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
|
} // namespace sp
|
||||||
63
include/sp/protocol/message/MessageInterfacesOptions.h
Normal file
63
include/sp/protocol/message/MessageInterfacesOptions.h
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
namespace details {
|
||||||
|
|
||||||
|
|
||||||
|
template <typename... TOptions>
|
||||||
|
struct MessageInterfaceParsedOptions {};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct MessageInterfaceParsedOptions<> {
|
||||||
|
static const bool HasMsgIdType = false;
|
||||||
|
static const bool HasLittleEndian = false;
|
||||||
|
static const bool HasReadOperations = false;
|
||||||
|
static const bool HasWriteOperations = false;
|
||||||
|
static const bool HasWriteId = false;
|
||||||
|
static const bool HasHandler = false;
|
||||||
|
static const bool HasValid = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template <typename T, typename... TOptions>
|
||||||
|
struct MessageInterfaceParsedOptions<option::MsgIdType<T>, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||||
|
static const bool HasMsgIdType = true;
|
||||||
|
using MsgIdType = T;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename... TOptions>
|
||||||
|
struct MessageInterfaceParsedOptions<option::LittleEndian, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||||
|
static const bool HasLittleEndian = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename... TOptions>
|
||||||
|
struct MessageInterfaceParsedOptions<option::ReadOperations, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||||
|
static const bool HasReadOperations = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename... TOptions>
|
||||||
|
struct MessageInterfaceParsedOptions<option::WriteOperations, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||||
|
static const bool HasWriteOperations = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename... TOptions>
|
||||||
|
struct MessageInterfaceParsedOptions<option::WriteId, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||||
|
static const bool HasWriteId = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T, typename... TOptions>
|
||||||
|
struct MessageInterfaceParsedOptions<option::Handler<T>, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||||
|
static const bool HasHandler = true;
|
||||||
|
using HandlerType = option::Handler<T>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename... TOptions>
|
||||||
|
struct MessageInterfaceParsedOptions<option::ValidCheckInterface, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||||
|
static const bool HasValid = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
|
} // namespace sp
|
||||||
21
src/main.cpp
21
src/main.cpp
@@ -4,18 +4,23 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
class KeepAliveHandler : public sp::PacketHandler {
|
class KeepAliveHandler : public sp::PacketHandler {
|
||||||
void Handle(KeepAlivePacket& packet) {
|
void Handle(const KeepAlivePacket& packet) {
|
||||||
std::cout << "KeepAlive handled !\n";
|
std::cout << "KeepAlive handled !\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void Handle(DisconnectPacket& packet) {
|
void Handle(const DisconnectPacket& packet) {
|
||||||
std::cout << "Disconnect handled !\n";
|
std::cout << "Disconnect handled !\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Handle(const UpgradeTowerPacket& packet) {
|
||||||
|
std::cout << "UpgradeTower handled !\n";
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
auto keepAlive = std::make_unique<KeepAlivePacket>(69);
|
auto upgradeTower = std::make_unique<UpgradeTowerPacket>(std::make_tuple(666, 9));
|
||||||
sp::PacketMessage* msg = keepAlive.get();
|
|
||||||
|
sp::PacketMessage* msg = upgradeTower.get();
|
||||||
|
|
||||||
KeepAliveHandler handler;
|
KeepAliveHandler handler;
|
||||||
msg->Dispatch(handler);
|
msg->Dispatch(handler);
|
||||||
@@ -26,15 +31,15 @@ int main() {
|
|||||||
std::uint8_t msgId;
|
std::uint8_t msgId;
|
||||||
buffer >> msgId;
|
buffer >> msgId;
|
||||||
|
|
||||||
auto keepAlive2 = std::make_unique<KeepAlivePacket>();
|
auto upgradeTower2 = std::make_unique<UpgradeTowerPacket>();
|
||||||
keepAlive2->Read(buffer);
|
upgradeTower2->Read(buffer);
|
||||||
|
|
||||||
std::cout << "KeepAlive2 : " << keepAlive2->GetField<KeepAliveId>() << "\n";
|
std::cout << "Test : " << (unsigned) upgradeTower2->GetTowerId() << "\n";
|
||||||
|
|
||||||
sp::PacketFactory factory;
|
sp::PacketFactory factory;
|
||||||
auto packet = factory.CreateMessage(msgId);
|
auto packet = factory.CreateMessage(msgId);
|
||||||
if (packet == nullptr) {
|
if (packet == nullptr) {
|
||||||
std::cout << "Mauvais ID !\n";
|
std::cout << "Bad ID !\n";
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
std::cout << (unsigned)packet->GetId() << std::endl;
|
std::cout << (unsigned)packet->GetId() << std::endl;
|
||||||
|
|||||||
@@ -4,9 +4,6 @@
|
|||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
|
||||||
#include <sp/misc/Format.h>
|
|
||||||
#include <sp/misc/Log.h>
|
|
||||||
|
|
||||||
namespace sp {
|
namespace sp {
|
||||||
|
|
||||||
DataBuffer::DataBuffer() : m_ReadOffset(0) {}
|
DataBuffer::DataBuffer() : m_ReadOffset(0) {}
|
||||||
@@ -133,31 +130,19 @@ std::ostream& operator<<(std::ostream& os, const DataBuffer& buffer) {
|
|||||||
return os;
|
return os;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DataBuffer::ReadFile(const std::string& fileName) {
|
void DataBuffer::ReadFile(const std::string& fileName) {
|
||||||
try {
|
std::ifstream file(fileName, std::istream::binary);
|
||||||
std::ifstream file(fileName, std::istream::binary);
|
std::ostringstream ss;
|
||||||
std::ostringstream ss;
|
ss << file.rdbuf();
|
||||||
ss << file.rdbuf();
|
const std::string& s = ss.str();
|
||||||
const std::string& s = ss.str();
|
m_Buffer = DataBuffer::Data(s.begin(), s.end());
|
||||||
m_Buffer = DataBuffer::Data(s.begin(), s.end());
|
m_ReadOffset = 0;
|
||||||
m_ReadOffset = 0;
|
|
||||||
} catch (std::exception& e) {
|
|
||||||
utils::LOGE(utils::Format("[IO] Failed to read file %s ! reason : %s", fileName.c_str(), e.what()));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return m_Buffer.size() > 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DataBuffer::WriteFile(const std::string& fileName) const {
|
void DataBuffer::WriteFile(const std::string& fileName) const {
|
||||||
try {
|
std::ofstream file(fileName, std::ostream::binary);
|
||||||
std::ofstream file(fileName, std::ostream::binary);
|
file.write(reinterpret_cast<const char*>(m_Buffer.data()), static_cast<std::streamsize>(m_Buffer.size()));
|
||||||
file.write(reinterpret_cast<const char*>(m_Buffer.data()), static_cast<std::streamsize>(m_Buffer.size()));
|
file.flush();
|
||||||
file.flush();
|
|
||||||
} catch (std::exception& e) {
|
|
||||||
utils::LOGE(utils::Format("[IO] Failed to read file %s ! reason : %s", fileName.c_str(), e.what()));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace sp
|
} // namespace sp
|
||||||
|
|||||||
@@ -1,37 +0,0 @@
|
|||||||
#include <sp/misc/Log.h>
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef SP_ANDROID_LOGGING
|
|
||||||
#include <android/log.h>
|
|
||||||
#else
|
|
||||||
#include <iostream>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace sp {
|
|
||||||
namespace utils {
|
|
||||||
|
|
||||||
void LOG(const std::string& msg) {
|
|
||||||
#ifdef SP_ANDROID_LOGGING
|
|
||||||
__android_log_print(ANDROID_LOG_INFO, "TRACKERS", "%s", msg.c_str());
|
|
||||||
#else
|
|
||||||
std::cout << msg << "\n";
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void LOGD(const std::string& msg) {
|
|
||||||
#if !defined(NDEBUG)
|
|
||||||
LOG(msg);
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
void LOGE(const std::string& err) {
|
|
||||||
#ifdef SP_ANDROID_LOGGING
|
|
||||||
__android_log_print(ANDROID_LOG_ERROR, "TRACKERS", "%s", err.c_str());
|
|
||||||
#else
|
|
||||||
std::cerr << err << "\n";
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace utils
|
|
||||||
} // namespace sp
|
|
||||||
@@ -1,14 +0,0 @@
|
|||||||
#include <sp/misc/Format.h>
|
|
||||||
#include <sp/misc/Log.h>
|
|
||||||
#include <sp/misc/Test.h>
|
|
||||||
|
|
||||||
namespace sp {
|
|
||||||
namespace test {
|
|
||||||
|
|
||||||
void LogAssert(const char* expression, const char* file, int line, const char* function) {
|
|
||||||
utils::LOGE(utils::Format("%s:%i: %s: Assertion failed !", file, line, function));
|
|
||||||
utils::LOGE(utils::Format(" %i |\t%s;", line, expression));
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace test
|
|
||||||
} // namespace sp
|
|
||||||
Reference in New Issue
Block a user