Compare commits
3 Commits
032800b220
...
51d7c8f66d
| Author | SHA1 | Date | |
|---|---|---|---|
| 51d7c8f66d | |||
| 534757f884 | |||
| 6725a63c07 |
@@ -9,6 +9,7 @@ class PacketHandler;
|
||||
using PacketMessage = Message<option::MsgIdType<std::uint8_t>, // add id() operation
|
||||
option::ReadOperations, // add read() operation
|
||||
option::WriteOperations, // add write() operation
|
||||
option::WriteId, // write id before data
|
||||
option::Handler<PacketHandler> // add dispatch() operation
|
||||
>;
|
||||
|
||||
|
||||
@@ -35,34 +35,6 @@ class Field {
|
||||
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>
|
||||
|
||||
@@ -121,9 +121,10 @@ 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);
|
||||
auto& allFields = this->GetFields();
|
||||
std::apply([&buffer, this]<typename T>(T& field) {
|
||||
this->ReadData(field.GetValue(), buffer);
|
||||
}, allFields);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -131,9 +132,10 @@ 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);
|
||||
auto& allFields = this->GetFields();
|
||||
std::apply([&buffer, this]<typename T>(T& field) {
|
||||
this->WriteData(field.GetValue(), buffer);
|
||||
}, allFields);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -30,8 +30,11 @@ struct MessageInterfaceBuilder {
|
||||
// add valid functionality if Valid tpe was provided
|
||||
using Base6 = typename MessageInterfaceProcessValid<Base5, ParsedOptions::HasValid>::Type;
|
||||
|
||||
// add write id functionality if write id and write was provided
|
||||
using Base7 = typename MessageInterfaceProcessWriteId<Base6, ParsedOptions::HasWriteId && ParsedOptions::HasWriteOperations>::Type;
|
||||
|
||||
// The last Base6 must be taken as final type.
|
||||
using Type = Base6;
|
||||
using Type = Base7;
|
||||
};
|
||||
|
||||
} // namespace details
|
||||
|
||||
@@ -14,6 +14,7 @@ struct MessageInterfaceParsedOptions<> {
|
||||
static const bool HasLittleEndian = false;
|
||||
static const bool HasReadOperations = false;
|
||||
static const bool HasWriteOperations = false;
|
||||
static const bool HasWriteId = false;
|
||||
static const bool HasHandler = false;
|
||||
static const bool HasValid = false;
|
||||
};
|
||||
@@ -43,6 +44,11 @@ struct MessageInterfaceParsedOptions<option::WriteOperations, TOptions...> : pub
|
||||
static const bool HasWriteOperations = true;
|
||||
};
|
||||
|
||||
template <typename... TOptions>
|
||||
struct MessageInterfaceParsedOptions<option::WriteId, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||
static const bool HasWriteId = true;
|
||||
};
|
||||
|
||||
template <typename T, typename... TOptions>
|
||||
struct MessageInterfaceParsedOptions<option::Handler<T>, TOptions...> : public MessageInterfaceParsedOptions<TOptions...> {
|
||||
static const bool HasHandler = true;
|
||||
@@ -76,13 +82,15 @@ template <typename TBase>
|
||||
class MessageInterfaceBigEndian : public TBase {
|
||||
protected:
|
||||
template <typename T>
|
||||
static T ReadData(DataBuffer& buffer) {
|
||||
// use big endian
|
||||
void ReadData(T& value, DataBuffer& buffer) {
|
||||
//TODO: use big endian
|
||||
buffer >> value;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void WriteData(T value, DataBuffer& buffer) {
|
||||
// use big endian
|
||||
void WriteData(T value, DataBuffer& buffer) {
|
||||
//TODO: use big endian
|
||||
buffer << value;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -91,13 +99,15 @@ template <typename TBase>
|
||||
class MessageInterfaceLittleEndian : public TBase {
|
||||
protected:
|
||||
template <typename T>
|
||||
static T ReadData(DataBuffer& buffer) {
|
||||
// use little endian
|
||||
void ReadData(T& value, DataBuffer& buffer) {
|
||||
//TODO: use little endian
|
||||
buffer >> value;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static void WriteData(const T& value, DataBuffer& buffer) {
|
||||
// use little endian
|
||||
void WriteData(const T& value, DataBuffer& buffer) {
|
||||
//TODO: use little endian
|
||||
buffer << value;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -118,7 +128,7 @@ template <typename TBase>
|
||||
class MessageInterfaceWriteBase : public TBase {
|
||||
public:
|
||||
void Write(DataBuffer& buffer) {
|
||||
return WriteImpl(buffer);
|
||||
WriteImpl(buffer);
|
||||
}
|
||||
|
||||
protected:
|
||||
@@ -151,7 +161,15 @@ class MessageInterfaceValidityBase : public TBase {
|
||||
virtual bool ValidImpl() const = 0;
|
||||
};
|
||||
|
||||
|
||||
// Writing id functionality chunk
|
||||
template <typename TBase>
|
||||
class MessageInterfaceWriteIdBase : public TBase {
|
||||
public:
|
||||
void Write(DataBuffer& buffer) {
|
||||
this->WriteData(this->GetId(), buffer);
|
||||
this->WriteImpl(buffer);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -239,5 +257,19 @@ template <typename TBase>
|
||||
struct MessageInterfaceProcessValid<TBase, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
|
||||
// Build id writing
|
||||
template <typename TBase, bool THasValid>
|
||||
struct MessageInterfaceProcessWriteId;
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageInterfaceProcessWriteId<TBase, true> {
|
||||
using Type = MessageInterfaceWriteIdBase<TBase>;
|
||||
};
|
||||
|
||||
template <typename TBase>
|
||||
struct MessageInterfaceProcessWriteId<TBase, false> {
|
||||
using Type = TBase;
|
||||
};
|
||||
} // namespace details
|
||||
} // namespace sp
|
||||
|
||||
@@ -15,6 +15,9 @@ struct ReadOperations {};
|
||||
// Enable writing
|
||||
struct WriteOperations {};
|
||||
|
||||
// Enable id writing
|
||||
struct WriteId {};
|
||||
|
||||
// Use little endian for serialisation (instead of default big)
|
||||
struct LittleEndian {};
|
||||
|
||||
|
||||
10
src/main.cpp
10
src/main.cpp
@@ -1,7 +1,7 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <memory>
|
||||
#include <examples/PacketExample.h>
|
||||
#include <memory>
|
||||
|
||||
class KeepAliveHandler : public sp::PacketHandler {
|
||||
void Handle(KeepAlivePacket& packet) {
|
||||
@@ -23,19 +23,21 @@ int main() {
|
||||
sp::DataBuffer buffer;
|
||||
msg->Write(buffer);
|
||||
|
||||
std::uint8_t msgId;
|
||||
buffer >> msgId;
|
||||
|
||||
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);
|
||||
auto packet = factory.CreateMessage(msgId);
|
||||
if (packet == nullptr) {
|
||||
std::cout << "Mauvais ID !\n";
|
||||
return 1;
|
||||
}
|
||||
std::cout << (unsigned) packet->GetId() << std::endl;
|
||||
std::cout << (unsigned)packet->GetId() << std::endl;
|
||||
packet->Dispatch(handler);
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user