LCOV - code coverage report
Current view: top level - source/network - NetMessageSim.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 63 109 57.8 %
Date: 2026-12-03 01:19:39 Functions: 6 13 46.2 %

          Line data    Source code
       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             : #include "precompiled.h"
      19             : 
      20             : #include "NetMessage.h"
      21             : 
      22             : #include "lib/code_annotation.h"
      23             : #include "network/NetMessage.h"
      24             : #include "ps/CStr.h"
      25             : #include "scriptinterface/JSON.h"
      26             : #include "scriptinterface/Request.h"
      27             : #include "simulation2/serialization/BinarySerializer.h"
      28             : #include "simulation2/serialization/StdDeserializer.h"
      29             : #include "simulation2/serialization/StdSerializer.h"
      30             : 
      31             : #include <cstring>
      32             : #include <js/RootingAPI.h>
      33             : #include <js/TypeDecls.h>
      34             : #include <js/Value.h>
      35             : #include <sstream>
      36             : #include <string>
      37             : 
      38             : namespace Script { class Interface; }
      39             : 
      40             : class CBufferBinarySerializerImpl
      41             : {
      42             : public:
      43           1 :     CBufferBinarySerializerImpl(std::uint8_t* buffer) :
      44           1 :         m_Buffer(buffer)
      45             :     {
      46             :     }
      47             : 
      48          11 :     void Put([[maybe_unused]] const char* name, const std::uint8_t* data, size_t len)
      49             :     {
      50             :         #if DEBUG_SERIALIZER_ANNOTATE
      51             :             std::string tag = "<";
      52             :             tag.append(name);
      53             :             tag.append(">");
      54             :             memcpy(m_Buffer, tag.c_str(), tag.length());
      55             :             m_Buffer += tag.length();
      56             :         #endif
      57           0 :         memcpy(m_Buffer, data, len);
      58          11 :         m_Buffer += len;
      59           0 :     }
      60             : 
      61             :     std::uint8_t* m_Buffer;
      62             : };
      63             : 
      64             : /**
      65             :  * Serializer instance that writes directly to a buffer (which must be long enough).
      66             :  */
      67           2 : class CBufferBinarySerializer : public CBinarySerializer<CBufferBinarySerializerImpl>
      68             : {
      69             : public:
      70           1 :     CBufferBinarySerializer(const Script::Interface& scriptInterface, std::uint8_t* buffer) :
      71           2 :         CBinarySerializer<CBufferBinarySerializerImpl>(scriptInterface, buffer)
      72             :     {
      73             :     }
      74             : 
      75           1 :     std::uint8_t* GetBuffer()
      76             :     {
      77           1 :         return m_Impl.m_Buffer;
      78             :     }
      79             : };
      80             : 
      81             : class CLengthBinarySerializerImpl
      82             : {
      83             : public:
      84           2 :     CLengthBinarySerializerImpl() :
      85           2 :         m_Length(0)
      86             :     {
      87             :     }
      88             : 
      89          22 :     void Put([[maybe_unused]] const char* name, const std::uint8_t* /*data*/, size_t len)
      90             :     {
      91             :         #if DEBUG_SERIALIZER_ANNOTATE
      92             :         m_Length += 2;  // '<' and '>'
      93             :         m_Length += strlen(name);
      94             :         #endif
      95          22 :         m_Length += len;
      96           0 :     }
      97             : 
      98             :     size_t m_Length;
      99             : };
     100             : 
     101             : /**
     102             :  * Serializer instance that simply counts how many bytes would be written.
     103             :  */
     104           4 : class CLengthBinarySerializer : public CBinarySerializer<CLengthBinarySerializerImpl>
     105             : {
     106             : public:
     107           2 :     CLengthBinarySerializer(const Script::Interface& scriptInterface) :
     108           4 :         CBinarySerializer<CLengthBinarySerializerImpl>(scriptInterface)
     109             :     {
     110             :     }
     111             : 
     112           2 :     size_t GetLength()
     113             :     {
     114           2 :         return m_Impl.m_Length;
     115             :     }
     116             : };
     117             : 
     118           1 : CSimulationMessage::CSimulationMessage(const Script::Interface& scriptInterface) :
     119           1 :     CNetMessage(NMT_SIMULATION_COMMAND), m_ScriptInterface(scriptInterface)
     120             : {
     121           1 :     Script::Request rq(scriptInterface);
     122           1 :     m_Data.init(rq.cx);
     123           1 : }
     124             : 
     125           1 : CSimulationMessage::CSimulationMessage(const Script::Interface& scriptInterface, std::uint32_t client,
     126           1 :     std::int32_t player, std::uint32_t turn, JS::HandleValue data) :
     127           1 :     CNetMessage(NMT_SIMULATION_COMMAND), m_ScriptInterface(scriptInterface),
     128           1 :     m_Client(client), m_Player(player), m_Turn(turn)
     129             : {
     130           1 :     Script::Request rq(scriptInterface);
     131           1 :     m_Data.init(rq.cx, data);
     132           1 : }
     133             : 
     134           0 : CSimulationMessage::CSimulationMessage(const CSimulationMessage& orig) :
     135           0 :     m_Client(orig.m_Client),
     136           0 :     m_Player(orig.m_Player),
     137           0 :     m_ScriptInterface(orig.m_ScriptInterface),
     138           0 :     m_Turn(orig.m_Turn),
     139           0 :     CNetMessage(orig)
     140             : {
     141           0 :     Script::Request rq(m_ScriptInterface);
     142           0 :     m_Data.init(rq.cx, orig.m_Data);
     143           0 : }
     144             : 
     145           1 : std::uint8_t* CSimulationMessage::Serialize(std::uint8_t* pBuffer) const
     146             : {
     147             :     // TODO: ought to handle serialization exceptions
     148             :     // TODO: ought to represent common commands more efficiently
     149           1 :     std::uint8_t* pos = CNetMessage::Serialize(pBuffer);
     150           1 :     CBufferBinarySerializer serializer(m_ScriptInterface, pos);
     151           1 :     serializer.NumberU32_Unbounded("client", m_Client);
     152           1 :     serializer.NumberI32_Unbounded("player", m_Player);
     153           1 :     serializer.NumberI32_Unbounded("turn", m_Turn);
     154             : 
     155           1 :     serializer.ScriptVal("command", const_cast<JS::PersistentRootedValue*>(&m_Data));
     156           1 :     return serializer.GetBuffer();
     157           1 : }
     158             : 
     159           1 : const std::uint8_t* CSimulationMessage::Deserialize(const std::uint8_t* pStart, const std::uint8_t* pEnd)
     160             : {
     161             :     // TODO: ought to handle serialization exceptions
     162             :     // TODO: ought to represent common commands more efficiently
     163           1 :     const std::uint8_t* pos = CNetMessage::Deserialize(pStart, pEnd);
     164           1 :     std::istringstream stream(std::string(pos, pEnd));
     165           1 :     CStdDeserializer deserializer(m_ScriptInterface, stream);
     166           1 :     deserializer.NumberU32_Unbounded("client", m_Client);
     167           1 :     deserializer.NumberI32_Unbounded("player", m_Player);
     168           1 :     deserializer.NumberI32_Unbounded("turn", m_Turn);
     169           1 :     deserializer.ScriptVal("command", &m_Data);
     170           1 :     return pEnd;
     171           1 : }
     172             : 
     173           2 : size_t CSimulationMessage::GetSerializedLength() const
     174             : {
     175             :     // TODO: serializing twice is stupidly inefficient - we should just
     176             :     // do it once, store the result, and use it here and in Serialize
     177           2 :     CLengthBinarySerializer serializer(m_ScriptInterface);
     178           2 :     serializer.NumberU32_Unbounded("client", m_Client);
     179           2 :     serializer.NumberI32_Unbounded("player", m_Player);
     180           2 :     serializer.NumberI32_Unbounded("turn", m_Turn);
     181             : 
     182             :     // TODO: The cast can probably be removed if and when ScriptVal can take a JS::HandleValue instead of
     183             :     // a JS::MutableHandleValue (relies on JSAPI change). Also search for other casts like this one in that case.
     184           2 :     serializer.ScriptVal("command", const_cast<JS::PersistentRootedValue*>(&m_Data));
     185           2 :     return CNetMessage::GetSerializedLength() + serializer.GetLength();
     186           2 : }
     187             : 
     188           2 : CStr CSimulationMessage::ToString() const
     189             : {
     190           2 :     std::string source = Script::ToString(Script::Request(m_ScriptInterface), const_cast<JS::PersistentRootedValue*>(&m_Data));
     191             : 
     192           2 :     std::stringstream stream;
     193           2 :     stream << "CSimulationMessage { m_Client: " << m_Client << ", m_Player: " << m_Player << ", m_Turn: " << m_Turn << ", m_Data: " << source << " }";
     194           4 :     return CStr(stream.str());
     195           2 : }
     196             : 
     197             : 
     198           0 : CGameSetupMessage::CGameSetupMessage(const Script::Interface& scriptInterface) :
     199           0 :     CNetMessage(NMT_GAME_SETUP), m_ScriptInterface(scriptInterface)
     200             : {
     201           0 :     Script::Request rq(m_ScriptInterface);
     202           0 :     m_Data.init(rq.cx);
     203           0 : }
     204             : 
     205           0 : CGameSetupMessage::CGameSetupMessage(const Script::Interface& scriptInterface, JS::HandleValue data) :
     206           0 :     CNetMessage(NMT_GAME_SETUP), m_ScriptInterface(scriptInterface)
     207             : {
     208           0 :     Script::Request rq(m_ScriptInterface);
     209           0 :     m_Data.init(rq.cx, data);
     210           0 : }
     211             : 
     212           0 : std::uint8_t* CGameSetupMessage::Serialize(std::uint8_t* pBuffer) const
     213             : {
     214             :     // TODO: ought to handle serialization exceptions
     215           0 :     std::uint8_t* pos = CNetMessage::Serialize(pBuffer);
     216           0 :     CBufferBinarySerializer serializer(m_ScriptInterface, pos);
     217           0 :     serializer.ScriptVal("command", const_cast<JS::PersistentRootedValue*>(&m_Data));
     218           0 :     return serializer.GetBuffer();
     219           0 : }
     220             : 
     221           0 : const std::uint8_t* CGameSetupMessage::Deserialize(const std::uint8_t* pStart, const std::uint8_t* pEnd)
     222             : {
     223             :     // TODO: ought to handle serialization exceptions
     224           0 :     const std::uint8_t* pos = CNetMessage::Deserialize(pStart, pEnd);
     225           0 :     std::istringstream stream(std::string(pos, pEnd));
     226           0 :     CStdDeserializer deserializer(m_ScriptInterface, stream);
     227           0 :     deserializer.ScriptVal("command", const_cast<JS::PersistentRootedValue*>(&m_Data));
     228           0 :     return pEnd;
     229           0 : }
     230             : 
     231           0 : size_t CGameSetupMessage::GetSerializedLength() const
     232             : {
     233           0 :     CLengthBinarySerializer serializer(m_ScriptInterface);
     234           0 :     serializer.ScriptVal("command", const_cast<JS::PersistentRootedValue*>(&m_Data));
     235           0 :     return CNetMessage::GetSerializedLength() + serializer.GetLength();
     236           0 : }
     237             : 
     238           0 : CStr CGameSetupMessage::ToString() const
     239             : {
     240           0 :     std::string source = Script::ToString(Script::Request(m_ScriptInterface), const_cast<JS::PersistentRootedValue*>(&m_Data));
     241             : 
     242           0 :     std::stringstream stream;
     243           0 :     stream << "CGameSetupMessage { m_Data: " << source << " }";
     244           0 :     return CStr(stream.str());
     245           0 : }

Generated by: LCOV version 1.16