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