Changeset View
Changeset View
Standalone View
Standalone View
network/NetMessageSim.cpp
/* Copyright (C) 2021 Wildfire Games. | /* Copyright (C) 2022 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 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 | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
▲ Show 20 Lines • Show All 102 Lines • ▼ Show 20 Lines | |||||
CSimulationMessage::CSimulationMessage(const ScriptInterface& scriptInterface) : | CSimulationMessage::CSimulationMessage(const ScriptInterface& scriptInterface) : | ||||
CNetMessage(NMT_SIMULATION_COMMAND), m_ScriptInterface(scriptInterface) | CNetMessage(NMT_SIMULATION_COMMAND), m_ScriptInterface(scriptInterface) | ||||
{ | { | ||||
ScriptRequest rq(scriptInterface); | ScriptRequest rq(scriptInterface); | ||||
m_Data.init(rq.cx); | m_Data.init(rq.cx); | ||||
} | } | ||||
CSimulationMessage::CSimulationMessage(const ScriptInterface& scriptInterface, u32 client, i32 player, u32 turn, JS::HandleValue data) : | CSimulationMessage::CSimulationMessage(const ScriptInterface& scriptInterface, u32 clientId, i32 player, u32 turn, JS::HandleValue data) : | ||||
CNetMessage(NMT_SIMULATION_COMMAND), m_ScriptInterface(scriptInterface), | CNetMessage(NMT_SIMULATION_COMMAND), m_ScriptInterface(scriptInterface), | ||||
m_Client(client), m_Player(player), m_Turn(turn) | m_ClientId(clientId), m_Player(player), m_Turn(turn) | ||||
{ | { | ||||
ScriptRequest rq(scriptInterface); | ScriptRequest rq(scriptInterface); | ||||
m_Data.init(rq.cx, data); | m_Data.init(rq.cx, data); | ||||
} | } | ||||
CSimulationMessage::CSimulationMessage(const CSimulationMessage& orig) : | CSimulationMessage::CSimulationMessage(const CSimulationMessage& orig) : | ||||
m_Client(orig.m_Client), | m_ClientId(orig.m_ClientId), | ||||
m_Player(orig.m_Player), | m_Player(orig.m_Player), | ||||
m_ScriptInterface(orig.m_ScriptInterface), | m_ScriptInterface(orig.m_ScriptInterface), | ||||
m_Turn(orig.m_Turn), | m_Turn(orig.m_Turn), | ||||
CNetMessage(orig) | CNetMessage(orig) | ||||
{ | { | ||||
ScriptRequest rq(m_ScriptInterface); | ScriptRequest rq(m_ScriptInterface); | ||||
m_Data.init(rq.cx, orig.m_Data); | m_Data.init(rq.cx, orig.m_Data); | ||||
} | } | ||||
u8* CSimulationMessage::Serialize(u8* pBuffer) const | u8* CSimulationMessage::Serialize(u8* pBuffer) const | ||||
{ | { | ||||
// TODO: ought to handle serialization exceptions | // TODO: ought to handle serialization exceptions | ||||
// TODO: ought to represent common commands more efficiently | // TODO: ought to represent common commands more efficiently | ||||
u8* pos = CNetMessage::Serialize(pBuffer); | u8* pos = CNetMessage::Serialize(pBuffer); | ||||
CBufferBinarySerializer serializer(m_ScriptInterface, pos); | CBufferBinarySerializer serializer(m_ScriptInterface, pos); | ||||
serializer.NumberU32_Unbounded("client", m_Client); | serializer.NumberU32_Unbounded("client", m_ClientId); | ||||
serializer.NumberI32_Unbounded("player", m_Player); | serializer.NumberI32_Unbounded("player", m_Player); | ||||
serializer.NumberU32_Unbounded("turn", m_Turn); | serializer.NumberU32_Unbounded("turn", m_Turn); | ||||
serializer.ScriptVal("command", const_cast<JS::PersistentRootedValue*>(&m_Data)); | serializer.ScriptVal("command", const_cast<JS::PersistentRootedValue*>(&m_Data)); | ||||
return serializer.GetBuffer(); | return serializer.GetBuffer(); | ||||
} | } | ||||
const u8* CSimulationMessage::Deserialize(const u8* pStart, const u8* pEnd) | const u8* CSimulationMessage::Deserialize(const u8* pStart, const u8* pEnd) | ||||
{ | { | ||||
// TODO: ought to handle serialization exceptions | // TODO: ought to handle serialization exceptions | ||||
// TODO: ought to represent common commands more efficiently | // TODO: ought to represent common commands more efficiently | ||||
const u8* pos = CNetMessage::Deserialize(pStart, pEnd); | const u8* pos = CNetMessage::Deserialize(pStart, pEnd); | ||||
std::istringstream stream(std::string(pos, pEnd)); | std::istringstream stream(std::string(pos, pEnd)); | ||||
CStdDeserializer deserializer(m_ScriptInterface, stream); | CStdDeserializer deserializer(m_ScriptInterface, stream); | ||||
deserializer.NumberU32_Unbounded("client", m_Client); | deserializer.NumberU32_Unbounded("client", m_ClientId); | ||||
deserializer.NumberI32_Unbounded("player", m_Player); | deserializer.NumberI32_Unbounded("player", m_Player); | ||||
deserializer.NumberU32_Unbounded("turn", m_Turn); | deserializer.NumberU32_Unbounded("turn", m_Turn); | ||||
deserializer.ScriptVal("command", &m_Data); | deserializer.ScriptVal("command", &m_Data); | ||||
return pEnd; | return pEnd; | ||||
} | } | ||||
size_t CSimulationMessage::GetSerializedLength() const | size_t CSimulationMessage::GetSerializedLength() const | ||||
{ | { | ||||
// TODO: serializing twice is stupidly inefficient - we should just | // TODO: serializing twice is stupidly inefficient - we should just | ||||
// do it once, store the result, and use it here and in Serialize | // do it once, store the result, and use it here and in Serialize | ||||
CLengthBinarySerializer serializer(m_ScriptInterface); | CLengthBinarySerializer serializer(m_ScriptInterface); | ||||
serializer.NumberU32_Unbounded("client", m_Client); | serializer.NumberU32_Unbounded("client", m_ClientId); | ||||
serializer.NumberI32_Unbounded("player", m_Player); | serializer.NumberI32_Unbounded("player", m_Player); | ||||
serializer.NumberU32_Unbounded("turn", m_Turn); | serializer.NumberU32_Unbounded("turn", m_Turn); | ||||
// TODO: The cast can probably be removed if and when ScriptVal can take a JS::HandleValue instead of | // 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. | // 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)); | serializer.ScriptVal("command", const_cast<JS::PersistentRootedValue*>(&m_Data)); | ||||
return CNetMessage::GetSerializedLength() + serializer.GetLength(); | return CNetMessage::GetSerializedLength() + serializer.GetLength(); | ||||
} | } | ||||
CStr CSimulationMessage::ToString() const | CStr CSimulationMessage::ToString() const | ||||
{ | { | ||||
std::string source = Script::ToString(ScriptRequest(m_ScriptInterface), const_cast<JS::PersistentRootedValue*>(&m_Data)); | std::string source = Script::ToString(ScriptRequest(m_ScriptInterface), const_cast<JS::PersistentRootedValue*>(&m_Data)); | ||||
std::stringstream stream; | std::stringstream stream; | ||||
stream << "CSimulationMessage { m_Client: " << m_Client << ", m_Player: " << m_Player << ", m_Turn: " << m_Turn << ", m_Data: " << source << " }"; | stream << "CSimulationMessage { m_ClientId: " << m_ClientId << ", m_Player: " << m_Player << ", m_Turn: " << m_Turn << ", m_Data: " << source << " }"; | ||||
return CStr(stream.str()); | return CStr(stream.str()); | ||||
} | } | ||||
CGameSetupMessage::CGameSetupMessage(const ScriptInterface& scriptInterface) : | CGameSetupMessage::CGameSetupMessage(const ScriptInterface& scriptInterface) : | ||||
CNetMessage(NMT_GAME_SETUP), m_ScriptInterface(scriptInterface) | CNetMessage(NMT_GAME_SETUP), m_ScriptInterface(scriptInterface) | ||||
{ | { | ||||
ScriptRequest rq(m_ScriptInterface); | ScriptRequest rq(m_ScriptInterface); | ||||
▲ Show 20 Lines • Show All 44 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator