Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
NetSession.h
Go to the documentation of this file.
1/* Copyright (C) 2024 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
22#include "network/FSM.h"
24#include "network/NetHost.h"
25#include "ps/CStr.h"
26
27#include <boost/lockfree/queue.hpp>
28
29#include <atomic>
30
31/**
32 * Report the peer if we didn't receive a packet after this time (milliseconds).
33 */
34inline constexpr u32 NETWORK_WARNING_TIMEOUT = 2000;
35
36class CNetClient;
38
39class CNetStatsTable;
40
41typedef struct _ENetHost ENetHost;
42
43/**
44 * @file
45 * Network client/server sessions.
46 *
47 * Each session has two classes: CNetClientSession runs on the client,
48 * and CNetServerSession runs on the server.
49 * A client runs one session at once; a server typically runs many.
50 */
51
52/**
53 * Interface for sessions to which messages can be sent.
54 */
56{
57public:
58 virtual ~INetSession() {}
59 virtual bool SendMessage(const CNetMessage* message) = 0;
60};
61
62/**
63 * The client end of a network session.
64 * Provides an abstraction of the network interface, allowing communication with the server.
65 * The NetClientSession is threaded, so all calls to the public interface must be thread-safe.
66 */
68{
70
71public:
74
75 bool Connect(const CStr& server, const u16 port, ENetHost* enetClient);
76
77 /**
78 * The client NetSession is threaded to avoid getting timeouts if the main thread hangs.
79 * Call Connect() before starting this loop.
80 */
81 static void RunNetLoop(CNetClientSession* session);
82
83 /**
84 * Shut down the net session.
85 */
86 void Shutdown();
87
88 /**
89 * Processes pending messages.
90 */
92
93 /**
94 * Queue up a message to send to the server on the next Loop() call.
95 */
96 virtual bool SendMessage(const CNetMessage* message) override;
97
98 /**
99 * Number of milliseconds since the most recent packet of the server was received.
100 */
101 u32 GetLastReceivedTime() const;
102
103 /**
104 * Average round trip time to the server.
105 */
106 u32 GetMeanRTT() const;
107
109private:
110 /**
111 * Process queued incoming messages.
112 */
113 void Poll();
114
115 /**
116 * Flush queued outgoing network messages.
117 */
118 void Flush();
119
121
123
124 // Net messages received and waiting for fetching.
125 boost::lockfree::queue<ENetEvent> m_IncomingMessages;
126 // Net messages to send on the next flush() call.
127 boost::lockfree::queue<ENetPacket*> m_OutgoingMessages;
128
129 // Last know state. If false, flushing errors are silenced.
130 bool m_Connected = false;
131
132 // Wrapper around enet stats - those are atomic as the code is lock-free.
133 std::atomic<u32> m_LastReceivedTime;
134 std::atomic<u32> m_MeanRTT;
135
136 // If this is true, calling Connect() or deleting the session is an error.
137 std::atomic<bool> m_LoopRunning;
138 std::atomic<bool> m_ShouldShutdown;
139
143};
144
145
146/**
147 * The server's end of a network session.
148 * Represents an abstraction of the state of the client, storing all the per-client data
149 * needed by the server.
150 *
151 * Thread-safety:
152 * - This is constructed and used by CNetServerWorker in the network server thread.
153 */
154class CNetServerSession : public CFsm<CNetServerSession>, public INetSession
155{
157
158public:
160
162
163 const CStr& GetGUID() const { return m_GUID; }
164 void SetGUID(const CStr& guid) { m_GUID = guid; }
165
166 const CStrW& GetUserName() const { return m_UserName; }
167 void SetUserName(const CStrW& name) { m_UserName = name; }
168
169 u32 GetHostID() const { return m_HostID; }
170 void SetHostID(u32 id) { m_HostID = id; }
171
172 u32 GetIPAddress() const;
173
174 /**
175 * Number of milliseconds since the latest packet of that client was received.
176 */
177 u32 GetLastReceivedTime() const;
178
179 /**
180 * Average round trip time to the client.
181 */
182 u32 GetMeanRTT() const;
183
184 /**
185 * Sends a disconnection notification to the client,
186 * and sends a NMT_CONNECTION_LOST message to the session FSM.
187 * The server will receive a disconnection notification after a while.
188 * The server will not receive any further messages sent via this session.
189 */
190 void Disconnect(NetDisconnectReason reason);
191
192 /**
193 * Sends an unreliable disconnection notification to the client.
194 * The server will not receive any disconnection notification.
195 * The server will not receive any further messages sent via this session.
196 */
198
199 /**
200 * Send a message to the client.
201 */
202 virtual bool SendMessage(const CNetMessage* message);
203
205
206private:
208
210
212
213 CStr m_GUID;
217};
218
219#endif // NETSESSION_H
struct _ENetHost ENetHost
Definition: NetClient.h:37
Various declarations shared by networking code.
struct _ENetPeer ENetPeer
Definition: NetHost.h:30
NetDisconnectReason
Reasons sent by server to clients in disconnection messages.
Definition: NetHost.h:63
constexpr u32 NETWORK_WARNING_TIMEOUT
Report the peer if we didn't receive a packet after this time (milliseconds).
Definition: NetSession.h:34
Manages states, events, actions and transitions between states.
Definition: FSM.h:68
The client end of a network session.
Definition: NetSession.h:68
u32 GetMeanRTT() const
Average round trip time to the server.
Definition: NetSession.cpp:225
bool m_Connected
Definition: NetSession.h:130
static void RunNetLoop(CNetClientSession *session)
The client NetSession is threaded to avoid getting timeouts if the main thread hangs.
Definition: NetSession.cpp:91
NONCOPYABLE(CNetClientSession)
std::atomic< bool > m_LoopRunning
Definition: NetSession.h:137
boost::lockfree::queue< ENetPacket * > m_OutgoingMessages
Definition: NetSession.h:127
ENetPeer * m_Server
Definition: NetSession.h:141
void ProcessPolledMessages()
Processes pending messages.
Definition: NetSession.cpp:171
std::atomic< u32 > m_MeanRTT
Definition: NetSession.h:134
u32 GetLastReceivedTime() const
Number of milliseconds since the most recent packet of the server was received.
Definition: NetSession.cpp:217
CNetClient & m_Client
Definition: NetSession.h:120
virtual bool SendMessage(const CNetMessage *message) override
Queue up a message to send to the server on the next Loop() call.
Definition: NetSession.cpp:199
std::atomic< u32 > m_LastReceivedTime
Definition: NetSession.h:133
void Poll()
Process queued incoming messages.
Definition: NetSession.cpp:121
CNetFileTransferer m_FileTransferer
Definition: NetSession.h:122
bool Connect(const CStr &server, const u16 port, ENetHost *enetClient)
Definition: NetSession.cpp:58
~CNetClientSession()
Definition: NetSession.cpp:41
CNetStatsTable * m_Stats
Definition: NetSession.h:142
void Flush()
Flush queued outgoing network messages.
Definition: NetSession.cpp:155
void Shutdown()
Shut down the net session.
Definition: NetSession.cpp:116
std::atomic< bool > m_ShouldShutdown
Definition: NetSession.h:138
ENetHost * m_Host
Definition: NetSession.h:140
CNetClientSession(CNetClient &client)
Definition: NetSession.cpp:34
CNetFileTransferer & GetFileTransferer()
Definition: NetSession.h:108
boost::lockfree::queue< ENetEvent > m_IncomingMessages
Definition: NetSession.h:125
Network client.
Definition: NetClient.h:60
Handles transferring files between clients and servers.
Definition: NetFileTransfer.h:49
The base class for all network messages exchanged within the game.
Definition: NetMessage.h:33
The server's end of a network session.
Definition: NetSession.h:155
void DisconnectNow(NetDisconnectReason reason)
Sends an unreliable disconnection notification to the client.
Definition: NetSession.cpp:269
CNetServerSession(CNetServerWorker &server, ENetPeer *peer)
Definition: NetSession.cpp:233
u32 GetMeanRTT() const
Average round trip time to the client.
Definition: NetSession.cpp:251
CStr m_GUID
Definition: NetSession.h:213
void Disconnect(NetDisconnectReason reason)
Sends a disconnection notification to the client, and sends a NMT_CONNECTION_LOST message to the sess...
Definition: NetSession.cpp:259
CStrW m_UserName
Definition: NetSession.h:214
virtual bool SendMessage(const CNetMessage *message)
Send a message to the client.
Definition: NetSession.cpp:277
u32 GetIPAddress() const
Definition: NetSession.cpp:238
ENetPeer * m_Peer
Definition: NetSession.h:211
const CStrW & GetUserName() const
Definition: NetSession.h:166
CStr m_Password
Definition: NetSession.h:216
void SetHostID(u32 id)
Definition: NetSession.h:170
CNetFileTransferer m_FileTransferer
Definition: NetSession.h:209
CNetFileTransferer & GetFileTransferer()
Definition: NetSession.h:204
void SetUserName(const CStrW &name)
Definition: NetSession.h:167
const CStr & GetGUID() const
Definition: NetSession.h:163
u32 GetLastReceivedTime() const
Number of milliseconds since the latest packet of that client was received.
Definition: NetSession.cpp:243
u32 m_HostID
Definition: NetSession.h:215
void SetGUID(const CStr &guid)
Definition: NetSession.h:164
CNetServerWorker & GetServer()
Definition: NetSession.h:161
NONCOPYABLE(CNetServerSession)
CNetServerWorker & m_Server
Definition: NetSession.h:207
u32 GetHostID() const
Definition: NetSession.h:169
Network server worker thread.
Definition: NetServer.h:213
ENet connection statistics profiler table.
Definition: NetStats.h:39
Interface for sessions to which messages can be sent.
Definition: NetSession.h:56
virtual bool SendMessage(const CNetMessage *message)=0
virtual ~INetSession()
Definition: NetSession.h:58
uint16_t u16
Definition: types.h:38
uint32_t u32
Definition: types.h:39