Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
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 */
33{
34 friend class CNetSession;
35
36public:
37
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
87private:
88 NetMessageType m_Type; // Message type
89};
90
91/**
92 * Creates messages from data received through the network.
93 */
95{
96public:
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{
115public:
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;
134private:
136};
137
138/**
139 * Special message type for updated to game startup settings.
140 */
142{
144public:
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;
153private:
155};
156
157// This time, the classes are created
158#include "NetMessages.h"
159
160#endif // NETMESSAGE_H
The list of messages used by the network subsystem.
NetMessageType
Definition: NetMessages.h:42
Special message type for updated to game startup settings.
Definition: NetMessage.h:142
virtual u8 * Serialize(u8 *pBuffer) const
Serialize the message into the specified buffer parameter.
Definition: NetMessageSim.cpp:206
virtual size_t GetSerializedLength() const
Retrieves the size in bytes of the serialized message.
Definition: NetMessageSim.cpp:225
NONCOPYABLE(CGameSetupMessage)
virtual const u8 * Deserialize(const u8 *pStart, const u8 *pEnd)
Deserializes the message from the specified buffer.
Definition: NetMessageSim.cpp:215
virtual CStr ToString() const
Returns a string representation for the message.
Definition: NetMessageSim.cpp:232
const ScriptInterface & m_ScriptInterface
Definition: NetMessage.h:154
CGameSetupMessage(const ScriptInterface &scriptInterface)
Definition: NetMessageSim.cpp:192
JS::PersistentRootedValue m_Data
Definition: NetMessage.h:152
Creates messages from data received through the network.
Definition: NetMessage.h:95
static CNetMessage * CreateMessage(const void *pData, size_t dataSize, const ScriptInterface &scriptInterface)
Factory method which creates a message object based on the given data.
Definition: NetMessage.cpp:94
The base class for all network messages exchanged within the game.
Definition: NetMessage.h:33
NetMessageType GetType() const
Retrieves the message type.
Definition: NetMessage.h:46
virtual ~CNetMessage()
Definition: NetMessage.cpp:40
virtual u8 * Serialize(u8 *pBuffer) const
Serialize the message into the specified buffer parameter.
Definition: NetMessage.cpp:44
friend class CNetSession
Definition: NetMessage.h:34
virtual size_t GetSerializedLength() const
Retrieves the size in bytes of the serialized message.
Definition: NetMessage.cpp:78
virtual const u8 * Deserialize(const u8 *pStart, const u8 *pEnd)
Deserializes the message from the specified buffer.
Definition: NetMessage.cpp:53
virtual CStr ToString() const
Returns a string representation for the message.
Definition: NetMessage.cpp:84
NetMessageType m_Type
Definition: NetMessage.h:88
CNetMessage()
Definition: NetMessage.cpp:30
Special message type for simulation commands.
Definition: NetMessage.h:114
JS::PersistentRooted< JS::Value > m_Data
Definition: NetMessage.h:133
virtual CStr ToString() const
Returns a string representation for the message.
Definition: NetMessageSim.cpp:182
u32 m_Client
Definition: NetMessage.h:130
const ScriptInterface & m_ScriptInterface
Definition: NetMessage.h:135
u32 m_Turn
Definition: NetMessage.h:132
CSimulationMessage(const ScriptInterface &scriptInterface)
Definition: NetMessageSim.cpp:113
virtual size_t GetSerializedLength() const
Retrieves the size in bytes of the serialized message.
Definition: NetMessageSim.cpp:167
virtual const u8 * Deserialize(const u8 *pStart, const u8 *pEnd)
Deserializes the message from the specified buffer.
Definition: NetMessageSim.cpp:153
i32 m_Player
Definition: NetMessage.h:131
virtual u8 * Serialize(u8 *pBuffer) const
Serialize the message into the specified buffer parameter.
Definition: NetMessageSim.cpp:139
An interface for serializable objects.
Definition: Serialization.h:76
Abstraction around a SpiderMonkey JS::Realm.
Definition: ScriptInterface.h:72
int32_t i32
Definition: types.h:34
uint8_t u8
Definition: types.h:37
uint32_t u32
Definition: types.h:39