cleaner example
This commit is contained in:
21
include/examples/KeepAlivePacket.h
Normal file
21
include/examples/KeepAlivePacket.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/default/DefaultPacket.h>
|
||||
#include <sp/protocol/Field.h>
|
||||
#include <sp/protocol/MessageBase.h>
|
||||
|
||||
enum PacketId {
|
||||
KeepAlive = 0,
|
||||
};
|
||||
|
||||
enum KeepAlivePacketFields {
|
||||
KeepAliveId = 0
|
||||
};
|
||||
|
||||
using KeepAliveFields = std::tuple<sp::Field<std::uint64_t>>;
|
||||
|
||||
class KeepAlivePacket : public sp::MessageBase<sp::PacketMessage,
|
||||
sp::option::StaticNumIdImpl<KeepAlive>, // provide idImpl() if needed
|
||||
sp::option::DispatchImpl<KeepAlivePacket>, // provide dispatchImpl() if needed
|
||||
sp::option::FieldsImpl<KeepAliveFields> // provide access to fields
|
||||
> {};
|
||||
@@ -1,20 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/default/DefaultPacket.h>
|
||||
#include <sp/protocol/Field.h>
|
||||
#include <sp/protocol/MessageBase.h>
|
||||
|
||||
enum PacketId {
|
||||
KeepAlive = 0,
|
||||
};
|
||||
|
||||
using KeepAliveFields = std::tuple<sp::Field<std::uint64_t>>;
|
||||
|
||||
class KeepAlivePacket : public sp::MessageBase<sp::PacketMessage,
|
||||
sp::option::StaticNumIdImpl<KeepAlive>, // provide idImpl() if needed
|
||||
sp::option::DispatchImpl<KeepAlivePacket>, // provide dispatchImpl() if needed
|
||||
sp::option::FieldsImpl<KeepAliveFields> // provide access to fields
|
||||
> {};
|
||||
#include <examples/KeepAlivePacket.h>
|
||||
|
||||
using AllPackets = std::tuple<KeepAlivePacket>;
|
||||
|
||||
|
||||
14
src/main.cpp
14
src/main.cpp
@@ -10,8 +10,20 @@ class KeepAliveHandler : public sp::PacketHandler {
|
||||
};
|
||||
|
||||
int main() {
|
||||
std::unique_ptr<sp::PacketMessage> msg = std::make_unique<KeepAlivePacket>();
|
||||
auto keepAlive = std::make_unique<KeepAlivePacket>();
|
||||
sp::PacketMessage* msg = keepAlive.get();
|
||||
|
||||
KeepAliveHandler handler;
|
||||
msg->Dispatch(handler);
|
||||
|
||||
keepAlive->GetField<KeepAliveId>() = 69;
|
||||
sp::DataBuffer buffer;
|
||||
msg->Write(buffer);
|
||||
|
||||
auto keepAlive2 = std::make_unique<KeepAlivePacket>();
|
||||
keepAlive2->Read(buffer);
|
||||
|
||||
std::cout << "KeepAlive2 : " << keepAlive2->GetField<KeepAliveId>() << "\n";
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user