Pyrogenesis  trunk
NetMessage.h
Go to the documentation of this file.
1 /* Copyright (C) 2017 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef NETMESSAGE_H
19 #define NETMESSAGE_H
20 
21 #include "Serialization.h"
22 
23 // We need the enum from NetMessages.h, but we can't create any classes in
24 // NetMessages.h, since they in turn require CNetMessage to be defined
25 #define ALLNETMSGS_DONT_CREATE_NMTS
26 #include "NetMessages.h"
27 #undef ALLNETMSGS_DONT_CREATE_NMTS
28 
29 /**
30  * The base class for all network messages exchanged within the game.
31  */
32 class CNetMessage : public ISerializable
33 {
34  friend class CNetSession;
35 
36 public:
37 
38  CNetMessage();
40  virtual ~CNetMessage();
41 
42  /**
43  * Retrieves the message type.
44  * @return Message type
45  */
46  NetMessageType GetType() const { return m_Type; }
47 
48  /**
49  * Serialize the message into the specified buffer parameter. The size
50  * required by the buffer parameter can be found by a call to
51  * GetSerializedLength method. The information contained within the message
52  * must be serialized before the message is sent. By default only the
53  * message type and its size are serialized in the buffer parameter.
54  *
55  * @param pBuffer Buffer where to serialize the message
56  * @return The position in the buffer right after the
57  * serialized message
58  */
59  virtual u8* Serialize(u8* pBuffer) const;
60 
61  /**
62  * Deserializes the message from the specified buffer.
63  *
64  * @param pStart Message start within the serialized buffer
65  * @param pEnd Message end within the serialized buffer
66  * @return The position in the buffer right after the
67  * message or NULL if an error occurred
68  */
69  virtual const u8* Deserialize(const u8* pStart, const u8* pEnd);
70 
71  /**
72  * Retrieves the size in bytes of the serialized message. Before calling
73  * Serialize, the memory size for the buffer where to serialize the message
74  * object can be found by calling this method.
75  *
76  * @return The size of serialized message
77  */
78  virtual size_t GetSerializedLength() const;
79 
80  /**
81  * Returns a string representation for the message
82  *
83  * @return The message as a string
84  */
85  virtual CStr ToString() const;
86 
87 private:
88  NetMessageType m_Type; // Message type
89 };
90 
91 /**
92  * Creates messages from data received through the network.
93  */
95 {
96 public:
97 
98  /**
99  * Factory method which creates a message object based on the given data
100  *
101  * @param pData Data buffer
102  * @param dataSize Size of data buffer
103  * @param scriptInterface Script instance to use when constructing scripted messages
104  * @return The new message created
105  */
106  static CNetMessage* CreateMessage(const void* pData, size_t dataSize, const ScriptInterface& scriptInterface);
107 };
108 
109 /**
110  * Special message type for simulation commands.
111  * These commands are exposed as arbitrary JS objects, associated with a specific player.
112  */
114 {
115 public:
116  CSimulationMessage(const ScriptInterface& scriptInterface);
117  CSimulationMessage(const ScriptInterface& scriptInterface, u32 client, i32 player, u32 turn, JS::HandleValue data);
118 
119  /** The compiler can't create a copy constructor because of the PersistentRooted member,
120  * so we have to write it manually.
121  * NOTE: It doesn't clone the m_Data member and the copy will reference the same JS::Value!
122  */
124 
125  virtual u8* Serialize(u8* pBuffer) const;
126  virtual const u8* Deserialize(const u8* pStart, const u8* pEnd);
127  virtual size_t GetSerializedLength() const;
128  virtual CStr ToString() const;
129 
133  JS::PersistentRooted<JS::Value> m_Data;
134 private:
136 };
137 
138 /**
139  * Special message type for updated to game startup settings.
140  */
142 {
144 public:
145  CGameSetupMessage(const ScriptInterface& scriptInterface);
146  CGameSetupMessage(const ScriptInterface& scriptInterface, JS::HandleValue data);
147  virtual u8* Serialize(u8* pBuffer) const;
148  virtual const u8* Deserialize(const u8* pStart, const u8* pEnd);
149  virtual size_t GetSerializedLength() const;
150  virtual CStr ToString() const;
151 
152  JS::PersistentRootedValue m_Data;
153 private:
155 };
156 
157 // This time, the classes are created
158 #include "NetMessages.h"
159 
160 #endif // NETMESSAGE_H
#define NONCOPYABLE(className)
Indicates that a class is noncopyable (usually due to const or reference members, or because the clas...
Definition: code_annotation.h:227
virtual CStr ToString() const
Returns a string representation for the message.
Definition: NetMessage.cpp:84
NetMessageType
Definition: NetMessages.h:41
JS::PersistentRooted< JS::Value > m_Data
Definition: NetMessage.h:133
JS::PersistentRootedValue m_Data
Definition: NetMessage.h:152
friend class CNetSession
Definition: NetMessage.h:34
The list of messages used by the network subsystem.
u32 m_Turn
Definition: NetMessage.h:132
i32 m_Player
Definition: NetMessage.h:131
virtual u8 * Serialize(u8 *pBuffer) const
Serialize the message into the specified buffer parameter.
Definition: NetMessage.cpp:44
const ScriptInterface & m_ScriptInterface
Definition: NetMessage.h:135
uint8_t u8
Definition: types.h:37
virtual size_t GetSerializedLength() const
Retrieves the size in bytes of the serialized message.
Definition: NetMessage.cpp:78
u32 m_Client
Definition: NetMessage.h:130
uint32_t u32
Definition: types.h:39
Creates messages from data received through the network.
Definition: NetMessage.h:94
Special message type for updated to game startup settings.
Definition: NetMessage.h:141
CNetMessage()
Definition: NetMessage.cpp:30
An interface for serializable objects.
Definition: Serialization.h:75
virtual ~CNetMessage()
Definition: NetMessage.cpp:40
The base class for all network messages exchanged within the game.
Definition: NetMessage.h:32
int32_t i32
Definition: types.h:34
NetMessageType GetType() const
Retrieves the message type.
Definition: NetMessage.h:46
const ScriptInterface & m_ScriptInterface
Definition: NetMessage.h:154
Abstraction around a SpiderMonkey JS::Realm.
Definition: ScriptInterface.h:71
virtual const u8 * Deserialize(const u8 *pStart, const u8 *pEnd)
Deserializes the message from the specified buffer.
Definition: NetMessage.cpp:53
NetMessageType m_Type
Definition: NetMessage.h:88
Special message type for simulation commands.
Definition: NetMessage.h:113