LCOV - code coverage report
Current view: top level - source/network - NetClientSession.h (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 1 0.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 NETSESSION_H
      19             : #define NETSESSION_H
      20             : 
      21             : #include "lib/code_annotation.h"
      22             : #include "lib/external_libraries/enet.h"
      23             : #include "network/NetFileTransfer.h"
      24             : #include "network/NetHost.h"
      25             : 
      26             : #include <atomic>
      27             : #include <boost/lockfree/queue.hpp>
      28             : 
      29             : class CNetClient;
      30             : class CNetMessage;
      31             : class CNetStatsTable;
      32             : class CStr;
      33             : 
      34             : typedef struct _ENetHost ENetHost;
      35             : 
      36             : /**
      37             :  * @file
      38             :  * Network client/server sessions.
      39             :  *
      40             :  * Each session has two classes: CNetClientSession runs on the client,
      41             :  * and CNetServerSession runs on the server.
      42             :  * A client runs one session at once; a server typically runs many.
      43             :  */
      44             : 
      45             : /**
      46             :  * The client end of a network session.
      47             :  * Provides an abstraction of the network interface, allowing communication with the server.
      48             :  * The NetClientSession is threaded, so all calls to the public interface must be thread-safe.
      49             :  */
      50             : class CNetClientSession
      51             : {
      52             :     NONCOPYABLE(CNetClientSession);
      53             : 
      54             : public:
      55             :     CNetClientSession(CNetClient& client);
      56             :     ~CNetClientSession();
      57             : 
      58             :     bool Connect(const CStr& server, const std::uint16_t port, ENetHost* enetClient);
      59             : 
      60             :     /**
      61             :      * The client NetSession is threaded to avoid getting timeouts if the main thread hangs.
      62             :      * Call Connect() before starting this loop.
      63             :      */
      64             :     static void RunNetLoop(CNetClientSession* session);
      65             : 
      66             :     /**
      67             :      * Shut down the net session.
      68             :      */
      69             :     void Shutdown();
      70             : 
      71             :     /**
      72             :      * Processes pending messages.
      73             :      */
      74             :     void ProcessPolledMessages();
      75             : 
      76             :     /**
      77             :      * Queue up a message to send to the server on the next Loop() call.
      78             :      */
      79             :     bool SendMessage(const CNetMessage* message);
      80             : 
      81             :     /**
      82             :      * Number of milliseconds since the most recent packet of the server was received.
      83             :      */
      84             :     std::uint32_t GetLastReceivedTime() const;
      85             : 
      86             :     /**
      87             :      * Average round trip time to the server.
      88             :      */
      89             :     std::uint32_t GetMeanRTT() const;
      90             : 
      91           0 :     CNetFileTransferer& GetFileTransferer() { return m_FileTransferer; }
      92             : private:
      93             :     /**
      94             :      * Process queued incoming messages.
      95             :      */
      96             :     void Poll();
      97             : 
      98             :     /**
      99             :      * Flush queued outgoing network messages.
     100             :      */
     101             :     void Flush();
     102             : 
     103             :     CNetClient& m_Client;
     104             : 
     105             :     CNetFileTransferer m_FileTransferer;
     106             : 
     107             :     // Net messages received and waiting for fetching.
     108             :     boost::lockfree::queue<ENetEvent> m_IncomingMessages{16};
     109             :     // Net messages to send on the next flush() call.
     110             :     boost::lockfree::queue<ENetPacket*> m_OutgoingMessages{16};
     111             : 
     112             :     // Last known state. If false, flushing errors are silenced.
     113             :     bool m_Connected{false};
     114             : 
     115             :     // Whether this session was ever connected to the server.
     116             :     bool m_WasConnected{false};
     117             : 
     118             :     // Wrapper around enet stats - those are atomic as the code is lock-free.
     119             :     std::atomic<std::uint32_t> m_LastReceivedTime{0};
     120             :     std::atomic<std::uint32_t> m_MeanRTT{0};
     121             : 
     122             :     // If this is true, calling Connect() or deleting the session is an error.
     123             :     std::atomic<bool> m_LoopRunning{false};
     124             :     std::atomic<bool> m_ShouldShutdown{false};
     125             : 
     126             :     std::unique_ptr<ENetHost, DestroyHost> m_Host;
     127             :     std::unique_ptr<ENetPeer, DestroyPeer> m_Server;
     128             :     std::unique_ptr<CNetStatsTable> m_Stats;
     129             : };
     130             : 
     131             : #endif  // NETSESSION_H

Generated by: LCOV version 1.16