Compare commits
5 Commits
044b12cdec
...
v1.0.2
| Author | SHA1 | Date | |
|---|---|---|---|
| 2acbd76c5a | |||
| 468f5ce8a0 | |||
| f145716cf6 | |||
| 6ee7524e17 | |||
| db0c5f3245 |
10
README.md
Normal file
10
README.md
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
# SimpleProtocolLib
|
||||||
|
|
||||||
|
Network engine used to (mainly) communicate with packets
|
||||||
|
|
||||||
|
# Integrate with xmake
|
||||||
|
|
||||||
|
```lua
|
||||||
|
add_repositories("persson-repo https://git.ale-pri.com/Persson-dev/xmake-repo.git")
|
||||||
|
add_requires("splib", { debug = is_mode("debug") })
|
||||||
|
```
|
||||||
@@ -4,7 +4,7 @@
|
|||||||
#include <sp/protocol/Field.h>
|
#include <sp/protocol/Field.h>
|
||||||
#include <sp/protocol/MessageBase.h>
|
#include <sp/protocol/MessageBase.h>
|
||||||
|
|
||||||
enum DisconnectPacketFields {
|
enum class DisconnectFieldsE {
|
||||||
Reason = 0
|
Reason = 0
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -15,6 +15,6 @@ DeclarePacket(Disconnect){
|
|||||||
PacketConstructor(Disconnect)
|
PacketConstructor(Disconnect)
|
||||||
|
|
||||||
const std::string& GetReason() const {
|
const std::string& GetReason() const {
|
||||||
return GetField<Reason>();
|
return GetField<DisconnectFieldsE, DisconnectFieldsE::Reason>();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,17 +4,12 @@
|
|||||||
#include <sp/protocol/Field.h>
|
#include <sp/protocol/Field.h>
|
||||||
#include <sp/protocol/MessageBase.h>
|
#include <sp/protocol/MessageBase.h>
|
||||||
|
|
||||||
enum KeepAlivePacketFields {
|
enum class KeepAliveFieldsE {
|
||||||
KeepAliveId = 0,
|
KeepAliveId = 0,
|
||||||
TestAlignField = 1,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
using KeepAliveFields = std::tuple<
|
using KeepAliveFields = std::tuple<
|
||||||
std::uint64_t, //<- KeepAliveId
|
std::uint64_t //<- KeepAliveId
|
||||||
sp::BitField<std::uint16_t,
|
|
||||||
sp::Field<std::uint16_t, 12>, //<- m_Tower
|
|
||||||
sp::Field<std::uint8_t, 4> //<- m_Upgrade
|
|
||||||
>
|
|
||||||
>;
|
>;
|
||||||
|
|
||||||
DeclarePacket(KeepAlive){
|
DeclarePacket(KeepAlive){
|
||||||
@@ -22,14 +17,6 @@ DeclarePacket(KeepAlive){
|
|||||||
PacketConstructor(KeepAlive)
|
PacketConstructor(KeepAlive)
|
||||||
|
|
||||||
std::uint64_t GetKeepAliveId() const {
|
std::uint64_t GetKeepAliveId() const {
|
||||||
return GetField<KeepAlive>();
|
return GetField<KeepAliveFieldsE, KeepAliveFieldsE::KeepAliveId>();
|
||||||
}
|
|
||||||
|
|
||||||
std::uint16_t GetTowerId() const {
|
|
||||||
return GetField<TestAlignField>().GetField<0>();
|
|
||||||
}
|
|
||||||
|
|
||||||
std::uint8_t GetTowerUpgrade() const {
|
|
||||||
return GetField<TestAlignField>().GetField<1>();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -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>
|
||||||
31
include/examples/UpgradeTowerPacket.h
Normal file
31
include/examples/UpgradeTowerPacket.h
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <sp/default/DefaultPacket.h>
|
||||||
|
#include <sp/protocol/Field.h>
|
||||||
|
#include <sp/protocol/MessageBase.h>
|
||||||
|
|
||||||
|
|
||||||
|
enum class UpgradeTowerFieldsE {
|
||||||
|
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<UpgradeTowerFieldsE, UpgradeTowerFieldsE::m_Tower>();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::uint8_t GetTowerUpgrade() const {
|
||||||
|
return GetField<0>().GetField<UpgradeTowerFieldsE, UpgradeTowerFieldsE::m_Upgrade>();
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -1,26 +1,10 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include <winsock2.h>
|
|
||||||
#else
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
namespace sp {
|
namespace sp {
|
||||||
|
|
||||||
template <typename T>
|
bool IsSystemBigEndian();
|
||||||
void SwapBytes(T& value) {
|
|
||||||
char* ptr = reinterpret_cast<char*>(&value);
|
|
||||||
std::reverse(ptr, ptr + sizeof(T));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IsSystemBigEndian() {
|
|
||||||
static constexpr std::uint16_t test = 10;
|
|
||||||
static const bool isBigEndian = reinterpret_cast<const std::uint8_t*>(&test)[1] == 10;
|
|
||||||
return isBigEndian;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Serialize value to (network byte order) big endian
|
* \brief Serialize value to (network byte order) big endian
|
||||||
@@ -29,21 +13,13 @@ template <typename T>
|
|||||||
void ToNetwork(T& value) {}
|
void ToNetwork(T& value) {}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void ToNetwork<std::uint16_t>(std::uint16_t& value) {
|
void ToNetwork<std::uint16_t>(std::uint16_t& value);
|
||||||
value = htons(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void ToNetwork<std::uint32_t>(std::uint32_t& value) {
|
void ToNetwork<std::uint32_t>(std::uint32_t& value);
|
||||||
value = htonl(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void ToNetwork<std::uint64_t>(std::uint64_t& value) {
|
void ToNetwork<std::uint64_t>(std::uint64_t& value);
|
||||||
if (IsSystemBigEndian())
|
|
||||||
return;
|
|
||||||
SwapBytes(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Deserialize value from (network byte order) big endian
|
* \brief Deserialize value from (network byte order) big endian
|
||||||
@@ -52,21 +28,13 @@ template <typename T>
|
|||||||
void FromNetwork(T& value) {}
|
void FromNetwork(T& value) {}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void FromNetwork<std::uint16_t>(std::uint16_t& value) {
|
void FromNetwork<std::uint16_t>(std::uint16_t& value);
|
||||||
value = ntohs(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void FromNetwork<std::uint32_t>(std::uint32_t& value) {
|
void FromNetwork<std::uint32_t>(std::uint32_t& value);
|
||||||
value = ntohl(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void FromNetwork<std::uint64_t>(std::uint64_t& value) {
|
void FromNetwork<std::uint64_t>(std::uint64_t& value);
|
||||||
if (IsSystemBigEndian())
|
|
||||||
return;
|
|
||||||
SwapBytes(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Swap bytes if the value is any kind of integer
|
* \brief Swap bytes if the value is any kind of integer
|
||||||
@@ -75,18 +43,12 @@ template <typename T>
|
|||||||
void TrySwapBytes(T& value) {}
|
void TrySwapBytes(T& value) {}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void TrySwapBytes<std::uint16_t>(std::uint16_t& value) {
|
void TrySwapBytes<std::uint16_t>(std::uint16_t& value);
|
||||||
SwapBytes(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void TrySwapBytes<std::uint32_t>(std::uint32_t& value) {
|
void TrySwapBytes<std::uint32_t>(std::uint32_t& value);
|
||||||
SwapBytes(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void TrySwapBytes<std::uint64_t>(std::uint64_t& value) {
|
void TrySwapBytes<std::uint64_t>(std::uint64_t& value);
|
||||||
SwapBytes(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace sp
|
} // namespace sp
|
||||||
|
|||||||
@@ -13,6 +13,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <sp/common/VarInt.h>
|
#include <sp/common/VarInt.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <map>
|
||||||
|
|
||||||
namespace sp {
|
namespace sp {
|
||||||
|
|
||||||
@@ -88,6 +89,19 @@ class DataBuffer {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Append a map to the buffer by first writing the size
|
||||||
|
* \param data The map to append
|
||||||
|
*/
|
||||||
|
template <typename K, typename V>
|
||||||
|
DataBuffer& operator<<(const std::map<K, V>& data) {
|
||||||
|
*this << VarInt{data.size()};
|
||||||
|
for (const auto& element : data) {
|
||||||
|
*this << element.first << element.second;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Append an array to the buffer by first writing the size
|
* \brief Append an array to the buffer by first writing the size
|
||||||
* \param data The buffer to append
|
* \param data The buffer to append
|
||||||
@@ -140,6 +154,23 @@ class DataBuffer {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Read a map (size + data) from the buffer
|
||||||
|
* \pre The map is assumed to be empty
|
||||||
|
*/
|
||||||
|
template <typename K, typename V>
|
||||||
|
DataBuffer& operator>>(std::map<K, V>& data) {
|
||||||
|
VarInt mapSize;
|
||||||
|
*this >> mapSize;
|
||||||
|
for (std::size_t i = 0; i < mapSize.GetValue(); i++) {
|
||||||
|
K newKey;
|
||||||
|
V newValue;
|
||||||
|
*this >> newKey >> newValue;
|
||||||
|
data.insert({newKey, newValue});
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Read an array from the buffer
|
* \brief Read an array from the buffer
|
||||||
*/
|
*/
|
||||||
@@ -261,13 +292,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
|
||||||
|
|||||||
@@ -6,7 +6,8 @@
|
|||||||
namespace sp {
|
namespace sp {
|
||||||
class PacketHandler;
|
class PacketHandler;
|
||||||
|
|
||||||
using PacketMessage = Message<option::MsgIdType<std::uint8_t>, // add id() operation
|
using PacketMessage = Message<
|
||||||
|
option::MsgIdType<std::uint8_t>, // add id() operation
|
||||||
option::ReadOperations, // add read() operation
|
option::ReadOperations, // add read() operation
|
||||||
option::WriteOperations, // add write() operation
|
option::WriteOperations, // add write() operation
|
||||||
option::WriteId, // write id before data
|
option::WriteId, // write id before data
|
||||||
|
|||||||
@@ -45,6 +45,12 @@ class BitField {
|
|||||||
return std::get<FIndex>(this->GetFields()).GetValue();
|
return std::get<FIndex>(this->GetFields()).GetValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// allow use of enums
|
||||||
|
template <typename E, E FIndex>
|
||||||
|
const auto& GetField() const {
|
||||||
|
return std::get<static_cast<std::size_t>(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) {}
|
||||||
@@ -108,6 +114,6 @@ template <typename TField, typename... TFields>
|
|||||||
struct FieldsBuilder<TField, TFields...> {
|
struct FieldsBuilder<TField, TFields...> {
|
||||||
using Type = sp::tuple_cat_t<std::tuple<Field<TField, 0>>, typename FieldsBuilder<TFields...>::Type>;
|
using Type = sp::tuple_cat_t<std::tuple<Field<TField, 0>>, typename FieldsBuilder<TFields...>::Type>;
|
||||||
};
|
};
|
||||||
} // namespace details
|
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
} // namespace sp
|
} // namespace sp
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -2,344 +2,15 @@
|
|||||||
|
|
||||||
#include <sp/protocol/Message.h>
|
#include <sp/protocol/Message.h>
|
||||||
|
|
||||||
|
#include <sp/protocol/message/MessageImplOptions.h>
|
||||||
|
|
||||||
|
#include <sp/protocol/message/MessagesImpl.h>
|
||||||
|
#include <sp/protocol/message/MessageImplProcess.h>
|
||||||
|
#include <sp/protocol/message/MessageImplBuilder.h>
|
||||||
|
|
||||||
namespace sp {
|
namespace sp {
|
||||||
namespace option {
|
|
||||||
|
|
||||||
// Provide static numeric ID, to facilitate implementation of GetIdImpl()
|
|
||||||
template <std::intmax_t TId>
|
|
||||||
struct StaticNumIdImpl {};
|
|
||||||
|
|
||||||
// Facilitate implementation of DispatchImpl()
|
|
||||||
template <typename TActual>
|
|
||||||
struct DispatchImpl {};
|
|
||||||
|
|
||||||
// Provide fields of the message, facilitate implementation of
|
|
||||||
// ReadImpl(), WriteImpl(), ValidImpl(), etc...
|
|
||||||
template <typename TFields>
|
|
||||||
struct FieldsImpl {};
|
|
||||||
|
|
||||||
} // namespace option
|
|
||||||
|
|
||||||
|
|
||||||
namespace details {
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template <typename... TOptions>
|
|
||||||
class MessageImplParsedOptions;
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct MessageImplParsedOptions<> {
|
|
||||||
static const bool HasStaticNumIdImpl = false;
|
|
||||||
static const bool HasDispatchImpl = false;
|
|
||||||
static const bool HasFieldsImpl = false;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template <std::intmax_t TId, typename... TOptions>
|
|
||||||
struct MessageImplParsedOptions<option::StaticNumIdImpl<TId>, TOptions...> : public MessageImplParsedOptions<TOptions...> {
|
|
||||||
static const bool HasStaticNumIdImpl = true;
|
|
||||||
static const std::intmax_t MsgId = TId;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename TActual, typename... TOptions>
|
|
||||||
struct MessageImplParsedOptions<option::DispatchImpl<TActual>, TOptions...> : public MessageImplParsedOptions<TOptions...> {
|
|
||||||
static const bool HasDispatchImpl = true;
|
|
||||||
using ActualMessage = TActual;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename TFields, typename... TOptions>
|
|
||||||
struct MessageImplParsedOptions<option::FieldsImpl<TFields>, TOptions...> : public MessageImplParsedOptions<TOptions...> {
|
|
||||||
static const bool HasFieldsImpl = true;
|
|
||||||
using Fields = TFields;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ID information chunk
|
|
||||||
template <typename TBase, std::intmax_t TId>
|
|
||||||
class MessageImplStaticNumIdBase : public TBase {
|
|
||||||
public:
|
|
||||||
// Reuse the message ID type defined in the interface
|
|
||||||
using MsgIdType = typename TBase::MsgIdType;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
virtual MsgIdType GetIdImpl() const override {
|
|
||||||
return static_cast<MsgIdType>(TId);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Dispatch implementation chunk
|
|
||||||
template <typename TBase, typename TActual>
|
|
||||||
class MessageImplDispatchBase : public TBase {
|
|
||||||
public:
|
|
||||||
// Reuse the Handler type defined in the interface class
|
|
||||||
using Handler = typename TBase::HandlerType;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
virtual void DispatchImpl(Handler& handler) override {
|
|
||||||
handler.Handle(static_cast<TActual&>(*this));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template <typename TBase, typename TFields>
|
|
||||||
class MessageImplFieldsBase : public TBase {
|
|
||||||
public:
|
|
||||||
using AllFields = typename details::FieldsBuilder<TFields>::Type;
|
|
||||||
|
|
||||||
template <typename... Args>
|
|
||||||
void Construct(Args... args) {
|
|
||||||
m_Fields = std::make_tuple(args...);
|
|
||||||
}
|
|
||||||
|
|
||||||
AllFields& GetFields() {
|
|
||||||
return m_Fields;
|
|
||||||
}
|
|
||||||
|
|
||||||
const AllFields& GetFields() const {
|
|
||||||
return m_Fields;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <std::size_t FIndex>
|
|
||||||
auto& GetField() {
|
|
||||||
return std::get<FIndex>(GetFields()).GetValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <std::size_t FIndex>
|
|
||||||
const auto& GetField() const {
|
|
||||||
return std::get<FIndex>(GetFields()).GetValue();
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
AllFields m_Fields;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename TBase>
|
|
||||||
class MessageImplFieldsReadBase : public TBase {
|
|
||||||
private:
|
|
||||||
// normal reading
|
|
||||||
template <typename TField>
|
|
||||||
void ReadField(Field<TField, 0>& field, DataBuffer& buffer) {
|
|
||||||
this->ReadData(field.GetValue(), buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
// reading field in bitfield
|
|
||||||
template <typename TFieldType, typename TField, int IAlignment>
|
|
||||||
void ReadField(Field<TField, IAlignment>& field, TFieldType data, std::size_t offset) {
|
|
||||||
static constexpr std::size_t TotalBitCount = sizeof(TFieldType) * 8;
|
|
||||||
// we suppose that the first element is at the highest bits
|
|
||||||
field.GetValue() = (data >> TotalBitCount - IAlignment - offset) & ((1 << IAlignment) - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// reading bitfield
|
|
||||||
template <typename TContainer, typename TFirst, typename... TFields>
|
|
||||||
void ReadField(Field<BitField<TContainer, TFirst, TFields...>, 0>& field, DataBuffer& buffer) {
|
|
||||||
TContainer data;
|
|
||||||
this->ReadData(data, buffer);
|
|
||||||
std::size_t offset = 0;
|
|
||||||
TupleForEach(
|
|
||||||
[data, this, &offset](auto& field) {
|
|
||||||
this->ReadField(field, data, offset);
|
|
||||||
offset += field.GetAlignment();
|
|
||||||
},
|
|
||||||
field.GetValue().GetFields());
|
|
||||||
}
|
|
||||||
|
|
||||||
void ReadImpl(DataBuffer& buffer) override {
|
|
||||||
auto& allFields = this->GetFields();
|
|
||||||
TupleForEach([&buffer, this](auto& field) { this->ReadField(field, buffer); }, allFields);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename TBase>
|
|
||||||
class MessageImplFieldsWriteBase : public TBase {
|
|
||||||
private:
|
|
||||||
// normal writing
|
|
||||||
template <typename TField>
|
|
||||||
void WriteField(Field<TField, 0>& field, DataBuffer& buffer) {
|
|
||||||
this->WriteData(field.GetValue(), buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
// writing field in bitfield
|
|
||||||
template <typename TFieldType, typename TField, int IAlignment>
|
|
||||||
void WriteField(Field<TField, IAlignment>& field, TFieldType& data, std::size_t offset) {
|
|
||||||
static constexpr std::size_t TotalBitCount = sizeof(TFieldType) * 8;
|
|
||||||
// we suppose that the first element is at the highest bits
|
|
||||||
data |= (field.GetValue() & ((1 << IAlignment) - 1)) << TotalBitCount - IAlignment - offset;
|
|
||||||
}
|
|
||||||
|
|
||||||
// writing bitfield
|
|
||||||
template <typename TContainer, typename TFirst, typename... TFields>
|
|
||||||
void WriteField(Field<BitField<TContainer, TFirst, TFields...>, 0>& field, DataBuffer& buffer) {
|
|
||||||
TContainer data = 0;
|
|
||||||
std::size_t offset = 0;
|
|
||||||
TupleForEach(
|
|
||||||
[&data, this, &offset](auto& field) {
|
|
||||||
this->WriteField(field, data, offset);
|
|
||||||
offset += field.GetAlignment();
|
|
||||||
},
|
|
||||||
field.GetValue().GetFields());
|
|
||||||
this->WriteData(data, buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
void WriteImpl(DataBuffer& buffer) override {
|
|
||||||
auto& allFields = this->GetFields();
|
|
||||||
TupleForEach([&buffer, this](auto& field) { this->WriteField(field, buffer); }, allFields);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename TBase>
|
|
||||||
class MessageImplFieldsValidBase : public TBase {
|
|
||||||
protected:
|
|
||||||
bool ValidImpl() const override {
|
|
||||||
// Access fields via interface provided in previous chunk
|
|
||||||
// auto& allFields = TBase::GetFields();
|
|
||||||
//... // validate all the fields
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// id impl
|
|
||||||
template <typename TBase, typename ParsedImplOptions, bool TImplement>
|
|
||||||
struct MessageImplProcessStaticNumId;
|
|
||||||
|
|
||||||
template <typename TBase, typename ParsedImplOptions>
|
|
||||||
struct MessageImplProcessStaticNumId<TBase, ParsedImplOptions, true> {
|
|
||||||
using Type = MessageImplStaticNumIdBase<TBase, ParsedImplOptions::MsgId>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename TBase, typename ParsedImplOptions>
|
|
||||||
struct MessageImplProcessStaticNumId<TBase, ParsedImplOptions, false> {
|
|
||||||
using Type = TBase;
|
|
||||||
};
|
|
||||||
|
|
||||||
// dispatch impl
|
|
||||||
template <typename TBase, typename ParsedImplOptions, bool TImplement>
|
|
||||||
struct MessageImplProcessDispatch;
|
|
||||||
|
|
||||||
template <typename TBase, typename ParsedImplOptions>
|
|
||||||
struct MessageImplProcessDispatch<TBase, ParsedImplOptions, true> {
|
|
||||||
using Type = MessageImplDispatchBase<TBase, typename ParsedImplOptions::ActualMessage>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename TBase, typename ParsedImplOptions>
|
|
||||||
struct MessageImplProcessDispatch<TBase, ParsedImplOptions, false> {
|
|
||||||
using Type = TBase;
|
|
||||||
};
|
|
||||||
|
|
||||||
// fields impl
|
|
||||||
template <typename TBase, typename ParsedImplOptions, bool TImplement>
|
|
||||||
struct MessageImplProcessFields;
|
|
||||||
|
|
||||||
template <typename TBase, typename ParsedImplOptions>
|
|
||||||
struct MessageImplProcessFields<TBase, ParsedImplOptions, true> {
|
|
||||||
using Type = MessageImplFieldsBase<TBase, typename ParsedImplOptions::Fields>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename TBase, typename ParsedImplOptions>
|
|
||||||
struct MessageImplProcessFields<TBase, ParsedImplOptions, false> {
|
|
||||||
using Type = TBase;
|
|
||||||
};
|
|
||||||
|
|
||||||
// read impl
|
|
||||||
template <typename TBase, bool TImplement>
|
|
||||||
struct MessageImplProcessReadFields;
|
|
||||||
|
|
||||||
template <typename TBase>
|
|
||||||
struct MessageImplProcessReadFields<TBase, true> {
|
|
||||||
using Type = MessageImplFieldsReadBase<TBase>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename TBase>
|
|
||||||
struct MessageImplProcessReadFields<TBase, false> {
|
|
||||||
using Type = TBase;
|
|
||||||
};
|
|
||||||
|
|
||||||
// write impl
|
|
||||||
template <typename TBase, bool TImplement>
|
|
||||||
struct MessageImplProcessWriteFields;
|
|
||||||
|
|
||||||
template <typename TBase>
|
|
||||||
struct MessageImplProcessWriteFields<TBase, true> {
|
|
||||||
using Type = MessageImplFieldsWriteBase<TBase>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename TBase>
|
|
||||||
struct MessageImplProcessWriteFields<TBase, false> {
|
|
||||||
using Type = TBase;
|
|
||||||
};
|
|
||||||
|
|
||||||
// valid impl
|
|
||||||
template <typename TBase, bool TImplement>
|
|
||||||
struct MessageImplProcessValidFields;
|
|
||||||
|
|
||||||
template <typename TBase>
|
|
||||||
struct MessageImplProcessValidFields<TBase, true> {
|
|
||||||
using Type = MessageImplFieldsValidBase<TBase>;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename TBase>
|
|
||||||
struct MessageImplProcessValidFields<TBase, false> {
|
|
||||||
using Type = TBase;
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// TBase is interface class
|
|
||||||
// TOptions... are the implementation options
|
|
||||||
template <typename TBase, typename... TOptions>
|
|
||||||
struct MessageImplBuilder {
|
|
||||||
// ParsedOptions class is supposed to be defined in comms::Message class
|
|
||||||
using InterfaceOptions = typename TBase::ParsedOptions;
|
|
||||||
|
|
||||||
// Parse implementation options
|
|
||||||
using ImplOptions = MessageImplParsedOptions<TOptions...>;
|
|
||||||
|
|
||||||
// Provide GetIdImpl() if possible
|
|
||||||
static const bool HasStaticNumIdImpl = InterfaceOptions::HasMsgIdType && ImplOptions::HasStaticNumIdImpl;
|
|
||||||
using Base1 = typename MessageImplProcessStaticNumId<TBase, ImplOptions, HasStaticNumIdImpl>::Type;
|
|
||||||
|
|
||||||
// Provide DispatchImpl() if possible
|
|
||||||
static const bool HasDispatchImpl = InterfaceOptions::HasHandler && ImplOptions::HasDispatchImpl;
|
|
||||||
using Base2 = typename MessageImplProcessDispatch<Base1, ImplOptions, HasDispatchImpl>::Type;
|
|
||||||
|
|
||||||
// Provide access to fields if possible
|
|
||||||
using Base3 = typename MessageImplProcessFields<Base2, ImplOptions, ImplOptions::HasFieldsImpl>::Type;
|
|
||||||
|
|
||||||
// Provide ReadImpl() if possible
|
|
||||||
static const bool HasReadImpl = InterfaceOptions::HasReadOperations && ImplOptions::HasFieldsImpl;
|
|
||||||
using Base4 = typename MessageImplProcessReadFields<Base3, HasReadImpl>::Type;
|
|
||||||
|
|
||||||
// Provide WriteImpl() if possible
|
|
||||||
static const bool HasWriteImpl = InterfaceOptions::HasWriteOperations && ImplOptions::HasFieldsImpl;
|
|
||||||
using Base5 = typename MessageImplProcessWriteFields<Base4, HasWriteImpl>::Type;
|
|
||||||
|
|
||||||
// Provide ValidImpl() if possible
|
|
||||||
static const bool HasValidImpl = InterfaceOptions::HasValid && ImplOptions::HasFieldsImpl;
|
|
||||||
using Base6 = typename MessageImplProcessValidFields<Base5, HasValidImpl>::Type;
|
|
||||||
|
|
||||||
// The last BaseN must be taken as final type.
|
|
||||||
using Type = Base6;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace details
|
|
||||||
|
|
||||||
template <typename TBase, typename... TOptions>
|
template <typename TBase, typename... TOptions>
|
||||||
class MessageBase : public details::MessageImplBuilder<TBase, TOptions...>::Type {};
|
class MessageBase : public details::MessageImplBuilder<TBase, TOptions...>::Type {};
|
||||||
|
|
||||||
|
|
||||||
} // namespace sp
|
} // namespace sp
|
||||||
45
include/sp/protocol/message/MessageImplBuilder.h
Normal file
45
include/sp/protocol/message/MessageImplBuilder.h
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
namespace details {
|
||||||
|
|
||||||
|
|
||||||
|
// TBase is interface class
|
||||||
|
// TOptions... are the implementation options
|
||||||
|
template <typename TBase, typename... TOptions>
|
||||||
|
struct MessageImplBuilder {
|
||||||
|
// ParsedOptions class is supposed to be defined in comms::Message class
|
||||||
|
using InterfaceOptions = typename TBase::ParsedOptions;
|
||||||
|
|
||||||
|
// Parse implementation options
|
||||||
|
using ImplOptions = MessageImplParsedOptions<TOptions...>;
|
||||||
|
|
||||||
|
// Provide GetIdImpl() if possible
|
||||||
|
static const bool HasStaticNumIdImpl = InterfaceOptions::HasMsgIdType && ImplOptions::HasStaticNumIdImpl;
|
||||||
|
using Base1 = typename MessageImplProcessStaticNumId<TBase, ImplOptions, HasStaticNumIdImpl>::Type;
|
||||||
|
|
||||||
|
// Provide DispatchImpl() if possible
|
||||||
|
static const bool HasDispatchImpl = InterfaceOptions::HasHandler && ImplOptions::HasDispatchImpl;
|
||||||
|
using Base2 = typename MessageImplProcessDispatch<Base1, ImplOptions, HasDispatchImpl>::Type;
|
||||||
|
|
||||||
|
// Provide access to fields if possible
|
||||||
|
using Base3 = typename MessageImplProcessFields<Base2, ImplOptions, ImplOptions::HasFieldsImpl>::Type;
|
||||||
|
|
||||||
|
// Provide ReadImpl() if possible
|
||||||
|
static const bool HasReadImpl = InterfaceOptions::HasReadOperations && ImplOptions::HasFieldsImpl;
|
||||||
|
using Base4 = typename MessageImplProcessReadFields<Base3, HasReadImpl>::Type;
|
||||||
|
|
||||||
|
// Provide WriteImpl() if possible
|
||||||
|
static const bool HasWriteImpl = InterfaceOptions::HasWriteOperations && ImplOptions::HasFieldsImpl;
|
||||||
|
using Base5 = typename MessageImplProcessWriteFields<Base4, HasWriteImpl>::Type;
|
||||||
|
|
||||||
|
// Provide ValidImpl() if possible
|
||||||
|
static const bool HasValidImpl = InterfaceOptions::HasValid && ImplOptions::HasFieldsImpl;
|
||||||
|
using Base6 = typename MessageImplProcessValidFields<Base5, HasValidImpl>::Type;
|
||||||
|
|
||||||
|
// The last BaseN must be taken as final type.
|
||||||
|
using Type = Base6;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
|
} // namespace sp
|
||||||
61
include/sp/protocol/message/MessageImplOptions.h
Normal file
61
include/sp/protocol/message/MessageImplOptions.h
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
namespace option {
|
||||||
|
|
||||||
|
// Provide static numeric ID, to facilitate implementation of GetIdImpl()
|
||||||
|
template <std::intmax_t TId>
|
||||||
|
struct StaticNumIdImpl {};
|
||||||
|
|
||||||
|
// Facilitate implementation of DispatchImpl()
|
||||||
|
template <typename TActual>
|
||||||
|
struct DispatchImpl {};
|
||||||
|
|
||||||
|
// Provide fields of the message, facilitate implementation of
|
||||||
|
// ReadImpl(), WriteImpl(), ValidImpl(), etc...
|
||||||
|
template <typename TFields>
|
||||||
|
struct FieldsImpl {};
|
||||||
|
|
||||||
|
} // namespace option
|
||||||
|
|
||||||
|
|
||||||
|
namespace details {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template <typename... TOptions>
|
||||||
|
class MessageImplParsedOptions;
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct MessageImplParsedOptions<> {
|
||||||
|
static const bool HasStaticNumIdImpl = false;
|
||||||
|
static const bool HasDispatchImpl = false;
|
||||||
|
static const bool HasFieldsImpl = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template <std::intmax_t TId, typename... TOptions>
|
||||||
|
struct MessageImplParsedOptions<option::StaticNumIdImpl<TId>, TOptions...> : public MessageImplParsedOptions<TOptions...> {
|
||||||
|
static const bool HasStaticNumIdImpl = true;
|
||||||
|
static const std::intmax_t MsgId = TId;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TActual, typename... TOptions>
|
||||||
|
struct MessageImplParsedOptions<option::DispatchImpl<TActual>, TOptions...> : public MessageImplParsedOptions<TOptions...> {
|
||||||
|
static const bool HasDispatchImpl = true;
|
||||||
|
using ActualMessage = TActual;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TFields, typename... TOptions>
|
||||||
|
struct MessageImplParsedOptions<option::FieldsImpl<TFields>, TOptions...> : public MessageImplParsedOptions<TOptions...> {
|
||||||
|
static const bool HasFieldsImpl = true;
|
||||||
|
using Fields = TFields;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
|
} // namespace sp
|
||||||
93
include/sp/protocol/message/MessageImplProcess.h
Normal file
93
include/sp/protocol/message/MessageImplProcess.h
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
namespace details {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// id impl
|
||||||
|
template <typename TBase, typename ParsedImplOptions, bool TImplement>
|
||||||
|
struct MessageImplProcessStaticNumId;
|
||||||
|
|
||||||
|
template <typename TBase, typename ParsedImplOptions>
|
||||||
|
struct MessageImplProcessStaticNumId<TBase, ParsedImplOptions, true> {
|
||||||
|
using Type = MessageImplStaticNumIdBase<TBase, ParsedImplOptions::MsgId>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TBase, typename ParsedImplOptions>
|
||||||
|
struct MessageImplProcessStaticNumId<TBase, ParsedImplOptions, false> {
|
||||||
|
using Type = TBase;
|
||||||
|
};
|
||||||
|
|
||||||
|
// dispatch impl
|
||||||
|
template <typename TBase, typename ParsedImplOptions, bool TImplement>
|
||||||
|
struct MessageImplProcessDispatch;
|
||||||
|
|
||||||
|
template <typename TBase, typename ParsedImplOptions>
|
||||||
|
struct MessageImplProcessDispatch<TBase, ParsedImplOptions, true> {
|
||||||
|
using Type = MessageImplDispatchBase<TBase, typename ParsedImplOptions::ActualMessage>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TBase, typename ParsedImplOptions>
|
||||||
|
struct MessageImplProcessDispatch<TBase, ParsedImplOptions, false> {
|
||||||
|
using Type = TBase;
|
||||||
|
};
|
||||||
|
|
||||||
|
// fields impl
|
||||||
|
template <typename TBase, typename ParsedImplOptions, bool TImplement>
|
||||||
|
struct MessageImplProcessFields;
|
||||||
|
|
||||||
|
template <typename TBase, typename ParsedImplOptions>
|
||||||
|
struct MessageImplProcessFields<TBase, ParsedImplOptions, true> {
|
||||||
|
using Type = MessageImplFieldsBase<TBase, typename ParsedImplOptions::Fields>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TBase, typename ParsedImplOptions>
|
||||||
|
struct MessageImplProcessFields<TBase, ParsedImplOptions, false> {
|
||||||
|
using Type = TBase;
|
||||||
|
};
|
||||||
|
|
||||||
|
// read impl
|
||||||
|
template <typename TBase, bool TImplement>
|
||||||
|
struct MessageImplProcessReadFields;
|
||||||
|
|
||||||
|
template <typename TBase>
|
||||||
|
struct MessageImplProcessReadFields<TBase, true> {
|
||||||
|
using Type = MessageImplFieldsReadBase<TBase>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TBase>
|
||||||
|
struct MessageImplProcessReadFields<TBase, false> {
|
||||||
|
using Type = TBase;
|
||||||
|
};
|
||||||
|
|
||||||
|
// write impl
|
||||||
|
template <typename TBase, bool TImplement>
|
||||||
|
struct MessageImplProcessWriteFields;
|
||||||
|
|
||||||
|
template <typename TBase>
|
||||||
|
struct MessageImplProcessWriteFields<TBase, true> {
|
||||||
|
using Type = MessageImplFieldsWriteBase<TBase>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TBase>
|
||||||
|
struct MessageImplProcessWriteFields<TBase, false> {
|
||||||
|
using Type = TBase;
|
||||||
|
};
|
||||||
|
|
||||||
|
// valid impl
|
||||||
|
template <typename TBase, bool TImplement>
|
||||||
|
struct MessageImplProcessValidFields;
|
||||||
|
|
||||||
|
template <typename TBase>
|
||||||
|
struct MessageImplProcessValidFields<TBase, true> {
|
||||||
|
using Type = MessageImplFieldsValidBase<TBase>;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TBase>
|
||||||
|
struct MessageImplProcessValidFields<TBase, false> {
|
||||||
|
using Type = TBase;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
|
} // namespace sp
|
||||||
@@ -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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ class MessageInterfaceBigEndian : public TBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void WriteData(T value, DataBuffer& buffer) {
|
void WriteData(T value, DataBuffer& buffer) const {
|
||||||
ToNetwork(value);
|
ToNetwork(value);
|
||||||
buffer << value;
|
buffer << value;
|
||||||
}
|
}
|
||||||
@@ -71,12 +71,12 @@ class MessageInterfaceReadBase : public TBase {
|
|||||||
template <typename TBase>
|
template <typename TBase>
|
||||||
class MessageInterfaceWriteBase : public TBase {
|
class MessageInterfaceWriteBase : public TBase {
|
||||||
public:
|
public:
|
||||||
void Write(DataBuffer& buffer) {
|
void Write(DataBuffer& buffer) const {
|
||||||
WriteImpl(buffer);
|
WriteImpl(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void WriteImpl(DataBuffer& buffer) = 0;
|
virtual void WriteImpl(DataBuffer& buffer) const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handler functionality chunk
|
// Handler functionality chunk
|
||||||
@@ -85,12 +85,12 @@ class MessageInterfaceHandlerBase : public TBase {
|
|||||||
public:
|
public:
|
||||||
using HandlerType = typename THandler::HandlerT;
|
using HandlerType = typename THandler::HandlerT;
|
||||||
|
|
||||||
void Dispatch(HandlerType& handler) {
|
void Dispatch(HandlerType& handler) const {
|
||||||
DispatchImpl(handler);
|
DispatchImpl(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void DispatchImpl(HandlerType& handler) = 0;
|
virtual void DispatchImpl(HandlerType& handler) const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Validity functionality chunk
|
// Validity functionality chunk
|
||||||
@@ -109,7 +109,7 @@ class MessageInterfaceValidityBase : public TBase {
|
|||||||
template <typename TBase>
|
template <typename TBase>
|
||||||
class MessageInterfaceWriteIdBase : public TBase {
|
class MessageInterfaceWriteIdBase : public TBase {
|
||||||
public:
|
public:
|
||||||
void Write(DataBuffer& buffer) {
|
void Write(DataBuffer& buffer) const {
|
||||||
this->WriteData(this->GetId(), buffer);
|
this->WriteData(this->GetId(), buffer);
|
||||||
this->WriteImpl(buffer);
|
this->WriteImpl(buffer);
|
||||||
}
|
}
|
||||||
|
|||||||
160
include/sp/protocol/message/MessagesImpl.h
Normal file
160
include/sp/protocol/message/MessagesImpl.h
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
namespace details {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// ID information chunk
|
||||||
|
template <typename TBase, std::intmax_t TId>
|
||||||
|
class MessageImplStaticNumIdBase : public TBase {
|
||||||
|
public:
|
||||||
|
// Reuse the message ID type defined in the interface
|
||||||
|
using MsgIdType = typename TBase::MsgIdType;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual MsgIdType GetIdImpl() const override {
|
||||||
|
return static_cast<MsgIdType>(TId);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Dispatch implementation chunk
|
||||||
|
template <typename TBase, typename TActual>
|
||||||
|
class MessageImplDispatchBase : public TBase {
|
||||||
|
public:
|
||||||
|
// Reuse the Handler type defined in the interface class
|
||||||
|
using Handler = typename TBase::HandlerType;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void DispatchImpl(Handler& handler) const override {
|
||||||
|
handler.Handle(static_cast<const TActual&>(*this));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template <typename TBase, typename TFields>
|
||||||
|
class MessageImplFieldsBase : public TBase {
|
||||||
|
public:
|
||||||
|
using AllFields = typename details::FieldsBuilder<TFields>::Type;
|
||||||
|
|
||||||
|
template <typename... Args>
|
||||||
|
void Construct(Args... args) {
|
||||||
|
m_Fields = std::make_tuple(args...);
|
||||||
|
}
|
||||||
|
|
||||||
|
AllFields& GetFields() {
|
||||||
|
return m_Fields;
|
||||||
|
}
|
||||||
|
|
||||||
|
const AllFields& GetFields() const {
|
||||||
|
return m_Fields;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::size_t FIndex>
|
||||||
|
auto& GetField() {
|
||||||
|
return std::get<FIndex>(GetFields()).GetValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <std::size_t FIndex>
|
||||||
|
const auto& GetField() const {
|
||||||
|
return std::get<FIndex>(GetFields()).GetValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
// allow use of enums
|
||||||
|
template <typename E, E FIndex>
|
||||||
|
const auto& GetField() const {
|
||||||
|
return std::get<static_cast<std::size_t>(FIndex)>(this->GetFields()).GetValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
AllFields m_Fields;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TBase>
|
||||||
|
class MessageImplFieldsReadBase : public TBase {
|
||||||
|
private:
|
||||||
|
// normal reading
|
||||||
|
template <typename TField>
|
||||||
|
void ReadField(Field<TField, 0>& field, DataBuffer& buffer) {
|
||||||
|
this->ReadData(field.GetValue(), buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// reading field in bitfield
|
||||||
|
template <typename TFieldType, typename TField, int IAlignment>
|
||||||
|
void ReadField(Field<TField, IAlignment>& field, TFieldType data, std::size_t offset) {
|
||||||
|
static constexpr std::size_t TotalBitCount = sizeof(TFieldType) * 8;
|
||||||
|
// we suppose that the first element is at the highest bits
|
||||||
|
field.GetValue() = (data >> TotalBitCount - IAlignment - offset) & ((1 << IAlignment) - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// reading bitfield
|
||||||
|
template <typename TContainer, typename TFirst, typename... TFields>
|
||||||
|
void ReadField(Field<BitField<TContainer, TFirst, TFields...>, 0>& field, DataBuffer& buffer) {
|
||||||
|
TContainer data;
|
||||||
|
this->ReadData(data, buffer);
|
||||||
|
std::size_t offset = 0;
|
||||||
|
TupleForEach(
|
||||||
|
[data, this, &offset](auto& field) {
|
||||||
|
this->ReadField(field, data, offset);
|
||||||
|
offset += field.GetAlignment();
|
||||||
|
},
|
||||||
|
field.GetValue().GetFields());
|
||||||
|
}
|
||||||
|
|
||||||
|
void ReadImpl(DataBuffer& buffer) override {
|
||||||
|
auto& allFields = this->GetFields();
|
||||||
|
TupleForEach([&buffer, this](auto& field) { this->ReadField(field, buffer); }, allFields);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TBase>
|
||||||
|
class MessageImplFieldsWriteBase : public TBase {
|
||||||
|
private:
|
||||||
|
// normal writing
|
||||||
|
template <typename TField>
|
||||||
|
void WriteField(const Field<TField, 0>& field, DataBuffer& buffer) const {
|
||||||
|
this->WriteData(field.GetValue(), buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
// writing field in bitfield
|
||||||
|
template <typename TFieldType, typename TField, int IAlignment>
|
||||||
|
void WriteField(const Field<TField, IAlignment>& field, TFieldType& data, std::size_t offset) const {
|
||||||
|
static constexpr std::size_t TotalBitCount = sizeof(TFieldType) * 8;
|
||||||
|
// we suppose that the first element is at the highest bits
|
||||||
|
data |= (field.GetValue() & ((1 << IAlignment) - 1)) << TotalBitCount - IAlignment - offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
// writing bitfield
|
||||||
|
template <typename TContainer, typename TFirst, typename... TFields>
|
||||||
|
void WriteField(const Field<BitField<TContainer, TFirst, TFields...>, 0>& field, DataBuffer& buffer) const {
|
||||||
|
TContainer data = 0;
|
||||||
|
std::size_t offset = 0;
|
||||||
|
TupleForEach(
|
||||||
|
[&data, this, &offset](auto& field) {
|
||||||
|
this->WriteField(field, data, offset);
|
||||||
|
offset += field.GetAlignment();
|
||||||
|
},
|
||||||
|
field.GetValue().GetFields());
|
||||||
|
this->WriteData(data, buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void WriteImpl(DataBuffer& buffer) const override {
|
||||||
|
auto& allFields = this->GetFields();
|
||||||
|
TupleForEach([&buffer, this](const auto& field) { this->WriteField(field, buffer); }, allFields);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename TBase>
|
||||||
|
class MessageImplFieldsValidBase : public TBase {
|
||||||
|
protected:
|
||||||
|
bool ValidImpl() const override {
|
||||||
|
// Access fields via interface provided in previous chunk
|
||||||
|
// auto& allFields = TBase::GetFields();
|
||||||
|
//... // validate all the fields
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace details
|
||||||
|
} // namespace sp
|
||||||
74
src/sp/common/ByteSwapping.cpp
Normal file
74
src/sp/common/ByteSwapping.cpp
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
#include <sp/common/ByteSwapping.h>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <winsock2.h>
|
||||||
|
#else
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void SwapBytes(T& value) {
|
||||||
|
char* ptr = reinterpret_cast<char*>(&value);
|
||||||
|
std::reverse(ptr, ptr + sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsSystemBigEndian() {
|
||||||
|
static constexpr std::uint16_t test = 10;
|
||||||
|
static const bool isBigEndian = reinterpret_cast<const std::uint8_t*>(&test)[1] == 10;
|
||||||
|
return isBigEndian;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void ToNetwork<std::uint16_t>(std::uint16_t& value) {
|
||||||
|
value = htons(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void ToNetwork<std::uint32_t>(std::uint32_t& value) {
|
||||||
|
value = htonl(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void ToNetwork<std::uint64_t>(std::uint64_t& value) {
|
||||||
|
if (IsSystemBigEndian())
|
||||||
|
return;
|
||||||
|
SwapBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void FromNetwork<std::uint16_t>(std::uint16_t& value) {
|
||||||
|
value = ntohs(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void FromNetwork<std::uint32_t>(std::uint32_t& value) {
|
||||||
|
value = ntohl(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void FromNetwork<std::uint64_t>(std::uint64_t& value) {
|
||||||
|
if (IsSystemBigEndian())
|
||||||
|
return;
|
||||||
|
SwapBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void TrySwapBytes<std::uint16_t>(std::uint16_t& value) {
|
||||||
|
SwapBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void TrySwapBytes<std::uint32_t>(std::uint32_t& value) {
|
||||||
|
SwapBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void TrySwapBytes<std::uint64_t>(std::uint64_t& value) {
|
||||||
|
SwapBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace sp
|
||||||
@@ -130,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
|
||||||
|
|||||||
@@ -4,19 +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, std::make_tuple(666, 9));
|
auto upgradeTower = std::make_unique<UpgradeTowerPacket>(std::make_tuple(666, 9));
|
||||||
|
|
||||||
sp::PacketMessage* msg = keepAlive.get();
|
sp::PacketMessage* msg = upgradeTower.get();
|
||||||
|
|
||||||
KeepAliveHandler handler;
|
KeepAliveHandler handler;
|
||||||
msg->Dispatch(handler);
|
msg->Dispatch(handler);
|
||||||
@@ -27,18 +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->GetKeepAliveId() << "\n";
|
std::cout << "Test : " << (unsigned) upgradeTower2->GetTowerId() << "\n";
|
||||||
|
|
||||||
keepAlive2->GetField<TestAlignField>().GetField<0>();
|
|
||||||
std::cout << "Test : " << (unsigned) keepAlive2->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;
|
||||||
85
xmake.lua
85
xmake.lua
@@ -2,15 +2,12 @@ add_rules("mode.debug", "mode.release")
|
|||||||
|
|
||||||
set_languages("c++17")
|
set_languages("c++17")
|
||||||
|
|
||||||
add_requires("enet6")
|
|
||||||
|
|
||||||
target("SimpleProtocolLib")
|
target("SimpleProtocolLib")
|
||||||
add_includedirs("include", {public = true})
|
add_includedirs("include")
|
||||||
set_kind("binary")
|
add_headerfiles("include/(sp/**.h)")
|
||||||
add_files("src/**.cpp")
|
set_group("Library")
|
||||||
add_packages("enet6", {public = true})
|
add_files("src/sp/**.cpp")
|
||||||
|
set_kind("$(kind)")
|
||||||
|
|
||||||
|
|
||||||
-- Tests
|
-- Tests
|
||||||
for _, file in ipairs(os.files("test/**.cpp")) do
|
for _, file in ipairs(os.files("test/**.cpp")) do
|
||||||
@@ -19,79 +16,9 @@ for _, file in ipairs(os.files("test/**.cpp")) do
|
|||||||
set_kind("binary")
|
set_kind("binary")
|
||||||
|
|
||||||
add_files(file)
|
add_files(file)
|
||||||
|
add_includedirs("include")
|
||||||
set_default(false)
|
|
||||||
|
|
||||||
add_deps("SimpleProtocolLib")
|
add_deps("SimpleProtocolLib")
|
||||||
|
|
||||||
add_tests("compile_and_run")
|
add_tests("compile_and_run")
|
||||||
end
|
end
|
||||||
--
|
|
||||||
-- If you want to known more usage about xmake, please see https://xmake.io
|
|
||||||
--
|
|
||||||
-- ## FAQ
|
|
||||||
--
|
|
||||||
-- You can enter the project directory firstly before building project.
|
|
||||||
--
|
|
||||||
-- $ cd projectdir
|
|
||||||
--
|
|
||||||
-- 1. How to build project?
|
|
||||||
--
|
|
||||||
-- $ xmake
|
|
||||||
--
|
|
||||||
-- 2. How to configure project?
|
|
||||||
--
|
|
||||||
-- $ xmake f -p [macosx|linux|iphoneos ..] -a [x86_64|i386|arm64 ..] -m [debug|release]
|
|
||||||
--
|
|
||||||
-- 3. Where is the build output directory?
|
|
||||||
--
|
|
||||||
-- The default output directory is `./build` and you can configure the output directory.
|
|
||||||
--
|
|
||||||
-- $ xmake f -o outputdir
|
|
||||||
-- $ xmake
|
|
||||||
--
|
|
||||||
-- 4. How to run and debug target after building project?
|
|
||||||
--
|
|
||||||
-- $ xmake run [targetname]
|
|
||||||
-- $ xmake run -d [targetname]
|
|
||||||
--
|
|
||||||
-- 5. How to install target to the system directory or other output directory?
|
|
||||||
--
|
|
||||||
-- $ xmake install
|
|
||||||
-- $ xmake install -o installdir
|
|
||||||
--
|
|
||||||
-- 6. Add some frequently-used compilation flags in xmake.lua
|
|
||||||
--
|
|
||||||
-- @code
|
|
||||||
-- -- add debug and release modes
|
|
||||||
-- add_rules("mode.debug", "mode.release")
|
|
||||||
--
|
|
||||||
-- -- add macro definition
|
|
||||||
-- add_defines("NDEBUG", "_GNU_SOURCE=1")
|
|
||||||
--
|
|
||||||
-- -- set warning all as error
|
|
||||||
-- set_warnings("all", "error")
|
|
||||||
--
|
|
||||||
-- -- set language: c99, c++11
|
|
||||||
-- set_languages("c99", "c++11")
|
|
||||||
--
|
|
||||||
-- -- set optimization: none, faster, fastest, smallest
|
|
||||||
-- set_optimize("fastest")
|
|
||||||
--
|
|
||||||
-- -- add include search directories
|
|
||||||
-- add_includedirs("/usr/include", "/usr/local/include")
|
|
||||||
--
|
|
||||||
-- -- add link libraries and search directories
|
|
||||||
-- add_links("tbox")
|
|
||||||
-- add_linkdirs("/usr/local/lib", "/usr/lib")
|
|
||||||
--
|
|
||||||
-- -- add system link libraries
|
|
||||||
-- add_syslinks("z", "pthread")
|
|
||||||
--
|
|
||||||
-- -- add compilation and link flags
|
|
||||||
-- add_cxflags("-stdnolib", "-fno-strict-aliasing")
|
|
||||||
-- add_ldflags("-L/usr/local/lib", "-lpthread", {force = true})
|
|
||||||
--
|
|
||||||
-- @endcode
|
|
||||||
--
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user