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 4 50.0 %
Date: 2023-01-19 00:18:29 Functions: 3 6 50.0 %

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

Generated by: LCOV version 1.13