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: 2021-09-24 14:46:47 Functions: 0 4 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2010 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/external_libraries/enet.h"
      23             : #include "network/NetMessage.h"
      24             : #include "ps/CLogger.h"
      25             : 
      26           0 : bool CNetHost::SendMessage(const CNetMessage* message, ENetPeer* peer, const char* peerName)
      27             : {
      28           0 :     ENetPacket* packet = CreatePacket(message);
      29           0 :     if (!packet)
      30             :         return false;
      31             : 
      32           0 :     LOGMESSAGE("Net: Sending message %s of size %lu to %s", message->ToString().c_str(), (unsigned long)packet->dataLength, peerName);
      33             : 
      34             :     // Let ENet send the message to peer
      35           0 :     if (enet_peer_send(peer, DEFAULT_CHANNEL, packet) < 0)
      36             :     {
      37           0 :         LOGERROR("Net: Failed to send packet to peer");
      38           0 :         return false;
      39             :     }
      40             : 
      41             :     // Don't call enet_host_flush now - let it queue up all the packets
      42             :     // and send them during the next frame
      43             :     //
      44             :     // TODO: we should flush explicitly at some appropriate point before the next frame
      45             : 
      46             :     return true;
      47             : }
      48             : 
      49           0 : ENetPacket* CNetHost::CreatePacket(const CNetMessage* message)
      50             : {
      51           0 :     size_t size = message->GetSerializedLength();
      52             : 
      53           0 :     ENSURE(size); // else we'll fail when accessing the 0th element
      54             : 
      55             :     // Adjust buffer for message
      56           0 :     std::vector<u8> buffer;
      57           0 :     buffer.resize(size);
      58             : 
      59             :     // Save message to internal buffer
      60           0 :     message->Serialize(&buffer[0]);
      61             : 
      62             :     // Create a reliable packet
      63           0 :     ENetPacket* packet = enet_packet_create(&buffer[0], size, ENET_PACKET_FLAG_RELIABLE);
      64           0 :     if (!packet)
      65           0 :         LOGERROR("Net: Failed to construct packet");
      66             : 
      67           0 :     return packet;
      68             : }
      69             : 
      70           0 : void CNetHost::Initialize()
      71             : {
      72           0 :     int ret = enet_initialize();
      73           0 :     ENSURE(ret == 0);
      74           0 : }
      75             : 
      76           0 : void CNetHost::Deinitialize()
      77             : {
      78           0 :     enet_deinitialize();
      79           0 : }

Generated by: LCOV version 1.13