LCOV - code coverage report
Current view: top level - source/network - NetMessage.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 31 129 24.0 %
Date: 2021-09-24 14:46:47 Functions: 6 9 66.7 %

          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             : #include "precompiled.h"
      19             : #include "NetMessage.h"
      20             : 
      21             : #include "ps/CLogger.h"
      22             : 
      23             : #include "ps/Game.h"
      24             : #include "simulation2/Simulation2.h"
      25             : 
      26             : #undef ALLNETMSGS_DONT_CREATE_NMTS
      27             : #define ALLNETMSGS_IMPLEMENT
      28             : #include "NetMessages.h"
      29             : 
      30           1 : CNetMessage::CNetMessage()
      31             : {
      32           1 :     m_Type  = NMT_INVALID;
      33           0 : }
      34             : 
      35           2 : CNetMessage::CNetMessage(NetMessageType type)
      36             : {
      37           0 :     m_Type  = type;
      38           2 : }
      39             : 
      40           3 : CNetMessage::~CNetMessage()
      41             : {
      42           2 : }
      43           0 : 
      44             : u8* CNetMessage::Serialize(u8* pBuffer) const
      45           0 : {
      46           2 :     size_t size = GetSerializedLength();
      47             :     Serialize_int_1(pBuffer, m_Type);
      48           2 :     Serialize_int_2(pBuffer, size);
      49             : 
      50           1 :     return pBuffer;
      51             : }
      52           1 : 
      53           1 : const u8* CNetMessage::Deserialize(const u8* pStart, const u8* pEnd)
      54           1 : {
      55             :     if (pStart + 3 > pEnd)
      56           0 :     {
      57             :         LOGERROR("CNetMessage: Corrupt packet (smaller than header)");
      58             :         return NULL;
      59           2 :     }
      60             : 
      61           2 :     const u8* pBuffer = pStart;
      62             : 
      63           0 :     int type;
      64           0 :     size_t size;
      65             :     Deserialize_int_1(pBuffer, type);
      66             :     Deserialize_int_2(pBuffer, size);
      67           2 :     m_Type = (NetMessageType)type;
      68             : 
      69           2 :     if (pStart + size != pEnd)
      70           2 :     {
      71           2 :         LOGERROR("CNetMessage: Corrupt packet (incorrect size)");
      72           2 :         return NULL;
      73           2 :     }
      74             : 
      75           2 :     return pBuffer;
      76             : }
      77           0 : 
      78           0 : size_t CNetMessage::GetSerializedLength() const
      79             : {
      80             :     // By default, return header size
      81             :     return 3;
      82             : }
      83             : 
      84           2 : CStr CNetMessage::ToString() const
      85             : {
      86             :     // This is called only when the subclass doesn't override it
      87           0 : 
      88             :     if (GetType() == NMT_INVALID)
      89             :         return "MESSAGE_TYPE_NONE { Undefined Message }";
      90           0 :     else
      91             :         return "Unknown Message " + CStr::FromInt(GetType());
      92             : }
      93             : 
      94           0 : CNetMessage* CNetMessageFactory::CreateMessage(const void* pData,
      95           0 :                                                size_t dataSize,
      96             :                                                const ScriptInterface& scriptInterface)
      97           0 : {
      98             :     CNetMessage* pNewMessage = NULL;
      99             :     CNetMessage header;
     100           1 : 
     101             :     // Figure out message type
     102             :     header.Deserialize((const u8*)pData, (const u8*)pData + dataSize);
     103             : 
     104           1 :     switch (header.GetType())
     105           4 :     {
     106             :     case NMT_GAME_SETUP:
     107             :         pNewMessage = new CGameSetupMessage(scriptInterface);
     108           1 :         break;
     109             : 
     110           2 :     case NMT_PLAYER_ASSIGNMENT:
     111             :         pNewMessage = new CPlayerAssignmentMessage;
     112           0 :         break;
     113           0 : 
     114             :     case NMT_FILE_TRANSFER_REQUEST:
     115             :         pNewMessage = new CFileTransferRequestMessage;
     116           0 :         break;
     117           0 : 
     118           0 :     case NMT_FILE_TRANSFER_RESPONSE:
     119             :         pNewMessage = new CFileTransferResponseMessage;
     120           0 :         break;
     121           0 : 
     122           0 :     case NMT_FILE_TRANSFER_DATA:
     123             :         pNewMessage = new CFileTransferDataMessage;
     124           0 :         break;
     125           0 : 
     126           0 :     case NMT_FILE_TRANSFER_ACK:
     127             :         pNewMessage = new CFileTransferAckMessage;
     128           0 :         break;
     129           0 : 
     130           0 :     case NMT_JOIN_SYNC_START:
     131             :         pNewMessage = new CJoinSyncStartMessage;
     132           0 :         break;
     133           0 : 
     134           0 :     case NMT_REJOINED:
     135             :         pNewMessage = new CRejoinedMessage;
     136           0 :         break;
     137           0 : 
     138           0 :     case NMT_KICKED:
     139             :         pNewMessage = new CKickedMessage;
     140           0 :         break;
     141           0 : 
     142           0 :     case NMT_CLIENT_TIMEOUT:
     143             :         pNewMessage = new CClientTimeoutMessage;
     144           0 :         break;
     145           0 : 
     146           0 :     case NMT_CLIENT_PERFORMANCE:
     147             :         pNewMessage = new CClientPerformanceMessage;
     148           0 :         break;
     149           0 : 
     150           0 :     case NMT_CLIENTS_LOADING:
     151             :         pNewMessage = new CClientsLoadingMessage;
     152           0 :         break;
     153           0 : 
     154           0 :     case NMT_CLIENT_PAUSED:
     155             :         pNewMessage = new CClientPausedMessage;
     156           0 :         break;
     157           0 : 
     158           0 :     case NMT_LOADED_GAME:
     159             :         pNewMessage = new CLoadedGameMessage;
     160           0 :         break;
     161           0 : 
     162           0 :     case NMT_SERVER_HANDSHAKE:
     163             :         pNewMessage = new CSrvHandshakeMessage;
     164           0 :         break;
     165           0 : 
     166           0 :     case NMT_SERVER_HANDSHAKE_RESPONSE:
     167             :         pNewMessage = new CSrvHandshakeResponseMessage;
     168           0 :         break;
     169           0 : 
     170           0 :     case NMT_CLIENT_HANDSHAKE:
     171             :         pNewMessage = new CCliHandshakeMessage;
     172           0 :         break;
     173           0 : 
     174           0 :     case NMT_AUTHENTICATE:
     175             :         pNewMessage = new CAuthenticateMessage;
     176           0 :         break;
     177           0 : 
     178           0 :     case NMT_AUTHENTICATE_RESULT:
     179             :         pNewMessage = new CAuthenticateResultMessage;
     180           0 :         break;
     181           0 : 
     182           0 :     case NMT_GAME_START:
     183             :         pNewMessage = new CGameStartMessage;
     184           0 :         break;
     185           0 : 
     186           0 :     case NMT_END_COMMAND_BATCH:
     187             :         pNewMessage = new CEndCommandBatchMessage;
     188           0 :         break;
     189           0 : 
     190           0 :     case NMT_SYNC_CHECK:
     191             :         pNewMessage = new CSyncCheckMessage;
     192           0 :         break;
     193           0 : 
     194           0 :     case NMT_SYNC_ERROR:
     195             :         pNewMessage = new CSyncErrorMessage;
     196           0 :         break;
     197           0 : 
     198           0 :     case NMT_CHAT:
     199             :         pNewMessage = new CChatMessage;
     200           0 :         break;
     201           0 : 
     202           0 :     case NMT_READY:
     203             :         pNewMessage = new CReadyMessage;
     204           0 :         break;
     205           0 : 
     206           0 :     case NMT_SIMULATION_COMMAND:
     207             :         pNewMessage = new CSimulationMessage(scriptInterface);
     208           0 :         break;
     209           0 : 
     210           0 :     case NMT_CLEAR_ALL_READY:
     211             :         pNewMessage = new CClearAllReadyMessage;
     212           1 :         break;
     213           1 : 
     214             :     case NMT_ASSIGN_PLAYER:
     215             :         pNewMessage = new CAssignPlayerMessage;
     216           0 :         break;
     217           0 : 
     218           0 :     default:
     219             :         LOGERROR("CNetMessageFactory::CreateMessage(): Unknown message type '%d' received", header.GetType());
     220           0 :         break;
     221           0 :     }
     222           0 : 
     223             :     if (pNewMessage)
     224           0 :         pNewMessage->Deserialize((const u8*)pData, (const u8*)pData + dataSize);
     225           0 : 
     226           0 :     return pNewMessage;
     227             : }

Generated by: LCOV version 1.13