dirty messages
This commit is contained in:
151
src/main.cpp
151
src/main.cpp
@@ -1,125 +1,58 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <sp/Field.h>
|
||||
#include <sp/GenericHandler.h>
|
||||
#include <sp/MessageBase.h>
|
||||
#include <sp/Templates.h>
|
||||
|
||||
#define PacketClass(className) class className : public sp::MessageBase<className>
|
||||
#include <memory>
|
||||
|
||||
template <typename T>
|
||||
class Packet : public sp::MessageBase<T> {
|
||||
public:
|
||||
virtual std::string_view GetName() const = 0;
|
||||
void WriteImpl(sp::DataBuffer& buffer) const = 0;
|
||||
void ReadImpl(const sp::DataBuffer& buffer) = 0;
|
||||
int GetId() const = 0;
|
||||
enum MyMsgId {
|
||||
MyMsgId_Msg1,
|
||||
MyMsgId_Msg2,
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class Command : public sp::MessageBase<T> {
|
||||
class MyHandler; // forward declaration of the handler class.
|
||||
|
||||
using MyMessage = sp::Message<sp::option::MsgIdType<MyMsgId>, // add id() operation
|
||||
sp::option::ReadOperations, // add read() operation
|
||||
sp::option::WriteOperations, // add write() operation
|
||||
sp::option::Handler<MyHandler>, // add dispatch() operation
|
||||
sp::option::ValidCheckInterface, // add valid() operation
|
||||
sp::option::LittleEndian // use little endian for serialisation
|
||||
>;
|
||||
|
||||
|
||||
|
||||
using ActualMessage1Fields = std::tuple<sp::Field<std::uint16_t>, sp::Field<std::int8_t>>;
|
||||
|
||||
|
||||
|
||||
template <typename TMessageInterface>
|
||||
class ActualMessage1 : public sp::MessageBase<TMessageInterface,
|
||||
sp::option::StaticNumIdImpl<MyMsgId_Msg1>, // provide idImpl() if needed
|
||||
sp::option::DispatchImpl<ActualMessage1<TMessageInterface>>, // provide dispatchImpl() if needed
|
||||
sp::option::FieldsImpl<ActualMessage1Fields> // provide access to fields and
|
||||
// readImpl(), writeImpl(),
|
||||
// lengthImpl(), validImpl()
|
||||
// functions if needed
|
||||
> {};
|
||||
|
||||
using MyActualMessage1 = ActualMessage1<MyMessage>;
|
||||
|
||||
using AllMessages = std::tuple<MyActualMessage1>;
|
||||
|
||||
class MyHandler : public sp::GenericHandler<MyMessage, AllMessages> {
|
||||
public:
|
||||
virtual std::string_view GetName() const = 0;
|
||||
void WriteImpl(sp::DataBuffer& buffer) const = 0;
|
||||
void ReadImpl(const sp::DataBuffer& buffer) = 0;
|
||||
int GetId() const = 0;
|
||||
};
|
||||
|
||||
|
||||
#define TestPacket(packetName) \
|
||||
class packetName : public Packet<packetName> { \
|
||||
public: \
|
||||
std::string_view GetName() const { \
|
||||
return #packetName; \
|
||||
} \
|
||||
void WriteImpl(sp::DataBuffer& buffer) const {} \
|
||||
void ReadImpl(const sp::DataBuffer& buffer) {} \
|
||||
int GetId() const { \
|
||||
return 0; \
|
||||
} \
|
||||
}
|
||||
|
||||
#define TestCommand(commandName) \
|
||||
class commandName : public Command<commandName> { \
|
||||
public: \
|
||||
std::string_view GetName() const { \
|
||||
return #commandName; \
|
||||
} \
|
||||
void WriteImpl(sp::DataBuffer& buffer) const {} \
|
||||
void ReadImpl(const sp::DataBuffer& buffer) {} \
|
||||
int GetId() const { \
|
||||
return 0; \
|
||||
} \
|
||||
}
|
||||
|
||||
TestPacket(ChatPacket);
|
||||
TestPacket(PlayerJoinPacket);
|
||||
TestPacket(PlayerLeavePacket);
|
||||
|
||||
TestCommand(SpawnTroopCommand);
|
||||
TestCommand(PlaceTowerCommand);
|
||||
|
||||
class ActualMessage2 : public sp::MessageBase<ActualMessage2> {
|
||||
std::string_view GetName() const {
|
||||
return "mesmes";
|
||||
}
|
||||
void WriteImpl(sp::DataBuffer& buffer) const {}
|
||||
void ReadImpl(const sp::DataBuffer& buffer) {}
|
||||
int GetId() const {
|
||||
return 0;
|
||||
}
|
||||
};
|
||||
|
||||
using AllPackets = std::tuple<ChatPacket, PlayerJoinPacket, PlayerLeavePacket>;
|
||||
using AllCommands = std::tuple<SpawnTroopCommand, PlaceTowerCommand>;
|
||||
using AllTests = std::tuple<ActualMessage2>;
|
||||
|
||||
using AllMessages = sp::tuple_cat_t<AllPackets, AllCommands, AllTests>;
|
||||
|
||||
namespace sp {
|
||||
class Handler : public sp::GenericHandler<sp::Message, AllMessages> {};
|
||||
} // namespace sp
|
||||
|
||||
using PacketHandler = sp::Handler;
|
||||
using CommandHandler = sp::Handler;
|
||||
|
||||
class PacketPrinter : public PacketHandler {
|
||||
public:
|
||||
void Handle(ChatPacket& packet) override {
|
||||
std::cout << packet.GetName() << std::endl;
|
||||
}
|
||||
|
||||
// void Handle(SpawnTroopCommand& cmd) {
|
||||
// std::cout << "NOOOOOO\n";
|
||||
// }
|
||||
};
|
||||
|
||||
|
||||
|
||||
class ActualHandler1 : public sp::Handler {
|
||||
public:
|
||||
virtual void Handle(ActualMessage2& msg) override {
|
||||
std::cout << "Handling ActualMessage2" << std::endl;
|
||||
}
|
||||
|
||||
virtual void Handle(sp::Message& msg) override {
|
||||
std::cout << "Common handling function is invoked" << std::endl;
|
||||
}
|
||||
|
||||
virtual void Handle(ChatPacket& msg) override {
|
||||
std::cout << "Chat invoked" << std::endl;
|
||||
void Handle(MyActualMessage1& msg) {
|
||||
std::cout << "Got message 1 !\n";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main() {
|
||||
std::unique_ptr<sp::Message> msg = std::make_unique<ActualMessage2>();
|
||||
std::unique_ptr<sp::Message> chat = std::make_unique<ChatPacket>();
|
||||
SpawnTroopCommand cmd;
|
||||
PacketPrinter packetHandler;
|
||||
PacketHandler pp;
|
||||
chat->Dispatch(packetHandler);
|
||||
ActualHandler1 handler;
|
||||
msg->Dispatch(handler);
|
||||
chat->Dispatch(handler);
|
||||
MyMessage::MsgIdType test;
|
||||
auto yes = std::make_unique<ActualMessage1<MyMessage>>();
|
||||
MyHandler handlerTest;
|
||||
yes->Dispatch(handlerTest);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user