1
0
forked from mirrors/0ad

Split NetSession into two files

Refs: #5212
This commit is contained in:
phosit
2026-03-07 15:52:39 +01:00
parent 789d24aa1c
commit d29890efb0
10 changed files with 193 additions and 142 deletions
+2 -2
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@@ -25,12 +25,12 @@
#include "lib/status.h"
#include "lib/utf8.h"
#include "lobby/IXmppClient.h"
#include "network/NetClientSession.h"
#include "network/NetClientTurnManager.h"
#include "network/NetEnet.h"
#include "network/NetFileTransfer.h"
#include "network/NetMessage.h"
#include "network/NetProtocol.h"
#include "network/NetSession.h"
#include "network/StunClient.h"
#include "ps/CLogger.h"
#include "ps/CStr.h"
@@ -17,14 +17,13 @@
#include "precompiled.h"
#include "NetSession.h"
#include "NetClientSession.h"
#include "lib/code_generation.h"
#include "lib/debug.h"
#include "network/NetClient.h"
#include "network/NetEnet.h"
#include "network/NetMessage.h"
#include "network/NetServer.h"
#include "network/NetStats.h"
#include "ps/CLogger.h"
#include "ps/ProfileViewer.h"
@@ -236,51 +235,3 @@ u32 CNetClientSession::GetMeanRTT() const
return m_MeanRTT;
}
CNetServerSession::CNetServerSession(CNetServerWorker& server, ENetPeer* peer) :
m_Server(server), m_FileTransferer(*this), m_Peer(peer)
{
}
u32 CNetServerSession::GetIPAddress() const
{
return m_Peer->address.host;
}
u32 CNetServerSession::GetLastReceivedTime() const
{
if (!m_Peer)
return 0;
return enet_time_get() - m_Peer->lastReceiveTime;
}
u32 CNetServerSession::GetMeanRTT() const
{
if (!m_Peer)
return 0;
return m_Peer->roundTripTime;
}
void CNetServerSession::Disconnect(NetDisconnectReason reason)
{
if (reason == NDR_UNKNOWN)
LOGWARNING("Disconnecting client without communicating the disconnect reason!");
Update((uint)NMT_CONNECTION_LOST, NULL);
enet_peer_disconnect(m_Peer, static_cast<enet_uint32>(reason));
}
void CNetServerSession::DisconnectNow(NetDisconnectReason reason)
{
if (reason == NDR_UNKNOWN)
LOGWARNING("Disconnecting client without communicating the disconnect reason!");
enet_peer_disconnect_now(m_Peer, static_cast<enet_uint32>(reason));
}
bool CNetServerSession::SendMessage(const CNetMessage* message)
{
return m_Server.SendMessage(m_Peer, message);
}
@@ -21,23 +21,16 @@
#include "lib/code_annotation.h"
#include "lib/external_libraries/enet.h"
#include "lib/types.h"
#include "network/FSM.h"
#include "network/NetFileTransfer.h"
#include "network/NetHost.h"
#include "ps/CStr.h"
#include <atomic>
#include <boost/lockfree/queue.hpp>
class CNetClient;
class CNetMessage;
class CNetServerWorker;
class CNetStatsTable;
/**
* Report the peer if we didn't receive a packet after this time (milliseconds).
*/
inline constexpr u32 NETWORK_WARNING_TIMEOUT = 2000;
class CStr;
typedef struct _ENetHost ENetHost;
@@ -118,10 +111,10 @@ private:
boost::lockfree::queue<ENetPacket*> m_OutgoingMessages{16};
// Last known state. If false, flushing errors are silenced.
bool m_Connected = false;
bool m_Connected{false};
// Whether this session was ever connected to the server.
bool m_WasConnected = false;
bool m_WasConnected{false};
// Wrapper around enet stats - those are atomic as the code is lock-free.
std::atomic<u32> m_LastReceivedTime{0};
@@ -136,78 +129,4 @@ private:
CNetStatsTable* m_Stats{nullptr};
};
/**
* The server's end of a network session.
* Represents an abstraction of the state of the client, storing all the per-client data
* needed by the server.
*
* Thread-safety:
* - This is constructed and used by CNetServerWorker in the network server thread.
*/
class CNetServerSession : public CFsm<CNetServerSession, CNetMessage*>
{
NONCOPYABLE(CNetServerSession);
public:
CNetServerSession(CNetServerWorker& server, ENetPeer* peer);
CNetServerWorker& GetServer() { return m_Server; }
const CStr& GetGUID() const { return m_GUID; }
void SetGUID(const CStr& guid) { m_GUID = guid; }
const CStrW& GetUserName() const { return m_UserName; }
void SetUserName(const CStrW& name) { m_UserName = name; }
u32 GetHostID() const { return m_HostID; }
void SetHostID(u32 id) { m_HostID = id; }
u32 GetIPAddress() const;
/**
* Number of milliseconds since the latest packet of that client was received.
*/
u32 GetLastReceivedTime() const;
/**
* Average round trip time to the client.
*/
u32 GetMeanRTT() const;
/**
* Sends a disconnection notification to the client,
* and sends a NMT_CONNECTION_LOST message to the session FSM.
* The server will receive a disconnection notification after a while.
* The server will not receive any further messages sent via this session.
*/
void Disconnect(NetDisconnectReason reason);
/**
* Sends an unreliable disconnection notification to the client.
* The server will not receive any disconnection notification.
* The server will not receive any further messages sent via this session.
*/
void DisconnectNow(NetDisconnectReason reason);
/**
* Send a message to the client.
*/
bool SendMessage(const CNetMessage* message);
CNetFileTransferer& GetFileTransferer() { return m_FileTransferer; }
private:
CNetServerWorker& m_Server;
CNetFileTransferer m_FileTransferer;
ENetPeer* m_Peer;
CStr m_GUID;
CStrW m_UserName;
u32 m_HostID{0};
CStr m_Password;
};
#endif // NETSESSION_H
-1
View File
@@ -23,7 +23,6 @@
#include "lib/posix/posix_types.h"
#include "lib/timer.h"
#include "network/NetMessage.h"
#include "network/NetSession.h"
#include "ps/CLogger.h"
#include "ps/CStr.h"
+6 -1
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@@ -27,6 +27,11 @@
#include <type_traits>
#include <vector>
/**
* Report the peer if we didn't receive a packet after this time (milliseconds).
*/
inline constexpr u32 NETWORK_WARNING_TIMEOUT{2000};
struct HandshakeError
{
std::string componentType;
+1 -1
View File
@@ -33,7 +33,7 @@
#include "network/NetMessage.h"
#include "network/NetProtocol.h"
#include "network/NetServerTurnManager.h"
#include "network/NetSession.h"
#include "network/NetServerSession.h"
#include "network/NetStats.h"
#include "network/StunClient.h"
#include "ps/CLogger.h"
+75
View File
@@ -0,0 +1,75 @@
/* Copyright (C) 2026 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#include "precompiled.h"
#include "NetServerSession.h"
#include "lib/external_libraries/enet.h"
#include "network/NetMessage.h"
#include "network/NetMessages.h"
#include "network/NetServer.h"
#include "ps/CLogger.h"
CNetServerSession::CNetServerSession(CNetServerWorker& server, ENetPeer* peer) :
m_Server(server), m_FileTransferer(*this), m_Peer(peer)
{
}
u32 CNetServerSession::GetIPAddress() const
{
return m_Peer->address.host;
}
u32 CNetServerSession::GetLastReceivedTime() const
{
if (!m_Peer)
return 0;
return enet_time_get() - m_Peer->lastReceiveTime;
}
u32 CNetServerSession::GetMeanRTT() const
{
if (!m_Peer)
return 0;
return m_Peer->roundTripTime;
}
void CNetServerSession::Disconnect(NetDisconnectReason reason)
{
if (reason == NDR_UNKNOWN)
LOGWARNING("Disconnecting client without communicating the disconnect reason!");
Update((uint)NMT_CONNECTION_LOST, NULL);
enet_peer_disconnect(m_Peer, static_cast<enet_uint32>(reason));
}
void CNetServerSession::DisconnectNow(NetDisconnectReason reason)
{
if (reason == NDR_UNKNOWN)
LOGWARNING("Disconnecting client without communicating the disconnect reason!");
enet_peer_disconnect_now(m_Peer, static_cast<enet_uint32>(reason));
}
bool CNetServerSession::SendMessage(const CNetMessage* message)
{
return m_Server.SendMessage(m_Peer, message);
}
+103
View File
@@ -0,0 +1,103 @@
/* Copyright (C) 2026 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* 0 A.D. is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef NET_SERVER_SESSION_H
#define NET_SERVER_SESSION_H
#include "network/FSM.h"
#include "network/NetFileTransfer.h"
#include "network/NetHost.h"
#include "ps/CStr.h"
class CNetServerWorker;
typedef struct _ENetPeer ENetPeer;
/**
* The server's end of a network session.
* Represents an abstraction of the state of the client, storing all the per-client data
* needed by the server.
*
* Thread-safety:
* - This is constructed and used by CNetServerWorker in the network server thread.
*/
class CNetServerSession : public CFsm<CNetServerSession, CNetMessage*>
{
NONCOPYABLE(CNetServerSession);
public:
CNetServerSession(CNetServerWorker& server, ENetPeer* peer);
CNetServerWorker& GetServer() { return m_Server; }
const CStr& GetGUID() const { return m_GUID; }
void SetGUID(const CStr& guid) { m_GUID = guid; }
const CStrW& GetUserName() const { return m_UserName; }
void SetUserName(const CStrW& name) { m_UserName = name; }
u32 GetHostID() const { return m_HostID; }
void SetHostID(u32 id) { m_HostID = id; }
u32 GetIPAddress() const;
/**
* Number of milliseconds since the latest packet of that client was received.
*/
u32 GetLastReceivedTime() const;
/**
* Average round trip time to the client.
*/
u32 GetMeanRTT() const;
/**
* Sends a disconnection notification to the client,
* and sends a NMT_CONNECTION_LOST message to the session FSM.
* The server will receive a disconnection notification after a while.
* The server will not receive any further messages sent via this session.
*/
void Disconnect(NetDisconnectReason reason);
/**
* Sends an unreliable disconnection notification to the client.
* The server will not receive any disconnection notification.
* The server will not receive any further messages sent via this session.
*/
void DisconnectNow(NetDisconnectReason reason);
/**
* Send a message to the client.
*/
bool SendMessage(const CNetMessage* message);
CNetFileTransferer& GetFileTransferer() { return m_FileTransferer; }
private:
CNetServerWorker& m_Server;
CNetFileTransferer m_FileTransferer;
ENetPeer* m_Peer;
CStr m_GUID;
CStrW m_UserName;
u32 m_HostID{0};
CStr m_Password;
};
#endif // NET_SERVER_SESSION_H
+2 -2
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2025 Wildfire Games.
/* Copyright (C) 2026 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@@ -24,7 +24,7 @@
#include "network/NetHost.h"
#include "network/NetMessage.h"
#include "network/NetServer.h"
#include "network/NetSession.h"
#include "network/NetServerSession.h"
#include "ps/CLogger.h"
#include "ps/ConfigDB.h"
#include "simulation2/system/TurnManager.h"
-1
View File
@@ -19,7 +19,6 @@
#include "network/NetFileTransfer.h"
#include "network/NetMessage.h"
#include "network/NetSession.h"
#include <cstddef>
#include <functional>