2021-05-14 12:18:03 +02:00
|
|
|
/* Copyright (C) 2021 Wildfire Games.
|
2010-05-20 02:59:01 +02:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2010-05-20 18:02:20 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
#include "NetMessage.h"
|
|
|
|
|
2010-06-30 23:41:04 +02:00
|
|
|
#include "lib/utf8.h"
|
2021-05-14 12:18:03 +02:00
|
|
|
#include "scriptinterface/ScriptRequest.h"
|
|
|
|
#include "scriptinterface/JSON.h"
|
2010-05-20 02:59:01 +02:00
|
|
|
#include "simulation2/serialization/BinarySerializer.h"
|
|
|
|
#include "simulation2/serialization/StdDeserializer.h"
|
2014-02-22 15:30:29 +01:00
|
|
|
#include "simulation2/serialization/StdSerializer.h" // for DEBUG_SERIALIZER_ANNOTATE
|
2010-05-20 02:59:01 +02:00
|
|
|
|
2010-07-07 12:39:41 +02:00
|
|
|
#include <sstream>
|
|
|
|
|
2010-05-25 21:01:30 +02:00
|
|
|
class CBufferBinarySerializerImpl
|
2010-05-20 02:59:01 +02:00
|
|
|
{
|
|
|
|
public:
|
2010-05-25 21:01:30 +02:00
|
|
|
CBufferBinarySerializerImpl(u8* buffer) :
|
|
|
|
m_Buffer(buffer)
|
2010-05-20 02:59:01 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-22 15:30:29 +01:00
|
|
|
void Put(const char* name, const u8* data, size_t len)
|
2010-05-20 02:59:01 +02:00
|
|
|
{
|
2014-02-22 15:30:29 +01:00
|
|
|
#if DEBUG_SERIALIZER_ANNOTATE
|
|
|
|
std::string tag = "<";
|
|
|
|
tag.append(name);
|
|
|
|
tag.append(">");
|
|
|
|
memcpy(m_Buffer, tag.c_str(), tag.length());
|
|
|
|
m_Buffer += tag.length();
|
|
|
|
#else
|
|
|
|
UNUSED2(name);
|
|
|
|
#endif
|
2010-05-20 02:59:01 +02:00
|
|
|
memcpy(m_Buffer, data, len);
|
|
|
|
m_Buffer += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
u8* m_Buffer;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2010-05-25 21:01:30 +02:00
|
|
|
* Serializer instance that writes directly to a buffer (which must be long enough).
|
2010-05-20 02:59:01 +02:00
|
|
|
*/
|
2010-05-25 21:01:30 +02:00
|
|
|
class CBufferBinarySerializer : public CBinarySerializer<CBufferBinarySerializerImpl>
|
2010-05-20 02:59:01 +02:00
|
|
|
{
|
|
|
|
public:
|
2017-08-24 02:32:42 +02:00
|
|
|
CBufferBinarySerializer(const ScriptInterface& scriptInterface, u8* buffer) :
|
2010-05-25 21:01:30 +02:00
|
|
|
CBinarySerializer<CBufferBinarySerializerImpl>(scriptInterface, buffer)
|
2010-05-20 02:59:01 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-05-25 21:01:30 +02:00
|
|
|
u8* GetBuffer()
|
|
|
|
{
|
|
|
|
return m_Impl.m_Buffer;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class CLengthBinarySerializerImpl
|
|
|
|
{
|
|
|
|
public:
|
2010-05-25 22:48:06 +02:00
|
|
|
CLengthBinarySerializerImpl() :
|
|
|
|
m_Length(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-02-22 15:30:29 +01:00
|
|
|
void Put(const char* name, const u8* UNUSED(data), size_t len)
|
2010-05-20 02:59:01 +02:00
|
|
|
{
|
2014-02-22 15:30:29 +01:00
|
|
|
#if DEBUG_SERIALIZER_ANNOTATE
|
|
|
|
m_Length += 2; // '<' and '>'
|
|
|
|
m_Length += strlen(name);
|
|
|
|
#else
|
|
|
|
UNUSED2(name);
|
|
|
|
#endif
|
2010-05-20 02:59:01 +02:00
|
|
|
m_Length += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t m_Length;
|
|
|
|
};
|
|
|
|
|
2010-05-25 21:01:30 +02:00
|
|
|
/**
|
|
|
|
* Serializer instance that simply counts how many bytes would be written.
|
|
|
|
*/
|
|
|
|
class CLengthBinarySerializer : public CBinarySerializer<CLengthBinarySerializerImpl>
|
|
|
|
{
|
|
|
|
public:
|
2017-08-24 02:32:42 +02:00
|
|
|
CLengthBinarySerializer(const ScriptInterface& scriptInterface) :
|
2010-05-25 21:01:30 +02:00
|
|
|
CBinarySerializer<CLengthBinarySerializerImpl>(scriptInterface)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetLength()
|
|
|
|
{
|
|
|
|
return m_Impl.m_Length;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-24 02:32:42 +02:00
|
|
|
CSimulationMessage::CSimulationMessage(const ScriptInterface& scriptInterface) :
|
2021-05-14 12:18:03 +02:00
|
|
|
CNetMessage(NMT_SIMULATION_COMMAND), m_ScriptInterface(scriptInterface)
|
2010-05-20 02:59:01 +02:00
|
|
|
{
|
2021-05-14 12:18:03 +02:00
|
|
|
ScriptRequest rq(scriptInterface);
|
|
|
|
m_Data.init(rq.cx);
|
2010-05-20 02:59:01 +02:00
|
|
|
}
|
|
|
|
|
2017-08-24 02:32:42 +02:00
|
|
|
CSimulationMessage::CSimulationMessage(const ScriptInterface& scriptInterface, u32 client, i32 player, u32 turn, JS::HandleValue data) :
|
|
|
|
CNetMessage(NMT_SIMULATION_COMMAND), m_ScriptInterface(scriptInterface),
|
2021-05-14 12:18:03 +02:00
|
|
|
m_Client(client), m_Player(player), m_Turn(turn)
|
2010-05-20 02:59:01 +02:00
|
|
|
{
|
2021-05-14 12:18:03 +02:00
|
|
|
ScriptRequest rq(scriptInterface);
|
|
|
|
m_Data.init(rq.cx, data);
|
2010-05-20 02:59:01 +02:00
|
|
|
}
|
|
|
|
|
2015-01-24 15:46:52 +01:00
|
|
|
CSimulationMessage::CSimulationMessage(const CSimulationMessage& orig) :
|
|
|
|
m_Client(orig.m_Client),
|
|
|
|
m_Player(orig.m_Player),
|
|
|
|
m_ScriptInterface(orig.m_ScriptInterface),
|
|
|
|
m_Turn(orig.m_Turn),
|
|
|
|
CNetMessage(orig)
|
|
|
|
{
|
2021-05-14 12:18:03 +02:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
|
|
|
m_Data.init(rq.cx, orig.m_Data);
|
2015-01-24 15:46:52 +01:00
|
|
|
}
|
|
|
|
|
2010-05-20 02:59:01 +02:00
|
|
|
u8* CSimulationMessage::Serialize(u8* pBuffer) const
|
|
|
|
{
|
|
|
|
// TODO: ought to handle serialization exceptions
|
|
|
|
// TODO: ought to represent common commands more efficiently
|
|
|
|
u8* pos = CNetMessage::Serialize(pBuffer);
|
2017-08-24 02:32:42 +02:00
|
|
|
CBufferBinarySerializer serializer(m_ScriptInterface, pos);
|
2010-05-20 02:59:01 +02:00
|
|
|
serializer.NumberU32_Unbounded("client", m_Client);
|
|
|
|
serializer.NumberI32_Unbounded("player", m_Player);
|
|
|
|
serializer.NumberU32_Unbounded("turn", m_Turn);
|
2015-01-24 15:46:52 +01:00
|
|
|
|
|
|
|
serializer.ScriptVal("command", const_cast<JS::PersistentRootedValue*>(&m_Data));
|
2010-05-25 21:01:30 +02:00
|
|
|
return serializer.GetBuffer();
|
2010-05-20 02:59:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const u8* CSimulationMessage::Deserialize(const u8* pStart, const u8* pEnd)
|
|
|
|
{
|
|
|
|
// TODO: ought to handle serialization exceptions
|
|
|
|
// TODO: ought to represent common commands more efficiently
|
|
|
|
const u8* pos = CNetMessage::Deserialize(pStart, pEnd);
|
|
|
|
std::istringstream stream(std::string(pos, pEnd));
|
2017-08-24 02:32:42 +02:00
|
|
|
CStdDeserializer deserializer(m_ScriptInterface, stream);
|
2010-09-17 19:53:26 +02:00
|
|
|
deserializer.NumberU32_Unbounded("client", m_Client);
|
|
|
|
deserializer.NumberI32_Unbounded("player", m_Player);
|
|
|
|
deserializer.NumberU32_Unbounded("turn", m_Turn);
|
2015-01-24 15:46:52 +01:00
|
|
|
deserializer.ScriptVal("command", &m_Data);
|
2010-05-20 02:59:01 +02:00
|
|
|
return pEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CSimulationMessage::GetSerializedLength() const
|
|
|
|
{
|
|
|
|
// TODO: serializing twice is stupidly inefficient - we should just
|
|
|
|
// do it once, store the result, and use it here and in Serialize
|
2017-08-24 02:32:42 +02:00
|
|
|
CLengthBinarySerializer serializer(m_ScriptInterface);
|
2010-05-20 02:59:01 +02:00
|
|
|
serializer.NumberU32_Unbounded("client", m_Client);
|
|
|
|
serializer.NumberI32_Unbounded("player", m_Player);
|
|
|
|
serializer.NumberU32_Unbounded("turn", m_Turn);
|
2015-01-24 15:46:52 +01:00
|
|
|
|
|
|
|
// TODO: The cast can probably be removed if and when ScriptVal can take a JS::HandleValue instead of
|
|
|
|
// a JS::MutableHandleValue (relies on JSAPI change). Also search for other casts like this one in that case.
|
|
|
|
serializer.ScriptVal("command", const_cast<JS::PersistentRootedValue*>(&m_Data));
|
2010-05-25 21:01:30 +02:00
|
|
|
return CNetMessage::GetSerializedLength() + serializer.GetLength();
|
2010-05-20 02:59:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CStr CSimulationMessage::ToString() const
|
|
|
|
{
|
2021-05-14 12:18:03 +02:00
|
|
|
std::string source = Script::ToString(ScriptRequest(m_ScriptInterface), const_cast<JS::PersistentRootedValue*>(&m_Data));
|
2010-05-20 02:59:01 +02:00
|
|
|
|
|
|
|
std::stringstream stream;
|
|
|
|
stream << "CSimulationMessage { m_Client: " << m_Client << ", m_Player: " << m_Player << ", m_Turn: " << m_Turn << ", m_Data: " << source << " }";
|
2011-02-17 21:08:20 +01:00
|
|
|
return CStr(stream.str());
|
2010-05-20 02:59:01 +02:00
|
|
|
}
|
2010-06-30 23:41:04 +02:00
|
|
|
|
|
|
|
|
2017-08-24 02:32:42 +02:00
|
|
|
CGameSetupMessage::CGameSetupMessage(const ScriptInterface& scriptInterface) :
|
2021-05-14 12:18:03 +02:00
|
|
|
CNetMessage(NMT_GAME_SETUP), m_ScriptInterface(scriptInterface)
|
2010-06-30 23:41:04 +02:00
|
|
|
{
|
2021-05-14 12:18:03 +02:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
|
|
|
m_Data.init(rq.cx);
|
2010-06-30 23:41:04 +02:00
|
|
|
}
|
|
|
|
|
2017-08-24 02:32:42 +02:00
|
|
|
CGameSetupMessage::CGameSetupMessage(const ScriptInterface& scriptInterface, JS::HandleValue data) :
|
2021-05-14 12:18:03 +02:00
|
|
|
CNetMessage(NMT_GAME_SETUP), m_ScriptInterface(scriptInterface)
|
2010-06-30 23:41:04 +02:00
|
|
|
{
|
2021-05-14 12:18:03 +02:00
|
|
|
ScriptRequest rq(m_ScriptInterface);
|
|
|
|
m_Data.init(rq.cx, data);
|
2010-06-30 23:41:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u8* CGameSetupMessage::Serialize(u8* pBuffer) const
|
|
|
|
{
|
|
|
|
// TODO: ought to handle serialization exceptions
|
|
|
|
u8* pos = CNetMessage::Serialize(pBuffer);
|
|
|
|
CBufferBinarySerializer serializer(m_ScriptInterface, pos);
|
2015-01-24 15:46:52 +01:00
|
|
|
serializer.ScriptVal("command", const_cast<JS::PersistentRootedValue*>(&m_Data));
|
2010-06-30 23:41:04 +02:00
|
|
|
return serializer.GetBuffer();
|
|
|
|
}
|
|
|
|
|
|
|
|
const u8* CGameSetupMessage::Deserialize(const u8* pStart, const u8* pEnd)
|
|
|
|
{
|
|
|
|
// TODO: ought to handle serialization exceptions
|
|
|
|
const u8* pos = CNetMessage::Deserialize(pStart, pEnd);
|
|
|
|
std::istringstream stream(std::string(pos, pEnd));
|
|
|
|
CStdDeserializer deserializer(m_ScriptInterface, stream);
|
2015-01-24 15:46:52 +01:00
|
|
|
deserializer.ScriptVal("command", const_cast<JS::PersistentRootedValue*>(&m_Data));
|
2010-06-30 23:41:04 +02:00
|
|
|
return pEnd;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CGameSetupMessage::GetSerializedLength() const
|
|
|
|
{
|
|
|
|
CLengthBinarySerializer serializer(m_ScriptInterface);
|
2015-01-24 15:46:52 +01:00
|
|
|
serializer.ScriptVal("command", const_cast<JS::PersistentRootedValue*>(&m_Data));
|
2010-06-30 23:41:04 +02:00
|
|
|
return CNetMessage::GetSerializedLength() + serializer.GetLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
CStr CGameSetupMessage::ToString() const
|
|
|
|
{
|
2021-05-14 12:18:03 +02:00
|
|
|
std::string source = Script::ToString(ScriptRequest(m_ScriptInterface), const_cast<JS::PersistentRootedValue*>(&m_Data));
|
2010-06-30 23:41:04 +02:00
|
|
|
|
|
|
|
std::stringstream stream;
|
|
|
|
stream << "CGameSetupMessage { m_Data: " << source << " }";
|
2011-02-17 21:08:20 +01:00
|
|
|
return CStr(stream.str());
|
2010-06-30 23:41:04 +02:00
|
|
|
}
|