1 /* Copyright (C) 2017 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
21 #include "Serialization.h"
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
30 * The base class for all network messages exchanged within the game.
32 class CNetMessage
: public ISerializable
34 friend class CNetSession
;
39 CNetMessage(NetMessageType type
);
40 virtual ~CNetMessage();
43 * Retrieves the message type.
44 * @return Message type
46 NetMessageType
GetType() const { return m_Type
; }
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.
55 * @param pBuffer Buffer where to serialize the message
56 * @return The position in the buffer right after the
59 virtual u8
* Serialize(u8
* pBuffer
) const;
62 * Deserializes the message from the specified buffer.
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
69 virtual const u8
* Deserialize(const u8
* pStart
, const u8
* pEnd
);
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.
76 * @return The size of serialized message
78 virtual size_t GetSerializedLength() const;
81 * Returns a string representation for the message
83 * @return The message as a string
85 virtual CStr
ToString() const;
88 NetMessageType m_Type
; // Message type
92 * Creates messages from data received through the network.
94 class CNetMessageFactory
99 * Factory method which creates a message object based on the given data
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
106 static CNetMessage
* CreateMessage(const void* pData
, size_t dataSize
, const ScriptInterface
& scriptInterface
);
110 * Special message type for simulation commands.
111 * These commands are exposed as arbitrary JS objects, associated with a specific player.
113 class CSimulationMessage
: public CNetMessage
116 CSimulationMessage(const ScriptInterface
& scriptInterface
);
117 CSimulationMessage(const ScriptInterface
& scriptInterface
, u32 client
, i32 player
, u32 turn
, JS::HandleValue data
);
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!
123 CSimulationMessage(const CSimulationMessage
& orig
);
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;
133 JS::PersistentRooted
<JS::Value
> m_Data
;
135 const ScriptInterface
& m_ScriptInterface
;
139 * Special message type for updated to game startup settings.
141 class CGameSetupMessage
: public CNetMessage
143 NONCOPYABLE(CGameSetupMessage
);
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;
152 JS::PersistentRootedValue m_Data
;
154 const ScriptInterface
& m_ScriptInterface
;
157 // This time, the classes are created
158 #include "NetMessages.h"
160 #endif // NETMESSAGE_H