28 #include "nrg_packet.h"
38 virtual uint16_t
getID()
const = 0;
47 virtual void onReceive(uint32_t creation_ms) = 0;
60 template<uint16_t id,
typename... Args>
62 static const size_t sz =
sizeof...(Args)-1;
63 typedef std::tuple<Args...> MsgTuple;
66 Message(Args... args) : values(args...), on_receive(){}
78 , on_receive(std::forward<F>(func)){}
82 const typename std::tuple_element<n, MsgTuple>::type&
get()
const {
83 return std::get<n>(values);
88 void set(
const typename tuple_element<n, MsgTuple>::type& val) {
89 std::get<n>(values) = val;
94 typename std::enable_if<n == sz, size_t>::type do_write(
Packet& p)
const {
99 typename std::enable_if<n != sz, size_t>::type do_write(Packet& p)
const {
100 return Codec<typename std::tuple_element<n, MsgTuple>::type>().encode(p, std::get<n>(values))
105 typename std::enable_if<n == sz, size_t>::type do_read(Packet& p){
106 return Codec<typename std::tuple_element<n, MsgTuple>::type>().decode(p, std::get<n>(values));
110 typename std::enable_if<n != sz, size_t>::type do_read(Packet& p){
111 return Codec<typename std::tuple_element<n, MsgTuple>::type>().decode(p, std::get<n>(values))
121 if(on_receive) on_receive(*
this, creation_ms);
129 return new Message(std::move(*
this));
133 return do_write<0>(p);
137 return do_read<0>(p);
142 std::function<void(const Message&, uint32_t)> on_receive;
Abstract base class for Message.
Definition: nrg_message.h:36
virtual MessageBase * clone() const =0
Returns a copy of the derived class of this MessageBase.
virtual size_t writeToPacket(Packet &p) const =0
Writes this Message to the Packet p.
size_t readFromPacket(Packet &p)
Reads this Message from the Packet p.
Definition: nrg_message.h:136
Contains the Codec class, which encodes and decodes types to and from Packets.
Class for storing data to be sent / received across the network.
Definition: nrg_packet.h:58
Encodes and Decodes any type into a Packet object.
Definition: nrg_codec.h:55
uint16_t getID() const
Returns the user-defined Message ID.
Definition: nrg_message.h:116
Message(F &&func)
Internally used Constructor for a Message that will run a callback function.
Definition: nrg_message.h:76
void onReceive(uint32_t creation_ms)
Function called when the message has been received.
Definition: nrg_message.h:120
MessageBase * clone() const
Returns a copy of the derived class of this MessageBase.
Definition: nrg_message.h:124
size_t writeToPacket(Packet &p) const
Writes this Message to the Packet p.
Definition: nrg_message.h:132
MessageBase * move_clone()
Moves the derived class into the returned MessageBase.
Definition: nrg_message.h:128
Variadic template class that encodes / decodes its data to and from Packets.
Definition: nrg_message.h:61
void set(const typename tuple_element< n, MsgTuple >::type &val)
Sets the element of the Message specified by the template parameter n to val.
Definition: nrg_message.h:88
Message(Args...args)
Standard Constructor for a Message to be sent over the network.
Definition: nrg_message.h:66
virtual MessageBase * move_clone()=0
Moves the derived class into the returned MessageBase.
virtual void onReceive(uint32_t creation_ms)=0
Function called when the message has been received.
virtual size_t readFromPacket(Packet &p)=0
Reads this Message from the Packet p.
virtual uint16_t getID() const =0
Returns the user-defined Message ID.
Common defines and includes used by all the other nrg header files.
virtual ~MessageBase()
Standard Destructor.
Definition: nrg_message.h:56