Pyrogenesis  trunk
NetSession.h
Go to the documentation of this file.
1 /* Copyright (C) 2022 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  */
34 inline constexpr u32 NETWORK_WARNING_TIMEOUT = 2000;
35 
36 class CNetClient;
37 class CNetServerWorker;
38 
39 class CNetStatsTable;
40 
41 typedef 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 {
57 public:
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 
71 public:
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  */
91  void ProcessPolledMessages();
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 
108  CNetFileTransferer& GetFileTransferer() { return m_FileTransferer; }
109 private:
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  */
154 class CNetServerSession : public CFsm, public INetSession
155 {
157 
158 public:
160 
161  CNetServerWorker& GetServer() { return m_Server; }
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  */
197  void DisconnectNow(NetDisconnectReason reason);
198 
199  /**
200  * Send a message to the client.
201  */
202  virtual bool SendMessage(const CNetMessage* message);
203 
204  CNetFileTransferer& GetFileTransferer() { return m_FileTransferer; }
205 
206 private:
208 
210 
212 
213  CStr m_GUID;
214  CStrW m_UserName;
217 };
218 
219 #endif // NETSESSION_H
#define NONCOPYABLE(className)
Indicates that a class is noncopyable (usually due to const or reference members, or because the clas...
Definition: code_annotation.h:227
Manages states, events, actions and transitions between states.
Definition: FSM.h:168
CNetFileTransferer m_FileTransferer
Definition: NetSession.h:122
void Shutdown(int flags)
Definition: GameSetup.cpp:339
CStr m_GUID
Definition: NetSession.h:213
CNetFileTransferer & GetFileTransferer()
Definition: NetSession.h:204
struct _ENetHost ENetHost
Definition: NetClient.h:37
uint16_t u16
Definition: types.h:38
boost::lockfree::queue< ENetPacket * > m_OutgoingMessages
Definition: NetSession.h:127
CNetServerWorker & GetServer()
Definition: NetSession.h:161
constexpr u32 NETWORK_WARNING_TIMEOUT
Report the peer if we didn&#39;t receive a packet after this time (milliseconds).
Definition: NetSession.h:34
void SetHostID(u32 id)
Definition: NetSession.h:170
Interface for sessions to which messages can be sent.
Definition: NetSession.h:55
void SetUserName(const CStrW &name)
Definition: NetSession.h:167
NetDisconnectReason
Reasons sent by server to clients in disconnection messages.
Definition: NetHost.h:62
struct _ENetPeer ENetPeer
Definition: NetHost.h:30
CNetFileTransferer m_FileTransferer
Definition: NetSession.h:209
CStrW m_UserName
Definition: NetSession.h:214
CNetServerWorker & m_Server
Definition: NetSession.h:207
ENetHost * m_Host
Definition: NetSession.h:140
uint32_t u32
Definition: types.h:39
Various declarations shared by networking code.
CStr m_Password
Definition: NetSession.h:216
u32 GetHostID() const
Definition: NetSession.h:169
const CStrW & GetUserName() const
Definition: NetSession.h:166
ENetPeer * m_Peer
Definition: NetSession.h:211
virtual bool SendMessage(const CNetMessage *message)=0
u32 m_HostID
Definition: NetSession.h:215
std::atomic< bool > m_ShouldShutdown
Definition: NetSession.h:138
ENet connection statistics profiler table.
Definition: NetStats.h:38
The base class for all network messages exchanged within the game.
Definition: NetMessage.h:32
CNetFileTransferer & GetFileTransferer()
Definition: NetSession.h:108
Handles transferring files between clients and servers.
Definition: NetFileTransfer.h:75
std::atomic< u32 > m_MeanRTT
Definition: NetSession.h:134
Network client.
Definition: NetClient.h:59
std::atomic< bool > m_LoopRunning
Definition: NetSession.h:137
CNetStatsTable * m_Stats
Definition: NetSession.h:142
ENetPeer * m_Server
Definition: NetSession.h:141
const CStr & GetGUID() const
Definition: NetSession.h:163
virtual ~INetSession()
Definition: NetSession.h:58
std::atomic< u32 > m_LastReceivedTime
Definition: NetSession.h:133
boost::lockfree::queue< ENetEvent > m_IncomingMessages
Definition: NetSession.h:125
CNetClient & m_Client
Definition: NetSession.h:120
The client end of a network session.
Definition: NetSession.h:67
Network server worker thread.
Definition: NetServer.h:212
The server&#39;s end of a network session.
Definition: NetSession.h:154
void SetGUID(const CStr &guid)
Definition: NetSession.h:164