LCOV - code coverage report
Current view: top level - source/network - NetMessage.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 2 2 100.0 %
Date: 2026-12-03 01:19:39 Functions: 0 0 -

          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             : #ifndef NETMESSAGE_H
      19             : #define NETMESSAGE_H
      20             : 
      21             : #include "lib/code_annotation.h"
      22             : #include "network/Serialization.h"
      23             : #include "ps/CStr.h"
      24             : 
      25             : #include <cstddef>
      26             : #include <js/RootingAPI.h>
      27             : #include <js/TypeDecls.h>
      28             : #include <js/Value.h>
      29             : 
      30             : namespace Script { class Interface; }
      31             : 
      32             : // We need the enum from NetMessages.h, but we can't create any classes in
      33             : // NetMessages.h, since they in turn require CNetMessage to be defined
      34             : #define ALLNETMSGS_DONT_CREATE_NMTS
      35             : #include "network/NetMessages.h" // IWYU pragma: keep
      36             : #undef ALLNETMSGS_DONT_CREATE_NMTS
      37             : 
      38             : /**
      39             :  * The base class for all network messages exchanged within the game.
      40             :  */
      41           5 : class CNetMessage : public ISerializable
      42             : {
      43             :     friend class CNetSession;
      44             : 
      45             : public:
      46             : 
      47             :     CNetMessage();
      48             :     CNetMessage(NetMessageType type);
      49             :     virtual ~CNetMessage();
      50             : 
      51             :     /**
      52             :      * Retrieves the message type.
      53             :      * @return                      Message type
      54             :      */
      55          10 :     NetMessageType GetType() const { return m_Type; }
      56             : 
      57             :     /**
      58             :      * Serialize the message into the specified buffer parameter. The size
      59             :      * required by the buffer parameter can be found by a call to
      60             :      * GetSerializedLength method. The information contained within the message
      61             :      * must be serialized before the message is sent. By default only the
      62             :      * message type and its size are serialized in the buffer parameter.
      63             :      *
      64             :      * @param pBuffer               Buffer where to serialize the message
      65             :      * @return                      The position in the buffer right after the
      66             :      *                              serialized message
      67             :      */
      68             :     virtual std::uint8_t* Serialize(std::uint8_t* pBuffer) const;
      69             : 
      70             :     /**
      71             :      * Deserializes the message from the specified buffer.
      72             :      *
      73             :      * @param pStart                Message start within the serialized buffer
      74             :      * @param pEnd                  Message end within the serialized buffer
      75             :      * @return                      The position in the buffer right after the
      76             :      *                              message or NULL if an error occurred
      77             :      */
      78             :     virtual const std::uint8_t* Deserialize(const std::uint8_t* pStart, const std::uint8_t* pEnd);
      79             : 
      80             :     /**
      81             :      * Retrieves the size in bytes of the serialized message. Before calling
      82             :      * Serialize, the memory size for the buffer where to serialize the message
      83             :      * object can be found by calling this method.
      84             :      *
      85             :      * @return                          The size of serialized message
      86             :      */
      87             :     virtual size_t GetSerializedLength() const;
      88             : 
      89             :     /**
      90             :      * Returns a string representation for the message
      91             :      *
      92             :      * @return                          The message as a string
      93             :      */
      94             :     virtual CStr ToString() const;
      95             : 
      96             : private:
      97             :     NetMessageType  m_Type;             // Message type
      98             : };
      99             : 
     100             : /**
     101             :  * Creates messages from data received through the network.
     102             :  */
     103             : class CNetMessageFactory
     104             : {
     105             : public:
     106             : 
     107             :     /**
     108             :      * Factory method which creates a message object based on the given data
     109             :      *
     110             :      * @param pData                     Data buffer
     111             :      * @param dataSize                  Size of data buffer
     112             :      * @param scriptInterface           Script instance to use when constructing scripted messages
     113             :      * @return                          The new message created
     114             :      */
     115             :     static CNetMessage* CreateMessage(const void* pData, size_t dataSize, const Script::Interface& scriptInterface);
     116             : };
     117             : 
     118             : /**
     119             :  * Special message type for simulation commands.
     120             :  * These commands are exposed as arbitrary JS objects, associated with a specific player.
     121             :  */
     122             : class CSimulationMessage : public CNetMessage
     123             : {
     124             : public:
     125             :     CSimulationMessage(const Script::Interface& scriptInterface);
     126             :     CSimulationMessage(const Script::Interface& scriptInterface, std::uint32_t client,
     127             :         std::int32_t player, std::uint32_t turn, JS::HandleValue data);
     128             : 
     129             :     /** The compiler can't create a copy constructor because of the PersistentRooted member,
     130             :      * so we have to write it manually.
     131             :      * NOTE: It doesn't clone the m_Data member and the copy will reference the same JS::Value!
     132             :      */
     133             :     CSimulationMessage(const CSimulationMessage& orig);
     134             : 
     135             :     virtual std::uint8_t* Serialize(std::uint8_t* pBuffer) const;
     136             :     virtual const std::uint8_t* Deserialize(const std::uint8_t* pStart, const std::uint8_t* pEnd);
     137             :     virtual size_t GetSerializedLength() const;
     138             :     virtual CStr ToString() const;
     139             : 
     140             :     std::uint32_t m_Client;
     141             :     std::int32_t m_Player;
     142             :     std::int32_t m_Turn;
     143             :     JS::PersistentRooted<JS::Value> m_Data;
     144             : private:
     145             :     const Script::Interface& m_ScriptInterface;
     146             : };
     147             : 
     148             : /**
     149             :  * Special message type for updated to game startup settings.
     150             :  */
     151             : class CGameSetupMessage : public CNetMessage
     152             : {
     153             :     NONCOPYABLE(CGameSetupMessage);
     154             : public:
     155             :     CGameSetupMessage(const Script::Interface& scriptInterface);
     156             :     CGameSetupMessage(const Script::Interface& scriptInterface, JS::HandleValue data);
     157             :     virtual std::uint8_t* Serialize(std::uint8_t* pBuffer) const;
     158             :     virtual const std::uint8_t* Deserialize(const std::uint8_t* pStart, const std::uint8_t* pEnd);
     159             :     virtual size_t GetSerializedLength() const;
     160             :     virtual CStr ToString() const;
     161             : 
     162             :     JS::PersistentRootedValue m_Data;
     163             : private:
     164             :     const Script::Interface& m_ScriptInterface;
     165             : };
     166             : 
     167             : // This time, the classes are created
     168             : #include "network/NetMessages.h" // IWYU pragma: keep
     169             : 
     170             : #endif // NETMESSAGE_H

Generated by: LCOV version 1.16