Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
NetSession.h
Go to the documentation of this file.
1 /* Copyright (C) 2011 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 class CNetClient;
28 class CNetServerWorker;
29 
30 class CNetStatsTable;
31 
32 /**
33  * @file
34  * Network client/server sessions.
35  *
36  * Each session has two classes: CNetClientSession runs on the client,
37  * and CNetServerSession runs on the server.
38  * A client runs one session at once; a server typically runs many.
39  */
40 
41 /**
42  * Interface for sessions to which messages can be sent.
43  */
45 {
46 public:
47  virtual ~INetSession() {}
48  virtual bool SendMessage(const CNetMessage* message) = 0;
49 };
50 
51 /**
52  * The client end of a network session.
53  * Provides an abstraction of the network interface, allowing communication with the server.
54  */
56 {
58 
59 public:
62 
63  bool Connect(u16 port, const CStr& server);
64 
65  /**
66  * Process queued incoming messages.
67  */
68  void Poll();
69 
70  /**
71  * Flush queued outgoing network messages.
72  */
73  void Flush();
74 
75  /**
76  * Disconnect from the server.
77  * Sends a disconnection notification to the server.
78  */
79  void Disconnect(u32 reason);
80 
81  /**
82  * Send a message to the server.
83  */
84  virtual bool SendMessage(const CNetMessage* message);
85 
87 
88 private:
90 
92 
96 };
97 
98 
99 /**
100  * The server's end of a network session.
101  * Represents an abstraction of the state of the client, storing all the per-client data
102  * needed by the server.
103  *
104  * Thread-safety:
105  * - This is constructed and used by CNetServerWorker in the network server thread.
106  */
107 class CNetServerSession : public CFsm, public INetSession
108 {
110 
111 public:
113 
115 
116  const CStr& GetGUID() const { return m_GUID; }
117  void SetGUID(const CStr& guid) { m_GUID = guid; }
118 
119  const CStrW& GetUserName() const { return m_UserName; }
120  void SetUserName(const CStrW& name) { m_UserName = name; }
121 
122  u32 GetHostID() const { return m_HostID; }
123  void SetHostID(u32 id) { m_HostID = id; }
124 
125  /**
126  * Sends a disconnection notification to the client,
127  * and sends a NMT_CONNECTION_LOST message to the session FSM.
128  * The server will receive a disconnection notification after a while.
129  * The server will not receive any further messages sent via this session.
130  */
131  void Disconnect(u32 reason);
132 
133  /**
134  * Sends an unreliable disconnection notification to the client.
135  * The server will not receive any disconnection notification.
136  * The server will not receive any further messages sent via this session.
137  */
138  void DisconnectNow(u32 reason);
139 
140  /**
141  * Send a message to the client.
142  */
143  virtual bool SendMessage(const CNetMessage* message);
144 
146 
147 private:
149 
151 
153 
154  CStr m_GUID;
155  CStrW m_UserName;
157 };
158 
159 #endif // NETSESSION_H
NONCOPYABLE(CNetServerSession)
Manages states, events, actions and transitions between states.
Definition: fsm.h:117
CNetFileTransferer m_FileTransferer
Definition: NetSession.h:91
CNetFileTransferer & GetFileTransferer()
Definition: NetSession.h:145
void DisconnectNow(u32 reason)
Sends an unreliable disconnection notification to the client.
Definition: NetSession.cpp:185
void Poll()
Process queued incoming messages.
Definition: NetSession.cpp:95
virtual bool SendMessage(const CNetMessage *message)
Send a message to the client.
Definition: NetSession.cpp:190
u32 GetHostID() const
Definition: NetSession.h:122
CNetServerWorker & GetServer()
Definition: NetSession.h:114
void SetHostID(u32 id)
Definition: NetSession.h:123
Interface for sessions to which messages can be sent.
Definition: NetSession.h:44
void Disconnect(u32 reason)
Sends a disconnection notification to the client, and sends a NMT_CONNECTION_LOST message to the sess...
Definition: NetSession.cpp:178
void SetUserName(const CStrW &name)
Definition: NetSession.h:120
struct _ENetPeer ENetPeer
Definition: NetHost.h:30
CNetFileTransferer m_FileTransferer
Definition: NetSession.h:150
CNetClientSession(CNetClient &client)
Definition: NetSession.cpp:30
CNetServerSession(CNetServerWorker &server, ENetPeer *peer)
Definition: NetSession.cpp:173
CNetServerWorker & m_Server
Definition: NetSession.h:148
ENetHost * m_Host
Definition: NetSession.h:93
Various declarations shared by networking code.
ENetPeer * m_Peer
Definition: NetSession.h:152
virtual bool SendMessage(const CNetMessage *message)
Send a message to the server.
Definition: NetSession.cpp:164
void Flush()
Flush queued outgoing network messages.
Definition: NetSession.cpp:155
virtual bool SendMessage(const CNetMessage *message)=0
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:86
Handles transferring files between clients and servers.
NONCOPYABLE(CNetClientSession)
const CStrW & GetUserName() const
Definition: NetSession.h:119
#define u16
Definition: types.h:40
const CStr & GetGUID() const
Definition: NetSession.h:116
#define u32
Definition: types.h:41
Network client.
Definition: NetClient.h:57
struct _ENetHost ENetHost
Definition: NetHost.h:32
CNetStatsTable * m_Stats
Definition: NetSession.h:95
ENetPeer * m_Server
Definition: NetSession.h:94
void Disconnect(u32 reason)
Disconnect from the server.
Definition: NetSession.cpp:81
virtual ~INetSession()
Definition: NetSession.h:47
CNetClient & m_Client
Definition: NetSession.h:89
The client end of a network session.
Definition: NetSession.h:55
Network server worker thread.
Definition: NetServer.h:160
The server&#39;s end of a network session.
Definition: NetSession.h:107
bool Connect(u16 port, const CStr &server)
Definition: NetSession.cpp:50
void SetGUID(const CStr &guid)
Definition: NetSession.h:117