Line data Source code
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 :
21 : #include "lib/external_libraries/enet.h"
22 : #include "network/FSM.h"
23 : #include "network/NetFileTransfer.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 : */
55 0 : class INetSession
56 : {
57 : public:
58 0 : 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 : */
67 : class CNetClientSession : public INetSession
68 : {
69 : NONCOPYABLE(CNetClientSession);
70 :
71 : public:
72 : CNetClientSession(CNetClient& client);
73 : ~CNetClientSession();
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 0 : 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 :
120 : CNetClient& m_Client;
121 :
122 : CNetFileTransferer m_FileTransferer;
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 :
140 : ENetHost* m_Host;
141 : ENetPeer* m_Server;
142 : CNetStatsTable* m_Stats;
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 0 : class CNetServerSession : public CFsm, public INetSession
155 : {
156 : NONCOPYABLE(CNetServerSession);
157 :
158 : public:
159 : CNetServerSession(CNetServerWorker& server, ENetPeer* peer);
160 :
161 0 : CNetServerWorker& GetServer() { return m_Server; }
162 :
163 0 : const CStr& GetGUID() const { return m_GUID; }
164 0 : void SetGUID(const CStr& guid) { m_GUID = guid; }
165 :
166 0 : const CStrW& GetUserName() const { return m_UserName; }
167 0 : void SetUserName(const CStrW& name) { m_UserName = name; }
168 :
169 0 : u32 GetHostID() const { return m_HostID; }
170 0 : 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 0 : CNetFileTransferer& GetFileTransferer() { return m_FileTransferer; }
205 :
206 : private:
207 : CNetServerWorker& m_Server;
208 :
209 : CNetFileTransferer m_FileTransferer;
210 :
211 : ENetPeer* m_Peer;
212 :
213 : CStr m_GUID;
214 : CStrW m_UserName;
215 : u32 m_HostID;
216 : CStr m_Password;
217 : };
218 :
219 : #endif // NETSESSION_H
|