LibNRG  0.0.1
Networking for Real-time Games library
 All Classes Files Functions Variables Friends
nrg_message.h
Go to the documentation of this file.
1 /*
2  LibNRG - Networking for Real-time Games
3 
4  Copyright (C) 2012-2014 Alex Baines <alex@abaines.me.uk>
5 
6  This software is provided 'as-is', without any express or implied
7  warranty. In no event will the authors be held liable for any damages
8  arising from the use of this software.
9 
10  Permission is granted to anyone to use this software for any purpose,
11  including commercial applications, and to alter it and redistribute it
12  freely, subject to the following restrictions:
13 
14  1. The origin of this software must not be misrepresented; you must not
15  claim that you wrote the original software. If you use this software
16  in a product, an acknowledgment in the product documentation would be
17  appreciated but is not required.
18  2. Altered source versions must be plainly marked as such, and must not be
19  misrepresented as being the original software.
20  3. This notice may not be removed or altered from any source distribution.
21 */
25 #ifndef NRG_MESSAGE_H
26 #define NRG_MESSAGE_H
27 #include "nrg_core.h"
28 #include "nrg_packet.h"
29 #include "nrg_codec.h"
30 #include <tuple>
31 #include <functional>
32 
33 namespace nrg {
34 
36 struct MessageBase {
38  virtual uint16_t getID() const = 0;
39 
41  virtual size_t writeToPacket(Packet& p) const = 0;
42 
44  virtual size_t readFromPacket(Packet& p) = 0;
45 
47  virtual void onReceive(uint32_t creation_ms) = 0;
48 
50  virtual MessageBase* clone() const = 0;
51 
53  virtual MessageBase* move_clone() = 0;
54 
56  virtual ~MessageBase(){};
57 };
58 
60 template<uint16_t id, typename... Args>
61 class Message : public MessageBase {
62  static const size_t sz = sizeof...(Args)-1;
63  typedef std::tuple<Args...> MsgTuple;
64 public:
66  Message(Args... args) : values(args...), on_receive(){}
67 
69  Message(Message&&) = default;
70 
72  Message(const Message&) = default;
73 
75  template<class F>
76  Message(F&& func)
77  : values()
78  , on_receive(std::forward<F>(func)){}
79 
81  template<size_t n>
82  const typename std::tuple_element<n, MsgTuple>::type& get() const {
83  return std::get<n>(values);
84  }
85 
87  template<size_t n>
88  void set(const typename tuple_element<n, MsgTuple>::type& val) {
89  std::get<n>(values) = val;
90  }
91 
93  template<size_t n>
94  typename std::enable_if<n == sz, size_t>::type do_write(Packet& p) const {
95  return Codec<typename std::tuple_element<n, MsgTuple>::type>().encode(p, std::get<n>(values));
96  }
97 
98  template<size_t n>
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))
101  + do_write<n+1>(p);
102  }
103 
104  template<size_t n>
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));
107  }
108 
109  template<size_t n>
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))
112  + do_read<n+1>(p);
113  }
116  uint16_t getID() const {
117  return id;
118  }
119 
120  void onReceive(uint32_t creation_ms){
121  if(on_receive) on_receive(*this, creation_ms);
122  }
123 
124  MessageBase* clone() const {
125  return new Message(*this);
126  }
127 
129  return new Message(std::move(*this));
130  }
131 
132  size_t writeToPacket(Packet& p) const {
133  return do_write<0>(p);
134  }
135 
136  size_t readFromPacket(Packet& p){
137  return do_read<0>(p);
138  }
139 
140 private:
141  MsgTuple values;
142  std::function<void(const Message&, uint32_t)> on_receive;
143 };
144 
145 }
146 #endif
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