LCOV - code coverage report
Current view: top level - source/network - NetHost.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 24 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 4 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             : #include "precompiled.h"
      19             : 
      20             : #include "NetHost.h"
      21             : 
      22             : #include "lib/debug.h"
      23             : #include "lib/external_libraries/enet.h"
      24             : #include "network/NetMessage.h"
      25             : #include "ps/CLogger.h"
      26             : 
      27             : #include <cstddef>
      28             : #include <vector>
      29             : 
      30           0 : bool CNetHost::SendMessage(const CNetMessage* message, ENetPeer* peer, const char* peerName)
      31             : {
      32           0 :     ENetPacket* packet = CreatePacket(message);
      33           0 :     if (!packet)
      34             :         return false;
      35             : 
      36           0 :     LOGMESSAGE("Net: Sending message {} of size {} to {}", message->ToString(), packet->dataLength,
      37             :         peerName);
      38             : 
      39             :     // Let ENet send the message to peer
      40           0 :     if (enet_peer_send(peer, DEFAULT_CHANNEL, packet) < 0)
      41             :     {
      42           0 :         LOGERROR("Net: Failed to send packet to peer");
      43           0 :         return false;
      44             :     }
      45             : 
      46             :     // Don't call enet_host_flush now - let it queue up all the packets
      47             :     // and send them during the next frame
      48             :     //
      49             :     // TODO: we should flush explicitly at some appropriate point before the next frame
      50             : 
      51             :     return true;
      52             : }
      53             : 
      54           0 : ENetPacket* CNetHost::CreatePacket(const CNetMessage* message)
      55             : {
      56           0 :     size_t size = message->GetSerializedLength();
      57             : 
      58           0 :     ENSURE(size); // else we'll fail when accessing the 0th element
      59             : 
      60             :     // Adjust buffer for message
      61           0 :     std::vector<std::uint8_t> buffer;
      62           0 :     buffer.resize(size);
      63             : 
      64             :     // Save message to internal buffer
      65           0 :     message->Serialize(&buffer[0]);
      66             : 
      67             :     // Create a reliable packet
      68           0 :     ENetPacket* packet = enet_packet_create(&buffer[0], size, ENET_PACKET_FLAG_RELIABLE);
      69           0 :     if (!packet)
      70           0 :         LOGERROR("Net: Failed to construct packet");
      71             : 
      72           0 :     return packet;
      73             : }
      74             : 
      75           0 : void CNetHost::Initialize()
      76             : {
      77           0 :     int ret = enet_initialize();
      78           0 :     ENSURE(ret == 0);
      79           0 : }
      80             : 
      81           0 : void CNetHost::Deinitialize()
      82             : {
      83           0 :     enet_deinitialize();
      84           0 : }

Generated by: LCOV version 1.16