Pyrogenesis  trunk
NetSession.h
Go to the documentation of this file.
1 /* Copyright (C) 2016 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 "network/fsm.h"
23 #include "network/NetHost.h"
24 #include "ps/CStr.h"
26 
27 /**
28  * Report the peer if we didn't receive a packet after this time (milliseconds).
29  */
30 extern const u32 NETWORK_WARNING_TIMEOUT;
31 
32 /**
33  * Maximum timeout of the local client of the host (milliseconds).
34  */
35 extern const u32 MAXIMUM_HOST_TIMEOUT;
36 
37 class CNetClient;
38 class CNetServerWorker;
39 
40 class CNetStatsTable;
41 
42 /**
43  * @file
44  * Network client/server sessions.
45  *
46  * Each session has two classes: CNetClientSession runs on the client,
47  * and CNetServerSession runs on the server.
48  * A client runs one session at once; a server typically runs many.
49  */
50 
51 /**
52  * Interface for sessions to which messages can be sent.
53  */
55 {
56 public:
57  virtual ~INetSession() {}
58  virtual bool SendMessage(const CNetMessage* message) = 0;
59 };
60 
61 /**
62  * The client end of a network session.
63  * Provides an abstraction of the network interface, allowing communication with the server.
64  */
66 {
68 
69 public:
72 
73  bool Connect(const CStr& server, const u16 port, const bool isLocalClient);
74 
75  /**
76  * Process queued incoming messages.
77  */
78  void Poll();
79 
80  /**
81  * Flush queued outgoing network messages.
82  */
83  void Flush();
84 
85  /**
86  * Disconnect from the server.
87  * Sends a disconnection notification to the server.
88  */
89  void Disconnect(u32 reason);
90 
91  /**
92  * Send a message to the server.
93  */
94  virtual bool SendMessage(const CNetMessage* message);
95 
96  /**
97  * Number of milliseconds since the most recent packet of the server was received.
98  */
99  u32 GetLastReceivedTime() const;
100 
101  /**
102  * Average round trip time to the server.
103  */
104  u32 GetMeanRTT() const;
105 
106  CNetFileTransferer& GetFileTransferer() { return m_FileTransferer; }
107 
108 private:
110 
112 
116 };
117 
118 
119 /**
120  * The server's end of a network session.
121  * Represents an abstraction of the state of the client, storing all the per-client data
122  * needed by the server.
123  *
124  * Thread-safety:
125  * - This is constructed and used by CNetServerWorker in the network server thread.
126  */
127 class CNetServerSession : public CFsm, public INetSession
128 {
130 
131 public:
133 
134  CNetServerWorker& GetServer() { return m_Server; }
135 
136  const CStr& GetGUID() const { return m_GUID; }
137  void SetGUID(const CStr& guid) { m_GUID = guid; }
138 
139  const CStrW& GetUserName() const { return m_UserName; }
140  void SetUserName(const CStrW& name) { m_UserName = name; }
141 
142  u32 GetHostID() const { return m_HostID; }
143  void SetHostID(u32 id) { m_HostID = id; }
144 
145  u32 GetIPAddress() const;
146 
147  /**
148  * Whether this client is running in the same process as the server.
149  */
150  bool IsLocalClient() const;
151 
152  /**
153  * Number of milliseconds since the latest packet of that client was received.
154  */
155  u32 GetLastReceivedTime() const;
156 
157  /**
158  * Average round trip time to the client.
159  */
160  u32 GetMeanRTT() const;
161 
162  /**
163  * Sends a disconnection notification to the client,
164  * and sends a NMT_CONNECTION_LOST message to the session FSM.
165  * The server will receive a disconnection notification after a while.
166  * The server will not receive any further messages sent via this session.
167  */
168  void Disconnect(u32 reason);
169 
170  /**
171  * Sends an unreliable disconnection notification to the client.
172  * The server will not receive any disconnection notification.
173  * The server will not receive any further messages sent via this session.
174  */
175  void DisconnectNow(u32 reason);
176 
177  /**
178  * Prevent timeouts for the client running in the same process as the server.
179  */
180  void SetLocalClient(bool isLocalClient);
181 
182  /**
183  * Send a message to the client.
184  */
185  virtual bool SendMessage(const CNetMessage* message);
186 
187  CNetFileTransferer& GetFileTransferer() { return m_FileTransferer; }
188 
189 private:
191 
193 
195 
196  CStr m_GUID;
197  CStrW m_UserName;
199 
201 };
202 
203 #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:217
Manages states, events, actions and transitions between states.
Definition: fsm.h:117
CNetFileTransferer m_FileTransferer
Definition: NetSession.h:111
CStr m_GUID
Definition: NetSession.h:196
CNetFileTransferer & GetFileTransferer()
Definition: NetSession.h:187
uint16_t u16
Definition: types.h:38
u32 GetHostID() const
Definition: NetSession.h:142
CNetServerWorker & GetServer()
Definition: NetSession.h:134
void SetHostID(u32 id)
Definition: NetSession.h:143
Interface for sessions to which messages can be sent.
Definition: NetSession.h:54
void SetUserName(const CStrW &name)
Definition: NetSession.h:140
struct _ENetPeer ENetPeer
Definition: NetHost.h:30
CNetFileTransferer m_FileTransferer
Definition: NetSession.h:192
CStrW m_UserName
Definition: NetSession.h:197
const u32 MAXIMUM_HOST_TIMEOUT
Maximum timeout of the local client of the host (milliseconds).
Definition: NetSession.cpp:31
const u32 NETWORK_WARNING_TIMEOUT
Report the peer if we didn&#39;t receive a packet after this time (milliseconds).
Definition: NetSession.cpp:29
CNetServerWorker & m_Server
Definition: NetSession.h:190
ENetHost * m_Host
Definition: NetSession.h:113
uint32_t u32
Definition: types.h:39
Various declarations shared by networking code.
ENetPeer * m_Peer
Definition: NetSession.h:194
virtual bool SendMessage(const CNetMessage *message)=0
u32 m_HostID
Definition: NetSession.h:198
ENet connection statistics profiler table.
Definition: NetStats.h:36
The base class for all network messages exchanged within the game.
Definition: NetMessage.h:32
CNetFileTransferer & GetFileTransferer()
Definition: NetSession.h:106
Handles transferring files between clients and servers.
Definition: NetFileTransfer.h:73
const CStrW & GetUserName() const
Definition: NetSession.h:139
const CStr & GetGUID() const
Definition: NetSession.h:136
Network client.
Definition: NetClient.h:57
bool m_IsLocalClient
Definition: NetSession.h:200
struct _ENetHost ENetHost
Definition: NetHost.h:32
CNetStatsTable * m_Stats
Definition: NetSession.h:115
ENetPeer * m_Server
Definition: NetSession.h:114
virtual ~INetSession()
Definition: NetSession.h:57
CNetClient & m_Client
Definition: NetSession.h:109
The client end of a network session.
Definition: NetSession.h:65
Network server worker thread.
Definition: NetServer.h:153
The server&#39;s end of a network session.
Definition: NetSession.h:127
void SetGUID(const CStr &guid)
Definition: NetSession.h:137