Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
NetServerSession.h
Go to the documentation of this file.
1/* Copyright (C) 2026 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 NET_SERVER_SESSION_H
19#define NET_SERVER_SESSION_H
20
21#include "network/FSM.h"
23#include "network/NetHost.h"
24#include "ps/CStr.h"
25
27
28typedef struct _ENetPeer ENetPeer;
29
30/**
31 * The server's end of a network session.
32 * Represents an abstraction of the state of the client, storing all the per-client data
33 * needed by the server.
34 *
35 * Thread-safety:
36 * - This is constructed and used by CNetServerWorker in the network server thread.
37 */
38class CNetServerSession : public CFsm<CNetServerSession, CNetMessage*>
39{
41
42public:
44
46
47 const CStr& GetGUID() const { return m_GUID; }
48 void SetGUID(const CStr& guid) { m_GUID = guid; }
49
50 const CStrW& GetUserName() const { return m_UserName; }
51 void SetUserName(const CStrW& name) { m_UserName = name; }
52
53 std::uint32_t GetHostID() const { return m_HostID; }
54 void SetHostID(std::uint32_t id) { m_HostID = id; }
55
56 std::uint32_t GetIPAddress() const;
57
58 /**
59 * Number of milliseconds since the latest packet of that client was received.
60 */
61 std::uint32_t GetLastReceivedTime() const;
62
63 /**
64 * Average round trip time to the client.
65 */
66 std::uint32_t GetMeanRTT() const;
67
68 /**
69 * Sends a disconnection notification to the client,
70 * and sends a NMT_CONNECTION_LOST message to the session FSM.
71 * The server will receive a disconnection notification after a while.
72 * The server will not receive any further messages sent via this session.
73 */
75
76 /**
77 * Sends an unreliable disconnection notification to the client.
78 * The server will not receive any disconnection notification.
79 * The server will not receive any further messages sent via this session.
80 */
82
83 /**
84 * Send a message to the client.
85 */
86 bool SendMessage(const CNetMessage* message);
87
89
90private:
92
94
96
97 CStr m_GUID;
99 std::uint32_t m_HostID{0};
101};
102
103#endif // NET_SERVER_SESSION_H
Various declarations shared by networking code.
struct _ENetPeer ENetPeer
Definition: NetHost.h:33
NetDisconnectReason
Reasons sent by server to clients in disconnection messages.
Definition: NetHost.h:65
Manages states, events, actions and transitions between states.
Definition: FSM.h:69
Handles transferring files between clients and servers.
Definition: NetFileTransfer.h:53
The base class for all network messages exchanged within the game.
Definition: NetMessage.h:42
The server's end of a network session.
Definition: NetServerSession.h:39
std::uint32_t GetIPAddress() const
Definition: NetServerSession.cpp:33
std::uint32_t GetMeanRTT() const
Average round trip time to the client.
Definition: NetServerSession.cpp:46
void DisconnectNow(NetDisconnectReason reason)
Sends an unreliable disconnection notification to the client.
Definition: NetServerSession.cpp:64
CNetServerSession(CNetServerWorker &server, ENetPeer *peer)
Definition: NetServerSession.cpp:28
std::uint32_t GetHostID() const
Definition: NetServerSession.h:53
CStr m_GUID
Definition: NetServerSession.h:97
void SetHostID(std::uint32_t id)
Definition: NetServerSession.h:54
void Disconnect(NetDisconnectReason reason)
Sends a disconnection notification to the client, and sends a NMT_CONNECTION_LOST message to the sess...
Definition: NetServerSession.cpp:54
std::uint32_t m_HostID
Definition: NetServerSession.h:99
CStrW m_UserName
Definition: NetServerSession.h:98
bool SendMessage(const CNetMessage *message)
Send a message to the client.
Definition: NetServerSession.cpp:72
std::uint32_t GetLastReceivedTime() const
Number of milliseconds since the latest packet of that client was received.
Definition: NetServerSession.cpp:38
ENetPeer * m_Peer
Definition: NetServerSession.h:95
const CStrW & GetUserName() const
Definition: NetServerSession.h:50
CStr m_Password
Definition: NetServerSession.h:100
CNetFileTransferer m_FileTransferer
Definition: NetServerSession.h:93
CNetFileTransferer & GetFileTransferer()
Definition: NetServerSession.h:88
void SetUserName(const CStrW &name)
Definition: NetServerSession.h:51
const CStr & GetGUID() const
Definition: NetServerSession.h:47
void SetGUID(const CStr &guid)
Definition: NetServerSession.h:48
CNetServerWorker & GetServer()
Definition: NetServerSession.h:45
NONCOPYABLE(CNetServerSession)
CNetServerWorker & m_Server
Definition: NetServerSession.h:91
Network server worker thread.
Definition: NetServer.h:113