Compare commits
17 Commits
v1.0.2
...
014db34116
| Author | SHA1 | Date | |
|---|---|---|---|
| 014db34116 | |||
| d99137d78d | |||
| 3bc1f60531 | |||
| 992a6eeca0 | |||
| 8762222e0f | |||
| c669e459dd | |||
| 3bc5b50905 | |||
| 66835554f1 | |||
| e16ad84865 | |||
| c1e6409c18 | |||
| 3ad18cacf6 | |||
| 2eab50932f | |||
| baa52d3baa | |||
| d924685e0c | |||
| 044b12cdec | |||
| e39f8de898 | |||
| 0b28bde25b |
10
README.md
10
README.md
@@ -1,10 +0,0 @@
|
|||||||
# SimpleProtocolLib
|
|
||||||
|
|
||||||
Network engine used to (mainly) communicate with packets
|
|
||||||
|
|
||||||
# Integrate with xmake
|
|
||||||
|
|
||||||
```lua
|
|
||||||
add_repositories("persson-repo https://git.ale-pri.com/Persson-dev/xmake-repo.git")
|
|
||||||
add_requires("splib", { debug = is_mode("debug") })
|
|
||||||
```
|
|
||||||
@@ -1,10 +1,26 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <winsock2.h>
|
||||||
|
#else
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
|
||||||
namespace sp {
|
namespace sp {
|
||||||
|
|
||||||
bool IsSystemBigEndian();
|
template <typename T>
|
||||||
|
void SwapBytes(T& value) {
|
||||||
|
char* ptr = reinterpret_cast<char*>(&value);
|
||||||
|
std::reverse(ptr, ptr + sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool IsSystemBigEndian() {
|
||||||
|
static constexpr std::uint16_t test = 10;
|
||||||
|
static const bool isBigEndian = reinterpret_cast<const std::uint8_t*>(&test)[1] == 10;
|
||||||
|
return isBigEndian;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Serialize value to (network byte order) big endian
|
* \brief Serialize value to (network byte order) big endian
|
||||||
@@ -13,13 +29,21 @@ template <typename T>
|
|||||||
void ToNetwork(T& value) {}
|
void ToNetwork(T& value) {}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void ToNetwork<std::uint16_t>(std::uint16_t& value);
|
void ToNetwork<std::uint16_t>(std::uint16_t& value) {
|
||||||
|
value = htons(value);
|
||||||
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void ToNetwork<std::uint32_t>(std::uint32_t& value);
|
void ToNetwork<std::uint32_t>(std::uint32_t& value) {
|
||||||
|
value = htonl(value);
|
||||||
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void ToNetwork<std::uint64_t>(std::uint64_t& value);
|
void ToNetwork<std::uint64_t>(std::uint64_t& value) {
|
||||||
|
if (IsSystemBigEndian())
|
||||||
|
return;
|
||||||
|
SwapBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Deserialize value from (network byte order) big endian
|
* \brief Deserialize value from (network byte order) big endian
|
||||||
@@ -28,13 +52,21 @@ template <typename T>
|
|||||||
void FromNetwork(T& value) {}
|
void FromNetwork(T& value) {}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void FromNetwork<std::uint16_t>(std::uint16_t& value);
|
void FromNetwork<std::uint16_t>(std::uint16_t& value) {
|
||||||
|
value = ntohs(value);
|
||||||
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void FromNetwork<std::uint32_t>(std::uint32_t& value);
|
void FromNetwork<std::uint32_t>(std::uint32_t& value) {
|
||||||
|
value = ntohl(value);
|
||||||
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void FromNetwork<std::uint64_t>(std::uint64_t& value);
|
void FromNetwork<std::uint64_t>(std::uint64_t& value) {
|
||||||
|
if (IsSystemBigEndian())
|
||||||
|
return;
|
||||||
|
SwapBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Swap bytes if the value is any kind of integer
|
* \brief Swap bytes if the value is any kind of integer
|
||||||
@@ -43,12 +75,18 @@ template <typename T>
|
|||||||
void TrySwapBytes(T& value) {}
|
void TrySwapBytes(T& value) {}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void TrySwapBytes<std::uint16_t>(std::uint16_t& value);
|
void TrySwapBytes<std::uint16_t>(std::uint16_t& value) {
|
||||||
|
SwapBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void TrySwapBytes<std::uint32_t>(std::uint32_t& value);
|
void TrySwapBytes<std::uint32_t>(std::uint32_t& value) {
|
||||||
|
SwapBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
void TrySwapBytes<std::uint64_t>(std::uint64_t& value);
|
void TrySwapBytes<std::uint64_t>(std::uint64_t& value) {
|
||||||
|
SwapBytes(value);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace sp
|
} // namespace sp
|
||||||
|
|||||||
@@ -13,7 +13,6 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <sp/common/VarInt.h>
|
#include <sp/common/VarInt.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <map>
|
|
||||||
|
|
||||||
namespace sp {
|
namespace sp {
|
||||||
|
|
||||||
@@ -89,19 +88,6 @@ class DataBuffer {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Append a map to the buffer by first writing the size
|
|
||||||
* \param data The map to append
|
|
||||||
*/
|
|
||||||
template <typename K, typename V>
|
|
||||||
DataBuffer& operator<<(const std::map<K, V>& data) {
|
|
||||||
*this << VarInt{data.size()};
|
|
||||||
for (const auto& element : data) {
|
|
||||||
*this << element.first << element.second;
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Append an array to the buffer by first writing the size
|
* \brief Append an array to the buffer by first writing the size
|
||||||
* \param data The buffer to append
|
* \param data The buffer to append
|
||||||
@@ -154,23 +140,6 @@ class DataBuffer {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Read a map (size + data) from the buffer
|
|
||||||
* \pre The map is assumed to be empty
|
|
||||||
*/
|
|
||||||
template <typename K, typename V>
|
|
||||||
DataBuffer& operator>>(std::map<K, V>& data) {
|
|
||||||
VarInt mapSize;
|
|
||||||
*this >> mapSize;
|
|
||||||
for (std::size_t i = 0; i < mapSize.GetValue(); i++) {
|
|
||||||
K newKey;
|
|
||||||
V newValue;
|
|
||||||
*this >> newKey >> newValue;
|
|
||||||
data.insert({newKey, newValue});
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Read an array from the buffer
|
* \brief Read an array from the buffer
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -6,8 +6,7 @@
|
|||||||
namespace sp {
|
namespace sp {
|
||||||
class PacketHandler;
|
class PacketHandler;
|
||||||
|
|
||||||
using PacketMessage = Message<
|
using PacketMessage = Message<option::MsgIdType<std::uint8_t>, // add id() operation
|
||||||
option::MsgIdType<std::uint8_t>, // add id() operation
|
|
||||||
option::ReadOperations, // add read() operation
|
option::ReadOperations, // add read() operation
|
||||||
option::WriteOperations, // add write() operation
|
option::WriteOperations, // add write() operation
|
||||||
option::WriteId, // write id before data
|
option::WriteId, // write id before data
|
||||||
|
|||||||
@@ -114,6 +114,6 @@ template <typename TField, typename... TFields>
|
|||||||
struct FieldsBuilder<TField, TFields...> {
|
struct FieldsBuilder<TField, TFields...> {
|
||||||
using Type = sp::tuple_cat_t<std::tuple<Field<TField, 0>>, typename FieldsBuilder<TFields...>::Type>;
|
using Type = sp::tuple_cat_t<std::tuple<Field<TField, 0>>, typename FieldsBuilder<TFields...>::Type>;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace details
|
} // namespace details
|
||||||
|
|
||||||
} // namespace sp
|
} // namespace sp
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ class MessageInterfaceBigEndian : public TBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void WriteData(T value, DataBuffer& buffer) const {
|
void WriteData(T value, DataBuffer& buffer) {
|
||||||
ToNetwork(value);
|
ToNetwork(value);
|
||||||
buffer << value;
|
buffer << value;
|
||||||
}
|
}
|
||||||
@@ -71,12 +71,12 @@ class MessageInterfaceReadBase : public TBase {
|
|||||||
template <typename TBase>
|
template <typename TBase>
|
||||||
class MessageInterfaceWriteBase : public TBase {
|
class MessageInterfaceWriteBase : public TBase {
|
||||||
public:
|
public:
|
||||||
void Write(DataBuffer& buffer) const {
|
void Write(DataBuffer& buffer) {
|
||||||
WriteImpl(buffer);
|
WriteImpl(buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void WriteImpl(DataBuffer& buffer) const = 0;
|
virtual void WriteImpl(DataBuffer& buffer) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Handler functionality chunk
|
// Handler functionality chunk
|
||||||
@@ -85,12 +85,12 @@ class MessageInterfaceHandlerBase : public TBase {
|
|||||||
public:
|
public:
|
||||||
using HandlerType = typename THandler::HandlerT;
|
using HandlerType = typename THandler::HandlerT;
|
||||||
|
|
||||||
void Dispatch(HandlerType& handler) const {
|
void Dispatch(HandlerType& handler) {
|
||||||
DispatchImpl(handler);
|
DispatchImpl(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void DispatchImpl(HandlerType& handler) const = 0;
|
virtual void DispatchImpl(HandlerType& handler) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Validity functionality chunk
|
// Validity functionality chunk
|
||||||
@@ -109,7 +109,7 @@ class MessageInterfaceValidityBase : public TBase {
|
|||||||
template <typename TBase>
|
template <typename TBase>
|
||||||
class MessageInterfaceWriteIdBase : public TBase {
|
class MessageInterfaceWriteIdBase : public TBase {
|
||||||
public:
|
public:
|
||||||
void Write(DataBuffer& buffer) const {
|
void Write(DataBuffer& buffer) {
|
||||||
this->WriteData(this->GetId(), buffer);
|
this->WriteData(this->GetId(), buffer);
|
||||||
this->WriteImpl(buffer);
|
this->WriteImpl(buffer);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,8 +26,8 @@ class MessageImplDispatchBase : public TBase {
|
|||||||
using Handler = typename TBase::HandlerType;
|
using Handler = typename TBase::HandlerType;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void DispatchImpl(Handler& handler) const override {
|
virtual void DispatchImpl(Handler& handler) override {
|
||||||
handler.Handle(static_cast<const TActual&>(*this));
|
handler.Handle(static_cast<TActual&>(*this));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -113,13 +113,13 @@ class MessageImplFieldsWriteBase : public TBase {
|
|||||||
private:
|
private:
|
||||||
// normal writing
|
// normal writing
|
||||||
template <typename TField>
|
template <typename TField>
|
||||||
void WriteField(const Field<TField, 0>& field, DataBuffer& buffer) const {
|
void WriteField(Field<TField, 0>& field, DataBuffer& buffer) {
|
||||||
this->WriteData(field.GetValue(), buffer);
|
this->WriteData(field.GetValue(), buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
// writing field in bitfield
|
// writing field in bitfield
|
||||||
template <typename TFieldType, typename TField, int IAlignment>
|
template <typename TFieldType, typename TField, int IAlignment>
|
||||||
void WriteField(const Field<TField, IAlignment>& field, TFieldType& data, std::size_t offset) const {
|
void WriteField(Field<TField, IAlignment>& field, TFieldType& data, std::size_t offset) {
|
||||||
static constexpr std::size_t TotalBitCount = sizeof(TFieldType) * 8;
|
static constexpr std::size_t TotalBitCount = sizeof(TFieldType) * 8;
|
||||||
// we suppose that the first element is at the highest bits
|
// we suppose that the first element is at the highest bits
|
||||||
data |= (field.GetValue() & ((1 << IAlignment) - 1)) << TotalBitCount - IAlignment - offset;
|
data |= (field.GetValue() & ((1 << IAlignment) - 1)) << TotalBitCount - IAlignment - offset;
|
||||||
@@ -127,7 +127,7 @@ class MessageImplFieldsWriteBase : public TBase {
|
|||||||
|
|
||||||
// writing bitfield
|
// writing bitfield
|
||||||
template <typename TContainer, typename TFirst, typename... TFields>
|
template <typename TContainer, typename TFirst, typename... TFields>
|
||||||
void WriteField(const Field<BitField<TContainer, TFirst, TFields...>, 0>& field, DataBuffer& buffer) const {
|
void WriteField(Field<BitField<TContainer, TFirst, TFields...>, 0>& field, DataBuffer& buffer) {
|
||||||
TContainer data = 0;
|
TContainer data = 0;
|
||||||
std::size_t offset = 0;
|
std::size_t offset = 0;
|
||||||
TupleForEach(
|
TupleForEach(
|
||||||
@@ -139,9 +139,9 @@ class MessageImplFieldsWriteBase : public TBase {
|
|||||||
this->WriteData(data, buffer);
|
this->WriteData(data, buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
void WriteImpl(DataBuffer& buffer) const override {
|
void WriteImpl(DataBuffer& buffer) override {
|
||||||
auto& allFields = this->GetFields();
|
auto& allFields = this->GetFields();
|
||||||
TupleForEach([&buffer, this](const auto& field) { this->WriteField(field, buffer); }, allFields);
|
TupleForEach([&buffer, this](auto& field) { this->WriteField(field, buffer); }, allFields);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,74 +0,0 @@
|
|||||||
#include <sp/common/ByteSwapping.h>
|
|
||||||
|
|
||||||
#ifdef _WIN32
|
|
||||||
#include <winsock2.h>
|
|
||||||
#else
|
|
||||||
#include <arpa/inet.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
namespace sp {
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
void SwapBytes(T& value) {
|
|
||||||
char* ptr = reinterpret_cast<char*>(&value);
|
|
||||||
std::reverse(ptr, ptr + sizeof(T));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool IsSystemBigEndian() {
|
|
||||||
static constexpr std::uint16_t test = 10;
|
|
||||||
static const bool isBigEndian = reinterpret_cast<const std::uint8_t*>(&test)[1] == 10;
|
|
||||||
return isBigEndian;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
void ToNetwork<std::uint16_t>(std::uint16_t& value) {
|
|
||||||
value = htons(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
void ToNetwork<std::uint32_t>(std::uint32_t& value) {
|
|
||||||
value = htonl(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
void ToNetwork<std::uint64_t>(std::uint64_t& value) {
|
|
||||||
if (IsSystemBigEndian())
|
|
||||||
return;
|
|
||||||
SwapBytes(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
void FromNetwork<std::uint16_t>(std::uint16_t& value) {
|
|
||||||
value = ntohs(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
void FromNetwork<std::uint32_t>(std::uint32_t& value) {
|
|
||||||
value = ntohl(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
void FromNetwork<std::uint64_t>(std::uint64_t& value) {
|
|
||||||
if (IsSystemBigEndian())
|
|
||||||
return;
|
|
||||||
SwapBytes(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
void TrySwapBytes<std::uint16_t>(std::uint16_t& value) {
|
|
||||||
SwapBytes(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
void TrySwapBytes<std::uint32_t>(std::uint32_t& value) {
|
|
||||||
SwapBytes(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
void TrySwapBytes<std::uint64_t>(std::uint64_t& value) {
|
|
||||||
SwapBytes(value);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace sp
|
|
||||||
Reference in New Issue
Block a user