Compare commits
62 Commits
097dab01fd
...
v2.3.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
75bae99daa
|
|||
|
90ae25bc8e
|
|||
|
0c82680af0
|
|||
|
695d15588f
|
|||
|
7eb96163ab
|
|||
|
b8dafa4eb1
|
|||
|
c5b3281be7
|
|||
|
9374332cd2
|
|||
|
bce37f59df
|
|||
|
a1a4176801
|
|||
|
5e9a0a9bae
|
|||
|
01e406cd89
|
|||
|
366a40afee
|
|||
| 45a3c427fb | |||
| 143b2f357c | |||
| 2149172b41 | |||
| 4c5c859221 | |||
| 632650e73d | |||
| 4aa25c4189 | |||
| 6f667deece | |||
| 6c24280690 | |||
| ed0b06f78d | |||
| 0d26879152 | |||
| 59bedd6482 | |||
| 073872df94 | |||
| 10b49b34dd | |||
| 194205be41 | |||
| 392fcb3d17 | |||
| 205c09a338 | |||
| 77356ce749 | |||
| 7f8d9e3f96 | |||
| 81c9dbadd6 | |||
| e8367cd91c | |||
| fa6ba74068 | |||
| d501c0181d | |||
| 680f72f881 | |||
| 4a7eb7a1df | |||
| 61d0ce3a47 | |||
| 5beb5e92a7 | |||
| 59aaf03421 | |||
| 03d799e064 | |||
| 8a5286d0ce | |||
| a194774925 | |||
| 8f32b09b17 | |||
| 60bb4ea06e | |||
| 2acbd76c5a | |||
| 468f5ce8a0 | |||
| f145716cf6 | |||
| 6ee7524e17 | |||
| db0c5f3245 | |||
| ee865021c2 | |||
| 462086b13c | |||
| 530232ba16 | |||
| dd808b4d57 | |||
| f5a3a443af | |||
| 06d69fb976 | |||
| 5e89531508 | |||
| 51d7c8f66d | |||
| 534757f884 | |||
| 6725a63c07 | |||
| 032800b220 | |||
| e13c73fe59 |
31
.github/workflows/ubuntu.yml
vendored
Normal file
31
.github/workflows/ubuntu.yml
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
name: Linux arm64
|
||||
run-name: Build And Test
|
||||
|
||||
on: [push]
|
||||
|
||||
|
||||
env:
|
||||
XMAKE_ROOT: y
|
||||
|
||||
jobs:
|
||||
Build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Check out repository code
|
||||
uses: actions/checkout@v3
|
||||
|
||||
- name: Prepare XMake
|
||||
uses: xmake-io/github-action-setup-xmake@v1
|
||||
with:
|
||||
xmake-version: latest
|
||||
actions-cache-folder: '.xmake-cache'
|
||||
actions-cache-key: 'ubuntu'
|
||||
|
||||
- name: XMake config
|
||||
run: xmake f -p linux -y
|
||||
|
||||
- name: Build
|
||||
run: xmake
|
||||
|
||||
- name: Test
|
||||
run: xmake test
|
||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -6,4 +6,6 @@ build/
|
||||
.DS_Store
|
||||
|
||||
|
||||
.vscode
|
||||
.vscode
|
||||
|
||||
*.bin
|
||||
10
README.md
Normal file
10
README.md
Normal file
@@ -0,0 +1,10 @@
|
||||
# SimpleProtocolLib
|
||||
|
||||
C++ Protocol engine used to (mainly) communicate with network
|
||||
|
||||
# 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,10 +4,28 @@
|
||||
#include <sp/protocol/Field.h>
|
||||
#include <sp/protocol/MessageBase.h>
|
||||
|
||||
enum DisconnectPacketFields {
|
||||
Reason = 0
|
||||
|
||||
struct DisconnectPacketData {
|
||||
std::string m_Reason;
|
||||
};
|
||||
|
||||
using DisconnectFields = std::tuple<std::string /*Reason*/>;
|
||||
class DisconnectPacket : public sp::MessageBase<sp::PacketMessage, sp::option::DispatchImpl<DisconnectPacket>> {
|
||||
private:
|
||||
DisconnectPacketData m_Data;
|
||||
|
||||
DeclarePacket(Disconnect);
|
||||
public:
|
||||
template<typename ... T>
|
||||
DisconnectPacket(T... args) : m_Data{args...} {}
|
||||
|
||||
const std::string& GetReason() const {
|
||||
return m_Data.m_Reason;
|
||||
}
|
||||
|
||||
virtual sp::PacketID GetId() const {
|
||||
return Disconnect;
|
||||
}
|
||||
};
|
||||
|
||||
void ff() {
|
||||
sizeof(std::string);
|
||||
}
|
||||
|
||||
@@ -4,10 +4,40 @@
|
||||
#include <sp/protocol/Field.h>
|
||||
#include <sp/protocol/MessageBase.h>
|
||||
|
||||
enum KeepAlivePacketFields {
|
||||
KeepAliveId = 0
|
||||
|
||||
template <sp::PacketID ID, typename TData>
|
||||
class ConcreteMessage {
|
||||
public:
|
||||
using DataType = TData;
|
||||
|
||||
template<typename... T>
|
||||
ConcreteMessage(const T&... args) : m_Data {args ...};
|
||||
|
||||
private:
|
||||
DataType m_Data;
|
||||
|
||||
virtual sp::PacketID GetId() const {
|
||||
return ID;
|
||||
}
|
||||
};
|
||||
|
||||
using KeepAliveFields = std::tuple<std::uint64_t /*KeepAliveId*/>;
|
||||
|
||||
DeclarePacket(KeepAlive);
|
||||
|
||||
|
||||
|
||||
struct KeepAlivePacket {
|
||||
std::uint64_t m_AliveId;
|
||||
};
|
||||
|
||||
class KeepAliveMessage : public sp::MessageBase<sp::PacketMessage, sp::option::DispatchImpl<DisconnectPacket>> {
|
||||
private:
|
||||
KeepAlivePacket m_Data;
|
||||
|
||||
public:
|
||||
template <typename... T>
|
||||
KeepAliveMessage(T... args) : m_Data{args...} {}
|
||||
|
||||
virtual sp::PacketID GetId() const {
|
||||
return KeepAlive;
|
||||
}
|
||||
};
|
||||
@@ -1,15 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
enum PacketId {
|
||||
enum PacketIds {
|
||||
KeepAlive = 0,
|
||||
Disconnect,
|
||||
UpgradeTower,
|
||||
};
|
||||
|
||||
#include <examples/KeepAlivePacket.h>
|
||||
#include <examples/DisconnectPacket.h>
|
||||
#include <examples/KeepAlivePacket.h>
|
||||
#include <examples/UpgradeTowerPacket.h>
|
||||
|
||||
// they must be in the same order !
|
||||
using AllPackets = std::tuple<KeepAlivePacket, DisconnectPacket>;
|
||||
// they must be in the same order as in the enum !
|
||||
using AllPackets = std::tuple<KeepAlivePacket, DisconnectPacket, UpgradeTowerPacket>;
|
||||
|
||||
#include <sp/default/DefaultPacketHandler.h>
|
||||
#include <sp/default/DefaultPacketFactory.h>
|
||||
#include <sp/default/DefaultPacketDispatcher.h>
|
||||
#include <sp/default/DefaultPacketFactory.h>
|
||||
#include <sp/default/DefaultPacketHandler.h>
|
||||
39
include/examples/UpgradeTowerPacket.h
Normal file
39
include/examples/UpgradeTowerPacket.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/default/DefaultPacket.h>
|
||||
#include <sp/protocol/Field.h>
|
||||
#include <sp/protocol/MessageBase.h>
|
||||
|
||||
struct UpgradeTowerPacketData {
|
||||
sp::BitField<std::uint16_t,
|
||||
sp::Field<std::uint16_t, 12>, // std::uint16_t m_Tower : 12;
|
||||
sp::Field<std::uint8_t, 4> // std::uint8_t m_Upgrade : 4;
|
||||
> m_TowerAndUpgrade;
|
||||
sp::VarInt m_Test;
|
||||
std::map<std::string, std::vector<int>> m_Test2;
|
||||
};
|
||||
|
||||
class UpgradeTowerPacket : public sp::MessageBase<sp::PacketMessage, sp::option::DispatchImpl<UpgradeTowerPacket>> {
|
||||
private:
|
||||
UpgradeTowerPacketData m_Data;
|
||||
|
||||
public:
|
||||
template <typename... T>
|
||||
UpgradeTowerPacket(T... args) : m_Data{args...} {}
|
||||
|
||||
std::uint16_t GetTowerId() const {
|
||||
return m_Data.m_TowerAndUpgrade.GetField<0>();
|
||||
}
|
||||
|
||||
std::uint8_t GetTowerUpgrade() const {
|
||||
return m_Data.m_TowerAndUpgrade.GetField<1>();
|
||||
}
|
||||
|
||||
virtual sp::PacketID GetId() const {
|
||||
return UpgradeTower;
|
||||
}
|
||||
|
||||
UpgradeTowerPacketData& GetData() {
|
||||
return m_Data;
|
||||
}
|
||||
};
|
||||
16
include/sp/common/ByteSwapping.h
Normal file
16
include/sp/common/ByteSwapping.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace sp {
|
||||
|
||||
bool IsLittleEndian();
|
||||
|
||||
void SwapBytes(std::uint8_t* begin, std::uint8_t* end);
|
||||
|
||||
template<typename T>
|
||||
void SwapBytes(T& a_Data) {
|
||||
SwapBytes(reinterpret_cast<std::uint8_t*>(&a_Data), reinterpret_cast<std::uint8_t*>(&a_Data) + sizeof(T));
|
||||
}
|
||||
|
||||
} // namespace sp
|
||||
@@ -10,8 +10,12 @@
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <sp/common/ByteSwapping.h>
|
||||
#include <sp/common/VarInt.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace sp {
|
||||
@@ -22,18 +26,21 @@ namespace sp {
|
||||
*/
|
||||
class DataBuffer {
|
||||
private:
|
||||
typedef std::vector<std::uint8_t> Data;
|
||||
using Data = std::vector<std::uint8_t>;
|
||||
|
||||
private:
|
||||
Data m_Buffer;
|
||||
std::size_t m_ReadOffset;
|
||||
|
||||
public:
|
||||
typedef Data::iterator iterator;
|
||||
typedef Data::const_iterator const_iterator;
|
||||
typedef Data::reference reference;
|
||||
typedef Data::const_reference const_reference;
|
||||
typedef Data::difference_type difference_type;
|
||||
using iterator = Data::iterator;
|
||||
using const_iterator = Data::const_iterator;
|
||||
using reference = Data::reference;
|
||||
using const_reference = Data::const_reference;
|
||||
using difference_type = Data::difference_type;
|
||||
|
||||
DataBuffer();
|
||||
DataBuffer(std::size_t a_InitialSize);
|
||||
DataBuffer(const DataBuffer& other);
|
||||
DataBuffer(const DataBuffer& other, difference_type offset);
|
||||
DataBuffer(DataBuffer&& other);
|
||||
@@ -44,113 +51,25 @@ class DataBuffer {
|
||||
|
||||
/**
|
||||
* \brief Append data to the buffer
|
||||
* \warning No endian checks
|
||||
*/
|
||||
template <typename T>
|
||||
void Append(const T& data) {
|
||||
std::size_t size = sizeof(data);
|
||||
void Append(const T& a_Data) {
|
||||
std::size_t size = sizeof(a_Data);
|
||||
std::size_t end_pos = m_Buffer.size();
|
||||
m_Buffer.resize(m_Buffer.size() + size);
|
||||
std::memcpy(&m_Buffer[end_pos], &data, size);
|
||||
std::memcpy(&m_Buffer[end_pos], &a_Data, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Append data to the buffer
|
||||
* \brief Read data into a_Data
|
||||
* \warning No endian checks
|
||||
*/
|
||||
template <typename T>
|
||||
DataBuffer& operator<<(const T& data) {
|
||||
Append(data);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Append a string to the buffer
|
||||
* \warning Don't use it for binary data !
|
||||
* \param str The string to append
|
||||
*/
|
||||
DataBuffer& operator<<(const std::string& str);
|
||||
|
||||
/**
|
||||
* \brief Append data to the buffer from another const buffer
|
||||
* \param data The buffer to append
|
||||
*/
|
||||
DataBuffer& operator<<(const DataBuffer& data);
|
||||
|
||||
/**
|
||||
* \brief Append a vector to the buffer by first writing the size
|
||||
* \param data The vector to append
|
||||
*/
|
||||
template <typename T>
|
||||
DataBuffer& operator<<(const std::vector<T>& data) {
|
||||
*this << VarInt{data.size()};
|
||||
for (const auto& element : data) {
|
||||
*this << element;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Append an array to the buffer by first writing the size
|
||||
* \param data The buffer to append
|
||||
*/
|
||||
template <typename T, std::size_t Size>
|
||||
DataBuffer& operator<<(const std::array<T, Size>& data) {
|
||||
for (const auto& element : data) {
|
||||
*this << element;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Read some data from the buffer and assign to desired variable
|
||||
*/
|
||||
template <typename T>
|
||||
DataBuffer& operator>>(T& data) {
|
||||
void Read(T& a_Data) {
|
||||
assert(m_ReadOffset + sizeof(T) <= GetSize());
|
||||
data = *(reinterpret_cast<T*>(&m_Buffer[m_ReadOffset]));
|
||||
std::memcpy(&a_Data, m_Buffer.data() + m_ReadOffset, sizeof(T));
|
||||
m_ReadOffset += sizeof(T);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Read some data from the buffer and assign to the new buffer
|
||||
* \param data The buffer to assign
|
||||
*/
|
||||
DataBuffer& operator>>(DataBuffer& data);
|
||||
|
||||
/**
|
||||
* \brief Read a string from the buffer
|
||||
* \param str The string to assign in the new buffer
|
||||
* \warning Don't use it for binary data !
|
||||
*/
|
||||
DataBuffer& operator>>(std::string& str);
|
||||
|
||||
/**
|
||||
* \brief Read a vector (size + data) from the buffer
|
||||
* \pre The vector is assumed to be empty
|
||||
*/
|
||||
template <typename T>
|
||||
DataBuffer& operator>>(std::vector<T>& data) {
|
||||
VarInt arraySize;
|
||||
*this >> arraySize;
|
||||
for (std::size_t i = 0; i < arraySize.GetValue(); i++) {
|
||||
T newElement;
|
||||
*this >> newElement;
|
||||
data.push_back(newElement);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Read an array from the buffer
|
||||
*/
|
||||
template <std::size_t Size, typename T>
|
||||
DataBuffer& operator>>(std::array<T, Size>& data) {
|
||||
for (std::size_t i = 0; i < Size; i++) {
|
||||
T newElement;
|
||||
*this >> newElement;
|
||||
data[i] = newElement;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -204,7 +123,6 @@ class DataBuffer {
|
||||
m_Buffer.reserve(amount);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* \brief Clear the buffer
|
||||
*/
|
||||
@@ -261,13 +179,13 @@ class DataBuffer {
|
||||
* \brief Read a file into the buffer
|
||||
* \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
|
||||
* \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
|
||||
@@ -293,8 +211,9 @@ class DataBuffer {
|
||||
};
|
||||
|
||||
/**
|
||||
* \brief Operator << to write a DataBuffer to an ostream
|
||||
* \brief Append data to the buffer from another const buffer
|
||||
* \param data The buffer to append
|
||||
*/
|
||||
std::ostream& operator<<(std::ostream& os, const DataBuffer& buffer);
|
||||
DataBuffer& operator<<(DataBuffer& a_Buffer, const DataBuffer& data);
|
||||
|
||||
} // namespace sp
|
||||
|
||||
455
include/sp/common/DataBufferOperators.h
Normal file
455
include/sp/common/DataBufferOperators.h
Normal file
@@ -0,0 +1,455 @@
|
||||
#pragma once
|
||||
|
||||
#include <boost/pfr.hpp>
|
||||
#include <sp/common/DataBuffer.h>
|
||||
|
||||
namespace sp {
|
||||
|
||||
template <typename T>
|
||||
using is_default_serializable = std::bool_constant<(std::is_class_v<T> && std::is_aggregate_v<T>) || !std::is_class_v<T>>;
|
||||
|
||||
template <typename T>
|
||||
static constexpr bool is_default_serializable_v = is_default_serializable<T>::value;
|
||||
|
||||
namespace details {
|
||||
|
||||
template <typename T>
|
||||
void WriteRaw(DataBuffer& a_Buffer, T a_Data);
|
||||
|
||||
template <typename T>
|
||||
void WriteFields(DataBuffer& a_Buffer, const T& a_Data);
|
||||
|
||||
template <typename T>
|
||||
void WriteSharedPtr(DataBuffer& a_Buffer, const std::shared_ptr<T>& a_Data);
|
||||
|
||||
template <typename T>
|
||||
void WriteUniquePtr(DataBuffer& a_Buffer, const std::unique_ptr<T>& a_Data);
|
||||
|
||||
template <typename T>
|
||||
void WriteVector(DataBuffer& a_Buffer, const std::vector<T>& a_Data);
|
||||
|
||||
template <typename T>
|
||||
void WriteList(DataBuffer& a_Buffer, const std::list<T>& a_Data);
|
||||
|
||||
template <typename K, typename V>
|
||||
void WriteMap(DataBuffer& a_Buffer, const std::map<K, V>& a_Data);
|
||||
|
||||
template <typename K, typename V>
|
||||
void WriteUnorderedMap(DataBuffer& a_Buffer, const std::unordered_map<K, V>& a_Data);
|
||||
|
||||
template <typename K, typename V>
|
||||
void WritePair(DataBuffer& a_Buffer, const std::pair<K, V>& a_Data);
|
||||
|
||||
template <typename T, std::size_t S>
|
||||
void WriteArray(DataBuffer& a_Buffer, const std::array<T, S>& a_Data);
|
||||
|
||||
|
||||
|
||||
template <typename T>
|
||||
void ReadRaw(DataBuffer& a_Buffer, T& a_Data);
|
||||
|
||||
template <typename T>
|
||||
void ReadFields(DataBuffer& a_Buffer, T& a_Data);
|
||||
|
||||
template <typename T>
|
||||
void ReadSharedPtr(DataBuffer& a_Buffer, std::shared_ptr<T>& a_Data);
|
||||
|
||||
template <typename T>
|
||||
void ReadUniquePtr(DataBuffer& a_Buffer, std::unique_ptr<T>& a_Data);
|
||||
|
||||
template <typename T>
|
||||
void ReadVector(DataBuffer& a_Buffer, std::vector<T>& a_Data);
|
||||
|
||||
template <typename T>
|
||||
void ReadList(DataBuffer& a_Buffer, std::list<T>& a_Data);
|
||||
|
||||
template <typename K, typename V>
|
||||
void ReadMap(DataBuffer& a_Buffer, std::map<K, V>& a_Data);
|
||||
|
||||
template <typename K, typename V>
|
||||
void ReadUnorderedMap(DataBuffer& a_Buffer, std::unordered_map<K, V>& a_Data);
|
||||
|
||||
template <typename K, typename V>
|
||||
void ReadPair(DataBuffer& a_Buffer, std::pair<K, V>& a_Data);
|
||||
|
||||
template <typename T, std::size_t S>
|
||||
void ReadArray(DataBuffer& a_Buffer, std::array<T, S>& a_Data);
|
||||
|
||||
} // namespace details
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* \brief Append data to the buffer (converted to big endian)
|
||||
*/
|
||||
template <typename T, typename = typename std::enable_if_t<is_default_serializable_v<T>>>
|
||||
DataBuffer& operator<<(DataBuffer& a_Buffer, const T& a_Data) {
|
||||
if constexpr (std::is_class_v<T>) {
|
||||
details::WriteFields(a_Buffer, a_Data);
|
||||
} else {
|
||||
details::WriteRaw(a_Buffer, a_Data);
|
||||
}
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Append a string to the buffer
|
||||
* \warning Don't use it for binary data !
|
||||
* \param str The string to append
|
||||
*/
|
||||
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::string& str);
|
||||
|
||||
/**
|
||||
* \brief Operator << to write a DataBuffer to an ostream
|
||||
*/
|
||||
std::ostream& operator<<(std::ostream& os, const DataBuffer& buffer);
|
||||
|
||||
/**
|
||||
* \brief Append a pointer to the buffer
|
||||
* \param data The data to append
|
||||
*/
|
||||
template <typename T, typename = typename std::enable_if_t<!std::is_abstract_v<T>>>
|
||||
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::shared_ptr<T>& a_Data) {
|
||||
details::WriteSharedPtr(a_Buffer, a_Data);
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Append a pointer to the buffer
|
||||
* \param data The data to append
|
||||
*/
|
||||
template <typename T, typename = typename std::enable_if_t<!std::is_abstract_v<T>>>
|
||||
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::unique_ptr<T>& a_Data) {
|
||||
details::WriteUniquePtr(a_Buffer, a_Data);
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Append a vector to the buffer by first writing the size
|
||||
* \param data The vector to append
|
||||
*/
|
||||
template <typename T>
|
||||
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::vector<T>& a_Data) {
|
||||
details::WriteVector(a_Buffer, a_Data);
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Append a list to the buffer by first writing the size
|
||||
* \param data The list to append
|
||||
*/
|
||||
template <typename T>
|
||||
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::list<T>& a_Data) {
|
||||
details::WriteList(a_Buffer, a_Data);
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* \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<<(DataBuffer& a_Buffer, const std::map<K, V>& a_Data) {
|
||||
details::WriteMap(a_Buffer, a_Data);
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* \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<<(DataBuffer& a_Buffer, const std::unordered_map<K, V>& a_Data) {
|
||||
details::WriteUnorderedMap(a_Buffer, a_Data);
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Append a pair to the buffer
|
||||
* \param data The pair to append
|
||||
*/
|
||||
template <typename K, typename V>
|
||||
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::pair<K, V>& a_Data) {
|
||||
details::WritePair(a_Buffer, a_Data);
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Append an array to the buffer by first writing the size
|
||||
* \param data The buffer to append
|
||||
*/
|
||||
template <typename T, std::size_t Size>
|
||||
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::array<T, Size>& a_Data) {
|
||||
details::WriteArray(a_Buffer, a_Data);
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* \brief Read some data from the buffer and assign to desired variable
|
||||
*/
|
||||
template <typename T, typename = typename std::enable_if_t<is_default_serializable_v<T>>>
|
||||
DataBuffer& operator>>(DataBuffer& a_Buffer, T& a_Data) {
|
||||
if constexpr (std::is_class_v<T>) {
|
||||
details::ReadFields(a_Buffer, a_Data);
|
||||
} else {
|
||||
details::ReadRaw(a_Buffer, a_Data);
|
||||
}
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Read some data from the buffer and assign to the new buffer
|
||||
* \param data The buffer to assign
|
||||
*/
|
||||
DataBuffer& operator>>(DataBuffer& a_Buffer, DataBuffer& a_Data);
|
||||
|
||||
/**
|
||||
* \brief Read a string from the buffer
|
||||
* \param str The string to assign in the new buffer
|
||||
* \warning Don't use it for binary data !
|
||||
*/
|
||||
DataBuffer& operator>>(DataBuffer& a_Buffer, std::string& str);
|
||||
|
||||
/**
|
||||
* \brief Read a pointer
|
||||
*/
|
||||
template <typename T, typename = typename std::enable_if_t<!std::is_abstract_v<T>>>
|
||||
DataBuffer& operator>>(DataBuffer& a_Buffer, std::shared_ptr<T>& a_Data) {
|
||||
details::ReadSharedPtr(a_Buffer, a_Data);
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Read a pointer
|
||||
*/
|
||||
template <typename T, typename = typename std::enable_if_t<!std::is_abstract_v<T>>>
|
||||
DataBuffer& operator>>(DataBuffer& a_Buffer, std::unique_ptr<T>& a_Data) {
|
||||
details::ReadUniquePtr(a_Buffer, a_Data);
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Read a vector (size + data) from the buffer
|
||||
* \pre The vector is assumed to be empty
|
||||
*/
|
||||
template <typename T>
|
||||
DataBuffer& operator>>(DataBuffer& a_Buffer, std::vector<T>& a_Data) {
|
||||
details::ReadVector(a_Buffer, a_Data);
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Read a list (size + data) from the buffer
|
||||
* \pre The list is assumed to be empty
|
||||
*/
|
||||
template <typename T>
|
||||
DataBuffer& operator>>(DataBuffer& a_Buffer, std::list<T>& a_Data) {
|
||||
details::ReadList(a_Buffer, a_Data);
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Read a map (size + data) from the buffer
|
||||
* \pre The map is assumed to be empty
|
||||
*/
|
||||
template <typename K, typename V>
|
||||
DataBuffer& operator>>(DataBuffer& a_Buffer, std::map<K, V>& a_Data) {
|
||||
details::ReadMap(a_Buffer, a_Data);
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Read a map (size + data) from the buffer
|
||||
* \pre The map is assumed to be empty
|
||||
*/
|
||||
template <typename K, typename V>
|
||||
DataBuffer& operator>>(DataBuffer& a_Buffer, std::unordered_map<K, V>& a_Data) {
|
||||
details::ReadUnorderedMap(a_Buffer, a_Data);
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Read a pair
|
||||
*/
|
||||
template <typename K, typename V>
|
||||
DataBuffer& operator>>(DataBuffer& a_Buffer, std::pair<K, V>& a_Data) {
|
||||
details::ReadPair(a_Buffer, a_Data);
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Read an array from the buffer
|
||||
*/
|
||||
template <std::size_t Size, typename T>
|
||||
DataBuffer& operator>>(DataBuffer& a_Buffer, std::array<T, Size>& a_Data) {
|
||||
details::ReadArray(a_Buffer, a_Data);
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
namespace details {
|
||||
|
||||
template <typename T>
|
||||
void WriteRaw(DataBuffer& a_Buffer, T a_Data) {
|
||||
SwapBytes(a_Data);
|
||||
a_Buffer.Append(a_Data);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void WriteFields(DataBuffer& a_Buffer, const T& a_Data) {
|
||||
boost::pfr::for_each_field(a_Data, [&a_Buffer](const auto& a_Field) { a_Buffer << a_Field; });
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void WriteSharedPtr(DataBuffer& a_Buffer, const std::shared_ptr<T>& a_Data) {
|
||||
a_Buffer << *a_Data;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void WriteUniquePtr(DataBuffer& a_Buffer, const std::unique_ptr<T>& a_Data) {
|
||||
a_Buffer << *a_Data;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void WriteVector(DataBuffer& a_Buffer, const std::vector<T>& a_Data) {
|
||||
a_Buffer << VarInt{a_Data.size()};
|
||||
for (const auto& element : a_Data) {
|
||||
a_Buffer << element;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void WriteList(DataBuffer& a_Buffer, const std::list<T>& a_Data) {
|
||||
a_Buffer << VarInt{a_Data.size()};
|
||||
for (const auto& element : a_Data) {
|
||||
a_Buffer << element;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
void WriteMap(DataBuffer& a_Buffer, const std::map<K, V>& a_Data) {
|
||||
a_Buffer << VarInt{a_Data.size()};
|
||||
for (const auto& [key, value] : a_Data) {
|
||||
a_Buffer << key << value;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
void WriteUnorderedMap(DataBuffer& a_Buffer, const std::unordered_map<K, V>& a_Data) {
|
||||
a_Buffer << VarInt{a_Data.size()};
|
||||
for (const auto& [key, value] : a_Data) {
|
||||
a_Buffer << key << value;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
void WritePair(DataBuffer& a_Buffer, const std::pair<K, V>& a_Data) {
|
||||
a_Buffer << a_Data.first << a_Data.second;
|
||||
}
|
||||
|
||||
template <typename T, std::size_t S>
|
||||
void WriteArray(DataBuffer& a_Buffer, const std::array<T, S>& a_Data) {
|
||||
for (const auto& element : a_Data) {
|
||||
a_Buffer << element;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template <typename T>
|
||||
void ReadRaw(DataBuffer& a_Buffer, T& a_Data) {
|
||||
a_Buffer.Read(a_Data);
|
||||
SwapBytes(a_Data);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void ReadFields(DataBuffer& a_Buffer, T& a_Data) {
|
||||
boost::pfr::for_each_field(a_Data, [&a_Buffer](auto& a_Field) { a_Buffer >> a_Field; });
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void ReadSharedPtr(DataBuffer& a_Buffer, std::shared_ptr<T>& a_Data) {
|
||||
a_Data = std::make_shared<T>();
|
||||
a_Buffer >> *a_Data;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void ReadUniquePtr(DataBuffer& a_Buffer, std::unique_ptr<T>& a_Data) {
|
||||
a_Data = std::make_unique<T>();
|
||||
a_Buffer >> *a_Data;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void ReadVector(DataBuffer& a_Buffer, std::vector<T>& a_Data) {
|
||||
VarInt arraySize;
|
||||
a_Buffer >> arraySize;
|
||||
for (std::size_t i = 0; i < arraySize.GetValue(); i++) {
|
||||
T newElement;
|
||||
a_Buffer >> newElement;
|
||||
a_Data.push_back(newElement);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void ReadList(DataBuffer& a_Buffer, std::list<T>& a_Data) {
|
||||
VarInt arraySize;
|
||||
a_Buffer >> arraySize;
|
||||
for (std::size_t i = 0; i < arraySize.GetValue(); i++) {
|
||||
T newElement;
|
||||
a_Buffer >> newElement;
|
||||
a_Data.push_back(newElement);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
void ReadMap(DataBuffer& a_Buffer, std::map<K, V>& a_Data) {
|
||||
VarInt mapSize;
|
||||
a_Buffer >> mapSize;
|
||||
for (std::size_t i = 0; i < mapSize.GetValue(); i++) {
|
||||
K newKey;
|
||||
V newValue;
|
||||
a_Buffer >> newKey >> newValue;
|
||||
a_Data.emplace(newKey, newValue);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
void ReadUnorderedMap(DataBuffer& a_Buffer, std::unordered_map<K, V>& a_Data) {
|
||||
VarInt mapSize;
|
||||
a_Buffer >> mapSize;
|
||||
for (std::size_t i = 0; i < mapSize.GetValue(); i++) {
|
||||
K newKey;
|
||||
V newValue;
|
||||
a_Buffer >> newKey >> newValue;
|
||||
a_Data.emplace(newKey, newValue);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename K, typename V>
|
||||
void ReadPair(DataBuffer& a_Buffer, std::pair<K, V>& a_Data) {
|
||||
a_Buffer >> a_Data.first >> a_Data.a_Data;
|
||||
}
|
||||
|
||||
template <typename T, std::size_t S>
|
||||
void ReadArray(DataBuffer& a_Buffer, std::array<T, S>& a_Data) {
|
||||
for (std::size_t i = 0; i < S; i++) {
|
||||
T newElement;
|
||||
a_Buffer >> newElement;
|
||||
a_Data[i] = newElement;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace details
|
||||
|
||||
} // namespace sp
|
||||
139
include/sp/common/GenericHandler.h
Normal file
139
include/sp/common/GenericHandler.h
Normal file
@@ -0,0 +1,139 @@
|
||||
#pragma once
|
||||
|
||||
#include <tuple>
|
||||
|
||||
namespace sp
|
||||
{
|
||||
// This class is inspired by https://arobenko.gitbooks.io/comms-protocols-cpp/content/
|
||||
|
||||
// TCommon is common interface class for all the messages
|
||||
// TAll is all the message types, that need to be handled, bundled in std::tuple
|
||||
template <typename TAll>
|
||||
class GenericHandler;
|
||||
|
||||
// Big boy to process packets 20 by 20, preventing needlessly copying vtable many times at each inheritance stage
|
||||
template <typename T1, typename T2, typename T3, typename T4, typename T5,
|
||||
typename T6, typename T7, typename T8, typename T9, typename T10,
|
||||
typename T11, typename T12, typename T13, typename T14, typename T15,
|
||||
typename T16, typename T17, typename T18, typename T19, typename T20,
|
||||
typename... TRest>
|
||||
class GenericHandler<std::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T11, T13, T14, T15, T16, T17, T18, T19, T20, TRest...> > : public GenericHandler<std::tuple<TRest...> >
|
||||
{
|
||||
using Base = GenericHandler<std::tuple<TRest...> >;
|
||||
public:
|
||||
using Base::Handle; // Don't hide all Handle() functions from base classes
|
||||
virtual void Handle(const T1& msg) {}
|
||||
virtual void Handle(const T2& msg) {}
|
||||
virtual void Handle(const T3& msg) {}
|
||||
virtual void Handle(const T4& msg) {}
|
||||
virtual void Handle(const T5& msg) {}
|
||||
virtual void Handle(const T6& msg) {}
|
||||
virtual void Handle(const T7& msg) {}
|
||||
virtual void Handle(const T8& msg) {}
|
||||
virtual void Handle(const T9& msg) {}
|
||||
virtual void Handle(const T10& msg) {}
|
||||
virtual void Handle(const T11& msg) {}
|
||||
virtual void Handle(const T12& msg) {}
|
||||
virtual void Handle(const T13& msg) {}
|
||||
virtual void Handle(const T14& msg) {}
|
||||
virtual void Handle(const T15& msg) {}
|
||||
virtual void Handle(const T16& msg) {}
|
||||
virtual void Handle(const T17& msg) {}
|
||||
virtual void Handle(const T18& msg) {}
|
||||
virtual void Handle(const T19& msg) {}
|
||||
virtual void Handle(const T20& msg) {}
|
||||
};
|
||||
|
||||
// 10 by 10
|
||||
template <typename T1, typename T2, typename T3, typename T4, typename T5,
|
||||
typename T6, typename T7, typename T8, typename T9, typename T10,
|
||||
typename... TRest>
|
||||
class GenericHandler<std::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TRest...> > : public GenericHandler<std::tuple<TRest...> >
|
||||
{
|
||||
using Base = GenericHandler<std::tuple<TRest...> >;
|
||||
public:
|
||||
using Base::Handle; // Don't hide all Handle() functions from base classes
|
||||
virtual void Handle(const T1& msg) {}
|
||||
virtual void Handle(const T2& msg) {}
|
||||
virtual void Handle(const T3& msg) {}
|
||||
virtual void Handle(const T4& msg) {}
|
||||
virtual void Handle(const T5& msg) {}
|
||||
virtual void Handle(const T6& msg) {}
|
||||
virtual void Handle(const T7& msg) {}
|
||||
virtual void Handle(const T8& msg) {}
|
||||
virtual void Handle(const T9& msg) {}
|
||||
virtual void Handle(const T10& msg) {}
|
||||
};
|
||||
|
||||
// 5 by 5
|
||||
template <
|
||||
typename T1, typename T2, typename T3, typename T4, typename T5,
|
||||
typename... TRest>
|
||||
class GenericHandler<std::tuple<T1, T2, T3, T4, T5, TRest...> > : public GenericHandler<std::tuple<TRest...> >
|
||||
{
|
||||
using Base = GenericHandler<std::tuple<TRest...> >;
|
||||
public:
|
||||
using Base::Handle; // Don't hide all Handle() functions from base classes
|
||||
virtual void Handle(const T1& msg) {}
|
||||
virtual void Handle(const T2& msg) {}
|
||||
virtual void Handle(const T3& msg) {}
|
||||
virtual void Handle(const T4& msg) {}
|
||||
virtual void Handle(const T5& msg) {}
|
||||
};
|
||||
|
||||
// Deal with rest with 4 types
|
||||
template <typename T1, typename T2, typename T3, typename T4>
|
||||
class GenericHandler<std::tuple<T1, T2, T3, T4> >
|
||||
{
|
||||
public:
|
||||
virtual ~GenericHandler() {}
|
||||
virtual void Handle(const T1& msg) {}
|
||||
virtual void Handle(const T2& msg) {}
|
||||
virtual void Handle(const T3& msg) {}
|
||||
virtual void Handle(const T4& msg) {}
|
||||
// virtual void Handle(const TCommon&) {} //Nothing to do
|
||||
};
|
||||
|
||||
// Deal with rest with 3 types
|
||||
template < typename T1, typename T2, typename T3>
|
||||
class GenericHandler<std::tuple<T1, T2, T3> >
|
||||
{
|
||||
public:
|
||||
virtual ~GenericHandler() {}
|
||||
virtual void Handle(const T1& msg) {}
|
||||
virtual void Handle(const T2& msg) {}
|
||||
virtual void Handle(const T3& msg) {}
|
||||
// virtual void Handle(const TCommon&) {} //Nothing to do
|
||||
};
|
||||
|
||||
// Deal with rest with 2 types
|
||||
template <typename T1, typename T2>
|
||||
class GenericHandler<std::tuple<T1, T2> >
|
||||
{
|
||||
public:
|
||||
virtual ~GenericHandler() {}
|
||||
virtual void Handle(const T1& msg) {}
|
||||
virtual void Handle(const T2& msg) {}
|
||||
// virtual void Handle(const TCommon&) {} //Nothing to do
|
||||
};
|
||||
|
||||
// Deal with rest with 1 type
|
||||
template <typename T1>
|
||||
class GenericHandler<std::tuple<T1> >
|
||||
{
|
||||
public:
|
||||
virtual ~GenericHandler() {}
|
||||
virtual void Handle(const T1& msg) {}
|
||||
// virtual void Handle(const TCommon&) {} //Nothing to do
|
||||
};
|
||||
|
||||
// Deal with rest with 0 type
|
||||
template <>
|
||||
class GenericHandler<std::tuple<> >
|
||||
{
|
||||
public:
|
||||
virtual ~GenericHandler() {}
|
||||
virtual void Handle() {} //Nothing to do
|
||||
};
|
||||
|
||||
} // sp
|
||||
@@ -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,12 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace sp {
|
||||
|
||||
/// @brief Concat multiple tuples in one big tuple
|
||||
/// @tparam ...input_t Multiple std::tuple types to concat
|
||||
template <typename... input_t>
|
||||
using tuple_cat_t = decltype(std::tuple_cat(std::declval<input_t>()...));
|
||||
|
||||
} // namespace sp
|
||||
99
include/sp/common/Tuples.h
Normal file
99
include/sp/common/Tuples.h
Normal file
@@ -0,0 +1,99 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <tuple>
|
||||
|
||||
namespace sp {
|
||||
|
||||
/// @brief Concat multiple tuples in one big tuple
|
||||
/// @tparam ...input_t Multiple std::tuple types to concat
|
||||
template <typename... input_t>
|
||||
using tuple_cat_t = decltype(std::tuple_cat(std::declval<input_t>()...));
|
||||
|
||||
namespace details {
|
||||
|
||||
template <std::size_t TRem>
|
||||
class TupleForEachHelper {
|
||||
public:
|
||||
template <typename TTuple, typename TFunc>
|
||||
static void Exec(TTuple&& tuple, TFunc&& func) {
|
||||
using Tuple = typename std::decay<TTuple>::type;
|
||||
static const std::size_t TupleSize = std::tuple_size<Tuple>::value;
|
||||
static_assert(TRem <= TupleSize, "Incorrect parameters");
|
||||
|
||||
// Invoke function with current element
|
||||
static const std::size_t Idx = TupleSize - TRem;
|
||||
func(std::get<Idx>(std::forward<TTuple>(tuple)));
|
||||
|
||||
// Compile time recursion - invoke function with the remaining elements
|
||||
TupleForEachHelper<TRem - 1>::Exec(std::forward<TTuple>(tuple), std::forward<TFunc>(func));
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
class TupleForEachHelper<0> {
|
||||
public:
|
||||
// Stop compile time recursion
|
||||
template <typename TTuple, typename TFunc>
|
||||
static void Exec(TTuple&& tuple, TFunc&& func) {
|
||||
static_cast<void>(tuple);
|
||||
static_cast<void>(func);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
|
||||
template <typename TFunc, typename TTuple>
|
||||
void TupleForEach(TFunc&& func, TTuple&& tuple) {
|
||||
using Tuple = typename std::decay<TTuple>::type;
|
||||
static const std::size_t TupleSize = std::tuple_size<Tuple>::value;
|
||||
|
||||
details::TupleForEachHelper<TupleSize>::Exec(std::forward<TTuple>(tuple), std::forward<TFunc>(func));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
namespace details {
|
||||
|
||||
template <typename T, typename U = void>
|
||||
struct is_mappish_impl : std::false_type {};
|
||||
|
||||
template <typename T>
|
||||
struct is_mappish_impl<T, std::void_t<typename T::key_type, typename T::mapped_type,
|
||||
decltype(std::declval<T&>()[std::declval<const typename T::key_type&>()])>> : std::true_type {};
|
||||
|
||||
template <typename T, typename U = void>
|
||||
struct is_vectish_impl : std::false_type {};
|
||||
|
||||
template <typename T>
|
||||
struct is_vectish_impl<T,
|
||||
std::void_t<typename T::value_type, decltype(std::declval<T&>()[std::declval<const typename T::value_type&>()])>>
|
||||
: std::true_type {};
|
||||
|
||||
template <typename T, typename U = void>
|
||||
struct is_pairish_impl : std::false_type {};
|
||||
|
||||
template <typename T>
|
||||
struct is_pairish_impl<T,
|
||||
std::void_t<typename T::first_type, decltype(std::declval<T&>()[std::declval<const typename T::first_type&>()])>>
|
||||
: std::true_type {};
|
||||
|
||||
template <typename T>
|
||||
using is_general_t = std::integral_constant<bool,
|
||||
(std::is_same_v<T, std::string> || (!std::is_same_v<T, char> && !std::is_same_v<T, unsigned char> && !std::is_arithmetic_v<T> &&
|
||||
!is_pairish_impl<T>::value && !is_mappish_impl<T>::value && !is_vectish_impl<T>::value))>;
|
||||
|
||||
template <typename T>
|
||||
using is_primitive =
|
||||
std::integral_constant<bool, std::is_same_v<T, char> || std::is_same_v<T, unsigned char> || std::is_arithmetic_v<T>>;
|
||||
|
||||
} // namespace details
|
||||
|
||||
} // namespace sp
|
||||
@@ -7,11 +7,14 @@
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
|
||||
namespace sp {
|
||||
|
||||
class DataBuffer;
|
||||
|
||||
using ReadFunc = std::function<void(std::uint8_t&)>;
|
||||
|
||||
/**
|
||||
* \class VarInt
|
||||
* \brief Variable-length format such that smaller numbers use fewer bytes.
|
||||
@@ -21,6 +24,8 @@ class VarInt {
|
||||
std::uint64_t m_Value;
|
||||
|
||||
public:
|
||||
static const std::uint64_t MAX_VALUE = static_cast<std::uint64_t>(-1) >> 8;
|
||||
|
||||
VarInt() : m_Value(0) {}
|
||||
/**
|
||||
* \brief Construct a variable integer from a value
|
||||
@@ -53,6 +58,8 @@ class VarInt {
|
||||
* \param var The variable integer to deserialize
|
||||
*/
|
||||
friend DataBuffer& operator>>(DataBuffer& in, VarInt& var);
|
||||
|
||||
void Read(const ReadFunc& read);
|
||||
};
|
||||
|
||||
} // namespace sp
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/protocol/GenericHandler.h>
|
||||
#include <sp/protocol/Message.h>
|
||||
|
||||
namespace sp {
|
||||
class PacketHandler;
|
||||
|
||||
using PacketMessage = Message<option::MsgIdType<std::uint8_t>, // add id() operation
|
||||
option::ReadOperations, // add read() operation
|
||||
option::WriteOperations, // add write() operation
|
||||
option::Handler<PacketHandler> // add dispatch() operation
|
||||
>;
|
||||
|
||||
#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>> { \
|
||||
public: \
|
||||
packetName##Packet() {} \
|
||||
template <typename... Args> \
|
||||
packetName##Packet(Args... args) { \
|
||||
Construct(args...); \
|
||||
} \
|
||||
}
|
||||
|
||||
} // namespace sp
|
||||
@@ -1,7 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/protocol/MessageFactory.h>
|
||||
|
||||
namespace sp {
|
||||
using PacketFactory = sp::MessageFactory<sp::PacketMessage, AllPackets>;
|
||||
} // namespace sp
|
||||
@@ -1,9 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/protocol/GenericHandler.h>
|
||||
|
||||
// the tuple AllPackets must be defined !
|
||||
|
||||
namespace sp {
|
||||
class PacketHandler : public sp::GenericHandler<PacketMessage, AllPackets> {};
|
||||
} // namespace sp
|
||||
47
include/sp/extensions/Compress.h
Normal file
47
include/sp/extensions/Compress.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* \file Compression.h
|
||||
* \brief File containing compress utilities
|
||||
*/
|
||||
|
||||
#include <cstdint>
|
||||
#include <sp/io/MessageEncapsulator.h>
|
||||
|
||||
namespace sp {
|
||||
namespace zlib {
|
||||
|
||||
/**
|
||||
* \brief Compress some data
|
||||
* \param buffer the data to compress
|
||||
* \return the compressed data
|
||||
*/
|
||||
DataBuffer Compress(const DataBuffer& buffer, std::size_t a_CompressionThreshold = 64);
|
||||
|
||||
/**
|
||||
* \brief Uncompress some data
|
||||
* \param buffer the data to uncompress
|
||||
* \param packetLength lenght of data
|
||||
* \return the uncompressed data
|
||||
*/
|
||||
DataBuffer Decompress(DataBuffer& buffer, std::uint64_t packetLength);
|
||||
|
||||
} // namespace zlib
|
||||
|
||||
|
||||
class ZlibCompress : public MessageEncapsulator {
|
||||
private:
|
||||
std::size_t m_CompressionThreshold;
|
||||
|
||||
public:
|
||||
ZlibCompress() : m_CompressionThreshold(64) {}
|
||||
ZlibCompress(const ZlibCompress&) = default;
|
||||
virtual ~ZlibCompress() {}
|
||||
|
||||
protected:
|
||||
virtual DataBuffer EncapsulateImpl(const DataBuffer& a_Data) override;
|
||||
virtual DataBuffer DecapsulateImpl(DataBuffer& a_Data) override;
|
||||
};
|
||||
|
||||
|
||||
} // namespace sp
|
||||
9
include/sp/extensions/Extensions.h
Normal file
9
include/sp/extensions/Extensions.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#if __has_include(<sp/extensions/Compress.h>)
|
||||
#include <sp/extensions/Compress.h>
|
||||
#endif
|
||||
|
||||
#if __has_include(<sp/extensions/Tcp.h>)
|
||||
#include <sp/extensions/Tcp.h>
|
||||
#endif
|
||||
4
include/sp/extensions/Tcp.h
Normal file
4
include/sp/extensions/Tcp.h
Normal file
@@ -0,0 +1,4 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/extensions/tcp/TcpListener.h>
|
||||
#include <sp/extensions/tcp/TcpSocket.h>
|
||||
68
include/sp/extensions/tcp/TcpListener.h
Normal file
68
include/sp/extensions/tcp/TcpListener.h
Normal file
@@ -0,0 +1,68 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/extensions/tcp/TcpSocket.h>
|
||||
#include <memory>
|
||||
|
||||
namespace sp {
|
||||
namespace io {
|
||||
|
||||
/**
|
||||
* \class TcpListener
|
||||
*/
|
||||
class TcpListener : private NonCopyable {
|
||||
public:
|
||||
using SocketHandle = TcpSocket::SocketHandle;
|
||||
|
||||
/**
|
||||
* \brief Starts listening for guests to connect
|
||||
* \param port The port to listen to
|
||||
* \param maxConnexions The maximum amount of connexion that can happen at the same time. \n
|
||||
* Every other guests will be kicked if this amount is reached.
|
||||
* \return Whether this action was succesfull
|
||||
*/
|
||||
TcpListener(std::uint16_t a_Port, int a_MaxConnexions);
|
||||
|
||||
/**
|
||||
* \brief Default destructor
|
||||
*/
|
||||
~TcpListener();
|
||||
|
||||
/**
|
||||
* \brief Tries to accept an incoming request to connect
|
||||
* \return the new socket if a new connexion was accepted or nullptr
|
||||
*/
|
||||
std::unique_ptr<TcpSocket> Accept();
|
||||
|
||||
/**
|
||||
* \brief Closes the socket
|
||||
*/
|
||||
void Close();
|
||||
|
||||
/**
|
||||
* \brief Allows to set the socket in non blocking/blocking mode
|
||||
* \param a_Blocking If set to true, every call to Read will wait until the socket receives something
|
||||
* \return true if the operation was successful
|
||||
*/
|
||||
bool SetBlocking(bool a_Blocking);
|
||||
|
||||
/**
|
||||
* \brief Getter of the m_Port member
|
||||
* \return The port which the socket listen to
|
||||
*/
|
||||
std::uint16_t GetListeningPort() const;
|
||||
|
||||
/**
|
||||
* \brief Getter of the m_MaxConnections member
|
||||
* \return The maximum amount of connexions that can happen at the same time.
|
||||
*/
|
||||
int GetMaximumConnections() const;
|
||||
|
||||
private:
|
||||
SocketHandle m_Handle;
|
||||
std::uint16_t m_Port;
|
||||
int m_MaxConnections;
|
||||
};
|
||||
|
||||
|
||||
} // namespace io
|
||||
} // namespace sp
|
||||
79
include/sp/extensions/tcp/TcpSocket.h
Normal file
79
include/sp/extensions/tcp/TcpSocket.h
Normal file
@@ -0,0 +1,79 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/io/IoInterface.h>
|
||||
|
||||
namespace sp {
|
||||
namespace io {
|
||||
|
||||
class TcpListener;
|
||||
|
||||
class SocketError : public std::exception {
|
||||
private:
|
||||
std::string m_Error;
|
||||
|
||||
public:
|
||||
SocketError(std::string&& a_Msg) : m_Error(std::move(a_Msg)) {}
|
||||
|
||||
virtual const char* what() const noexcept override {
|
||||
return m_Error.c_str();
|
||||
}
|
||||
};
|
||||
|
||||
class TcpSocket : public sp::IoInterface {
|
||||
public:
|
||||
using SocketHandle = int;
|
||||
|
||||
/**
|
||||
* \enum Status
|
||||
* \brief Describes the state of a socket
|
||||
*/
|
||||
enum class Status {
|
||||
/** The socket is connected */
|
||||
Connected,
|
||||
/** The socket is not connected */
|
||||
Disconnected,
|
||||
/** Something bad happened */
|
||||
Error,
|
||||
};
|
||||
|
||||
TcpSocket();
|
||||
TcpSocket(const std::string& a_Host, std::uint16_t a_Port);
|
||||
TcpSocket(TcpSocket&& a_Other);
|
||||
TcpSocket& operator=(TcpSocket&& a_Other);
|
||||
virtual ~TcpSocket();
|
||||
|
||||
virtual DataBuffer Read(std::size_t a_Amount) override;
|
||||
virtual void Write(const sp::DataBuffer& a_Data) override;
|
||||
|
||||
/**
|
||||
* \brief Allows to set the socket in non blocking/blocking mode
|
||||
* \param a_Block If set to true, every call to Read will wait until the socket receives something
|
||||
* \return true if the operation was successful
|
||||
*/
|
||||
bool SetBlocking(bool a_Block);
|
||||
|
||||
/**
|
||||
* \brief Getter of the m_Status member
|
||||
* \return The TcpSocket::Status of this socket
|
||||
*/
|
||||
Status GetStatus() const;
|
||||
|
||||
/**
|
||||
* \brief Disconnects the socket from the remote
|
||||
* \note Does nothing if the socket is not connected. \n
|
||||
* This function is also called by the destructor.
|
||||
*/
|
||||
void Disconnect();
|
||||
|
||||
|
||||
private:
|
||||
SocketHandle m_Handle;
|
||||
Status m_Status;
|
||||
|
||||
void Connect(const std::string& a_Host, std::uint16_t a_Port);
|
||||
|
||||
friend class TcpListener;
|
||||
};
|
||||
|
||||
} // namespace io
|
||||
} // namespace sp
|
||||
90
include/sp/io/BitBuffer.h
Normal file
90
include/sp/io/BitBuffer.h
Normal file
@@ -0,0 +1,90 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/common/DataBuffer.h>
|
||||
#include <sp/protocol/BitField.h>
|
||||
|
||||
namespace sp {
|
||||
|
||||
// TODO: flush if offset exceeds 64
|
||||
class BitBuffer {
|
||||
private:
|
||||
using Data = std::uint64_t;
|
||||
|
||||
private:
|
||||
DataBuffer& m_Buffer;
|
||||
Data m_Data;
|
||||
std::size_t m_Offset;
|
||||
bool m_WasBitField;
|
||||
|
||||
public:
|
||||
BitBuffer(DataBuffer& a_Buffer) : m_Buffer(a_Buffer), m_Data(0), m_Offset(0), m_WasBitField(false) {}
|
||||
|
||||
void UpdateWrite(bool a_IsBitField) {
|
||||
if (m_Offset == 0)
|
||||
return;
|
||||
if ((m_Offset % 8 == 0) || (!a_IsBitField && m_WasBitField)) {
|
||||
Flush();
|
||||
}
|
||||
m_WasBitField = a_IsBitField;
|
||||
}
|
||||
|
||||
void UpdateRead(bool a_IsBitField) {
|
||||
if (m_Offset == 0)
|
||||
return;
|
||||
if ((m_Offset % 8 == 0) || (!a_IsBitField && m_WasBitField)) {
|
||||
MoveReadOffset();
|
||||
}
|
||||
m_WasBitField = a_IsBitField;
|
||||
}
|
||||
|
||||
template <typename T, std::size_t BitSize>
|
||||
void Append(T a_Data) {
|
||||
Data bin = static_cast<Data>(a_Data);
|
||||
bin &= ((1 << BitSize) - 1); // prevents overflow
|
||||
std::size_t pushCount = sizeof(Data) * 8 - m_Offset - BitSize;
|
||||
m_Data |= bin << pushCount;
|
||||
m_Offset += BitSize;
|
||||
}
|
||||
|
||||
template <typename T, std::size_t BitSize>
|
||||
void Read(T& a_Data) {
|
||||
std::size_t byteCount = GetByteCount(m_Offset + BitSize);
|
||||
constexpr Data dataMask = (1 << BitSize) - 1;
|
||||
m_Buffer.ReadSome(reinterpret_cast<std::uint8_t*>(&m_Data), byteCount);
|
||||
SwapBytes(reinterpret_cast<std::uint8_t*>(&m_Data), reinterpret_cast<std::uint8_t*>(&m_Data) + byteCount);
|
||||
m_Data >>= byteCount * 8 - m_Offset - BitSize;
|
||||
m_Data &= dataMask;
|
||||
a_Data = T(m_Data);
|
||||
m_Buffer.SetReadOffset(m_Buffer.GetReadOffset() - byteCount);
|
||||
m_Offset += BitSize;
|
||||
}
|
||||
|
||||
private:
|
||||
void Flush() {
|
||||
std::size_t byteCount = GetByteCount();
|
||||
m_Data >>= (sizeof(Data) - byteCount) * 8;
|
||||
SwapBytes(reinterpret_cast<std::uint8_t*>(&m_Data), reinterpret_cast<std::uint8_t*>(&m_Data) + byteCount);
|
||||
m_Buffer.WriteSome(reinterpret_cast<std::uint8_t*>(&m_Data), byteCount);
|
||||
m_Offset = 0;
|
||||
m_WasBitField = false;
|
||||
m_Data = 0;
|
||||
}
|
||||
|
||||
void MoveReadOffset() {
|
||||
std::size_t byteCount = GetByteCount();
|
||||
m_Buffer.SetReadOffset(m_Buffer.GetReadOffset() + byteCount);
|
||||
m_Offset = 0;
|
||||
m_WasBitField = false;
|
||||
m_Data = 0;
|
||||
}
|
||||
|
||||
std::size_t GetByteCount(std::size_t a_Offset = -1) const {
|
||||
if (a_Offset == static_cast<std::size_t>(-1))
|
||||
a_Offset = m_Offset;
|
||||
if (a_Offset <= 8)
|
||||
return 1;
|
||||
return (a_Offset - 1) / 8 + 1;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace sp
|
||||
14
include/sp/io/IoInterface.h
Normal file
14
include/sp/io/IoInterface.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/common/DataBuffer.h>
|
||||
#include <sp/common/NonCopyable.h>
|
||||
|
||||
namespace sp {
|
||||
|
||||
class IoInterface : private NonCopyable {
|
||||
public:
|
||||
virtual DataBuffer Read(std::size_t a_Amount) = 0;
|
||||
virtual void Write(const DataBuffer& a_Data) = 0;
|
||||
};
|
||||
|
||||
} // namespace sp
|
||||
31
include/sp/io/MessageEncapsulator.h
Normal file
31
include/sp/io/MessageEncapsulator.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/common/DataBuffer.h>
|
||||
|
||||
namespace sp {
|
||||
|
||||
class MessageEncapsulator {
|
||||
protected:
|
||||
bool m_Enabled = true;
|
||||
|
||||
public:
|
||||
MessageEncapsulator() {}
|
||||
virtual ~MessageEncapsulator() {}
|
||||
|
||||
DataBuffer Encapsulate(const DataBuffer& a_Data) {
|
||||
if (!m_Enabled)
|
||||
return a_Data;
|
||||
return EncapsulateImpl(a_Data);
|
||||
}
|
||||
DataBuffer Decapsulate(DataBuffer& a_Data) {
|
||||
if (!m_Enabled)
|
||||
return a_Data;
|
||||
return DecapsulateImpl(a_Data);
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual DataBuffer EncapsulateImpl(const DataBuffer& a_Data) = 0;
|
||||
virtual DataBuffer DecapsulateImpl(DataBuffer& a_Data) = 0;
|
||||
};
|
||||
|
||||
} // namespace sp
|
||||
53
include/sp/io/MessageIO.h
Normal file
53
include/sp/io/MessageIO.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/io/BitBuffer.h>
|
||||
#include <sp/common/DataBufferOperators.h>
|
||||
|
||||
namespace sp {
|
||||
namespace details {
|
||||
|
||||
template <typename T, std::size_t BitSize>
|
||||
void WriteField(DataBuffer& a_Buffer, const BitField<T, BitSize>& a_Data, BitBuffer& a_BitBuffer) {
|
||||
a_BitBuffer.Append<T, BitSize>(*a_Data);
|
||||
a_BitBuffer.UpdateWrite(true);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void WriteField(DataBuffer& a_Buffer, const T& a_Data, BitBuffer& a_BitBuffer) {
|
||||
a_Buffer << a_Data;
|
||||
a_BitBuffer.UpdateWrite(false);
|
||||
}
|
||||
|
||||
template <typename T, std::size_t BitSize>
|
||||
void ReadField(DataBuffer& a_Buffer, BitField<T, BitSize>& a_Data, BitBuffer& a_BitBuffer) {
|
||||
a_BitBuffer.Read<T, BitSize>(*a_Data);
|
||||
a_BitBuffer.UpdateRead(true);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void ReadField(DataBuffer& a_Buffer, T& a_Data, BitBuffer& a_BitBuffer) {
|
||||
a_Buffer >> a_Data;
|
||||
a_BitBuffer.UpdateRead(false);
|
||||
}
|
||||
|
||||
} // namespace details
|
||||
|
||||
template <typename TData>
|
||||
DataBuffer WriteMessage(const TData& a_MessageData) {
|
||||
DataBuffer buffer;
|
||||
BitBuffer bitBuffer(buffer);
|
||||
boost::pfr::for_each_field(
|
||||
a_MessageData, [&buffer, &bitBuffer](const auto& a_Field) { details::WriteField(buffer, a_Field, bitBuffer); });
|
||||
bitBuffer.UpdateWrite(false);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
template <typename TData>
|
||||
void ReadMessage(DataBuffer& a_Buffer, TData& a_MessageData) {
|
||||
BitBuffer bitBuffer(a_Buffer);
|
||||
boost::pfr::for_each_field(
|
||||
a_MessageData, [&a_Buffer, &bitBuffer](auto& a_Field) { details::ReadField(a_Buffer, a_Field, bitBuffer); });
|
||||
bitBuffer.UpdateRead(false);
|
||||
}
|
||||
|
||||
} // namespace sp
|
||||
54
include/sp/io/MessageStream.h
Normal file
54
include/sp/io/MessageStream.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/common/DataBuffer.h>
|
||||
#include <sp/io/IoInterface.h>
|
||||
#include <sp/io/MessageEncapsulator.h>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
namespace sp {
|
||||
|
||||
template <typename TMessageFactory>
|
||||
class MessageStream {
|
||||
protected:
|
||||
std::vector<std::shared_ptr<MessageEncapsulator>> m_Encapsulators;
|
||||
std::shared_ptr<IoInterface> m_Stream;
|
||||
|
||||
using MessageBaseType = typename TMessageFactory::MessageBaseType;
|
||||
using MessageIdType = typename MessageBaseType::MessageIdType;
|
||||
|
||||
public:
|
||||
MessageStream(std::shared_ptr<IoInterface>&& a_Stream) : m_Stream(std::move(a_Stream)) {}
|
||||
|
||||
template<typename... TEnc>
|
||||
MessageStream(std::shared_ptr<IoInterface>&& a_Stream, TEnc&&... a_Encapsulators) :
|
||||
m_Stream(std::move(a_Stream)){
|
||||
m_Encapsulators.reserve(sizeof...(a_Encapsulators));
|
||||
AddEncapsulators(std::move(a_Encapsulators ...));
|
||||
}
|
||||
|
||||
std::unique_ptr<MessageBaseType> ReadMessage();
|
||||
std::unique_ptr<MessageBaseType> ReadMessage(MessageIdType a_Id);
|
||||
|
||||
void WriteMessage(const MessageBaseType& a_Message, bool a_WriteId = true);
|
||||
|
||||
template<typename... Args>
|
||||
void AddEncapsulators(Args&& ... a_Encapsulators) {
|
||||
AddEncapsulators(std::move(std::make_tuple<>(a_Encapsulators ...)));
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
void AddEncapsulators(std::tuple<Args...>&& a_Encapsulators) {
|
||||
TupleForEach([this](auto&& a_Encapsulator){
|
||||
m_Encapsulators.push_back(std::move(a_Encapsulator));
|
||||
}, a_Encapsulators);
|
||||
}
|
||||
|
||||
private:
|
||||
DataBuffer ReadAndDecapsulate();
|
||||
std::unique_ptr<MessageBaseType> MakeMessage(DataBuffer& buffer, MessageIdType a_Id);
|
||||
};
|
||||
|
||||
} // namespace sp
|
||||
|
||||
#include <sp/io/MessageStream.inl>
|
||||
66
include/sp/io/MessageStream.inl
Normal file
66
include/sp/io/MessageStream.inl
Normal file
@@ -0,0 +1,66 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/common/VarInt.h>
|
||||
#include <sp/protocol/MessageFactory.h>
|
||||
|
||||
namespace sp {
|
||||
|
||||
template <typename TMessageFactory>
|
||||
DataBuffer MessageStream<TMessageFactory>::ReadAndDecapsulate() {
|
||||
VarInt messageLength;
|
||||
messageLength.Read([this](std::uint8_t& data) {
|
||||
DataBuffer buffer = m_Stream->Read(1);
|
||||
data = *buffer.data();
|
||||
});
|
||||
std::size_t amount = messageLength.GetValue();
|
||||
DataBuffer buffer = m_Stream->Read(amount);
|
||||
|
||||
for (auto& enc : m_Encapsulators) {
|
||||
buffer = enc->Decapsulate(buffer);
|
||||
}
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
template <typename TMessageFactory>
|
||||
std::unique_ptr<typename TMessageFactory::MessageBaseType> MessageStream<TMessageFactory>::MakeMessage(DataBuffer& buffer, MessageIdType a_Id) {
|
||||
static const TMessageFactory FACTORY;
|
||||
auto message = FACTORY.CreateMessage(a_Id);
|
||||
message->Read(buffer);
|
||||
return message;
|
||||
}
|
||||
|
||||
template <typename TMessageFactory>
|
||||
std::unique_ptr<typename TMessageFactory::MessageBaseType> MessageStream<TMessageFactory>::ReadMessage(MessageIdType a_Id) {
|
||||
|
||||
DataBuffer buffer = ReadAndDecapsulate();
|
||||
|
||||
return MakeMessage(buffer, a_Id);
|
||||
}
|
||||
|
||||
template <typename TMessageFactory>
|
||||
std::unique_ptr<typename TMessageFactory::MessageBaseType> MessageStream<TMessageFactory>::ReadMessage() {
|
||||
DataBuffer buffer = ReadAndDecapsulate();
|
||||
|
||||
VarInt messageId;
|
||||
buffer >> messageId;
|
||||
|
||||
return MakeMessage(buffer, MessageIdType(messageId.GetValue()));
|
||||
}
|
||||
|
||||
template <typename TMessageFactory>
|
||||
void MessageStream<TMessageFactory>::WriteMessage(const MessageBaseType& a_Message, bool a_WriteId) {
|
||||
DataBuffer buffer;
|
||||
if (a_WriteId)
|
||||
buffer << VarInt{static_cast<std::uint64_t>(a_Message.GetId())};
|
||||
buffer << a_Message.Write();
|
||||
for (auto& enc : m_Encapsulators) {
|
||||
buffer = enc->Encapsulate(buffer);
|
||||
}
|
||||
DataBuffer header;
|
||||
header << VarInt{buffer.GetSize()};
|
||||
m_Stream->Write(header);
|
||||
m_Stream->Write(buffer);
|
||||
}
|
||||
|
||||
} // namespace sp
|
||||
56
include/sp/io/SerializableMessage.h
Normal file
56
include/sp/io/SerializableMessage.h
Normal file
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/common/DataBuffer.h>
|
||||
|
||||
namespace sp {
|
||||
|
||||
template <typename TMessageFactory>
|
||||
class SerializableMessage {
|
||||
using MessageBaseType = typename TMessageFactory::MessageBaseType;
|
||||
using HandlerType = typename MessageBaseType::HandlerType;
|
||||
using MessageIdType = typename MessageBaseType::MessageIdType;
|
||||
|
||||
public:
|
||||
std::shared_ptr<MessageBaseType> m_Message;
|
||||
|
||||
SerializableMessage() {}
|
||||
SerializableMessage(std::shared_ptr<MessageBaseType>&& a_Message) : m_Message(a_Message) {}
|
||||
SerializableMessage(const std::shared_ptr<MessageBaseType>& a_Message) : m_Message(a_Message) {}
|
||||
|
||||
MessageBaseType* operator->() {
|
||||
return m_Message.get();
|
||||
}
|
||||
|
||||
operator bool() {
|
||||
return m_Message.get();
|
||||
}
|
||||
|
||||
const MessageBaseType* operator->() const {
|
||||
return m_Message.get();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template <typename TMessageFactory>
|
||||
DataBuffer& operator<<(DataBuffer& a_Buffer, const SerializableMessage<TMessageFactory>& a_Message) {
|
||||
return a_Buffer << VarInt{static_cast<std::uint64_t>(a_Message->GetId())} << a_Message->Write();
|
||||
}
|
||||
|
||||
template <typename TMessageFactory>
|
||||
DataBuffer& operator>>(DataBuffer& a_Buffer, SerializableMessage<TMessageFactory>& a_Message) {
|
||||
using MsgId = typename TMessageFactory::IdType;
|
||||
using MsgBase = typename TMessageFactory::MessageBaseType;
|
||||
static TMessageFactory factory;
|
||||
|
||||
VarInt msgId;
|
||||
a_Buffer >> msgId;
|
||||
|
||||
auto msgPtr = std::shared_ptr<MsgBase>(factory.CreateMessage(MsgId(msgId.GetValue())).release());
|
||||
|
||||
a_Message = SerializableMessage<TMessageFactory>(msgPtr);
|
||||
a_Message->Read(a_Buffer);
|
||||
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
} // namespace sp
|
||||
43
include/sp/io/StdIo.h
Normal file
43
include/sp/io/StdIo.h
Normal file
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/io/IoInterface.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace sp {
|
||||
|
||||
class StdInput : public IoInterface {
|
||||
private:
|
||||
std::istream& m_Io;
|
||||
|
||||
public:
|
||||
StdInput(std::istream& a_Io) : m_Io(a_Io) {}
|
||||
|
||||
virtual DataBuffer Read(std::size_t a_Amount) override {
|
||||
DataBuffer buffer(a_Amount);
|
||||
m_Io.read(reinterpret_cast<char*>(buffer.data()), a_Amount);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
virtual void Write(const DataBuffer& a_Data) override {
|
||||
assert(!"Write not implemented !");
|
||||
}
|
||||
};
|
||||
|
||||
class StdOuput : public IoInterface {
|
||||
private:
|
||||
std::ostream& m_Io;
|
||||
|
||||
public:
|
||||
StdOuput(std::ostream& a_Io) : m_Io(a_Io) {}
|
||||
|
||||
virtual DataBuffer Read(std::size_t a_Amount) override {
|
||||
assert(!"Read not implemented !");
|
||||
return {};
|
||||
}
|
||||
|
||||
virtual void Write(const DataBuffer& a_Data) override {
|
||||
m_Io.write(reinterpret_cast<const char*>(a_Data.data()), a_Data.GetSize());
|
||||
}
|
||||
};
|
||||
|
||||
} // 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
|
||||
37
include/sp/protocol/BitField.h
Normal file
37
include/sp/protocol/BitField.h
Normal file
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace sp {
|
||||
|
||||
template <typename T, std::size_t BitSize>
|
||||
class BitField {
|
||||
private:
|
||||
static constexpr int BITS_IN_CHAR = 8;
|
||||
static_assert(sizeof(T) * BITS_IN_CHAR > BitSize, "The bit count must be lower than the actual type size !");
|
||||
|
||||
T m_Data;
|
||||
|
||||
public:
|
||||
BitField() : m_Data{} {};
|
||||
BitField(T a_Data) : m_Data(a_Data) {}
|
||||
|
||||
BitField& operator=(T a_Data) {
|
||||
m_Data = a_Data;
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr std::size_t GetBitSize() const {
|
||||
return BitSize;
|
||||
}
|
||||
|
||||
T& operator*() {
|
||||
return m_Data;
|
||||
}
|
||||
|
||||
const T& operator*() const {
|
||||
return m_Data;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace sp
|
||||
57
include/sp/protocol/ConcreteMessage.h
Normal file
57
include/sp/protocol/ConcreteMessage.h
Normal file
@@ -0,0 +1,57 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/protocol/MessageBase.h>
|
||||
#include <sp/io/MessageIO.h>
|
||||
|
||||
namespace sp {
|
||||
|
||||
template <typename TData, typename MessageBase, typename MessageBase::MessageIdType ID, bool DefineDispatch = true>
|
||||
class ConcreteMessage : public MessageBase {
|
||||
public:
|
||||
using DataType = TData;
|
||||
using MessageIdType = typename MessageBase::MessageIdType;
|
||||
using HandlerType = typename MessageBase::HandlerType;
|
||||
|
||||
template <typename... T>
|
||||
ConcreteMessage(T&&... args) : m_Data{std::move(args)...} {}
|
||||
|
||||
virtual ~ConcreteMessage() {}
|
||||
|
||||
virtual MessageIdType GetId() const override {
|
||||
return ID;
|
||||
}
|
||||
|
||||
virtual void Dispatch(HandlerType& handler) const override {
|
||||
if constexpr (DefineDispatch)
|
||||
handler.Handle(*this);
|
||||
}
|
||||
|
||||
virtual void Read(DataBuffer& a_Buffer) override {
|
||||
ReadMessage(a_Buffer, m_Data);
|
||||
}
|
||||
|
||||
virtual DataBuffer Write() const override {
|
||||
return WriteMessage(m_Data);
|
||||
}
|
||||
|
||||
DataType& operator*() {
|
||||
return m_Data;
|
||||
}
|
||||
|
||||
DataType* operator->() {
|
||||
return &m_Data;
|
||||
}
|
||||
|
||||
const DataType& operator*() const {
|
||||
return m_Data;
|
||||
}
|
||||
|
||||
const DataType* operator->() const {
|
||||
return &m_Data;
|
||||
}
|
||||
|
||||
private:
|
||||
DataType m_Data;
|
||||
};
|
||||
|
||||
} // namespace sp
|
||||
@@ -1,87 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/common/DataBuffer.h>
|
||||
#include <sp/common/Templates.h>
|
||||
|
||||
namespace sp {
|
||||
|
||||
template <typename ValueType>
|
||||
class Field {
|
||||
public:
|
||||
// Provide an access to the stored value
|
||||
ValueType& GetValue() {
|
||||
return m_Value;
|
||||
}
|
||||
const ValueType& GetValue() const {
|
||||
return m_Value;
|
||||
}
|
||||
|
||||
// Read (deserialise) and update internal value
|
||||
void Read(DataBuffer& buffer) {
|
||||
buffer >> m_Value;
|
||||
}
|
||||
|
||||
// Write (serialise) internal value
|
||||
void Write(DataBuffer& buffer) const {
|
||||
buffer << m_Value;
|
||||
}
|
||||
|
||||
Field& operator=(const ValueType& value) {
|
||||
m_Value = value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
ValueType m_Value;
|
||||
};
|
||||
|
||||
// Functor used to read all tuple values
|
||||
class FieldReader {
|
||||
public:
|
||||
FieldReader(DataBuffer& buffer) : m_Buffer(buffer) {}
|
||||
|
||||
template <typename TField>
|
||||
void operator()(TField& field) {
|
||||
field.Read(m_Buffer);
|
||||
}
|
||||
|
||||
private:
|
||||
DataBuffer& m_Buffer;
|
||||
};
|
||||
|
||||
// Functor used to write all tuple values
|
||||
class FieldWriter {
|
||||
public:
|
||||
FieldWriter(DataBuffer& buffer) : m_Buffer(buffer) {}
|
||||
|
||||
template <typename TField>
|
||||
void operator()(TField& field) {
|
||||
field.Write(m_Buffer);
|
||||
}
|
||||
|
||||
private:
|
||||
DataBuffer& m_Buffer;
|
||||
};
|
||||
|
||||
namespace details {
|
||||
|
||||
template <typename... TFields>
|
||||
struct FieldsBuilder {};
|
||||
|
||||
template <>
|
||||
struct FieldsBuilder<> {
|
||||
using Type = std::tuple<>;
|
||||
};
|
||||
|
||||
template <typename... TFields>
|
||||
struct FieldsBuilder<std::tuple<TFields...>> {
|
||||
using Type = typename FieldsBuilder<TFields...>::Type;
|
||||
};
|
||||
|
||||
template <typename TField, typename... TFields>
|
||||
struct FieldsBuilder<TField, TFields...> {
|
||||
using Type = sp::tuple_cat_t<std::tuple<Field<TField>>, typename FieldsBuilder<TFields...>::Type>;
|
||||
};
|
||||
} // namespace details
|
||||
|
||||
} // namespace sp
|
||||
@@ -1,141 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <tuple>
|
||||
|
||||
namespace sp
|
||||
{
|
||||
// This class is inspired by https://arobenko.gitbooks.io/comms-protocols-cpp/content/
|
||||
|
||||
// TCommon is common interface class for all the messages
|
||||
// TAll is all the message types, that need to be handled, bundled in std::tuple
|
||||
template <typename TCommon, typename TAll>
|
||||
class GenericHandler;
|
||||
|
||||
// Big boy to process packets 20 by 20, preventing needlessly copying vtable many times at each inheritance stage
|
||||
template <typename TCommon,
|
||||
typename T1, typename T2, typename T3, typename T4, typename T5,
|
||||
typename T6, typename T7, typename T8, typename T9, typename T10,
|
||||
typename T11, typename T12, typename T13, typename T14, typename T15,
|
||||
typename T16, typename T17, typename T18, typename T19, typename T20,
|
||||
typename... TRest>
|
||||
class GenericHandler<TCommon, std::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T11, T13, T14, T15, T16, T17, T18, T19, T20, TRest...> > : public GenericHandler<TCommon, std::tuple<TRest...> >
|
||||
{
|
||||
using Base = GenericHandler<TCommon, std::tuple<TRest...> >;
|
||||
public:
|
||||
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(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T3& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T4& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T5& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T6& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T7& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T8& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T9& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T10& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T11& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T12& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T13& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T14& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T15& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T16& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T17& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T18& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T19& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T20& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
};
|
||||
|
||||
// 10 by 10
|
||||
template <typename TCommon,
|
||||
typename T1, typename T2, typename T3, typename T4, typename T5,
|
||||
typename T6, typename T7, typename T8, typename T9, typename T10,
|
||||
typename... TRest>
|
||||
class GenericHandler<TCommon, std::tuple<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TRest...> > : public GenericHandler<TCommon, std::tuple<TRest...> >
|
||||
{
|
||||
using Base = GenericHandler<TCommon, std::tuple<TRest...> >;
|
||||
public:
|
||||
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(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T3& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T4& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T5& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T6& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T7& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T8& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T9& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T10& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
};
|
||||
|
||||
// 5 by 5
|
||||
template <typename TCommon,
|
||||
typename T1, typename T2, typename T3, typename T4, typename T5,
|
||||
typename... TRest>
|
||||
class GenericHandler<TCommon, std::tuple<T1, T2, T3, T4, T5, TRest...> > : public GenericHandler<TCommon, std::tuple<TRest...> >
|
||||
{
|
||||
using Base = GenericHandler<TCommon, std::tuple<TRest...> >;
|
||||
public:
|
||||
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(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T3& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T4& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T5& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
};
|
||||
|
||||
// Deal with rest with 4 types
|
||||
template <typename TCommon, typename T1, typename T2, typename T3, typename T4>
|
||||
class GenericHandler<TCommon, std::tuple<T1, T2, T3, T4> >
|
||||
{
|
||||
public:
|
||||
virtual ~GenericHandler() {}
|
||||
virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T3& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T4& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(TCommon&) { } //Nothing to do
|
||||
};
|
||||
|
||||
// Deal with rest with 3 types
|
||||
template <typename TCommon, typename T1, typename T2, typename T3>
|
||||
class GenericHandler<TCommon, std::tuple<T1, T2, T3> >
|
||||
{
|
||||
public:
|
||||
virtual ~GenericHandler() {}
|
||||
virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T3& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(TCommon&) { } //Nothing to do
|
||||
};
|
||||
|
||||
// Deal with rest with 2 types
|
||||
template <typename TCommon, typename T1, typename T2>
|
||||
class GenericHandler<TCommon, std::tuple<T1, T2> >
|
||||
{
|
||||
public:
|
||||
virtual ~GenericHandler() {}
|
||||
virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(T2& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(TCommon&) { } //Nothing to do
|
||||
};
|
||||
|
||||
// Deal with rest with 1 type
|
||||
template <typename TCommon, typename T1>
|
||||
class GenericHandler<TCommon, std::tuple<T1> >
|
||||
{
|
||||
public:
|
||||
virtual ~GenericHandler() {}
|
||||
virtual void Handle(T1& msg) { this->Handle(static_cast<TCommon&>(msg)); }
|
||||
virtual void Handle(TCommon&) { } //Nothing to do
|
||||
};
|
||||
|
||||
// Deal with rest with 0 type
|
||||
template <typename TCommon>
|
||||
class GenericHandler<TCommon, std::tuple<> >
|
||||
{
|
||||
public:
|
||||
virtual ~GenericHandler() {}
|
||||
virtual void Handle(TCommon&) { } //Nothing to do
|
||||
};
|
||||
|
||||
} // sp
|
||||
@@ -1,13 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/protocol/MessageInterfaceBuilder.h>
|
||||
|
||||
namespace sp {
|
||||
|
||||
template <typename... TOptions>
|
||||
class Message : public details::MessageInterfaceBuilder<TOptions...>::Type {
|
||||
public:
|
||||
using ParsedOptions = typename details::MessageInterfaceBuilder<TOptions...>::ParsedOptions;
|
||||
};
|
||||
|
||||
} // namespace sp
|
||||
@@ -1,285 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/protocol/Message.h>
|
||||
#include <iosfwd>
|
||||
#include <sp/common/DataBuffer.h>
|
||||
|
||||
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 {
|
||||
template <typename TMessageID, typename THandler>
|
||||
class MessageBase {
|
||||
public:
|
||||
// Reuse the message ID type defined in the interface
|
||||
using MsgIdType = typename TBase::MsgIdType;
|
||||
using HandlerType = THandler;
|
||||
using MessageIdType = TMessageID;
|
||||
|
||||
protected:
|
||||
virtual MsgIdType GetIdImpl() const override {
|
||||
return static_cast<MsgIdType>(TId);
|
||||
}
|
||||
MessageBase() {}
|
||||
virtual ~MessageBase() {}
|
||||
|
||||
virtual MessageIdType GetId() const = 0;
|
||||
|
||||
virtual void Dispatch(HandlerType& handler) const = 0;
|
||||
|
||||
virtual void Read(DataBuffer& a_Buffer) = 0;
|
||||
virtual DataBuffer Write() const = 0;
|
||||
};
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
private:
|
||||
AllFields m_Fields;
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
class MessageImplFieldsReadBase : public TBase {
|
||||
protected:
|
||||
void ReadImpl(DataBuffer& buffer) override {
|
||||
// TODO: add endianess
|
||||
auto& allFields = TBase::GetFields();
|
||||
std::apply(FieldReader{buffer}, allFields);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
class MessageImplFieldsWriteBase : public TBase {
|
||||
protected:
|
||||
void WriteImpl(DataBuffer& buffer) override {
|
||||
// TODO: add endianess + write ID
|
||||
auto& allFields = TBase::GetFields();
|
||||
std::apply(FieldWriter{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>
|
||||
class MessageBase : public details::MessageImplBuilder<TBase, TOptions...>::Type {};
|
||||
|
||||
|
||||
} // namespace sp
|
||||
} // namespace sp
|
||||
|
||||
54
include/sp/protocol/MessageDispatcher.h
Normal file
54
include/sp/protocol/MessageDispatcher.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* \file MessageDispatcher.h
|
||||
* \brief File containing the sp::MessageDispatcher class
|
||||
*/
|
||||
|
||||
#include <map>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace sp {
|
||||
|
||||
/**
|
||||
* \class MessageDispatcher
|
||||
* \brief Class used to dispatch messages
|
||||
*/
|
||||
template <typename MessageBase>
|
||||
class MessageDispatcher {
|
||||
public:
|
||||
using MessageBaseType = MessageBase;
|
||||
using MessageHandler = typename MessageBase::HandlerType;
|
||||
|
||||
/**
|
||||
* \brief Constructor
|
||||
*/
|
||||
MessageDispatcher() {}
|
||||
|
||||
/**
|
||||
* \brief Dispatch a packet
|
||||
* \param packet The packet to dispatch
|
||||
*/
|
||||
void Dispatch(const MessageBase& a_Message);
|
||||
|
||||
/**
|
||||
* \brief Register a packet handler
|
||||
* \param type The packet type
|
||||
* \param handler The packet handler
|
||||
*/
|
||||
void RegisterHandler(const std::shared_ptr<MessageHandler>& a_Handler);
|
||||
|
||||
/**
|
||||
* \brief Unregister a packet handler
|
||||
* \param handler The packet handler
|
||||
*/
|
||||
void UnregisterHandler(const std::shared_ptr<MessageHandler>& a_Handler);
|
||||
|
||||
private:
|
||||
std::vector<std::weak_ptr<MessageHandler>> m_Handlers;
|
||||
};
|
||||
|
||||
} // namespace sp
|
||||
|
||||
#include <sp/protocol/MessageDispatcherImpl.inl>
|
||||
28
include/sp/protocol/MessageDispatcherImpl.inl
Normal file
28
include/sp/protocol/MessageDispatcherImpl.inl
Normal file
@@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
|
||||
namespace sp {
|
||||
|
||||
template <typename MessageBase>
|
||||
void MessageDispatcher<MessageBase>::RegisterHandler(const std::shared_ptr<MessageHandler>& a_Handler) {
|
||||
assert(a_Handler);
|
||||
m_Handlers.push_back(a_Handler);
|
||||
}
|
||||
|
||||
template <typename MessageBase>
|
||||
void MessageDispatcher<MessageBase>::UnregisterHandler(const std::shared_ptr<MessageHandler>& a_Handler) {
|
||||
auto found = std::find(m_Handlers.begin(), m_Handlers.end(), a_Handler);
|
||||
if (found != m_Handlers.end())
|
||||
m_Handlers.erase(found);
|
||||
}
|
||||
|
||||
template <typename MessageBase>
|
||||
void MessageDispatcher<MessageBase>::Dispatch(const MessageBase& a_Message) {
|
||||
for (auto& handler : m_Handlers) {
|
||||
a_Message.Dispatch(*handler.lock());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sp
|
||||
@@ -2,62 +2,37 @@
|
||||
|
||||
#include <array>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
|
||||
#include <sp/common/Tuples.h>
|
||||
|
||||
namespace sp {
|
||||
|
||||
namespace details {
|
||||
|
||||
template <typename TBase>
|
||||
using ArrayType = std::vector<std::function<std::unique_ptr<TBase>(void)>>;
|
||||
|
||||
|
||||
|
||||
template <typename TBase, typename... TMessages>
|
||||
struct ArrayFiller {};
|
||||
|
||||
template <typename TBase, typename... TMessages>
|
||||
struct ArrayFiller<TBase, std::tuple<TMessages...>> {
|
||||
static void ArrayAppend(details::ArrayType<TBase>& array) {
|
||||
ArrayFiller<TBase, TMessages...>::ArrayAppend(array);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename TBase, typename TMessage, typename... TMessages>
|
||||
struct ArrayFiller<TBase, TMessage, TMessages...> {
|
||||
static void ArrayAppend(details::ArrayType<TBase>& array) {
|
||||
ArrayFiller<TBase, TMessage>::ArrayAppend(array);
|
||||
ArrayFiller<TBase, TMessages...>::ArrayAppend(array);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename TBase, typename TMessage>
|
||||
struct ArrayFiller<TBase, TMessage> {
|
||||
static void ArrayAppend(details::ArrayType<TBase>& array) {
|
||||
array.push_back([]() -> std::unique_ptr<TBase> { return std::make_unique<TMessage>(); });
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
|
||||
template <typename TBase, typename TTMessages>
|
||||
class MessageFactory {
|
||||
public:
|
||||
using IdType = typename TBase::MsgIdType;
|
||||
using IdType = typename TBase::MessageIdType;
|
||||
using MessageBaseType = TBase;
|
||||
|
||||
MessageFactory() {
|
||||
details::ArrayFiller<TBase, TTMessages>::ArrayAppend(m_Factory);
|
||||
constexpr std::size_t messageCount = std::tuple_size_v<TTMessages>;
|
||||
m_Factory.resize(messageCount);
|
||||
TupleForEach([this](const auto& message){
|
||||
std::size_t messageID = static_cast<std::size_t>(message.GetId());
|
||||
using MessageType = std::decay_t<decltype(message)>;
|
||||
m_Factory.emplace(m_Factory.begin() + messageID, []() -> std::unique_ptr<TBase> { return std::make_unique<MessageType>(); });
|
||||
}, TTMessages{});
|
||||
}
|
||||
|
||||
std::unique_ptr<TBase> CreateMessage(IdType id) {
|
||||
if (id >= m_Factory.size())
|
||||
std::unique_ptr<TBase> CreateMessage(IdType id) const {
|
||||
std::size_t idSize = static_cast<std::size_t>(id);
|
||||
if (idSize >= m_Factory.size())
|
||||
return nullptr;
|
||||
return m_Factory.at(id)();
|
||||
return m_Factory.at(idSize)();
|
||||
}
|
||||
|
||||
private:
|
||||
details::ArrayType<TBase> m_Factory;
|
||||
std::vector<std::function<std::unique_ptr<TBase>(void)>> m_Factory;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/protocol/MessageInterfaces.h>
|
||||
|
||||
namespace sp {
|
||||
namespace details {
|
||||
|
||||
class EmptyBase {};
|
||||
|
||||
template <typename... TOptions>
|
||||
struct MessageInterfaceBuilder {
|
||||
// Parse the options
|
||||
using ParsedOptions = MessageInterfaceParsedOptions<TOptions...>;
|
||||
|
||||
// Add ID retrieval functionality if ID type was provided
|
||||
using Base1 = typename MessageInterfaceProcessMsgId<EmptyBase, ParsedOptions, ParsedOptions::HasMsgIdType>::Type;
|
||||
|
||||
// Add ReadData() and WriteData(), that use the right endian
|
||||
using Base2 = typename MessageInterfaceProcessEndian<Base1, ParsedOptions::HasLittleEndian>::Type;
|
||||
|
||||
// Add read functionality if Read type was provided
|
||||
using Base3 = typename MessageInterfaceProcessRead<Base2, ParsedOptions::HasReadOperations>::Type;
|
||||
|
||||
// Add write functionality if Write type was provided
|
||||
using Base4 = typename MessageInterfaceProcessWrite<Base3, ParsedOptions::HasWriteOperations>::Type;
|
||||
|
||||
// add dispatch functionality if Handler type was provided
|
||||
using Base5 = typename MessageInterfaceProcessHandler<Base4, ParsedOptions, ParsedOptions::HasHandler>::Type;
|
||||
|
||||
// add valid functionality if Valid tpe was provided
|
||||
using Base6 = typename MessageInterfaceProcessValid<Base5, ParsedOptions::HasValid>::Type;
|
||||
|
||||
// The last Base6 must be taken as final type.
|
||||
using Type = Base6;
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
} // namespace sp
|
||||
@@ -1,243 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/protocol/Options.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 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 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>
|
||||
static T ReadData(DataBuffer& buffer) {
|
||||
// use big endian
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void WriteData(T value, DataBuffer& buffer) {
|
||||
// use big endian
|
||||
}
|
||||
};
|
||||
|
||||
// Little endian serialisation chunk
|
||||
template <typename TBase>
|
||||
class MessageInterfaceLittleEndian : public TBase {
|
||||
protected:
|
||||
template <typename T>
|
||||
static T ReadData(DataBuffer& buffer) {
|
||||
// use little endian
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void WriteData(const T& value, DataBuffer& buffer) {
|
||||
// use little endian
|
||||
}
|
||||
};
|
||||
|
||||
// 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) {
|
||||
return 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;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// 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;
|
||||
};
|
||||
} // namespace details
|
||||
} // namespace sp
|
||||
@@ -1,31 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/common/DataBuffer.h>
|
||||
|
||||
namespace sp {
|
||||
namespace option {
|
||||
|
||||
// Define type used to store message ID
|
||||
template <typename T>
|
||||
struct MsgIdType {};
|
||||
|
||||
// Enable reading
|
||||
struct ReadOperations {};
|
||||
|
||||
// Enable writing
|
||||
struct WriteOperations {};
|
||||
|
||||
// Use little endian for serialisation (instead of default big)
|
||||
struct LittleEndian {};
|
||||
|
||||
// Include validity check in public interface
|
||||
struct ValidCheckInterface {};
|
||||
|
||||
// Define handler class
|
||||
template <typename T>
|
||||
struct Handler {
|
||||
using HandlerT = T;
|
||||
};
|
||||
|
||||
} // namespace option
|
||||
} // namespace sp
|
||||
42
src/main.cpp
42
src/main.cpp
@@ -1,42 +0,0 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <memory>
|
||||
#include <examples/PacketExample.h>
|
||||
|
||||
class KeepAliveHandler : public sp::PacketHandler {
|
||||
void Handle(KeepAlivePacket& packet) {
|
||||
std::cout << "KeepAlive handled !\n";
|
||||
}
|
||||
|
||||
void Handle(DisconnectPacket& packet) {
|
||||
std::cout << "Disconnect handled !\n";
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
auto keepAlive = std::make_unique<KeepAlivePacket>(69);
|
||||
sp::PacketMessage* msg = keepAlive.get();
|
||||
|
||||
KeepAliveHandler handler;
|
||||
msg->Dispatch(handler);
|
||||
|
||||
sp::DataBuffer buffer;
|
||||
msg->Write(buffer);
|
||||
|
||||
auto keepAlive2 = std::make_unique<KeepAlivePacket>();
|
||||
keepAlive2->Read(buffer);
|
||||
|
||||
std::cout << "KeepAlive2 : " << keepAlive2->GetField<KeepAliveId>() << "\n";
|
||||
|
||||
//TODO: write ID
|
||||
sp::PacketFactory factory;
|
||||
auto packet = factory.CreateMessage(Disconnect);
|
||||
if (packet == nullptr) {
|
||||
std::cout << "Mauvais ID !\n";
|
||||
return 1;
|
||||
}
|
||||
std::cout << (unsigned) packet->GetId() << std::endl;
|
||||
packet->Dispatch(handler);
|
||||
|
||||
return 0;
|
||||
}
|
||||
21
src/sp/common/ByteSwapping.cpp
Normal file
21
src/sp/common/ByteSwapping.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#include <sp/common/ByteSwapping.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace sp {
|
||||
|
||||
bool IsLittleEndian() {
|
||||
#ifdef SP_BIG_ENDIAN
|
||||
return false;
|
||||
#else
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
|
||||
void SwapBytes(std::uint8_t* begin, std::uint8_t* end) {
|
||||
if (IsLittleEndian()) {
|
||||
std::reverse(begin, end);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sp
|
||||
@@ -4,13 +4,12 @@
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
#include <sp/misc/Format.h>
|
||||
#include <sp/misc/Log.h>
|
||||
|
||||
namespace sp {
|
||||
|
||||
DataBuffer::DataBuffer() : m_ReadOffset(0) {}
|
||||
|
||||
DataBuffer::DataBuffer(std::size_t a_InitialSize) : m_Buffer(a_InitialSize), m_ReadOffset(0) {}
|
||||
|
||||
DataBuffer::DataBuffer(const DataBuffer& other) : m_Buffer(other.m_Buffer), m_ReadOffset(other.m_ReadOffset) {}
|
||||
|
||||
DataBuffer::DataBuffer(DataBuffer&& other) : m_Buffer(std::move(other.m_Buffer)), m_ReadOffset(std::move(other.m_ReadOffset)) {}
|
||||
@@ -22,33 +21,35 @@ DataBuffer::DataBuffer(const DataBuffer& other, Data::difference_type offset) :
|
||||
std::copy(other.m_Buffer.begin() + offset, other.m_Buffer.end(), std::back_inserter(m_Buffer));
|
||||
}
|
||||
|
||||
DataBuffer& DataBuffer::operator<<(const std::string& str) {
|
||||
DataBuffer& operator<<(DataBuffer& a_Buffer, const std::string& str) {
|
||||
std::size_t strlen = str.length() + 1; // including null character
|
||||
Resize(GetSize() + strlen);
|
||||
std::memcpy(m_Buffer.data() + GetSize() - strlen, str.data(), strlen);
|
||||
return *this;
|
||||
a_Buffer.Resize(a_Buffer.GetSize() + strlen);
|
||||
std::memcpy(a_Buffer.data() + a_Buffer.GetSize() - strlen, str.data(), strlen);
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
DataBuffer& DataBuffer::operator<<(const DataBuffer& data) {
|
||||
m_Buffer.insert(m_Buffer.end(), data.begin(), data.end());
|
||||
return *this;
|
||||
DataBuffer& operator<<(DataBuffer& a_Buffer, const DataBuffer& data) {
|
||||
std::size_t end = a_Buffer.GetSize();
|
||||
a_Buffer.Resize(a_Buffer.GetSize() + data.GetSize());
|
||||
std::copy(data.begin(), data.end(), a_Buffer.begin() + end);
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
DataBuffer& DataBuffer::operator>>(std::string& str) {
|
||||
std::size_t stringSize = strlen(reinterpret_cast<const char*>(m_Buffer.data()) + m_ReadOffset) + 1; // including null character
|
||||
DataBuffer& operator>>(DataBuffer& a_Buffer, std::string& str) {
|
||||
std::size_t stringSize = strlen(reinterpret_cast<const char*>(a_Buffer.data()) + a_Buffer.GetReadOffset()) + 1; // including null character
|
||||
str.resize(stringSize);
|
||||
std::copy(m_Buffer.begin() + static_cast<difference_type>(m_ReadOffset),
|
||||
m_Buffer.begin() + static_cast<difference_type>(m_ReadOffset + stringSize), str.begin());
|
||||
m_ReadOffset += stringSize;
|
||||
std::copy(a_Buffer.begin() + static_cast<DataBuffer::difference_type>(a_Buffer.GetReadOffset()),
|
||||
a_Buffer.begin() + static_cast<DataBuffer::difference_type>(a_Buffer.GetReadOffset() + stringSize), str.begin());
|
||||
a_Buffer.SetReadOffset(a_Buffer.GetReadOffset() + stringSize);
|
||||
str.resize(stringSize - 1);
|
||||
return *this;
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
DataBuffer& DataBuffer::operator>>(DataBuffer& data) {
|
||||
data.Resize(GetSize() - m_ReadOffset);
|
||||
std::copy(m_Buffer.begin() + static_cast<difference_type>(m_ReadOffset), m_Buffer.end(), data.begin());
|
||||
m_ReadOffset = m_Buffer.size();
|
||||
return *this;
|
||||
DataBuffer& operator>>(DataBuffer& a_Buffer, DataBuffer& data) {
|
||||
data.Resize(a_Buffer.GetSize() - a_Buffer.GetReadOffset());
|
||||
std::copy(a_Buffer.begin() + static_cast<DataBuffer::difference_type>(a_Buffer.GetReadOffset()), a_Buffer.end(), data.begin());
|
||||
a_Buffer.SetReadOffset(a_Buffer.GetSize());
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
void DataBuffer::WriteSome(const char* buffer, std::size_t amount) {
|
||||
@@ -133,31 +134,19 @@ std::ostream& operator<<(std::ostream& os, const DataBuffer& buffer) {
|
||||
return os;
|
||||
}
|
||||
|
||||
bool DataBuffer::ReadFile(const std::string& fileName) {
|
||||
try {
|
||||
std::ifstream file(fileName, std::istream::binary);
|
||||
std::ostringstream ss;
|
||||
ss << file.rdbuf();
|
||||
const std::string& s = ss.str();
|
||||
m_Buffer = DataBuffer::Data(s.begin(), s.end());
|
||||
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;
|
||||
void DataBuffer::ReadFile(const std::string& fileName) {
|
||||
std::ifstream file(fileName, std::istream::binary);
|
||||
std::ostringstream ss;
|
||||
ss << file.rdbuf();
|
||||
const std::string& s = ss.str();
|
||||
m_Buffer = DataBuffer::Data(s.begin(), s.end());
|
||||
m_ReadOffset = 0;
|
||||
}
|
||||
|
||||
bool DataBuffer::WriteFile(const std::string& fileName) const {
|
||||
try {
|
||||
std::ofstream file(fileName, std::ostream::binary);
|
||||
file.write(reinterpret_cast<const char*>(m_Buffer.data()), static_cast<std::streamsize>(m_Buffer.size()));
|
||||
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;
|
||||
void DataBuffer::WriteFile(const std::string& fileName) const {
|
||||
std::ofstream file(fileName, std::ostream::binary);
|
||||
file.write(reinterpret_cast<const char*>(m_Buffer.data()), static_cast<std::streamsize>(m_Buffer.size()));
|
||||
file.flush();
|
||||
}
|
||||
|
||||
} // namespace sp
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
#include <sp/common/VarInt.h>
|
||||
|
||||
#include <stdexcept>
|
||||
#include <sp/common/DataBuffer.h>
|
||||
#include <sp/common/DataBufferOperators.h>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace sp {
|
||||
|
||||
static constexpr int SEGMENT_BITS = 0x7F;
|
||||
static constexpr int CONTINUE_BIT = 0x80;
|
||||
static constexpr int SEGMENT_BITS = (1 << 7) - 1;
|
||||
static constexpr int CONTINUE_BIT = 1 << 7;
|
||||
|
||||
std::size_t VarInt::GetSerializedLength() const {
|
||||
DataBuffer buffer;
|
||||
@@ -29,24 +30,29 @@ DataBuffer& operator<<(DataBuffer& out, const VarInt& var) {
|
||||
}
|
||||
|
||||
DataBuffer& operator>>(DataBuffer& in, VarInt& var) {
|
||||
var.m_Value = 0;
|
||||
var.Read([&in](std::uint8_t& data){
|
||||
in.ReadSome(&data, 1);
|
||||
});
|
||||
return in;
|
||||
}
|
||||
|
||||
void VarInt::Read(const ReadFunc& read) {
|
||||
m_Value = 0;
|
||||
unsigned int position = 0;
|
||||
std::uint8_t currentByte;
|
||||
|
||||
while (true) {
|
||||
in.ReadSome(¤tByte, 1);
|
||||
var.m_Value |= static_cast<std::uint64_t>(currentByte & SEGMENT_BITS) << position;
|
||||
read(currentByte);
|
||||
m_Value |= static_cast<std::uint64_t>(currentByte & SEGMENT_BITS) << position;
|
||||
|
||||
if ((currentByte & CONTINUE_BIT) == 0)
|
||||
break;
|
||||
|
||||
position += 7;
|
||||
|
||||
if (position >= 8 * sizeof(var.m_Value))
|
||||
if (position >= 8 * sizeof(m_Value))
|
||||
throw std::runtime_error("VarInt is too big");
|
||||
}
|
||||
|
||||
return in;
|
||||
}
|
||||
|
||||
} // namespace sp
|
||||
|
||||
85
src/sp/extensions/Compress.cpp
Normal file
85
src/sp/extensions/Compress.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
#include <sp/extensions/Compress.h>
|
||||
|
||||
#include <cassert>
|
||||
#include <sp/common/VarInt.h>
|
||||
#include <zlib.h>
|
||||
|
||||
namespace sp {
|
||||
namespace zlib {
|
||||
|
||||
static DataBuffer Inflate(const std::uint8_t* source, std::size_t size, std::size_t uncompressedSize) {
|
||||
DataBuffer result;
|
||||
result.Resize(uncompressedSize);
|
||||
|
||||
uncompress(static_cast<Bytef*>(result.data()), reinterpret_cast<uLongf*>(&uncompressedSize), static_cast<const Bytef*>(source),
|
||||
static_cast<uLong>(size));
|
||||
|
||||
assert(result.GetSize() == uncompressedSize);
|
||||
return result;
|
||||
}
|
||||
|
||||
static DataBuffer Deflate(const std::uint8_t* source, std::size_t size) {
|
||||
DataBuffer result;
|
||||
uLongf compressedSize = size;
|
||||
|
||||
result.Resize(size); // Resize for the compressed data to fit into
|
||||
compress(static_cast<Bytef*>(result.data()), &compressedSize, static_cast<const Bytef*>(source), static_cast<uLong>(size));
|
||||
result.Resize(compressedSize); // Resize to cut useless data
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
DataBuffer Compress(const DataBuffer& buffer, std::size_t a_CompressionThreshold) {
|
||||
DataBuffer packet;
|
||||
|
||||
if (buffer.GetSize() < a_CompressionThreshold) {
|
||||
// Don't compress since it's a small packet
|
||||
packet << VarInt{0};
|
||||
packet << buffer;
|
||||
return packet;
|
||||
}
|
||||
|
||||
DataBuffer compressedData = Deflate(buffer.data(), buffer.GetSize());
|
||||
VarInt uncompressedDataLength = buffer.GetSize();
|
||||
|
||||
if (compressedData.GetSize() >= buffer.GetSize()) {
|
||||
// the compression is overkill so we don't send the compressed buffer
|
||||
packet << VarInt{0};
|
||||
packet << buffer;
|
||||
} else {
|
||||
packet << uncompressedDataLength;
|
||||
packet << compressedData;
|
||||
}
|
||||
|
||||
return packet;
|
||||
}
|
||||
|
||||
DataBuffer Decompress(DataBuffer& buffer, std::uint64_t packetLength) {
|
||||
VarInt uncompressedLength;
|
||||
buffer >> uncompressedLength;
|
||||
|
||||
std::uint64_t compressedLength = packetLength - uncompressedLength.GetSerializedLength();
|
||||
|
||||
if (uncompressedLength.GetValue() == 0) {
|
||||
// Data already uncompressed. Nothing to do
|
||||
DataBuffer ret;
|
||||
buffer.ReadSome(ret, compressedLength);
|
||||
return ret;
|
||||
}
|
||||
|
||||
assert(buffer.GetReadOffset() + compressedLength <= buffer.GetSize());
|
||||
|
||||
return Inflate(buffer.data() + buffer.GetReadOffset(), compressedLength, uncompressedLength.GetValue());
|
||||
}
|
||||
|
||||
} // namespace zlib
|
||||
|
||||
DataBuffer ZlibCompress::EncapsulateImpl(const DataBuffer& a_Data) {
|
||||
return zlib::Compress(a_Data, m_CompressionThreshold);
|
||||
}
|
||||
|
||||
DataBuffer ZlibCompress::DecapsulateImpl(DataBuffer& a_Data) {
|
||||
return zlib::Decompress(a_Data, a_Data.GetSize());
|
||||
}
|
||||
|
||||
} // namespace sp
|
||||
114
src/sp/extensions/TcpListener.cpp
Normal file
114
src/sp/extensions/TcpListener.cpp
Normal file
@@ -0,0 +1,114 @@
|
||||
#include <sp/extensions/tcp/TcpListener.h>
|
||||
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
// Windows
|
||||
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
|
||||
#define ioctl ioctlsocket
|
||||
#define WOULDBLOCK WSAEWOULDBLOCK
|
||||
#define MSG_DONTWAIT 0
|
||||
|
||||
#else
|
||||
|
||||
// Linux/Unix
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define closesocket close
|
||||
#define WOULDBLOCK EWOULDBLOCK
|
||||
#define SD_BOTH SHUT_RDWR
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifndef INVALID_SOCKET
|
||||
#define INVALID_SOCKET -1
|
||||
#endif
|
||||
|
||||
|
||||
namespace sp {
|
||||
namespace io {
|
||||
|
||||
TcpListener::TcpListener(std::uint16_t a_Port, int a_MaxConnexions) {
|
||||
if ((m_Handle = static_cast<SocketHandle>(socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))) < 0) {
|
||||
throw SocketError("Failed to create server socket");
|
||||
}
|
||||
|
||||
struct sockaddr_in address;
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_addr.s_addr = INADDR_ANY;
|
||||
address.sin_port = htons(a_Port);
|
||||
|
||||
if (bind(m_Handle, reinterpret_cast<sockaddr*>(&address), sizeof(address)) < 0)
|
||||
throw SocketError("Failed to create server socket");
|
||||
|
||||
if (listen(m_Handle, a_MaxConnexions) < 0)
|
||||
throw SocketError("Failed to create server socket");
|
||||
|
||||
socklen_t len = sizeof(address);
|
||||
if (getsockname(m_Handle, reinterpret_cast<sockaddr*>(&address), &len) < 0)
|
||||
throw SocketError("Failed to create server socket");
|
||||
|
||||
m_Port = ntohs(address.sin_port);
|
||||
m_MaxConnections = a_MaxConnexions;
|
||||
}
|
||||
|
||||
|
||||
TcpListener::~TcpListener() {
|
||||
Close();
|
||||
}
|
||||
|
||||
std::unique_ptr<TcpSocket> TcpListener::Accept() {
|
||||
sockaddr remoteAddress;
|
||||
int addrlen = sizeof(remoteAddress);
|
||||
|
||||
auto newSocket = std::make_unique<TcpSocket>();
|
||||
|
||||
newSocket->m_Handle = static_cast<SocketHandle>(
|
||||
accept(m_Handle, reinterpret_cast<sockaddr*>(&remoteAddress), reinterpret_cast<socklen_t*>(&addrlen)));
|
||||
|
||||
if (newSocket->m_Handle < 0)
|
||||
return nullptr;
|
||||
|
||||
newSocket->m_Status = TcpSocket::Status::Connected;
|
||||
return newSocket;
|
||||
}
|
||||
|
||||
void TcpListener::Close() {
|
||||
if (m_Handle > 0) {
|
||||
closesocket(m_Handle);
|
||||
shutdown(m_Handle, SD_BOTH);
|
||||
}
|
||||
}
|
||||
|
||||
bool TcpListener::SetBlocking(bool a_Blocking) {
|
||||
unsigned long mode = !a_Blocking;
|
||||
|
||||
if (ioctl(m_Handle, FIONBIO, &mode) < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
std::uint16_t TcpListener::GetListeningPort() const {
|
||||
return m_Port;
|
||||
}
|
||||
|
||||
int TcpListener::GetMaximumConnections() const {
|
||||
return m_MaxConnections;
|
||||
}
|
||||
|
||||
} // namespace io
|
||||
} // namespace sp
|
||||
164
src/sp/extensions/TcpSocket.cpp
Normal file
164
src/sp/extensions/TcpSocket.cpp
Normal file
@@ -0,0 +1,164 @@
|
||||
#include <sp/extensions/tcp/TcpSocket.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
// Windows
|
||||
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
|
||||
#define ioctl ioctlsocket
|
||||
#define WOULDBLOCK WSAEWOULDBLOCK
|
||||
#define MSG_DONTWAIT 0
|
||||
|
||||
#else
|
||||
|
||||
// Linux/Unix
|
||||
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define closesocket close
|
||||
#define WOULDBLOCK EWOULDBLOCK
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
#ifndef INVALID_SOCKET
|
||||
#define INVALID_SOCKET -1
|
||||
#endif
|
||||
|
||||
|
||||
namespace sp {
|
||||
namespace io {
|
||||
|
||||
TcpSocket::TcpSocket() : m_Handle(static_cast<SocketHandle>(INVALID_SOCKET)), m_Status(Status::Disconnected) {}
|
||||
|
||||
TcpSocket::TcpSocket(const std::string& a_Host, std::uint16_t a_Port) : TcpSocket() {
|
||||
Connect(a_Host, a_Port);
|
||||
}
|
||||
|
||||
TcpSocket::TcpSocket(TcpSocket&& a_Other) {
|
||||
std::swap(m_Handle, a_Other.m_Handle);
|
||||
std::swap(m_Status, a_Other.m_Status);
|
||||
}
|
||||
|
||||
TcpSocket::~TcpSocket() {}
|
||||
|
||||
void TcpSocket::Connect(const std::string& a_Host, std::uint16_t a_Port) {
|
||||
struct addrinfo hints {};
|
||||
|
||||
struct addrinfo* result = nullptr;
|
||||
|
||||
hints.ai_family = AF_INET;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
hints.ai_protocol = IPPROTO_TCP;
|
||||
|
||||
m_Status = Status::Error;
|
||||
|
||||
if (getaddrinfo(a_Host.c_str(), std::to_string(static_cast<int>(a_Port)).c_str(), &hints, &result) != 0) {
|
||||
throw SocketError("Failed to get address info");
|
||||
}
|
||||
|
||||
m_Handle = static_cast<SocketHandle>(socket(AF_INET, SOCK_STREAM, IPPROTO_TCP));
|
||||
if (m_Handle < 0) {
|
||||
throw SocketError("Failed to create socket");
|
||||
}
|
||||
|
||||
struct addrinfo* ptr = nullptr;
|
||||
for (ptr = result; ptr != nullptr; ptr = ptr->ai_next) {
|
||||
struct sockaddr* sockaddr = ptr->ai_addr;
|
||||
if (connect(m_Handle, sockaddr, sizeof(sockaddr_in)) == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
freeaddrinfo(result);
|
||||
|
||||
if (!ptr) {
|
||||
throw SocketError("Could not find a suitable interface for connecting");
|
||||
}
|
||||
|
||||
m_Status = Status::Connected;
|
||||
}
|
||||
|
||||
DataBuffer TcpSocket::Read(std::size_t a_Amount) {
|
||||
DataBuffer buffer(a_Amount);
|
||||
|
||||
std::size_t totalRecieved = 0;
|
||||
|
||||
while (totalRecieved < a_Amount) {
|
||||
int recvAmount =
|
||||
recv(m_Handle, reinterpret_cast<char*>(buffer.data() + totalRecieved), static_cast<int>(a_Amount - totalRecieved), 0);
|
||||
if (recvAmount <= 0) {
|
||||
#if defined(_WIN32) || defined(WIN32)
|
||||
int err = WSAGetLastError();
|
||||
#else
|
||||
int err = errno;
|
||||
#endif
|
||||
if (err == WOULDBLOCK) {
|
||||
// we are in non blocking mode and nothing is available
|
||||
return {};
|
||||
}
|
||||
|
||||
Disconnect();
|
||||
m_Status = Status::Error;
|
||||
throw SocketError("Error while reading");
|
||||
}
|
||||
totalRecieved += recvAmount;
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void TcpSocket::Write(const sp::DataBuffer& a_Data) {
|
||||
if (GetStatus() != Status::Connected)
|
||||
return;
|
||||
|
||||
std::size_t sent = 0;
|
||||
|
||||
while (sent < a_Data.GetSize()) {
|
||||
int cur = send(m_Handle, reinterpret_cast<const char*>(a_Data.data() + sent), static_cast<int>(a_Data.GetSize() - sent), 0);
|
||||
|
||||
if (cur <= 0) {
|
||||
Disconnect();
|
||||
m_Status = Status::Error;
|
||||
return;
|
||||
}
|
||||
sent += static_cast<std::size_t>(cur);
|
||||
}
|
||||
}
|
||||
|
||||
bool TcpSocket::SetBlocking(bool a_Block) {
|
||||
unsigned long mode = !a_Block;
|
||||
|
||||
if (ioctl(m_Handle, FIONBIO, &mode) < 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
TcpSocket::Status TcpSocket::GetStatus() const {
|
||||
return m_Status;
|
||||
}
|
||||
|
||||
void TcpSocket::Disconnect() {
|
||||
if (m_Handle > 0)
|
||||
closesocket(m_Handle);
|
||||
m_Status = Status::Disconnected;
|
||||
}
|
||||
|
||||
TcpSocket& TcpSocket::operator=(TcpSocket&& a_Other) {
|
||||
std::swap(m_Handle, a_Other.m_Handle);
|
||||
std::swap(m_Status, a_Other.m_Status);
|
||||
return *this;
|
||||
}
|
||||
|
||||
} // namespace io
|
||||
} // 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
|
||||
102
test/test_message.cpp
Normal file
102
test/test_message.cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
#include <sp/common/GenericHandler.h>
|
||||
#include <sp/io/MessageStream.h>
|
||||
#include <sp/io/StdIo.h>
|
||||
#include <sp/protocol/BitField.h>
|
||||
#include <sp/protocol/ConcreteMessage.h>
|
||||
#include <sp/protocol/MessageDispatcher.h>
|
||||
#include <sp/protocol/MessageFactory.h>
|
||||
|
||||
#include <sp/extensions/Compress.h>
|
||||
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
enum class PacketID : std::uint8_t { KeepAlive = 0, MDC = 1 };
|
||||
|
||||
class PacketHandler;
|
||||
|
||||
using PacketBase = sp::MessageBase<PacketID, PacketHandler>;
|
||||
|
||||
template <typename TData, PacketID ID>
|
||||
using Message = sp::ConcreteMessage<TData, PacketBase, ID>;
|
||||
|
||||
struct KeepAlivePacket {
|
||||
sp::BitField<std::uint16_t, 12> one;
|
||||
sp::BitField<std::uint16_t, 4> two;
|
||||
std::shared_ptr<std::string> test;
|
||||
};
|
||||
|
||||
struct MDCPacket {
|
||||
sp::BitField<std::uint16_t, 12> one;
|
||||
sp::BitField<PacketID, 4> two;
|
||||
std::unique_ptr<std::string> test;
|
||||
};
|
||||
|
||||
using KeepAliveMessage = Message<KeepAlivePacket, PacketID::KeepAlive>;
|
||||
using MDCMessage = Message<MDCPacket, PacketID::MDC>;
|
||||
|
||||
using AllMessages = std::tuple<KeepAliveMessage, MDCMessage>;
|
||||
|
||||
class PacketHandler : public sp::GenericHandler<AllMessages> {};
|
||||
|
||||
class MyHandler : public PacketHandler {
|
||||
public:
|
||||
virtual void Handle(const KeepAliveMessage& msg) override {
|
||||
std::cout << "I recieved a keep alive : " << *msg->one << " : " << *msg->two << " : " << (msg->test ? *msg->test : "nullptr") << "\n";
|
||||
}
|
||||
virtual void Handle(const MDCMessage& msg) override {
|
||||
std::cout << "I recieved a mdc : " << *msg->one << " : " << static_cast<unsigned>(*msg->two) << " : " << *msg->test << "\n";
|
||||
}
|
||||
};
|
||||
|
||||
using PacketDispatcher = sp::MessageDispatcher<PacketBase>;
|
||||
|
||||
using PacketFactory = sp::MessageFactory<PacketBase, AllMessages>;
|
||||
|
||||
using PacketStream = sp::MessageStream<PacketFactory>;
|
||||
|
||||
int main() {
|
||||
KeepAliveMessage m{69, 5, std::make_shared<std::string>("I'm a keepalive")};
|
||||
|
||||
// dispatch tests
|
||||
|
||||
auto h = std::make_shared<MyHandler>();
|
||||
PacketDispatcher d;
|
||||
d.RegisterHandler(h);
|
||||
d.Dispatch(m);
|
||||
PacketFactory f;
|
||||
auto message = f.CreateMessage(PacketID::KeepAlive);
|
||||
d.Dispatch(*message);
|
||||
|
||||
|
||||
// write tests
|
||||
|
||||
auto compress = std::make_shared<sp::ZlibCompress>();
|
||||
|
||||
std::ofstream file{"test.bin"};
|
||||
|
||||
PacketStream p(std::make_shared<sp::StdOuput>(file));
|
||||
|
||||
p.WriteMessage(m);
|
||||
p.WriteMessage(MDCMessage{42, PacketID::MDC, std::make_unique<std::string>("Coucou")});
|
||||
|
||||
file.flush();
|
||||
|
||||
std::ifstream file2{"test.bin"};
|
||||
|
||||
PacketStream p2(std::make_shared<sp::StdInput>(file2));
|
||||
|
||||
auto message2 = p2.ReadMessage();
|
||||
auto message3 = p2.ReadMessage();
|
||||
|
||||
d.Dispatch(*message2);
|
||||
d.Dispatch(*message3);
|
||||
|
||||
// message->Write(file);
|
||||
// file << std::endl;
|
||||
// m.Write(file);
|
||||
// file << std::endl;
|
||||
// message->Read(file);
|
||||
return 0;
|
||||
}
|
||||
155
test/type_name.hpp
Normal file
155
test/type_name.hpp
Normal file
@@ -0,0 +1,155 @@
|
||||
// Copyright (c) 2018 Will Wray https://keybase.io/willwray
|
||||
//
|
||||
// Distributed under the Boost Software License, Version 1.0.
|
||||
// (http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <typeinfo>
|
||||
#include <type_traits>
|
||||
|
||||
#if __has_include(<cxxabi.h>)
|
||||
# include <cxxabi.h>
|
||||
# include <cstdlib>
|
||||
# include <memory>
|
||||
#endif
|
||||
|
||||
constexpr bool CXXABI
|
||||
{
|
||||
#if __has_include(<cxxabi.h>)
|
||||
true
|
||||
#endif
|
||||
};
|
||||
|
||||
/*
|
||||
"type_name_rt.hpp": get type names at runtime (hence 'rt')
|
||||
^^^^^^^^^^^^^^^^
|
||||
This header defines, in global scope (i.e. not namespace'd):
|
||||
(1) A function template type_name_str<T>() for extracting a type's name.
|
||||
(2) A variable template type_name_rt<T> initialized to the type's name.
|
||||
(also an incomplete class template IdT<T>, an implementation detail.)
|
||||
The template type parameter T is mapped to a readable name for the type.
|
||||
The work is done at runtime by what is the most standard current method;
|
||||
runtime type information (RTTI) and, on CXXABI, a demangle call
|
||||
(for compile-time alternatives see type_name_pt or type_name_ct).
|
||||
(1) type_name_str<T>()
|
||||
Returns a std::string copy of the demangled typeid name.
|
||||
On each call it does all the work, and cleans it all up
|
||||
(i.e. it frees any demangle allocation once copied from).
|
||||
|
||||
(2) type_name_rt<T>
|
||||
A std::string_view global constant (a view into the
|
||||
demangle buffer, on CXXABI, which is not ever free'd).
|
||||
All work is done in static initialization, before main()
|
||||
|
||||
Failure is signalled by an empty return value; ""
|
||||
(indicates a demangle failure as typeid is assumed fail-safe).
|
||||
Requirements:
|
||||
C++17 for string_view, constexpr-if, CTAD (unique_ptr) and __has_include
|
||||
RTTI, the compiler's runtime type information, must be enabled
|
||||
Dependencies: <string>,<string_view>,<type_traits> for std::conditional
|
||||
<typeinfo> (RTTI)
|
||||
for typeid(T).name(), an implementation-defined name.
|
||||
<cxxabi.h> (on CXXABI platforms only - GCC, Clang, etc.)
|
||||
for abi::__cxa_demangle(name,...)
|
||||
to map typeid(T).name to a human readable name for T.
|
||||
<cstdlib> for std::free, <memory> for std::unique_ptr
|
||||
E.g.
|
||||
int i;
|
||||
std::cout << type_name_rt<decltype(i)> << "\n^^^ tada!";
|
||||
--- stdout ---
|
||||
int
|
||||
^^^ tada!
|
||||
*/
|
||||
|
||||
// IdT<T> wraps T as template param so typeid can't decay ref or cv quals.
|
||||
// An implementation detail; must be a 3-character id, any 3 chars will do.
|
||||
template <typename T> struct IdT {};
|
||||
|
||||
namespace impl
|
||||
{
|
||||
// demangle<bool Free=false>( const char* name)
|
||||
//
|
||||
// (1) On non-CXXABI returns name, regardless of the template parameter.
|
||||
// i.e. the function does nothing but return its parameter, a char*.
|
||||
//
|
||||
// (2) On CXXABI the demangle ABI is called and the result is returned
|
||||
// with return type depending on the boolean template argument:
|
||||
// (a) char* by default (Free=false). Any demangle malloc is not free'd.
|
||||
// (b) unique_ptr<char> (Free=true) to RAII-free any malloc'd chars.
|
||||
//
|
||||
// The input name should be a valid mangled name like typeid(T).name()
|
||||
// Null return value implies demangle fail (no malloc, free is harmless).
|
||||
//
|
||||
template <bool Free = false>
|
||||
auto
|
||||
demangle(char const* name) noexcept(!CXXABI)
|
||||
{
|
||||
if constexpr (!CXXABI) {
|
||||
return name; // NOP: assume already demangled if not on CXXABI
|
||||
} else {
|
||||
auto dmg = abi::__cxa_demangle(name, nullptr, nullptr, nullptr);
|
||||
if constexpr (Free)
|
||||
return std::unique_ptr<char, decltype(std::free)*>( dmg, std::free);
|
||||
else
|
||||
return dmg;
|
||||
}
|
||||
}
|
||||
|
||||
// prefix_len (constant): prefix length of demangled typeid(IdT<T>)
|
||||
// for different compilers (remove 4 chars "int>" from the length)
|
||||
size_t IdT_prefix_len()
|
||||
{
|
||||
static size_t const len = std::strlen(demangle<>(typeid(IdT<int>).name()))
|
||||
- std::strlen("int>");
|
||||
return len;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
|
||||
|
||||
template <typename T>
|
||||
inline constexpr bool is_cvref_v = !std::is_same_v<T,remove_cvref_t<T>>;
|
||||
|
||||
// type_name_rt<T>() Returns string, frees any malloc from ABI demangle
|
||||
// type_name_rt<T,false>() Returns string_view, does not free demangle malloc
|
||||
//
|
||||
template <typename T, bool Free = true, typename = void>
|
||||
auto
|
||||
type_name_rt() noexcept(!CXXABI) -> std::conditional_t<Free, std::string,
|
||||
std::string_view>
|
||||
{
|
||||
if constexpr (!is_cvref_v<T>)
|
||||
{
|
||||
if (auto dmg = demangle<Free>(typeid(T).name()))
|
||||
{
|
||||
return { &*dmg };
|
||||
}
|
||||
}
|
||||
else // wrap all cvref types for now - maybe only do functions and arrays
|
||||
{
|
||||
if (auto dmg = demangle<Free>(typeid(IdT<T>).name()))
|
||||
{
|
||||
size_t const p = IdT_prefix_len();
|
||||
return { &*dmg + p, std::strlen(&*dmg) - p - 1 };
|
||||
}
|
||||
}
|
||||
return "";
|
||||
}
|
||||
} // namespace impl
|
||||
|
||||
// type_name_str<T>() Returns a std::string copy of the demangled typeid name.
|
||||
//
|
||||
template <typename T>
|
||||
std::string
|
||||
const type_name_str() { return impl::type_name_rt<T>(); }
|
||||
|
||||
// type_name_rt<T> Global constant; "The Demangle that Never Dangles"
|
||||
//
|
||||
template <typename T>
|
||||
inline
|
||||
std::string_view
|
||||
const type_name_rt = impl::type_name_rt<T, false>();
|
||||
174
xmake.lua
174
xmake.lua
@@ -1,14 +1,102 @@
|
||||
add_rules("mode.debug", "mode.release")
|
||||
|
||||
includes("@builtin/check")
|
||||
|
||||
add_requires("boost_pfr")
|
||||
|
||||
set_warnings("all")
|
||||
|
||||
set_languages("c++17")
|
||||
|
||||
add_requires("enet6")
|
||||
local modules = {
|
||||
Compression = {
|
||||
Option = "zlib",
|
||||
Deps = {"zlib"},
|
||||
Includes = {"include/(sp/extensions/Compress.h)"},
|
||||
Sources = {"src/sp/extensions/Compress.cpp"}
|
||||
},
|
||||
TcpSocket = {
|
||||
Option = "tcp",
|
||||
Deps = {},
|
||||
Includes = {"include/(sp/extensions/Tcp.h)", "include/(sp/extensions/tcp/*.h)"},
|
||||
Sources = {"src/sp/extensions/Tcp*.cpp"}
|
||||
}
|
||||
}
|
||||
|
||||
target("SimpleProtocolLib")
|
||||
add_includedirs("include", {public = true})
|
||||
set_kind("binary")
|
||||
add_files("src/**.cpp")
|
||||
add_packages("enet6", {public = true})
|
||||
|
||||
|
||||
|
||||
|
||||
-- Map modules to options
|
||||
for name, module in table.orderpairs(modules) do
|
||||
if module.Option then
|
||||
option(module.Option, { description = "Enables the " .. name .. " module", default = true, category = "Modules" })
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
-- Add modules requirements
|
||||
for name, module in table.orderpairs(modules) do
|
||||
if module.Deps then
|
||||
add_requires(module.Deps)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
-- Add modules targets
|
||||
for name, module in table.orderpairs(modules) do
|
||||
if module.Deps and has_config(module.Option) then
|
||||
target("SimpleProtocol-" .. name)
|
||||
add_includedirs("include")
|
||||
for _, include in table.orderpairs(module.Includes) do
|
||||
add_headerfiles(include)
|
||||
end
|
||||
for _, source in table.orderpairs(module.Sources) do
|
||||
add_files(source)
|
||||
end
|
||||
for _, package in table.orderpairs(module.Deps) do
|
||||
add_packages(package)
|
||||
end
|
||||
set_group("Library")
|
||||
set_kind("$(kind)")
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
target("SimpleProtocol")
|
||||
add_includedirs("include")
|
||||
add_files("src/sp/**.cpp")
|
||||
set_group("Library")
|
||||
set_kind("$(kind)")
|
||||
add_packages("boost_pfr", {public = true})
|
||||
check_bigendian("SP_BIG_ENDIAN")
|
||||
|
||||
add_headerfiles("include/(sp/**.h)", "include/(sp/**.inl)")
|
||||
|
||||
-- adding extensions
|
||||
for name, module in table.orderpairs(modules) do
|
||||
if module.Deps and has_config(module.Option) then
|
||||
add_deps("SimpleProtocol-" .. name)
|
||||
end
|
||||
end
|
||||
|
||||
-- we don't want extensions
|
||||
remove_files("src/sp/extensions/**.cpp")
|
||||
remove_headerfiles("include/(sp/extension/**.h)")
|
||||
|
||||
-- we need this for endian functions
|
||||
if is_os("windows") then
|
||||
add_links("ws2_32")
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -19,79 +107,11 @@ for _, file in ipairs(os.files("test/**.cpp")) do
|
||||
set_kind("binary")
|
||||
|
||||
add_files(file)
|
||||
add_includedirs("include")
|
||||
|
||||
set_default(false)
|
||||
set_rundir(".")
|
||||
|
||||
add_deps("SimpleProtocolLib")
|
||||
add_deps("SimpleProtocol")
|
||||
|
||||
add_tests("compile_and_run")
|
||||
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