Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
NetClientSession.h
Go to the documentation of this file.
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"
24#include "network/NetHost.h"
25
26#include <atomic>
27#include <boost/lockfree/queue.hpp>
28
29class CNetClient;
30class CNetMessage;
31class CNetStatsTable;
32class CStr;
33
34typedef 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 */
51{
53
54public:
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 */
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
92private:
93 /**
94 * Process queued incoming messages.
95 */
96 void Poll();
97
98 /**
99 * Flush queued outgoing network messages.
100 */
101 void Flush();
102
104
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
struct _ENetHost ENetHost
Definition: NetClient.h:50
Various declarations shared by networking code.
The client end of a network session.
Definition: NetClientSession.h:51
bool SendMessage(const CNetMessage *message)
Queue up a message to send to the server on the next Loop() call.
Definition: NetClientSession.cpp:191
bool m_Connected
Definition: NetClientSession.h:113
static void RunNetLoop(CNetClientSession *session)
The client NetSession is threaded to avoid getting timeouts if the main thread hangs.
Definition: NetClientSession.cpp:78
NONCOPYABLE(CNetClientSession)
std::atomic< bool > m_LoopRunning
Definition: NetClientSession.h:123
boost::lockfree::queue< ENetPacket * > m_OutgoingMessages
Definition: NetClientSession.h:110
std::atomic< std::uint32_t > m_LastReceivedTime
Definition: NetClientSession.h:119
void ProcessPolledMessages()
Processes pending messages.
Definition: NetClientSession.cpp:159
bool m_WasConnected
Definition: NetClientSession.h:116
std::unique_ptr< ENetPeer, DestroyPeer > m_Server
Definition: NetClientSession.h:127
std::unique_ptr< ENetHost, DestroyHost > m_Host
Definition: NetClientSession.h:126
CNetClient & m_Client
Definition: NetClientSession.h:103
bool Connect(const CStr &server, const std::uint16_t port, ENetHost *enetClient)
Definition: NetClientSession.cpp:47
std::uint32_t GetMeanRTT() const
Average round trip time to the server.
Definition: NetClientSession.cpp:217
void Poll()
Process queued incoming messages.
Definition: NetClientSession.cpp:108
CNetFileTransferer m_FileTransferer
Definition: NetClientSession.h:105
~CNetClientSession()
Definition: NetClientSession.cpp:42
void Flush()
Flush queued outgoing network messages.
Definition: NetClientSession.cpp:143
std::unique_ptr< CNetStatsTable > m_Stats
Definition: NetClientSession.h:128
void Shutdown()
Shut down the net session.
Definition: NetClientSession.cpp:103
std::atomic< bool > m_ShouldShutdown
Definition: NetClientSession.h:124
CNetClientSession(CNetClient &client)
Definition: NetClientSession.cpp:37
CNetFileTransferer & GetFileTransferer()
Definition: NetClientSession.h:91
std::uint32_t GetLastReceivedTime() const
Number of milliseconds since the most recent packet of the server was received.
Definition: NetClientSession.cpp:209
boost::lockfree::queue< ENetEvent > m_IncomingMessages
Definition: NetClientSession.h:108
std::atomic< std::uint32_t > m_MeanRTT
Definition: NetClientSession.h:120
Network client.
Definition: NetClient.h:73
Handles transferring files between clients and servers.
Definition: NetFileTransfer.h:53
The base class for all network messages exchanged within the game.
Definition: NetMessage.h:42
ENet connection statistics profiler table.
Definition: NetStats.h:43