Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NetMessageSim.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2011 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 #include "precompiled.h"
19 
20 #include "NetMessage.h"
21 
22 #include "lib/utf8.h"
26 
27 #include <sstream>
28 
30 {
31 public:
33  m_Buffer(buffer)
34  {
35  }
36 
37  void Put(const char* UNUSED(name), const u8* data, size_t len)
38  {
39  memcpy(m_Buffer, data, len);
40  m_Buffer += len;
41  }
42 
44 };
45 
46 /**
47  * Serializer instance that writes directly to a buffer (which must be long enough).
48  */
49 class CBufferBinarySerializer : public CBinarySerializer<CBufferBinarySerializerImpl>
50 {
51 public:
52  CBufferBinarySerializer(ScriptInterface& scriptInterface, u8* buffer) :
53  CBinarySerializer<CBufferBinarySerializerImpl>(scriptInterface, buffer)
54  {
55  }
56 
58  {
59  return m_Impl.m_Buffer;
60  }
61 };
62 
64 {
65 public:
67  m_Length(0)
68  {
69  }
70 
71  void Put(const char* UNUSED(name), const u8* UNUSED(data), size_t len)
72  {
73  m_Length += len;
74  }
75 
76  size_t m_Length;
77 };
78 
79 /**
80  * Serializer instance that simply counts how many bytes would be written.
81  */
82 class CLengthBinarySerializer : public CBinarySerializer<CLengthBinarySerializerImpl>
83 {
84 public:
87  {
88  }
89 
90  size_t GetLength()
91  {
92  return m_Impl.m_Length;
93  }
94 };
95 
97  CNetMessage(NMT_SIMULATION_COMMAND), m_ScriptInterface(&scriptInterface)
98 {
99 }
100 
101 CSimulationMessage::CSimulationMessage(ScriptInterface& scriptInterface, u32 client, i32 player, u32 turn, jsval data) :
102  CNetMessage(NMT_SIMULATION_COMMAND), m_ScriptInterface(&scriptInterface),
103  m_Client(client), m_Player(player), m_Turn(turn), m_Data(scriptInterface.GetContext(), data)
104 {
105 }
106 
108 {
109  // TODO: ought to handle serialization exceptions
110  // TODO: ought to represent common commands more efficiently
111 
112  u8* pos = CNetMessage::Serialize(pBuffer);
113  CBufferBinarySerializer serializer(*m_ScriptInterface, pos);
114  serializer.NumberU32_Unbounded("client", m_Client);
115  serializer.NumberI32_Unbounded("player", m_Player);
116  serializer.NumberU32_Unbounded("turn", m_Turn);
117  serializer.ScriptVal("command", m_Data);
118  return serializer.GetBuffer();
119 }
120 
121 const u8* CSimulationMessage::Deserialize(const u8* pStart, const u8* pEnd)
122 {
123  // TODO: ought to handle serialization exceptions
124  // TODO: ought to represent common commands more efficiently
125 
126  const u8* pos = CNetMessage::Deserialize(pStart, pEnd);
127  std::istringstream stream(std::string(pos, pEnd));
128  CStdDeserializer deserializer(*m_ScriptInterface, stream);
129  deserializer.NumberU32_Unbounded("client", m_Client);
130  deserializer.NumberI32_Unbounded("player", m_Player);
131  deserializer.NumberU32_Unbounded("turn", m_Turn);
132  deserializer.ScriptVal("command", m_Data);
133  return pEnd;
134 }
135 
137 {
138  // TODO: serializing twice is stupidly inefficient - we should just
139  // do it once, store the result, and use it here and in Serialize
140 
142  serializer.NumberU32_Unbounded("client", m_Client);
143  serializer.NumberI32_Unbounded("player", m_Player);
144  serializer.NumberU32_Unbounded("turn", m_Turn);
145  serializer.ScriptVal("command", m_Data);
146  return CNetMessage::GetSerializedLength() + serializer.GetLength();
147 }
148 
150 {
151  std::string source = utf8_from_wstring(m_ScriptInterface->ToString(m_Data.get()));
152 
153  std::stringstream stream;
154  stream << "CSimulationMessage { m_Client: " << m_Client << ", m_Player: " << m_Player << ", m_Turn: " << m_Turn << ", m_Data: " << source << " }";
155  return CStr(stream.str());
156 }
157 
158 
160  CNetMessage(NMT_GAME_SETUP), m_ScriptInterface(scriptInterface)
161 {
162 }
163 
165  CNetMessage(NMT_GAME_SETUP), m_ScriptInterface(scriptInterface),
166  m_Data(scriptInterface.GetContext(), data)
167 {
168 }
169 
171 {
172  // TODO: ought to handle serialization exceptions
173 
174  u8* pos = CNetMessage::Serialize(pBuffer);
176  serializer.ScriptVal("command", m_Data);
177  return serializer.GetBuffer();
178 }
179 
180 const u8* CGameSetupMessage::Deserialize(const u8* pStart, const u8* pEnd)
181 {
182  // TODO: ought to handle serialization exceptions
183 
184  const u8* pos = CNetMessage::Deserialize(pStart, pEnd);
185  std::istringstream stream(std::string(pos, pEnd));
186  CStdDeserializer deserializer(m_ScriptInterface, stream);
187  deserializer.ScriptVal("command", m_Data);
188  return pEnd;
189 }
190 
192 {
194  serializer.ScriptVal("command", m_Data);
195  return CNetMessage::GetSerializedLength() + serializer.GetLength();
196 }
197 
199 {
200  std::string source = utf8_from_wstring(m_ScriptInterface.ToString(m_Data.get()));
201 
202  std::stringstream stream;
203  stream << "CGameSetupMessage { m_Data: " << source << " }";
204  return CStr(stream.str());
205 }
#define u8
Definition: types.h:39
virtual CStr ToString() const
Returns a string representation for the message.
CBufferBinarySerializer(ScriptInterface &scriptInterface, u8 *buffer)
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
virtual CStr ToString() const
Returns a string representation for the message.
virtual size_t GetSerializedLength() const
Retrieves the size in bytes of the serialized message.
void Put(const char *name, const u8 *data, size_t len)
virtual const u8 * Deserialize(const u8 *pStart, const u8 *pEnd)
Deserializes the message from the specified buffer.
std::string utf8_from_wstring(const std::wstring &src, Status *err)
opposite of wstring_from_utf8
Definition: utf8.cpp:208
virtual u8 * Serialize(u8 *pBuffer) const
Serialize the message into the specified buffer parameter.
#define i32
Definition: types.h:36
virtual void NumberU32_Unbounded(const char *name, uint32_t &out)
CLengthBinarySerializer(ScriptInterface &scriptInterface)
void ScriptVal(const char *name, jsval value)
Serialize a jsval.
Definition: ISerializer.cpp:95
virtual void ScriptVal(const char *name, jsval &out)
Deserialize a jsval, replacing &#39;out&#39;.
ScriptInterface * m_ScriptInterface
Definition: NetMessage.h:128
CGameSetupMessage(ScriptInterface &scriptInterface)
virtual u8 * Serialize(u8 *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.
CSimulationMessage(ScriptInterface &scriptInterface)
virtual const u8 * Deserialize(const u8 *pStart, const u8 *pEnd)
Deserializes the message from the specified buffer.
CBufferBinarySerializerImpl(u8 *buffer)
void NumberU32_Unbounded(const char *name, uint32_t value)
Serialize a number.
Definition: ISerializer.h:171
The base class for all network messages exchanged within the game.
Definition: NetMessage.h:32
CScriptValRooted m_Data
Definition: NetMessage.h:126
ScriptInterface & m_ScriptInterface
Definition: NetMessage.h:147
#define u32
Definition: types.h:41
virtual size_t GetSerializedLength() const
Retrieves the size in bytes of the serialized message.
Definition: NetMessage.cpp:78
jsval get() const
Returns the current value (or JSVAL_VOID if uninitialised).
Definition: ScriptVal.cpp:45
Abstraction around a SpiderMonkey JSContext.
void NumberI32_Unbounded(const char *name, int32_t value)
Serialize a number.
Definition: ISerializer.h:176
Serializer instance that writes directly to a buffer (which must be long enough). ...
virtual u8 * Serialize(u8 *pBuffer) const
Serialize the message into the specified buffer parameter.
std::wstring ToString(jsval obj, bool pretty=false)
Serialize to a binary stream.
CScriptValRooted m_Data
Definition: NetMessage.h:145
virtual const u8 * Deserialize(const u8 *pStart, const u8 *pEnd)
Deserializes the message from the specified buffer.
Definition: NetMessage.cpp:53
void Put(const char *name, const u8 *data, size_t len)
virtual void NumberI32_Unbounded(const char *name, int32_t &out)
Serializer instance that simply counts how many bytes would be written.