Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
NetMessage.h
Go to the documentation of this file.
1/* Copyright (C) 2026 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 "lib/code_annotation.h"
23#include "ps/CStr.h"
24
25#include <cstddef>
26#include <js/RootingAPI.h>
27#include <js/TypeDecls.h>
28#include <js/Value.h>
29
30namespace Script { class Interface; }
31
32// We need the enum from NetMessages.h, but we can't create any classes in
33// NetMessages.h, since they in turn require CNetMessage to be defined
34#define ALLNETMSGS_DONT_CREATE_NMTS
35#include "network/NetMessages.h" // IWYU pragma: keep
36#undef ALLNETMSGS_DONT_CREATE_NMTS
37
38/**
39 * The base class for all network messages exchanged within the game.
40 */
42{
43 friend class CNetSession;
44
45public:
46
49 virtual ~CNetMessage();
50
51 /**
52 * Retrieves the message type.
53 * @return Message type
54 */
55 NetMessageType GetType() const { return m_Type; }
56
57 /**
58 * Serialize the message into the specified buffer parameter. The size
59 * required by the buffer parameter can be found by a call to
60 * GetSerializedLength method. The information contained within the message
61 * must be serialized before the message is sent. By default only the
62 * message type and its size are serialized in the buffer parameter.
63 *
64 * @param pBuffer Buffer where to serialize the message
65 * @return The position in the buffer right after the
66 * serialized message
67 */
68 virtual std::uint8_t* Serialize(std::uint8_t* pBuffer) const;
69
70 /**
71 * Deserializes the message from the specified buffer.
72 *
73 * @param pStart Message start within the serialized buffer
74 * @param pEnd Message end within the serialized buffer
75 * @return The position in the buffer right after the
76 * message or NULL if an error occurred
77 */
78 virtual const std::uint8_t* Deserialize(const std::uint8_t* pStart, const std::uint8_t* pEnd);
79
80 /**
81 * Retrieves the size in bytes of the serialized message. Before calling
82 * Serialize, the memory size for the buffer where to serialize the message
83 * object can be found by calling this method.
84 *
85 * @return The size of serialized message
86 */
87 virtual size_t GetSerializedLength() const;
88
89 /**
90 * Returns a string representation for the message
91 *
92 * @return The message as a string
93 */
94 virtual CStr ToString() const;
95
96private:
97 NetMessageType m_Type; // Message type
98};
99
100/**
101 * Creates messages from data received through the network.
102 */
104{
105public:
106
107 /**
108 * Factory method which creates a message object based on the given data
109 *
110 * @param pData Data buffer
111 * @param dataSize Size of data buffer
112 * @param scriptInterface Script instance to use when constructing scripted messages
113 * @return The new message created
114 */
115 static CNetMessage* CreateMessage(const void* pData, size_t dataSize, const Script::Interface& scriptInterface);
116};
117
118/**
119 * Special message type for simulation commands.
120 * These commands are exposed as arbitrary JS objects, associated with a specific player.
121 */
123{
124public:
125 CSimulationMessage(const Script::Interface& scriptInterface);
126 CSimulationMessage(const Script::Interface& scriptInterface, std::uint32_t client,
127 std::int32_t player, std::uint32_t turn, JS::HandleValue data);
128
129 /** The compiler can't create a copy constructor because of the PersistentRooted member,
130 * so we have to write it manually.
131 * NOTE: It doesn't clone the m_Data member and the copy will reference the same JS::Value!
132 */
134
135 virtual std::uint8_t* Serialize(std::uint8_t* pBuffer) const;
136 virtual const std::uint8_t* Deserialize(const std::uint8_t* pStart, const std::uint8_t* pEnd);
137 virtual size_t GetSerializedLength() const;
138 virtual CStr ToString() const;
139
140 std::uint32_t m_Client;
141 std::int32_t m_Player;
142 std::int32_t m_Turn;
143 JS::PersistentRooted<JS::Value> m_Data;
144private:
146};
147
148/**
149 * Special message type for updated to game startup settings.
150 */
152{
154public:
155 CGameSetupMessage(const Script::Interface& scriptInterface);
156 CGameSetupMessage(const Script::Interface& scriptInterface, JS::HandleValue data);
157 virtual std::uint8_t* Serialize(std::uint8_t* pBuffer) const;
158 virtual const std::uint8_t* Deserialize(const std::uint8_t* pStart, const std::uint8_t* pEnd);
159 virtual size_t GetSerializedLength() const;
160 virtual CStr ToString() const;
161
162 JS::PersistentRootedValue m_Data;
163private:
165};
166
167// This time, the classes are created
168#include "network/NetMessages.h" // IWYU pragma: keep
169
170#endif // NETMESSAGE_H
The list of messages used by the network subsystem.
NetMessageType
Definition: NetMessages.h:41
Special message type for updated to game startup settings.
Definition: NetMessage.h:152
virtual const std::uint8_t * Deserialize(const std::uint8_t *pStart, const std::uint8_t *pEnd)
Deserializes the message from the specified buffer.
Definition: NetMessageSim.cpp:221
virtual size_t GetSerializedLength() const
Retrieves the size in bytes of the serialized message.
Definition: NetMessageSim.cpp:231
virtual std::uint8_t * Serialize(std::uint8_t *pBuffer) const
Serialize the message into the specified buffer parameter.
Definition: NetMessageSim.cpp:212
NONCOPYABLE(CGameSetupMessage)
CGameSetupMessage(const Script::Interface &scriptInterface)
Definition: NetMessageSim.cpp:198
virtual CStr ToString() const
Returns a string representation for the message.
Definition: NetMessageSim.cpp:238
JS::PersistentRootedValue m_Data
Definition: NetMessage.h:162
const Script::Interface & m_ScriptInterface
Definition: NetMessage.h:164
Creates messages from data received through the network.
Definition: NetMessage.h:104
static CNetMessage * CreateMessage(const void *pData, size_t dataSize, const Script::Interface &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:42
NetMessageType GetType() const
Retrieves the message type.
Definition: NetMessage.h:55
virtual ~CNetMessage()
Definition: NetMessage.cpp:40
virtual const std::uint8_t * Deserialize(const std::uint8_t *pStart, const std::uint8_t *pEnd)
Deserializes the message from the specified buffer.
Definition: NetMessage.cpp:53
friend class CNetSession
Definition: NetMessage.h:43
virtual std::uint8_t * Serialize(std::uint8_t *pBuffer) const
Serialize the message into the specified buffer parameter.
Definition: NetMessage.cpp:44
virtual size_t GetSerializedLength() const
Retrieves the size in bytes of the serialized message.
Definition: NetMessage.cpp:78
virtual CStr ToString() const
Returns a string representation for the message.
Definition: NetMessage.cpp:84
NetMessageType m_Type
Definition: NetMessage.h:97
CNetMessage()
Definition: NetMessage.cpp:30
Special message type for simulation commands.
Definition: NetMessage.h:123
CSimulationMessage(const Script::Interface &scriptInterface)
Definition: NetMessageSim.cpp:118
JS::PersistentRooted< JS::Value > m_Data
Definition: NetMessage.h:143
std::uint32_t m_Client
Definition: NetMessage.h:140
std::int32_t m_Player
Definition: NetMessage.h:141
virtual CStr ToString() const
Returns a string representation for the message.
Definition: NetMessageSim.cpp:188
virtual size_t GetSerializedLength() const
Retrieves the size in bytes of the serialized message.
Definition: NetMessageSim.cpp:173
std::int32_t m_Turn
Definition: NetMessage.h:142
const Script::Interface & m_ScriptInterface
Definition: NetMessage.h:145
virtual std::uint8_t * Serialize(std::uint8_t *pBuffer) const
Serialize the message into the specified buffer parameter.
Definition: NetMessageSim.cpp:145
virtual const std::uint8_t * Deserialize(const std::uint8_t *pStart, const std::uint8_t *pEnd)
Deserializes the message from the specified buffer.
Definition: NetMessageSim.cpp:159
An interface for serializable objects.
Definition: Serialization.h:81
Definition: Interface.h:93
Abstraction around a SpiderMonkey JSContext.
Definition: DapInterface.h:32