3 Commits

Author SHA1 Message Date
f145716cf6 serialize maps 2025-02-23 11:27:57 +01:00
6ee7524e17 update README 2025-02-23 10:50:39 +01:00
db0c5f3245 fix alignment + refactor
Reviewed-on: #1
Co-authored-by: Persson-dev <sim16.prib@gmail.com>
Co-committed-by: Persson-dev <sim16.prib@gmail.com>
2025-02-23 09:40:46 +00:00
4 changed files with 49 additions and 7 deletions

10
README.md Normal file
View File

@@ -0,0 +1,10 @@
# SimpleProtocolLib
Network engine used to (mainly) communicate with packets
# Integrate with xmake
```lua
add_repositories("persson-repo https://git.ale-pri.com/Persson-dev/xmake-repo.git")
add_requires("splib", { debug = is_mode("debug") })
```

View File

@@ -13,6 +13,7 @@
#include <string> #include <string>
#include <sp/common/VarInt.h> #include <sp/common/VarInt.h>
#include <vector> #include <vector>
#include <map>
namespace sp { namespace sp {
@@ -88,6 +89,19 @@ class DataBuffer {
return *this; return *this;
} }
/**
* \brief Append a map to the buffer by first writing the size
* \param data The map to append
*/
template <typename K, typename V>
DataBuffer& operator<<(const std::map<K, V>& data) {
*this << VarInt{data.size()};
for (const auto& element : data) {
*this << element.first << element.second;
}
return *this;
}
/** /**
* \brief Append an array to the buffer by first writing the size * \brief Append an array to the buffer by first writing the size
* \param data The buffer to append * \param data The buffer to append
@@ -140,6 +154,23 @@ class DataBuffer {
return *this; return *this;
} }
/**
* \brief Read a map (size + data) from the buffer
* \pre The map is assumed to be empty
*/
template <typename K, typename V>
DataBuffer& operator>>(std::map<K, V>& data) {
VarInt mapSize;
*this >> mapSize;
for (std::size_t i = 0; i < mapSize.GetValue(); i++) {
K newKey;
V newValue;
*this >> newKey >> newValue;
data.insert({newKey, newValue});
}
return *this;
}
/** /**
* \brief Read an array from the buffer * \brief Read an array from the buffer
*/ */

View File

@@ -6,12 +6,13 @@
namespace sp { namespace sp {
class PacketHandler; class PacketHandler;
using PacketMessage = Message<option::MsgIdType<std::uint8_t>, // add id() operation using PacketMessage = Message<
option::ReadOperations, // add read() operation option::MsgIdType<std::uint8_t>, // add id() operation
option::WriteOperations, // add write() operation option::ReadOperations, // add read() operation
option::WriteId, // write id before data option::WriteOperations, // add write() operation
option::Handler<PacketHandler> // add dispatch() operation option::WriteId, // write id before data
>; option::Handler<PacketHandler> // add dispatch() operation
>;
#define PacketConstructor(packetName) \ #define PacketConstructor(packetName) \
packetName##Packet() {} \ packetName##Packet() {} \

View File

@@ -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